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