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.HashMap;
22 import oic.simulator.serviceprovider.manager.UiListenerHandler;
23 import oic.simulator.serviceprovider.utils.AttributeValueStringConverter;
24 import oic.simulator.serviceprovider.utils.Constants;
26 import org.oic.simulator.ArrayProperty;
27 import org.oic.simulator.AttributeProperty;
28 import org.oic.simulator.AttributeValue;
29 import org.oic.simulator.DoubleProperty;
30 import org.oic.simulator.IntegerProperty;
31 import org.oic.simulator.InvalidArgsException;
32 import org.oic.simulator.ModelProperty;
33 import org.oic.simulator.SimulatorResourceAttribute;
34 import org.oic.simulator.SimulatorResourceModel;
35 import org.oic.simulator.StringProperty;
36 import org.oic.simulator.server.SimulatorResource.AutoUpdateType;
38 public class AttributeElement {
39 private Object mParent = null;
40 private SimulatorResourceAttribute mAttribute = null;
41 private Map<String, AttributeElement> mChildAttributes = new HashMap<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
278 for (Map.Entry<String, AttributeValue> entry : resModel.get()
280 AttributeElement attributeElement = mChildAttributes.get(entry
282 if (attributeElement != null) {
283 attributeElement.update(new SimulatorResourceAttribute(
284 entry.getKey(), entry.getValue()));
286 // Display new attribute in UI
287 AttributeElement newAttribute = new AttributeElement(this,
288 new SimulatorResourceAttribute(entry.getKey(),
289 entry.getValue()), false);
290 mChildAttributes.put(entry.getKey(), newAttribute);
292 UiListenerHandler.getInstance()
293 .attributeAddedUINotification(newAttribute);
296 } else if (typeInfo.mType == AttributeValue.ValueType.ARRAY
297 && typeInfo.mBaseType == AttributeValue.ValueType.RESOURCEMODEL) {
298 if (typeInfo.mDepth == 1) {
299 SimulatorResourceModel[] resModelArray = (SimulatorResourceModel[]) attribute
301 for (int i = 0; i < resModelArray.length; i++) {
302 SimulatorResourceAttribute indexAttribute = new SimulatorResourceAttribute(
303 "[" + Integer.toString(i) + "]",
304 new AttributeValue(resModelArray[i]), null);
305 AttributeElement attributeElement = mChildAttributes
306 .get("[" + Integer.toString(i) + "]");
307 if (attributeElement != null) {
308 attributeElement.update(indexAttribute);
310 // Display new attribute in UI
311 AttributeElement newAttribute = new AttributeElement(
312 this, indexAttribute, false);
313 mChildAttributes.put("[" + Integer.toString(i) + "]",
315 UiListenerHandler.getInstance()
316 .attributeAddedUINotification(newAttribute);
320 if (typeInfo.mDepth == 2) {
321 SimulatorResourceModel[][] resModelArray = (SimulatorResourceModel[][]) attribute
323 for (int i = 0; i < resModelArray.length; i++) {
324 SimulatorResourceAttribute indexAttribute = new SimulatorResourceAttribute(
325 "[" + Integer.toString(i) + "]",
326 new AttributeValue(resModelArray[i]), null);
327 AttributeElement attributeElement = mChildAttributes
328 .get("[" + Integer.toString(i) + "]");
329 if (attributeElement != null) {
330 attributeElement.update(indexAttribute);
332 // Display new attribute in UI
333 AttributeElement newAttribute = new AttributeElement(
334 this, indexAttribute, false);
335 mChildAttributes.put("[" + Integer.toString(i) + "]",
337 UiListenerHandler.getInstance()
338 .attributeAddedUINotification(newAttribute);
342 if (typeInfo.mDepth == 3) {
343 SimulatorResourceModel[][][] resModelArray = (SimulatorResourceModel[][][]) attribute
345 for (int i = 0; i < resModelArray.length; i++) {
346 SimulatorResourceAttribute indexAttribute = new SimulatorResourceAttribute(
347 "[" + Integer.toString(i) + "]",
348 new AttributeValue(resModelArray[i]), null);
349 AttributeElement attributeElement = mChildAttributes
350 .get("[" + Integer.toString(i) + "]");
351 if (attributeElement != null) {
352 attributeElement.update(indexAttribute);
354 // Display new attribute in UI
355 AttributeElement newAttribute = new AttributeElement(
356 this, indexAttribute, false);
357 mChildAttributes.put("[" + Integer.toString(i) + "]",
359 UiListenerHandler.getInstance()
360 .attributeAddedUINotification(newAttribute);
365 String currentValue = new AttributeValueStringConverter(
366 mAttribute.value()).toString();
367 String newValue = new AttributeValueStringConverter(
368 attribute.value()).toString();
369 if (!currentValue.equals(newValue)) {
370 mAttribute.setValue(attribute.value());
371 UiListenerHandler.getInstance().attributeUpdatedUINotification(
377 public void deepSetChildValue(SimulatorResourceAttribute attribute)
378 throws InvalidArgsException {
379 if (null == attribute || null == attribute.name())
382 AttributeValue.TypeInfo myValuetypeInfo = mAttribute.value().typeInfo();
383 if (myValuetypeInfo.mType == AttributeValue.ValueType.RESOURCEMODEL) {
384 SimulatorResourceModel resModel = (SimulatorResourceModel) mAttribute
386 if (resModel.contains(attribute.name()))
387 resModel.set(attribute.name(), attribute.value());
392 if (mParent instanceof AttributeElement)
393 ((AttributeElement) mParent).deepSetChildValue(mAttribute);