Displaying and editing the complex value types for attributes.
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ClientControllerPlugin / src / oic / simulator / clientcontroller / remoteresource / ResourceRepresentation.java
1 package oic.simulator.clientcontroller.remoteresource;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import org.oic.simulator.InvalidArgsException;
7 import org.oic.simulator.SimulatorResourceAttribute;
8 import org.oic.simulator.SimulatorResourceModel;
9
10 public class ResourceRepresentation {
11     private Map<String, AttributeElement> mAttributes = new HashMap<String, AttributeElement>();
12
13     public ResourceRepresentation(SimulatorResourceModel resourceModel) {
14         if (resourceModel != null && resourceModel.size() > 0) {
15             for (Map.Entry<String, SimulatorResourceAttribute> entry : resourceModel
16                     .getAttributes().entrySet())
17                 mAttributes.put(entry.getKey(), new AttributeElement(this,
18                         entry.getValue()));
19         }
20     }
21
22     public Map<String, AttributeElement> getAttributes() {
23         return mAttributes;
24     }
25
26     public void update(SimulatorResourceModel resourceModel,
27             boolean ramlUploaded) {
28         for (Map.Entry<String, SimulatorResourceAttribute> entry : resourceModel
29                 .getAttributes().entrySet()) {
30             AttributeElement attributeElement = mAttributes.get(entry.getKey());
31             if (attributeElement != null) {
32                 if (ramlUploaded)
33                     attributeElement.updateForRAMLUpload(entry.getValue());
34                 else
35                     attributeElement.update(entry.getValue());
36             } else // Display new attribute in UI
37             {
38                 AttributeElement newAttribute = new AttributeElement(this,
39                         entry.getValue());
40                 mAttributes.put(entry.getKey(), newAttribute);
41             }
42         }
43     }
44
45     public SimulatorResourceModel getModel() {
46         if (null == mAttributes || mAttributes.isEmpty()) {
47             return null;
48         }
49         SimulatorResourceModel model = new SimulatorResourceModel();
50         for (Map.Entry<String, AttributeElement> entry : mAttributes.entrySet()) {
51             AttributeElement attributeElement = mAttributes.get(entry.getKey());
52             if (attributeElement != null) {
53                 try {
54                     model.addAttribute(attributeElement
55                             .getSimulatorResourceAttribute());
56                 } catch (InvalidArgsException e) {
57                     e.printStackTrace();
58                 }
59             }
60         }
61         return model;
62     }
63
64     public SimulatorResourceModel getSelectedModel() {
65         if (null == mAttributes || mAttributes.isEmpty()) {
66             return null;
67         }
68         SimulatorResourceModel model = new SimulatorResourceModel();
69         for (Map.Entry<String, AttributeElement> entry : mAttributes.entrySet()) {
70             AttributeElement attributeElement = mAttributes.get(entry.getKey());
71             if (attributeElement != null && attributeElement.getPostState()) {
72                 try {
73                     model.addAttribute(attributeElement
74                             .getSimulatorResourceAttribute());
75                 } catch (InvalidArgsException e) {
76                     e.printStackTrace();
77                 }
78             }
79         }
80         return model;
81     }
82 }