Subversion Repositories SmartDukaan

Rev

Rev 9225 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.googleadwords.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.thrift.TException;
import org.apache.thrift.transport.TTransportException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import adwords.axis.v201309.basicoperations.AddTextAds;
import adwords.axis.v201309.basicoperations.PauseAd;

import in.shop2020.googleadwords.AdwordsAdGroup;
import in.shop2020.googleadwords.AdwordsAdGroupAd;
import in.shop2020.googleadwords.GoogleAdwordsServiceException;
import in.shop2020.googleadwords.GoogleAdwordsService.Client;
import in.shop2020.model.v1.catalog.CatalogServiceException;
import in.shop2020.model.v1.catalog.Item;
import in.shop2020.model.v1.catalog.status;
import in.shop2020.thrift.clients.AdwordsClient;
import in.shop2020.thrift.clients.CatalogClient;

/*
 * Class AdwordsTextAdItemPriceSync - To Change Price of Item in the AdGroup Text Ad 
 * Accordingly to Item Price stored in our system.   
 */

public class AdwordsTextAdItemPriceSync{
        private static Logger logger = LoggerFactory.getLogger(AdwordsTextAdItemPriceSync.class);
        
        public static void main(String[] args) {
                AdwordsClient adwordsServiceClient= null;
                CatalogClient catalogServiceClient = null;
                try {
                        adwordsServiceClient = new AdwordsClient();
                        catalogServiceClient = new CatalogClient();
                } catch (TTransportException e1) {
                        System.out.println("Error while Getting AdwordsClient.. "+e1.getMessage());
                        logger.error("Error while Getting AdwordsClient.. "+e1.getMessage());
                }
                
                if(adwordsServiceClient!=null){
                        Client client = adwordsServiceClient.getClient();
                        //Get List of All Running AdGroups
                        List<AdwordsAdGroup> adgroupList =null;
                        try {
                                adgroupList = client.getAllAdwordsAdGroups();
                        } catch (GoogleAdwordsServiceException e) {
                                System.out.println("Error while Requesting Data from AdwordsClient.. "+e.getMessage());
                                logger.error("Error while Requesting Data from AdwordsClient.. "+e.getMessage());
                        } catch (TException e) {
                                System.out.println("Error while Getting Data from AdwordsClient.. "+e.getMessage());
                                logger.error("Error while Getting Data from AdwordsClient.. "+e.getMessage());
                        }
                        
                        //Creating Set of AdGroups that having Catalog Id greater than zero.
                        Set<AdwordsAdGroup> adGroupSet = new HashSet<AdwordsAdGroup>();
                        for(AdwordsAdGroup adgroup: adgroupList){
                                if(adgroup.getCatalogItemId()>0L){
                                        adGroupSet.add(adgroup);
                                }
                        }
                        List<Long> errorAdGroupList = new ArrayList<Long>();
                        List<Long> errorTextAdList = new ArrayList<Long>();
                        for(AdwordsAdGroup adGroup : adGroupSet){
                                if(catalogServiceClient!=null){
                                        long systemItemPrice = 0l;
                                        in.shop2020.model.v1.catalog.CatalogService.Client catalogClient = catalogServiceClient.getClient();
                                        //Get List of Items corresponding to Catalog Id.
                                        List<Item> itemList = null;
                                        try {
                                                itemList = catalogClient.getItemsByCatalogId(adGroup.getCatalogItemId());
                                        } catch (CatalogServiceException e) {
                                                System.out.println("Catalog Service EXception.."+e.getMessage());
                                                logger.error("Catalog Service EXception.."+e.getMessage());
                                        } catch (TException e) {
                                                System.out.println("Error While Getting Data from Catalog Client"+e.getMessage());
                                                logger.error("Error While Getting Data from Catalog Client"+e.getMessage());
                                        }
                                        //Get Item system price. Here minimum item price will be considered.
                                        List<Double> priceList = new ArrayList<Double>();
                                        if(itemList!=null){
                                                for(Item item : itemList){
                                                        if(status.ACTIVE==item.getItemStatus()){
                                                                priceList.add(item.getSellingPrice());
                                                        }
                                                }
                                        }
                                        else{
                                                continue;
                                        }
                                        Collections.sort(priceList);
                                        systemItemPrice = (long)priceList.get(0).doubleValue();
                                        long priceAtGoogleEnd = 0l;
                                        //Get All AdGroup Ads as per AdGroup Id.
                                        List<AdwordsAdGroupAd> textAdList = null;
                                        try {
                                                textAdList = client.getAdwordsAdGroupAdsByAdgroupId(adGroup.getAdgroupId());
                                                if(textAdList!=null){
                                                        for(AdwordsAdGroupAd textAd : textAdList){
                                                                //Check For Price Pattern in the Ad.
                                                                Pattern pattern = Pattern.compile("Rs(.*?)#");
                                                            Matcher matcher = pattern.matcher(textAd.getDescription1());
                                                            String change = "";
                                                            while (matcher.find()) {
                                                                change=matcher.group(1);
                                                            }
                                                            if("".equalsIgnoreCase(change)){
                                                                continue;
                                                            }
                                                            try{
                                                                priceAtGoogleEnd = Long.parseLong(change);
                                                                if(priceAtGoogleEnd > 0l){
                                                                        //Check for Price Change
                                                                        if(priceAtGoogleEnd != systemItemPrice){
                                                                                //Change status of Current Ad to PAUSED and Add a new Ad.
                                                                                try {
                                                                                        AdwordsAdGroupAd adgrpad = new AdwordsAdGroupAd();
                                                                                                adgrpad.setAdgroupadId(PauseAd.runExample(textAd.getAdgroupId(), textAd.getAdgroupadId()));
                                                                                                adgrpad.setAdgroupId(textAd.getAdgroupId());
                                                                                                adgrpad.setCampaignId(textAd.getCampaignId());
                                                                                                adgrpad.setDescription1(textAd.getDescription1());
                                                                                                adgrpad.setDescription2(textAd.getDescription2());
                                                                                                adgrpad.setDisplayUrl(textAd.getDisplayUrl());
                                                                                                adgrpad.setHeadline(textAd.getHeadline());
                                                                                                adgrpad.setUrl(textAd.getUrl());
                                                                                                client.updateAdwordsAdGroupAd(adgrpad);
                                                                                        } catch (Exception e1) {
                                                                                                System.out.println("Error While Pausing the Adwords Text Ad with Ad Id.."+textAd.getAdgroupadId()+"...Exception...."+e1.getMessage());
                                                                                                logger.error("Error While Pausing the Adwords Text Ad with Ad Id.."+textAd.getAdgroupadId()+"...Exception...."+e1.getMessage());
                                                                                                continue;
                                                                                        }
                                                                                String newDescription1 = textAd.getDescription1().replace(change, systemItemPrice+"");
                                                                                Long adGroupAdId =0l;
                                                                                        try {
                                                                                                adGroupAdId = AddTextAds.runExample(textAd.getAdgroupId(), textAd.getHeadline(), newDescription1, textAd.getDescription2(), textAd.getUrl(), textAd.getDisplayUrl());
                                                                                        } catch (Exception e) {
                                                                                                errorAdGroupList.add(textAd.getAdgroupId());
                                                                                                errorTextAdList.add(textAd.getAdgroupadId());
                                                                                                continue;
                                                                                        }
                                                                                        if(adGroupAdId > 0l){
                                                                                        AdwordsAdGroupAd adgroupad = new AdwordsAdGroupAd();
                                                                                        adgroupad.setAdgroupadId(adGroupAdId);
                                                                                        adgroupad.setAdgroupId(textAd.getAdgroupId());
                                                                                        adgroupad.setCampaignId(textAd.getCampaignId());
                                                                                        adgroupad.setDescription1(newDescription1);
                                                                                        adgroupad.setDescription2(textAd.getDescription2());
                                                                                        adgroupad.setDisplayUrl(textAd.getDisplayUrl());
                                                                                        adgroupad.setHeadline(textAd.getHeadline());
                                                                                        adgroupad.setUrl(textAd.getUrl());
                                                                                        client.addAdwordsAdGroupAd(adgroupad);
                                                                                        }
                                                                        }
                                                                }
                                                            } catch(NumberFormatException e){
                                                                        System.out.println(e.getMessage());
                                                                        continue;
                                                                } 
                                                            
                                                        }
                                                }
                                        } catch (GoogleAdwordsServiceException e) {
                                                e.printStackTrace();
                                        } catch (TException e) {
                                                e.printStackTrace();
                                        }
                                }
                        }
                        
                        
                        System.out.println("Error Ad Group List");
                        for(Long l: errorAdGroupList){
                                System.out.println(l);
                        }
                        
                        System.out.println("Error Text Ad List");
                        for(Long l: errorTextAdList){
                                System.out.println(l);
                        }
                        
                }
        }
}