Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / service / things-manager / sdk / java / jni / tm / src / jni_action.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 "jni_action.h"
22 #include "jni_things_manager_jvm.h"
23 #include "jni_getter.h"
24 #include "jni_setter.h"
25
26 #include <ActionSet.h>
27
28 #define JACTION_TARGET              ("target")
29 #define JACTION_LISTOF_CAPABILITY   ("listOfCapability")
30
31 JniAction::JniAction(JNIEnv *env, jobject obj) : JObject(env, obj)
32 {
33 }
34
35 JniAction::JniAction(JNIEnv *env) : JObject(env, TM_SERVICE_ACTION_PATH)
36 {
37 }
38
39 JniAction::~JniAction()
40 {
41 }
42
43 bool JniAction::getTarget(std::string &target)
44 {
45     //Retrieves target value from JniAction class object
46     return JGetter::getJStringField(m_pEnv, m_pObject, JACTION_TARGET, target);
47 }
48
49 bool JniAction::setTarget(const std::string target)
50 {
51     //Sets target value of JniAction class object
52     return JSetter::setJStringField(m_pEnv, m_pObject, JACTION_TARGET, target.c_str());
53 }
54
55 bool JniAction::getJniCapabilityValues(std::vector<OIC::Capability *> &capabilityList)
56 {
57     jclass jvectorClass = m_pEnv->FindClass(TM_JAVA_VECTOR_CLASS_PATH);
58     jmethodID vectorMethodID = m_pEnv->GetMethodID(jvectorClass, "get", "(I)Ljava/lang/Object;");
59     jmethodID vectorSizeMethodID = m_pEnv->GetMethodID(jvectorClass, "size", "()I");
60
61     jobject jAttrList = NULL;
62     bool res = JGetter::getJObjectField(m_pEnv, m_pObject,
63                                         JACTION_LISTOF_CAPABILITY, TM_JAVA_VECTOR_TYPE, jAttrList);
64
65     if (res == true)
66     {
67         capabilityList.clear();
68         int attrCount = m_pEnv->CallIntMethod(jAttrList, vectorSizeMethodID);
69         for (int i = 0; i < attrCount; i++)
70         {
71             JniCapability *attr_var = new JniCapability(m_pEnv,
72                     m_pEnv->CallObjectMethod(jAttrList, vectorMethodID, i));
73
74             OIC::Capability *cap = new OIC::Capability();
75             attr_var->getJniCapabilityValue(cap->capability);
76             attr_var->getJniCapabilityStatus(cap->status);
77             delete attr_var;
78             capabilityList.push_back(cap);
79         }
80     }
81     m_pEnv->DeleteLocalRef(jvectorClass);
82     return res;
83 }