5e05bb7244e1e4535969858596545b20baf30d73
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ServiceProviderPlugin / src / oic / simulator / serviceprovider / model / AttributeElement.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.HashMap;
20 import java.util.Map;
21
22 import oic.simulator.serviceprovider.manager.UiListenerHandler;
23 import oic.simulator.serviceprovider.utils.AttributeValueStringConverter;
24 import oic.simulator.serviceprovider.utils.Constants;
25
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;
37
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;
48
49     public AttributeElement(Object parent,
50             SimulatorResourceAttribute attribute, boolean autoUpdateSupport)
51             throws NullPointerException {
52         mParent = parent;
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
59                     .value().get();
60             ModelProperty modelProp = null;
61             if (null != attribute.property()) {
62                 modelProp = attribute.property().asModel();
63             }
64             String attName;
65             for (Map.Entry<String, AttributeValue> entry : resModel.get()
66                     .entrySet()) {
67                 attName = entry.getKey();
68                 AttributeProperty prop = null;
69                 if (null != modelProp)
70                     prop = modelProp.get(attName);
71                 mChildAttributes.put(
72                         attName,
73                         new AttributeElement(this,
74                                 new SimulatorResourceAttribute(attName, entry
75                                         .getValue(), prop), false));
76             }
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
82                             .value().get();
83
84                     ArrayProperty arrProp = null;
85                     ModelProperty modelProp = null;
86                     if (null != attribute.property()) {
87                         arrProp = attribute.property().asArray();
88                         if (null != arrProp
89                                 && null != arrProp.getElementProperty()) {
90                             modelProp = arrProp.getElementProperty().asModel();
91                         }
92                     }
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,
99                                         false));
100                     }
101                 } else if (typeInfo.mDepth == 2) {
102                     SimulatorResourceModel[][] resModelArray = (SimulatorResourceModel[][]) attribute
103                             .value().get();
104
105                     ArrayProperty arrProp = null;
106                     ArrayProperty arrChildPropLevel1 = null;
107                     ModelProperty modelProp = null;
108                     if (null != attribute.property()) {
109                         arrProp = attribute.property().asArray();
110                         if (null != arrProp
111                                 && null != arrProp.getElementProperty()) {
112                             arrChildPropLevel1 = arrProp.getElementProperty()
113                                     .asArray();
114                             if (null != arrChildPropLevel1
115                                     && null != arrChildPropLevel1
116                                             .getElementProperty()) {
117                                 modelProp = arrProp.getElementProperty()
118                                         .asModel();
119                             }
120                         }
121                     }
122
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,
129                                         false));
130                     }
131                 } else if (typeInfo.mDepth == 3) {
132                     SimulatorResourceModel[][][] resModelArray = (SimulatorResourceModel[][][]) attribute
133                             .value().get();
134
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();
141                         if (null != arrProp
142                                 && null != arrProp.getElementProperty()) {
143                             arrChildPropLevel1 = arrProp.getElementProperty()
144                                     .asArray();
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();
155                                 }
156                             }
157                         }
158                     }
159
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,
166                                         false));
167                     }
168                 }
169             }
170         }
171     }
172
173     public Object getParent() {
174         return mParent;
175     }
176
177     public boolean hasChildren() {
178         if (mChildAttributes != null && mChildAttributes.size() > 0)
179             return true;
180         return false;
181     }
182
183     public Map<String, AttributeElement> getChildren() {
184         if (hasChildren() == true)
185             return mChildAttributes;
186         return null;
187     }
188
189     public SimulatorResourceAttribute getSimulatorResourceAttribute() {
190         return mAttribute;
191     }
192
193     public int getAutoUpdateId() {
194         return mAutoUpdateId;
195     }
196
197     public void setAutoUpdateId(int id) {
198         mAutoUpdateId = id;
199     }
200
201     public boolean isAutoUpdateInProgress() {
202         return mAutoUpdateState;
203     }
204
205     public void setAutoUpdateState(boolean state) {
206         if (mAutoUpdateState != state) {
207             mAutoUpdateState = state;
208
209             UiListenerHandler.getInstance()
210                     .attributeUpdatedUINotification(this);
211         }
212     }
213
214     public int getAutoUpdateInterval() {
215         return mAutoUpdateInterval;
216     }
217
218     public void setAutoUpdateInterval(int interval) {
219         mAutoUpdateInterval = interval;
220     }
221
222     public AutoUpdateType getAutoUpdateType() {
223         return mAutoUpdateType;
224     }
225
226     public void setAutoUpdateType(AutoUpdateType type) {
227         mAutoUpdateType = type;
228     }
229
230     public boolean isAutoUpdateSupport() {
231         return mAutoUpdateSupport;
232     }
233
234     public boolean isReadOnly() {
235         AttributeProperty prop = mAttribute.property();
236         if (null == prop) {
237             return true;
238         }
239
240         if (prop.isInteger()) {
241             IntegerProperty intProperty = prop.asInteger();
242             return !(intProperty.hasRange() || intProperty.hasValues());
243         }
244
245         if (prop.isDouble()) {
246             DoubleProperty dblProperty = prop.asDouble();
247             return !(dblProperty.hasRange() || dblProperty.hasValues());
248         }
249
250         if (prop.isBoolean()) {
251             return false;
252         }
253
254         if (prop.isString()) {
255             StringProperty strProperty = prop.asString();
256             return !(strProperty.hasValues());
257         }
258
259         return true;
260     }
261
262     public synchronized boolean getEditLock() {
263         return mEditLock;
264     }
265
266     public synchronized void setEditLock(boolean editLock) {
267         this.mEditLock = editLock;
268     }
269
270     public void update(SimulatorResourceAttribute attribute) {
271         if (attribute == null)
272             return;
273
274         AttributeValue.TypeInfo typeInfo = attribute.value().typeInfo();
275         if (typeInfo.mType == AttributeValue.ValueType.RESOURCEMODEL) {
276             SimulatorResourceModel resModel = (SimulatorResourceModel) attribute
277                     .value().get();
278             for (Map.Entry<String, AttributeValue> entry : resModel.get()
279                     .entrySet()) {
280                 AttributeElement attributeElement = mChildAttributes.get(entry
281                         .getKey());
282                 if (attributeElement != null) {
283                     attributeElement.update(new SimulatorResourceAttribute(
284                             entry.getKey(), entry.getValue()));
285                 } else {
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);
291
292                     UiListenerHandler.getInstance()
293                             .attributeAddedUINotification(newAttribute);
294                 }
295             }
296         } else if (typeInfo.mType == AttributeValue.ValueType.ARRAY
297                 && typeInfo.mBaseType == AttributeValue.ValueType.RESOURCEMODEL) {
298             if (typeInfo.mDepth == 1) {
299                 SimulatorResourceModel[] resModelArray = (SimulatorResourceModel[]) attribute
300                         .value().get();
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);
309                     } else {
310                         // Display new attribute in UI
311                         AttributeElement newAttribute = new AttributeElement(
312                                 this, indexAttribute, false);
313                         mChildAttributes.put("[" + Integer.toString(i) + "]",
314                                 newAttribute);
315                         UiListenerHandler.getInstance()
316                                 .attributeAddedUINotification(newAttribute);
317                     }
318                 }
319             }
320             if (typeInfo.mDepth == 2) {
321                 SimulatorResourceModel[][] resModelArray = (SimulatorResourceModel[][]) attribute
322                         .value().get();
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);
331                     } else {
332                         // Display new attribute in UI
333                         AttributeElement newAttribute = new AttributeElement(
334                                 this, indexAttribute, false);
335                         mChildAttributes.put("[" + Integer.toString(i) + "]",
336                                 newAttribute);
337                         UiListenerHandler.getInstance()
338                                 .attributeAddedUINotification(newAttribute);
339                     }
340                 }
341             }
342             if (typeInfo.mDepth == 3) {
343                 SimulatorResourceModel[][][] resModelArray = (SimulatorResourceModel[][][]) attribute
344                         .value().get();
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);
353                     } else {
354                         // Display new attribute in UI
355                         AttributeElement newAttribute = new AttributeElement(
356                                 this, indexAttribute, false);
357                         mChildAttributes.put("[" + Integer.toString(i) + "]",
358                                 newAttribute);
359                         UiListenerHandler.getInstance()
360                                 .attributeAddedUINotification(newAttribute);
361                     }
362                 }
363             }
364         } else {
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(
372                         this);
373             }
374         }
375     }
376
377     public void deepSetChildValue(SimulatorResourceAttribute attribute)
378             throws InvalidArgsException {
379         if (null == attribute || null == attribute.name())
380             return;
381
382         AttributeValue.TypeInfo myValuetypeInfo = mAttribute.value().typeInfo();
383         if (myValuetypeInfo.mType == AttributeValue.ValueType.RESOURCEMODEL) {
384             SimulatorResourceModel resModel = (SimulatorResourceModel) mAttribute
385                     .value().get();
386             if (resModel.contains(attribute.name()))
387                 resModel.set(attribute.name(), attribute.value());
388             else
389                 return;
390         }
391
392         if (mParent instanceof AttributeElement)
393             ((AttributeElement) mParent).deepSetChildValue(mAttribute);
394     }
395 }