Collection resource not notifying its updated model when child resource is added...
[platform/upstream/iotivity.git] / service / simulator / java / jni / simulator_single_resource_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_exceptions_jni.h"
23 #include "simulator_utils_jni.h"
24 #include "jni_sharedobject_holder.h"
25 #include "jni_listener_holder.h"
26 #include "jni_string.h"
27 #include "jni_vector.h"
28
29 #include "simulator_single_resource.h"
30
31 #define VALIDATE_OBJECT(ENV, OBJECT) if (!OBJECT){throwBadObjectException(ENV, "No corresponsing native object!"); return;}
32 #define VALIDATE_OBJECT_RET(ENV, OBJECT, RET) if (!OBJECT){throwBadObjectException(ENV, "No corresponsing native object!"); return RET;}
33
34 SimulatorSingleResourceSP simulatorSingleResourceToCpp(JNIEnv *env, jobject object)
35 {
36     JniSharedObjectHolder<SimulatorSingleResource> *jniResource =
37         GetHandle<JniSharedObjectHolder<SimulatorSingleResource>>(env, object);
38     if (jniResource)
39         return jniResource->get();
40     return nullptr;
41 }
42
43 static AutomationType AutomationTypeToCpp(JNIEnv *env, jobject jType)
44 {
45     static jmethodID ordinalMID = env->GetMethodID(
46                                       gSimulatorClassRefs.automationTypeCls, "ordinal", "()I");
47
48     int ordinal = env->CallIntMethod(jType, ordinalMID);
49     return AutomationType(ordinal);
50 }
51
52 static void onAutoUpdationComplete(jobject listener, const std::string &uri, const int id)
53 {
54     JNIEnv *env = getEnv();
55     if (!env)
56         return;
57
58     jclass listenerCls = env->GetObjectClass(listener);
59     jmethodID listenerMethod = env->GetMethodID(listenerCls, "onUpdateComplete",
60                                "(Ljava/lang/String;I)V");
61
62     jstring jUri = env->NewStringUTF(uri.c_str());
63     env->CallVoidMethod(listener, listenerMethod, jUri, id);
64     releaseEnv();
65 }
66
67 #ifdef __cplusplus
68 extern "C" {
69 #endif
70
71 JNIEXPORT jobject JNICALL
72 Java_org_oic_simulator_server_SimulatorSingleResource_getAttribute
73 (JNIEnv *env, jobject object, jstring attrName)
74 {
75     VALIDATE_INPUT_RET(env, !attrName, "Attribute name is null!", nullptr)
76
77     SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object);
78     VALIDATE_OBJECT_RET(env, singleResource, nullptr)
79
80     JniString jniAttrName(env, attrName);
81     SimulatorResourceModel::Attribute attribute;
82     if (singleResource->getAttribute(jniAttrName.get(), attribute))
83         return simulatorResourceAttributeToJava(env, attribute);
84     return nullptr;
85 }
86
87 JNIEXPORT void JNICALL
88 Java_org_oic_simulator_server_SimulatorSingleResource_addAttribute
89 (JNIEnv *env, jobject object, jobject resAttribute)
90 {
91     VALIDATE_INPUT(env, !resAttribute, "Resource attribute is null!")
92
93     SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object);
94     VALIDATE_OBJECT(env, singleResource)
95
96     try
97     {
98         SimulatorResourceModel::Attribute attribute;
99         if (!simulatorResourceAttributeToCpp(env, resAttribute, attribute))
100         {
101             throwSimulatorException(env, SIMULATOR_ERROR,
102                                     "Failed to covnert SimulatorResourceAttribute java object!");
103             return;
104         }
105
106         singleResource->addAttribute(attribute);
107     }
108     catch (SimulatorException &e)
109     {
110         throwSimulatorException(env, e.code(), e.what());
111     }
112 }
113
114 JNIEXPORT void JNICALL
115 Java_org_oic_simulator_server_SimulatorSingleResource_updateAttribute
116 (JNIEnv *env, jobject object, jstring attrName, jobject attrValue)
117 {
118     VALIDATE_INPUT(env, !attrName, "Attribute name is null!")
119     VALIDATE_INPUT(env, !attrValue, "Attribute value is null!")
120
121     SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object);
122     VALIDATE_OBJECT(env, singleResource)
123
124     SimulatorResourceModel::ValueVariant value;
125     if (!AttributeValueToCpp(env, attrValue, value))
126     {
127         throwSimulatorException(env, SIMULATOR_ERROR,
128                                 "Failed to covnert AttributeValue java object!");
129         return;
130     }
131
132     SimulatorResourceModel::Attribute attribute(JniString(env, attrName).get());
133     attribute.setValue(value);
134     singleResource->updateAttributeValue(attribute);
135 }
136
137 JNIEXPORT void JNICALL
138 Java_org_oic_simulator_server_SimulatorSingleResource_removeAttribute
139 (JNIEnv *env, jobject object, jstring attrName)
140 {
141     VALIDATE_INPUT(env, !attrName, "Attribute name is null!")
142
143     SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object);
144     VALIDATE_OBJECT(env, singleResource)
145
146     try
147     {
148         JniString jniAttrName(env, attrName);
149         singleResource->removeAttribute(jniAttrName.get());
150     }
151     catch (InvalidArgsException &e)
152     {
153         throwInvalidArgsException(env, e.code(), e.what());
154     }
155 }
156
157 JNIEXPORT jint JNICALL
158 Java_org_oic_simulator_server_SimulatorSingleResource_startResourceUpdation
159 (JNIEnv *env, jobject object, jint type, jint interval, jobject listener)
160 {
161     VALIDATE_CALLBACK_RET(env, listener, -1)
162
163     SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object);
164     VALIDATE_OBJECT_RET(env, singleResource, -1)
165
166     jobject listenerRef = env->NewGlobalRef(listener);
167     updateCompleteCallback callback =  [listenerRef](const std::string & uri, const int id)
168     {
169         onAutoUpdationComplete(listenerRef, uri, id);
170     };
171
172     try
173     {
174         int id = singleResource->startResourceUpdation((1 == type) ? AutomationType::RECURRENT :
175                  AutomationType::NORMAL, interval, callback);
176         return id;
177     }
178     catch (InvalidArgsException &e)
179     {
180         throwInvalidArgsException(env, e.code(), e.what());
181     }
182     catch (SimulatorException &e)
183     {
184         throwSimulatorException(env, e.code(), e.what());
185     }
186
187     return -1;
188 }
189
190 JNIEXPORT jint JNICALL
191 Java_org_oic_simulator_server_SimulatorSingleResource_startAttributeUpdation
192 (JNIEnv *env, jobject object, jstring attrName, jobject type, jint interval, jobject listener)
193 {
194     VALIDATE_INPUT_RET(env, !attrName, "Attribute name is null!", -1)
195     VALIDATE_CALLBACK_RET(env, listener, -1)
196
197     SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object);
198     VALIDATE_OBJECT_RET(env, singleResource, -1)
199
200     jobject listenerRef = env->NewGlobalRef(listener);
201     updateCompleteCallback callback =  [listenerRef](const std::string & uri, const int id)
202     {
203         onAutoUpdationComplete(listenerRef, uri, id);
204     };
205
206     JniString jniAttrName(env, attrName);
207     AutomationType automationType = AutomationTypeToCpp(env, type);
208
209     try
210     {
211         int id = singleResource->startAttributeUpdation(jniAttrName.get(), automationType,
212                  interval, callback);
213         return id;
214     }
215     catch (InvalidArgsException &e)
216     {
217         throwInvalidArgsException(env, e.code(), e.what());
218     }
219     catch (SimulatorException &e)
220     {
221         throwSimulatorException(env, e.code(), e.what());
222     }
223
224     return -1;
225 }
226
227 JNIEXPORT void JNICALL
228 Java_org_oic_simulator_server_SimulatorSingleResource_stopUpdation
229 (JNIEnv *env, jobject object, jint id)
230 {
231     SimulatorSingleResourceSP singleResource = simulatorSingleResourceToCpp(env, object);
232     VALIDATE_OBJECT(env, singleResource)
233
234     singleResource->stopUpdation(id);
235 }
236
237 JNIEXPORT void JNICALL
238 Java_org_oic_simulator_server_SimulatorSingleResource_dispose
239 (JNIEnv *env, jobject object)
240 {
241     JniSharedObjectHolder<SimulatorSingleResource> *resource =
242         GetHandle<JniSharedObjectHolder<SimulatorSingleResource>>(env, object);
243     delete resource;
244 }
245
246 #ifdef __cplusplus
247 }
248 #endif