2 * Copyright 2015 Samsung Electronics All Rights Reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package oic.simulator.serviceprovider.model;
19 import java.util.HashSet;
22 import oic.simulator.serviceprovider.utils.Constants;
23 import oic.simulator.serviceprovider.utils.Utility;
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;
31 public class AttributeHelper {
32 private String attributeName;
33 private String attributeType;
36 private Set<String> allowedValues;
37 private String attributeDflValue;
39 private Type validValuesType;
41 public AttributeHelper() {
42 allowedValues = new HashSet<String>();
43 setValidValuesType(Type.UNKNOWN);
47 public AttributeHelper clone() {
48 AttributeHelper att = new AttributeHelper();
49 att.setAttributeName(attributeName);
50 att.setAttributeType(attributeType);
53 if (null != allowedValues) {
54 Set<String> values = new HashSet<String>();
55 values.addAll(allowedValues);
56 att.setAllowedValues(values);
58 att.setAttributeDflValue(attributeDflValue);
59 att.setValidValuesType(validValuesType);
63 public String toString() {
64 return attributeName + "," + attributeType + "," + attributeDflValue
65 + "," + validValuesType + ",(" + min + max + "),("
66 + allowedValues + ")";
69 public String getAttributeName() {
73 public void setAttributeName(String attributeName) {
74 this.attributeName = attributeName;
77 public String getAttributeType() {
81 public void setAttributeType(String attributeType) {
82 this.attributeType = attributeType;
85 public String getMin() {
89 public void setMin(String min) {
93 public String getMax() {
97 public void setMax(String max) {
101 public Set<String> getAllowedValues() {
102 return allowedValues;
105 public void setAllowedValues(Set<String> allowedValues) {
106 this.allowedValues = allowedValues;
109 public void addValueToAllowedValues(String value) {
110 if (null != value && value.trim().length() > 0) {
111 if (null == allowedValues) {
112 allowedValues = new HashSet<String>();
114 allowedValues.add(value);
118 public void removeValueFromAllowedValues(String value) {
119 if (null != allowedValues && null != value && value.trim().length() > 0) {
120 allowedValues.remove(value);
124 public void removeAllCustomValues() {
125 allowedValues.clear();
128 public String getAttributeDflValue() {
129 return attributeDflValue;
132 public void setAttributeDflValue(String attributeDflValue) {
133 this.attributeDflValue = attributeDflValue;
136 public Type getValidValuesType() {
137 return validValuesType;
140 public void setValidValuesType(Type validValuesType) {
141 this.validValuesType = validValuesType;
144 public boolean isValueValid(String value, String type) {
145 if (null == value || value.trim().length() < 1 || null == type
146 || type.trim().length() < 1)
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);
158 public boolean isBoolValue(String value) {
159 boolean isBool = false;
161 && (value.equalsIgnoreCase("true") || value
162 .equalsIgnoreCase("false"))) {
168 public boolean isIntValue(String value) {
169 boolean isInt = true;
171 Integer.parseInt(value);
172 } catch (Exception e) {
178 public boolean isDoubleValue(String value) {
179 boolean isDouble = true;
181 Double.parseDouble(value);
182 } catch (Exception e) {
188 public boolean isRangeValid(String minStr, String maxStr, String type) {
189 if (null == type || type.trim().isEmpty()) {
192 boolean result = true;
193 if (type.equals(Constants.INT)) {
196 min = Integer.parseInt(minStr);
197 max = Integer.parseInt(maxStr);
201 } catch (Exception e) {
204 } else if (type.equals(Constants.DOUBLE)) {
207 min = Double.parseDouble(minStr);
208 max = Double.parseDouble(maxStr);
212 } catch (Exception e) {
219 public boolean isDefaultValueValid(String value) {
220 if (null == attributeType || attributeType.trim().isEmpty()) {
223 boolean result = true;
224 if (attributeType.equals(Constants.STRING)) {
225 if (validValuesType == Type.VALUESET && null != allowedValues) {
226 result = allowedValues.contains(value);
230 } else if (attributeType.equals(Constants.BOOL)) {
231 String val = value.toLowerCase();
232 if (!(val.equals("true") || val.equals("false"))) {
236 if (validValuesType == Type.RANGE) {
237 if (attributeType.equals(Constants.INT)) {
238 int min, max, dflValue;
240 min = Integer.parseInt(this.min);
241 max = Integer.parseInt(this.max);
242 dflValue = Integer.parseInt(value);
243 if (dflValue < min || dflValue > max) {
246 } catch (Exception e) {
249 } else if (attributeType.equals(Constants.DOUBLE)) {
250 double min, max, dflValue;
252 min = Double.parseDouble(this.min);
253 max = Double.parseDouble(this.max);
254 dflValue = Double.parseDouble(value);
255 if (dflValue < min || dflValue > max) {
258 } catch (Exception e) {
262 } else if (validValuesType == Type.VALUESET
263 && null != allowedValues && !allowedValues.isEmpty()) {
264 result = allowedValues.contains(value);
270 public void setAllowedValuesByArray(String[] cusItems) {
271 if (null == cusItems) {
274 for (String item : cusItems) {
275 addValueToAllowedValues(item);
279 public boolean isAllowedValueExist(String[] items, String value) {
280 if (null == items || items.length < 1 || null == value
283 for (String item : items) {
284 if (value.equalsIgnoreCase(item)) {
291 public LocalResourceAttribute convertToLocalResourceAttribute() {
292 LocalResourceAttribute attribute = new LocalResourceAttribute();
294 // Initially disabling the automation
295 attribute.setAutomationInProgress(false);
297 // Assigning the default automation interval
299 .setAutomationUpdateInterval(Constants.DEFAULT_AUTOMATION_INTERVAL);
301 // Setting the default automation type
302 attribute.setAutomationType(Constants.DEFAULT_AUTOMATION_TYPE);
304 AttributeValue attValue = null;
305 AttributeProperty attProperty = null;
306 SimulatorResourceAttribute simResAtt;
308 ValueType valueType = Utility.getAttributeTypeEnum(attributeType);
311 attValue = new AttributeValue(
312 Integer.parseInt(attributeDflValue));
313 if (validValuesType == Type.VALUESET) {
314 attProperty = new AttributeProperty(
315 Utility.convertSetToArrayInt(Utility
316 .convertSetStringToSetObject(allowedValues,
318 } else if (validValuesType == Type.RANGE) {
319 attProperty = new AttributeProperty(
320 Double.parseDouble(min), Double.parseDouble(max));
325 attValue = new AttributeValue(
326 Double.parseDouble(attributeDflValue));
327 if (validValuesType == Type.VALUESET) {
328 attProperty = new AttributeProperty(
329 Utility.convertSetToArrayDouble(Utility
330 .convertSetStringToSetObject(allowedValues,
332 } else if (validValuesType == Type.RANGE) {
333 attProperty = new AttributeProperty(
334 Double.parseDouble(min), Double.parseDouble(max));
340 attValue = new AttributeValue(
341 Boolean.parseBoolean(attributeDflValue));
342 boolean[] arr = { true, false };
343 attProperty = new AttributeProperty(arr);
346 attValue = new AttributeValue(attributeDflValue);
347 if (validValuesType == Type.VALUESET) {
348 attProperty = new AttributeProperty(
349 Utility.convertSetToArrayString(Utility
350 .convertSetStringToSetObject(allowedValues,
359 simResAtt = new SimulatorResourceAttribute(attributeName, attValue,
361 attribute.setResourceAttributeRef(simResAtt);