d363d0b5292b4650fce4a4aa53684fb5d1a3249e
[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_error_codes.h"
23 #include "simulator_exceptions_jni.h"
24 #include "simulator_utils_jni.h"
25 #include "jni_string.h"
26
27 class JniTypeInfo
28 {
29     public:
30         JniTypeInfo(JNIEnv *env) : m_env(env) {}
31
32         SimulatorResourceModel::TypeInfo toCpp(jobject jAttributeValue)
33         {
34             static jmethodID typeInfoMID = m_env->GetMethodID(gSimulatorClassRefs.attributeValueCls,
35                                            "typeInfo", "()Lorg/oic/simulator/AttributeValue$TypeInfo;");
36             static jfieldID typeFID = m_env->GetFieldID(gSimulatorClassRefs.attributeTypeInfoCls,
37                                       "mType", "Lorg/oic/simulator/AttributeValue$ValueType;");
38             static jfieldID baseTypeFID = m_env->GetFieldID(gSimulatorClassRefs.attributeTypeInfoCls,
39                                           "mBaseType", "Lorg/oic/simulator/AttributeValue$ValueType;");
40             static jfieldID depthFID = m_env->GetFieldID(gSimulatorClassRefs.attributeTypeInfoCls,
41                                        "mDepth", "I");
42
43             jobject jTypeInfo = m_env->CallObjectMethod(jAttributeValue, typeInfoMID);
44             jobject jType = m_env->GetObjectField(jTypeInfo, typeFID);
45             jobject jBaseType = m_env->GetObjectField(jTypeInfo, baseTypeFID);
46             jint jDepth = m_env->GetIntField(jTypeInfo, depthFID);
47
48             return SimulatorResourceModel::TypeInfo(getValueType(jType),
49                                                     getValueType(jBaseType), jDepth);
50         }
51
52     private:
53         SimulatorResourceModel::ValueType getValueType(jobject jValueType)
54         {
55             static jmethodID ordinalMID = m_env->GetMethodID(
56                                               gSimulatorClassRefs.attributeValueTypeCls, "ordinal", "()I");
57
58             int ordinal = m_env->CallIntMethod(jValueType, ordinalMID);
59             return SimulatorResourceModel::ValueType(ordinal);
60         }
61
62         JNIEnv *m_env;
63 };
64
65 class ValueConverterJava : public boost::static_visitor<jobject>
66 {
67     public:
68         ValueConverterJava(JNIEnv *env) : m_env(env) {}
69
70         jobject operator ()(const int &value)
71         {
72             static jmethodID integerCtor =
73                 m_env->GetMethodID(gSimulatorClassRefs.integerCls, "<init>", "(I)V");
74             return m_env->NewObject(gSimulatorClassRefs.integerCls,
75                                     integerCtor, value);
76         }
77
78         jobject operator ()(const double &value)
79         {
80             static jmethodID doubleCtor =
81                 m_env->GetMethodID(gSimulatorClassRefs.doubleCls, "<init>", "(D)V");
82             return m_env->NewObject(gSimulatorClassRefs.doubleCls,
83                                     doubleCtor, value);
84         }
85
86         jobject operator ()(const bool &value)
87         {
88             static jmethodID booleanCtor =
89                 m_env->GetMethodID(gSimulatorClassRefs.booleanCls, "<init>", "(Z)V");
90             return m_env->NewObject(gSimulatorClassRefs.booleanCls,
91                                     booleanCtor, value);
92         }
93
94         jobject operator ()(const std::string &value)
95         {
96             jstring stringValue = m_env->NewStringUTF(value.c_str());
97             return static_cast<jobject>(stringValue);
98         }
99
100         jobject operator ()(const SimulatorResourceModel &value)
101         {
102             return simulatorResourceModelToJava(m_env, const_cast<SimulatorResourceModel &>(value));
103         }
104
105         template <typename T>
106         jobject operator ()(const std::vector<T> &values)
107         {
108             jobjectArray jArray = m_env->NewObjectArray(values.size(), getClass(values), nullptr);
109             if (!jArray)
110                 return nullptr;
111
112             for (size_t index = 0; index < values.size(); index++)
113             {
114                 jobject element = operator()(values[index]);
115                 m_env->SetObjectArrayElement(jArray, index, element);
116             }
117
118             return jArray;
119         }
120
121         template <typename T>
122         jobject operator ()(const std::vector<std::vector<T>> &values)
123         {
124             jobjectArray jArray = m_env->NewObjectArray(values.size(), getClass(values), nullptr);
125             if (!jArray)
126                 return nullptr;
127
128             for (size_t index = 0; index < values.size(); index++)
129             {
130                 jobject element = operator()(values[index]);
131                 m_env->SetObjectArrayElement(jArray, index, element);
132             }
133
134             return jArray;
135         }
136
137         template <typename T>
138         jobject operator ()(const std::vector<std::vector<std::vector<T>>> &values)
139         {
140             jobjectArray jArray = m_env->NewObjectArray(values.size(), getClass(values), nullptr);
141             if (!jArray)
142                 return nullptr;
143
144             for (size_t index = 0; index < values.size(); index++)
145             {
146                 jobject element = operator()(values[index]);
147                 m_env->SetObjectArrayElement(jArray, index, element);
148             }
149
150             return jArray;
151         }
152
153     private:
154         jclass getClass(const std::vector<int> &)
155         {
156             return gSimulatorClassRefs.integerCls;
157         }
158
159         jclass getClass(const std::vector<std::vector<int>> &)
160         {
161             return gSimulatorClassRefs.integer1DArrayCls;
162         }
163
164         jclass getClass(const std::vector<std::vector<std::vector<int>>> &)
165         {
166             return gSimulatorClassRefs.integer2DArrayCls;
167         }
168
169         jclass getClass(const std::vector<double> &)
170         {
171             return gSimulatorClassRefs.doubleCls;
172         }
173
174         jclass getClass(const std::vector<std::vector<double>> &)
175         {
176             return gSimulatorClassRefs.double1DArrayCls;
177         }
178
179         jclass getClass(const std::vector<std::vector<std::vector<double>>> &)
180         {
181             return gSimulatorClassRefs.double2DArrayCls;
182         }
183
184         jclass getClass(const std::vector<bool> &)
185         {
186             return gSimulatorClassRefs.booleanCls;
187         }
188
189         jclass getClass(const std::vector<std::vector<bool>> &)
190         {
191             return gSimulatorClassRefs.boolean1DArrayCls;
192         }
193
194         jclass getClass(const std::vector<std::vector<std::vector<bool>>> &)
195         {
196             return gSimulatorClassRefs.boolean2DArrayCls;
197         }
198
199         jclass getClass(const std::vector<std::string> &)
200         {
201             return gSimulatorClassRefs.stringCls;
202         }
203
204         jclass getClass(const std::vector<std::vector<std::string>> &)
205         {
206             return gSimulatorClassRefs.string1DArrayCls;
207         }
208
209         jclass getClass(const std::vector<std::vector<std::vector<std::string>>> &)
210         {
211             return gSimulatorClassRefs.string2DArrayCls;
212         }
213
214         jclass getClass(const std::vector<SimulatorResourceModel> &)
215         {
216             return gSimulatorClassRefs.simulatorResourceModelCls;
217         }
218
219         jclass getClass(const std::vector<std::vector<SimulatorResourceModel>> &)
220         {
221             return gSimulatorClassRefs.simulatorResModel1DArrayCls;
222         }
223
224         jclass getClass(const std::vector<std::vector<std::vector<SimulatorResourceModel>>> &)
225         {
226             return gSimulatorClassRefs.simulatorResModel2DArrayCls;
227         }
228
229         JNIEnv *m_env;
230 };
231
232 class ValueConverterCpp
233 {
234     public:
235         ValueConverterCpp(JNIEnv *env, SimulatorResourceModel::TypeInfo &typeInfo,
236                           jobject &value) : m_env(env), m_typeInfo(typeInfo), m_value(value) {}
237
238         void convert()
239         {
240             switch (m_typeInfo.baseType())
241             {
242                 case SimulatorResourceModel::ValueType::INTEGER:
243                     return handleByDepth<int>();
244                 case SimulatorResourceModel::ValueType::DOUBLE:
245                     return handleByDepth<double>();
246                 case SimulatorResourceModel::ValueType::BOOLEAN:
247                     return handleByDepth<bool>();
248                 case SimulatorResourceModel::ValueType::STRING:
249                     return handleByDepth<std::string>();
250                 case SimulatorResourceModel::ValueType::RESOURCE_MODEL:
251                     return handleByDepth<SimulatorResourceModel>();
252                 case SimulatorResourceModel::ValueType::VECTOR:
253                 case SimulatorResourceModel::ValueType::UNKNOWN:
254                     break;
255             }
256         }
257
258         SimulatorResourceModel::ValueVariant get()
259         {
260             return std::move(m_result);
261         }
262
263     private:
264         template <typename T>
265         void handleByDepth()
266         {
267             if (0 == m_typeInfo.depth())
268             {
269                 T value;
270                 getValue(m_value, value);
271                 m_result = value;
272             }
273             else if (1 == m_typeInfo.depth())
274             {
275                 std::vector<T> value;
276                 getValue(m_value, value);
277                 m_result = value;
278             }
279             else if (2 == m_typeInfo.depth())
280             {
281                 std::vector<std::vector<T>> value;
282                 getValue(m_value, value);
283                 m_result = value;
284             }
285             else if (3 == m_typeInfo.depth())
286             {
287                 std::vector<std::vector<std::vector<T>>> value;
288                 getValue(m_value, value);
289                 m_result = value;
290             }
291         }
292
293         void getValue(jobject &jValue, int &value)
294         {
295             static jmethodID intValueMID = m_env->GetMethodID(
296                                                gSimulatorClassRefs.integerCls, "intValue", "()I");
297
298             jint temp = m_env->CallIntMethod(jValue, intValueMID);
299             value = temp;
300         }
301
302         void getValue(jobject &jValue, std::vector<int> &value)
303         {
304             jobjectArray array = (jobjectArray) jValue;
305             size_t length = m_env->GetArrayLength(array);
306             for (size_t i = 0; i < length; i++)
307             {
308                 jobject jElement = m_env->GetObjectArrayElement(array, i);
309
310                 int element;
311                 getValue(jElement, element);
312                 value.push_back(element);
313             }
314         }
315
316         void getValue(jobject &jValue, std::vector<std::vector<int>> &value)
317         {
318             jobjectArray array = (jobjectArray) jValue;
319             size_t length = m_env->GetArrayLength(array);
320             for (size_t i = 0; i < length; i++)
321             {
322                 jobject jElement = m_env->GetObjectArrayElement(array, i);
323
324                 std::vector<int> element;
325                 getValue(jElement, element);
326                 value.push_back(element);
327             }
328         }
329
330         void getValue(jobject &jValue, std::vector<std::vector<std::vector<int>>> &value)
331         {
332             jobjectArray array = (jobjectArray) jValue;
333             size_t length = m_env->GetArrayLength(array);
334             for (size_t i = 0; i < length; i++)
335             {
336                 jobject jElement = m_env->GetObjectArrayElement(array, i);
337
338                 std::vector<std::vector<int>> element;
339                 getValue(jElement, element);
340                 value.push_back(element);
341             }
342         }
343
344         void getValue(jobject &jValue, double &value)
345         {
346             static jmethodID doubleValueMID = m_env->GetMethodID(
347                                                   gSimulatorClassRefs.doubleCls, "doubleValue", "()D");
348
349             value = m_env->CallDoubleMethod(jValue, doubleValueMID);
350         }
351
352         void getValue(jobject &jValue, std::vector<double> &value)
353         {
354             jobjectArray array = (jobjectArray) jValue;
355             size_t length = m_env->GetArrayLength(array);
356             for (size_t i = 0; i < length; i++)
357             {
358                 jobject jElement = m_env->GetObjectArrayElement(array, i);
359
360                 double element;
361                 getValue(jElement, element);
362                 value.push_back(element);
363             }
364         }
365
366         void getValue(jobject &jValue, std::vector<std::vector<double>> &value)
367         {
368             jobjectArray array = (jobjectArray) jValue;
369             size_t length = m_env->GetArrayLength(array);
370             for (size_t i = 0; i < length; i++)
371             {
372                 jobject jElement = m_env->GetObjectArrayElement(array, i);
373
374                 std::vector<double> element;
375                 getValue(jElement, element);
376                 value.push_back(element);
377             }
378         }
379
380         void getValue(jobject &jValue, std::vector<std::vector<std::vector<double>>> &value)
381         {
382             jobjectArray array = (jobjectArray) jValue;
383             size_t length = m_env->GetArrayLength(array);
384             for (size_t i = 0; i < length; i++)
385             {
386                 jobject jElement = m_env->GetObjectArrayElement(array, i);
387
388                 std::vector<std::vector<double>> element;
389                 getValue(jElement, element);
390                 value.push_back(element);
391             }
392         }
393
394         void getValue(jobject &jValue, bool &value)
395         {
396             static jmethodID boolValueMID = m_env->GetMethodID(
397                                                 gSimulatorClassRefs.booleanCls, "booleanValue", "()Z");
398
399             value = m_env->CallBooleanMethod(jValue, boolValueMID);
400         }
401
402         void getValue(jobject &jValue, std::vector<bool> &value)
403         {
404             jobjectArray array = (jobjectArray) jValue;
405             size_t length = m_env->GetArrayLength(array);
406             for (size_t i = 0; i < length; i++)
407             {
408                 jobject jElement = m_env->GetObjectArrayElement(array, i);
409
410                 bool element;
411                 getValue(jElement, element);
412                 value.push_back(element);
413             }
414         }
415
416         void getValue(jobject &jValue, std::vector<std::vector<bool>> &value)
417         {
418             jobjectArray array = (jobjectArray) jValue;
419             size_t length = m_env->GetArrayLength(array);
420             for (size_t i = 0; i < length; i++)
421             {
422                 jobject jElement = m_env->GetObjectArrayElement(array, i);
423
424                 std::vector<bool> element;
425                 getValue(jElement, element);
426                 value.push_back(element);
427             }
428         }
429
430         void getValue(jobject &jValue, std::vector<std::vector<std::vector<bool>>> &value)
431         {
432             jobjectArray array = (jobjectArray) jValue;
433             size_t length = m_env->GetArrayLength(array);
434             for (size_t i = 0; i < length; i++)
435             {
436                 jobject jElement = m_env->GetObjectArrayElement(array, i);
437
438                 std::vector<std::vector<bool>> element;
439                 getValue(jElement, element);
440                 value.push_back(element);
441             }
442         }
443
444         void getValue(jobject &jValue, std::string &value)
445         {
446             jstring stringValue = (jstring) jValue;
447             JniString jniValue(m_env, stringValue);
448             value = jniValue.get();
449         }
450
451         void getValue(jobject jValue, std::vector<std::string> &value)
452         {
453             jobjectArray array = (jobjectArray) jValue;
454             size_t length = m_env->GetArrayLength(array);
455             for (size_t i = 0; i < length; i++)
456             {
457                 jobject jElement = m_env->GetObjectArrayElement(array, i);
458
459                 std::string element;
460                 getValue(jElement, element);
461                 value.push_back(element);
462             }
463         }
464
465         void getValue(jobject &jValue, std::vector<std::vector<std::string>> &value)
466         {
467             jobjectArray array = (jobjectArray) jValue;
468             size_t length = m_env->GetArrayLength(array);
469             for (size_t i = 0; i < length; i++)
470             {
471                 jobject jElement = m_env->GetObjectArrayElement(array, i);
472
473                 std::vector<std::string> element;
474                 getValue(jElement, element);
475                 value.push_back(element);
476             }
477         }
478
479         void getValue(jobject &jValue, std::vector<std::vector<std::vector<std::string>>> &value)
480         {
481             jobjectArray array = (jobjectArray) jValue;
482             size_t length = m_env->GetArrayLength(array);
483             for (size_t i = 0; i < length; i++)
484             {
485                 jobject jElement = m_env->GetObjectArrayElement(array, i);
486
487                 std::vector<std::vector<std::string>> element;
488                 getValue(jElement, element);
489                 value.push_back(element);
490             }
491         }
492
493         void getValue(jobject &jValue, SimulatorResourceModel &value)
494         {
495         }
496
497         void getValue(jobject &jValue, std::vector<SimulatorResourceModel> &value)
498         {
499         }
500
501         void getValue(jobject &jValue, std::vector<std::vector<SimulatorResourceModel>> &value)
502         {
503         }
504
505         void getValue(jobject &jValue, std::vector<std::vector<std::vector<SimulatorResourceModel>>> &value)
506         {
507         }
508
509         JNIEnv *m_env;
510         SimulatorResourceModel::TypeInfo &m_typeInfo;
511         jobject &m_value;
512         SimulatorResourceModel::ValueVariant m_result;
513 };
514
515 class JniAttributeValue
516 {
517     public:
518         static jobject toJava(JNIEnv *env, SimulatorResourceModel::Attribute &attribute)
519         {
520             auto value = attribute.getValue();
521             return toJava(env, value);
522         }
523
524         static jobject toJava(JNIEnv *env, SimulatorResourceModel::ValueVariant &value)
525         {
526             ValueConverterJava converter(env);
527             jobject jValue =  boost::apply_visitor(converter, value);
528
529             static jmethodID attrValueCtor = env->GetMethodID(
530                                                  gSimulatorClassRefs.attributeValueCls, "<init>", "(Ljava/lang/Object;)V");
531
532             return env->NewObject(gSimulatorClassRefs.attributeValueCls, attrValueCtor, jValue);
533         }
534
535         static SimulatorResourceModel::ValueVariant toCpp(JNIEnv *env, jobject &jAttributeValue)
536         {
537             static jmethodID getMID = env->GetMethodID(gSimulatorClassRefs.attributeValueCls,
538                                       "get", "()Ljava/lang/Object;");
539
540             SimulatorResourceModel::TypeInfo typeInfo = JniTypeInfo(env).toCpp(jAttributeValue);
541             jobject jValue = env->CallObjectMethod(jAttributeValue, getMID);
542
543             ValueConverterCpp converter(env, typeInfo, jValue);
544             converter.convert();
545             return converter.get();
546         }
547 };
548
549 class JniAttributeProperty
550 {
551     public:
552         static jobject toJava(JNIEnv *env,
553                               SimulatorResourceModel::AttributeProperty &property)
554         {
555             jobject jAttributeProperty = nullptr;
556             if (SimulatorResourceModel::AttributeProperty::Type::RANGE == property.type())
557             {
558                 static jmethodID propertyCtor = env->GetMethodID(
559                                                     gSimulatorClassRefs.attributePropertyCls, "<init>", "(DD)V");
560
561                 jAttributeProperty = env->NewObject(gSimulatorClassRefs.attributePropertyCls, propertyCtor,
562                                                     property.min(), property.max());
563             }
564             else
565             {
566                 static jmethodID propertyCtor = env->GetMethodID(
567                                                     gSimulatorClassRefs.attributePropertyCls, "<init>", "([Lorg/oic/simulator/AttributeValue;)V");
568
569                 jobjectArray jValueSet = env->NewObjectArray(property.valueSetSize(),
570                                          gSimulatorClassRefs.attributeValueCls, nullptr);
571                 int index = 0;
572                 for (auto &value : property.valueSet())
573                 {
574                     jobject jValue = JniAttributeValue::toJava(env, value);
575                     env->SetObjectArrayElement(jValueSet, index++, jValue);
576                 }
577
578                 jAttributeProperty = env->NewObject(gSimulatorClassRefs.attributePropertyCls, propertyCtor,
579                                                     jValueSet);
580             }
581
582             // Add child property
583             if (jAttributeProperty && property.getChildProperty())
584             {
585                 SimulatorResourceModel::AttributeProperty childProperty = *(property.getChildProperty());
586                 jobject jChildProperty = JniAttributeProperty::toJava(env,  property);
587                 if (jChildProperty)
588                 {
589                     static jfieldID childPropFID = env->GetFieldID(gSimulatorClassRefs.attributePropertyCls,
590                                                    "mChildProperty", "Lorg/oic/simulator/AttributeProperty;");
591                     env->SetObjectField(jAttributeProperty, childPropFID, jChildProperty);
592                 }
593             }
594
595             return jAttributeProperty;
596         }
597
598         static SimulatorResourceModel::AttributeProperty toCpp(JNIEnv *env, jobject jAttributeProperty)
599         {
600             static jfieldID typeFID = env->GetFieldID(gSimulatorClassRefs.attributePropertyCls,
601                                       "mType", "Lorg/oic/simulator/AttributeProperty$Type;");
602             static jfieldID minFID = env->GetFieldID(gSimulatorClassRefs.attributePropertyCls,
603                                      "mMin", "D");
604             static jfieldID maxFID = env->GetFieldID(gSimulatorClassRefs.attributePropertyCls,
605                                      "mMax", "D");
606             static jfieldID valueSetFID = env->GetFieldID(gSimulatorClassRefs.attributePropertyCls,
607                                           "mValueSet", "[Lorg/oic/simulator/AttributeValue;");
608             static jfieldID childPropFID = env->GetFieldID(gSimulatorClassRefs.attributePropertyCls,
609                                            "mChildProperty", "Lorg/oic/simulator/AttributeProperty;");
610             static jmethodID ordinalMID = env->GetMethodID(
611                                               gSimulatorClassRefs.attributePropertyTypeCls, "ordinal", "()I");
612
613             SimulatorResourceModel::AttributeProperty attributeProperty;
614             jobject jType = env->GetObjectField(jAttributeProperty, typeFID);
615             jdouble jMin = env->GetDoubleField(jAttributeProperty, minFID);
616             jdouble jMax = env->GetDoubleField(jAttributeProperty, maxFID);
617             jobjectArray jValueSet = (jobjectArray) env->GetObjectField(jAttributeProperty, valueSetFID);
618             jobject jChildProperty = env->GetObjectField(jAttributeProperty, childPropFID);
619
620             int ordinal = env->CallIntMethod(jType, ordinalMID);
621             switch (SimulatorResourceModel::AttributeProperty::Type(ordinal))
622             {
623                 case SimulatorResourceModel::AttributeProperty::Type::RANGE:
624                     {
625                         attributeProperty = SimulatorResourceModel::AttributeProperty(jMin, jMax);
626                     }
627                     break;
628
629                 case SimulatorResourceModel::AttributeProperty::Type::VALUE_SET:
630                     {
631                         std::vector<SimulatorResourceModel::ValueVariant> valueSet;
632                         size_t length = env->GetArrayLength(jValueSet);
633                         for (size_t i = 0; i < length; i++)
634                         {
635                             jobject jAttributeValue = env->GetObjectArrayElement(jValueSet, i);
636                             valueSet.push_back(JniAttributeValue::toCpp(env, jAttributeValue));
637                         }
638
639                         attributeProperty = SimulatorResourceModel::AttributeProperty(valueSet);
640                     }
641                     break;
642             }
643
644             // Set child property
645             if (jChildProperty)
646             {
647                 SimulatorResourceModel::AttributeProperty childProperty =
648                     JniAttributeProperty::toCpp(env, jAttributeProperty);
649                 attributeProperty.setChildProperty(childProperty);
650             }
651
652             return attributeProperty;
653         }
654 };
655
656 static jobject createHashMap(JNIEnv *env)
657 {
658     static jmethodID hashMapCtor = env->GetMethodID(
659                                        gSimulatorClassRefs.hashMapCls, "<init>", "()V");
660     return env->NewObject(gSimulatorClassRefs.hashMapCls, hashMapCtor);
661 }
662
663 static void addEntryToHashMap(JNIEnv *env, jobject mapobj, jobject key, jobject value)
664 {
665     if (!mapobj || !key || !value)
666         return;
667
668     static jmethodID hashMapPutMethod = env->GetMethodID(gSimulatorClassRefs.hashMapCls,
669                                         "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
670     env->CallObjectMethod(mapobj, hashMapPutMethod, key, value);
671 }
672
673 jobject simulatorResourceModelToJava(JNIEnv *env, SimulatorResourceModel &resModel)
674 {
675     jobject attributesMap = createHashMap(env);
676     jobject propertiesMap = createHashMap(env);
677     if (!attributesMap || !propertiesMap)
678         return nullptr;
679
680     for (auto &attributeEntry : resModel.getAttributes())
681     {
682         jstring jAttrName = env->NewStringUTF((attributeEntry.first).c_str());
683         jobject jAttributeValue = JniAttributeValue::toJava(env, attributeEntry.second);
684         addEntryToHashMap(env, attributesMap, jAttrName, jAttributeValue);
685
686         jobject jAttributeProperty = JniAttributeProperty::toJava(env, attributeEntry.second.getProperty());
687         addEntryToHashMap(env, propertiesMap, jAttrName, jAttributeProperty);
688     }
689
690     static jmethodID simulatorResourceModelCtor = env->GetMethodID(
691                 gSimulatorClassRefs.simulatorResourceModelCls, "<init>", "(Ljava/util/Map;Ljava/util/Map;)V");
692
693     return env->NewObject(gSimulatorClassRefs.simulatorResourceModelCls,
694                           simulatorResourceModelCtor, attributesMap, propertiesMap);
695 }
696
697 jobject simulatorResourceAttributeToJava(JNIEnv *env, SimulatorResourceModel::Attribute &attribute)
698 {
699     static jmethodID simulatorResAttributeCtor = env->GetMethodID(
700                 gSimulatorClassRefs.simulatorResourceAttributeCls, "<init>",
701                 "(Ljava/lang/String;Lorg/oic/simulator/AttributeValue;Lorg/oic/simulator/AttributeProperty;)V");
702
703     jstring jAttrName = env->NewStringUTF(attribute.getName().c_str());
704     jobject jAttributeValue = JniAttributeValue::toJava(env, attribute);
705     jobject jAttributeProperty = JniAttributeProperty::toJava(env, attribute.getProperty());
706
707     return env->NewObject(gSimulatorClassRefs.simulatorResourceAttributeCls,
708                           simulatorResAttributeCtor, jAttrName, jAttributeValue, jAttributeProperty);
709 }
710
711 bool simulatorResourceModelToCpp(JNIEnv *env, jobject jResModel, SimulatorResourceModel &resModel)
712 {
713     if (!jResModel)
714         return false;
715
716     static jfieldID valuesFID = env->GetFieldID(gSimulatorClassRefs.simulatorResourceModelCls,
717                                 "mValues", "Ljava/util/Map;");
718     static jfieldID propertiesFID = env->GetFieldID(gSimulatorClassRefs.simulatorResourceModelCls,
719                                     "mProperties", "Ljava/util/Map;");
720     static jmethodID entrySetMID = env->GetMethodID(gSimulatorClassRefs.mapCls, "entrySet",
721                                    "()Ljava/util/Set;");
722     static jmethodID iteratorMID = env->GetMethodID(gSimulatorClassRefs.setCls, "iterator",
723                                    "()Ljava/util/Iterator;");
724     static jmethodID hasNextMID = env->GetMethodID(gSimulatorClassRefs.iteratorCls, "hasNext",
725                                   "()Z");
726     static jmethodID nextMID = env->GetMethodID(gSimulatorClassRefs.iteratorCls, "next",
727                                "()Ljava/lang/Object;");
728     static jmethodID getKeyMID = env->GetMethodID(gSimulatorClassRefs.mapEntryCls, "getKey",
729                                  "()Ljava/lang/Object;");
730     static jmethodID getValueMID = env->GetMethodID(gSimulatorClassRefs.mapEntryCls, "getValue",
731                                    "()Ljava/lang/Object;");
732
733     jobject jValues = env->GetObjectField(jResModel, valuesFID);
734     jobject jProperties = env->GetObjectField(jResModel, propertiesFID);
735
736     if (jValues)
737     {
738         jobject entrySet = env->CallObjectMethod(jValues, entrySetMID);
739         jobject iterator = env->CallObjectMethod(entrySet, iteratorMID);
740         if (entrySet && iterator)
741         {
742             while (env->CallBooleanMethod(iterator, hasNextMID))
743             {
744                 jobject entry = env->CallObjectMethod(iterator, nextMID);
745                 jstring key = (jstring) env->CallObjectMethod(entry, getKeyMID);
746                 jobject value = env->CallObjectMethod(entry, getValueMID);
747                 resModel.add(JniString(env, key).get(), JniAttributeValue::toCpp(env, value));
748
749                 env->DeleteLocalRef(entry);
750                 env->DeleteLocalRef(key);
751                 env->DeleteLocalRef(value);
752             }
753         }
754     }
755
756     if (jProperties)
757     {
758         jobject entrySet = env->CallObjectMethod(jProperties, entrySetMID);
759         jobject iterator = env->CallObjectMethod(entrySet, iteratorMID);
760         if (entrySet && iterator)
761         {
762             while (env->CallBooleanMethod(iterator, hasNextMID))
763             {
764                 jobject entry = env->CallObjectMethod(iterator, nextMID);
765                 jstring key = (jstring) env->CallObjectMethod(entry, getKeyMID);
766                 jobject value = env->CallObjectMethod(entry, getValueMID);
767                 resModel.setAttributeProperty(JniString(env, key).get(),
768                                               JniAttributeProperty::toCpp(env, value));
769
770                 env->DeleteLocalRef(entry);
771                 env->DeleteLocalRef(key);
772                 env->DeleteLocalRef(value);
773             }
774         }
775     }
776
777     return true;
778 }
779
780 bool simulatorResourceAttributeToCpp(JNIEnv *env, jobject jAttribute,
781                                      SimulatorResourceModel::Attribute &attribute)
782 {
783     if (!jAttribute)
784         return false;
785
786     static jfieldID nameFID = env->GetFieldID(gSimulatorClassRefs.simulatorResourceAttributeCls,
787                               "mName", "Ljava/lang/String;");
788     static jfieldID valueFID = env->GetFieldID(gSimulatorClassRefs.simulatorResourceAttributeCls,
789                                "mValue", "Lorg/oic/simulator/AttributeValue;");
790     static jfieldID propertyFID = env->GetFieldID(gSimulatorClassRefs.simulatorResourceAttributeCls,
791                                   "mProperty", "Lorg/oic/simulator/AttributeProperty;");
792
793     jstring jAttributeName = (jstring) env->GetObjectField(jAttribute, nameFID);
794     jobject jAttributeValue = env->GetObjectField(jAttribute, valueFID);
795     jobject jAttributeProperty = env->GetObjectField(jAttribute, propertyFID);
796
797     if (!jAttributeName || !jAttributeValue)
798         return false;
799
800     JniString attrName(env, jAttributeName);
801     SimulatorResourceModel::ValueVariant value = JniAttributeValue::toCpp(env, jAttributeValue);
802
803     attribute.setName(attrName.get());
804     attribute.setValue(value);
805     if (jAttributeProperty)
806     {
807         SimulatorResourceModel::AttributeProperty property = JniAttributeProperty::toCpp(env,
808                 jAttributeProperty);
809         attribute.setProperty(property);
810     }
811
812     return true;
813 }
814
815 bool AttributeValueToCpp(JNIEnv *env, jobject jAttributeValue,
816                          SimulatorResourceModel::ValueVariant &value)
817 {
818     if (!jAttributeValue)
819         return false;
820
821     value = JniAttributeValue::toCpp(env, jAttributeValue);
822     return true;
823 }
824