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.ArrayProperty;
25 import org.oic.simulator.AttributeProperty;
26 import org.oic.simulator.AttributeValue;
27 import org.oic.simulator.InvalidArgsException;
28 import org.oic.simulator.ModelProperty;
29 import org.oic.simulator.SimulatorResourceAttribute;
30 import org.oic.simulator.SimulatorResourceModel;
32 public class AttributeElement {
33 private Object mParent = null;
34 private SimulatorResourceAttribute mAttribute = null;
35 private Map<String, AttributeElement> mChildAttributes = new HashMap<String, AttributeElement>();
36 private boolean mPostState = false;
38 public AttributeElement(Object parent, SimulatorResourceAttribute attribute) {
40 mAttribute = attribute;
41 AttributeValue.TypeInfo typeInfo = attribute.value().typeInfo();
42 if (typeInfo.mType == AttributeValue.ValueType.RESOURCEMODEL) {
43 SimulatorResourceModel resModel = (SimulatorResourceModel) attribute
45 ModelProperty modelProp = null;
46 if (null != attribute.property()) {
47 modelProp = attribute.property().asModel();
50 for (Map.Entry<String, AttributeValue> entry : resModel.get()
52 attName = entry.getKey();
53 AttributeProperty prop = null;
54 if (null != modelProp)
55 prop = modelProp.get(attName);
58 new AttributeElement(this,
59 new SimulatorResourceAttribute(attName, entry
62 } else if (typeInfo.mType == AttributeValue.ValueType.ARRAY) {
63 if (typeInfo.mBaseType == AttributeValue.ValueType.RESOURCEMODEL) {
64 if (typeInfo.mDepth == 1) {
65 SimulatorResourceModel[] resModelArray = (SimulatorResourceModel[]) attribute
68 ArrayProperty arrProp = null;
69 ModelProperty modelProp = null;
70 if (null != attribute.property()) {
71 arrProp = attribute.property().asArray();
73 && null != arrProp.getElementProperty()) {
74 modelProp = arrProp.getElementProperty().asModel();
77 for (int i = 0; i < resModelArray.length; i++) {
78 SimulatorResourceAttribute indexAttribute = new SimulatorResourceAttribute(
79 "[" + Integer.toString(i) + "]",
80 new AttributeValue(resModelArray[i]), modelProp);
81 mChildAttributes.put("[" + Integer.toString(i) + "]",
82 new AttributeElement(this, indexAttribute));
84 } else if (typeInfo.mDepth == 2) {
85 SimulatorResourceModel[][] resModelArray = (SimulatorResourceModel[][]) attribute
88 ArrayProperty arrProp = null;
89 ArrayProperty arrChildPropLevel1 = null;
90 ModelProperty modelProp = null;
91 if (null != attribute.property()) {
92 arrProp = attribute.property().asArray();
94 && null != arrProp.getElementProperty()) {
95 arrChildPropLevel1 = arrProp.getElementProperty()
97 if (null != arrChildPropLevel1
98 && null != arrChildPropLevel1
99 .getElementProperty()) {
100 modelProp = arrProp.getElementProperty()
106 for (int i = 0; i < resModelArray.length; i++) {
107 SimulatorResourceAttribute indexAttribute = new SimulatorResourceAttribute(
108 "[" + Integer.toString(i) + "]",
109 new AttributeValue(resModelArray[i]), modelProp);
110 mChildAttributes.put("[" + Integer.toString(i) + "]",
111 new AttributeElement(this, indexAttribute));
113 } else if (typeInfo.mDepth == 3) {
114 SimulatorResourceModel[][][] resModelArray = (SimulatorResourceModel[][][]) attribute
117 ArrayProperty arrProp = null;
118 ArrayProperty arrChildPropLevel1 = null;
119 ArrayProperty arrChildPropLevel2 = null;
120 ModelProperty modelProp = null;
121 if (null != attribute.property()) {
122 arrProp = attribute.property().asArray();
124 && null != arrProp.getElementProperty()) {
125 arrChildPropLevel1 = arrProp.getElementProperty()
127 if (null != arrChildPropLevel1
128 && null != arrChildPropLevel1
129 .getElementProperty()) {
130 arrChildPropLevel2 = arrChildPropLevel1
131 .getElementProperty().asArray();
132 if (null != arrChildPropLevel2
133 && null != arrChildPropLevel2
134 .getElementProperty()) {
135 modelProp = arrChildPropLevel2
136 .getElementProperty().asModel();
142 for (int i = 0; i < resModelArray.length; i++) {
143 SimulatorResourceAttribute indexAttribute = new SimulatorResourceAttribute(
144 "[" + Integer.toString(i) + "]",
145 new AttributeValue(resModelArray[i]), modelProp);
146 mChildAttributes.put("[" + Integer.toString(i) + "]",
147 new AttributeElement(this, indexAttribute));
154 public Object getParent() {
158 public boolean hasChildren() {
159 if (mChildAttributes != null && mChildAttributes.size() > 0)
164 public Map<String, AttributeElement> getChildren() {
165 if (hasChildren() == true)
166 return mChildAttributes;
170 public SimulatorResourceAttribute getSimulatorResourceAttribute() {
174 public boolean isPostSupported() {
175 /* Auto update is disabled for ARRAY and RESOURCEMODEL type values */
176 AttributeValue.TypeInfo typeInfo = mAttribute.value().typeInfo();
177 return (!(typeInfo.mType == AttributeValue.ValueType.ARRAY && typeInfo.mBaseType == AttributeValue.ValueType.RESOURCEMODEL) && typeInfo.mType != AttributeValue.ValueType.RESOURCEMODEL);
180 public void update(SimulatorResourceAttribute attribute,
181 boolean ramlUploaded) {
182 if (attribute == null)
185 AttributeValue.TypeInfo typeInfo = attribute.value().typeInfo();
186 if (typeInfo.mType == AttributeValue.ValueType.RESOURCEMODEL) {
187 SimulatorResourceModel resModel = (SimulatorResourceModel) attribute
189 for (Map.Entry<String, AttributeValue> entry : resModel.get()
191 AttributeElement attributeElement = mChildAttributes.get(entry
193 if (attributeElement != null) {
194 attributeElement.update(new SimulatorResourceAttribute(
195 entry.getKey(), entry.getValue()), ramlUploaded);
197 // Display new attribute in UI
198 AttributeElement newAttribute = new AttributeElement(this,
199 new SimulatorResourceAttribute(entry.getKey(),
201 mChildAttributes.put(entry.getKey(), newAttribute);
204 } else if (typeInfo.mType == AttributeValue.ValueType.ARRAY
205 && typeInfo.mBaseType == AttributeValue.ValueType.RESOURCEMODEL) {
206 if (typeInfo.mDepth == 1) {
207 SimulatorResourceModel[] resModelArray = (SimulatorResourceModel[]) attribute
209 for (int i = 0; i < resModelArray.length; i++) {
210 SimulatorResourceAttribute indexAttribute = new SimulatorResourceAttribute(
211 "[" + Integer.toString(i) + "]",
212 new AttributeValue(resModelArray[i]), null);
213 AttributeElement attributeElement = mChildAttributes
214 .get("[" + Integer.toString(i) + "]");
215 if (attributeElement != null) {
216 attributeElement.update(indexAttribute, ramlUploaded);
218 // Display new attribute in UI
219 AttributeElement newAttribute = new AttributeElement(
220 this, indexAttribute);
221 mChildAttributes.put("[" + Integer.toString(i) + "]",
226 if (typeInfo.mDepth == 2) {
227 SimulatorResourceModel[][] resModelArray = (SimulatorResourceModel[][]) attribute
229 for (int i = 0; i < resModelArray.length; i++) {
230 SimulatorResourceAttribute indexAttribute = new SimulatorResourceAttribute(
231 "[" + Integer.toString(i) + "]",
232 new AttributeValue(resModelArray[i]), null);
233 AttributeElement attributeElement = mChildAttributes
234 .get("[" + Integer.toString(i) + "]");
235 if (attributeElement != null) {
236 attributeElement.update(indexAttribute, ramlUploaded);
238 // Display new attribute in UI
239 AttributeElement newAttribute = new AttributeElement(
240 this, indexAttribute);
241 mChildAttributes.put("[" + Integer.toString(i) + "]",
246 if (typeInfo.mDepth == 3) {
247 SimulatorResourceModel[][][] resModelArray = (SimulatorResourceModel[][][]) attribute
249 for (int i = 0; i < resModelArray.length; i++) {
250 SimulatorResourceAttribute indexAttribute = new SimulatorResourceAttribute(
251 "[" + Integer.toString(i) + "]",
252 new AttributeValue(resModelArray[i]), null);
253 AttributeElement attributeElement = mChildAttributes
254 .get("[" + Integer.toString(i) + "]");
255 if (attributeElement != null) {
256 attributeElement.update(indexAttribute, ramlUploaded);
258 // Display new attribute in UI
259 AttributeElement newAttribute = new AttributeElement(
260 this, indexAttribute);
261 mChildAttributes.put("[" + Integer.toString(i) + "]",
268 mAttribute.setProperty(attribute.property());
270 String currentValue = new AttributeValueStringConverter(
271 mAttribute.value()).toString();
272 String newValue = new AttributeValueStringConverter(
273 attribute.value()).toString();
274 if (!currentValue.equals(newValue)) {
275 mAttribute.setValue(attribute.value());
281 public boolean getPostState() {
285 public void setPostState(boolean mPostState) {
286 this.mPostState = mPostState;
289 public void deepSetChildValue(SimulatorResourceAttribute attribute)
290 throws InvalidArgsException {
291 if (null == attribute || null == attribute.name())
294 AttributeValue.TypeInfo myValuetypeInfo = mAttribute.value().typeInfo();
295 if (myValuetypeInfo.mType == AttributeValue.ValueType.RESOURCEMODEL) {
296 SimulatorResourceModel resModel = (SimulatorResourceModel) mAttribute
298 if (resModel.contains(attribute.name()))
299 resModel.set(attribute.name(), attribute.value());
304 if (mParent instanceof AttributeElement)
305 ((AttributeElement) mParent).deepSetChildValue(mAttribute);