73499cf57ba3d27fd07c755f14b08e39545ed2f0
[public/netxms.git] / src / java / client / netxms-client / src / main / java / org / netxms / client / datacollection / TableThreshold.java
1 /**
2 * NetXMS - open source network management system
3 * Copyright (C) 2003-2013 Victor Kirhenshtein
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 package org.netxms.client.datacollection;
20
21 import java.util.ArrayList;
22 import java.util.List;
23 import org.netxms.base.NXCPMessage;
24
25 /**
26 * Threshold definition for table DCI
27 */
28 public class TableThreshold
29 {
30 private static final String[] OPERATIONS = { "<", "<=", "==", ">=", ">", "!=", "like", "!like" };
31
32 private long id;
33 private int activationEvent;
34 private int deactivationEvent;
35 private List<List<TableCondition>> conditions;
36 private long nextVarId;
37
38 /**
39 * Create new empty threshold
40 */
41 public TableThreshold()
42 {
43 id = 0;
44 activationEvent = 69;
45 deactivationEvent = 70;
46 conditions = new ArrayList<List<TableCondition>>(0);
47 }
48
49 /**
50 * Copy constructor
51 *
52 * @param src
53 */
54 public TableThreshold(TableThreshold src)
55 {
56 id = src.id;
57 activationEvent = src.activationEvent;
58 deactivationEvent = src.deactivationEvent;
59 conditions = new ArrayList<List<TableCondition>>(src.conditions.size());
60 for(List<TableCondition> sl : src.conditions)
61 {
62 List<TableCondition> dl = new ArrayList<TableCondition>(sl.size());
63 for(TableCondition c : sl)
64 dl.add(new TableCondition(c));
65 conditions.add(dl);
66 }
67 }
68
69 /**
70 * Create from NXCP message
71 *
72 * @param msg
73 * @param baseId
74 */
75 protected TableThreshold(NXCPMessage msg, long baseId)
76 {
77 long varId = baseId;
78 id = msg.getFieldAsInt64(varId++);
79 activationEvent = msg.getFieldAsInt32(varId++);
80 deactivationEvent = msg.getFieldAsInt32(varId++);
81
82 int groupCount = msg.getFieldAsInt32(varId++);
83 conditions = new ArrayList<List<TableCondition>>(groupCount);
84 for(int i = 0; i < groupCount; i++)
85 {
86 int condCount = msg.getFieldAsInt32(varId++);
87 List<TableCondition> list = new ArrayList<TableCondition>(condCount);
88 for(int j = 0; j < condCount; j++)
89 {
90 list.add(new TableCondition(msg, varId));
91 varId += 3;
92 }
93 conditions.add(list);
94 }
95 nextVarId = varId;
96 }
97
98 /**
99 * Fill NXCP message with threshold data
100 *
101 * @param msg
102 * @param baseId
103 * @return
104 */
105 protected long fillMessage(NXCPMessage msg, long baseId)
106 {
107 long varId = baseId;
108
109 msg.setFieldInt32(varId++, (int)id);
110 msg.setFieldInt32(varId++, activationEvent);
111 msg.setFieldInt32(varId++, deactivationEvent);
112 msg.setFieldInt32(varId++, conditions.size());
113 for(List<TableCondition> l : conditions)
114 {
115 msg.setFieldInt32(varId++, l.size());
116 for(TableCondition c : l)
117 {
118 msg.setField(varId++, c.getColumn());
119 msg.setFieldInt16(varId++, c.getOperation());
120 msg.setField(varId++, c.getValue());
121 }
122 }
123
124 return varId;
125 }
126
127 /**
128 * Get threshold condition as text
129 *
130 * @return
131 */
132 public String getConditionAsText()
133 {
134 StringBuilder sb = new StringBuilder();
135 for(List<TableCondition> group : conditions)
136 {
137 if (sb.length() > 0)
138 sb.append(" OR ");
139 if (conditions.size() > 1)
140 sb.append('(');
141 for(TableCondition cond : group)
142 {
143 if (cond != group.get(0))
144 sb.append(" AND ");
145 sb.append(cond.getColumn());
146 sb.append(' ');
147 try
148 {
149 sb.append(OPERATIONS[cond.getOperation()]);
150 }
151 catch(ArrayIndexOutOfBoundsException e)
152 {
153 sb.append('?');
154 }
155 sb.append(' ');
156 sb.append(cond.getValue());
157 }
158 if (conditions.size() > 1)
159 sb.append(')');
160 }
161 return sb.toString();
162 }
163
164 /**
165 * @return the nextVarId
166 */
167 public long getNextVarId()
168 {
169 return nextVarId;
170 }
171
172 /**
173 * @return the activationEvent
174 */
175 public int getActivationEvent()
176 {
177 return activationEvent;
178 }
179
180 /**
181 * @param activationEvent the activationEvent to set
182 */
183 public void setActivationEvent(int activationEvent)
184 {
185 this.activationEvent = activationEvent;
186 }
187
188 /**
189 * @return the deactivationEvent
190 */
191 public int getDeactivationEvent()
192 {
193 return deactivationEvent;
194 }
195
196 /**
197 * @param deactivationEvent the deactivationEvent to set
198 */
199 public void setDeactivationEvent(int deactivationEvent)
200 {
201 this.deactivationEvent = deactivationEvent;
202 }
203
204 /**
205 * @return the conditions
206 */
207 public List<List<TableCondition>> getConditions()
208 {
209 return conditions;
210 }
211
212 /**
213 * @param conditions the conditions to set
214 */
215 public void setConditions(List<List<TableCondition>> conditions)
216 {
217 this.conditions = conditions;
218 }
219
220 /**
221 * @return the id
222 */
223 public long getId()
224 {
225 return id;
226 }
227 }