Added UI Support for updating model arrays.
[contrib/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.Map;
20 import java.util.TreeMap;
21
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;
33
34 import oic.simulator.serviceprovider.manager.UiListenerHandler;
35 import oic.simulator.serviceprovider.utils.AttributeValueStringConverter;
36 import oic.simulator.serviceprovider.utils.Constants;
37
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;
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             String attName;
279             for (Map.Entry<String, AttributeValue> entry : resModel.get()
280                     .entrySet()) {
281                 attName = entry.getKey();
282                 AttributeElement attributeElement = mChildAttributes
283                         .get(attName);
284
285                 if (attributeElement != null) {
286                     attributeElement.update(new SimulatorResourceAttribute(
287                             attName, entry.getValue()));
288                 } else {
289                     // Get the property from the newly received attribute.
290                     ModelProperty modelProp = null;
291                     if (null != attribute.property()) {
292                         modelProp = attribute.property().asModel();
293                     }
294
295                     AttributeProperty prop = null;
296                     if (null != modelProp)
297                         prop = modelProp.get(attName);
298
299                     AttributeElement newAttribute = new AttributeElement(this,
300                             new SimulatorResourceAttribute(attName,
301                                     entry.getValue(), prop), false);
302
303                     // Update the attribute's value (Adding the new child
304                     // attribute).
305                     AttributeValue currentValue = mAttribute.value();
306                     SimulatorResourceModel curModel = (SimulatorResourceModel) currentValue
307                             .get();
308                     try {
309                         curModel.set(attName, entry.getValue());
310                     } catch (InvalidArgsException e) {
311                     }
312
313                     mChildAttributes.put(attName, newAttribute);
314
315                     // Refresh UI to display the new attribute.
316                     UiListenerHandler.getInstance()
317                             .attributeAddedUINotification(newAttribute);
318                 }
319             }
320         } else if (typeInfo.mType == AttributeValue.ValueType.ARRAY
321                 && typeInfo.mBaseType == AttributeValue.ValueType.RESOURCEMODEL) {
322             if (typeInfo.mDepth == 1) {
323                 SimulatorResourceModel[] resModelArray = (SimulatorResourceModel[]) attribute
324                         .value().get();
325
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();
332                     }
333                 }
334
335                 mChildAttributes.clear();
336
337                 SimulatorResourceModel[] newModelArray = new SimulatorResourceModel[resModelArray.length];
338
339                 for (int i = 0; i < resModelArray.length; i++) {
340                     SimulatorResourceAttribute indexAttribute = new SimulatorResourceAttribute(
341                             "[" + Integer.toString(i) + "]",
342                             new AttributeValue(resModelArray[i]), modelProp);
343
344                     // Update the attribute's value (Adding the new child
345                     // attribute).
346                     AttributeElement newAttributeElement = new AttributeElement(
347                             this, indexAttribute, false);
348
349                     newModelArray[i] = resModelArray[i];
350
351                     mChildAttributes.put("[" + Integer.toString(i) + "]",
352                             newAttributeElement);
353                 }
354
355                 // Changing the value of the model array attribute. Property
356                 // remains the same.
357                 mAttribute.setValue(new AttributeValue(newModelArray));
358
359                 // Refresh UI to display the new attribute.
360                 UiListenerHandler.getInstance().attributeAddedUINotification(
361                         this);
362             }
363             if (typeInfo.mDepth == 2) {
364                 SimulatorResourceModel[][] resModelArray = (SimulatorResourceModel[][]) attribute
365                         .value().get();
366
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()
374                                 .asArray();
375                         if (null != arrChildPropLevel1
376                                 && null != arrChildPropLevel1
377                                         .getElementProperty()) {
378                             modelProp = arrProp.getElementProperty().asModel();
379                         }
380                     }
381                 }
382
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);
391                     } else {
392                         indexAttribute.setProperty(modelProp);
393
394                         // Update the attribute's value (Adding the new child
395                         // attribute).
396                         AttributeElement newAttribute = new AttributeElement(
397                                 this, indexAttribute, false);
398                         AttributeValue currentValue = mAttribute.value();
399
400                         SimulatorResourceModel[][] curModelArray = (SimulatorResourceModel[][]) currentValue
401                                 .get();
402                         SimulatorResourceModel[][] newModelArray = new SimulatorResourceModel[curModelArray.length + 1][];
403
404                         int j = 0;
405                         for (SimulatorResourceModel[] modelArray : curModelArray) {
406                             newModelArray[j++] = modelArray;
407                         }
408                         newModelArray[j] = resModelArray[i];
409
410                         mAttribute.setValue(new AttributeValue(newModelArray));
411                         mChildAttributes.put("[" + Integer.toString(i) + "]",
412                                 newAttribute);
413
414                         // Refresh UI to display the new attribute.
415                         UiListenerHandler.getInstance()
416                                 .attributeAddedUINotification(newAttribute);
417                     }
418                 }
419             }
420             if (typeInfo.mDepth == 3) {
421                 SimulatorResourceModel[][][] resModelArray = (SimulatorResourceModel[][][]) attribute
422                         .value().get();
423
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()
432                                 .asArray();
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();
443                             }
444                         }
445                     }
446                 }
447
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);
456                     } else {
457                         indexAttribute.setProperty(modelProp);
458
459                         // Update the attribute's value (Adding the new child
460                         // attribute).
461                         AttributeElement newAttribute = new AttributeElement(
462                                 this, indexAttribute, false);
463                         AttributeValue currentValue = mAttribute.value();
464
465                         SimulatorResourceModel[][][] curModelArray = (SimulatorResourceModel[][][]) currentValue
466                                 .get();
467                         SimulatorResourceModel[][][] newModelArray = new SimulatorResourceModel[curModelArray.length + 1][][];
468
469                         int j = 0;
470                         for (SimulatorResourceModel[][] modelArray : curModelArray) {
471                             newModelArray[j++] = modelArray;
472                         }
473                         newModelArray[j] = resModelArray[i];
474
475                         mAttribute.setValue(new AttributeValue(newModelArray));
476                         mChildAttributes.put("[" + Integer.toString(i) + "]",
477                                 newAttribute);
478
479                         // Refresh UI to display the new attribute.
480                         UiListenerHandler.getInstance()
481                                 .attributeAddedUINotification(newAttribute);
482                     }
483                 }
484             }
485         } else {
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) {
493                     try {
494                         ((AttributeElement) mParent)
495                                 .deepSetChildValue(mAttribute);
496                     } catch (InvalidArgsException e) {
497                     }
498                 }
499                 UiListenerHandler.getInstance().attributeUpdatedUINotification(
500                         this);
501             }
502         }
503     }
504
505     public void deepSetChildValue(SimulatorResourceAttribute attribute)
506             throws InvalidArgsException {
507         if (null == attribute || null == attribute.name())
508             return;
509
510         AttributeValue.TypeInfo myValuetypeInfo = mAttribute.value().typeInfo();
511         if (myValuetypeInfo.mType == AttributeValue.ValueType.RESOURCEMODEL) {
512             SimulatorResourceModel resModel = (SimulatorResourceModel) mAttribute
513                     .value().get();
514             if (resModel.contains(attribute.name()))
515                 resModel.set(attribute.name(), attribute.value());
516             else
517                 return;
518         }
519
520         if (mParent instanceof AttributeElement)
521             ((AttributeElement) mParent).deepSetChildValue(mAttribute);
522     }
523 }