Imported Upstream version 1.0.1
[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 "resource_attributes_jni.h"
24 #include "simulator_error_codes.h"
25 #include "simulator_jni_utils.h"
26
27 using namespace std;
28
29 extern SimulatorClassRefs gSimulatorClassRefs;
30
31 JSimulatorResourceModel::JSimulatorResourceModel(SimulatorResourceModel resModel)
32     : m_resourceModel(resModel)
33 {}
34
35 JSimulatorResourceModel::JSimulatorResourceModel(SimulatorResourceModelSP resModel)
36     : m_resModelPtr(resModel)
37 {}
38
39 bool JSimulatorResourceModel::getResourceModel(JNIEnv *env, jobject thiz,
40         SimulatorResourceModel &resModel)
41 {
42     JSimulatorResourceModel *resource = GetHandle<JSimulatorResourceModel>(env, thiz);
43     if (env->ExceptionCheck())
44     {
45         return false;
46     }
47
48     if (nullptr != resource->m_resModelPtr)
49         resModel = *(resource->m_resModelPtr.get());
50     else
51         resModel = resource->m_resourceModel;
52     return true;
53 }
54
55 SimulatorResourceModelSP JSimulatorResourceModel::getResourceModelPtr(JNIEnv *env, jobject thiz)
56 {
57     JSimulatorResourceModel *resource = GetHandle<JSimulatorResourceModel>(env, thiz);
58     if (env->ExceptionCheck())
59     {
60         return nullptr;
61     }
62
63     if (nullptr != resource->m_resModelPtr)
64         return resource->m_resModelPtr;
65     return nullptr;
66 }
67
68 jobject JSimulatorResourceModel::toJava(JNIEnv *env, jlong nativeHandle)
69 {
70     jobject resourceObj = (jobject) env->NewObject(gSimulatorClassRefs.classSimulatorResourceModel,
71                           gSimulatorClassRefs.classSimulatorResourceModelCtor, nativeHandle);
72     if (!resourceObj)
73     {
74         return NULL;
75     }
76     return resourceObj;
77 }
78
79 void JSimulatorResourceModel::toJava(JNIEnv *env, jobject thiz, jlong nativeHandle)
80 {
81     if (env && thiz && nativeHandle)
82     {
83         env->SetLongField(thiz, GetHandleField(env, thiz), nativeHandle);
84     }
85 }
86
87 static jobject createHashMap(JNIEnv *env)
88 {
89     jobject mapobj = env->NewObject(gSimulatorClassRefs.classHashMap,
90                                     gSimulatorClassRefs.classHashMapCtor);
91     return mapobj;
92 }
93
94 static void addEntryToHashMap(JNIEnv *env, jobject mapobj, jobject key, jobject value)
95 {
96     if (!mapobj || !key || !value)
97     {
98         return;
99     }
100
101     env->CallObjectMethod(mapobj, gSimulatorClassRefs.classHashMapPut, key, value);
102 }
103
104 JNIEXPORT void JNICALL
105 Java_org_oic_simulator_SimulatorResourceModel_create
106 (JNIEnv *env, jobject thiz)
107 {
108     SimulatorResourceModelSP resModel = std::make_shared<SimulatorResourceModel>();
109     JSimulatorResourceModel *jresModel = new JSimulatorResourceModel(resModel);
110     JSimulatorResourceModel::toJava(env, thiz, reinterpret_cast<jlong>(jresModel));
111 }
112
113 JNIEXPORT jint JNICALL
114 Java_org_oic_simulator_SimulatorResourceModel_size
115 (JNIEnv *env, jobject thiz)
116 {
117     SimulatorResourceModel resourceModel;
118     bool result = JSimulatorResourceModel::getResourceModel(env, thiz, resourceModel);
119     if (!result)
120     {
121         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "Resource model not found!");
122         return SIMULATOR_BAD_OBJECT;
123     }
124
125     return resourceModel.size();
126 }
127
128 JNIEXPORT jobject JNICALL
129 Java_org_oic_simulator_SimulatorResourceModel_getAttributes
130 (JNIEnv *env, jobject thiz)
131 {
132     SimulatorResourceModel resourceModel;
133     bool result = JSimulatorResourceModel::getResourceModel(env, thiz, resourceModel);
134     if (!result)
135     {
136         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "Resource model not found!");
137         return nullptr;
138     }
139
140     map<string, SimulatorResourceModel::Attribute> attributesMap = resourceModel.getAttributes();
141
142     // Create Java HashMap object
143     jobject jHashMap = NULL;
144     jHashMap = createHashMap(env);
145     if (!jHashMap)
146     {
147         throwSimulatorException(env, SIMULATOR_ERROR, "Java map creation failed!");
148         return nullptr;
149     }
150
151     for (auto & attributeEntry : attributesMap)
152     {
153         SimulatorResourceModel::Attribute attribute(attributeEntry.second);
154
155         // Create a object of ResourceAttribute java class
156         JResourceAttributeConverter converter(attribute);
157         jobject jAttribute = converter.toJava(env);
158
159         // Add an entry with attribute.first and javaSimualatorResourceAttribute to the HashMap
160         jstring jAttrName = env->NewStringUTF((attributeEntry.first).c_str());
161         addEntryToHashMap(env, jHashMap, static_cast<jobject>(jAttrName), jAttribute);
162         env->DeleteLocalRef(jAttrName);
163     }
164
165     return jHashMap;
166 }
167
168 JNIEXPORT jobject JNICALL
169 Java_org_oic_simulator_SimulatorResourceModel_getAttribute
170 (JNIEnv *env, jobject thiz, jstring jAttrName)
171 {
172     if (!jAttrName)
173     {
174         throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid attribute name!");
175         return nullptr;
176     }
177
178     const char *attrName = env->GetStringUTFChars(jAttrName, NULL);
179     if (!attrName)
180     {
181         throwSimulatorException(env, SIMULATOR_ERROR, "String error!");
182         return nullptr;
183     }
184
185     SimulatorResourceModel resourceModel;
186     bool result = JSimulatorResourceModel::getResourceModel(env, thiz, resourceModel);
187     if (!result)
188     {
189         env->ReleaseStringUTFChars(jAttrName, attrName);
190         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "Resource model not found!");
191         return nullptr;
192     }
193
194     SimulatorResourceModel::Attribute attribute;
195     bool found = resourceModel.getAttribute(attrName, attribute);
196     if (!found)
197     {
198         env->ReleaseStringUTFChars(jAttrName, attrName);
199         throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Attribute does not exist!");
200         return nullptr;
201     }
202
203     env->ReleaseStringUTFChars(jAttrName, attrName);
204
205     // Create a object of ResourceAttribute java class
206     JResourceAttributeConverter converter(attribute);
207     return converter.toJava(env);
208 }
209
210 JNIEXPORT void JNICALL
211 Java_org_oic_simulator_SimulatorResourceModel_addAttributeInt
212 (JNIEnv *env, jobject thiz, jstring jname, jint jvalue)
213 {
214     if (!jname)
215     {
216         throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid attribute name!");
217         return;
218     }
219
220     SimulatorResourceModelSP resModelPtr;
221     resModelPtr = JSimulatorResourceModel::getResourceModelPtr(env, thiz);
222     if (!resModelPtr)
223     {
224         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "Resource model not found!");
225         return;
226     }
227
228     const char *nameCstr = env->GetStringUTFChars(jname, NULL);
229     if (!nameCstr)
230     {
231         throwSimulatorException(env, SIMULATOR_ERROR, "String error!");
232         return;
233     }
234
235     std::string attrName(nameCstr);
236     int value = static_cast<int>(jvalue);
237     resModelPtr->addAttribute(attrName, value);
238
239     // Release created c string
240     env->ReleaseStringUTFChars(jname, nameCstr);
241 }
242
243 JNIEXPORT void JNICALL
244 Java_org_oic_simulator_SimulatorResourceModel_addAttributeDouble
245 (JNIEnv *env, jobject thiz, jstring jname, jdouble jvalue)
246 {
247     if (!jname)
248     {
249         throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid attribute name!");
250         return;
251     }
252
253     SimulatorResourceModelSP resModelPtr;
254     resModelPtr = JSimulatorResourceModel::getResourceModelPtr(env, thiz);
255     if (!resModelPtr)
256     {
257         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "Resource model not found!");
258         return;
259     }
260
261     const char *nameCstr = env->GetStringUTFChars(jname, NULL);
262     if (!nameCstr)
263     {
264         throwSimulatorException(env, SIMULATOR_ERROR, "String error!");
265         return;
266     }
267
268     std::string attrName(nameCstr);
269     double value = static_cast<double>(jvalue);
270     resModelPtr->addAttribute(attrName, value);
271
272     // Release created c string
273     env->ReleaseStringUTFChars(jname, nameCstr);
274 }
275
276 JNIEXPORT void JNICALL
277 Java_org_oic_simulator_SimulatorResourceModel_addAttributeBoolean
278 (JNIEnv *env, jobject thiz, jstring jname, jboolean jvalue)
279 {
280     if (!jname)
281     {
282         throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid attribute name!");
283         return;
284     }
285
286     SimulatorResourceModelSP resModelPtr;
287     resModelPtr = JSimulatorResourceModel::getResourceModelPtr(env, thiz);
288     if (!resModelPtr)
289     {
290         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "Resource model not found!");
291         return;
292     }
293
294     const char *nameCstr = env->GetStringUTFChars(jname, NULL);
295     if (!nameCstr)
296     {
297         throwSimulatorException(env, SIMULATOR_ERROR, "String error!");
298         return;
299     }
300
301     std::string attrName(nameCstr);
302     bool value = static_cast<bool>(jvalue);
303     resModelPtr->addAttribute(attrName, value);
304
305     // Release created c string
306     env->ReleaseStringUTFChars(jname, nameCstr);
307 }
308
309 JNIEXPORT void JNICALL
310 Java_org_oic_simulator_SimulatorResourceModel_addAttributeString
311 (JNIEnv *env, jobject thiz, jstring jname, jstring jvalue)
312 {
313     if (!jname)
314     {
315         throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Invalid attribute name!");
316         return;
317     }
318
319     if (!jvalue)
320     {
321         throwInvalidArgsException(env, SIMULATOR_INVALID_PARAM, "Attribute value cannot be null!");
322         return;
323     }
324
325     SimulatorResourceModelSP resModelPtr;
326     resModelPtr = JSimulatorResourceModel::getResourceModelPtr(env, thiz);
327     if (!resModelPtr)
328     {
329         throwSimulatorException(env, SIMULATOR_BAD_OBJECT, "Resource model not found!");
330         return;
331     }
332
333     const char *nameCstr = env->GetStringUTFChars(jname, NULL);
334     if (!nameCstr)
335     {
336         throwSimulatorException(env, SIMULATOR_ERROR, "String error!");
337         return;
338     }
339
340     const char *valueCstr = env->GetStringUTFChars(jvalue, NULL);
341     if (!valueCstr)
342     {
343         env->ReleaseStringUTFChars(jname, nameCstr);
344         throwSimulatorException(env, SIMULATOR_ERROR, "String error!");
345         return;
346     }
347
348     std::string attrName(nameCstr);
349     std::string value(valueCstr);
350     resModelPtr->addAttribute(attrName, value);
351
352     // Release created c string
353     env->ReleaseStringUTFChars(jname, nameCstr);
354     env->ReleaseStringUTFChars(jvalue, valueCstr);
355 }
356
357 JNIEXPORT void JNICALL
358 Java_org_oic_simulator_SimulatorResourceModel_dispose
359 (JNIEnv *env, jobject thiz)
360 {
361     JSimulatorResourceModel *resourceModel = GetHandle<JSimulatorResourceModel>(env, thiz);
362     delete resourceModel;
363 }