Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package com.unbxd.client.feed;
import com.unbxd.client.ConnectionManager;
import com.unbxd.client.feed.exceptions.FeedInputException;
import com.unbxd.client.feed.exceptions.FeedUploadException;
import com.unbxd.client.feed.response.FeedResponse;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.log4j.Logger;
import org.codehaus.jackson.map.ObjectMapper;
import org.w3c.dom.Document;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* Created with IntelliJ IDEA.
* User: sourabh
* Date: 07/07/14
* Time: 3:30 PM
*
* http://unbxd.com/docs/
*
* Client class for calling Feed APIs
*/
public class FeedClient {
private static final Logger LOG = Logger.getLogger(FeedClient.class);
private String siteKey;
private String secretKey;
private boolean secure;
private List _fields;
private Map _addedDocs;
private Map _updatedDocs;
private Set _deletedDocs;
private List _taxonomyNodes;
private Map> _taxonomyMappings;
protected FeedClient(String siteKey, String secretKey, boolean secure){
this.siteKey = siteKey;
this.secretKey = secretKey;
this.secure = secure;
_fields = new ArrayList();
_addedDocs = new HashMap();
_updatedDocs = new HashMap();
_deletedDocs = new HashSet();
_taxonomyNodes = new ArrayList();
_taxonomyMappings = new HashMap>();
}
private String getFeedUrl(){
return (secure ? "https://" : "http://") + "feed.unbxdapi.com/upload/v2/" + secretKey + "/" + siteKey;
}
/**
* Adds schema for a field. Schema needs to be added only once.
*
* @param fieldName Name of the field. Following rules apply for field names.
*
*
Should be alphnumeric
*
Can contain hyphens and underscores
*
Can not start and end with -- or __
*
Can not start with numbers
*
* @param datatype Datatype of the field. Refer {@link DataType}
* @param multivalued True for allowing multiple values for each document
* @param autosuggest True to include field in autosuggest response
* @return this
*/
public FeedClient addSchema(String fieldName, DataType datatype, boolean multivalued, boolean autosuggest){
_fields.add(new FeedField(fieldName, datatype, multivalued, autosuggest));
return this;
}
/**
* Adds schema for a field. Schema needs to be added only once.
*
* @param fieldName Name of the field. Following rules apply for field names.
*
*
Should be alphnumeric
*
Can contain hyphens and underscores
*
Can not start and end with -- or __
*
Can not start with numbers
*
* @param datatype Datatype of the field. Refer {@link DataType}
* @return this
*/
public FeedClient addSchema(String fieldName, DataType datatype){
_fields.add(new FeedField(fieldName, datatype, false, false));
return this;
}
/**
* Adds a product to the field. If a product with the same uniqueId is found to be already present the product will be overwritten
* @param product
* @return this
*/
public FeedClient addProduct(FeedProduct product) {
_addedDocs.put(product.getUniqueId(), product);
return this;
}
/**
* Adds a list of products to the field. If a product with the same uniqueId is found to be already present the product will be overwritten
* @param products
* @return this
*/
public FeedClient addProducts(List products) {
for(FeedProduct product : products){
this.addProduct(product);
}
return this;
}
/**
* Add a variant to a product.
* @param parentUniqueId Unique Id of the parent product
* @param variantAttributes Attributes of the variant
* @return this
* @throws FeedInputException
*/
public FeedClient addVariant(String parentUniqueId, Map variantAttributes) throws FeedInputException {
if(!_addedDocs.containsKey(parentUniqueId)){
throw new FeedInputException("Parent product needs to be added");
}
_addedDocs.get(parentUniqueId).addAssociatedProduct(variantAttributes);
return this;
}
/**
* Add variants to a product.
* @param parentUniqueId Unique Id of the parent product
* @param variants List of attributes of the variants
* @return this
* @throws FeedInputException
*/
public FeedClient addVariants(String parentUniqueId, List