Added UI Support for updating model arrays.
[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.ModelProperty;
25 import org.oic.simulator.SimulatorResourceAttribute;
26 import org.oic.simulator.SimulatorResourceModel;
27 import org.oic.simulator.client.SimulatorRequestModel;
28
29 public class ResourceRepresentation {
30     private Map<String, AttributeElement> mAttributes = new HashMap<String, AttributeElement>();
31
32     public ResourceRepresentation(SimulatorResourceModel resourceModel) {
33         if (resourceModel != null && resourceModel.size() > 0) {
34             for (Map.Entry<String, AttributeValue> entry : resourceModel.get()
35                     .entrySet())
36                 if (isCoreAttribute(entry.getKey())) {
37                     mAttributes.put(entry.getKey(), new AttributeElement(this,
38                             new SimulatorResourceAttribute(entry.getKey(),
39                                     entry.getValue())));
40                 }
41         }
42     }
43
44     public ResourceRepresentation(
45             Map<String, SimulatorResourceAttribute> attributes,
46             boolean onlyCoreAttributes) {
47         if (attributes != null && attributes.size() > 0) {
48             for (Map.Entry<String, SimulatorResourceAttribute> entry : attributes
49                     .entrySet()) {
50                 if (onlyCoreAttributes && !isCoreAttribute(entry.getKey())) {
51                     // Skip this attribute.
52                     continue;
53                 }
54
55                 mAttributes.put(entry.getKey(), new AttributeElement(this,
56                         entry.getValue()));
57             }
58         }
59     }
60
61     private boolean isCoreAttribute(String attName) {
62         if (null == attName || attName.isEmpty()) {
63             return false;
64         }
65
66         if (attName.equalsIgnoreCase("if") || attName.equalsIgnoreCase("rt")
67                 || attName.equalsIgnoreCase("p")
68                 || attName.equalsIgnoreCase("n")
69                 || attName.equalsIgnoreCase("id")) {
70             return false;
71         }
72
73         return true;
74     }
75
76     public Map<String, AttributeElement> getAttributes() {
77         return mAttributes;
78     }
79
80     public boolean updateAttributeProperties(
81             SimulatorRequestModel requestModel,
82             SimulatorResourceModel resourceModelRef) throws Exception {
83         if (null == requestModel || null == resourceModelRef) {
84             return false;
85         }
86
87         ModelProperty modelProp = requestModel.getRequestBodyModel();
88         if (null == modelProp) {
89             return false;
90         }
91
92         for (Map.Entry<String, AttributeValue> entry : resourceModelRef.get()
93                 .entrySet()) {
94             if (isCoreAttribute(entry.getKey())) {
95                 AttributeElement attributeElement = mAttributes.get(entry
96                         .getKey());
97                 if (attributeElement != null) {
98                     attributeElement
99                             .setAttributeProperty(new SimulatorResourceAttribute(
100                                     entry.getKey(), entry.getValue(), modelProp
101                                             .get(entry.getKey())));
102                 }
103             }
104         }
105
106         return true;
107     }
108
109     public SimulatorResourceModel getModel() {
110         if (null == mAttributes || mAttributes.isEmpty()) {
111             return null;
112         }
113         SimulatorResourceModel model = new SimulatorResourceModel();
114         for (Map.Entry<String, AttributeElement> entry : mAttributes.entrySet()) {
115             AttributeElement attributeElement = mAttributes.get(entry.getKey());
116             if (attributeElement != null) {
117                 try {
118                     model.set(entry.getKey(), attributeElement
119                             .getSimulatorResourceAttribute().value());
120                 } catch (InvalidArgsException e) {
121                     e.printStackTrace();
122                 }
123             }
124         }
125         return model;
126     }
127
128     public SimulatorResourceModel getSelectedModel() {
129         if (null == mAttributes || mAttributes.isEmpty()) {
130             return null;
131         }
132         SimulatorResourceModel model = new SimulatorResourceModel();
133         for (Map.Entry<String, AttributeElement> entry : mAttributes.entrySet()) {
134             AttributeElement attributeElement = mAttributes.get(entry.getKey());
135             if (attributeElement != null && attributeElement.getPostState()) {
136                 try {
137                     model.set(entry.getKey(), attributeElement
138                             .getSimulatorResourceAttribute().value());
139                 } catch (InvalidArgsException e) {
140                     e.printStackTrace();
141                 }
142             }
143         }
144         return model;
145     }
146 }