Integrated resource model related changes with eclipse plug-ins.
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ClientControllerPlugin / src / oic / simulator / clientcontroller / remoteresource / ResourceRepresentation.java
1 /*
2  * Copyright 2015 Samsung Electronics All Rights Reserved.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package oic.simulator.clientcontroller.remoteresource;
18
19 import java.util.HashMap;
20 import java.util.Map;
21
22 import org.oic.simulator.AttributeValue;
23 import org.oic.simulator.InvalidArgsException;
24 import org.oic.simulator.SimulatorResourceAttribute;
25 import org.oic.simulator.SimulatorResourceModel;
26
27 public class ResourceRepresentation {
28     private Map<String, AttributeElement> mAttributes = new HashMap<String, AttributeElement>();
29
30     public ResourceRepresentation(SimulatorResourceModel resourceModel) {
31         if (resourceModel != null && resourceModel.size() > 0) {
32             for (Map.Entry<String, AttributeValue> entry : resourceModel.get()
33                     .entrySet())
34                 mAttributes.put(entry.getKey(),
35                         new AttributeElement(this,
36                                 new SimulatorResourceAttribute(entry.getKey(),
37                                         entry.getValue())));
38         }
39     }
40
41     public Map<String, AttributeElement> getAttributes() {
42         return mAttributes;
43     }
44
45     public void update(SimulatorResourceModel resourceModel,
46             boolean ramlUploaded) {
47         for (Map.Entry<String, AttributeValue> entry : resourceModel.get()
48                 .entrySet()) {
49             AttributeElement attributeElement = mAttributes.get(entry.getKey());
50             if (attributeElement != null) {
51                 attributeElement.update(
52                         new SimulatorResourceAttribute(entry.getKey(), entry
53                                 .getValue()), ramlUploaded);
54             } else {
55                 // Display new attribute in UI
56                 AttributeElement newAttribute = new AttributeElement(this,
57                         new SimulatorResourceAttribute(entry.getKey(),
58                                 entry.getValue()));
59                 mAttributes.put(entry.getKey(), newAttribute);
60             }
61         }
62     }
63
64     public SimulatorResourceModel getModel() {
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) {
72                 try {
73                     model.set(entry.getKey(), attributeElement
74                             .getSimulatorResourceAttribute().value());
75                 } catch (InvalidArgsException e) {
76                     e.printStackTrace();
77                 }
78             }
79         }
80         return model;
81     }
82
83     public SimulatorResourceModel getSelectedModel() {
84         if (null == mAttributes || mAttributes.isEmpty()) {
85             return null;
86         }
87         SimulatorResourceModel model = new SimulatorResourceModel();
88         for (Map.Entry<String, AttributeElement> entry : mAttributes.entrySet()) {
89             AttributeElement attributeElement = mAttributes.get(entry.getKey());
90             if (attributeElement != null && attributeElement.getPostState()) {
91                 try {
92                     model.set(entry.getKey(), attributeElement
93                             .getSimulatorResourceAttribute().value());
94                 } catch (InvalidArgsException e) {
95                     e.printStackTrace();
96                 }
97             }
98         }
99         return model;
100     }
101 }