4280f32437891b8aaf1cd3cb45e37654b6fb9f99
[platform/upstream/iotivity.git] / service / simulator / java / jni / simulator_resource_model_jni.cpp
1 /******************************************************************
2  *
3  * Copyright 2015 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_model_jni.h"
22 #include "simulator_common_jni.h"
23 #include "simulator_resource_attributes_jni.h"
24 #include "simulator_error_codes.h"
25
26 using namespace std;
27
28 extern SimulatorClassRefs gSimulatorClassRefs;
29
30 JniSimulatorResourceModel::JniSimulatorResourceModel(SimulatorResourceModel resourceModel)
31     : m_resourceModel(resourceModel)
32 {}
33
34 bool JniSimulatorResourceModel::getResourceModel(JNIEnv *env, jobject thiz,
35         SimulatorResourceModel &resourceModel)
36 {
37     JniSimulatorResourceModel *resource = GetHandle<JniSimulatorResourceModel>(env, thiz);
38     if (env->ExceptionCheck())
39     {
40         cout << "Exception while converting the nativeHandle to JniSimulatorResourceModel" << endl;
41         return false;
42     }
43     resourceModel = resource->m_resourceModel;
44     return true;
45 }
46
47 jobject JniSimulatorResourceModel::toJava(JNIEnv *env, jlong resource)
48 {
49     jobject resourceObj = (jobject) env->NewObject(gSimulatorClassRefs.classSimulatorResourceModel,
50                           gSimulatorClassRefs.classSimulatorResourceModelCtor, resource);
51     if (!resourceObj)
52     {
53         return NULL;
54     }
55     return resourceObj;
56 }
57
58 static jobject createHashMap(JNIEnv *env)
59 {
60     jobject mapobj = env->NewObject(gSimulatorClassRefs.classHashMap,
61                                     gSimulatorClassRefs.classHashMapCtor);
62     return mapobj;
63 }
64
65 static void addEntryToHashMap(JNIEnv *env, jobject mapobj, jobject key, jobject value)
66 {
67     if (!mapobj || !key || !value)
68     {
69         return;
70     }
71
72     env->CallObjectMethod(mapobj, gSimulatorClassRefs.classHashMapPut, key, value);
73 }
74
75 JNIEXPORT jint JNICALL
76 Java_org_iotivity_simulator_SimulatorResourceModel_size
77 (JNIEnv *env, jobject thiz)
78 {
79     SimulatorResourceModel resourceModel;
80     bool result = JniSimulatorResourceModel::getResourceModel(env, thiz, resourceModel);
81     if (!result)
82     {
83         return SIMULATOR_ERROR;
84     }
85
86     return resourceModel.size();
87 }
88
89 JNIEXPORT jobject JNICALL
90 Java_org_iotivity_simulator_SimulatorResourceModel_getAttributes
91 (JNIEnv *env, jobject thiz)
92 {
93     SimulatorResourceModel resourceModel;
94     bool result = JniSimulatorResourceModel::getResourceModel(env, thiz, resourceModel);
95     if (!result)
96     {
97         return NULL;
98     }
99
100     map<string, SimulatorResourceModel::Attribute> attributesMap = resourceModel.getAttributes();
101
102     // Create Java HashMap object
103     jobject jHashMap = NULL;
104     jHashMap = createHashMap(env);
105     if (!jHashMap)
106     {
107         return NULL;
108     }
109
110     for (auto & attributeEntry : attributesMap)
111     {
112
113         // Create JniSimulatorResourceAttribute object and put the attribute.second into it
114         SimulatorResourceModel::Attribute *attribute = new SimulatorResourceModel::Attribute(
115             attributeEntry.second);
116
117         // Create a java object for SimulatorResourceAttribute
118         jobject jAttribute = JniSimulatorResourceAttribute::toJava(env, reinterpret_cast<jlong>(attribute));
119
120         // Add an entry with attribute.first and javaSimualatorResourceAttribute to the HashMap
121         jstring jAttrName = env->NewStringUTF((attributeEntry.first).c_str());
122         addEntryToHashMap(env, jHashMap, static_cast<jobject>(jAttrName), jAttribute);
123         env->DeleteLocalRef(jAttrName);
124     }
125     return jHashMap;
126 }
127
128 JNIEXPORT jobject JNICALL
129 Java_org_iotivity_simulator_SimulatorResourceModel_getAttribute
130 (JNIEnv *env, jobject thiz, jstring jAttrName)
131 {
132     if (!jAttrName)
133     {
134         std::cout << "getAttribute: AttributeName is Empty";
135         return NULL;
136     }
137
138     const char *attrName = env->GetStringUTFChars(jAttrName, NULL);
139     if (!attrName)
140     {
141         std::cout << "getAttribute: Failed to convert jstring to char string!";
142         return NULL;
143     }
144
145     SimulatorResourceModel resourceModel;
146     bool result = JniSimulatorResourceModel::getResourceModel(env, thiz, resourceModel);
147     if (!result)
148     {
149         std::cout << "getAttribute: getResourceModel failed!";
150         env->ReleaseStringUTFChars(jAttrName, attrName);
151         return NULL;
152     }
153
154     SimulatorResourceModel::Attribute *attribute = new SimulatorResourceModel::Attribute();
155     bool found = resourceModel.getAttribute(attrName, *attribute);
156     if (!found)
157     {
158         std::cout << "getAttribute: Attribute not found in ResourceModel!";
159         env->ReleaseStringUTFChars(jAttrName, attrName);
160         delete attribute;
161         return NULL;
162     }
163
164     env->ReleaseStringUTFChars(jAttrName, attrName);
165
166     // Create a java object for SimulatorResourceAttribute
167     jobject jsimulatorResourceAttribute = JniSimulatorResourceAttribute::toJava(env,
168                                           reinterpret_cast<jlong>(attribute));
169     return jsimulatorResourceAttribute;
170 }
171
172 JNIEXPORT jobjectArray JNICALL
173 Java_org_iotivity_simulator_SimulatorResourceModel_getAllowedValues
174 (JNIEnv *env, jobject thiz, jstring jAttrName)
175 {
176     if (!jAttrName)
177     {
178         std::cout << "getAllowedValues: AttributeName is Empty";
179         return NULL;
180     }
181
182     const char *attrName = env->GetStringUTFChars(jAttrName, NULL);
183     if (!attrName)
184     {
185         std::cout << "getAllowedValues: Failed to convert jstring to char string!";
186         env->ReleaseStringUTFChars(jAttrName, attrName);
187         return NULL;
188     }
189
190     SimulatorResourceModel resourceModel;
191     bool result = JniSimulatorResourceModel::getResourceModel(env, thiz, resourceModel);
192     if (!result)
193     {
194         std::cout << "getAllowedValues: getResourceModel failed!";
195         env->ReleaseStringUTFChars(jAttrName, attrName);
196         return NULL;
197     }
198
199     SimulatorResourceModel::Attribute *attribute = new SimulatorResourceModel::Attribute();
200     bool found = resourceModel.getAttribute(attrName, *attribute);
201     if (!found)
202     {
203         std::cout << "getAllowedValues: Attribute not found in ResourceModel!";
204         env->ReleaseStringUTFChars(jAttrName, attrName);
205         delete attribute;
206         return NULL;
207     }
208
209     env->ReleaseStringUTFChars(jAttrName, attrName);
210
211     std::vector<std::string> values = attribute->allowedValuesToVectorString();
212
213     int size = attribute->getAllowedValuesSize();
214
215     // Create a jObjectArray for AllowedValues vector.
216     jobjectArray allowedValuesArr = env->NewObjectArray(size, env->FindClass("java/lang/String"),
217                                     env->NewStringUTF(""));
218
219     int i = 0;
220     for (std::vector<std::string>::iterator it = values.begin(); it != values.end(); ++it, i++)
221     {
222         env->SetObjectArrayElement(allowedValuesArr, i, env->NewStringUTF((*it).c_str()));
223     }
224     return allowedValuesArr;
225 }
226
227 JNIEXPORT void JNICALL
228 Java_org_iotivity_simulator_SimulatorResourceModel_dispose
229 (JNIEnv *env, jobject thiz)
230 {
231     JniSimulatorResourceModel *resourceModel = GetHandle<JniSimulatorResourceModel>(env, thiz);
232     delete resourceModel;
233 }
234