1 package oic.simulator.serviceprovider.model;
3 import java.util.HashMap;
6 import oic.simulator.serviceprovider.manager.UiListenerHandler;
7 import oic.simulator.serviceprovider.utils.AttributeValueStringConverter;
8 import oic.simulator.serviceprovider.utils.Constants;
10 import org.oic.simulator.AttributeValue;
11 import org.oic.simulator.InvalidArgsException;
12 import org.oic.simulator.SimulatorResourceAttribute;
13 import org.oic.simulator.SimulatorResourceModel;
14 import org.oic.simulator.server.SimulatorResource.AutoUpdateType;
16 public class AttributeElement {
17 private Object mParent = null;
18 private SimulatorResourceAttribute mAttribute = null;
19 private Map<String, AttributeElement> mChildAttributes = new HashMap<String, AttributeElement>();
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)
28 throws NullPointerException {
30 mAttribute = attribute;
31 mAutoUpdateSupport = autoUpdateSupport;
32 AttributeValue.TypeInfo typeInfo = attribute.value().typeInfo();
33 if (typeInfo.mType == AttributeValue.ValueType.RESOURCEMODEL) {
34 mAutoUpdateSupport = false;
35 SimulatorResourceModel resModel = (SimulatorResourceModel) attribute
37 for (Map.Entry<String, SimulatorResourceAttribute> entrySet : resModel
38 .getAttributes().entrySet()) {
39 mChildAttributes.put(entrySet.getKey(), new AttributeElement(
40 this, entrySet.getValue(), false));
42 } else if (typeInfo.mType == AttributeValue.ValueType.ARRAY) {
43 mAutoUpdateSupport = false;
44 if (typeInfo.mBaseType == AttributeValue.ValueType.RESOURCEMODEL) {
45 if (typeInfo.mDepth == 1) {
46 SimulatorResourceModel[] resModelArray = (SimulatorResourceModel[]) attribute
48 for (int i = 0; i < resModelArray.length; i++) {
49 SimulatorResourceAttribute indexAttribute = new SimulatorResourceAttribute(
50 "[" + Integer.toString(i) + "]",
51 new AttributeValue(resModelArray[i]), null);
52 mChildAttributes.put("[" + Integer.toString(i) + "]",
53 new AttributeElement(this, indexAttribute,
56 } else if (typeInfo.mDepth == 2) {
57 SimulatorResourceModel[][] resModelArray = (SimulatorResourceModel[][]) attribute
59 for (int i = 0; i < resModelArray.length; i++) {
60 SimulatorResourceAttribute indexAttribute = new SimulatorResourceAttribute(
61 "[" + Integer.toString(i) + "]",
62 new AttributeValue(resModelArray[i]), null);
63 mChildAttributes.put("[" + Integer.toString(i) + "]",
64 new AttributeElement(this, indexAttribute,
67 } else if (typeInfo.mDepth == 3) {
68 SimulatorResourceModel[][][] resModelArray = (SimulatorResourceModel[][][]) attribute
70 for (int i = 0; i < resModelArray.length; i++) {
71 SimulatorResourceAttribute indexAttribute = new SimulatorResourceAttribute(
72 "[" + Integer.toString(i) + "]",
73 new AttributeValue(resModelArray[i]), null);
74 mChildAttributes.put("[" + Integer.toString(i) + "]",
75 new AttributeElement(this, indexAttribute,
83 public Object getParent() {
87 public boolean hasChildren() {
88 if (mChildAttributes != null && mChildAttributes.size() > 0)
93 public Map<String, AttributeElement> getChildren() {
94 if (hasChildren() == true)
95 return mChildAttributes;
99 public SimulatorResourceAttribute getSimulatorResourceAttribute() {
103 public int getAutoUpdateId() {
104 return mAutoUpdateId;
107 public void setAutoUpdateId(int id) {
111 public boolean isAutoUpdateInProgress() {
112 return mAutoUpdateState;
115 public void setAutoUpdateState(boolean state) {
116 if (mAutoUpdateState != state) {
117 mAutoUpdateState = state;
119 UiListenerHandler.getInstance()
120 .attributeUpdatedUINotification(this);
124 public int getAutoUpdateInterval() {
125 return mAutoUpdateInterval;
128 public void setAutoUpdateInterval(int interval) {
129 mAutoUpdateInterval = interval;
132 public AutoUpdateType getAutoUpdateType() {
133 return mAutoUpdateType;
136 public void setAutoUpdateType(AutoUpdateType type) {
137 mAutoUpdateType = type;
140 public boolean isAutoUpdateSupport() {
141 return mAutoUpdateSupport;
144 public boolean isReadOnly() {
145 return (null == mAttribute.property());
148 public void update(SimulatorResourceAttribute attribute) {
149 if (attribute == null)
152 AttributeValue.TypeInfo typeInfo = attribute.value().typeInfo();
153 if (typeInfo.mType == AttributeValue.ValueType.RESOURCEMODEL) {
154 SimulatorResourceModel resModel = (SimulatorResourceModel) attribute
156 for (Map.Entry<String, SimulatorResourceAttribute> entry : resModel
157 .getAttributes().entrySet()) {
158 AttributeElement attributeElement = mChildAttributes.get(entry
160 if (attributeElement != null) {
161 attributeElement.update(entry.getValue());
162 } else // Display new attribute in UI
164 AttributeElement newAttribute = new AttributeElement(this,
165 entry.getValue(), false);
166 mChildAttributes.put(entry.getKey(), newAttribute);
168 UiListenerHandler.getInstance()
169 .attributeAddedUINotification(newAttribute);
172 } else if (typeInfo.mType == AttributeValue.ValueType.ARRAY
173 && typeInfo.mBaseType == AttributeValue.ValueType.RESOURCEMODEL) {
174 if (typeInfo.mDepth == 1) {
175 SimulatorResourceModel[] resModelArray = (SimulatorResourceModel[]) attribute
177 for (int i = 0; i < resModelArray.length; i++) {
178 SimulatorResourceAttribute indexAttribute = new SimulatorResourceAttribute(
179 "[" + Integer.toString(i) + "]",
180 new AttributeValue(resModelArray[i]), null);
181 AttributeElement attributeElement = mChildAttributes
182 .get("[" + Integer.toString(i) + "]");
183 if (attributeElement != null) {
184 attributeElement.update(indexAttribute);
185 } else // Display new attribute in UI
187 AttributeElement newAttribute = new AttributeElement(
188 this, indexAttribute, false);
189 mChildAttributes.put("[" + Integer.toString(i) + "]",
191 UiListenerHandler.getInstance()
192 .attributeAddedUINotification(newAttribute);
196 if (typeInfo.mDepth == 2) {
197 SimulatorResourceModel[][] resModelArray = (SimulatorResourceModel[][]) attribute
199 for (int i = 0; i < resModelArray.length; i++) {
200 SimulatorResourceAttribute indexAttribute = new SimulatorResourceAttribute(
201 "[" + Integer.toString(i) + "]",
202 new AttributeValue(resModelArray[i]), null);
203 AttributeElement attributeElement = mChildAttributes
204 .get("[" + Integer.toString(i) + "]");
205 if (attributeElement != null) {
206 attributeElement.update(indexAttribute);
207 } else // Display new attribute in UI
209 AttributeElement newAttribute = new AttributeElement(
210 this, indexAttribute, false);
211 mChildAttributes.put("[" + Integer.toString(i) + "]",
213 UiListenerHandler.getInstance()
214 .attributeAddedUINotification(newAttribute);
218 if (typeInfo.mDepth == 3) {
219 SimulatorResourceModel[][][] resModelArray = (SimulatorResourceModel[][][]) attribute
221 for (int i = 0; i < resModelArray.length; i++) {
222 SimulatorResourceAttribute indexAttribute = new SimulatorResourceAttribute(
223 "[" + Integer.toString(i) + "]",
224 new AttributeValue(resModelArray[i]), null);
225 AttributeElement attributeElement = mChildAttributes
226 .get("[" + Integer.toString(i) + "]");
227 if (attributeElement != null) {
228 attributeElement.update(indexAttribute);
229 } else // Display new attribute in UI
231 AttributeElement newAttribute = new AttributeElement(
232 this, indexAttribute, false);
233 mChildAttributes.put("[" + Integer.toString(i) + "]",
235 UiListenerHandler.getInstance()
236 .attributeAddedUINotification(newAttribute);
241 String currentValue = new AttributeValueStringConverter(
242 mAttribute.value()).toString();
243 String newValue = new AttributeValueStringConverter(
244 attribute.value()).toString();
245 if (!currentValue.equals(newValue)) {
246 mAttribute = attribute;
247 UiListenerHandler.getInstance().attributeUpdatedUINotification(
253 public void deepSetChildValue(SimulatorResourceAttribute attribute)
254 throws InvalidArgsException {
255 if (null == attribute || null == attribute.name())
258 AttributeValue.TypeInfo myValuetypeInfo = mAttribute.value().typeInfo();
259 if (myValuetypeInfo.mType == AttributeValue.ValueType.RESOURCEMODEL) {
260 SimulatorResourceModel resModel = (SimulatorResourceModel) mAttribute
262 if (resModel.containsAttribute(attribute.name()))
263 resModel.setAttributeValue(attribute.name(), attribute.value());
268 if (mParent instanceof AttributeElement)
269 ((AttributeElement) mParent).deepSetChildValue(mAttribute);