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