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