Add module holms-actions code
[holmes/common.git] / holmes-actions / src / main / java / org / openo / holmes / common / db / mapper / AlarmsCorrelationMapper.java
1 /**\r
2  * Copyright 2017 ZTE Corporation.\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 package org.openo.holmes.common.db.mapper;\r
17 \r
18 import java.sql.ResultSet;\r
19 import java.sql.SQLException;\r
20 import java.util.Date;\r
21 import java.util.Map;\r
22 \r
23 import org.jvnet.hk2.annotations.Service;\r
24 import org.openo.holmes.common.api.entity.AlarmsCorrelation;\r
25 import org.skife.jdbi.v2.StatementContext;\r
26 import org.skife.jdbi.v2.tweak.ResultSetMapper;\r
27 \r
28 @Service\r
29 public class AlarmsCorrelationMapper implements ResultSetMapper<AlarmsCorrelation> {\r
30 \r
31   private final static String RULE_ID = "ruleId";\r
32   private final static String RULE_INFO = "ruleInfo";\r
33   private final static String RESULT_TYPE = "resultType";\r
34   private final static String CREATE_TIME = "createTime";\r
35   private final static String PARENT_ALARM_ID = "parentAlarmId";\r
36   private final static String CHILD_ALARM_ID = "childAlarmId";\r
37   private final static String RESERVE1 = "reserve1";\r
38   private final static String RESERVE2 = "reserve2";\r
39   private final static String RESERVE3 = "reserve3";\r
40 \r
41   @Override\r
42   public AlarmsCorrelation map(int i, ResultSet resultSet, StatementContext statementContext)\r
43       throws SQLException {\r
44     AlarmsCorrelation aplusCorrelation = new AlarmsCorrelation();\r
45     aplusCorrelation.setRuleId(resultSet.getString(RULE_ID));\r
46     aplusCorrelation.setRuleInfo(resultSet.getString(RULE_INFO));\r
47     aplusCorrelation.setResultType(resultSet.getByte(RESULT_TYPE));\r
48     aplusCorrelation.setCreateTime(resultSet.getDate(CREATE_TIME));\r
49     aplusCorrelation.setParentAlarmId(resultSet.getLong(PARENT_ALARM_ID));\r
50     aplusCorrelation.setChildAlarmId(resultSet.getLong(CHILD_ALARM_ID));\r
51     aplusCorrelation.setReserve1(resultSet.getLong(RESERVE1));\r
52     aplusCorrelation.setReserve2(resultSet.getLong(RESERVE2));\r
53     aplusCorrelation.setReserve3(resultSet.getLong(RESERVE3));\r
54     return aplusCorrelation;\r
55   }\r
56 \r
57   public AlarmsCorrelation getAlarmCorrelationByMap(Map<String, Object> map) {\r
58     AlarmsCorrelation aplusCorrelation = new AlarmsCorrelation();\r
59     aplusCorrelation.setRuleId(getStringValue4Map(map, RULE_ID));\r
60     aplusCorrelation.setRuleInfo(getStringValue4Map(map, RULE_INFO));\r
61     aplusCorrelation.setResultType(getByteValue4Map(map, RESULT_TYPE, -1));\r
62     aplusCorrelation.setCreateTime((Date) map.get(CREATE_TIME));\r
63     aplusCorrelation.setParentAlarmId(getLongValue4Map(map, PARENT_ALARM_ID, -1));\r
64     aplusCorrelation.setChildAlarmId(getLongValue4Map(map, CHILD_ALARM_ID, -1));\r
65     aplusCorrelation.setReserve1(getLongValue4Map(map, RESERVE1, -1));\r
66     aplusCorrelation.setReserve2(getLongValue4Map(map, RESERVE2, -1));\r
67     aplusCorrelation.setReserve3(getLongValue4Map(map, RESERVE3, -1));\r
68     return aplusCorrelation;\r
69   }\r
70 \r
71   private String getStringValue4Map(Map<String, Object> map, String key) {\r
72     Object value = map.get(key);\r
73     return value == null ? "" : String.valueOf(value);\r
74   }\r
75 \r
76   private long getLongValue4Map(Map<String, Object> map, String key, long defaultValue) {\r
77     Object value = map.get(key);\r
78     return value == null ? defaultValue : Long.valueOf(String.valueOf(value));\r
79   }\r
80 \r
81   private byte getByteValue4Map(Map<String, Object> map, String key, int defaultValue) {\r
82     Object value = map.get(key);\r
83     return value == null ? Byte.valueOf(String.valueOf(defaultValue))\r
84         : Byte.valueOf(String.valueOf(value));\r
85   }\r
86 }\r