2 * Copyright 2015 Samsung Electronics All Rights Reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package oic.simulator.clientcontroller.remoteresource;
19 import java.util.HashMap;
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;
29 public class ResourceRepresentation {
30 private Map<String, AttributeElement> mAttributes = new HashMap<String, AttributeElement>();
32 public ResourceRepresentation(SimulatorResourceModel resourceModel) {
33 if (resourceModel != null && resourceModel.size() > 0) {
34 for (Map.Entry<String, AttributeValue> entry : resourceModel.get()
36 if (isCoreAttribute(entry.getKey())) {
37 mAttributes.put(entry.getKey(), new AttributeElement(this,
38 new SimulatorResourceAttribute(entry.getKey(),
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
50 if (onlyCoreAttributes && !isCoreAttribute(entry.getKey())) {
51 // Skip this attribute.
55 mAttributes.put(entry.getKey(), new AttributeElement(this,
61 private boolean isCoreAttribute(String attName) {
62 if (null == attName || attName.isEmpty()) {
66 if (attName.equalsIgnoreCase("if") || attName.equalsIgnoreCase("rt")
67 || attName.equalsIgnoreCase("p")
68 || attName.equalsIgnoreCase("n")
69 || attName.equalsIgnoreCase("id")) {
76 public Map<String, AttributeElement> getAttributes() {
80 public boolean updateAttributeProperties(
81 SimulatorRequestModel requestModel,
82 SimulatorResourceModel resourceModelRef) throws Exception {
83 if (null == requestModel || null == resourceModelRef) {
87 ModelProperty modelProp = requestModel.getRequestBodyModel();
88 if (null == modelProp) {
92 for (Map.Entry<String, AttributeValue> entry : resourceModelRef.get()
94 if (isCoreAttribute(entry.getKey())) {
95 AttributeElement attributeElement = mAttributes.get(entry
97 if (attributeElement != null) {
99 .setAttributeProperty(new SimulatorResourceAttribute(
100 entry.getKey(), entry.getValue(), modelProp
101 .get(entry.getKey())));
109 public SimulatorResourceModel getModel() {
110 if (null == mAttributes || mAttributes.isEmpty()) {
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) {
118 model.set(entry.getKey(), attributeElement
119 .getSimulatorResourceAttribute().value());
120 } catch (InvalidArgsException e) {
128 public SimulatorResourceModel getSelectedModel() {
129 if (null == mAttributes || mAttributes.isEmpty()) {
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()) {
137 model.set(entry.getKey(), attributeElement
138 .getSimulatorResourceAttribute().value());
139 } catch (InvalidArgsException e) {