Updated the order of imports in the source files of simulator plugins.
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ClientControllerPlugin / src / oic / simulator / clientcontroller / remoteresource / 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.clientcontroller.remoteresource;
18
19 import java.util.HashMap;
20 import java.util.Map;
21
22 import org.oic.simulator.ArrayProperty;
23 import org.oic.simulator.AttributeProperty;
24 import org.oic.simulator.AttributeValue;
25 import org.oic.simulator.InvalidArgsException;
26 import org.oic.simulator.ModelProperty;
27 import org.oic.simulator.SimulatorResourceAttribute;
28 import org.oic.simulator.SimulatorResourceModel;
29
30 import oic.simulator.clientcontroller.utils.AttributeValueStringConverter;
31
32 public class AttributeElement {
33     private Object                        mParent          = null;
34     private SimulatorResourceAttribute    mAttribute       = null;
35     private Map<String, AttributeElement> mChildAttributes = new HashMap<String, AttributeElement>();
36     private boolean                       mPostState       = false;
37
38     public AttributeElement(Object parent, SimulatorResourceAttribute attribute) {
39         mParent = parent;
40         mAttribute = attribute;
41         AttributeValue.TypeInfo typeInfo = attribute.value().typeInfo();
42         if (typeInfo.mType == AttributeValue.ValueType.RESOURCEMODEL) {
43             SimulatorResourceModel resModel = (SimulatorResourceModel) attribute
44                     .value().get();
45             ModelProperty modelProp = null;
46             if (null != attribute.property()) {
47                 modelProp = attribute.property().asModel();
48             }
49             String attName;
50             for (Map.Entry<String, AttributeValue> entry : resModel.get()
51                     .entrySet()) {
52                 attName = entry.getKey();
53                 AttributeProperty prop = null;
54                 if (null != modelProp)
55                     prop = modelProp.get(attName);
56                 mChildAttributes.put(
57                         attName,
58                         new AttributeElement(this,
59                                 new SimulatorResourceAttribute(attName, entry
60                                         .getValue(), prop)));
61             }
62         } else if (typeInfo.mType == AttributeValue.ValueType.ARRAY) {
63             if (typeInfo.mBaseType == AttributeValue.ValueType.RESOURCEMODEL) {
64                 if (typeInfo.mDepth == 1) {
65                     SimulatorResourceModel[] resModelArray = (SimulatorResourceModel[]) attribute
66                             .value().get();
67
68                     ArrayProperty arrProp = null;
69                     ModelProperty modelProp = null;
70                     if (null != attribute.property()) {
71                         arrProp = attribute.property().asArray();
72                         if (null != arrProp
73                                 && null != arrProp.getElementProperty()) {
74                             modelProp = arrProp.getElementProperty().asModel();
75                         }
76                     }
77                     for (int i = 0; i < resModelArray.length; i++) {
78                         SimulatorResourceAttribute indexAttribute = new SimulatorResourceAttribute(
79                                 "[" + Integer.toString(i) + "]",
80                                 new AttributeValue(resModelArray[i]), modelProp);
81                         mChildAttributes.put("[" + Integer.toString(i) + "]",
82                                 new AttributeElement(this, indexAttribute));
83                     }
84                 } else if (typeInfo.mDepth == 2) {
85                     SimulatorResourceModel[][] resModelArray = (SimulatorResourceModel[][]) attribute
86                             .value().get();
87
88                     ArrayProperty arrProp = null;
89                     ArrayProperty arrChildPropLevel1 = null;
90                     ModelProperty modelProp = null;
91                     if (null != attribute.property()) {
92                         arrProp = attribute.property().asArray();
93                         if (null != arrProp
94                                 && null != arrProp.getElementProperty()) {
95                             arrChildPropLevel1 = arrProp.getElementProperty()
96                                     .asArray();
97                             if (null != arrChildPropLevel1
98                                     && null != arrChildPropLevel1
99                                             .getElementProperty()) {
100                                 modelProp = arrProp.getElementProperty()
101                                         .asModel();
102                             }
103                         }
104                     }
105
106                     for (int i = 0; i < resModelArray.length; i++) {
107                         SimulatorResourceAttribute indexAttribute = new SimulatorResourceAttribute(
108                                 "[" + Integer.toString(i) + "]",
109                                 new AttributeValue(resModelArray[i]), modelProp);
110                         mChildAttributes.put("[" + Integer.toString(i) + "]",
111                                 new AttributeElement(this, indexAttribute));
112                     }
113                 } else if (typeInfo.mDepth == 3) {
114                     SimulatorResourceModel[][][] resModelArray = (SimulatorResourceModel[][][]) attribute
115                             .value().get();
116
117                     ArrayProperty arrProp = null;
118                     ArrayProperty arrChildPropLevel1 = null;
119                     ArrayProperty arrChildPropLevel2 = null;
120                     ModelProperty modelProp = null;
121                     if (null != attribute.property()) {
122                         arrProp = attribute.property().asArray();
123                         if (null != arrProp
124                                 && null != arrProp.getElementProperty()) {
125                             arrChildPropLevel1 = arrProp.getElementProperty()
126                                     .asArray();
127                             if (null != arrChildPropLevel1
128                                     && null != arrChildPropLevel1
129                                             .getElementProperty()) {
130                                 arrChildPropLevel2 = arrChildPropLevel1
131                                         .getElementProperty().asArray();
132                                 if (null != arrChildPropLevel2
133                                         && null != arrChildPropLevel2
134                                                 .getElementProperty()) {
135                                     modelProp = arrChildPropLevel2
136                                             .getElementProperty().asModel();
137                                 }
138                             }
139                         }
140                     }
141
142                     for (int i = 0; i < resModelArray.length; i++) {
143                         SimulatorResourceAttribute indexAttribute = new SimulatorResourceAttribute(
144                                 "[" + Integer.toString(i) + "]",
145                                 new AttributeValue(resModelArray[i]), modelProp);
146                         mChildAttributes.put("[" + Integer.toString(i) + "]",
147                                 new AttributeElement(this, indexAttribute));
148                     }
149                 }
150             }
151         }
152     }
153
154     public Object getParent() {
155         return mParent;
156     }
157
158     public boolean hasChildren() {
159         if (mChildAttributes != null && mChildAttributes.size() > 0)
160             return true;
161         return false;
162     }
163
164     public Map<String, AttributeElement> getChildren() {
165         if (hasChildren() == true)
166             return mChildAttributes;
167         return null;
168     }
169
170     public SimulatorResourceAttribute getSimulatorResourceAttribute() {
171         return mAttribute;
172     }
173
174     public boolean isPostSupported() {
175         /* Auto update is disabled for ARRAY and RESOURCEMODEL type values */
176         AttributeValue.TypeInfo typeInfo = mAttribute.value().typeInfo();
177         return (!(typeInfo.mType == AttributeValue.ValueType.ARRAY && typeInfo.mBaseType == AttributeValue.ValueType.RESOURCEMODEL) && typeInfo.mType != AttributeValue.ValueType.RESOURCEMODEL);
178     }
179
180     public void update(SimulatorResourceAttribute attribute,
181             boolean ramlUploaded) {
182         if (attribute == null)
183             return;
184
185         AttributeValue.TypeInfo typeInfo = attribute.value().typeInfo();
186         if (typeInfo.mType == AttributeValue.ValueType.RESOURCEMODEL) {
187             SimulatorResourceModel resModel = (SimulatorResourceModel) attribute
188                     .value().get();
189             for (Map.Entry<String, AttributeValue> entry : resModel.get()
190                     .entrySet()) {
191                 AttributeElement attributeElement = mChildAttributes.get(entry
192                         .getKey());
193                 if (attributeElement != null) {
194                     attributeElement.update(new SimulatorResourceAttribute(
195                             entry.getKey(), entry.getValue()), ramlUploaded);
196                 } else {
197                     // Display new attribute in UI
198                     AttributeElement newAttribute = new AttributeElement(this,
199                             new SimulatorResourceAttribute(entry.getKey(),
200                                     entry.getValue()));
201                     mChildAttributes.put(entry.getKey(), newAttribute);
202                 }
203             }
204         } else if (typeInfo.mType == AttributeValue.ValueType.ARRAY
205                 && typeInfo.mBaseType == AttributeValue.ValueType.RESOURCEMODEL) {
206             if (typeInfo.mDepth == 1) {
207                 SimulatorResourceModel[] resModelArray = (SimulatorResourceModel[]) attribute
208                         .value().get();
209                 for (int i = 0; i < resModelArray.length; i++) {
210                     SimulatorResourceAttribute indexAttribute = new SimulatorResourceAttribute(
211                             "[" + Integer.toString(i) + "]",
212                             new AttributeValue(resModelArray[i]), null);
213                     AttributeElement attributeElement = mChildAttributes
214                             .get("[" + Integer.toString(i) + "]");
215                     if (attributeElement != null) {
216                         attributeElement.update(indexAttribute, ramlUploaded);
217                     } else {
218                         // Display new attribute in UI
219                         AttributeElement newAttribute = new AttributeElement(
220                                 this, indexAttribute);
221                         mChildAttributes.put("[" + Integer.toString(i) + "]",
222                                 newAttribute);
223                     }
224                 }
225             }
226             if (typeInfo.mDepth == 2) {
227                 SimulatorResourceModel[][] resModelArray = (SimulatorResourceModel[][]) attribute
228                         .value().get();
229                 for (int i = 0; i < resModelArray.length; i++) {
230                     SimulatorResourceAttribute indexAttribute = new SimulatorResourceAttribute(
231                             "[" + Integer.toString(i) + "]",
232                             new AttributeValue(resModelArray[i]), null);
233                     AttributeElement attributeElement = mChildAttributes
234                             .get("[" + Integer.toString(i) + "]");
235                     if (attributeElement != null) {
236                         attributeElement.update(indexAttribute, ramlUploaded);
237                     } else {
238                         // Display new attribute in UI
239                         AttributeElement newAttribute = new AttributeElement(
240                                 this, indexAttribute);
241                         mChildAttributes.put("[" + Integer.toString(i) + "]",
242                                 newAttribute);
243                     }
244                 }
245             }
246             if (typeInfo.mDepth == 3) {
247                 SimulatorResourceModel[][][] resModelArray = (SimulatorResourceModel[][][]) attribute
248                         .value().get();
249                 for (int i = 0; i < resModelArray.length; i++) {
250                     SimulatorResourceAttribute indexAttribute = new SimulatorResourceAttribute(
251                             "[" + Integer.toString(i) + "]",
252                             new AttributeValue(resModelArray[i]), null);
253                     AttributeElement attributeElement = mChildAttributes
254                             .get("[" + Integer.toString(i) + "]");
255                     if (attributeElement != null) {
256                         attributeElement.update(indexAttribute, ramlUploaded);
257                     } else {
258                         // Display new attribute in UI
259                         AttributeElement newAttribute = new AttributeElement(
260                                 this, indexAttribute);
261                         mChildAttributes.put("[" + Integer.toString(i) + "]",
262                                 newAttribute);
263                     }
264                 }
265             }
266         } else {
267             if (ramlUploaded) {
268                 mAttribute.setProperty(attribute.property());
269             } else {
270                 String currentValue = new AttributeValueStringConverter(
271                         mAttribute.value()).toString();
272                 String newValue = new AttributeValueStringConverter(
273                         attribute.value()).toString();
274                 if (!currentValue.equals(newValue)) {
275                     mAttribute.setValue(attribute.value());
276                 }
277             }
278         }
279     }
280
281     public boolean getPostState() {
282         return mPostState;
283     }
284
285     public void setPostState(boolean mPostState) {
286         this.mPostState = mPostState;
287     }
288
289     public void deepSetChildValue(SimulatorResourceAttribute attribute)
290             throws InvalidArgsException {
291         if (null == attribute || null == attribute.name())
292             return;
293
294         AttributeValue.TypeInfo myValuetypeInfo = mAttribute.value().typeInfo();
295         if (myValuetypeInfo.mType == AttributeValue.ValueType.RESOURCEMODEL) {
296             SimulatorResourceModel resModel = (SimulatorResourceModel) mAttribute
297                     .value().get();
298             if (resModel.contains(attribute.name()))
299                 resModel.set(attribute.name(), attribute.value());
300             else
301                 return;
302         }
303
304         if (mParent instanceof AttributeElement)
305             ((AttributeElement) mParent).deepSetChildValue(mAttribute);
306     }
307 }