275a7e688fcc901aa2fda41e94cd622f0002f2aa
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ServiceProviderPlugin / src / oic / simulator / serviceprovider / model / 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.serviceprovider.model;
18
19 import java.util.HashMap;
20 import java.util.Map;
21
22 import oic.simulator.serviceprovider.manager.UiListenerHandler;
23
24 import org.oic.simulator.AttributeValue;
25 import org.oic.simulator.SimulatorResourceAttribute;
26 import org.oic.simulator.SimulatorResourceModel;
27
28 public class ResourceRepresentation {
29     private Map<String, AttributeElement> mAttributes = new HashMap<String, AttributeElement>();
30
31     public ResourceRepresentation(
32             Map<String, SimulatorResourceAttribute> attributes)
33             throws NullPointerException {
34         if (attributes != null && attributes.size() > 0) {
35             for (Map.Entry<String, SimulatorResourceAttribute> entry : attributes
36                     .entrySet()) {
37                 mAttributes.put(entry.getKey(), new AttributeElement(this,
38                         entry.getValue(), true));
39             }
40         }
41     }
42
43     public Map<String, AttributeElement> getAttributes() {
44         return mAttributes;
45     }
46
47     public void update(SimulatorResourceModel model) {
48         if (null == model)
49             return;
50
51         for (Map.Entry<String, AttributeValue> entry : model.get().entrySet()) {
52             AttributeElement attributeElement = mAttributes.get(entry.getKey());
53             if (null != attributeElement) {
54                 if (!attributeElement.getEditLock())
55                     attributeElement.update(new SimulatorResourceAttribute(
56                             entry.getKey(), entry.getValue()));
57             } else {
58                 // Display new attribute in UI
59                 AttributeElement newAttribute = new AttributeElement(this,
60                         new SimulatorResourceAttribute(entry.getKey(),
61                                 entry.getValue()), true);
62                 mAttributes.put(entry.getKey(), newAttribute);
63
64                 UiListenerHandler.getInstance().attributeAddedUINotification(
65                         newAttribute);
66             }
67         }
68     }
69 }