Rev 13789 | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020;import in.shop2020.model.v1.catalog.CatalogService.Client;import in.shop2020.model.v1.catalog.FlipkartItem;import in.shop2020.model.v1.catalog.Item;import in.shop2020.model.v1.catalog.MarketPlaceItemPrice;import in.shop2020.serving.services.FlipkartPricingPannel;import in.shop2020.thrift.clients.CatalogClient;import in.shop2020.utils.GmailUtils;import java.io.BufferedReader;import java.io.File;import java.io.IOException;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import java.nio.charset.Charset;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import javax.mail.MessagingException;import org.apache.commons.codec.binary.Base64;import org.apache.http.HttpHeaders;import org.apache.http.HttpResponse;import org.apache.http.auth.AuthScope;import org.apache.http.auth.UsernamePasswordCredentials;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.StringEntity;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.thrift.TException;import org.apache.thrift.transport.TTransportException;import org.json.JSONException;public class CreateSubmitFlipkartPricingFeed {private static long time = System.currentTimeMillis();private static List<MarketPlaceItemPrice> flipkartPricingItems = new ArrayList<MarketPlaceItemPrice>();private static Map<Long,Item> aliveItemsMap = new HashMap<Long,Item>();private static Map<Long,FlipkartItem> flipkartItemsMap = new HashMap<Long,FlipkartItem>();static List<Long> updatedItems = new ArrayList<Long>();static java.text.SimpleDateFormat sdf;static String emailFromAddress;static String password;static GmailUtils mailer;static String sendTo[];static String errorSendTo[];private static List<MarketPlaceItemPrice> flipkartPricingUpdateItems;static{sdf = new java.text.SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");emailFromAddress = "build@shop2020.in";password = "cafe@nes";mailer = new GmailUtils();//sendTo = new String[]{"manish.sharma@shop2020.in"};sendTo = new String[]{ "sandeep.sachdeva@shop2020.in", "manish.sharma@shop2020.in", "rajneesh.arora@shop2020.in","khushal.bhatia@shop2020.in","manoj.kumar@saholic.com","chaitnaya.vats@saholic.com","yukti.jain@shop2020.in","yatin.singh@shop2020.in","chandan.kumar@shop2020.in","ankush.dhingra@shop2020.in","anikendra.das@shop2020.in"};errorSendTo = new String[]{"kshitij.sood@shop2020.in","manish.sharma@shop2020.in","amit.gupta@shop2020.in","anikendra.das@shop2020.in"};Client catalogServiceClient = null;try {catalogServiceClient = new CatalogClient().getClient();} catch (TTransportException e) {e.printStackTrace();}try {time = System.currentTimeMillis();flipkartPricingUpdateItems = catalogServiceClient.getMarketPlaceItemsForPriceUpdate(8);for(MarketPlaceItemPrice flipkartPricingUpdateItem:flipkartPricingUpdateItems){if(flipkartPricingUpdateItem.getLastUpdatedOn() > flipkartPricingUpdateItem.getLastUpdatedOnMarketplace() && !flipkartPricingUpdateItem.isIsPriceOverride() && flipkartPricingUpdateItem.isIsListedOnSource() && flipkartPricingUpdateItem.getSellingPrice()!=0){flipkartPricingItems.add(flipkartPricingUpdateItem);}}List<FlipkartItem> flipkartItems = null;boolean retry = true;while(retry){try{flipkartItems = catalogServiceClient.getAllFlipkartItems();retry = false;}catch(Exception e){catalogServiceClient = new CatalogClient().getClient();}}for(FlipkartItem flipkartItem:flipkartItems){flipkartItemsMap.put(flipkartItem.getItem_id(),flipkartItem);}retry = true;List<Item> aliveItems = null;while(retry){try{aliveItems = catalogServiceClient.getAllAliveItems();retry = false;}catch(Exception e){catalogServiceClient = new CatalogClient().getClient();}}for(in.shop2020.model.v1.catalog.Item thriftItem:aliveItems){aliveItemsMap.put(thriftItem.getId(), thriftItem);}} catch (TException e) {e.printStackTrace();}if(flipkartPricingItems.size()==0){String text = "";try {mailer.sendSSLMessage(sendTo,"No Change in Flipkart item Prices ", emailFromAddress, password, text);System.exit(0);} catch (MessagingException e) {e.printStackTrace();}}}public static void main(String[] args) throws ClientProtocolException, TException{DefaultHttpClient httpclient = new DefaultHttpClient();//httpclient = (DefaultHttpClient) WebClientWrapper.wrapClient(httpclient);//httpclient.getCredentialsProvider().setCredentials(// new AuthScope("sandbox-api.flipkart.net", 443),// new UsernamePasswordCredentials("og3yg5994bxbxxc1", "8a137068-417c-4e4f-ae95-9295ab938d88"));/// Test Server Access Keys//httpclient.getCredentialsProvider().setCredentials(// new AuthScope("api.flipkart.net", 443),// new UsernamePasswordCredentials("m2z93iskuj81qiid","0c7ab6a5-98c0-4cdc-8be3-72c591e0add4")); //Prod Server Access KeysHttpPost httppost = new HttpPost("https://api.flipkart.net/sellers/skus/listings/bulk"); // PRODString auth = "m2z93iskuj81qiid"+":"+"0c7ab6a5-98c0-4cdc-8be3-72c591e0add4";byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("US-ASCII")));String authHeader = "Basic " + new String(encodedAuth);httppost.setHeader(HttpHeaders.AUTHORIZATION, authHeader);//HttpPost httppost = new HttpPost("https://sandbox-api.flipkart.net/sellers/skus/listings/bulk"); // TESTStringBuffer jsonRequest = new StringBuffer();StringBuffer jsonStart = new StringBuffer();jsonStart = jsonStart.append("{\"listings\":[");StringBuffer jsonEnd = new StringBuffer();jsonEnd.append("]}");//System.out.println("JSON request " + jsonRequest);int i=0;StringBuffer pricingItems =new StringBuffer();String tableHeader = "<html><table border=\"1\" align=\"center\"><tr>"+ "<td><b>" + "Product Name" +"</b></td>"+"<td><b>" + "Item ID" + "</b></td>"+"<td><b>" + "Selling Price" + "</b></td>"+"</tr>";String tableFooter = "</table></html>";Item item;Map<Long, Boolean> feedResponseMap = new HashMap<Long, Boolean>();FlipkartPricingPannel fkPricing = new FlipkartPricingPannel();for(MarketPlaceItemPrice entry:flipkartPricingItems){if(aliveItemsMap.containsKey(entry.getItem_id())){item = aliveItemsMap.get(entry.getItem_id());}else{continue;}System.out.println("Item ID " + entry.getItem_id());System.out.println("Product Name " + getProductName(item));System.out.println("Selling Price " + entry.getSellingPrice());pricingItems.append("<tr>"+ "<td>" +getProductName(item)+"</td>"+"<td>" + item.getId() + "</td>"+"<td>" + entry.getSellingPrice() + "</td>"+"</tr>");try {feedResponseMap.put(entry.getItem_id(), fkPricing.updatePrice(flipkartItemsMap.get(entry.getItem_id()).getSkuAtFlipkart(), entry.getSellingPrice()+""));} catch (JSONException e) {e.printStackTrace();try {mailer.sendSSLMessage(errorSendTo, "Error While Flipkart Pricing Updation Item Id: "+ entry.getItem_id(), "Due to Json Exception in Flipkart Pricing Panel", emailFromAddress, password,new ArrayList<File>());}catch (MessagingException ex) {ex.printStackTrace();}} catch (IOException e) {e.printStackTrace();try{mailer.sendSSLMessage(errorSendTo, "Error While Flipkart Pricing Updation Item Id: "+ entry.getItem_id(), "Due to IO Exception in Flipkart Pricing Panel", emailFromAddress, password,new ArrayList<File>());}catch (MessagingException ex) {ex.printStackTrace();}}/*jsonRequest.append("{\"skuId\":"+"\""+flipkartItemsMap.get(entry.getItem_id()).getSkuAtFlipkart()+"\","+"\"attributeValues\""+":"+"{\"selling_price\""+":"+"\""+ new Double(entry.getSellingPrice()).intValue()+"\""+"},"+"\"listingValidations\":" +"{\"PRICE_ERROR_CHECK\":" +"\"disable\"}"+"}");i++;if(flipkartPricingItems.size()!=1 && (i!=flipkartPricingItems.size()) ){if(i%10!=0){jsonRequest.append(",");}}System.out.println("Value of i " + i + " FlipkartPricing Items List size " + flipkartPricingItems.size());if(i%10==0 || i==flipkartPricingItems.size()){System.out.println("Inside send code block");StringEntity input = null;try {input = new StringEntity(jsonStart+jsonRequest.toString()+jsonEnd);System.out.println("Json input " + jsonStart+jsonRequest.toString()+jsonEnd);jsonRequest = new StringBuffer();} catch (UnsupportedEncodingException e) {e.printStackTrace();}input.setContentType("application/json");httppost.setEntity(input);HttpResponse response = null;try {System.out.println("Before send " );response = httpclient.execute(httppost);System.out.println("After send " );} catch (IOException e) {e.printStackTrace();}BufferedReader rd = null;try {rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));} catch (IllegalStateException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}feedResponse.append("");String line;try {while ((line = rd.readLine()) != null) {System.out.println("Response " + line);if(line.equalsIgnoreCase("{\"status\":\"success\"}")){}else{feedResponse.append(line);}}} catch (IOException e) {e.printStackTrace();}}*/}System.out.println("Before sending email");String text = tableHeader+pricingItems.toString()+tableFooter;try {mailer.sendSSLMessage(sendTo,"Flipkart Pricing Sent Details ", emailFromAddress, password, text);} catch (MessagingException e) {e.printStackTrace();}Client catalogClient = null;StringBuffer feedResponse = new StringBuffer();for(Long itemId: feedResponseMap.keySet()){boolean updated = feedResponseMap.get(itemId);if(updated){feedResponse.append("Price Updated at Flipkart for Item Id : "+itemId +" Status: Successful\n");updatedItems.add(itemId);}else{feedResponse.append("Price Updated at Flipkart for Item Id : "+itemId +" Status: Failed\n");}}if(feedResponse.toString().length()>0){try {mailer.sendSSLMessage(sendTo, "Flipkart Pricing Updation Response ", feedResponse.toString(), emailFromAddress, password,new ArrayList<File>());if(updatedItems.size()>0){try {catalogClient = new CatalogClient().getClient();catalogClient.updateMarketPlacePriceUpdateStatus(updatedItems,time,8);} catch (TException e) {try {new CatalogClient().getClient().updateMarketPlacePriceUpdateStatus(updatedItems, time,8);} catch (TTransportException e1) {e1.printStackTrace();} catch (TException e1) {e1.printStackTrace();}e.printStackTrace();}}} catch (MessagingException e) {try {catalogClient = new CatalogClient().getClient();catalogClient.updateMarketPlacePriceUpdateStatus(updatedItems,time,8);} catch (TException ex) {try {new CatalogClient().getClient().updateMarketPlacePriceUpdateStatus(updatedItems, time,8);} catch (TTransportException e1) {e1.printStackTrace();} catch (TException e1) {e1.printStackTrace();}ex.printStackTrace();}e.printStackTrace();}}}static String getProductName(Item item){return getName(item.getBrand())+" " + getName(item.getModelName())+" " + getName(item.getModelNumber())+" " + getName(item.getColor());}static String getName(String name){if(name==null || name.length()==0){return "";}else{return name;}}}