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