<commit_before>/*
* //******************************************************************
* //
* // Copyright 2015 Intel Corporation.
* //
* //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
* //
* // Licensed under the Apache License, Version 2.0 (the "License");
* // you may not use this file except in compliance with the License.
* // You may obtain a copy of the License at
* //
* //      http://www.apache.org/licenses/LICENSE-2.0
* //
* // Unless required by applicable law or agreed to in writing, software
* // distributed under the License is distributed on an "AS IS" BASIS,
* // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* // See the License for the specific language governing permissions and
* // limitations under the License.
* //
* //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
*/

#include <map>

#include "JniOcRepresentation.h"
#include "JniUtils.h"

using namespace OC;

OCRepresentation* JniOcRepresentation::getOCRepresentationPtr(JNIEnv *env, jobject thiz)
{
    OCRepresentation *rep = GetHandle<OCRepresentation>(env, thiz);
    if (env->ExceptionCheck())
    {
        LOGE("Failed to get native handle from OcRepresentation");
    }
    if (!rep)
    {
        ThrowOcException(JNI_NO_NATIVE_POINTER, "");
    }
    return rep;
}

/*
* Class:     org_iotivity_base_OcRepresentation
* Method:    getValues
* Signature: ()Ljava/util/Map;
*/
JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcRepresentation_getValues
(JNIEnv *env, jobject thiz)
{
    LOGD("OcRepresentation_getValues");
    OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
    if (!rep)
    {
        return nullptr;
    }

    std::map<std::string, AttributeValue> values = rep->getValues();
    jobject jHashMap = env->NewObject(g_cls_HashMap, g_mid_HashMap_ctor);
    if (!jHashMap)
    {
        return nullptr;
    }

    for (std::map<std::string, AttributeValue>::const_iterator it = values.begin(); it != values.end(); it++)
    {
        jobject key = static_cast<jobject>(env->NewStringUTF(it->first.c_str()));
        jobject val = boost::apply_visitor(JObjectConverter(env), it->second);
        env->CallObjectMethod(jHashMap, g_mid_HashMap_put, key, val);
    }
    return jHashMap;
}

/*
* Class:     org_iotivity_base_OcRepresentation
* Method:    getValueN
* Signature: (Ljava/lang/String;)Ljava/lang/Object;
*/
JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcRepresentation_getValueN
(JNIEnv *env, jobject thiz, jstring jKey)
{
    LOGD("OcRepresentation_getValue");
    if (!jKey)
    {
        ThrowOcException(OC_STACK_INVALID_PARAM, "attributeKey cannot be null");
        return nullptr;
    }
    OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
    if (!rep)
    {
        return nullptr;
    }

    std::string key = env->GetStringUTFChars(jKey, nullptr);

    AttributeValue attrValue;
    if (!rep->getAttributeValue(key.c_str(), attrValue))
    {
        ThrowOcException(JNI_NO_SUCH_KEY, " attribute key does not exist");
        return nullptr;
    }
    return boost::apply_visitor(JObjectConverter(env), attrValue);
}

/*
* Class:     org_iotivity_base_OcRepresentation
* Method:    setValueInteger
* Signature: (Ljava/lang/String;I)V
*/
JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueInteger
(JNIEnv *env, jobject thiz, jstring jKey, jint jValue)
{
    LOGD("OcRepresentation_setValueInteger");
    if (!jKey)
    {
        ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
        return;
    }
    OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
    if (!rep)
    {
        return;
    }

    std::string str = env->GetStringUTFChars(jKey, nullptr);
    rep->setValue(str, static_cast<int>(jValue));
}

/*
* Class:     org_iotivity_base_OcRepresentation
* Method:    setValueDouble
* Signature: (Ljava/lang/String;D)V
*/
JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueDouble
(JNIEnv *env, jobject thiz, jstring jKey, jdouble jValue)
{
    LOGD("OcRepresentation_setValueDouble");
    if (!jKey)
    {
        ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
        return;
    }
    OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
    if (!rep)
    {
        return;
    }

    std::string str = env->GetStringUTFChars(jKey, nullptr);
    rep->setValue(str, static_cast<double>(jValue));
}

/*
* Class:     org_iotivity_base_OcRepresentation
* Method:    setValueBoolean
* Signature: (Ljava/lang/String;Z)V
*/
JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueBoolean
(JNIEnv *env, jobject thiz, jstring jKey, jboolean jValue)
{
    LOGD("OcRepresentation_setValueBoolean");
    if (!jKey)
    {
        ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
        return;
    }
    OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
    if (!rep)
    {
        return;
    }

    std::string str = env->GetStringUTFChars(jKey, nullptr);
    rep->setValue(str, static_cast<bool>(jValue));
}

/*
* Class:     org_iotivity_base_OcRepresentation
* Method:    setValueStringN
* Signature: (Ljava/lang/String;Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueStringN
(JNIEnv *env, jobject thiz, jstring jKey, jstring jValue)
{
    LOGD("OcRepresentation_setValueString");
    if (!jKey)
    {
        ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
        return;
    }
    OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
    if (!rep)
    {
        return;
    }

    std::string key = env->GetStringUTFChars(jKey, nullptr);
    std::string value = env->GetStringUTFChars(jValue, nullptr);

    rep->setValue(key, value);
}

/*
* Class:     org_iotivity_base_OcRepresentation
* Method:    setValueRepresentation
* Signature: (Ljava/lang/String;Lorg/iotivity/base/OcRepresentation;)V
*/
JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueRepresentation<commit_msg>Fix for JIRA IOT-2044.<commit_after>/*
* //******************************************************************
* //
* // Copyright 2015 Intel Corporation.
* //
* //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
* //
* // Licensed under the Apache License, Version 2.0 (the "License");
* // you may not use this file except in compliance with the License.
* // You may obtain a copy of the License at
* //
* //      http://www.apache.org/licenses/LICENSE-2.0
* //
* // Unless required by applicable law or agreed to in writing, software
* // distributed under the License is distributed on an "AS IS" BASIS,
* // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* // See the License for the specific language governing permissions and
* // limitations under the License.
* //
* //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
*/

#include <map>

#include "JniOcRepresentation.h"
#include "JniUtils.h"

using namespace OC;

OCRepresentation* JniOcRepresentation::getOCRepresentationPtr(JNIEnv *env, jobject thiz)
{
    OCRepresentation *rep = GetHandle<OCRepresentation>(env, thiz);
    if (env->ExceptionCheck())
    {
        LOGE("Failed to get native handle from OcRepresentation");
    }
    if (!rep)
    {
        ThrowOcException(JNI_NO_NATIVE_POINTER, "");
    }
    return rep;
}

/*
* Class:     org_iotivity_base_OcRepresentation
* Method:    getValues
* Signature: ()Ljava/util/Map;
*/
JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcRepresentation_getValues
(JNIEnv *env, jobject thiz)
{
    LOGD("OcRepresentation_getValues");
    OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
    if (!rep)
    {
        return nullptr;
    }

    std::map<std::string, AttributeValue> values = rep->getValues();
    jobject jHashMap = env->NewObject(g_cls_HashMap, g_mid_HashMap_ctor);
    if (!jHashMap)
    {
        return nullptr;
    }

    for (std::map<std::string, AttributeValue>::const_iterator it = values.begin(); it != values.end(); it++)
    {
        jobject key = static_cast<jobject>(env->NewStringUTF(it->first.c_str()));
        jobject val = boost::apply_visitor(JObjectConverter(env), it->second);
        env->CallObjectMethod(jHashMap, g_mid_HashMap_put, key, val);
    }
    return jHashMap;
}

/*
* Class:     org_iotivity_base_OcRepresentation
* Method:    getValueN
* Signature: (Ljava/lang/String;)Ljava/lang/Object;
*/
JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcRepresentation_getValueN
(JNIEnv *env, jobject thiz, jstring jKey)
{
    LOGD("OcRepresentation_getValue");
    if (!jKey)
    {
        ThrowOcException(OC_STACK_INVALID_PARAM, "attributeKey cannot be null");
        return nullptr;
    }
    OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
    if (!rep)
    {
        return nullptr;
    }

    std::string key = env->GetStringUTFChars(jKey, nullptr);

    AttributeValue attrValue;
    if (!rep->getAttributeValue(key.c_str(), attrValue))
    {
        ThrowOcException(JNI_NO_SUCH_KEY, " attribute key does not exist");
        return nullptr;
    }
    return boost::apply_visitor(JObjectConverter(env), attrValue);
}

/*
* Class:     org_iotivity_base_OcRepresentation
* Method:    setValueInteger
* Signature: (Ljava/lang/String;I)V
*/
JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueInteger
(JNIEnv *env, jobject thiz, jstring jKey, jint jValue)
{
    LOGD("OcRepresentation_setValueInteger");
    if (!jKey)
    {
        ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
        return;
    }
    OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
    if (!rep)
    {
        return;
    }

    std::string str = env->GetStringUTFChars(jKey, nullptr);
    rep->setValue(str, static_cast<int>(jValue));
}

/*
* Class:     org_iotivity_base_OcRepresentation
* Method:    setValueDouble
* Signature: (Ljava/lang/String;D)V
*/
JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueDouble
(JNIEnv *env, jobject thiz, jstring jKey, jdouble jValue)
{
    LOGD("OcRepresentation_setValueDouble");
    if (!jKey)
    {
        ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
        return;
    }
    OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
    if (!rep)
    {
        return;
    }

    std::string str = env->GetStringUTFChars(jKey, nullptr);
    rep->setValue(str, static_cast<double>(jValue));
}

/*
* Class:     org_iotivity_base_OcRepresentation
* Method:    setValueBoolean
* Signature: (Ljava/lang/String;Z)V
*/
JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueBoolean
(JNIEnv *env, jobject thiz, jstring jKey, jboolean jValue)
{
    LOGD("OcRepresentation_setValueBoolean");
    if (!jKey)
    {
        ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
        return;
    }
    OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
    if (!rep)
    {
        return;
    }

    std::string str = env->GetStringUTFChars(jKey, nullptr);
    rep->setValue(str, static_cast<bool>(jValue));
}

/*
* Class:     org_iotivity_base_OcRepresentation
* Method:    setValueStringN
* Signature: (Ljava/lang/String;Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueStringN
(JNIEnv *env, jobject thiz, jstring jKey, jstring jValue)
{
    LOGD("OcRepresentation_setValueString");
    if (!jKey)
    {
        ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
        return;
    }
    OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
    if (!rep)
    {
        return;
    }

    std::string key = env->GetStringUTFChars(jKey, nullptr);
    std::string value;
    if(jValue)
        value = env->GetStringUTFChars(jValue, nullptr);

    rep->setValue(key, value);
}

/*
* Class:     org_iotivity_base_OcRepresentation
* Method:    setValueRepresentation
* Signature: (Ljava/lang/String;Lorg/iotivity/base/OcRepresentation;)V
*/
JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueRepresentation
{'fraction_non_alphanumeric': 0.14281074937078833, 'fraction_numerical': 0.0016237720224080538, 'mean_word_length': 3.741339491916859, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '796948', 'n_tokens_mistral': 4151, 'n_tokens_neox': 4114, 'n_words': 812}