Merge branch 'upstream' into tizen
[platform/upstream/iotivity.git] / service / resource-container / android / resource-container / src / main / jni / JniRcsResourceAttributes.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 "JniRcsResourceAttributes.h"
22
23 #include "JNIEnvWrapper.h"
24 #include "JavaClasses.h"
25 #include "JavaLocalRef.h"
26 #include "JniRcsValue.h"
27 #include "Log.h"
28 #include "Verify.h"
29
30 using namespace OIC::Service;
31
32 #define LOG_TAG "JNI-RCSResourceAttributes"
33
34 namespace
35 {
36     jclass g_cls_RCSResourceAttributes;
37
38     jmethodID g_ctor_RCSResourceAttributes;
39
40     jfieldID g_field_mCache;
41 }
42
43 void initRCSResourceAttributes(JNIEnvWrapper* env)
44 {
45     g_cls_RCSResourceAttributes = env->FindClassAsGlobalRef(CLS_NAME_RESOURCEATTRIBUTES);
46     g_ctor_RCSResourceAttributes = env->GetConstructorID(g_cls_RCSResourceAttributes, "()V");
47
48     g_field_mCache = env->GetFieldID(g_cls_RCSResourceAttributes, "mCache", AS_SIG(CLS_NAME_MAP));
49 }
50
51 void clearRCSResourceAttributes(JNIEnvWrapper* env)
52 {
53     env->DeleteGlobalRef(g_cls_RCSResourceAttributes);
54 }
55
56 jobject newAttributesObject(JNIEnv* env, const RCSResourceAttributes& attrs)
57 {
58     jobject obj = env->NewObject(g_cls_RCSResourceAttributes, g_ctor_RCSResourceAttributes);
59     VERIFY_NO_EXC_RET_DEF(env);
60
61     jobject mapObj = env->GetObjectField(obj, g_field_mCache);
62
63     //EXPECT(mapObj, "Map is null.");
64     for (const auto& p : attrs) {
65         JavaLocalObject keyObj{ env, newStringObject(env, p.key()) };
66         //VERIFY_NO_EXC(env);
67
68         JavaLocalObject valueObj{ env, newRCSValueObject(env, p.value()) };
69         //VERIFY_NO_EXC(env);
70
71         invoke_Map_put(env, mapObj, keyObj, valueObj);
72         //VERIFY_NO_EXC(env);
73     }
74
75  //   setSafeNativeHandle< RCSResourceAttributes >(env, obj, attrs);
76
77     return obj;
78 }
79
80 jobject newAttributesObject(JNIEnvWrapper* env, const RCSResourceAttributes& attrs)
81 {
82     jobject obj = env->NewObject(g_cls_RCSResourceAttributes, g_ctor_RCSResourceAttributes);
83
84     jobject mapObj = env->GetObjectField(obj, g_field_mCache);
85
86     //EXPECT(mapObj, "Map is null.");
87     for (const auto& p : attrs) {
88         JavaLocalObject keyObj{ env, newStringObject(env, p.key()) };
89         //VERIFY_NO_EXC(env);
90
91         JavaLocalObject valueObj{ env, newRCSValueObject(env, p.value()) };
92         //VERIFY_NO_EXC(env);
93
94         invoke_Map_put(env, mapObj, keyObj, valueObj);
95         //VERIFY_NO_EXC(env);
96     }
97
98 //    setSafeNativeHandle< RCSResourceAttributes >(env, obj, attrs);
99
100     return obj;
101 }
102
103 RCSResourceAttributes toNativeAttributes(JNIEnv* env, jobject attrsObj)
104 {
105     EXPECT_RET(attrsObj, "attrsObj is null!", { });
106
107     JNIEnvWrapper envWrapper{ env };
108
109     try
110     {
111         return toNativeAttributes(&envWrapper, attrsObj);
112     }
113     catch (const JavaException&)
114     {
115         return {};
116     }
117 }
118
119 RCSResourceAttributes toNativeAttributes(JNIEnvWrapper* env, jobject attrsObj)
120 {
121     EXPECT_RET(attrsObj, "attrsObj is null!", { });
122
123     RCSResourceAttributes attrs;
124
125     /*if (hasNativeHandle(env, attrsObj))
126     {
127         attrs = getNativeHandleAs< RCSResourceAttributes >(env, attrsObj);
128     }*/
129     LOGD("writeNativeAttributesFromMap");
130
131     writeNativeAttributesFromMap(env,
132             JavaLocalObject{ env, env->GetObjectField(attrsObj, g_field_mCache) }, attrs);
133
134     return attrs;
135 }
136
137 void writeNativeAttributesFromMap(JNIEnv* env, jobject mapObj, RCSResourceAttributes& targetAttrs)
138 {
139     JNIEnvWrapper envWrapper{ env };
140
141     try
142     {
143         return writeNativeAttributesFromMap(&envWrapper, mapObj, targetAttrs);
144     }
145     catch (const JavaException&)
146     {
147     }
148 }
149
150 void writeNativeAttributesFromMap(JNIEnvWrapper* env, jobject mapObj,
151         RCSResourceAttributes& targetAttrs)
152 {
153     LOGD("in write native attributes from map");
154     LOGD("invoke map entry set");
155     JavaLocalObject setObj{ env, invoke_Map_entrySet(env, mapObj) };
156     LOGD("invoke set iterator");
157     JavaLocalObject iterObj{ env, invoke_Set_iterator(env, setObj) };
158     LOGD("invoke has next");
159     while (invoke_Iterator_hasNext(env, iterObj))
160     {
161         LOGD("invoke entry obj next");
162         JavaLocalObject entryObj{ env, invoke_Iterator_next(env, iterObj) };
163         LOGD("invoke key obj next");
164         JavaLocalObject keyObj{ env, invoke_MapEntry_getKey(env, entryObj) };
165         LOGD("invoke value obj next");
166         JavaLocalObject valueObj{ env, invoke_MapEntry_getValue(env, entryObj) };
167         LOGD("invoke toStdString");
168         auto key = toStdString(env, static_cast< jstring >(keyObj.get()));
169         LOGD("invoke toNativeAttrsvalue");
170         targetAttrs[std::move(key)] = toNativeAttrsValue(env, valueObj);
171     }
172     LOGD("in write native attributes from map finished");
173 }
174