1115cd2caSopenharmony_ci/* 2115cd2caSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3115cd2caSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4115cd2caSopenharmony_ci * you may not use this file except in compliance with the License. 5115cd2caSopenharmony_ci * You may obtain a copy of the License at 6115cd2caSopenharmony_ci * 7115cd2caSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8115cd2caSopenharmony_ci * 9115cd2caSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10115cd2caSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11115cd2caSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12115cd2caSopenharmony_ci * See the License for the specific language governing permissions and 13115cd2caSopenharmony_ci * limitations under the License. 14115cd2caSopenharmony_ci */ 15115cd2caSopenharmony_ci 16115cd2caSopenharmony_ci#include "contacts_build.h" 17115cd2caSopenharmony_ci 18115cd2caSopenharmony_ci#include "result_convert.h" 19115cd2caSopenharmony_ci 20115cd2caSopenharmony_cinamespace OHOS { 21115cd2caSopenharmony_cinamespace ContactsApi { 22115cd2caSopenharmony_ciContactsBuild::ContactsBuild(void) 23115cd2caSopenharmony_ci{ 24115cd2caSopenharmony_ci} 25115cd2caSopenharmony_ci 26115cd2caSopenharmony_ciContactsBuild::~ContactsBuild() 27115cd2caSopenharmony_ci{ 28115cd2caSopenharmony_ci} 29115cd2caSopenharmony_ci 30115cd2caSopenharmony_civoid ContactsBuild::GetContactDataByObject(napi_env env, napi_value object, Contacts &contact) 31115cd2caSopenharmony_ci{ 32115cd2caSopenharmony_ci contact.id = GetIntValueByKey(env, object, "id"); 33115cd2caSopenharmony_ci contact.key = GetStringValueByKey(env, object, "key"); 34115cd2caSopenharmony_ci contact.emails = GetEmails(env, object); 35115cd2caSopenharmony_ci contact.events = GetEvent(env, object); 36115cd2caSopenharmony_ci contact.groups = GetGroup(env, object); 37115cd2caSopenharmony_ci contact.imAddresses = GetImAddress(env, object); 38115cd2caSopenharmony_ci contact.phoneNumbers = GetPhoneNumbers(env, object); 39115cd2caSopenharmony_ci contact.portrait = GetUri(env, object); 40115cd2caSopenharmony_ci contact.relations = GetRelation(env, object); 41115cd2caSopenharmony_ci contact.sipAddresses = GetSipAddress(env, object); 42115cd2caSopenharmony_ci contact.websites = GetWebsite(env, object); 43115cd2caSopenharmony_ci contact.name = GetName(env, object); 44115cd2caSopenharmony_ci contact.nickName = GetNickName(env, object); 45115cd2caSopenharmony_ci contact.note = GetNote(env, object); 46115cd2caSopenharmony_ci contact.organization = GetOrganization(env, object); 47115cd2caSopenharmony_ci contact.postalAddresses = GetPostalAddress(env, object); 48115cd2caSopenharmony_ci} 49115cd2caSopenharmony_ci 50115cd2caSopenharmony_civoid ContactsBuild::GetContactData(napi_env env, napi_value object, 51115cd2caSopenharmony_ci std::vector<DataShare::DataShareValuesBucket> &valueContact, 52115cd2caSopenharmony_ci std::vector<DataShare::DataShareValuesBucket> &valueContactData) 53115cd2caSopenharmony_ci{ 54115cd2caSopenharmony_ci Contacts contact; 55115cd2caSopenharmony_ci GetContactDataByObject(env, object, contact); 56115cd2caSopenharmony_ci BuildValueContact(contact, valueContact); 57115cd2caSopenharmony_ci BuildValueContactData(contact, valueContactData); 58115cd2caSopenharmony_ci} 59115cd2caSopenharmony_ci 60115cd2caSopenharmony_civoid ContactsBuild::BuildValueContact(Contacts &contact, std::vector<DataShare::DataShareValuesBucket> &valueContact) 61115cd2caSopenharmony_ci{ 62115cd2caSopenharmony_ci DataShare::DataShareValuesBucket valuesBucketContact; 63115cd2caSopenharmony_ci if (!contact.name.fullName.empty() || contact.name.fullName != "") { 64115cd2caSopenharmony_ci valuesBucketContact.Put("display_name", contact.name.fullName); 65115cd2caSopenharmony_ci } 66115cd2caSopenharmony_ci if (!contact.organization.name.empty() || contact.organization.name != "") { 67115cd2caSopenharmony_ci valuesBucketContact.Put("company", contact.organization.name); 68115cd2caSopenharmony_ci } 69115cd2caSopenharmony_ci if (!contact.organization.title.empty() || contact.organization.title != "") { 70115cd2caSopenharmony_ci valuesBucketContact.Put("position", contact.organization.title); 71115cd2caSopenharmony_ci } 72115cd2caSopenharmony_ci valueContact.push_back(valuesBucketContact); 73115cd2caSopenharmony_ci} 74115cd2caSopenharmony_ci 75115cd2caSopenharmony_civoid ContactsBuild::BuildValueContactData(Contacts &contact, 76115cd2caSopenharmony_ci std::vector<DataShare::DataShareValuesBucket> &valueContactData) 77115cd2caSopenharmony_ci{ 78115cd2caSopenharmony_ci GetValuesBucketEmail(contact, valueContactData); 79115cd2caSopenharmony_ci GetValuesBucketEvent(contact, valueContactData); 80115cd2caSopenharmony_ci GetValuesBucketGroup(contact, valueContactData); 81115cd2caSopenharmony_ci GetValuesBucketImAddress(contact, valueContactData); 82115cd2caSopenharmony_ci GetValuesBucketPortrait(contact, valueContactData); 83115cd2caSopenharmony_ci GetValuesBucketPhoneNumber(contact, valueContactData); 84115cd2caSopenharmony_ci GetValuesBucketPostalAddress(contact, valueContactData); 85115cd2caSopenharmony_ci GetValuesBucketRelation(contact, valueContactData); 86115cd2caSopenharmony_ci GetValuesBucketSipAddress(contact, valueContactData); 87115cd2caSopenharmony_ci GetValuesBucketWebsite(contact, valueContactData); 88115cd2caSopenharmony_ci GetValuesBucketName(contact, valueContactData); 89115cd2caSopenharmony_ci GetValuesBucketNickName(contact, valueContactData); 90115cd2caSopenharmony_ci GetValuesBucketNote(contact, valueContactData); 91115cd2caSopenharmony_ci GetValuesBucketOrganization(contact, valueContactData); 92115cd2caSopenharmony_ci} 93115cd2caSopenharmony_ci 94115cd2caSopenharmony_civoid ContactsBuild::BuildValueContactDataByType( 95115cd2caSopenharmony_ci Contacts &contact, int typeId, std::vector<DataShare::DataShareValuesBucket> &valueContactData) 96115cd2caSopenharmony_ci{ 97115cd2caSopenharmony_ci switch (typeId) { 98115cd2caSopenharmony_ci case EMAIL: 99115cd2caSopenharmony_ci GetValuesBucketEmail(contact, valueContactData); 100115cd2caSopenharmony_ci break; 101115cd2caSopenharmony_ci case IM: 102115cd2caSopenharmony_ci GetValuesBucketImAddress(contact, valueContactData); 103115cd2caSopenharmony_ci break; 104115cd2caSopenharmony_ci case NICKNAME: 105115cd2caSopenharmony_ci GetValuesBucketNickName(contact, valueContactData); 106115cd2caSopenharmony_ci break; 107115cd2caSopenharmony_ci case ORGANIZATION: 108115cd2caSopenharmony_ci GetValuesBucketOrganization(contact, valueContactData); 109115cd2caSopenharmony_ci break; 110115cd2caSopenharmony_ci case PHONE: 111115cd2caSopenharmony_ci GetValuesBucketPhoneNumber(contact, valueContactData); 112115cd2caSopenharmony_ci break; 113115cd2caSopenharmony_ci case NAME: 114115cd2caSopenharmony_ci GetValuesBucketName(contact, valueContactData); 115115cd2caSopenharmony_ci break; 116115cd2caSopenharmony_ci case POSTAL_ADDRESS: 117115cd2caSopenharmony_ci GetValuesBucketPostalAddress(contact, valueContactData); 118115cd2caSopenharmony_ci break; 119115cd2caSopenharmony_ci default: 120115cd2caSopenharmony_ci TypeSwitchSplit(typeId, contact, valueContactData); 121115cd2caSopenharmony_ci break; 122115cd2caSopenharmony_ci } 123115cd2caSopenharmony_ci} 124115cd2caSopenharmony_ci 125115cd2caSopenharmony_civoid ContactsBuild::TypeSwitchSplit( 126115cd2caSopenharmony_ci int typeId, Contacts &contact, std::vector<DataShare::DataShareValuesBucket> &valueContactData) 127115cd2caSopenharmony_ci{ 128115cd2caSopenharmony_ci switch (typeId) { 129115cd2caSopenharmony_ci case PHOTO: 130115cd2caSopenharmony_ci GetValuesBucketPortrait(contact, valueContactData); 131115cd2caSopenharmony_ci break; 132115cd2caSopenharmony_ci case GROUP_MEMBERSHIP: 133115cd2caSopenharmony_ci GetValuesBucketGroup(contact, valueContactData); 134115cd2caSopenharmony_ci break; 135115cd2caSopenharmony_ci case NOTE: 136115cd2caSopenharmony_ci GetValuesBucketNote(contact, valueContactData); 137115cd2caSopenharmony_ci break; 138115cd2caSopenharmony_ci case CONTACT_EVENT: 139115cd2caSopenharmony_ci GetValuesBucketEvent(contact, valueContactData); 140115cd2caSopenharmony_ci break; 141115cd2caSopenharmony_ci case WEBSITE: 142115cd2caSopenharmony_ci GetValuesBucketWebsite(contact, valueContactData); 143115cd2caSopenharmony_ci break; 144115cd2caSopenharmony_ci case RELATION: 145115cd2caSopenharmony_ci GetValuesBucketRelation(contact, valueContactData); 146115cd2caSopenharmony_ci break; 147115cd2caSopenharmony_ci case CONTACT_MISC: 148115cd2caSopenharmony_ci HILOG_INFO("TypeSwitchSplit is CONTACT_MISC "); 149115cd2caSopenharmony_ci break; 150115cd2caSopenharmony_ci case HICALL_DEVICE: 151115cd2caSopenharmony_ci HILOG_INFO("TypeSwitchSplit is HICALL_DEVICE "); 152115cd2caSopenharmony_ci break; 153115cd2caSopenharmony_ci case CAMCARD: 154115cd2caSopenharmony_ci case SIP_ADDRESS: 155115cd2caSopenharmony_ci GetValuesBucketSipAddress(contact, valueContactData); 156115cd2caSopenharmony_ci break; 157115cd2caSopenharmony_ci default: 158115cd2caSopenharmony_ci HILOG_ERROR("TypeSwitchSplit type is error "); 159115cd2caSopenharmony_ci break; 160115cd2caSopenharmony_ci } 161115cd2caSopenharmony_ci} 162115cd2caSopenharmony_ci 163115cd2caSopenharmony_ci/** 164115cd2caSopenharmony_ci * @brief Establish ValuesBucket condition in EMAIL case 165115cd2caSopenharmony_ci * 166115cd2caSopenharmony_ci * @param contact Conditions for establish ValuesBucket operation 167115cd2caSopenharmony_ci * @param valueContactData Conditions for establish ValuesBucket operation 168115cd2caSopenharmony_ci */ 169115cd2caSopenharmony_civoid ContactsBuild::GetValuesBucketEmail(Contacts &contact, 170115cd2caSopenharmony_ci std::vector<DataShare::DataShareValuesBucket> &valueContactData) 171115cd2caSopenharmony_ci{ 172115cd2caSopenharmony_ci unsigned int emailsSize = contact.emails.size(); 173115cd2caSopenharmony_ci for (unsigned int i = 0; i < emailsSize; i++) { 174115cd2caSopenharmony_ci DataShare::DataShareValuesBucket valuesBucketEmail; 175115cd2caSopenharmony_ci valuesBucketEmail.Put("detail_info", contact.emails[i].email); 176115cd2caSopenharmony_ci if (contact.emails[i].labelId != 0) { 177115cd2caSopenharmony_ci valuesBucketEmail.Put("extend7", std::to_string(contact.emails[i].labelId)); 178115cd2caSopenharmony_ci } 179115cd2caSopenharmony_ci if (!contact.emails[i].labelName.empty()) { 180115cd2caSopenharmony_ci valuesBucketEmail.Put("custom_data", contact.emails[i].labelName); 181115cd2caSopenharmony_ci valuesBucketEmail.Put("extend7", std::to_string(Email::CUSTOM_LABEL)); 182115cd2caSopenharmony_ci } 183115cd2caSopenharmony_ci if (!contact.emails[i].displayName.empty()) { 184115cd2caSopenharmony_ci valuesBucketEmail.Put("alias_detail_info", contact.emails[i].displayName); 185115cd2caSopenharmony_ci } 186115cd2caSopenharmony_ci valuesBucketEmail.Put("content_type", "email"); 187115cd2caSopenharmony_ci valueContactData.push_back(valuesBucketEmail); 188115cd2caSopenharmony_ci } 189115cd2caSopenharmony_ci} 190115cd2caSopenharmony_ci 191115cd2caSopenharmony_ci/** 192115cd2caSopenharmony_ci * @brief Establish ValuesBucket condition in CONTACT_EVENT case 193115cd2caSopenharmony_ci * 194115cd2caSopenharmony_ci * @param contact Conditions for establish ValuesBucket operation 195115cd2caSopenharmony_ci * @param valueContactData Conditions for establish ValuesBucket operation 196115cd2caSopenharmony_ci */ 197115cd2caSopenharmony_civoid ContactsBuild::GetValuesBucketEvent(Contacts &contact, 198115cd2caSopenharmony_ci std::vector<DataShare::DataShareValuesBucket> &valueContactData) 199115cd2caSopenharmony_ci{ 200115cd2caSopenharmony_ci unsigned int eventsSize = contact.events.size(); 201115cd2caSopenharmony_ci for (unsigned int i = 0; i < eventsSize; i++) { 202115cd2caSopenharmony_ci DataShare::DataShareValuesBucket valuesBucketEvent; 203115cd2caSopenharmony_ci valuesBucketEvent.Put("detail_info", contact.events[i].eventDate); 204115cd2caSopenharmony_ci if (contact.events[i].labelId != 0) { 205115cd2caSopenharmony_ci valuesBucketEvent.Put("extend7", std::to_string(contact.events[i].labelId)); 206115cd2caSopenharmony_ci } 207115cd2caSopenharmony_ci if (!contact.events[i].labelName.empty()) { 208115cd2caSopenharmony_ci valuesBucketEvent.Put("custom_data", contact.events[i].labelName); 209115cd2caSopenharmony_ci valuesBucketEvent.Put("extend7", std::to_string(Event::CUSTOM_LABEL)); 210115cd2caSopenharmony_ci } 211115cd2caSopenharmony_ci valuesBucketEvent.Put("content_type", "contact_event"); 212115cd2caSopenharmony_ci valueContactData.push_back(valuesBucketEvent); 213115cd2caSopenharmony_ci } 214115cd2caSopenharmony_ci} 215115cd2caSopenharmony_ci 216115cd2caSopenharmony_ci/** 217115cd2caSopenharmony_ci * @brief Establish ValuesBucket condition in GROUP_MEMBERSHIP case 218115cd2caSopenharmony_ci * 219115cd2caSopenharmony_ci * @param contact Conditions for establish ValuesBucket operation 220115cd2caSopenharmony_ci * @param valueContactData Conditions for establish ValuesBucket operation 221115cd2caSopenharmony_ci */ 222115cd2caSopenharmony_civoid ContactsBuild::GetValuesBucketGroup(Contacts &contact, 223115cd2caSopenharmony_ci std::vector<DataShare::DataShareValuesBucket> &valueContactData) 224115cd2caSopenharmony_ci{ 225115cd2caSopenharmony_ci unsigned int groupsSize = contact.groups.size(); 226115cd2caSopenharmony_ci for (unsigned int i = 0; i < groupsSize; i++) { 227115cd2caSopenharmony_ci DataShare::DataShareValuesBucket valuesBucketGroup; 228115cd2caSopenharmony_ci if (contact.groups[i].groupId != ERROR) { 229115cd2caSopenharmony_ci valuesBucketGroup.Put("detail_info", std::to_string(contact.groups[i].groupId)); 230115cd2caSopenharmony_ci } 231115cd2caSopenharmony_ci valuesBucketGroup.Put("alias_detail_info", contact.groups[i].title); 232115cd2caSopenharmony_ci valuesBucketGroup.Put("content_type", "group_membership"); 233115cd2caSopenharmony_ci valueContactData.push_back(valuesBucketGroup); 234115cd2caSopenharmony_ci } 235115cd2caSopenharmony_ci} 236115cd2caSopenharmony_ci 237115cd2caSopenharmony_ci/** 238115cd2caSopenharmony_ci * @brief Establish ValuesBucket condition in Im case 239115cd2caSopenharmony_ci * 240115cd2caSopenharmony_ci * @param contact Conditions for establish ValuesBucket operation 241115cd2caSopenharmony_ci * @param valueContactData Conditions for establish ValuesBucket operation 242115cd2caSopenharmony_ci */ 243115cd2caSopenharmony_civoid ContactsBuild::GetValuesBucketImAddress(Contacts &contact, 244115cd2caSopenharmony_ci std::vector<DataShare::DataShareValuesBucket> &valueContactData) 245115cd2caSopenharmony_ci{ 246115cd2caSopenharmony_ci unsigned int imAddressSize = contact.imAddresses.size(); 247115cd2caSopenharmony_ci for (unsigned int i = 0; i < imAddressSize; i++) { 248115cd2caSopenharmony_ci DataShare::DataShareValuesBucket valuesBucketImAddress; 249115cd2caSopenharmony_ci valuesBucketImAddress.Put("detail_info", contact.imAddresses[i].imAddress); 250115cd2caSopenharmony_ci if (contact.imAddresses[i].labelId != 0) { 251115cd2caSopenharmony_ci valuesBucketImAddress.Put("extend7", std::to_string(contact.imAddresses[i].labelId)); 252115cd2caSopenharmony_ci } 253115cd2caSopenharmony_ci if (!contact.imAddresses[i].labelName.empty()) { 254115cd2caSopenharmony_ci valuesBucketImAddress.Put("custom_data", contact.imAddresses[i].labelName); 255115cd2caSopenharmony_ci valuesBucketImAddress.Put("extend7", std::to_string(ImAddress::CUSTOM_LABEL)); 256115cd2caSopenharmony_ci } 257115cd2caSopenharmony_ci valuesBucketImAddress.Put("content_type", "im"); 258115cd2caSopenharmony_ci valueContactData.push_back(valuesBucketImAddress); 259115cd2caSopenharmony_ci } 260115cd2caSopenharmony_ci} 261115cd2caSopenharmony_ci 262115cd2caSopenharmony_ci/** 263115cd2caSopenharmony_ci * @brief Establish ValuesBucket condition in PHOTO case 264115cd2caSopenharmony_ci * 265115cd2caSopenharmony_ci * @param contact Conditions for establish ValuesBucket operation 266115cd2caSopenharmony_ci * @param valueContactData Conditions for establish ValuesBucket operation 267115cd2caSopenharmony_ci */ 268115cd2caSopenharmony_civoid ContactsBuild::GetValuesBucketPortrait(Contacts &contact, 269115cd2caSopenharmony_ci std::vector<DataShare::DataShareValuesBucket> &valueContactData) 270115cd2caSopenharmony_ci{ 271115cd2caSopenharmony_ci if (!contact.portrait.uri.empty() || contact.portrait.uri != "") { 272115cd2caSopenharmony_ci DataShare::DataShareValuesBucket valuesBucketPortrait; 273115cd2caSopenharmony_ci valuesBucketPortrait.Put("detail_info", contact.portrait.uri); 274115cd2caSopenharmony_ci valuesBucketPortrait.Put("content_type", "photo"); 275115cd2caSopenharmony_ci valueContactData.push_back(valuesBucketPortrait); 276115cd2caSopenharmony_ci } 277115cd2caSopenharmony_ci} 278115cd2caSopenharmony_ci 279115cd2caSopenharmony_ci/** 280115cd2caSopenharmony_ci * @brief Establish ValuesBucket condition in PHONE case 281115cd2caSopenharmony_ci * 282115cd2caSopenharmony_ci * @param contact Conditions for establish ValuesBucket operation 283115cd2caSopenharmony_ci * @param valueContactData Conditions for establish ValuesBucket operation 284115cd2caSopenharmony_ci */ 285115cd2caSopenharmony_civoid ContactsBuild::GetValuesBucketPhoneNumber( 286115cd2caSopenharmony_ci Contacts &contact, std::vector<DataShare::DataShareValuesBucket> &valueContactData) 287115cd2caSopenharmony_ci{ 288115cd2caSopenharmony_ci unsigned int phoneNumbersSize = contact.phoneNumbers.size(); 289115cd2caSopenharmony_ci for (unsigned int i = 0; i < phoneNumbersSize; i++) { 290115cd2caSopenharmony_ci DataShare::DataShareValuesBucket valuesBucketPhoneNumber; 291115cd2caSopenharmony_ci valuesBucketPhoneNumber.Put("detail_info", contact.phoneNumbers[i].phoneNumber); 292115cd2caSopenharmony_ci if (contact.phoneNumbers[i].labelId != 0) { 293115cd2caSopenharmony_ci valuesBucketPhoneNumber.Put("extend7", std::to_string(contact.phoneNumbers[i].labelId)); 294115cd2caSopenharmony_ci } 295115cd2caSopenharmony_ci if (!contact.phoneNumbers[i].labelName.empty()) { 296115cd2caSopenharmony_ci valuesBucketPhoneNumber.Put("custom_data", contact.phoneNumbers[i].labelName); 297115cd2caSopenharmony_ci valuesBucketPhoneNumber.Put("extend7", std::to_string(PhoneNumber::CUSTOM_LABEL)); 298115cd2caSopenharmony_ci } 299115cd2caSopenharmony_ci valuesBucketPhoneNumber.Put("content_type", "phone"); 300115cd2caSopenharmony_ci valueContactData.push_back(valuesBucketPhoneNumber); 301115cd2caSopenharmony_ci } 302115cd2caSopenharmony_ci} 303115cd2caSopenharmony_ci 304115cd2caSopenharmony_ci/** 305115cd2caSopenharmony_ci * @brief Establish ValuesBucket condition in POSTAL_ADDRESS case 306115cd2caSopenharmony_ci * 307115cd2caSopenharmony_ci * @param contact Conditions for establish ValuesBucket operation 308115cd2caSopenharmony_ci * @param valueContactData Conditions for establish ValuesBucket operation 309115cd2caSopenharmony_ci */ 310115cd2caSopenharmony_civoid ContactsBuild::GetValuesBucketPostalAddress( 311115cd2caSopenharmony_ci Contacts &contact, std::vector<DataShare::DataShareValuesBucket> &valueContactData) 312115cd2caSopenharmony_ci{ 313115cd2caSopenharmony_ci unsigned int postalAddressesSize = contact.postalAddresses.size(); 314115cd2caSopenharmony_ci for (unsigned int i = 0; i < postalAddressesSize; i++) { 315115cd2caSopenharmony_ci DataShare::DataShareValuesBucket valuesBucketPostalAddress; 316115cd2caSopenharmony_ci valuesBucketPostalAddress.Put("detail_info", contact.postalAddresses[i].postalAddress); 317115cd2caSopenharmony_ci if (contact.postalAddresses[i].labelId != 0) { 318115cd2caSopenharmony_ci valuesBucketPostalAddress.Put("extend7", std::to_string(contact.postalAddresses[i].labelId)); 319115cd2caSopenharmony_ci } 320115cd2caSopenharmony_ci if (!contact.postalAddresses[i].labelName.empty()) { 321115cd2caSopenharmony_ci valuesBucketPostalAddress.Put("custom_data", contact.postalAddresses[i].labelName); 322115cd2caSopenharmony_ci valuesBucketPostalAddress.Put("extend7", std::to_string(PostalAddress::CUSTOM_LABEL)); 323115cd2caSopenharmony_ci } 324115cd2caSopenharmony_ci if (!contact.postalAddresses[i].neighborhood.empty()) { 325115cd2caSopenharmony_ci valuesBucketPostalAddress.Put("neighborhood", contact.postalAddresses[i].neighborhood); 326115cd2caSopenharmony_ci } 327115cd2caSopenharmony_ci if (!contact.postalAddresses[i].pobox.empty()) { 328115cd2caSopenharmony_ci valuesBucketPostalAddress.Put("pobox", contact.postalAddresses[i].pobox); 329115cd2caSopenharmony_ci } 330115cd2caSopenharmony_ci if (!contact.postalAddresses[i].postcode.empty()) { 331115cd2caSopenharmony_ci valuesBucketPostalAddress.Put("postcode", contact.postalAddresses[i].postcode); 332115cd2caSopenharmony_ci } 333115cd2caSopenharmony_ci if (!contact.postalAddresses[i].region.empty()) { 334115cd2caSopenharmony_ci valuesBucketPostalAddress.Put("region", contact.postalAddresses[i].region); 335115cd2caSopenharmony_ci } 336115cd2caSopenharmony_ci if (!contact.postalAddresses[i].street.empty()) { 337115cd2caSopenharmony_ci valuesBucketPostalAddress.Put("street", contact.postalAddresses[i].street); 338115cd2caSopenharmony_ci } 339115cd2caSopenharmony_ci if (!contact.postalAddresses[i].city.empty()) { 340115cd2caSopenharmony_ci valuesBucketPostalAddress.Put("city", contact.postalAddresses[i].city); 341115cd2caSopenharmony_ci } 342115cd2caSopenharmony_ci if (!contact.postalAddresses[i].country.empty()) { 343115cd2caSopenharmony_ci valuesBucketPostalAddress.Put("country", contact.postalAddresses[i].country); 344115cd2caSopenharmony_ci } 345115cd2caSopenharmony_ci valuesBucketPostalAddress.Put("content_type", "postal_address"); 346115cd2caSopenharmony_ci valueContactData.push_back(valuesBucketPostalAddress); 347115cd2caSopenharmony_ci } 348115cd2caSopenharmony_ci} 349115cd2caSopenharmony_ci 350115cd2caSopenharmony_ci/** 351115cd2caSopenharmony_ci * @brief Establish ValuesBucket condition in RELATION case 352115cd2caSopenharmony_ci * 353115cd2caSopenharmony_ci * @param contact Conditions for establish ValuesBucket operation 354115cd2caSopenharmony_ci * @param valueContactData Conditions for establish ValuesBucket operation 355115cd2caSopenharmony_ci */ 356115cd2caSopenharmony_civoid ContactsBuild::GetValuesBucketRelation(Contacts &contact, 357115cd2caSopenharmony_ci std::vector<DataShare::DataShareValuesBucket> &valueContactData) 358115cd2caSopenharmony_ci{ 359115cd2caSopenharmony_ci unsigned int relationsSize = contact.relations.size(); 360115cd2caSopenharmony_ci for (unsigned int i = 0; i < relationsSize; i++) { 361115cd2caSopenharmony_ci DataShare::DataShareValuesBucket valuesBucketRelation; 362115cd2caSopenharmony_ci valuesBucketRelation.Put("detail_info", contact.relations[i].relationName); 363115cd2caSopenharmony_ci if (contact.relations[i].labelId != 0) { 364115cd2caSopenharmony_ci valuesBucketRelation.Put("extend7", std::to_string(contact.relations[i].labelId)); 365115cd2caSopenharmony_ci } 366115cd2caSopenharmony_ci if (!contact.relations[i].labelName.empty()) { 367115cd2caSopenharmony_ci valuesBucketRelation.Put("custom_data", contact.relations[i].labelName); 368115cd2caSopenharmony_ci valuesBucketRelation.Put("extend7", std::to_string(Relation::CUSTOM_LABEL)); 369115cd2caSopenharmony_ci } 370115cd2caSopenharmony_ci valuesBucketRelation.Put("content_type", "relation"); 371115cd2caSopenharmony_ci valueContactData.push_back(valuesBucketRelation); 372115cd2caSopenharmony_ci } 373115cd2caSopenharmony_ci} 374115cd2caSopenharmony_ci 375115cd2caSopenharmony_ci/** 376115cd2caSopenharmony_ci * @brief Establish ValuesBucket condition in SIP_ADDRESS case 377115cd2caSopenharmony_ci * 378115cd2caSopenharmony_ci * @param contact Conditions for establish ValuesBucket operation 379115cd2caSopenharmony_ci * @param valueContactData Conditions for establish ValuesBucket operation 380115cd2caSopenharmony_ci */ 381115cd2caSopenharmony_civoid ContactsBuild::GetValuesBucketSipAddress(Contacts &contact, 382115cd2caSopenharmony_ci std::vector<DataShare::DataShareValuesBucket> &valueContactData) 383115cd2caSopenharmony_ci{ 384115cd2caSopenharmony_ci unsigned int sipAddressesSize = contact.sipAddresses.size(); 385115cd2caSopenharmony_ci for (unsigned int i = 0; i < sipAddressesSize; i++) { 386115cd2caSopenharmony_ci DataShare::DataShareValuesBucket valuesBucketSipAddress; 387115cd2caSopenharmony_ci valuesBucketSipAddress.Put("detail_info", contact.sipAddresses[i].sipAddress); 388115cd2caSopenharmony_ci if (contact.sipAddresses[i].labelId != 0) { 389115cd2caSopenharmony_ci valuesBucketSipAddress.Put("extend7", std::to_string(contact.sipAddresses[i].labelId)); 390115cd2caSopenharmony_ci } 391115cd2caSopenharmony_ci if (!contact.sipAddresses[i].labelName.empty()) { 392115cd2caSopenharmony_ci valuesBucketSipAddress.Put("custom_data", contact.sipAddresses[i].labelName); 393115cd2caSopenharmony_ci valuesBucketSipAddress.Put("extend7", std::to_string(SipAddress::CUSTOM_LABEL)); 394115cd2caSopenharmony_ci } 395115cd2caSopenharmony_ci valuesBucketSipAddress.Put("content_type", "sip_address"); 396115cd2caSopenharmony_ci valueContactData.push_back(valuesBucketSipAddress); 397115cd2caSopenharmony_ci } 398115cd2caSopenharmony_ci} 399115cd2caSopenharmony_ci 400115cd2caSopenharmony_ci/** 401115cd2caSopenharmony_ci * @brief Establish ValuesBucket condition in WEBSITE case 402115cd2caSopenharmony_ci * 403115cd2caSopenharmony_ci * @param contact Conditions for establish ValuesBucket operation 404115cd2caSopenharmony_ci * @param valueContactData Conditions for establish ValuesBucket operation 405115cd2caSopenharmony_ci */ 406115cd2caSopenharmony_civoid ContactsBuild::GetValuesBucketWebsite(Contacts &contact, 407115cd2caSopenharmony_ci std::vector<DataShare::DataShareValuesBucket> &valueContactData) 408115cd2caSopenharmony_ci{ 409115cd2caSopenharmony_ci unsigned int websitesSize = contact.websites.size(); 410115cd2caSopenharmony_ci for (unsigned int i = 0; i < websitesSize; i++) { 411115cd2caSopenharmony_ci DataShare::DataShareValuesBucket valuesBucketWebsite; 412115cd2caSopenharmony_ci valuesBucketWebsite.Put("detail_info", contact.websites[i].website); 413115cd2caSopenharmony_ci valuesBucketWebsite.Put("content_type", "website"); 414115cd2caSopenharmony_ci valueContactData.push_back(valuesBucketWebsite); 415115cd2caSopenharmony_ci } 416115cd2caSopenharmony_ci} 417115cd2caSopenharmony_ci 418115cd2caSopenharmony_ci/** 419115cd2caSopenharmony_ci * @brief Establish ValuesBucket condition in NAME case 420115cd2caSopenharmony_ci * 421115cd2caSopenharmony_ci * @param contact Conditions for establish ValuesBucket operation 422115cd2caSopenharmony_ci * @param valueContactData Conditions for establish ValuesBucket operation 423115cd2caSopenharmony_ci */ 424115cd2caSopenharmony_civoid ContactsBuild::GetValuesBucketName(Contacts &contact, 425115cd2caSopenharmony_ci std::vector<DataShare::DataShareValuesBucket> &valueContactData) 426115cd2caSopenharmony_ci{ 427115cd2caSopenharmony_ci if (!contact.name.fullName.empty() || contact.name.fullName != "") { 428115cd2caSopenharmony_ci DataShare::DataShareValuesBucket valuesBucketName; 429115cd2caSopenharmony_ci valuesBucketName.Put("detail_info", contact.name.fullName); 430115cd2caSopenharmony_ci if (!contact.name.namePrefix.empty()) { 431115cd2caSopenharmony_ci valuesBucketName.Put("alpha_name", contact.name.namePrefix); 432115cd2caSopenharmony_ci } 433115cd2caSopenharmony_ci if (!contact.name.middleName.empty()) { 434115cd2caSopenharmony_ci valuesBucketName.Put("other_lan_last_name", contact.name.middleName); 435115cd2caSopenharmony_ci } 436115cd2caSopenharmony_ci if (!contact.name.nameSuffix.empty()) { 437115cd2caSopenharmony_ci valuesBucketName.Put("other_lan_first_name", contact.name.nameSuffix); 438115cd2caSopenharmony_ci } 439115cd2caSopenharmony_ci if (!contact.name.familyName.empty()) { 440115cd2caSopenharmony_ci valuesBucketName.Put("family_name", contact.name.familyName); 441115cd2caSopenharmony_ci } 442115cd2caSopenharmony_ci if (!contact.name.middleNamePhonetic.empty()) { 443115cd2caSopenharmony_ci valuesBucketName.Put("middle_name_phonetic", contact.name.middleNamePhonetic); 444115cd2caSopenharmony_ci } 445115cd2caSopenharmony_ci if (!contact.name.givenName.empty()) { 446115cd2caSopenharmony_ci valuesBucketName.Put("given_name", contact.name.givenName); 447115cd2caSopenharmony_ci } 448115cd2caSopenharmony_ci if (!contact.name.givenNamePhonetic.empty()) { 449115cd2caSopenharmony_ci valuesBucketName.Put("given_name_phonetic", contact.name.givenNamePhonetic); 450115cd2caSopenharmony_ci } 451115cd2caSopenharmony_ci if (!contact.name.familyNamePhonetic.empty()) { 452115cd2caSopenharmony_ci valuesBucketName.Put("phonetic_name", contact.name.familyNamePhonetic); 453115cd2caSopenharmony_ci } 454115cd2caSopenharmony_ci valuesBucketName.Put("content_type", "name"); 455115cd2caSopenharmony_ci valueContactData.push_back(valuesBucketName); 456115cd2caSopenharmony_ci } 457115cd2caSopenharmony_ci} 458115cd2caSopenharmony_ci 459115cd2caSopenharmony_ci/** 460115cd2caSopenharmony_ci * @brief Establish ValuesBucket condition in NICKNAME case 461115cd2caSopenharmony_ci * 462115cd2caSopenharmony_ci * @param contact Conditions for establish ValuesBucket operation 463115cd2caSopenharmony_ci * @param valueContactData Conditions for establish ValuesBucket operation 464115cd2caSopenharmony_ci */ 465115cd2caSopenharmony_civoid ContactsBuild::GetValuesBucketNickName(Contacts &contact, 466115cd2caSopenharmony_ci std::vector<DataShare::DataShareValuesBucket> &valueContactData) 467115cd2caSopenharmony_ci{ 468115cd2caSopenharmony_ci if (!contact.nickName.nickName.empty() || contact.nickName.nickName != "") { 469115cd2caSopenharmony_ci DataShare::DataShareValuesBucket valuesBucketNickName; 470115cd2caSopenharmony_ci valuesBucketNickName.Put("detail_info", contact.nickName.nickName); 471115cd2caSopenharmony_ci valuesBucketNickName.Put("content_type", "nickname"); 472115cd2caSopenharmony_ci valueContactData.push_back(valuesBucketNickName); 473115cd2caSopenharmony_ci } 474115cd2caSopenharmony_ci} 475115cd2caSopenharmony_ci 476115cd2caSopenharmony_ci/** 477115cd2caSopenharmony_ci * @brief Establish ValuesBucket condition in NOTE case 478115cd2caSopenharmony_ci * 479115cd2caSopenharmony_ci * @param contact Conditions for establish ValuesBucket operation 480115cd2caSopenharmony_ci * @param valueContactData Conditions for establish ValuesBucket operation 481115cd2caSopenharmony_ci */ 482115cd2caSopenharmony_civoid ContactsBuild::GetValuesBucketNote(Contacts &contact, 483115cd2caSopenharmony_ci std::vector<DataShare::DataShareValuesBucket> &valueContactData) 484115cd2caSopenharmony_ci{ 485115cd2caSopenharmony_ci if (!contact.note.noteContent.empty() || contact.note.noteContent != "") { 486115cd2caSopenharmony_ci DataShare::DataShareValuesBucket valuesBucketNote; 487115cd2caSopenharmony_ci valuesBucketNote.Put("detail_info", contact.note.noteContent); 488115cd2caSopenharmony_ci valuesBucketNote.Put("content_type", "note"); 489115cd2caSopenharmony_ci valueContactData.push_back(valuesBucketNote); 490115cd2caSopenharmony_ci } 491115cd2caSopenharmony_ci} 492115cd2caSopenharmony_ci 493115cd2caSopenharmony_ci/** 494115cd2caSopenharmony_ci * @brief Establish ValuesBucket condition in ORGANIZATION case 495115cd2caSopenharmony_ci * 496115cd2caSopenharmony_ci * @param contact Conditions for establish ValuesBucket operation 497115cd2caSopenharmony_ci * @param valueContactData Conditions for establish ValuesBucket operation 498115cd2caSopenharmony_ci */ 499115cd2caSopenharmony_civoid ContactsBuild::GetValuesBucketOrganization( 500115cd2caSopenharmony_ci Contacts &contact, std::vector<DataShare::DataShareValuesBucket> &valueContactData) 501115cd2caSopenharmony_ci{ 502115cd2caSopenharmony_ci if (!contact.organization.name.empty()) { 503115cd2caSopenharmony_ci DataShare::DataShareValuesBucket valuesBucketData; 504115cd2caSopenharmony_ci valuesBucketData.Put("detail_info", contact.organization.name); 505115cd2caSopenharmony_ci if (!contact.organization.title.empty()) { 506115cd2caSopenharmony_ci valuesBucketData.Put("position", contact.organization.title); 507115cd2caSopenharmony_ci } 508115cd2caSopenharmony_ci valuesBucketData.Put("content_type", "organization"); 509115cd2caSopenharmony_ci valueContactData.push_back(valuesBucketData); 510115cd2caSopenharmony_ci } 511115cd2caSopenharmony_ci} 512115cd2caSopenharmony_ci 513115cd2caSopenharmony_cinapi_value ContactsBuild::GetObjectByKey(napi_env env, napi_value object, std::string key) 514115cd2caSopenharmony_ci{ 515115cd2caSopenharmony_ci ResultConvert resultConvert; 516115cd2caSopenharmony_ci return resultConvert.GetNapiValue(env, key.c_str(), object); 517115cd2caSopenharmony_ci} 518115cd2caSopenharmony_ci 519115cd2caSopenharmony_cinapi_value ContactsBuild::GetArrayByKey(napi_env env, napi_value valueObject, std::string key) 520115cd2caSopenharmony_ci{ 521115cd2caSopenharmony_ci ResultConvert resultConvert; 522115cd2caSopenharmony_ci napi_value array = resultConvert.GetNapiValue(env, key.c_str(), valueObject); 523115cd2caSopenharmony_ci bool isArray = false; 524115cd2caSopenharmony_ci napi_is_array(env, array, &isArray); 525115cd2caSopenharmony_ci if (!isArray) { 526115cd2caSopenharmony_ci HILOG_INFO("ContactsBuild GetArrayByKey value is not array"); 527115cd2caSopenharmony_ci return nullptr; 528115cd2caSopenharmony_ci } 529115cd2caSopenharmony_ci return array; 530115cd2caSopenharmony_ci} 531115cd2caSopenharmony_ci 532115cd2caSopenharmony_cistd::string ContactsBuild::GetStringValueByKey(napi_env env, napi_value valueObject, std::string key) 533115cd2caSopenharmony_ci{ 534115cd2caSopenharmony_ci ResultConvert resultConvert; 535115cd2caSopenharmony_ci napi_value value = resultConvert.GetNapiValue(env, key.c_str(), valueObject); 536115cd2caSopenharmony_ci std::string result = NapiGetValueString(env, value); 537115cd2caSopenharmony_ci return result; 538115cd2caSopenharmony_ci} 539115cd2caSopenharmony_ci 540115cd2caSopenharmony_ciint ContactsBuild::GetIntValueByKey(napi_env env, napi_value valueObject, std::string key) 541115cd2caSopenharmony_ci{ 542115cd2caSopenharmony_ci ResultConvert resultConvert; 543115cd2caSopenharmony_ci napi_value value = resultConvert.GetNapiValue(env, key.c_str(), valueObject); 544115cd2caSopenharmony_ci if (value == nullptr) { 545115cd2caSopenharmony_ci return ERROR; 546115cd2caSopenharmony_ci } 547115cd2caSopenharmony_ci int64_t result; 548115cd2caSopenharmony_ci napi_get_value_int64(env, value, &result); 549115cd2caSopenharmony_ci int code = result; 550115cd2caSopenharmony_ci return code; 551115cd2caSopenharmony_ci} 552115cd2caSopenharmony_ci 553115cd2caSopenharmony_ciName ContactsBuild::GetName(napi_env env, napi_value object) 554115cd2caSopenharmony_ci{ 555115cd2caSopenharmony_ci HILOG_INFO("ContactsBuild into GetName"); 556115cd2caSopenharmony_ci Name name; 557115cd2caSopenharmony_ci if (object == nullptr) { 558115cd2caSopenharmony_ci HILOG_ERROR("ContactsBuild GetName nameKey is null "); 559115cd2caSopenharmony_ci return name; 560115cd2caSopenharmony_ci } 561115cd2caSopenharmony_ci napi_value nameObj = GetObjectByKey(env, object, "name"); 562115cd2caSopenharmony_ci napi_valuetype valueType; 563115cd2caSopenharmony_ci napi_typeof(env, nameObj, &valueType); 564115cd2caSopenharmony_ci if (nameObj == nullptr || valueType != napi_object) { 565115cd2caSopenharmony_ci HILOG_ERROR("ContactsBuild GetName nameObj is null or object type is not object"); 566115cd2caSopenharmony_ci return name; 567115cd2caSopenharmony_ci } 568115cd2caSopenharmony_ci name.familyName = GetStringValueByKey(env, nameObj, "familyName"); 569115cd2caSopenharmony_ci name.middleNamePhonetic = GetStringValueByKey(env, nameObj, "middleNamePhonetic"); 570115cd2caSopenharmony_ci name.givenName = GetStringValueByKey(env, nameObj, "givenName"); 571115cd2caSopenharmony_ci name.givenNamePhonetic = GetStringValueByKey(env, nameObj, "givenNamePhonetic"); 572115cd2caSopenharmony_ci name.familyNamePhonetic = GetStringValueByKey(env, nameObj, "familyNamePhonetic"); 573115cd2caSopenharmony_ci name.fullName = GetStringValueByKey(env, nameObj, "fullName"); 574115cd2caSopenharmony_ci name.middleName = GetStringValueByKey(env, nameObj, "middleName"); 575115cd2caSopenharmony_ci name.namePrefix = GetStringValueByKey(env, nameObj, "namePrefix"); 576115cd2caSopenharmony_ci name.nameSuffix = GetStringValueByKey(env, nameObj, "nameSuffix"); 577115cd2caSopenharmony_ci return name; 578115cd2caSopenharmony_ci} 579115cd2caSopenharmony_ci 580115cd2caSopenharmony_ciPortrait ContactsBuild::GetUri(napi_env env, napi_value object) 581115cd2caSopenharmony_ci{ 582115cd2caSopenharmony_ci Portrait portrait; 583115cd2caSopenharmony_ci napi_value portraitObj = GetObjectByKey(env, object, "portrait"); 584115cd2caSopenharmony_ci napi_valuetype valueType; 585115cd2caSopenharmony_ci napi_typeof(env, portraitObj, &valueType); 586115cd2caSopenharmony_ci if (portraitObj == nullptr || valueType != napi_object) { 587115cd2caSopenharmony_ci HILOG_ERROR("ContactsBuild GetUri portraitObj is null or object type is not object"); 588115cd2caSopenharmony_ci return portrait; 589115cd2caSopenharmony_ci } 590115cd2caSopenharmony_ci portrait.uri = GetStringValueByKey(env, portraitObj, "uri"); 591115cd2caSopenharmony_ci return portrait; 592115cd2caSopenharmony_ci} 593115cd2caSopenharmony_ci 594115cd2caSopenharmony_cistd::vector<Email> ContactsBuild::GetEmails(napi_env env, napi_value object) 595115cd2caSopenharmony_ci{ 596115cd2caSopenharmony_ci std::vector<Email> emailVec; 597115cd2caSopenharmony_ci napi_value EmailArray = GetArrayByKey(env, object, "emails"); 598115cd2caSopenharmony_ci if (EmailArray == nullptr) { 599115cd2caSopenharmony_ci HILOG_ERROR("ContactsBuild GetEmail napiValueEmail is null "); 600115cd2caSopenharmony_ci return emailVec; 601115cd2caSopenharmony_ci } 602115cd2caSopenharmony_ci uint32_t size = 0; 603115cd2caSopenharmony_ci napi_get_array_length(env, EmailArray, &size); 604115cd2caSopenharmony_ci for (uint32_t i = 0; i < size; i++) { 605115cd2caSopenharmony_ci Email email; 606115cd2caSopenharmony_ci napi_value object; 607115cd2caSopenharmony_ci napi_get_element(env, EmailArray, i, &object); 608115cd2caSopenharmony_ci email.email = GetStringValueByKey(env, object, "email"); 609115cd2caSopenharmony_ci email.labelName = GetStringValueByKey(env, object, "labelName"); 610115cd2caSopenharmony_ci email.displayName = GetStringValueByKey(env, object, "displayName"); 611115cd2caSopenharmony_ci email.labelId = GetIntValueByKey(env, object, "labelId"); 612115cd2caSopenharmony_ci emailVec.push_back(email); 613115cd2caSopenharmony_ci } 614115cd2caSopenharmony_ci return emailVec; 615115cd2caSopenharmony_ci} 616115cd2caSopenharmony_ci 617115cd2caSopenharmony_cistd::vector<Event> ContactsBuild::GetEvent(napi_env env, napi_value object) 618115cd2caSopenharmony_ci{ 619115cd2caSopenharmony_ci napi_value eventArray = GetArrayByKey(env, object, "events"); 620115cd2caSopenharmony_ci if (eventArray == nullptr) { 621115cd2caSopenharmony_ci HILOG_ERROR("ContactsBuild GetEvent arrKey is null "); 622115cd2caSopenharmony_ci return std::vector<Event>(); 623115cd2caSopenharmony_ci } 624115cd2caSopenharmony_ci uint32_t size = 0; 625115cd2caSopenharmony_ci napi_get_array_length(env, eventArray, &size); 626115cd2caSopenharmony_ci std::vector<Event> resultVec; 627115cd2caSopenharmony_ci for (uint32_t i = 0; i < size; i++) { 628115cd2caSopenharmony_ci napi_value obj; 629115cd2caSopenharmony_ci napi_get_element(env, eventArray, i, &obj); 630115cd2caSopenharmony_ci Event eventObj; 631115cd2caSopenharmony_ci eventObj.eventDate = GetStringValueByKey(env, obj, "eventDate"); 632115cd2caSopenharmony_ci eventObj.labelName = GetStringValueByKey(env, obj, "labelName"); 633115cd2caSopenharmony_ci eventObj.labelId = GetIntValueByKey(env, obj, "labelId"); 634115cd2caSopenharmony_ci resultVec.push_back(eventObj); 635115cd2caSopenharmony_ci } 636115cd2caSopenharmony_ci return resultVec; 637115cd2caSopenharmony_ci} 638115cd2caSopenharmony_ci 639115cd2caSopenharmony_cistd::vector<Group> ContactsBuild::GetGroup(napi_env env, napi_value object) 640115cd2caSopenharmony_ci{ 641115cd2caSopenharmony_ci napi_value groupArray = GetArrayByKey(env, object, "groups"); 642115cd2caSopenharmony_ci std::vector<Group> resultVec; 643115cd2caSopenharmony_ci if (groupArray == nullptr) { 644115cd2caSopenharmony_ci HILOG_ERROR("ContactsBuild GetGroup arrKey is null "); 645115cd2caSopenharmony_ci return resultVec; 646115cd2caSopenharmony_ci } 647115cd2caSopenharmony_ci uint32_t size = 0; 648115cd2caSopenharmony_ci napi_get_array_length(env, groupArray, &size); 649115cd2caSopenharmony_ci for (uint32_t i = 0; i < size; i++) { 650115cd2caSopenharmony_ci napi_value obj; 651115cd2caSopenharmony_ci napi_get_element(env, groupArray, i, &obj); 652115cd2caSopenharmony_ci Group groupObj; 653115cd2caSopenharmony_ci groupObj.groupId = GetIntValueByKey(env, obj, "groupId"); 654115cd2caSopenharmony_ci groupObj.title = GetStringValueByKey(env, obj, "title"); 655115cd2caSopenharmony_ci resultVec.push_back(groupObj); 656115cd2caSopenharmony_ci } 657115cd2caSopenharmony_ci return resultVec; 658115cd2caSopenharmony_ci} 659115cd2caSopenharmony_ci 660115cd2caSopenharmony_cistd::vector<ImAddress> ContactsBuild::GetImAddress(napi_env env, napi_value object) 661115cd2caSopenharmony_ci{ 662115cd2caSopenharmony_ci napi_value imAddressArray = GetArrayByKey(env, object, "imAddresses"); 663115cd2caSopenharmony_ci std::vector<ImAddress> resultVec; 664115cd2caSopenharmony_ci if (imAddressArray == nullptr) { 665115cd2caSopenharmony_ci HILOG_ERROR("ContactsBuild GetImAddress arrKey is null "); 666115cd2caSopenharmony_ci return resultVec; 667115cd2caSopenharmony_ci } 668115cd2caSopenharmony_ci uint32_t size = 0; 669115cd2caSopenharmony_ci napi_get_array_length(env, imAddressArray, &size); 670115cd2caSopenharmony_ci for (uint32_t i = 0; i < size; i++) { 671115cd2caSopenharmony_ci napi_value obj; 672115cd2caSopenharmony_ci napi_get_element(env, imAddressArray, i, &obj); 673115cd2caSopenharmony_ci ImAddress targetObj; 674115cd2caSopenharmony_ci targetObj.imAddress = GetStringValueByKey(env, obj, "imAddress"); 675115cd2caSopenharmony_ci targetObj.labelName = GetStringValueByKey(env, obj, "labelName"); 676115cd2caSopenharmony_ci targetObj.labelId = GetIntValueByKey(env, obj, "labelId"); 677115cd2caSopenharmony_ci resultVec.push_back(targetObj); 678115cd2caSopenharmony_ci } 679115cd2caSopenharmony_ci return resultVec; 680115cd2caSopenharmony_ci} 681115cd2caSopenharmony_ci 682115cd2caSopenharmony_cistd::vector<PhoneNumber> ContactsBuild::GetPhoneNumbers(napi_env env, napi_value object) 683115cd2caSopenharmony_ci{ 684115cd2caSopenharmony_ci std::vector<PhoneNumber> resultVec; 685115cd2caSopenharmony_ci napi_value phoneNumberArray = GetArrayByKey(env, object, "phoneNumbers"); 686115cd2caSopenharmony_ci if (phoneNumberArray == nullptr) { 687115cd2caSopenharmony_ci HILOG_ERROR("ContactsBuild GetPhoneNumber napiValuePhoneNumber is null "); 688115cd2caSopenharmony_ci return resultVec; 689115cd2caSopenharmony_ci } 690115cd2caSopenharmony_ci uint32_t size = 0; 691115cd2caSopenharmony_ci napi_get_array_length(env, phoneNumberArray, &size); 692115cd2caSopenharmony_ci for (uint32_t i = 0; i < size; i++) { 693115cd2caSopenharmony_ci PhoneNumber phones; 694115cd2caSopenharmony_ci napi_value obj; 695115cd2caSopenharmony_ci napi_get_element(env, phoneNumberArray, i, &obj); 696115cd2caSopenharmony_ci phones.phoneNumber = GetStringValueByKey(env, obj, "phoneNumber"); 697115cd2caSopenharmony_ci phones.labelName = GetStringValueByKey(env, obj, "labelName"); 698115cd2caSopenharmony_ci phones.labelId = GetIntValueByKey(env, obj, "labelId"); 699115cd2caSopenharmony_ci resultVec.push_back(phones); 700115cd2caSopenharmony_ci } 701115cd2caSopenharmony_ci return resultVec; 702115cd2caSopenharmony_ci} 703115cd2caSopenharmony_ci 704115cd2caSopenharmony_cistd::vector<PostalAddress> ContactsBuild::GetPostalAddress(napi_env env, napi_value object) 705115cd2caSopenharmony_ci{ 706115cd2caSopenharmony_ci napi_value postalAddressArray = GetArrayByKey(env, object, "postalAddresses"); 707115cd2caSopenharmony_ci std::vector<PostalAddress> resultVec; 708115cd2caSopenharmony_ci if (postalAddressArray == nullptr) { 709115cd2caSopenharmony_ci HILOG_ERROR("GetPostalAddress arrKey is null "); 710115cd2caSopenharmony_ci return resultVec; 711115cd2caSopenharmony_ci } 712115cd2caSopenharmony_ci uint32_t size = 0; 713115cd2caSopenharmony_ci napi_get_array_length(env, postalAddressArray, &size); 714115cd2caSopenharmony_ci for (uint32_t i = 0; i < size; i++) { 715115cd2caSopenharmony_ci napi_value obj; 716115cd2caSopenharmony_ci napi_get_element(env, postalAddressArray, i, &obj); 717115cd2caSopenharmony_ci PostalAddress targetObj; 718115cd2caSopenharmony_ci targetObj.city = GetStringValueByKey(env, obj, "city"); 719115cd2caSopenharmony_ci targetObj.country = GetStringValueByKey(env, obj, "country"); 720115cd2caSopenharmony_ci targetObj.labelName = GetStringValueByKey(env, obj, "labelName"); 721115cd2caSopenharmony_ci targetObj.neighborhood = GetStringValueByKey(env, obj, "neighborhood"); 722115cd2caSopenharmony_ci targetObj.pobox = GetStringValueByKey(env, obj, "pobox"); 723115cd2caSopenharmony_ci targetObj.postalAddress = GetStringValueByKey(env, obj, "postalAddress"); 724115cd2caSopenharmony_ci targetObj.postcode = GetStringValueByKey(env, obj, "postcode"); 725115cd2caSopenharmony_ci targetObj.region = GetStringValueByKey(env, obj, "region"); 726115cd2caSopenharmony_ci targetObj.street = GetStringValueByKey(env, obj, "street"); 727115cd2caSopenharmony_ci targetObj.labelId = GetIntValueByKey(env, obj, "labelId"); 728115cd2caSopenharmony_ci resultVec.push_back(targetObj); 729115cd2caSopenharmony_ci } 730115cd2caSopenharmony_ci return resultVec; 731115cd2caSopenharmony_ci} 732115cd2caSopenharmony_ci 733115cd2caSopenharmony_cistd::vector<Relation> ContactsBuild::GetRelation(napi_env env, napi_value object) 734115cd2caSopenharmony_ci{ 735115cd2caSopenharmony_ci napi_value relationArray = GetArrayByKey(env, object, "relations"); 736115cd2caSopenharmony_ci std::vector<Relation> resultVec; 737115cd2caSopenharmony_ci if (relationArray == nullptr) { 738115cd2caSopenharmony_ci HILOG_ERROR("GetRelation arrKey is null "); 739115cd2caSopenharmony_ci return resultVec; 740115cd2caSopenharmony_ci } 741115cd2caSopenharmony_ci uint32_t size = 0; 742115cd2caSopenharmony_ci napi_get_array_length(env, relationArray, &size); 743115cd2caSopenharmony_ci for (uint32_t i = 0; i < size; i++) { 744115cd2caSopenharmony_ci napi_value obj; 745115cd2caSopenharmony_ci napi_get_element(env, relationArray, i, &obj); 746115cd2caSopenharmony_ci Relation targetObj; 747115cd2caSopenharmony_ci targetObj.relationName = GetStringValueByKey(env, obj, "relationName"); 748115cd2caSopenharmony_ci targetObj.labelName = GetStringValueByKey(env, obj, "labelName"); 749115cd2caSopenharmony_ci targetObj.labelId = GetIntValueByKey(env, obj, "labelId"); 750115cd2caSopenharmony_ci resultVec.push_back(targetObj); 751115cd2caSopenharmony_ci } 752115cd2caSopenharmony_ci return resultVec; 753115cd2caSopenharmony_ci} 754115cd2caSopenharmony_ci 755115cd2caSopenharmony_cistd::vector<SipAddress> ContactsBuild::GetSipAddress(napi_env env, napi_value object) 756115cd2caSopenharmony_ci{ 757115cd2caSopenharmony_ci napi_value sipAddressArray = GetArrayByKey(env, object, "sipAddresses"); 758115cd2caSopenharmony_ci std::vector<SipAddress> resultVec; 759115cd2caSopenharmony_ci if (sipAddressArray == nullptr) { 760115cd2caSopenharmony_ci HILOG_ERROR("GetSipAddress arrKey is null "); 761115cd2caSopenharmony_ci return resultVec; 762115cd2caSopenharmony_ci } 763115cd2caSopenharmony_ci uint32_t size = 0; 764115cd2caSopenharmony_ci napi_get_array_length(env, sipAddressArray, &size); 765115cd2caSopenharmony_ci for (uint32_t i = 0; i < size; i++) { 766115cd2caSopenharmony_ci napi_value obj; 767115cd2caSopenharmony_ci napi_get_element(env, sipAddressArray, i, &obj); 768115cd2caSopenharmony_ci SipAddress targetObj; 769115cd2caSopenharmony_ci targetObj.sipAddress = GetStringValueByKey(env, obj, "sipAddress"); 770115cd2caSopenharmony_ci targetObj.labelName = GetStringValueByKey(env, obj, "labelName"); 771115cd2caSopenharmony_ci targetObj.labelId = GetIntValueByKey(env, obj, "labelId"); 772115cd2caSopenharmony_ci resultVec.push_back(targetObj); 773115cd2caSopenharmony_ci } 774115cd2caSopenharmony_ci return resultVec; 775115cd2caSopenharmony_ci} 776115cd2caSopenharmony_ci 777115cd2caSopenharmony_cistd::vector<Website> ContactsBuild::GetWebsite(napi_env env, napi_value object) 778115cd2caSopenharmony_ci{ 779115cd2caSopenharmony_ci napi_value websiteArray = GetArrayByKey(env, object, "websites"); 780115cd2caSopenharmony_ci std::vector<Website> resultVec; 781115cd2caSopenharmony_ci if (websiteArray == nullptr) { 782115cd2caSopenharmony_ci HILOG_ERROR("ContactsBuild GetWebsite arrKey is null "); 783115cd2caSopenharmony_ci return resultVec; 784115cd2caSopenharmony_ci } 785115cd2caSopenharmony_ci uint32_t size = 0; 786115cd2caSopenharmony_ci napi_get_array_length(env, websiteArray, &size); 787115cd2caSopenharmony_ci for (uint32_t i = 0; i < size; i++) { 788115cd2caSopenharmony_ci napi_value obj; 789115cd2caSopenharmony_ci napi_get_element(env, websiteArray, i, &obj); 790115cd2caSopenharmony_ci Website targetObj; 791115cd2caSopenharmony_ci targetObj.website = GetStringValueByKey(env, obj, "website"); 792115cd2caSopenharmony_ci resultVec.push_back(targetObj); 793115cd2caSopenharmony_ci } 794115cd2caSopenharmony_ci return resultVec; 795115cd2caSopenharmony_ci} 796115cd2caSopenharmony_ci 797115cd2caSopenharmony_ciNickName ContactsBuild::GetNickName(napi_env env, napi_value object) 798115cd2caSopenharmony_ci{ 799115cd2caSopenharmony_ci HILOG_INFO("ContactsBuild into GetNickName"); 800115cd2caSopenharmony_ci napi_value nickNameObj = GetObjectByKey(env, object, "nickName"); 801115cd2caSopenharmony_ci NickName resultObj; 802115cd2caSopenharmony_ci if (nickNameObj == nullptr) { 803115cd2caSopenharmony_ci HILOG_ERROR("ContactsBuild GetNickName nickNameKey is null "); 804115cd2caSopenharmony_ci return resultObj; 805115cd2caSopenharmony_ci } 806115cd2caSopenharmony_ci resultObj.nickName = GetStringValueByKey(env, nickNameObj, "nickName"); 807115cd2caSopenharmony_ci return resultObj; 808115cd2caSopenharmony_ci} 809115cd2caSopenharmony_ci 810115cd2caSopenharmony_ciNote ContactsBuild::GetNote(napi_env env, napi_value object) 811115cd2caSopenharmony_ci{ 812115cd2caSopenharmony_ci Note resultObj; 813115cd2caSopenharmony_ci napi_value noteObj = GetObjectByKey(env, object, "note"); 814115cd2caSopenharmony_ci if (noteObj == nullptr) { 815115cd2caSopenharmony_ci HILOG_ERROR("GetNote noteKey is null "); 816115cd2caSopenharmony_ci return resultObj; 817115cd2caSopenharmony_ci } 818115cd2caSopenharmony_ci resultObj.noteContent = GetStringValueByKey(env, noteObj, "noteContent"); 819115cd2caSopenharmony_ci return resultObj; 820115cd2caSopenharmony_ci} 821115cd2caSopenharmony_ci 822115cd2caSopenharmony_ciOrganization ContactsBuild::GetOrganization(napi_env env, napi_value object) 823115cd2caSopenharmony_ci{ 824115cd2caSopenharmony_ci napi_value organizationObject = GetObjectByKey(env, object, "organization"); 825115cd2caSopenharmony_ci Organization resultObj; 826115cd2caSopenharmony_ci if (organizationObject == nullptr) { 827115cd2caSopenharmony_ci HILOG_ERROR("GetOrganization organizationKey is null "); 828115cd2caSopenharmony_ci return resultObj; 829115cd2caSopenharmony_ci } 830115cd2caSopenharmony_ci resultObj.name = GetStringValueByKey(env, organizationObject, "name"); 831115cd2caSopenharmony_ci resultObj.title = GetStringValueByKey(env, organizationObject, "title"); 832115cd2caSopenharmony_ci return resultObj; 833115cd2caSopenharmony_ci} 834115cd2caSopenharmony_ci 835115cd2caSopenharmony_ciint ContactsBuild::GetInt(napi_env env, napi_value id) 836115cd2caSopenharmony_ci{ 837115cd2caSopenharmony_ci int64_t value = 0; 838115cd2caSopenharmony_ci if (id == nullptr) { 839115cd2caSopenharmony_ci HILOG_ERROR("GetInt id is 0"); 840115cd2caSopenharmony_ci return value; 841115cd2caSopenharmony_ci } 842115cd2caSopenharmony_ci napi_get_value_int64(env, id, &value); 843115cd2caSopenharmony_ci return value; 844115cd2caSopenharmony_ci} 845115cd2caSopenharmony_ci 846115cd2caSopenharmony_ciHolder ContactsBuild::GetHolder(napi_env env, napi_value object) 847115cd2caSopenharmony_ci{ 848115cd2caSopenharmony_ci Holder holder; 849115cd2caSopenharmony_ci if (object == nullptr) { 850115cd2caSopenharmony_ci HILOG_ERROR("GetHolder Holder is null "); 851115cd2caSopenharmony_ci return holder; 852115cd2caSopenharmony_ci } 853115cd2caSopenharmony_ci holder.bundleName = GetStringValueByKey(env, object, "bundleName"); 854115cd2caSopenharmony_ci holder.displayName = GetStringValueByKey(env, object, "displayName"); 855115cd2caSopenharmony_ci holder.holderId = GetIntValueByKey(env, object, "holderId"); 856115cd2caSopenharmony_ci HILOG_INFO(" ContactsBuild::GetHolder int id = %{public}d", holder.holderId); 857115cd2caSopenharmony_ci return holder; 858115cd2caSopenharmony_ci} 859115cd2caSopenharmony_ci 860115cd2caSopenharmony_ciContactAttributes ContactsBuild::GetContactAttributes(napi_env env, napi_value object) 861115cd2caSopenharmony_ci{ 862115cd2caSopenharmony_ci ContactAttributes contactAttributes; 863115cd2caSopenharmony_ci if (object == nullptr) { 864115cd2caSopenharmony_ci HILOG_ERROR("GetContactAttributes object is null "); 865115cd2caSopenharmony_ci return contactAttributes; 866115cd2caSopenharmony_ci } 867115cd2caSopenharmony_ci napi_value napiValueAttr = GetArrayByKey(env, object, "attributes"); 868115cd2caSopenharmony_ci std::vector<int> attrVector; 869115cd2caSopenharmony_ci uint32_t size = 0; 870115cd2caSopenharmony_ci napi_get_array_length(env, napiValueAttr, &size); 871115cd2caSopenharmony_ci for (uint32_t i = 0; i < size; i++) { 872115cd2caSopenharmony_ci napi_value intValue; 873115cd2caSopenharmony_ci napi_get_element(env, napiValueAttr, i, &intValue); 874115cd2caSopenharmony_ci int64_t intNapiValue; 875115cd2caSopenharmony_ci napi_get_value_int64(env, intValue, &intNapiValue); 876115cd2caSopenharmony_ci if (intNapiValue != 0) { 877115cd2caSopenharmony_ci attrVector.push_back(intNapiValue); 878115cd2caSopenharmony_ci } 879115cd2caSopenharmony_ci } 880115cd2caSopenharmony_ci contactAttributes.attributes = attrVector; 881115cd2caSopenharmony_ci return contactAttributes; 882115cd2caSopenharmony_ci} 883115cd2caSopenharmony_ci 884115cd2caSopenharmony_cistd::string ContactsBuild::NapiGetValueString(napi_env env, napi_value value) 885115cd2caSopenharmony_ci{ 886115cd2caSopenharmony_ci if (value == nullptr) { 887115cd2caSopenharmony_ci return ""; 888115cd2caSopenharmony_ci } 889115cd2caSopenharmony_ci char valueString[NAPI_GET_STRING_SIZE]; 890115cd2caSopenharmony_ci size_t valueSize = NAPI_GET_STRING_SIZE; 891115cd2caSopenharmony_ci size_t resultSize = 0; 892115cd2caSopenharmony_ci napi_get_value_string_utf8(env, value, valueString, valueSize, &resultSize); 893115cd2caSopenharmony_ci std::string resultValue = valueString; 894115cd2caSopenharmony_ci if (resultValue == "") { 895115cd2caSopenharmony_ci HILOG_ERROR("ContactsBuild NapiGetValueString Data error"); 896115cd2caSopenharmony_ci return ""; 897115cd2caSopenharmony_ci } 898115cd2caSopenharmony_ci return resultValue; 899115cd2caSopenharmony_ci} 900115cd2caSopenharmony_ci} // namespace ContactsApi 901115cd2caSopenharmony_ci} // namespace OHOS