Merge branch 'master' into simulator.
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ServiceProviderPlugin / src / oic / simulator / serviceprovider / model / AttributeHelper.java
1 /*
2  * Copyright 2015 Samsung Electronics All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package oic.simulator.serviceprovider.model;
18
19 import java.util.HashSet;
20 import java.util.Iterator;
21 import java.util.Set;
22
23 import oic.simulator.serviceprovider.utils.Constants;
24 import oic.simulator.serviceprovider.utils.Utility;
25
26 import org.oic.simulator.AttributeProperty;
27 import org.oic.simulator.AttributeProperty.Type;
28 import org.oic.simulator.AttributeValue;
29 import org.oic.simulator.AttributeValue.ValueType;
30 import org.oic.simulator.SimulatorResourceAttribute;
31
32 public class AttributeHelper {
33     private String      attributeName;
34     private String      attributeType;
35     private String      min;
36     private String      max;
37     private Set<String> allowedValues;
38     private String      attributeDflValue;
39
40     private Type        validValuesType;
41
42     public AttributeHelper() {
43         allowedValues = new HashSet<String>();
44         setValidValuesType(Type.UNKNOWN);
45         min = max = "";
46     }
47
48     public AttributeHelper clone() {
49         AttributeHelper att = new AttributeHelper();
50         att.setAttributeName(attributeName);
51         att.setAttributeType(attributeType);
52         att.setMin(min);
53         att.setMax(max);
54         if (null != allowedValues) {
55             Set<String> values = new HashSet<String>();
56             values.addAll(allowedValues);
57             att.setAllowedValues(values);
58         }
59         att.setAttributeDflValue(attributeDflValue);
60         att.setValidValuesType(validValuesType);
61         return att;
62     }
63
64     public String toString() {
65         return attributeName + "," + attributeType + "," + attributeDflValue
66                 + "," + validValuesType + ",(" + min + max + "),("
67                 + allowedValues + ")";
68     }
69
70     public String getAttributeName() {
71         return attributeName;
72     }
73
74     public void setAttributeName(String attributeName) {
75         this.attributeName = attributeName;
76     }
77
78     public String getAttributeType() {
79         return attributeType;
80     }
81
82     public void setAttributeType(String attributeType) {
83         this.attributeType = attributeType;
84     }
85
86     public String getMin() {
87         return min;
88     }
89
90     public void setMin(String min) {
91         this.min = min;
92     }
93
94     public String getMax() {
95         return max;
96     }
97
98     public void setMax(String max) {
99         this.max = max;
100     }
101
102     public Set<String> getAllowedValues() {
103         return allowedValues;
104     }
105
106     public void setAllowedValues(Set<String> allowedValues) {
107         this.allowedValues = allowedValues;
108     }
109
110     public void addValueToAllowedValues(String value) {
111         if (null != value && value.trim().length() > 0) {
112             if (null == allowedValues) {
113                 allowedValues = new HashSet<String>();
114             }
115             allowedValues.add(value);
116         }
117     }
118
119     public void removeValueFromAllowedValues(String value) {
120         if (null != allowedValues && null != value && value.trim().length() > 0) {
121             allowedValues.remove(value);
122         }
123     }
124
125     public void removeAllCustomValues() {
126         allowedValues.clear();
127     }
128
129     public String getAttributeDflValue() {
130         return attributeDflValue;
131     }
132
133     public void setAttributeDflValue(String attributeDflValue) {
134         this.attributeDflValue = attributeDflValue;
135     }
136
137     public Type getValidValuesType() {
138         return validValuesType;
139     }
140
141     public void setValidValuesType(Type validValuesType) {
142         this.validValuesType = validValuesType;
143     }
144
145     public boolean isValueValid(String value, String type) {
146         if (null == value || value.trim().length() < 1 || null == type
147                 || type.trim().length() < 1)
148             return false;
149         if (type.equalsIgnoreCase(Constants.INT)) {
150             return isIntValue(value);
151         } else if (type.equalsIgnoreCase(Constants.DOUBLE)) {
152             return isDoubleValue(value);
153         } else if (type.equalsIgnoreCase(Constants.BOOL)) {
154             return isBoolValue(value);
155         }
156         return true;
157     }
158
159     public boolean isBoolValue(String value) {
160         boolean isBool = false;
161         if (null != value
162                 && (value.equalsIgnoreCase("true") || value
163                         .equalsIgnoreCase("false"))) {
164             isBool = true;
165         }
166         return isBool;
167     }
168
169     public boolean isIntValue(String value) {
170         boolean isInt = true;
171         try {
172             Integer.parseInt(value);
173         } catch (Exception e) {
174             isInt = false;
175         }
176         return isInt;
177     }
178
179     public boolean isDoubleValue(String value) {
180         boolean isDouble = true;
181         try {
182             Double.parseDouble(value);
183         } catch (Exception e) {
184             isDouble = false;
185         }
186         return isDouble;
187     }
188
189     public boolean isRangeValid(String minStr, String maxStr, String type) {
190         if (null == type || type.trim().isEmpty()) {
191             return false;
192         }
193         boolean result = true;
194         if (type.equals(Constants.INT)) {
195             int min, max;
196             try {
197                 min = Integer.parseInt(minStr);
198                 max = Integer.parseInt(maxStr);
199                 if (max <= min) {
200                     result = false;
201                 }
202             } catch (Exception e) {
203                 result = false;
204             }
205         } else if (type.equals(Constants.DOUBLE)) {
206             double min, max;
207             try {
208                 min = Double.parseDouble(minStr);
209                 max = Double.parseDouble(maxStr);
210                 if (max <= min) {
211                     result = false;
212                 }
213             } catch (Exception e) {
214                 result = false;
215             }
216         }
217         return result;
218     }
219
220     public boolean isDefaultValueValid(String value) {
221         if (null == attributeType || attributeType.trim().isEmpty()) {
222             return false;
223         }
224         boolean result = true;
225         if (attributeType.equals(Constants.STRING)) {
226             if (validValuesType == Type.VALUESET && null != allowedValues) {
227                 result = allowedValues.contains(value);
228             } else {
229                 result = true;
230             }
231         } else if (attributeType.equals(Constants.BOOL)) {
232             String val = value.toLowerCase();
233             if (!(val.equals("true") || val.equals("false"))) {
234                 result = false;
235             }
236         } else {
237             if (validValuesType == Type.RANGE) {
238                 if (attributeType.equals(Constants.INT)) {
239                     int min, max, dflValue;
240                     try {
241                         min = Integer.parseInt(this.min);
242                         max = Integer.parseInt(this.max);
243                         dflValue = Integer.parseInt(value);
244                         if (dflValue < min || dflValue > max) {
245                             result = false;
246                         }
247                     } catch (Exception e) {
248                         result = false;
249                     }
250                 } else if (attributeType.equals(Constants.DOUBLE)) {
251                     double min, max, dflValue;
252                     try {
253                         min = Double.parseDouble(this.min);
254                         max = Double.parseDouble(this.max);
255                         dflValue = Double.parseDouble(value);
256                         boolean found = false;
257                         for (double val = min; val <= max; val += 1.0) {
258                             if (dflValue == val) {
259                                 found = true;
260                                 break;
261                             }
262                         }
263                         if (!found) {
264                             result = false;
265                         }
266                     } catch (Exception e) {
267                         result = false;
268                     }
269                 }
270             } else if (validValuesType == Type.VALUESET
271                     && null != allowedValues && !allowedValues.isEmpty()) {
272                 boolean found = false;
273                 if (attributeType.equals(Constants.INT)) {
274                     Iterator<String> itr = allowedValues.iterator();
275                     while (itr.hasNext()) {
276                         String val = itr.next();
277                         int v1 = Integer.parseInt(val);
278                         int v2 = Integer.parseInt(value);
279                         if (v1 == v2) {
280                             found = true;
281                             break;
282                         }
283                     }
284                 } else {
285                     Iterator<String> itr = allowedValues.iterator();
286                     while (itr.hasNext()) {
287                         String val = itr.next();
288                         double v1 = Double.parseDouble(val);
289                         double v2 = Double.parseDouble(value);
290                         if (v1 == v2) {
291                             found = true;
292                             break;
293                         }
294                     }
295                 }
296                 if (!found) {
297                     result = false;
298                 }
299             }
300         }
301         return result;
302     }
303
304     public void setAllowedValuesByArray(String[] cusItems) {
305         if (null == cusItems) {
306             return;
307         }
308         for (String item : cusItems) {
309             addValueToAllowedValues(item);
310         }
311     }
312
313     public boolean isAllowedValueExist(String[] items, String value) {
314         if (null == items || items.length < 1 || null == value
315                 || value.isEmpty())
316             return false;
317         for (String item : items) {
318             if (value.equalsIgnoreCase(item)) {
319                 return true;
320             }
321         }
322         return false;
323     }
324
325     public SimulatorResourceAttribute convertToSimulatorResourceAttribute() {
326         AttributeValue attValue = null;
327         AttributeProperty attProperty = null;
328         SimulatorResourceAttribute simResAtt;
329
330         ValueType valueType = Utility.getAttributeTypeEnum(attributeType);
331         switch (valueType) {
332             case INTEGER:
333                 attValue = new AttributeValue(
334                         Integer.parseInt(attributeDflValue));
335                 if (validValuesType == Type.VALUESET) {
336                     attProperty = new AttributeProperty(
337                             Utility.convertSetToArrayInt(Utility
338                                     .convertSetStringToSetObject(allowedValues,
339                                             valueType)));
340                 } else if (validValuesType == Type.RANGE) {
341                     attProperty = new AttributeProperty(
342                             Double.parseDouble(min), Double.parseDouble(max));
343                 } else {
344                     attProperty = null;
345                 }
346                 break;
347             case DOUBLE:
348                 attValue = new AttributeValue(
349                         Double.parseDouble(attributeDflValue));
350                 if (validValuesType == Type.VALUESET) {
351                     attProperty = new AttributeProperty(
352                             Utility.convertSetToArrayDouble(Utility
353                                     .convertSetStringToSetObject(allowedValues,
354                                             valueType)));
355                 } else if (validValuesType == Type.RANGE) {
356                     attProperty = new AttributeProperty(
357                             Double.parseDouble(min), Double.parseDouble(max));
358                 } else {
359                     attProperty = null;
360                 }
361                 break;
362             case BOOLEAN:
363                 attValue = new AttributeValue(
364                         Boolean.parseBoolean(attributeDflValue));
365                 boolean[] arr = { true, false };
366                 attProperty = new AttributeProperty(arr);
367                 break;
368             case STRING:
369                 attValue = new AttributeValue(attributeDflValue);
370                 if (validValuesType == Type.VALUESET) {
371                     attProperty = new AttributeProperty(
372                             Utility.convertSetToArrayString(Utility
373                                     .convertSetStringToSetObject(allowedValues,
374                                             valueType)));
375                 } else {
376                     attProperty = null;
377                 }
378                 break;
379             default:
380                 break;
381         }
382         simResAtt = new SimulatorResourceAttribute(attributeName, attValue,
383                 attProperty);
384         return simResAtt;
385     }
386 }