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