Subversion Repositories SmartDukaan

Rev

Rev 9225 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 9225 Rev 9262
Line 8... Line 8...
8
import java.util.regex.Matcher;
8
import java.util.regex.Matcher;
9
import java.util.regex.Pattern;
9
import java.util.regex.Pattern;
10
 
10
 
11
import org.apache.thrift.TException;
11
import org.apache.thrift.TException;
12
import org.apache.thrift.transport.TTransportException;
12
import org.apache.thrift.transport.TTransportException;
-
 
13
import org.slf4j.Logger;
-
 
14
import org.slf4j.LoggerFactory;
13
 
15
 
14
import adwords.axis.v201309.basicoperations.AddTextAds;
16
import adwords.axis.v201309.basicoperations.AddTextAds;
15
import adwords.axis.v201309.basicoperations.PauseAd;
17
import adwords.axis.v201309.basicoperations.PauseAd;
16
 
18
 
17
import in.shop2020.googleadwords.AdwordsAdGroup;
19
import in.shop2020.googleadwords.AdwordsAdGroup;
Line 22... Line 24...
22
import in.shop2020.model.v1.catalog.Item;
24
import in.shop2020.model.v1.catalog.Item;
23
import in.shop2020.model.v1.catalog.status;
25
import in.shop2020.model.v1.catalog.status;
24
import in.shop2020.thrift.clients.AdwordsClient;
26
import in.shop2020.thrift.clients.AdwordsClient;
25
import in.shop2020.thrift.clients.CatalogClient;
27
import in.shop2020.thrift.clients.CatalogClient;
26
 
28
 
-
 
29
/*
-
 
30
 * Class AdwordsTextAdItemPriceSync - To Change Price of Item in the AdGroup Text Ad 
-
 
31
 * Accordingly to Item Price stored in our system.   
-
 
32
 */
-
 
33
 
27
public class AdwordsTextAdItemPriceSync{
34
public class AdwordsTextAdItemPriceSync{
-
 
35
	private static Logger logger = LoggerFactory.getLogger(AdwordsTextAdItemPriceSync.class);
-
 
36
	
28
	public static void main(String[] args) {
37
	public static void main(String[] args) {
29
		AdwordsClient adwordsServiceClient= null;
38
		AdwordsClient adwordsServiceClient= null;
30
		CatalogClient catalogServiceClient = null;
39
		CatalogClient catalogServiceClient = null;
31
		try {
40
		try {
32
			adwordsServiceClient = new AdwordsClient();
41
			adwordsServiceClient = new AdwordsClient();
33
			catalogServiceClient = new CatalogClient();
42
			catalogServiceClient = new CatalogClient();
34
		} catch (TTransportException e1) {
43
		} catch (TTransportException e1) {
35
			e1.printStackTrace();
44
			System.out.println("Error while Getting AdwordsClient.. "+e1.getMessage());
-
 
45
			logger.error("Error while Getting AdwordsClient.. "+e1.getMessage());
36
		}
46
		}
37
		
47
		
38
		if(adwordsServiceClient!=null){
48
		if(adwordsServiceClient!=null){
39
			Client client = adwordsServiceClient.getClient();
49
			Client client = adwordsServiceClient.getClient();
-
 
50
			//Get List of All Running AdGroups
40
			List<AdwordsAdGroup> adgroupList =null;
51
			List<AdwordsAdGroup> adgroupList =null;
41
			try {
52
			try {
42
				adgroupList = client.getAllAdwordsAdGroups();
53
				adgroupList = client.getAllAdwordsAdGroups();
43
			} catch (GoogleAdwordsServiceException e) {
54
			} catch (GoogleAdwordsServiceException e) {
-
 
55
				System.out.println("Error while Requesting Data from AdwordsClient.. "+e.getMessage());
44
				e.printStackTrace();
56
				logger.error("Error while Requesting Data from AdwordsClient.. "+e.getMessage());
45
			} catch (TException e) {
57
			} catch (TException e) {
-
 
58
				System.out.println("Error while Getting Data from AdwordsClient.. "+e.getMessage());
46
				e.printStackTrace();
59
				logger.error("Error while Getting Data from AdwordsClient.. "+e.getMessage());
47
			}
60
			}
-
 
61
			
-
 
62
			//Creating Set of AdGroups that having Catalog Id greater than zero.
48
			Set<AdwordsAdGroup> adGroupSet = new HashSet<AdwordsAdGroup>();
63
			Set<AdwordsAdGroup> adGroupSet = new HashSet<AdwordsAdGroup>();
49
			for(AdwordsAdGroup adgroup: adgroupList){
64
			for(AdwordsAdGroup adgroup: adgroupList){
50
				if(adgroup.getCatalogItemId()>0L){
65
				if(adgroup.getCatalogItemId()>0L){
51
					adGroupSet.add(adgroup);
66
					adGroupSet.add(adgroup);
52
				}
67
				}
Line 55... Line 70...
55
			List<Long> errorTextAdList = new ArrayList<Long>();
70
			List<Long> errorTextAdList = new ArrayList<Long>();
56
			for(AdwordsAdGroup adGroup : adGroupSet){
71
			for(AdwordsAdGroup adGroup : adGroupSet){
57
				if(catalogServiceClient!=null){
72
				if(catalogServiceClient!=null){
58
					long systemItemPrice = 0l;
73
					long systemItemPrice = 0l;
59
					in.shop2020.model.v1.catalog.CatalogService.Client catalogClient = catalogServiceClient.getClient();
74
					in.shop2020.model.v1.catalog.CatalogService.Client catalogClient = catalogServiceClient.getClient();
-
 
75
					//Get List of Items corresponding to Catalog Id.
60
					List<Item> itemList = null;
76
					List<Item> itemList = null;
61
					try {
77
					try {
62
						itemList = catalogClient.getItemsByCatalogId(adGroup.getCatalogItemId());
78
						itemList = catalogClient.getItemsByCatalogId(adGroup.getCatalogItemId());
63
					} catch (CatalogServiceException e1) {
79
					} catch (CatalogServiceException e) {
64
						e1.printStackTrace();
80
						System.out.println("Catalog Service EXception.."+e.getMessage());
-
 
81
						logger.error("Catalog Service EXception.."+e.getMessage());
65
					} catch (TException e1) {
82
					} catch (TException e) {
-
 
83
						System.out.println("Error While Getting Data from Catalog Client"+e.getMessage());
66
						e1.printStackTrace();
84
						logger.error("Error While Getting Data from Catalog Client"+e.getMessage());
67
					}
85
					}
-
 
86
					//Get Item system price. Here minimum item price will be considered.
68
					List<Double> priceList = new ArrayList<Double>();
87
					List<Double> priceList = new ArrayList<Double>();
69
					if(itemList!=null){
88
					if(itemList!=null){
70
						for(Item item : itemList){
89
						for(Item item : itemList){
71
							if(status.ACTIVE==item.getItemStatus()){
90
							if(status.ACTIVE==item.getItemStatus()){
72
								priceList.add(item.getSellingPrice());
91
								priceList.add(item.getSellingPrice());
Line 77... Line 96...
77
						continue;
96
						continue;
78
					}
97
					}
79
					Collections.sort(priceList);
98
					Collections.sort(priceList);
80
					systemItemPrice = (long)priceList.get(0).doubleValue();
99
					systemItemPrice = (long)priceList.get(0).doubleValue();
81
					long priceAtGoogleEnd = 0l;
100
					long priceAtGoogleEnd = 0l;
-
 
101
					//Get All AdGroup Ads as per AdGroup Id.
82
					List<AdwordsAdGroupAd> textAdList = null;
102
					List<AdwordsAdGroupAd> textAdList = null;
83
					try {
103
					try {
84
						textAdList = client.getAdwordsAdGroupAdsByAdgroupId(adGroup.getAdgroupId());
104
						textAdList = client.getAdwordsAdGroupAdsByAdgroupId(adGroup.getAdgroupId());
85
						if(textAdList!=null){
105
						if(textAdList!=null){
86
							for(AdwordsAdGroupAd textAd : textAdList){
106
							for(AdwordsAdGroupAd textAd : textAdList){
-
 
107
								//Check For Price Pattern in the Ad.
87
								Pattern pattern = Pattern.compile("Rs(.*?)#");
108
								Pattern pattern = Pattern.compile("Rs(.*?)#");
88
							    Matcher matcher = pattern.matcher(textAd.getDescription1());
109
							    Matcher matcher = pattern.matcher(textAd.getDescription1());
89
							    String change = "";
110
							    String change = "";
90
							    while (matcher.find()) {
111
							    while (matcher.find()) {
91
							        change=matcher.group(1);
112
							        change=matcher.group(1);
Line 94... Line 115...
94
							    	continue;
115
							    	continue;
95
							    }
116
							    }
96
							    try{
117
							    try{
97
							    	priceAtGoogleEnd = Long.parseLong(change);
118
							    	priceAtGoogleEnd = Long.parseLong(change);
98
							    	if(priceAtGoogleEnd > 0l){
119
							    	if(priceAtGoogleEnd > 0l){
-
 
120
							    		//Check for Price Change
99
							    		if(priceAtGoogleEnd != systemItemPrice){
121
							    		if(priceAtGoogleEnd != systemItemPrice){
-
 
122
							    			//Change status of Current Ad to PAUSED and Add a new Ad.
100
							    			try {
123
							    			try {
101
							    				AdwordsAdGroupAd adgrpad = new AdwordsAdGroupAd();
124
							    				AdwordsAdGroupAd adgrpad = new AdwordsAdGroupAd();
102
												adgrpad.setAdgroupadId(PauseAd.runExample(textAd.getAdgroupId(), textAd.getAdgroupadId()));
125
												adgrpad.setAdgroupadId(PauseAd.runExample(textAd.getAdgroupId(), textAd.getAdgroupadId()));
103
												adgrpad.setAdgroupId(textAd.getAdgroupId());
126
												adgrpad.setAdgroupId(textAd.getAdgroupId());
104
												adgrpad.setCampaignId(textAd.getCampaignId());
127
												adgrpad.setCampaignId(textAd.getCampaignId());
Line 107... Line 130...
107
												adgrpad.setDisplayUrl(textAd.getDisplayUrl());
130
												adgrpad.setDisplayUrl(textAd.getDisplayUrl());
108
												adgrpad.setHeadline(textAd.getHeadline());
131
												adgrpad.setHeadline(textAd.getHeadline());
109
												adgrpad.setUrl(textAd.getUrl());
132
												adgrpad.setUrl(textAd.getUrl());
110
												client.updateAdwordsAdGroupAd(adgrpad);
133
												client.updateAdwordsAdGroupAd(adgrpad);
111
											} catch (Exception e1) {
134
											} catch (Exception e1) {
112
												System.out.println("Error While Pausing the Adwords Text Ad with Ad Id.."+textAd.getAdgroupadId());
135
												System.out.println("Error While Pausing the Adwords Text Ad with Ad Id.."+textAd.getAdgroupadId()+"...Exception...."+e1.getMessage());
-
 
136
												logger.error("Error While Pausing the Adwords Text Ad with Ad Id.."+textAd.getAdgroupadId()+"...Exception...."+e1.getMessage());
113
												continue;
137
												continue;
114
											}
138
											}
115
							    			String newDescription1 = textAd.getDescription1().replace(change, systemItemPrice+"");
139
							    			String newDescription1 = textAd.getDescription1().replace(change, systemItemPrice+"");
116
							    			Long adGroupAdId =0l;
140
							    			Long adGroupAdId =0l;
117
											try {
141
											try {