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