1 package oic.simulator.clientcontroller.remoteresource;
3 import java.util.HashMap;
6 import oic.simulator.clientcontroller.utils.AttributeValueStringConverter;
8 import org.oic.simulator.AttributeValue;
9 import org.oic.simulator.InvalidArgsException;
10 import org.oic.simulator.SimulatorResourceAttribute;
11 import org.oic.simulator.SimulatorResourceModel;
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;
19 public AttributeElement(Object parent, SimulatorResourceAttribute attribute) {
21 mAttribute = attribute;
22 AttributeValue.TypeInfo typeInfo = attribute.value().typeInfo();
23 if (typeInfo.mType == AttributeValue.ValueType.RESOURCEMODEL) {
24 SimulatorResourceModel resModel = (SimulatorResourceModel) attribute
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
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));
42 } else if (typeInfo.mDepth == 2) {
43 SimulatorResourceModel[][] resModelArray = (SimulatorResourceModel[][]) attribute
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));
52 } else if (typeInfo.mDepth == 3) {
53 SimulatorResourceModel[][][] resModelArray = (SimulatorResourceModel[][][]) attribute
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));
66 public Object getParent() {
70 public boolean hasChildren() {
71 if (mChildAttributes != null && mChildAttributes.size() > 0)
76 public Map<String, AttributeElement> getChildren() {
77 if (hasChildren() == true)
78 return mChildAttributes;
82 public SimulatorResourceAttribute getSimulatorResourceAttribute() {
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);
92 public void update(SimulatorResourceAttribute attribute) {
93 if (attribute == null)
96 AttributeValue.TypeInfo typeInfo = attribute.value().typeInfo();
97 if (typeInfo.mType == AttributeValue.ValueType.RESOURCEMODEL) {
98 SimulatorResourceModel resModel = (SimulatorResourceModel) attribute
100 for (Map.Entry<String, SimulatorResourceAttribute> entry : resModel
101 .getAttributes().entrySet()) {
102 AttributeElement attributeElement = mChildAttributes.get(entry
104 if (attributeElement != null) {
105 attributeElement.update(entry.getValue());
106 } else // Display new attribute in UI
108 AttributeElement newAttribute = new AttributeElement(this,
110 mChildAttributes.put(entry.getKey(), newAttribute);
113 } else if (typeInfo.mType == AttributeValue.ValueType.ARRAY
114 && typeInfo.mBaseType == AttributeValue.ValueType.RESOURCEMODEL) {
115 if (typeInfo.mDepth == 1) {
116 SimulatorResourceModel[] resModelArray = (SimulatorResourceModel[]) attribute
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
128 AttributeElement newAttribute = new AttributeElement(
129 this, indexAttribute);
130 mChildAttributes.put("[" + Integer.toString(i) + "]",
135 if (typeInfo.mDepth == 2) {
136 SimulatorResourceModel[][] resModelArray = (SimulatorResourceModel[][]) attribute
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
148 AttributeElement newAttribute = new AttributeElement(
149 this, indexAttribute);
150 mChildAttributes.put("[" + Integer.toString(i) + "]",
155 if (typeInfo.mDepth == 3) {
156 SimulatorResourceModel[][][] resModelArray = (SimulatorResourceModel[][][]) attribute
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
168 AttributeElement newAttribute = new AttributeElement(
169 this, indexAttribute);
170 mChildAttributes.put("[" + Integer.toString(i) + "]",
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());
186 public void updateForRAMLUpload(SimulatorResourceAttribute attribute) {
187 if (attribute == null)
190 AttributeValue.TypeInfo typeInfo = attribute.value().typeInfo();
191 if (typeInfo.mType == AttributeValue.ValueType.RESOURCEMODEL) {
192 SimulatorResourceModel resModel = (SimulatorResourceModel) attribute
194 for (Map.Entry<String, SimulatorResourceAttribute> entry : resModel
195 .getAttributes().entrySet()) {
196 AttributeElement attributeElement = mChildAttributes.get(entry
198 if (attributeElement != null) {
199 attributeElement.update(entry.getValue());
200 } else // Display new attribute in UI
202 AttributeElement newAttribute = new AttributeElement(this,
204 mChildAttributes.put(entry.getKey(), newAttribute);
207 } else if (typeInfo.mType == AttributeValue.ValueType.ARRAY
208 && typeInfo.mBaseType == AttributeValue.ValueType.RESOURCEMODEL) {
209 if (typeInfo.mDepth == 1) {
210 SimulatorResourceModel[] resModelArray = (SimulatorResourceModel[]) attribute
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
222 AttributeElement newAttribute = new AttributeElement(
223 this, indexAttribute);
224 mChildAttributes.put("[" + Integer.toString(i) + "]",
229 if (typeInfo.mDepth == 2) {
230 SimulatorResourceModel[][] resModelArray = (SimulatorResourceModel[][]) attribute
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
242 AttributeElement newAttribute = new AttributeElement(
243 this, indexAttribute);
244 mChildAttributes.put("[" + Integer.toString(i) + "]",
249 if (typeInfo.mDepth == 3) {
250 SimulatorResourceModel[][][] resModelArray = (SimulatorResourceModel[][][]) attribute
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
262 AttributeElement newAttribute = new AttributeElement(
263 this, indexAttribute);
264 mChildAttributes.put("[" + Integer.toString(i) + "]",
270 mAttribute.setProperty(attribute.property());
274 public boolean getPostState() {
278 public void setPostState(boolean mPostState) {
279 this.mPostState = mPostState;
282 public void deepSetChildValue(SimulatorResourceAttribute attribute)
283 throws InvalidArgsException {
284 if (null == attribute || null == attribute.name())
287 AttributeValue.TypeInfo myValuetypeInfo = mAttribute.value().typeInfo();
288 if (myValuetypeInfo.mType == AttributeValue.ValueType.RESOURCEMODEL) {
289 SimulatorResourceModel resModel = (SimulatorResourceModel) mAttribute
291 if (resModel.containsAttribute(attribute.name()))
292 resModel.setAttributeValue(attribute.name(), attribute.value());
297 if (mParent instanceof AttributeElement)
298 ((AttributeElement) mParent).deepSetChildValue(mAttribute);