2 * Copyright 2015 Samsung Electronics All Rights Reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package oic.simulator.clientcontroller.remoteresource;
19 import java.util.HashMap;
22 import oic.simulator.clientcontroller.utils.AttributeValueStringConverter;
24 import org.oic.simulator.AttributeValue;
25 import org.oic.simulator.InvalidArgsException;
26 import org.oic.simulator.SimulatorResourceAttribute;
27 import org.oic.simulator.SimulatorResourceModel;
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;
35 public AttributeElement(Object parent, SimulatorResourceAttribute attribute) {
37 mAttribute = attribute;
38 AttributeValue.TypeInfo typeInfo = attribute.value().typeInfo();
39 if (typeInfo.mType == AttributeValue.ValueType.RESOURCEMODEL) {
40 SimulatorResourceModel resModel = (SimulatorResourceModel) attribute
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
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));
58 } else if (typeInfo.mDepth == 2) {
59 SimulatorResourceModel[][] resModelArray = (SimulatorResourceModel[][]) attribute
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));
68 } else if (typeInfo.mDepth == 3) {
69 SimulatorResourceModel[][][] resModelArray = (SimulatorResourceModel[][][]) attribute
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));
82 public Object getParent() {
86 public boolean hasChildren() {
87 if (mChildAttributes != null && mChildAttributes.size() > 0)
92 public Map<String, AttributeElement> getChildren() {
93 if (hasChildren() == true)
94 return mChildAttributes;
98 public SimulatorResourceAttribute getSimulatorResourceAttribute() {
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);
108 public void update(SimulatorResourceAttribute attribute) {
109 if (attribute == null)
112 AttributeValue.TypeInfo typeInfo = attribute.value().typeInfo();
113 if (typeInfo.mType == AttributeValue.ValueType.RESOURCEMODEL) {
114 SimulatorResourceModel resModel = (SimulatorResourceModel) attribute
116 for (Map.Entry<String, SimulatorResourceAttribute> entry : resModel
117 .getAttributes().entrySet()) {
118 AttributeElement attributeElement = mChildAttributes.get(entry
120 if (attributeElement != null) {
121 attributeElement.update(entry.getValue());
122 } else // Display new attribute in UI
124 AttributeElement newAttribute = new AttributeElement(this,
126 mChildAttributes.put(entry.getKey(), newAttribute);
129 } else if (typeInfo.mType == AttributeValue.ValueType.ARRAY
130 && typeInfo.mBaseType == AttributeValue.ValueType.RESOURCEMODEL) {
131 if (typeInfo.mDepth == 1) {
132 SimulatorResourceModel[] resModelArray = (SimulatorResourceModel[]) attribute
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
144 AttributeElement newAttribute = new AttributeElement(
145 this, indexAttribute);
146 mChildAttributes.put("[" + Integer.toString(i) + "]",
151 if (typeInfo.mDepth == 2) {
152 SimulatorResourceModel[][] resModelArray = (SimulatorResourceModel[][]) attribute
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
164 AttributeElement newAttribute = new AttributeElement(
165 this, indexAttribute);
166 mChildAttributes.put("[" + Integer.toString(i) + "]",
171 if (typeInfo.mDepth == 3) {
172 SimulatorResourceModel[][][] resModelArray = (SimulatorResourceModel[][][]) attribute
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
184 AttributeElement newAttribute = new AttributeElement(
185 this, indexAttribute);
186 mChildAttributes.put("[" + Integer.toString(i) + "]",
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());
202 public void updateForRAMLUpload(SimulatorResourceAttribute attribute) {
203 if (attribute == null)
206 AttributeValue.TypeInfo typeInfo = attribute.value().typeInfo();
207 if (typeInfo.mType == AttributeValue.ValueType.RESOURCEMODEL) {
208 SimulatorResourceModel resModel = (SimulatorResourceModel) attribute
210 for (Map.Entry<String, SimulatorResourceAttribute> entry : resModel
211 .getAttributes().entrySet()) {
212 AttributeElement attributeElement = mChildAttributes.get(entry
214 if (attributeElement != null) {
215 attributeElement.update(entry.getValue());
216 } else // Display new attribute in UI
218 AttributeElement newAttribute = new AttributeElement(this,
220 mChildAttributes.put(entry.getKey(), newAttribute);
223 } else if (typeInfo.mType == AttributeValue.ValueType.ARRAY
224 && typeInfo.mBaseType == AttributeValue.ValueType.RESOURCEMODEL) {
225 if (typeInfo.mDepth == 1) {
226 SimulatorResourceModel[] resModelArray = (SimulatorResourceModel[]) attribute
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
238 AttributeElement newAttribute = new AttributeElement(
239 this, indexAttribute);
240 mChildAttributes.put("[" + Integer.toString(i) + "]",
245 if (typeInfo.mDepth == 2) {
246 SimulatorResourceModel[][] resModelArray = (SimulatorResourceModel[][]) attribute
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
258 AttributeElement newAttribute = new AttributeElement(
259 this, indexAttribute);
260 mChildAttributes.put("[" + Integer.toString(i) + "]",
265 if (typeInfo.mDepth == 3) {
266 SimulatorResourceModel[][][] resModelArray = (SimulatorResourceModel[][][]) attribute
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
278 AttributeElement newAttribute = new AttributeElement(
279 this, indexAttribute);
280 mChildAttributes.put("[" + Integer.toString(i) + "]",
286 mAttribute.setProperty(attribute.property());
290 public boolean getPostState() {
294 public void setPostState(boolean mPostState) {
295 this.mPostState = mPostState;
298 public void deepSetChildValue(SimulatorResourceAttribute attribute)
299 throws InvalidArgsException {
300 if (null == attribute || null == attribute.name())
303 AttributeValue.TypeInfo myValuetypeInfo = mAttribute.value().typeInfo();
304 if (myValuetypeInfo.mType == AttributeValue.ValueType.RESOURCEMODEL) {
305 SimulatorResourceModel resModel = (SimulatorResourceModel) mAttribute
307 if (resModel.containsAttribute(attribute.name()))
308 resModel.setAttributeValue(attribute.name(), attribute.value());
313 if (mParent instanceof AttributeElement)
314 ((AttributeElement) mParent).deepSetChildValue(mAttribute);