Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / service / simulator / java / jni / simulator_resource_attribute_jni.cpp
1 /******************************************************************
2  *
3  * Copyright 2016 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************/
20
21 #include "simulator_resource_attribute_jni.h"
22 #include "simulator_resource_model_jni.h"
23 #include "simulator_resource_model_schema_jni.h"
24 #include "simulator_utils_jni.h"
25 #include "jni_string.h"
26 #include "jni_map.h"
27
28 jobject SimulatorResourceAttributeToJava(JNIEnv *env,
29         const SimulatorResourceAttribute &attribute)
30 {
31     static jmethodID simulatorResAttributeCtor = env->GetMethodID(
32                 gSimulatorClassRefs.simulatorResourceAttributeCls, "<init>",
33                 "(Ljava/lang/String;Lorg/oic/simulator/AttributeValue;Lorg/oic/simulator/AttributeProperty;)V");
34
35     jstring jAttrName = env->NewStringUTF(attribute.getName().c_str());
36     jobject jAttrValue = AttributeValueToJava(env, attribute.getValue());
37     jobject jAttrProperty = AttributePropertyToJava(env, attribute.getProperty());
38
39     return env->NewObject(gSimulatorClassRefs.simulatorResourceAttributeCls,
40                           simulatorResAttributeCtor, jAttrName, jAttrValue, jAttrProperty);
41 }
42
43 bool SimulatorResourceAttributeToCpp(JNIEnv *env, jobject jAttribute,
44                                      SimulatorResourceAttribute &attribute)
45 {
46     if (!jAttribute)
47         return false;
48
49     static jfieldID nameFID = env->GetFieldID(gSimulatorClassRefs.simulatorResourceAttributeCls,
50                               "mName", "Ljava/lang/String;");
51     static jfieldID valueFID = env->GetFieldID(gSimulatorClassRefs.simulatorResourceAttributeCls,
52                                "mValue", "Lorg/oic/simulator/AttributeValue;");
53     static jfieldID propertyFID = env->GetFieldID(gSimulatorClassRefs.simulatorResourceAttributeCls,
54                                   "mProperty", "Lorg/oic/simulator/AttributeProperty;");
55
56     jstring jAttrName = (jstring) env->GetObjectField(jAttribute, nameFID);
57     jobject jAttrValue = env->GetObjectField(jAttribute, valueFID);
58     jobject jAttrProperty = env->GetObjectField(jAttribute, propertyFID);
59
60     if (!jAttrName || !jAttrValue)
61         return false;
62
63     JniString attrName(env, jAttrName);
64     AttributeValueVariant value;
65     AttributeValueToCpp(env, jAttrValue, value);
66
67     attribute.setName(attrName.get());
68     attribute.setValue(value);
69     if (jAttrProperty)
70     {
71         std::shared_ptr<AttributeProperty> property = AttributePropertyToCpp(env, jAttrProperty);
72         attribute.setProperty(property);
73     }
74
75     return true;
76 }
77
78 jobject SimulatorResourceAttributesToJava(JNIEnv *env,
79         const std::map<std::string, SimulatorResourceAttribute> &attributes)
80 {
81     JniMap jAttributesMap(env);
82
83     // Add attribute values for Attributes table
84     for (auto &attributeEntry : attributes)
85     {
86         jstring jAttrName = env->NewStringUTF((attributeEntry.first).c_str());
87         jobject jAttribute = SimulatorResourceAttributeToJava(env, attributeEntry.second);
88
89         jAttributesMap.put(jAttrName, jAttribute);
90     }
91
92     return jAttributesMap.get();
93 }