Integrated resource model related changes with eclipse plug-ins.
[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 import java.util.Vector;
23
24 import oic.simulator.serviceprovider.utils.Constants;
25 import oic.simulator.serviceprovider.utils.Utility;
26
27 import org.oic.simulator.AttributeProperty;
28 import org.oic.simulator.AttributeValue;
29 import org.oic.simulator.AttributeValue.ValueType;
30 import org.oic.simulator.BooleanProperty;
31 import org.oic.simulator.DoubleProperty;
32 import org.oic.simulator.IntegerProperty;
33 import org.oic.simulator.SimulatorResourceAttribute;
34 import org.oic.simulator.StringProperty;
35
36 public class AttributeHelper {
37     private String      attributeName;
38     private String      attributeType;
39     private String      min;
40     private String      max;
41     private Set<String> allowedValues;
42     private String      attributeDflValue;
43
44     public enum ValidValuesType {
45         RANGE, VALUESET, UNKNOWN
46     }
47
48     private ValidValuesType validValuesType;
49
50     public AttributeHelper() {
51         allowedValues = new HashSet<String>();
52         setValidValuesType(ValidValuesType.UNKNOWN);
53         min = max = "";
54     }
55
56     public AttributeHelper clone() {
57         AttributeHelper att = new AttributeHelper();
58         att.setAttributeName(attributeName);
59         att.setAttributeType(attributeType);
60         att.setMin(min);
61         att.setMax(max);
62         if (null != allowedValues) {
63             Set<String> values = new HashSet<String>();
64             values.addAll(allowedValues);
65             att.setAllowedValues(values);
66         }
67         att.setAttributeDflValue(attributeDflValue);
68         att.setValidValuesType(validValuesType);
69         return att;
70     }
71
72     public String toString() {
73         return attributeName + "," + attributeType + "," + attributeDflValue
74                 + "," + validValuesType + ",(" + min + max + "),("
75                 + allowedValues + ")";
76     }
77
78     public String getAttributeName() {
79         return attributeName;
80     }
81
82     public void setAttributeName(String attributeName) {
83         this.attributeName = attributeName;
84     }
85
86     public String getAttributeType() {
87         return attributeType;
88     }
89
90     public void setAttributeType(String attributeType) {
91         this.attributeType = attributeType;
92     }
93
94     public String getMin() {
95         return min;
96     }
97
98     public void setMin(String min) {
99         this.min = min;
100     }
101
102     public String getMax() {
103         return max;
104     }
105
106     public void setMax(String max) {
107         this.max = max;
108     }
109
110     public Set<String> getAllowedValues() {
111         return allowedValues;
112     }
113
114     public void setAllowedValues(Set<String> allowedValues) {
115         this.allowedValues = allowedValues;
116     }
117
118     public void addValueToAllowedValues(String value) {
119         if (null != value && value.trim().length() > 0) {
120             if (null == allowedValues) {
121                 allowedValues = new HashSet<String>();
122             }
123             allowedValues.add(value);
124         }
125     }
126
127     public void removeValueFromAllowedValues(String value) {
128         if (null != allowedValues && null != value && value.trim().length() > 0) {
129             allowedValues.remove(value);
130         }
131     }
132
133     public void removeAllCustomValues() {
134         allowedValues.clear();
135     }
136
137     public String getAttributeDflValue() {
138         return attributeDflValue;
139     }
140
141     public void setAttributeDflValue(String attributeDflValue) {
142         this.attributeDflValue = attributeDflValue;
143     }
144
145     public ValidValuesType getValidValuesType() {
146         return validValuesType;
147     }
148
149     public void setValidValuesType(ValidValuesType validValuesType) {
150         this.validValuesType = validValuesType;
151     }
152
153     public boolean isValueValid(String value, String type) {
154         if (null == value || value.trim().length() < 1 || null == type
155                 || type.trim().length() < 1)
156             return false;
157         if (type.equalsIgnoreCase(Constants.INT)) {
158             return isIntValue(value);
159         } else if (type.equalsIgnoreCase(Constants.DOUBLE)) {
160             return isDoubleValue(value);
161         } else if (type.equalsIgnoreCase(Constants.BOOL)) {
162             return isBoolValue(value);
163         }
164         return true;
165     }
166
167     public boolean isBoolValue(String value) {
168         boolean isBool = false;
169         if (null != value
170                 && (value.equalsIgnoreCase("true") || value
171                         .equalsIgnoreCase("false"))) {
172             isBool = true;
173         }
174         return isBool;
175     }
176
177     public boolean isIntValue(String value) {
178         boolean isInt = true;
179         try {
180             Integer.parseInt(value);
181         } catch (Exception e) {
182             isInt = false;
183         }
184         return isInt;
185     }
186
187     public boolean isDoubleValue(String value) {
188         boolean isDouble = true;
189         try {
190             Double.parseDouble(value);
191         } catch (Exception e) {
192             isDouble = false;
193         }
194         return isDouble;
195     }
196
197     public boolean isRangeValid(String minStr, String maxStr, String type) {
198         if (null == type || type.trim().isEmpty()) {
199             return false;
200         }
201         boolean result = true;
202         if (type.equals(Constants.INT)) {
203             int min, max;
204             try {
205                 min = Integer.parseInt(minStr);
206                 max = Integer.parseInt(maxStr);
207                 if (max <= min) {
208                     result = false;
209                 }
210             } catch (Exception e) {
211                 result = false;
212             }
213         } else if (type.equals(Constants.DOUBLE)) {
214             double min, max;
215             try {
216                 min = Double.parseDouble(minStr);
217                 max = Double.parseDouble(maxStr);
218                 if (max <= min) {
219                     result = false;
220                 }
221             } catch (Exception e) {
222                 result = false;
223             }
224         }
225         return result;
226     }
227
228     public boolean isDefaultValueValid(String value) {
229         if (null == attributeType || attributeType.trim().isEmpty()) {
230             return false;
231         }
232         boolean result = true;
233         if (attributeType.equals(Constants.STRING)) {
234             if (validValuesType == ValidValuesType.VALUESET
235                     && null != allowedValues) {
236                 result = allowedValues.contains(value);
237             } else {
238                 result = true;
239             }
240         } else if (attributeType.equals(Constants.BOOL)) {
241             String val = value.toLowerCase();
242             if (!(val.equals("true") || val.equals("false"))) {
243                 result = false;
244             }
245         } else {
246             if (validValuesType == ValidValuesType.RANGE) {
247                 if (attributeType.equals(Constants.INT)) {
248                     int min, max, dflValue;
249                     try {
250                         min = Integer.parseInt(this.min);
251                         max = Integer.parseInt(this.max);
252                         dflValue = Integer.parseInt(value);
253                         if (dflValue < min || dflValue > max) {
254                             result = false;
255                         }
256                     } catch (Exception e) {
257                         result = false;
258                     }
259                 } else if (attributeType.equals(Constants.DOUBLE)) {
260                     double min, max, dflValue;
261                     try {
262                         min = Double.parseDouble(this.min);
263                         max = Double.parseDouble(this.max);
264                         dflValue = Double.parseDouble(value);
265                         boolean found = false;
266                         for (double val = min; val <= max; val += 1.0) {
267                             if (dflValue == val) {
268                                 found = true;
269                                 break;
270                             }
271                         }
272                         if (!found) {
273                             result = false;
274                         }
275                     } catch (Exception e) {
276                         result = false;
277                     }
278                 }
279             } else if (validValuesType == ValidValuesType.VALUESET
280                     && null != allowedValues && !allowedValues.isEmpty()) {
281                 boolean found = false;
282                 if (attributeType.equals(Constants.INT)) {
283                     Iterator<String> itr = allowedValues.iterator();
284                     while (itr.hasNext()) {
285                         String val = itr.next();
286                         int v1 = Integer.parseInt(val);
287                         int v2 = Integer.parseInt(value);
288                         if (v1 == v2) {
289                             found = true;
290                             break;
291                         }
292                     }
293                 } else {
294                     Iterator<String> itr = allowedValues.iterator();
295                     while (itr.hasNext()) {
296                         String val = itr.next();
297                         double v1 = Double.parseDouble(val);
298                         double v2 = Double.parseDouble(value);
299                         if (v1 == v2) {
300                             found = true;
301                             break;
302                         }
303                     }
304                 }
305                 if (!found) {
306                     result = false;
307                 }
308             }
309         }
310         return result;
311     }
312
313     public void setAllowedValuesByArray(String[] cusItems) {
314         if (null == cusItems) {
315             return;
316         }
317         for (String item : cusItems) {
318             addValueToAllowedValues(item);
319         }
320     }
321
322     public boolean isAllowedValueExist(String[] items, String value) {
323         if (null == items || items.length < 1 || null == value
324                 || value.isEmpty())
325             return false;
326         for (String item : items) {
327             if (value.equalsIgnoreCase(item)) {
328                 return true;
329             }
330         }
331         return false;
332     }
333
334     public SimulatorResourceAttribute convertToSimulatorResourceAttribute() {
335         AttributeValue attValue = null;
336         AttributeProperty attProperty = null;
337         SimulatorResourceAttribute simResAtt;
338
339         ValueType valueType = Utility.getAttributeTypeEnum(attributeType);
340         switch (valueType) {
341             case INTEGER:
342                 IntegerProperty.Builder intPropertyBuilder = new IntegerProperty.Builder();
343                 attValue = new AttributeValue(
344                         Integer.parseInt(attributeDflValue));
345
346                 // Set default value.
347                 intPropertyBuilder.setDefaultValue(Integer
348                         .parseInt(attributeDflValue));
349
350                 if (validValuesType == ValidValuesType.VALUESET) {
351                     // Set allowed values.
352                     intPropertyBuilder.setValues(Utility
353                             .convertSetToArrayInt(Utility
354                                     .convertSetStringToSetObject(allowedValues,
355                                             valueType)));
356                 } else if (validValuesType == ValidValuesType.RANGE) {
357                     // Set Range.
358                     intPropertyBuilder.setRange(Integer.parseInt(min),
359                             Integer.parseInt(max));
360                 }
361                 attProperty = intPropertyBuilder.build();
362                 break;
363             case DOUBLE:
364                 DoubleProperty.Builder dblPropertyBuilder = new DoubleProperty.Builder();
365                 attValue = new AttributeValue(
366                         Double.parseDouble(attributeDflValue));
367
368                 // Set default value.
369                 dblPropertyBuilder.setDefaultValue(Double
370                         .parseDouble(attributeDflValue));
371
372                 if (validValuesType == ValidValuesType.VALUESET) {
373                     // Set allowed values.
374                     dblPropertyBuilder.setValues(Utility
375                             .convertSetToArrayDouble(Utility
376                                     .convertSetStringToSetObject(allowedValues,
377                                             valueType)));
378                 } else if (validValuesType == ValidValuesType.RANGE) {
379                     // Set Range.
380                     dblPropertyBuilder.setRange(Double.parseDouble(min),
381                             Double.parseDouble(max));
382                 }
383                 attProperty = dblPropertyBuilder.build();
384                 break;
385             case BOOLEAN:
386                 attValue = new AttributeValue(
387                         Boolean.parseBoolean(attributeDflValue));
388                 BooleanProperty.Builder boolPropertyBuilder = new BooleanProperty.Builder();
389                 // Set Default Value.
390                 boolPropertyBuilder.setDefaultValue(Boolean
391                         .parseBoolean(attributeDflValue));
392
393                 attProperty = boolPropertyBuilder.build();
394                 break;
395             case STRING:
396                 attValue = new AttributeValue(attributeDflValue);
397                 StringProperty.Builder stringPropertyBuilder = new StringProperty.Builder();
398
399                 // Set Default Value.
400                 stringPropertyBuilder.setDefaultValue(attributeDflValue);
401
402                 if (validValuesType == ValidValuesType.VALUESET) {
403                     // Set Allowed Values.
404                     stringPropertyBuilder.setValues(Utility
405                             .convertSetToArrayString(Utility
406                                     .convertSetStringToSetObject(allowedValues,
407                                             valueType)));
408                 }
409                 attProperty = stringPropertyBuilder.build();
410                 break;
411             default:
412                 break;
413         }
414         simResAtt = new SimulatorResourceAttribute(attributeName, attValue,
415                 attProperty);
416         return simResAtt;
417     }
418 }