1 package oic.simulator.serviceprovider.model;
3 import java.util.HashMap;
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;
12 import oic.simulator.serviceprovider.utils.AttributeValueStringConverter;
13 import oic.simulator.serviceprovider.utils.Constants;
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;
26 public AttributeElement(Object parent,
27 SimulatorResourceAttribute attribute, boolean autoUpdateSupport) {
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
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
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,
54 } else if (typeInfo.mDepth == 2) {
55 SimulatorResourceModel[][] resModelArray = (SimulatorResourceModel[][]) attribute
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,
65 } else if (typeInfo.mDepth == 3) {
66 SimulatorResourceModel[][][] resModelArray = (SimulatorResourceModel[][][]) attribute
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,
81 public Object getParent() {
85 public boolean hasChildren() {
86 if (mChildAttributes != null && mChildAttributes.size() > 0)
91 public Map<String, AttributeElement> getChildren() {
92 if (hasChildren() == true)
93 return mChildAttributes;
97 public SimulatorResourceAttribute getSimulatorResourceAttribute() {
101 public int getAutoUpdateId() {
102 return mAutoUpdateId;
105 public void setAutoUpdateId(int id) {
109 public boolean isAutoUpdateInProgress() {
110 return mAutoUpdateState;
113 public void setAutoUpdateState(boolean state) {
114 if (mAutoUpdateState != state) {
115 mAutoUpdateState = state;
116 if (mListener != null)
117 mListener.update(this);
121 public int getAutoUpdateInterval() {
122 return mAutoUpdateInterval;
125 public void setAutoUpdateInterval(int interval) {
126 mAutoUpdateInterval = interval;
129 public AutoUpdateType getAutoUpdateType() {
130 return mAutoUpdateType;
133 public void setAutoUpdateType(AutoUpdateType type) {
134 mAutoUpdateType = type;
137 public boolean isAutoUpdateSupport() {
138 return mAutoUpdateSupport;
141 public boolean isReadOnly() {
142 return (null == mAttribute.property());
145 public void setListener(DataChangeListener listener) {
146 mListener = listener;
147 for (Map.Entry<String, AttributeElement> entry : mChildAttributes
149 entry.getValue().setListener(listener);
152 public DataChangeListener getListener() {
156 public void update(SimulatorResourceAttribute attribute) {
157 if (attribute == null)
160 AttributeValue.TypeInfo typeInfo = attribute.value().typeInfo();
161 if (typeInfo.mType == AttributeValue.ValueType.RESOURCEMODEL) {
162 SimulatorResourceModel resModel = (SimulatorResourceModel) attribute
164 for (Map.Entry<String, SimulatorResourceAttribute> entry : resModel
165 .getAttributes().entrySet()) {
166 AttributeElement attributeElement = mChildAttributes.get(entry
168 if (attributeElement != null) {
169 attributeElement.update(entry.getValue());
170 } else // Display new attribute in UI
172 AttributeElement newAttribute = new AttributeElement(this,
173 entry.getValue(), false);
174 mChildAttributes.put(entry.getKey(), newAttribute);
175 if (mListener != null)
176 mListener.add(newAttribute);
179 } else if (typeInfo.mType == AttributeValue.ValueType.ARRAY
180 && typeInfo.mBaseType == AttributeValue.ValueType.RESOURCEMODEL) {
181 if (typeInfo.mDepth == 1) {
182 SimulatorResourceModel[] resModelArray = (SimulatorResourceModel[]) attribute
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
194 AttributeElement newAttribute = new AttributeElement(
195 this, indexAttribute, false);
196 mChildAttributes.put("[" + Integer.toString(i) + "]",
198 if (mListener != null)
199 mListener.add(newAttribute);
203 if (typeInfo.mDepth == 2) {
204 SimulatorResourceModel[][] resModelArray = (SimulatorResourceModel[][]) attribute
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
216 AttributeElement newAttribute = new AttributeElement(
217 this, indexAttribute, false);
218 mChildAttributes.put("[" + Integer.toString(i) + "]",
220 if (mListener != null)
221 mListener.add(newAttribute);
225 if (typeInfo.mDepth == 3) {
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, false);
240 mChildAttributes.put("[" + Integer.toString(i) + "]",
242 if (mListener != null)
243 mListener.add(newAttribute);
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);
260 public void deepSetChildValue(SimulatorResourceAttribute attribute)
261 throws InvalidArgsException {
262 if (null == attribute || null == attribute.name())
265 AttributeValue.TypeInfo myValuetypeInfo = mAttribute.value().typeInfo();
266 if (myValuetypeInfo.mType == AttributeValue.ValueType.RESOURCEMODEL) {
267 SimulatorResourceModel resModel = (SimulatorResourceModel) mAttribute
269 if (resModel.containsAttribute(attribute.name()))
270 resModel.setAttributeValue(attribute.name(), attribute.value());
275 if (mParent instanceof AttributeElement)
276 ((AttributeElement) mParent).deepSetChildValue(mAttribute);