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;
20 import java.util.TreeMap;
22 import org.oic.simulator.ArrayProperty;
23 import org.oic.simulator.AttributeProperty;
24 import org.oic.simulator.AttributeValue;
25 import org.oic.simulator.DoubleProperty;
26 import org.oic.simulator.IntegerProperty;
27 import org.oic.simulator.InvalidArgsException;
28 import org.oic.simulator.ModelProperty;
29 import org.oic.simulator.SimulatorResourceAttribute;
30 import org.oic.simulator.SimulatorResourceModel;
31 import org.oic.simulator.StringProperty;
32 import org.oic.simulator.server.SimulatorResource.AutoUpdateType;
34 import oic.simulator.serviceprovider.manager.UiListenerHandler;
35 import oic.simulator.serviceprovider.utils.AttributeValueStringConverter;
36 import oic.simulator.serviceprovider.utils.Constants;
38 public class AttributeElement {
39 private Object mParent = null;
40 private SimulatorResourceAttribute mAttribute = null;
41 private Map<String, AttributeElement> mChildAttributes = new TreeMap<String, AttributeElement>();
42 private boolean mAutoUpdateSupport = false;
43 private int mAutoUpdateId = -1;
44 private boolean mAutoUpdateState = false;
45 private int mAutoUpdateInterval = Constants.DEFAULT_AUTOMATION_INTERVAL;
46 private AutoUpdateType mAutoUpdateType = Constants.DEFAULT_AUTOMATION_TYPE;
47 private boolean mEditLock = false;
49 public AttributeElement(Object parent,
50 SimulatorResourceAttribute attribute, boolean autoUpdateSupport)
51 throws NullPointerException {
53 mAttribute = attribute;
54 mAutoUpdateSupport = autoUpdateSupport;
55 AttributeValue.TypeInfo typeInfo = attribute.value().typeInfo();
56 if (typeInfo.mType == AttributeValue.ValueType.RESOURCEMODEL) {
57 mAutoUpdateSupport = false;
58 SimulatorResourceModel resModel = (SimulatorResourceModel) attribute
60 ModelProperty modelProp = null;
61 if (null != attribute.property()) {
62 modelProp = attribute.property().asModel();
65 for (Map.Entry<String, AttributeValue> entry : resModel.get()
67 attName = entry.getKey();
68 AttributeProperty prop = null;
69 if (null != modelProp)
70 prop = modelProp.get(attName);
73 new AttributeElement(this,
74 new SimulatorResourceAttribute(attName, entry
75 .getValue(), prop), false));
77 } else if (typeInfo.mType == AttributeValue.ValueType.ARRAY) {
78 mAutoUpdateSupport = false;
79 if (typeInfo.mBaseType == AttributeValue.ValueType.RESOURCEMODEL) {
80 if (typeInfo.mDepth == 1) {
81 SimulatorResourceModel[] resModelArray = (SimulatorResourceModel[]) attribute
84 ArrayProperty arrProp = null;
85 ModelProperty modelProp = null;
86 if (null != attribute.property()) {
87 arrProp = attribute.property().asArray();
89 && null != arrProp.getElementProperty()) {
90 modelProp = arrProp.getElementProperty().asModel();
93 for (int i = 0; i < resModelArray.length; i++) {
94 SimulatorResourceAttribute indexAttribute = new SimulatorResourceAttribute(
95 "[" + Integer.toString(i) + "]",
96 new AttributeValue(resModelArray[i]), modelProp);
97 mChildAttributes.put("[" + Integer.toString(i) + "]",
98 new AttributeElement(this, indexAttribute,
101 } else if (typeInfo.mDepth == 2) {
102 SimulatorResourceModel[][] resModelArray = (SimulatorResourceModel[][]) attribute
105 ArrayProperty arrProp = null;
106 ArrayProperty arrChildPropLevel1 = null;
107 ModelProperty modelProp = null;
108 if (null != attribute.property()) {
109 arrProp = attribute.property().asArray();
111 && null != arrProp.getElementProperty()) {
112 arrChildPropLevel1 = arrProp.getElementProperty()
114 if (null != arrChildPropLevel1
115 && null != arrChildPropLevel1
116 .getElementProperty()) {
117 modelProp = arrProp.getElementProperty()
123 for (int i = 0; i < resModelArray.length; i++) {
124 SimulatorResourceAttribute indexAttribute = new SimulatorResourceAttribute(
125 "[" + Integer.toString(i) + "]",
126 new AttributeValue(resModelArray[i]), modelProp);
127 mChildAttributes.put("[" + Integer.toString(i) + "]",
128 new AttributeElement(this, indexAttribute,
131 } else if (typeInfo.mDepth == 3) {
132 SimulatorResourceModel[][][] resModelArray = (SimulatorResourceModel[][][]) attribute
135 ArrayProperty arrProp = null;
136 ArrayProperty arrChildPropLevel1 = null;
137 ArrayProperty arrChildPropLevel2 = null;
138 ModelProperty modelProp = null;
139 if (null != attribute.property()) {
140 arrProp = attribute.property().asArray();
142 && null != arrProp.getElementProperty()) {
143 arrChildPropLevel1 = arrProp.getElementProperty()
145 if (null != arrChildPropLevel1
146 && null != arrChildPropLevel1
147 .getElementProperty()) {
148 arrChildPropLevel2 = arrChildPropLevel1
149 .getElementProperty().asArray();
150 if (null != arrChildPropLevel2
151 && null != arrChildPropLevel2
152 .getElementProperty()) {
153 modelProp = arrChildPropLevel2
154 .getElementProperty().asModel();
160 for (int i = 0; i < resModelArray.length; i++) {
161 SimulatorResourceAttribute indexAttribute = new SimulatorResourceAttribute(
162 "[" + Integer.toString(i) + "]",
163 new AttributeValue(resModelArray[i]), modelProp);
164 mChildAttributes.put("[" + Integer.toString(i) + "]",
165 new AttributeElement(this, indexAttribute,
173 public Object getParent() {
177 public boolean hasChildren() {
178 if (mChildAttributes != null && mChildAttributes.size() > 0)
183 public Map<String, AttributeElement> getChildren() {
184 if (hasChildren() == true)
185 return mChildAttributes;
189 public SimulatorResourceAttribute getSimulatorResourceAttribute() {
193 public int getAutoUpdateId() {
194 return mAutoUpdateId;
197 public void setAutoUpdateId(int id) {
201 public boolean isAutoUpdateInProgress() {
202 return mAutoUpdateState;
205 public void setAutoUpdateState(boolean state) {
206 if (mAutoUpdateState != state) {
207 mAutoUpdateState = state;
209 UiListenerHandler.getInstance()
210 .attributeUpdatedUINotification(this);
214 public int getAutoUpdateInterval() {
215 return mAutoUpdateInterval;
218 public void setAutoUpdateInterval(int interval) {
219 mAutoUpdateInterval = interval;
222 public AutoUpdateType getAutoUpdateType() {
223 return mAutoUpdateType;
226 public void setAutoUpdateType(AutoUpdateType type) {
227 mAutoUpdateType = type;
230 public boolean isAutoUpdateSupport() {
231 return mAutoUpdateSupport;
234 public boolean isReadOnly() {
235 AttributeProperty prop = mAttribute.property();
240 if (prop.isInteger()) {
241 IntegerProperty intProperty = prop.asInteger();
242 return !(intProperty.hasRange() || intProperty.hasValues());
245 if (prop.isDouble()) {
246 DoubleProperty dblProperty = prop.asDouble();
247 return !(dblProperty.hasRange() || dblProperty.hasValues());
250 if (prop.isBoolean()) {
254 if (prop.isString()) {
255 StringProperty strProperty = prop.asString();
256 return !(strProperty.hasValues());
262 public synchronized boolean getEditLock() {
266 public synchronized void setEditLock(boolean editLock) {
267 this.mEditLock = editLock;
270 public void update(SimulatorResourceAttribute attribute) {
271 if (attribute == null)
274 AttributeValue.TypeInfo typeInfo = attribute.value().typeInfo();
275 if (typeInfo.mType == AttributeValue.ValueType.RESOURCEMODEL) {
276 SimulatorResourceModel resModel = (SimulatorResourceModel) attribute
279 for (Map.Entry<String, AttributeValue> entry : resModel.get()
281 attName = entry.getKey();
282 AttributeElement attributeElement = mChildAttributes
285 if (attributeElement != null) {
286 attributeElement.update(new SimulatorResourceAttribute(
287 attName, entry.getValue()));
289 // Get the property from the newly received attribute.
290 ModelProperty modelProp = null;
291 if (null != attribute.property()) {
292 modelProp = attribute.property().asModel();
295 AttributeProperty prop = null;
296 if (null != modelProp)
297 prop = modelProp.get(attName);
299 AttributeElement newAttribute = new AttributeElement(this,
300 new SimulatorResourceAttribute(attName,
301 entry.getValue(), prop), false);
303 // Update the attribute's value (Adding the new child
305 AttributeValue currentValue = mAttribute.value();
306 SimulatorResourceModel curModel = (SimulatorResourceModel) currentValue
309 curModel.set(attName, entry.getValue());
310 } catch (InvalidArgsException e) {
313 mChildAttributes.put(attName, newAttribute);
315 // Refresh UI to display the new attribute.
316 UiListenerHandler.getInstance()
317 .attributeAddedUINotification(newAttribute);
320 } else if (typeInfo.mType == AttributeValue.ValueType.ARRAY
321 && typeInfo.mBaseType == AttributeValue.ValueType.RESOURCEMODEL) {
322 if (typeInfo.mDepth == 1) {
323 SimulatorResourceModel[] resModelArray = (SimulatorResourceModel[]) attribute
326 ArrayProperty arrProp = null;
327 ModelProperty modelProp = null;
328 if (null != mAttribute.property()) {
329 arrProp = mAttribute.property().asArray();
330 if (null != arrProp && null != arrProp.getElementProperty()) {
331 modelProp = arrProp.getElementProperty().asModel();
335 mChildAttributes.clear();
337 SimulatorResourceModel[] newModelArray = new SimulatorResourceModel[resModelArray.length];
339 for (int i = 0; i < resModelArray.length; i++) {
340 SimulatorResourceAttribute indexAttribute = new SimulatorResourceAttribute(
341 "[" + Integer.toString(i) + "]",
342 new AttributeValue(resModelArray[i]), modelProp);
344 // Update the attribute's value (Adding the new child
346 AttributeElement newAttributeElement = new AttributeElement(
347 this, indexAttribute, false);
349 newModelArray[i] = resModelArray[i];
351 mChildAttributes.put("[" + Integer.toString(i) + "]",
352 newAttributeElement);
355 // Changing the value of the model array attribute. Property
357 mAttribute.setValue(new AttributeValue(newModelArray));
359 // Refresh UI to display the new attribute.
360 UiListenerHandler.getInstance().attributeAddedUINotification(
363 if (typeInfo.mDepth == 2) {
364 SimulatorResourceModel[][] resModelArray = (SimulatorResourceModel[][]) attribute
367 ArrayProperty arrProp = null;
368 ArrayProperty arrChildPropLevel1 = null;
369 ModelProperty modelProp = null;
370 if (null != mAttribute.property()) {
371 arrProp = mAttribute.property().asArray();
372 if (null != arrProp && null != arrProp.getElementProperty()) {
373 arrChildPropLevel1 = arrProp.getElementProperty()
375 if (null != arrChildPropLevel1
376 && null != arrChildPropLevel1
377 .getElementProperty()) {
378 modelProp = arrProp.getElementProperty().asModel();
383 for (int i = 0; i < resModelArray.length; i++) {
384 SimulatorResourceAttribute indexAttribute = new SimulatorResourceAttribute(
385 "[" + Integer.toString(i) + "]",
386 new AttributeValue(resModelArray[i]));
387 AttributeElement attributeElement = mChildAttributes
388 .get("[" + Integer.toString(i) + "]");
389 if (attributeElement != null) {
390 attributeElement.update(indexAttribute);
392 indexAttribute.setProperty(modelProp);
394 // Update the attribute's value (Adding the new child
396 AttributeElement newAttribute = new AttributeElement(
397 this, indexAttribute, false);
398 AttributeValue currentValue = mAttribute.value();
400 SimulatorResourceModel[][] curModelArray = (SimulatorResourceModel[][]) currentValue
402 SimulatorResourceModel[][] newModelArray = new SimulatorResourceModel[curModelArray.length + 1][];
405 for (SimulatorResourceModel[] modelArray : curModelArray) {
406 newModelArray[j++] = modelArray;
408 newModelArray[j] = resModelArray[i];
410 mAttribute.setValue(new AttributeValue(newModelArray));
411 mChildAttributes.put("[" + Integer.toString(i) + "]",
414 // Refresh UI to display the new attribute.
415 UiListenerHandler.getInstance()
416 .attributeAddedUINotification(newAttribute);
420 if (typeInfo.mDepth == 3) {
421 SimulatorResourceModel[][][] resModelArray = (SimulatorResourceModel[][][]) attribute
424 ArrayProperty arrProp = null;
425 ArrayProperty arrChildPropLevel1 = null;
426 ArrayProperty arrChildPropLevel2 = null;
427 ModelProperty modelProp = null;
428 if (null != mAttribute.property()) {
429 arrProp = mAttribute.property().asArray();
430 if (null != arrProp && null != arrProp.getElementProperty()) {
431 arrChildPropLevel1 = arrProp.getElementProperty()
433 if (null != arrChildPropLevel1
434 && null != arrChildPropLevel1
435 .getElementProperty()) {
436 arrChildPropLevel2 = arrChildPropLevel1
437 .getElementProperty().asArray();
438 if (null != arrChildPropLevel2
439 && null != arrChildPropLevel2
440 .getElementProperty()) {
441 modelProp = arrChildPropLevel2
442 .getElementProperty().asModel();
448 for (int i = 0; i < resModelArray.length; i++) {
449 SimulatorResourceAttribute indexAttribute = new SimulatorResourceAttribute(
450 "[" + Integer.toString(i) + "]",
451 new AttributeValue(resModelArray[i]));
452 AttributeElement attributeElement = mChildAttributes
453 .get("[" + Integer.toString(i) + "]");
454 if (attributeElement != null) {
455 attributeElement.update(indexAttribute);
457 indexAttribute.setProperty(modelProp);
459 // Update the attribute's value (Adding the new child
461 AttributeElement newAttribute = new AttributeElement(
462 this, indexAttribute, false);
463 AttributeValue currentValue = mAttribute.value();
465 SimulatorResourceModel[][][] curModelArray = (SimulatorResourceModel[][][]) currentValue
467 SimulatorResourceModel[][][] newModelArray = new SimulatorResourceModel[curModelArray.length + 1][][];
470 for (SimulatorResourceModel[][] modelArray : curModelArray) {
471 newModelArray[j++] = modelArray;
473 newModelArray[j] = resModelArray[i];
475 mAttribute.setValue(new AttributeValue(newModelArray));
476 mChildAttributes.put("[" + Integer.toString(i) + "]",
479 // Refresh UI to display the new attribute.
480 UiListenerHandler.getInstance()
481 .attributeAddedUINotification(newAttribute);
486 String currentValue = new AttributeValueStringConverter(
487 mAttribute.value()).toString();
488 String newValue = new AttributeValueStringConverter(
489 attribute.value()).toString();
490 if (!currentValue.equals(newValue)) {
491 mAttribute.setValue(attribute.value());
492 if (mParent instanceof AttributeElement) {
494 ((AttributeElement) mParent)
495 .deepSetChildValue(mAttribute);
496 } catch (InvalidArgsException e) {
499 UiListenerHandler.getInstance().attributeUpdatedUINotification(
505 public void deepSetChildValue(SimulatorResourceAttribute attribute)
506 throws InvalidArgsException {
507 if (null == attribute || null == attribute.name())
510 AttributeValue.TypeInfo myValuetypeInfo = mAttribute.value().typeInfo();
511 if (myValuetypeInfo.mType == AttributeValue.ValueType.RESOURCEMODEL) {
512 SimulatorResourceModel resModel = (SimulatorResourceModel) mAttribute
514 if (resModel.contains(attribute.name()))
515 resModel.set(attribute.name(), attribute.value());
520 if (mParent instanceof AttributeElement)
521 ((AttributeElement) mParent).deepSetChildValue(mAttribute);