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