All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.plenigo.sdk.services.CompanyService Maven / Gradle / Ivy

There is a newer version: 1.7.2
Show newest version
package com.plenigo.sdk.services;

import com.plenigo.sdk.PlenigoException;
import com.plenigo.sdk.PlenigoManager;
import com.plenigo.sdk.internal.ApiParams;
import com.plenigo.sdk.internal.ApiResults;
import com.plenigo.sdk.internal.ApiURLs;
import com.plenigo.sdk.internal.util.HttpConfig;
import com.plenigo.sdk.internal.util.JWT;
import com.plenigo.sdk.internal.util.SdkUtils;
import com.plenigo.sdk.internal.util.ValidationUtils;
import com.plenigo.sdk.models.CompanyUser;
import com.plenigo.sdk.models.CompanyUserBillingAddress;
import com.plenigo.sdk.models.ElementList;
import com.plenigo.sdk.models.PageRequest;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import static com.plenigo.sdk.internal.util.SdkUtils.buildUrlQueryString;
import static com.plenigo.sdk.internal.util.SdkUtils.getValueIfNotNull;
import static com.plenigo.sdk.internal.util.SdkUtils.isNotBlank;
import static com.plenigo.sdk.internal.util.SdkUtils.toCsv;


/**
 * 

* This contains the services related to companies with plenigo, *

*

* Thread safety: This class is thread safe and can be injected. *

*/ public final class CompanyService { private static final Logger LOGGER = Logger.getLogger(CompanyService.class.getName()); private static final String RESPONSE_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss Z"; /** * Default constructor. */ private CompanyService() { } /** * Returns an user list based on a page request. * * @param request search criteria * * @return an element list of company users * * @throws PlenigoException if any error happens */ public static ElementList getUserList(PageRequest request) throws PlenigoException { Map params = new HashMap(); ValidationUtils.validate(request); params.put(ApiParams.PAGE_SIZE, request.getPageSize()); params.put(ApiParams.PAGE_NUMBER, request.getPageNumber()); Map objectMap = HttpConfig.get().getClient().get(PlenigoManager.get().getUrl(), ApiURLs.COMPANY_USERS, ApiURLs.COMPANY_USERS, buildUrlQueryString(params), JWT.generateJWTTokenHeader(PlenigoManager.get().getCompanyId(), PlenigoManager.get().getSecret())); return buildElementListForCompanyUsers(objectMap, request.getPageNumber()); } /** * Returns an user list based on the provided user list. * * @param userList user list to find * * @return the users found related to the company * * @throws PlenigoException if any error happens */ public static List getUserList(List userList) throws PlenigoException { Map params = new HashMap(); params.put(ApiParams.USER_IDS, toCsv(userList)); Map objectMap = HttpConfig.get().getClient().get(PlenigoManager.get().getUrl(), ApiURLs.COMPANY_USERS_SELECT, ApiURLs.COMPANY_USERS_SELECT, buildUrlQueryString(params), JWT.generateJWTTokenHeader(PlenigoManager.get().getCompanyId(), PlenigoManager.get().getSecret())); Object companyUsersObj = objectMap.get(ApiResults.ELEMENTS); return buildCompanyUserList(companyUsersObj); } /** * Builds an element list of company users. * * @param objectMap the json representation * @param pageNumber the page number to use * * @return an element list */ private static ElementList buildElementListForCompanyUsers(Map objectMap, int pageNumber) { String totalElementsStr = SdkUtils.getValueIfNotNull(objectMap, ApiResults.TOTAL_ELEMENTS); long totalElements = 0; if (!totalElementsStr.isEmpty()) { totalElements = Long.parseLong(totalElementsStr); } String pageSizeStr = SdkUtils.getValueIfNotNull(objectMap, ApiResults.PAGE_SIZE); int pageSize = 0; if (!pageSizeStr.isEmpty()) { pageSize = Integer.parseInt(pageSizeStr); } Object companyUsersObj = objectMap.get(ApiResults.ELEMENTS); List companyUsersList = buildCompanyUserList(companyUsersObj); return new ElementList(pageNumber, pageSize, totalElements, companyUsersList); } /** * Builds a list of company users from the company user json list. * * @param companyUsersObj the json list representation * * @return a list of company users */ private static List buildCompanyUserList(Object companyUsersObj) { List companyUsersList = new LinkedList(); if (companyUsersObj != null && companyUsersObj instanceof List) { List> companyUsers = (List>) companyUsersObj; for (Map companyUser : companyUsers) { String customerId = getValueIfNotNull(companyUser, ApiResults.CUST_ID); String email = getValueIfNotNull(companyUser, ApiResults.EMAIL); String userName = getValueIfNotNull(companyUser, ApiResults.USERNAME); String language = getValueIfNotNull(companyUser, ApiResults.LANGUAGE); String gender = getValueIfNotNull(companyUser, ApiResults.GENDER); String firstName = getValueIfNotNull(companyUser, ApiResults.FIRST_NAME); String name = getValueIfNotNull(companyUser, ApiResults.LAST_NAME); String mobileNumber = getValueIfNotNull(companyUser, ApiResults.MOBILE_NUMBER); String userState = getValueIfNotNull(companyUser, ApiResults.USER_STATE); String birthdayStr = getValueIfNotNull(companyUser, ApiResults.BIRTHDAY); Date birthday = null; if (isNotBlank(birthdayStr)) { try { birthday = new SimpleDateFormat(RESPONSE_DATE_FORMAT).parse(birthdayStr); } catch (ParseException e) { LOGGER.log(Level.SEVERE, "Could not parse the following date string: " + birthdayStr, e); } } String postCode = getValueIfNotNull(companyUser, ApiResults.POST_CODE); String street = getValueIfNotNull(companyUser, ApiResults.STREET); String additionalAddressInfo = getValueIfNotNull(companyUser, ApiResults.ADDITIONAL_ADDRESS_INFO); String city = getValueIfNotNull(companyUser, ApiResults.CITY); String state = getValueIfNotNull(companyUser, ApiResults.STATE); String country = getValueIfNotNull(companyUser, ApiResults.COUNTRY); String agreementState = getValueIfNotNull(companyUser, ApiResults.AGREEMENT_STATE); CompanyUserBillingAddress billingAddress = buildBillingAddressInfo(companyUser); companyUsersList.add(new CompanyUser(customerId, email, userName, language, gender, firstName, name, mobileNumber, userState, birthday, postCode, street, additionalAddressInfo, city, state, country, agreementState, billingAddress)); } } return companyUsersList; } /** * Builds a billing address info object from the provided json object representation. * * @param companyUser company user json object representation * * @return a company user billing address object */ private static CompanyUserBillingAddress buildBillingAddressInfo(Map companyUser) { String gender = getValueIfNotNull(companyUser, ApiResults.GENDER); String firstName = getValueIfNotNull(companyUser, ApiResults.FIRST_NAME); String name = getValueIfNotNull(companyUser, ApiResults.LAST_NAME); String company = getValueIfNotNull(companyUser, ApiResults.COMPANY); String street = getValueIfNotNull(companyUser, ApiResults.STREET); String additionalAddressInfo = getValueIfNotNull(companyUser, ApiResults.ADDITIONAL_ADDRESS_INFO); String postCode = getValueIfNotNull(companyUser, ApiResults.POST_CODE); String city = getValueIfNotNull(companyUser, ApiResults.CITY); String state = getValueIfNotNull(companyUser, ApiResults.STATE); String country = getValueIfNotNull(companyUser, ApiResults.COUNTRY); String vatNumber = getValueIfNotNull(companyUser, ApiResults.VAT_NUMBER); return new CompanyUserBillingAddress(gender, firstName, name, company, street, additionalAddressInfo, postCode, city, state, country, vatNumber); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy