
com.plenigo.sdk.services.ProductService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-sdk Show documentation
Show all versions of java-sdk Show documentation
Java SDK wrapping plenigo REST-API for easier usage.
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.models.PagingInfo;
import com.plenigo.sdk.internal.util.JWT;
import com.plenigo.sdk.internal.util.RestClient;
import com.plenigo.sdk.internal.util.SdkUtils;
import com.plenigo.sdk.models.ActionPeriod;
import com.plenigo.sdk.models.CategoryData;
import com.plenigo.sdk.models.CategoryInfo;
import com.plenigo.sdk.models.Image;
import com.plenigo.sdk.models.PagedList;
import com.plenigo.sdk.models.PricingData;
import com.plenigo.sdk.models.ProductData;
import com.plenigo.sdk.models.ProductInfo;
import com.plenigo.sdk.models.Subscription;
import com.plenigo.sdk.models.ValidityTime;
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;
/**
*
* This contains the services required for Product management with plenigo.
*
*
* Thread safety: This class is thread safe and can be injected.
*
*/
public final class ProductService {
private static final Logger LOGGER = Logger.getLogger(ProductService.class.getName());
/**
* Rest client used internally.
*/
private static RestClient client = new RestClient();
/**
* Default constructor.
*/
private ProductService() {
}
/**
* This method retrieves the product data of a provided product id.
* This can only be used for plenigo managed products.
*
* @param productId The product id to use.
*
* @return the user data related to the access token
*
* @throws PlenigoException whenever an error happens
*/
public static ProductData getProductData(String productId) throws PlenigoException {
LOGGER.log(Level.FINEST, "Getting the product data for the product id: {0} using the following company id: {1}",
new Object[]{productId, PlenigoManager.get().getCompanyId()});
Map response = client.get(PlenigoManager.get().getUrl(), ApiURLs.GET_PRODUCT, ApiURLs.GET_PRODUCT + "/" + productId, null,
JWT.generateJWTTokenHeader(PlenigoManager.get().getCompanyId(), PlenigoManager.get().getSecret()));
LOGGER.log(Level.FINEST, "JSON Data response from product id {0} : {1}", new Object[]{productId, response});
ProductData productData = buildProductData(response);
LOGGER.log(Level.FINEST, "Built Product Data from product id {0} : {1}", new Object[]{productId, productData});
return productData;
}
/**
* Obtain a list of product in a paginated way.
*
* @param pageSize The size of the page, it will be trimmed to 10...100
* @param lastId the last id of the page that will set the page number to be requested
*
* @return A list as a ResultSet with totalElements, page size, last id and the list of products
*
* @throws PlenigoException If any error happens
*/
public static PagedList getProductList(int pageSize, String lastId) throws PlenigoException {
Map params = new HashMap();
params.put(ApiParams.PAGE_SIZE, pageSize);
if (lastId != null && !lastId.trim().isEmpty()) {
params.put(ApiParams.LAST_ID, lastId);
}
Map objectMap = client.get(PlenigoManager.get().getUrl(), ApiURLs.LIST_PRODUCTS, ApiURLs.LIST_PRODUCTS,
SdkUtils.buildUrlQueryString(params), JWT.generateJWTTokenHeader(PlenigoManager.get().getCompanyId(), PlenigoManager.get().getSecret()));
return buildProductList(objectMap);
}
/**
* Obtain a list of product in a paginated way.
*
* @param pageSize The size of the page, it will be trimmed to 10...100
* @param lastId the last id of the page that will set the page number to be requested
*
* @return A list as a ResultSet with totalElements, page size, last id and the list of products
*
* @throws PlenigoException If any error happens
*/
public static PagedList getCategoryList(int pageSize, String lastId) throws PlenigoException {
Map params = new HashMap();
params.put(ApiParams.PAGE_SIZE, pageSize);
if (lastId != null && !lastId.trim().isEmpty()) {
params.put(ApiParams.LAST_ID, lastId);
}
Map objectMap = client.get(PlenigoManager.get().getUrl(), ApiURLs.LIST_CATEGORIES, ApiURLs.LIST_CATEGORIES,
SdkUtils.buildUrlQueryString(params), JWT.generateJWTTokenHeader(PlenigoManager.get().getCompanyId(), PlenigoManager.get().getSecret()));
return buildCategoryList(objectMap);
}
/**
* Retrieves the product list data values provided in the map,.
*
* @param response The response from the API call
*
* @return A {@link PagedList} object built from the map
*/
@SuppressWarnings("unchecked")
private static PagedList buildCategoryList(Map response) {
PagingInfo pagingInfo = buildPagingInfo(response, ApiResults.CATEGORY_ID);
Object elementsObj = response.get(ApiResults.ELEMENTS);
List categories = new LinkedList();
//https://code.google.com/p/json-simple/ Json simple library maps arrays as Lists
if (elementsObj instanceof List) {
List elements = (List) elementsObj;
for (Object elementObj : elements) {
//and objects as maps
if (elementObj instanceof Map) {
Map element = (Map) elementObj;
String catId = SdkUtils.getValueIfNotNull(element, ApiResults.CATEGORY_ID);
String title = SdkUtils.getValueIfNotNull(element, ApiResults.TITLE);
categories.add(new CategoryInfo(catId, title));
}
}
}
return new PagedList(categories, pagingInfo);
}
/**
* Retrieves the product list data values provided in the map,.
*
* @param response The response from the API call
*
* @return A {@link PagedList} object built from the map
*/
@SuppressWarnings("unchecked")
private static PagedList buildProductList(Map response) {
PagingInfo pagingInfo = buildPagingInfo(response, ApiResults.PROD_ID);
Object elementsObj = response.get(ApiResults.ELEMENTS);
List products = new LinkedList();
//https://code.google.com/p/json-simple/ Json simple library maps arrays as Lists
if (elementsObj instanceof List) {
List elements = (List) elementsObj;
for (Object elementObj : elements) {
//and objects as maps
if (elementObj instanceof Map) {
Map element = (Map) elementObj;
String prodId = SdkUtils.getValueIfNotNull(element, ApiResults.PROD_ID);
String title = SdkUtils.getValueIfNotNull(element, ApiResults.TITLE);
String description = SdkUtils.getValueIfNotNull(element, ApiResults.DESCRIPTION);
products.add(new ProductInfo(prodId, title, description));
}
}
}
return new PagedList(products, pagingInfo);
}
/**
* Builds an object containing the required paging information.
*
* @param response The response data from the API
* @param lastIdParam The parameter that contains the last id in the map
*
* @return a paging info object
*/
@SuppressWarnings("unchecked")
private static PagingInfo buildPagingInfo(Map response, String lastIdParam) {
String totalElementsStr = SdkUtils.getValueIfNotNull(response, ApiResults.TOTAL_ELEMENTS);
int totalElements = 0;
if (!totalElementsStr.isEmpty()) {
totalElements = Integer.parseInt(totalElementsStr);
}
String pageSizeStr = SdkUtils.getValueIfNotNull(response, ApiResults.PAGE_SIZE);
int pageSize = 0;
if (!pageSizeStr.isEmpty()) {
pageSize = Integer.parseInt(pageSizeStr);
}
Object lastIdProd = response.get(ApiResults.LAST_ID);
String lastId = null;
if (lastIdProd instanceof Map) {
Map lastIdMap = (Map) lastIdProd;
lastId = lastIdMap.get(lastIdParam).toString();
}
return new PagingInfo(lastId, pageSize, totalElements);
}
/**
* Retrieves the product data values provided in the map,.
*
* @param response The map that contains the data to retrieve
*
* @return A {@link ProductData} object built from the map
*/
private static ProductData buildProductData(Map response) {
String id = SdkUtils.getValueIfNotNull(response, ApiResults.ID);
String title = SdkUtils.getValueIfNotNull(response, ApiResults.TITLE);
String description = SdkUtils.getValueIfNotNull(response, ApiResults.DESCRIPTION);
Boolean collectible = false;
String isCollectible = SdkUtils.getValueIfNotNull(response, ApiResults.COLLECTIBLE);
if (!isCollectible.isEmpty()) {
collectible = Boolean.parseBoolean(isCollectible);
}
PricingData pricingData = buildPricingData(response);
Subscription subscriptionData = buildSubscription(response);
ActionPeriod actionPeriod = buildActionPeriod(response);
List imageData = buildImageList(response);
String maxParallelAccessStr = SdkUtils.getValueIfNotNull(response, ApiResults.MAX_PARALLEL_APP_ACCESS);
int maxParallelAccess = 0;
if (!maxParallelAccessStr.isEmpty()) {
maxParallelAccess = Integer.parseInt(maxParallelAccessStr);
}
String customInfo = SdkUtils.getValueIfNotNull(response, ApiResults.CUSTOM_INFO);
return new ProductData(id, subscriptionData, title, description, collectible, pricingData, actionPeriod, imageData, maxParallelAccess, customInfo);
}
/**
* Retrieves the image list values provided in the map.
*
* @param response The map that contains the data to retrieve
*
* @return a list of {@link Image} objects built from the map
*/
@SuppressWarnings("unchecked")
private static List buildImageList(Map response) {
Object images = response.get(ApiResults.IMAGES);
List imageData = new LinkedList();
if (images instanceof List) {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy