Subversion Repositories SmartDukaan

Rev

Rev 9284 | Rev 9435 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
9067 manish.sha 1
package in.shop2020.support.controllers;
2
 
9351 manish.sha 3
import java.io.IOException;
9067 manish.sha 4
import java.text.ParseException;
5
import java.text.SimpleDateFormat;
6
import java.util.ArrayList;
7
import java.util.Collection;
9284 manish.sha 8
import java.util.Collections;
9067 manish.sha 9
import java.util.Date;
10
import java.util.HashMap;
11
import java.util.List;
12
import java.util.Map;
13
 
14
import in.shop2020.googleadwords.AdwordsAdGroup;
15
import in.shop2020.googleadwords.AdwordsAdGroupAd;
16
import in.shop2020.googleadwords.AdwordsAdGroupStatus;
17
import in.shop2020.googleadwords.AdwordsAdKeyword;
18
import in.shop2020.googleadwords.AdwordsCampaign;
19
import in.shop2020.googleadwords.AdwordsCampaignStatus;
20
import in.shop2020.googleadwords.GoogleAdwordsService.Client;
21
import in.shop2020.googleadwords.GoogleAdwordsServiceException;
22
import in.shop2020.googleadwords.KeywordMatchType;
23
import in.shop2020.googleadwords.NetworkSettingType;
9351 manish.sha 24
import in.shop2020.googleadwords.feedshandler.ContentFeedsHandler;
25
import in.shop2020.googleadwords.model.Product;
26
import in.shop2020.googleadwords.util.UserInformation;
27
import in.shop2020.logistics.DeliveryType;
28
import in.shop2020.logistics.LogisticsService;
9230 manish.sha 29
import in.shop2020.model.v1.catalog.CatalogServiceException;
30
import in.shop2020.model.v1.catalog.Item;
31
import in.shop2020.model.v1.catalog.status;
9067 manish.sha 32
import in.shop2020.support.utils.ReportsUtils;
33
import in.shop2020.thrift.clients.AdwordsClient;
9230 manish.sha 34
import in.shop2020.thrift.clients.CatalogClient;
9351 manish.sha 35
import in.shop2020.thrift.clients.LogisticsClient;
9067 manish.sha 36
 
37
import javax.servlet.ServletContext;
38
import javax.servlet.http.HttpServletRequest;
39
import javax.servlet.http.HttpSession;
40
 
41
import org.apache.struts2.convention.annotation.InterceptorRef;
42
import org.apache.struts2.convention.annotation.InterceptorRefs;
43
import org.apache.struts2.convention.annotation.Result;
44
import org.apache.struts2.convention.annotation.Results;
45
import org.apache.struts2.interceptor.ServletRequestAware;
46
import org.apache.struts2.util.ServletContextAware;
47
import org.apache.thrift.TException;
48
import org.apache.thrift.transport.TTransportException;
49
import org.json.JSONObject;
50
import org.slf4j.Logger;
51
import org.slf4j.LoggerFactory;
52
 
9230 manish.sha 53
import adwords.axis.v201309.basicoperations.UpdateKeyword;
9067 manish.sha 54
import adwords.axis.v201309.basicoperations.AddAdGroups;
55
import adwords.axis.v201309.basicoperations.AddCampaigns;
56
import adwords.axis.v201309.basicoperations.AddKeywords;
57
import adwords.axis.v201309.basicoperations.AddTextAds;
9230 manish.sha 58
import adwords.axis.v201309.basicoperations.UpdateAdGroup;
59
import adwords.axis.v201309.basicoperations.UpdateCampaign;
9067 manish.sha 60
 
61
import com.opensymphony.xwork2.ActionSupport;
62
 
63
@InterceptorRefs({
64
    @InterceptorRef("defaultStack"),
65
    @InterceptorRef("login")
66
})
67
 
68
@Results({
69
    @Result(name="authfail", type="redirectAction", params = {"actionName" , "reports"}),
70
	@Result(name="redirect", type="redirectAction", location = "google-adwords",  params={"statusCode", "301"})
71
})
72
public class GoogleAdwordsController extends ActionSupport implements ServletRequestAware {
73
	/**
74
	 * 
75
	 */
76
	private static Logger logger = LoggerFactory.getLogger(GoogleAdwordsController.class);
77
	private static final long serialVersionUID = 5703456181320066690L;
78
	private HttpServletRequest request;
79
	private HttpSession session;
80
	private ServletContext context;
81
 
82
	private String errorMsg = "";
83
	private String successmsg = "";
84
	private String campaignId = "";
9230 manish.sha 85
	private String adgroupId = "";
86
	private String keywordId = "";
9067 manish.sha 87
	private JSONObject jsonData = null;
9230 manish.sha 88
	private JSONObject jsonCampaignData = null;
89
	private JSONObject jsonAdgroupData = null;
90
	private JSONObject jsonKeywordList = null;
91
	private JSONObject jsonKeywordData = null;
92
 
9067 manish.sha 93
	private AdwordsCampaignStatus campStatus;
94
	private AdwordsAdGroupStatus adgrpStatus;
95
	private KeywordMatchType keywordMatchType1;
96
	private KeywordMatchType keywordMatchType2;
97
	private KeywordMatchType keywordMatchType3;
98
	private KeywordMatchType keywordMatchType4;
99
	private KeywordMatchType keywordMatchType5;
100
 
101
	private NetworkSettingType networkSettingType;
102
 
103
	private boolean googlesearchNetwork = false;
104
	private boolean displaynetwork = false;
105
	private boolean partnersearchNetwork = false;
106
	private int stocklinked = 0;
107
 
108
	private String adwordOpRadio;
109
	private String adwordAddRadio;
110
	private String campaignName;
111
	private String campaignStatus;
112
	private String startDate;
113
	private String endDate;
114
	private String campaignAmount;
115
	private String googleSearchNetwork;
116
	private String displayNetwork;
117
	private String partnerSearchNetwork;
118
	private String campaignList1;
119
	private String adgroupName;
120
	private String adgroupStatus;
121
	private String catalogItemId;
122
	private String bidAmount;
123
	private String stockLinked;
124
	private String campaignList2;
125
	private String adgroupList1;
126
	private String headline;
127
	private String description1;
128
	private String description2;
129
	private String url;
130
	private String displayUrl;
131
	private String campaignList3;
132
	private String adgroupList2;
133
	private String keyword1;
134
	private String keyword2;
135
	private String keyword3;
136
	private String keyword4;
137
	private String keyword5;
138
	private String bid1;
139
	private String bid2;
140
	private String bid3;
141
	private String bid4;
142
	private String bid5;
143
	private String matchType1;
144
	private String matchType2;
145
	private String matchType3;
146
	private String matchType4;
147
	private String matchType5;
9230 manish.sha 148
 
149
	private String adwordUpdateRadio;
150
	private String campaignListUpdate1;
151
	private String campaignNameUpdate;
152
	private String campaignStatusUpdate;
153
	private String campaignAmountUpdate;
154
	private String campaignListUpdate2;
155
	private String adgroupListUpdate1;
156
	private String adgroupNameUpdate;
157
	private String adgroupStatusUpdate;
158
	private String catalogItemIdUpdate;
159
	private String bidAmountUpdate;
160
	private String campaignListUpdate3;
161
	private String adgroupListUpdate2;
162
	private String keywordListUpdate1;
163
	private String keywordUpdate;
164
	private String bidUpdate;
165
	private String matchTypeUpdate;
166
 
9351 manish.sha 167
	private String cataLogItemId;
168
	private String sentFeedResult;
169
 
9067 manish.sha 170
 
171
	public String index() {
172
		if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getServletPath()))
173
			return "authfail";
174
		checkForErrors();
175
		return "index";
176
	}
177
 
178
	public String create(){
179
		if(adwordOpRadio!=null && ("addOpRadio").equalsIgnoreCase(adwordOpRadio)){
180
			if(adwordAddRadio!=null && ("campaignRdo").equalsIgnoreCase(adwordAddRadio)){
181
				processCampaignStatus();
182
				SimpleDateFormat timestmapFormatForGoogle = new SimpleDateFormat("yyyyMMdd");
183
				String startdateStr = timestmapFormatForGoogle.format(new Date().getTime());
184
				String enddateStr = "20371230";
185
				if(startDate!=null && !startDate.isEmpty()){
186
					startdateStr = processedStartEndDate(startDate, timestmapFormatForGoogle);
187
				}
188
				if(endDate!=null && !endDate.isEmpty()){
189
					 enddateStr = processedStartEndDate(endDate, timestmapFormatForGoogle);
190
				}
191
				processNetworkSetting();
192
				Long budgetAmount = new Long(Long.parseLong(campaignAmount));
193
				boolean addCampaignResult = addAdwordsCampaign(campaignName, startdateStr, enddateStr, (long)(budgetAmount * Math.pow(10, 6)), campStatus.toString(), googlesearchNetwork, displaynetwork, partnersearchNetwork, networkSettingType);
194
				if(addCampaignResult){
195
					successmsg = "Adwords Campaign Added Successfully";
196
				}
197
			}
198
			if(adwordAddRadio!=null && ("adgroupRdo").equalsIgnoreCase(adwordAddRadio)){
199
				processAdGroupStatus();
200
				if(catalogItemId==null || ("").equalsIgnoreCase(catalogItemId)){
201
					catalogItemId="0";
202
				}
203
				processStockLinked();
204
				boolean addAdGroupResult = addAdwordsAdGroup(Long.parseLong(campaignList1), adgroupName, adgrpStatus.toString(), (long)(Double.parseDouble(bidAmount) * Math.pow(10, 6)), Long.parseLong(catalogItemId));
205
				if(addAdGroupResult){
206
					successmsg = "Adwords AdGroup Added Successfully";
207
				}
208
			}
209
			if(adwordAddRadio!=null && ("textAdRdo").equalsIgnoreCase(adwordAddRadio)){
210
				boolean addAdgroupAdResult = addAdwordsAdGroupAd();
211
				if(addAdgroupAdResult){
212
					successmsg = "Adwords AdGroup Text Ad Added Successfully";
213
				}
214
			}
215
			if(adwordAddRadio!=null && ("keywordRdo").equalsIgnoreCase(adwordAddRadio)){
216
				AdwordsAdGroup adgroup = getAdwordsAdgroup(Long.parseLong(adgroupList2));
217
				List<Map<String, String>> keywordDataList = new ArrayList<Map<String, String>>();
218
				if(keyword1 != null && !keyword1.isEmpty()){
219
					keywordMatchType1 = processAdKeywordMatchType(matchType1);
220
					if(bid1!=null && !bid1.isEmpty()){
221
						keywordDataList.add(getKeywordMap(keyword1,bid1,keywordMatchType1.toString()));
222
					}
223
					else{
224
						keywordDataList.add(getKeywordMap(keyword1,adgroup.getBidAmount()+"",keywordMatchType1.toString()));
225
					}
226
				}
227
				if(keyword2 != null && !keyword2.isEmpty()){
228
					keywordMatchType2 = processAdKeywordMatchType(matchType2);
229
					if(bid2!=null && !bid2.isEmpty()){
230
						keywordDataList.add(getKeywordMap(keyword2,bid2,keywordMatchType2.toString()));
231
					}
232
					else{
233
						keywordDataList.add(getKeywordMap(keyword2,adgroup.getBidAmount()+"",keywordMatchType2.toString()));
234
					}
235
				}
236
				if(keyword3 != null && !keyword3.isEmpty()){
237
					keywordMatchType3 = processAdKeywordMatchType(matchType3);
238
					if(bid3!=null && !bid3.isEmpty()){
239
						keywordDataList.add(getKeywordMap(keyword3,bid3,keywordMatchType3.toString()));
240
					}
241
					else{
242
						keywordDataList.add(getKeywordMap(keyword3,adgroup.getBidAmount()+"",keywordMatchType3.toString()));
243
					}
244
				}
245
				if(keyword4 != null && !keyword4.isEmpty()){
246
					keywordMatchType4 = processAdKeywordMatchType(matchType4);
247
					if(bid4!=null && !bid4.isEmpty()){
248
						keywordDataList.add(getKeywordMap(keyword4,bid4,keywordMatchType4.toString()));
249
					}
250
					else{
251
						keywordDataList.add(getKeywordMap(keyword4,adgroup.getBidAmount()+"",keywordMatchType4.toString()));
252
					}
253
				}
254
				if(keyword5 != null && !keyword5.isEmpty()){
255
					keywordMatchType5 = processAdKeywordMatchType(matchType5);
256
					if(bid5!=null && !bid5.isEmpty()){
257
						keywordDataList.add(getKeywordMap(keyword5,bid5,keywordMatchType5.toString()));
258
					}
259
					else{
260
						keywordDataList.add(getKeywordMap(keyword5,adgroup.getBidAmount()+"",keywordMatchType5.toString()));
261
					}
262
				}
263
				boolean addAdKeywordResult = addAdwordsAdKeyword(keywordDataList);
264
				if(addAdKeywordResult){
265
					successmsg = "Adwords AdGroup Ad Keyword Added Successfully";
266
				}
267
			}
268
		}
9230 manish.sha 269
		if(adwordOpRadio!=null && ("updateOpRadio").equalsIgnoreCase(adwordOpRadio)){
270
			if(adwordUpdateRadio!=null && ("campaignRdoUpdate").equalsIgnoreCase(adwordUpdateRadio)){
271
				boolean updateCamapaignResult = updateAdwordsCampaign();
272
				if(updateCamapaignResult){
273
					successmsg = "Adwords Campaign Updated Successfully";
274
				}
275
			}
276
			if(adwordUpdateRadio!=null && ("adgroupRdoUpdate").equalsIgnoreCase(adwordUpdateRadio)){
277
				boolean updateAdGroupResult = updateAdwordsAdGroup();
278
				if(updateAdGroupResult){
279
					successmsg = "Adwords AdGroup Updated Successfully";
280
				}
281
			}
282
			if(adwordUpdateRadio!=null && ("keywordRdoUpdate").equalsIgnoreCase(adwordUpdateRadio)){
283
				boolean updateKeywordResult = updateAdwordsAdKeyword();
284
				if(updateKeywordResult){
285
					successmsg = "Adwords AdKeyword Updated Successfully";
286
				}
287
			}
288
		}
9067 manish.sha 289
 
290
		return index();
291
	}
292
 
9351 manish.sha 293
	public String sendProductFeeds(){
294
		//boolean sendFeedsResult = false;
295
		Product insertedProduct = null;
296
		if(cataLogItemId!=null && !("").equalsIgnoreCase(cataLogItemId)){
297
			List<Long> itemIdList = getEntityLogisticsEstimation(Long.parseLong(cataLogItemId));
298
			UserInformation userInformation = new UserInformation("8587366",
299
					"http://www.saholic.com",
300
					"adwords@shop2020.in", "adwords_shop2020");
301
			ContentFeedsHandler sample = new ContentFeedsHandler(userInformation,
302
					"https://content.googleapis.com/content/v1/");
303
			if(itemIdList!=null && itemIdList.size()>0){
304
				try {
305
					Item item = new CatalogClient().getClient().getItem(itemIdList.get(0));
306
					insertedProduct = sample.sendProductFeed(item);
307
				} catch (IOException e) {
308
					e.printStackTrace();
309
				} catch (TTransportException e) {
310
					e.printStackTrace();
311
				} catch (CatalogServiceException e) {
312
					e.printStackTrace();
313
				} catch (TException e) {
314
					e.printStackTrace();
315
				}
316
			}
317
			else{
318
				sentFeedResult = "Error: There is No Product avaliable for given Catalog Item Id.";
319
				return "output";
320
			}
321
		}
322
		if(insertedProduct!=null){
323
			if(insertedProduct.content!=null && insertedProduct.content.value.indexOf("Error:")>-1){
324
				sentFeedResult = insertedProduct.content.value;
325
				return "output";
326
			}
327
			sentFeedResult = "Product Feeds are sent Successfully and Product Details are Given Below:\nProduct Id- "
328
				+insertedProduct.externalId+" Product Title- "+insertedProduct.title;
329
			return "output";
330
		}
331
		else{
332
			sentFeedResult = "Some Error occured while sending feeds. Please contact concerned Team";
333
			return "output";
334
		}
335
		/*if(sendFeedsResult){
336
			successmsg = "Product Feeds are sent Successfully";
337
			return index();
338
		}
339
		else{
340
			errorMsg = "Some Error occured while sending feeds. Please contact concerned Team";
341
			return index();
342
		}*/
343
	}
344
 
345
	public List<Long> getEntityLogisticsEstimation(long productId){
346
    	List<Long> items=null; 
347
    	try {
348
    		LogisticsClient cl = new LogisticsClient();
349
            LogisticsService.Client client = cl.getClient();
350
            items = client.getEntityLogisticsEstimation(productId, "110001", DeliveryType.PREPAID );
351
        } catch (Exception e1) {
352
            System.out.println("Unable to get items by catalog item id"+e1.getMessage());
353
        }
354
        return items;
355
    }
356
 
9067 manish.sha 357
	public KeywordMatchType processAdKeywordMatchType(String value){
358
		return KeywordMatchType.findByValue(Integer.parseInt(value));
359
	}
360
 
361
	public AdwordsAdGroup getAdwordsAdgroup(long adgroupId){
362
		AdwordsAdGroup adgroup = new AdwordsAdGroup();
363
		try {
364
			adgroup = new AdwordsClient().getClient().getAdwordsAdGroupByAdGroupId(adgroupId);
365
		} catch (TTransportException e) {
366
			e.printStackTrace();
367
		} catch (GoogleAdwordsServiceException e) {
368
			e.printStackTrace();
369
		} catch (TException e) {
370
			e.printStackTrace();
371
		}
372
		return adgroup;
373
	}
374
 
375
	public Map<String, String> getKeywordMap(String keyword, String bid, String type){
376
		Map<String, String> keywordMap = new HashMap<String, String>();
377
		keywordMap.put("Text", keyword1);
378
		keywordMap.put("Type", type);
379
		double bidamount = Double.parseDouble(bid);
380
		bidamount = bidamount * Math.pow(10, 6);
381
		keywordMap.put("BidAmount", bidamount+"");
382
		return keywordMap;
383
	}
384
	public void processAdGroupStatus(){
385
		if(adgroupStatus!=null){
386
			if("1".equalsIgnoreCase(adgroupStatus)){
387
				adgrpStatus = AdwordsAdGroupStatus.ENABLED;
388
			}
389
			else if("2".equalsIgnoreCase(campaignStatus)){
390
				adgrpStatus = AdwordsAdGroupStatus.PAUSED;
391
			}
392
			else{
393
				campStatus = AdwordsCampaignStatus.DELETED;
394
			}
395
		}
396
	}
397
 
398
	public String processedStartEndDate(String dateString, SimpleDateFormat dateformat){
399
		SimpleDateFormat timestmapFormatRequest = new SimpleDateFormat("mm/dd/yyyy");
400
		String dateStr = "";
401
		try {
402
			Date date = timestmapFormatRequest.parse(dateString);
403
			dateStr = dateformat.format(date.getTime());
404
		} catch (ParseException e) {
405
			e.printStackTrace();
406
		}
407
		return dateStr;
408
	}
409
	public void processCampaignStatus(){
410
		if(campaignStatus!=null){
411
			if("1".equalsIgnoreCase(campaignStatus)){
412
				campStatus = AdwordsCampaignStatus.ACTIVE;
413
			}
414
			else if("2".equalsIgnoreCase(campaignStatus)){
415
				campStatus = AdwordsCampaignStatus.PAUSED;
416
			}
417
			else{
418
				campStatus = AdwordsCampaignStatus.DELETED;
419
			}
420
		}
421
	}
422
 
423
	public void processStockLinked(){
424
		if(stockLinked!=null && ("on").equalsIgnoreCase(stockLinked)){
425
			stocklinked = 1;
426
		}
427
	}
428
	public void processNetworkSetting(){
429
		if(googleSearchNetwork!=null && ("on").equalsIgnoreCase(googleSearchNetwork)){
430
			googlesearchNetwork = true;
431
		}
432
		if(displayNetwork!=null && ("on").equalsIgnoreCase(displayNetwork)){
433
			displaynetwork = true;
434
		}
435
		if(partnerSearchNetwork!=null && ("on").equalsIgnoreCase(partnerSearchNetwork)){
436
			partnersearchNetwork = true;
437
		}
438
 
439
		if(googlesearchNetwork && !displaynetwork && !partnersearchNetwork){
440
			networkSettingType = NetworkSettingType.GOOGLE_SEARCH_AND_SEARCH_NETWORK;
441
		}
442
		else if(googlesearchNetwork && displaynetwork && !partnersearchNetwork){
443
			networkSettingType = NetworkSettingType.SEARCH_AND_DISPLAY_NETWORK;
444
		}
445
		else if(googlesearchNetwork && !displaynetwork && partnersearchNetwork){
446
			networkSettingType = NetworkSettingType.SEARCH_AND_PARTNER_NETWORK;
447
		}
448
		else if(!googlesearchNetwork && displaynetwork && !partnersearchNetwork){
449
			networkSettingType = NetworkSettingType.DISPLAY_NETWORK;
450
		}
451
		else if(!googlesearchNetwork && displaynetwork && partnersearchNetwork){
452
			networkSettingType = NetworkSettingType.DISPLAY_AND_PARTNER_NETWORK;
453
		}
454
		else if(!googlesearchNetwork && !displaynetwork && partnersearchNetwork){
455
			networkSettingType = NetworkSettingType.GOOGLE_PARTNER_SEARCH_NETWORK;
456
		}
457
		else if(googlesearchNetwork && displaynetwork && partnersearchNetwork){
458
			networkSettingType = NetworkSettingType.ALL_AVALIABLE_SOURCES;
459
		}
460
 
461
	}
462
 
463
	public boolean addAdwordsCampaign(String name, String startDateStr, String endDateStr, 
464
			Long amount, String status, boolean googlesrchNtwrk, boolean displayntwrk, 
465
			boolean partnersrchNtwrk, NetworkSettingType networkType){
466
		Long campaignId = 0L;
467
		boolean retry = true;
468
		int attempt =0;
469
		while(retry && attempt<2){
470
			try{
471
				attempt = attempt+1;
472
				campaignId = AddCampaigns.runExample(name, status, startDateStr, endDateStr, amount, googlesrchNtwrk, displayntwrk, partnersrchNtwrk);
473
				retry = false;
474
				System.out.println("@@@@@ CampaignId...... "+campaignId);
475
			}
476
			catch (Exception e) {
477
				retry = true;
478
				logger.error("Error while requesting Google Adwords Campaign Service", e);
479
			}
480
		}
481
		if(campaignId==0L){
482
			addActionError("Error while processing Add Request for Google Campaign Service");
483
			return false;
484
		}
485
		try{
486
			SimpleDateFormat timestmapFormatForGoogle = new SimpleDateFormat("yyyymmdd");
487
			AdwordsCampaign campaign = new AdwordsCampaign();
488
			campaign.setCampaignId(campaignId);
489
			campaign.setName(name);
490
			campaign.setBudgetAmount(amount * Math.pow(10, -6));
491
			campaign.setNetworkType(networkType);
492
			campaign.setStartDate(timestmapFormatForGoogle.parse(startDateStr).getTime());
493
			campaign.setEndDate(timestmapFormatForGoogle.parse(endDateStr).getTime());
494
			campaign.setStatus(campStatus);
495
			new AdwordsClient().getClient().addAdwordsCampaign(campaign);
496
			return true;
497
		}
498
		catch(ParseException pe){
499
			addActionError("Unable to parse proper Data");
500
			logger.error("Error while parsing proper Data", pe);
501
		} catch (TTransportException e) {
9230 manish.sha 502
			addActionError("Unable to get Connection with Saholic Google Adowrds Services in addAdwordsCampaign Method");
503
			logger.error("Error while getting Connection with Saholic Google Adowrds Services in addAdwordsCampaign Method", e);
9067 manish.sha 504
		} catch (GoogleAdwordsServiceException e) {
9230 manish.sha 505
			addActionError("Unable to perform operations with Saholic Google Adowrds Services in addAdwordsCampaign Method");
506
			logger.error("Error while performing operations with Saholic Google Adowrds Services in addAdwordsCampaign Method", e);
9067 manish.sha 507
		} catch (TException e) {
9230 manish.sha 508
			addActionError("Unable to perform operations with Saholic Google Adowrds Services Client in addAdwordsCampaign Method");
509
			logger.error("Error while performing operations with Saholic Google Adowrds Services Client in addAdwordsCampaign Method", e);
9067 manish.sha 510
		}
511
 
512
		return false;
513
	}
514
 
515
	public boolean addAdwordsAdGroup(long campaignId, String name, String status, Long bidAmount, long catalogId){
516
		Long adGroupId = 0L;
517
		boolean retry = true;
518
		int attempt =0;
519
		while(retry && attempt < 2){
520
			try{
521
				attempt =attempt +1;
522
				adGroupId = AddAdGroups.runExample(campaignId, name, status, bidAmount);
523
				retry = false;
524
			}
525
			catch (Exception e) {
526
				retry = true;
527
				logger.error("Error while requesting Google Adwords AdGroup Service", e);
528
			}
529
		}
530
		if(adGroupId == 0L){
531
			addActionError("Error while processing Add Request for Google AdGroup Service");
532
			return false;
533
		}
534
		try{
535
			AdwordsAdGroup adgroup = new AdwordsAdGroup();
536
			adgroup.setAdgroupId(adGroupId);
537
			adgroup.setCampaignId(campaignId);
538
			adgroup.setCatalogItemId(catalogId);
539
			adgroup.setStatus(adgrpStatus);
540
			adgroup.setName(name);
541
			adgroup.setBidAmount(bidAmount * Math.pow(10, -6));
542
			adgroup.setStockLinked(stocklinked);
543
			new AdwordsClient().getClient().addAdwordsAdGroup(adgroup);
544
			return true;
545
		}
546
		catch (TTransportException e) {
9230 manish.sha 547
			addActionError("Unable to get Connection with Saholic Google Adowrds Services in addAdwordsAdGroup Method");
548
			logger.error("Error while getting Connection with Saholic Google Adowrds Services in addAdwordsAdGroup Method", e);
9067 manish.sha 549
		} catch (GoogleAdwordsServiceException e) {
9230 manish.sha 550
			addActionError("Unable to perform operations with Saholic Google Adowrds Services in addAdwordsAdGroup Method");
551
			logger.error("Error while performing operations with Saholic Google Adowrds Services in addAdwordsAdGroup Method", e);
9067 manish.sha 552
		} catch (TException e) {
9230 manish.sha 553
			addActionError("Unable to perform operations with Saholic Google Adowrds Services Client in addAdwordsAdGroup Method");
554
			logger.error("Error while performing operations with Saholic Google Adowrds Services Client in addAdwordsAdGroup Method", e);
9067 manish.sha 555
		}
556
		return false;
557
	}
558
 
559
	public boolean addAdwordsAdGroupAd(){
560
		Long adGroupAdId = 0L;
561
		int attempt =0;
562
		boolean retry = true;
9230 manish.sha 563
		if(description1.contains("#price")){
564
			description1 = description1.replace("#price", "Rs"+getTextAdItemPrice()+"#");
565
		}
566
		if(description1.length()>35){
567
			addActionError("Description1 length is greater than 35. Final String is given as.... "+description1);
568
			return false;
569
		}
9067 manish.sha 570
		while(retry && attempt<2){
571
			try{
572
				attempt = attempt+1;
573
				adGroupAdId = AddTextAds.runExample(Long.parseLong(adgroupList1), headline, description1, description2, url, displayUrl);
574
				retry = false;
575
			}
576
			catch (Exception e) {
577
				retry = true;
578
				logger.error("Error while requesting Google Adwords AdGroupAd Service", e);
579
			}
580
		}
581
		if(adGroupAdId == 0L){
582
			addActionError("Error while processing Add Request for Google AdGroupAd Service");
583
			return false;
584
		}
585
		try{
586
			AdwordsAdGroupAd adgroupad = new AdwordsAdGroupAd();
587
			adgroupad.setAdgroupadId(adGroupAdId);
588
			adgroupad.setCampaignId(Long.parseLong(campaignList2));
589
			adgroupad.setAdgroupId(Long.parseLong(adgroupList1));
590
			adgroupad.setDescription1(description1);
591
			adgroupad.setDescription2(description2);
592
			adgroupad.setHeadline(headline);
593
			adgroupad.setDisplayUrl(displayUrl);
594
			adgroupad.setUrl(url);
595
			new AdwordsClient().getClient().addAdwordsAdGroupAd(adgroupad);
596
			return true;
597
		}
598
		catch (TTransportException e) {
9230 manish.sha 599
			addActionError("Unable to get Connection with Saholic Google Adowrds Services in addAdwordsAdGroupAd Method");
600
			logger.error("Error while getting Connection with Saholic Google Adowrds Services in addAdwordsAdGroupAd Method", e);
9067 manish.sha 601
		} catch (GoogleAdwordsServiceException e) {
9230 manish.sha 602
			addActionError("Unable to perform operations with Saholic Google Adowrds Services in addAdwordsAdGroupAd Method");
603
			logger.error("Error while performing operations with Saholic Google Adowrds Services in addAdwordsAdGroupAd Method", e);
9067 manish.sha 604
		} catch (TException e) {
9230 manish.sha 605
			addActionError("Unable to perform operations with Saholic Google Adowrds Services Client in addAdwordsAdGroupAd Method");
606
			logger.error("Error while performing operations with Saholic Google Adowrds Services Client in addAdwordsAdGroupAd Method", e);
9067 manish.sha 607
		}
608
 
609
		return false;
610
	}
611
 
612
	public boolean addAdwordsAdKeyword(List<Map<String,String>> keywordDataList){
613
		List<Map<String,String>> adgroupDataList = new ArrayList<Map<String,String>>();
614
		int attempt =0;
615
		boolean retry = true;
616
		while(retry && attempt<2){
617
			try{
618
				attempt = attempt+1;
619
				adgroupDataList = AddKeywords.runExample(Long.parseLong(adgroupList2), keywordDataList);
620
				retry = false;
621
			}
622
			catch (Exception e) {
623
				retry = true;
624
				logger.error("Error while requesting Google Adwords AdGroup Criterion Service", e);
625
			}
626
		}
627
		if(adgroupDataList!=null && adgroupDataList.size()>0){
628
			try{
629
				AdwordsClient adwordsServiceClient = new AdwordsClient();
630
				Client client = adwordsServiceClient.getClient();
631
				for(Map<String,String> keywordMap : adgroupDataList){
632
					AdwordsAdKeyword adkeyword = new AdwordsAdKeyword();
633
					adkeyword.setAdgroupId(Long.parseLong(adgroupList2));
634
					adkeyword.setCampaignId(Long.parseLong(campaignList3));
635
					adkeyword.setCriterionId(Long.parseLong(keywordMap.get("CriterionId")));
636
					adkeyword.setText(keywordMap.get("Text"));
637
					adkeyword.setMatchType(KeywordMatchType.valueOf(keywordMap.get("Type")));
638
					adkeyword.setBidAmount( (Double.parseDouble(keywordMap.get("BidAmount"))) * (Math.pow(10, -6) ));
639
					client.addAdwordsAdKeyword(adkeyword);
640
				}
641
				return true;
642
			}
643
			catch (TTransportException e) {
9230 manish.sha 644
				addActionError("Unable to get Connection with Saholic Google Adowrds Services in addAdwordsAdKeyword Method");
645
				logger.error("Error while getting Connection with Saholic Google Adowrds Services in addAdwordsAdKeyword Method", e);
9067 manish.sha 646
			} catch (GoogleAdwordsServiceException e) {
9230 manish.sha 647
				addActionError("Unable to perform operations with Saholic Google Adowrds Services in addAdwordsAdKeyword Method");
648
				logger.error("Error while performing operations with Saholic Google Adowrds Services in addAdwordsAdKeyword Method", e);
9067 manish.sha 649
			} catch (TException e) {
9230 manish.sha 650
				addActionError("Unable to perform operations with Saholic Google Adowrds Services Client in addAdwordsAdKeyword Method");
651
				logger.error("Error while performing operations with Saholic Google Adowrds Services Client in addAdwordsAdKeyword Method", e);
9067 manish.sha 652
			}
653
		}
9230 manish.sha 654
		else{
655
			addActionError("Error while processing Add Request for Google AdGroupCriterion Service");
656
		}
9067 manish.sha 657
		return false;
658
	}
659
 
9230 manish.sha 660
	public boolean updateAdwordsCampaign(){
661
		AdwordsClient adwordsServiceClient = null;
662
		try {
663
			adwordsServiceClient = new AdwordsClient();
664
		} catch (TTransportException e) {
665
			addActionError("Unable to get Connection with Saholic Google Adowrds Services in updateAdwordsCampaign Method");
666
			logger.error("Error while getting Connection with Saholic Google Adowrds Services in updateAdwordsCampaign Method", e); 
667
		}
668
		if(adwordsServiceClient!=null){
669
			Client client = adwordsServiceClient.getClient();
670
			if(campaignListUpdate1!=null && !("-1").equalsIgnoreCase(campaignListUpdate1)){
671
				long amount= -1l;
672
				String status = "";
673
				String name = "";
674
				AdwordsCampaign campaign = null;
675
				try {
676
					campaign = client.getAdwordsCampaignByCampaignId(Long.parseLong(campaignListUpdate1));
677
				} catch (NumberFormatException e) {
678
					addActionError("Number Format Exception for CampaignId.."+campaignListUpdate1);
679
					logger.error("Number Format Exception for CampaignId.."+campaignListUpdate1, e);
680
				} catch (GoogleAdwordsServiceException e) {
681
					addActionError("Unable to perform operations with Saholic Google Adowrds Services in updateAdwordsCampaign Method while getting Data");
682
					logger.error("Error while performing operations with Saholic Google Adowrds Services in updateAdwordsCampaign Method while getting Data", e);
683
				} catch (TException e) {
684
					addActionError("Unable to perform operations with Saholic Google Adowrds Services Client in updateAdwordsCampaign Method while getting Data");
685
					logger.error("Error while performing operations with Saholic Google Adowrds Services Client in updateAdwordsCampaign Method while getting Data", e);
686
				}
687
				if(campaign!=null){
688
					if(campaignNameUpdate!=null && !("").equalsIgnoreCase(campaignNameUpdate)){
689
						if(!campaign.getName().equalsIgnoreCase(campaignNameUpdate)){
690
							campaign.setName(campaignNameUpdate);
691
							name = campaignNameUpdate;
692
						}
693
					}
694
					if(!campaign.getStatus().toString().equalsIgnoreCase(AdwordsCampaignStatus.valueOf(campaignStatusUpdate).toString())){
695
						campaign.setStatus(AdwordsCampaignStatus.valueOf(campaignStatusUpdate));
696
						status = AdwordsCampaignStatus.valueOf(campaignStatusUpdate).toString();
697
					}
698
					if(campaignAmountUpdate!=null && !("").equalsIgnoreCase(campaignAmountUpdate)){
699
						if(campaign.getBudgetAmount()!=Double.parseDouble(campaignAmountUpdate)){
700
							campaign.setBudgetAmount(Double.parseDouble(campaignAmountUpdate));
701
							amount = (long)(Double.parseDouble(campaignAmountUpdate) * Math.pow(10, 6));
702
						}
703
					}
704
					Long updatedCampaignId = 0l;
705
					try {
706
						updatedCampaignId = UpdateCampaign.runExample(Long.parseLong(campaignListUpdate1), status, name, amount);
707
					} catch (NumberFormatException e) {
708
						addActionError("Number Format Exception for CampaignId (Google End).."+campaignListUpdate1);
709
						logger.error("Number Format Exception for CampaignId (Google End).."+campaignListUpdate1, e);
710
					} catch (Exception e) {
711
						addActionError("Campaign Service Mutate Operation Exception ");
712
						logger.error("Error while Campaign Service Mutate Operation", e);
713
					}
714
					if(updatedCampaignId > 0l){
715
						campaign.setCampaignId(updatedCampaignId);
716
						try {
717
							client.updateAdwordsCampaign(campaign);
718
							return true;
719
						} catch (GoogleAdwordsServiceException e) {
720
							addActionError("Unable to perform operations with Saholic Google Adowrds Services in updateAdwordsCampaign Method while updating Data");
721
							logger.error("Error while performing operations with Saholic Google Adowrds Services in updateAdwordsCampaign Method while updating Data", e); 
722
						} catch (TException e) {
723
							addActionError("Unable to perform operations with Saholic Google Adowrds Services Client in updateAdwordsCampaign Method while updating Data");
724
							logger.error("Error while performing operations with Saholic Google Adowrds Services Client in updateAdwordsCampaign Method while updating Data", e);
725
						}
726
					}
727
					else{
728
						addActionError("Error while processing Update Request for Google Campaign Service");
729
					}
730
				}
731
			}
732
		}
733
 
734
		return false;
735
	}
736
 
737
	public boolean updateAdwordsAdGroup(){
738
		AdwordsClient adwordsServiceClient = null;
739
		try {
740
			adwordsServiceClient = new AdwordsClient();
741
		} catch (TTransportException e) {
742
			e.printStackTrace();
743
		}
744
		if(adwordsServiceClient!=null){
745
			Client client = adwordsServiceClient.getClient();
746
			if(adgroupListUpdate1!=null && !("-1").equalsIgnoreCase(adgroupListUpdate1)){
747
				String name="";
748
				String status ="";
749
				long bidAmount = -1L;
750
				AdwordsAdGroup adgroup = null;
751
				try {
752
					adgroup = client.getAdwordsAdGroupByAdGroupId(Long.parseLong(adgroupListUpdate1));
753
				} catch (NumberFormatException e) {
754
					addActionError("Number Format Exception for AdGroupId.."+adgroupListUpdate1);
755
					logger.error("Number Format Exception for AdGroupId.."+adgroupListUpdate1, e);
756
				} catch (GoogleAdwordsServiceException e) {
757
					addActionError("Unable to perform operations with Saholic Google Adowrds Services in updateAdwordsAdGroup Method while getting Data");
758
					logger.error("Error while performing operations with Saholic Google Adowrds Services in updateAdwordsAdGroup Method while getting Data", e);
759
				} catch (TException e) {
760
					addActionError("Unable to perform operations with Saholic Google Adowrds Services Client in updateAdwordsAdGroup Method while getting Data");
761
					logger.error("Error while performing operations with Saholic Google Adowrds Services Client in updateAdwordsAdGroup Method while getting Data", e);
762
				}
763
				if(adgroup!=null){
764
					if(adgroupNameUpdate!=null && !("").equalsIgnoreCase(adgroupNameUpdate)){
765
						if(!adgroup.getName().equalsIgnoreCase(adgroupNameUpdate)){
766
							adgroup.setName(adgroupNameUpdate);
767
							name = adgroupNameUpdate;
768
						}			
769
					}
770
					if(!adgroup.getStatus().toString().equalsIgnoreCase(AdwordsAdGroupStatus.valueOf(adgroupStatusUpdate).toString())){
771
						adgroup.setStatus(AdwordsAdGroupStatus.valueOf(adgroupStatusUpdate));
772
						status = AdwordsAdGroupStatus.valueOf(adgroupStatusUpdate).toString();
773
					}
774
					if(catalogItemIdUpdate!=null && !("").equalsIgnoreCase(catalogItemIdUpdate)){
775
						if(adgroup.getCatalogItemId()!= Long.parseLong(catalogItemIdUpdate)){
776
							adgroup.setCatalogItemId(Long.parseLong(catalogItemIdUpdate));
777
						}
778
					}
779
					if(bidAmountUpdate!=null && !("").equalsIgnoreCase(bidAmountUpdate)){
780
						if(adgroup.getBidAmount()!=Double.parseDouble(bidAmountUpdate)){
781
							adgroup.setBidAmount(Double.parseDouble(bidAmountUpdate));
782
							bidAmount = (long) (Double.parseDouble(bidAmountUpdate) * Math.pow(10, 6));
783
						}
784
					}
785
 
786
					Long updatedAdgroupId = 0l;
787
					try {
788
						updatedAdgroupId = UpdateAdGroup.runExample(name, status, bidAmount, Long.parseLong(adgroupListUpdate1));
789
					} catch (NumberFormatException e) {
790
						addActionError("Number Format Exception for AdGroupId (Google End).."+adgroupListUpdate1);
791
						logger.error("Number Format Exception for AdGroupId (Google End).."+adgroupListUpdate1, e);
792
					} catch (Exception e) {
793
						addActionError("AdGroup Service Mutate Operation Exception ");
794
						logger.error("Error while AdGroup Service Mutate Operation", e);
795
					}
796
					if(updatedAdgroupId>0l){
797
						adgroup.setAdgroupId(updatedAdgroupId);
798
						try {
799
							client.updateAdwordsAdGroup(adgroup);
800
							return true;
801
						} catch (GoogleAdwordsServiceException e) {
802
							addActionError("Unable to perform operations with Saholic Google Adowrds Services in updateAdwordsAdGroup Method while updating Data");
803
							logger.error("Error while performing operations with Saholic Google Adowrds Services in updateAdwordsAdGroup Method while updating Data", e); 
804
						} catch (TException e) {
805
							addActionError("Unable to perform operations with Saholic Google Adowrds Services Client in updateAdwordsAdGroup Method while updating Data");
806
							logger.error("Error while performing operations with Saholic Google Adowrds Services Client in updateAdwordsAdGroup Method while updating Data", e);
807
						}
808
					}
809
					else{
810
						addActionError("Error while processing Update Request for Google AdGroup Service");
811
					}
812
				}
813
 
814
			}
815
		}
816
		return false;
817
	}
818
 
819
	public boolean updateAdwordsAdKeyword(){
820
		AdwordsClient adwordsServiceClient = null;
821
		try {
822
			adwordsServiceClient = new AdwordsClient();
823
		} catch (TTransportException e) {
824
			e.printStackTrace();
825
		}
826
		if(adwordsServiceClient!=null){
827
			Client client = adwordsServiceClient.getClient();
828
			if(adgroupListUpdate2!=null && !("-1").equalsIgnoreCase(adgroupListUpdate2) && keywordListUpdate1!=null && ("-1").equalsIgnoreCase(keywordListUpdate1)){
829
				String text = "";
830
				String type = "";
831
				long bidAmount = -1l;
832
 
833
				AdwordsAdKeyword adkeyword = null;
834
				try {
835
					adkeyword = client.getAdwordsAdKeywordByCriterionIdAndAdGroupId(Long.parseLong(keywordListUpdate1), Long.parseLong(adgroupListUpdate2));
836
				} catch (NumberFormatException e) {
837
					addActionError("Number Format Exception for AdGroupCriterionId.."+keywordListUpdate1+"..AdGroupId.."+adgroupListUpdate2);
838
					logger.error("Number Format Exception for AdGroupCriterionId.."+keywordListUpdate1+"..AdGroupId.."+adgroupListUpdate2, e);
839
				} catch (GoogleAdwordsServiceException e) {
840
					addActionError("Unable to perform operations with Saholic Google Adowrds Services in updateAdwordsAdKeyword Method while getting Data");
841
					logger.error("Error while performing operations with Saholic Google Adowrds Services in updateAdwordsAdKeyword Method while getting Data", e);
842
				} catch (TException e) {
843
					addActionError("Unable to perform operations with Saholic Google Adowrds Services Client in updateAdwordsAdKeyword Method while getting Data");
844
					logger.error("Error while performing operations with Saholic Google Adowrds Services Client in updateAdwordsAdKeyword Method while getting Data", e);
845
				}
846
 
847
				if(adkeyword!=null){
848
					if(keywordUpdate!=null && !("").equalsIgnoreCase(keywordUpdate)){
849
						if(!adkeyword.getText().equalsIgnoreCase(keywordUpdate)){
850
							adkeyword.setText(keywordUpdate);
851
							text = keywordUpdate;
852
						}
853
					}
854
					if(bidUpdate!=null && !("").equalsIgnoreCase(bidUpdate)){
855
						if(adkeyword.getBidAmount()!=Double.parseDouble(bidUpdate)){
856
							adkeyword.setBidAmount(Double.parseDouble(bidUpdate));
857
							bidAmount = (long) (Double.parseDouble(bidUpdate) * Math.pow(10, 6));
858
						}
859
					}
860
					if(!adkeyword.getMatchType().toString().equalsIgnoreCase(KeywordMatchType.valueOf(matchTypeUpdate).toString())){
861
						adkeyword.setMatchType(KeywordMatchType.valueOf(matchTypeUpdate));
862
						type = KeywordMatchType.valueOf(matchTypeUpdate).toString();
863
					}
864
 
865
					Long updatedCriterionId = 0l;
866
					try {
867
						updatedCriterionId = UpdateKeyword.runExample(Long.parseLong(adgroupListUpdate2), Long.parseLong(keywordListUpdate1), text, bidAmount, type);
868
					} catch (NumberFormatException e) {
869
						addActionError("Number Format Exception for AdGroupCriterionId.."+keywordListUpdate1+"..AdGroupId.."+adgroupListUpdate2+ "(Google End)..");
870
						logger.error("Number Format Exception for AdGroupCriterionId.."+keywordListUpdate1+"..AdGroupId.."+adgroupListUpdate2+ "(Google End)..", e);
871
					} catch (Exception e) {
872
						addActionError("AdGroup Crierion Service Mutate Operation Exception ");
873
						logger.error("Error while AdGroup Crierion Service Mutate Operation", e);
874
					}
875
 
876
					if(updatedCriterionId>0l){
877
						adkeyword.setCriterionId(updatedCriterionId);
878
						try {
879
							client.updateAdwordsAdKeyword(adkeyword);
880
							return true;
881
						} catch (GoogleAdwordsServiceException e) {
882
							addActionError("Unable to perform operations with Saholic Google Adowrds Services in updateAdwordsAdKeyword Method while updating Data");
883
							logger.error("Error while performing operations with Saholic Google Adowrds Services in updateAdwordsAdKeyword Method while updating Data", e); 
884
						} catch (TException e) {
885
							addActionError("Unable to perform operations with Saholic Google Adowrds Services Client in updateAdwordsAdKeyword Method while updating Data");
886
							logger.error("Error while performing operations with Saholic Google Adowrds Services Client in updateAdwordsAdKeyword Method while updating Data", e);
887
						}
888
					}
889
					else{
890
						addActionError("Error while processing Update Request for Google AdGroupCriterion Service");
891
					}
892
 
893
				}
894
 
895
			}
896
		}
897
		return false;
898
	}
899
 
9067 manish.sha 900
	@Override
901
	public void setServletRequest(HttpServletRequest request) {
902
		this.request = request;
903
		this.session = request.getSession();
904
	}
905
 
906
	public String getCampaignId() {
907
		return campaignId;
908
	}
909
 
910
	public void setCampaignId(String campaignId) {
911
		this.campaignId = campaignId;
912
	}
913
 
914
	public String getErrorMsg(){
915
		return this.errorMsg;
916
	}
917
 
918
	public String getSuccessMessage(){
919
		return this.successmsg ;
920
	}
921
 
922
	public JSONObject getJsonData() {
923
		return jsonData;
924
	}
925
 
926
	public void setJsonData(JSONObject jsonData) {
927
		this.jsonData = jsonData;
928
	}
9230 manish.sha 929
 
930
	public JSONObject getJsonCampaignData() {
931
		return jsonCampaignData;
932
	}
9067 manish.sha 933
 
9230 manish.sha 934
	public void setJsonCampaignData(JSONObject jsonCampaignData) {
935
		this.jsonCampaignData = jsonCampaignData;
936
	}
937
 
938
	public JSONObject getJsonAdgroupData() {
939
		return jsonAdgroupData;
940
	}
941
 
942
	public void setJsonAdgroupData(JSONObject jsonAdgroupData) {
943
		this.jsonAdgroupData = jsonAdgroupData;
944
	}
945
 
946
	public String getKeywordId() {
947
		return keywordId;
948
	}
949
 
950
	public void setKeywordId(String keywordId) {
951
		this.keywordId = keywordId;
952
	}
953
 
954
	public String getAdgroupId() {
955
		return adgroupId;
956
	}
957
 
958
	public void setAdgroupId(String adgroupId) {
959
		this.adgroupId = adgroupId;
960
	}
961
 
962
	public JSONObject getJsonKeywordList() {
963
		return jsonKeywordList;
964
	}
965
 
966
	public void setJsonKeywordList(JSONObject jsonKeywordList) {
967
		this.jsonKeywordList = jsonKeywordList;
968
	}
969
 
970
	public JSONObject getJsonKeywordData() {
971
		return jsonKeywordData;
972
	}
973
 
974
	public void setJsonKeywordData(JSONObject jsonKeywordData) {
975
		this.jsonKeywordData = jsonKeywordData;
976
	}
977
 
978
	public String getTextAdItemPrice(){
979
		String itemPrice = "";
980
		List<Item> itemList = new ArrayList<Item>();
981
		try {
982
			itemList = new CatalogClient().getClient().getItemsByCatalogId(Long.parseLong(catalogItemId));
983
		} catch (NumberFormatException e) {
984
			e.printStackTrace();
985
		} catch (TTransportException e) {
986
			e.printStackTrace();
987
		} catch (CatalogServiceException e) {
988
			e.printStackTrace();
989
		} catch (TException e) {
990
			e.printStackTrace();
991
		}
9284 manish.sha 992
		List<Long> itemPriceList = new ArrayList<Long>();
9230 manish.sha 993
		for(Item item : itemList){
9284 manish.sha 994
			itemPriceList.add((long)item.getSellingPrice());
9230 manish.sha 995
		}
9284 manish.sha 996
		if(itemPriceList!=null && itemPriceList.size()>0){
997
			Collections.sort(itemPriceList);
998
			itemPrice = itemPriceList.get(0).longValue()+"";
999
		}
9351 manish.sha 1000
 
9230 manish.sha 1001
		return itemPrice;
1002
	}
9067 manish.sha 1003
	public String getAdgroupList(){
1004
		List<Map<String,String>> adgroupDataList = new ArrayList<Map<String,String>>();
1005
		Map<String, List<Map<String,String>>> mainMap = new HashMap<String, List<Map<String,String>>>();
1006
		List<AdwordsAdGroup> adgroupList =null;
1007
		try {
1008
			adgroupList = new AdwordsClient().getClient().getAdwordsAdGroupsByCampaignId(Long.parseLong(campaignId));
1009
		} catch (TTransportException e) {
1010
			e.printStackTrace();
1011
		} catch (GoogleAdwordsServiceException e) {
1012
			e.printStackTrace();
1013
		} catch (TException e) {
1014
			e.printStackTrace();
1015
		}
1016
		for(int i=0 ; i<adgroupList.size() ; i++){
1017
			AdwordsAdGroup adgroup = adgroupList.get(i);
1018
			Map<String,String> adgroupDataMap = new HashMap<String,String>();
1019
			adgroupDataMap.put("AdgroupId",adgroup.getAdgroupId()+"");
1020
			adgroupDataMap.put("Name", adgroup.getName());
1021
			adgroupDataList.add(adgroupDataMap);
1022
		}
1023
		mainMap.put("AdGroupData", adgroupDataList);
1024
		jsonData = new JSONObject(mainMap);
9351 manish.sha 1025
 
9067 manish.sha 1026
		return "output";
1027
	}
9230 manish.sha 1028
 
1029
	public String getKeywordList(){
1030
		List<Map<String,String>> adkeywordDataList = new ArrayList<Map<String,String>>();
1031
		Map<String, List<Map<String,String>>> mainMap = new HashMap<String, List<Map<String,String>>>();
1032
		List<AdwordsAdKeyword> adkeywordList = null;
1033
		try {
1034
			adkeywordList = new AdwordsClient().getClient().getAdwordsAdKeywordsByAdgroupId(Long.parseLong(adgroupId));
1035
		}
1036
		catch (TTransportException e) {
1037
			e.printStackTrace();
1038
		} catch (GoogleAdwordsServiceException e) {
1039
			e.printStackTrace();
1040
		} catch (TException e) {
1041
			e.printStackTrace();
1042
		}
1043
 
1044
		for(AdwordsAdKeyword adkeyword : adkeywordList){
1045
			Map<String,String> adkeywordDataMap = new HashMap<String,String>();
1046
			adkeywordDataMap.put("KeywordId", adkeyword.getCriterionId()+"");
1047
			adkeywordDataMap.put("Text", adkeyword.getText());
1048
			adkeywordDataList.add(adkeywordDataMap);
1049
		}
1050
		mainMap.put("AdKeywordData", adkeywordDataList);
1051
		jsonKeywordList = new JSONObject(mainMap);
1052
		return "output";
1053
	}
1054
 
1055
	public String getCampaignDetails(){
1056
		Map<String, String> campaignUnitDataMap = new HashMap<String, String>();
1057
		AdwordsCampaign campaign = null;
1058
		try {
1059
			campaign = new AdwordsClient().getClient().getAdwordsCampaignByCampaignId(Long.parseLong(campaignId));
1060
		} catch (NumberFormatException e) {
1061
			e.printStackTrace();
1062
		} catch (TTransportException e) {
1063
			e.printStackTrace();
1064
		} catch (GoogleAdwordsServiceException e) {
1065
			e.printStackTrace();
1066
		} catch (TException e) {
1067
			e.printStackTrace();
1068
		}
1069
 
1070
		if(campaign!=null){
1071
			campaignUnitDataMap.put("Name", campaign.getName());
1072
			campaignUnitDataMap.put("BudgetAmount", campaign.getBudgetAmount()+"");
1073
			campaignUnitDataMap.put("Status", campaign.getStatus().getValue()+"");
1074
		}
1075
		jsonCampaignData = new JSONObject(campaignUnitDataMap);
1076
		return "output";
1077
	}
1078
 
1079
	public String getAdGroupDetails(){
1080
		Map<String, String> adgroupUnitDataMap = new HashMap<String, String>();
1081
		AdwordsAdGroup adGroup = null;
1082
		try {
1083
			adGroup = new AdwordsClient().getClient().getAdwordsAdGroupByAdGroupId(Long.parseLong(adgroupId));
1084
		} catch (NumberFormatException e) {
1085
			e.printStackTrace();
1086
		} catch (TTransportException e) {
1087
			e.printStackTrace();
1088
		} catch (GoogleAdwordsServiceException e) {
1089
			e.printStackTrace();
1090
		} catch (TException e) {
1091
			e.printStackTrace();
1092
		}
1093
 
1094
		if(adGroup!=null){
1095
			adgroupUnitDataMap.put("Name", adGroup.getName());
1096
			adgroupUnitDataMap.put("BidAmount", adGroup.getBidAmount()+"");
1097
			adgroupUnitDataMap.put("Status", adGroup.getStatus().getValue()+"");
1098
			adgroupUnitDataMap.put("CatalogItemId", adGroup.getCatalogItemId()+"");
1099
		}
1100
		jsonAdgroupData = new JSONObject(adgroupUnitDataMap);
1101
		return "output";
1102
	}
1103
 
1104
	public String getKeywordDetails(){
1105
		Map<String, String> adkeywordUnitDataMap = new HashMap<String, String>();
1106
		AdwordsAdKeyword adKeyword = null;
1107
		try {
1108
			System.out.println("keywordId" +keywordId);
1109
			System.out.println("adgroupId" +adgroupId);
1110
			adKeyword = new AdwordsClient().getClient().getAdwordsAdKeywordByCriterionIdAndAdGroupId(Long.parseLong(keywordId), Long.parseLong(adgroupId));
1111
		} catch (NumberFormatException e) {
1112
			e.printStackTrace();
1113
		} catch (TTransportException e) {
1114
			e.printStackTrace();
1115
		} catch (GoogleAdwordsServiceException e) {
1116
			e.printStackTrace();
1117
		} catch (TException e) {
1118
			e.printStackTrace();
1119
		}
1120
 
1121
		if(adKeyword!=null){
1122
			adkeywordUnitDataMap.put("Text", adKeyword.getText());
1123
			adkeywordUnitDataMap.put("Bid", adKeyword.getBidAmount()+"");
1124
			adkeywordUnitDataMap.put("MatchType", adKeyword.getMatchType().getValue()+"");
1125
		}
1126
		jsonKeywordData = new JSONObject(adkeywordUnitDataMap);
1127
		return "output";
1128
	}
9067 manish.sha 1129
 
1130
	public List<AdwordsCampaign> getCampaigns(){
1131
		List<AdwordsCampaign> campaignList =null;
1132
		try {
1133
			campaignList = new AdwordsClient().getClient().getAllAdwordsCampaigns();
1134
		} catch (TTransportException e) {
1135
			e.printStackTrace();
1136
		} catch (GoogleAdwordsServiceException e) {
1137
			e.printStackTrace();
1138
		} catch (TException e) {
1139
			e.printStackTrace();
1140
		}
1141
		return campaignList;
1142
	}
1143
 
1144
	private boolean checkForErrors(){
1145
		Collection<String> actionErrors = getActionErrors();
1146
		if(actionErrors != null && !actionErrors.isEmpty()){
1147
			for (String str : actionErrors) {
1148
				errorMsg += "<BR/>" + str;
1149
			}
1150
			return true;
1151
		}
1152
		return false;
1153
	}
1154
 
1155
	public String getAdwordOpRadio() {
1156
		return adwordOpRadio;
1157
	}
1158
 
1159
	public void setAdwordOpRadio(String adwordOpRadio) {
1160
		this.adwordOpRadio = adwordOpRadio;
1161
	}
1162
 
1163
	public String getAdwordAddRadio() {
1164
		return adwordAddRadio;
1165
	}
1166
 
1167
	public void setAdwordAddRadio(String adwordAddRadio) {
1168
		this.adwordAddRadio = adwordAddRadio;
1169
	}
1170
 
1171
	public String getCampaignName() {
1172
		return campaignName;
1173
	}
1174
 
1175
	public void setCampaignName(String campaignName) {
1176
		this.campaignName = campaignName;
1177
	}
1178
 
1179
	public String getCampaignStatus() {
1180
		return campaignStatus;
1181
	}
1182
 
1183
	public void setCampaignStatus(String campaignStatus) {
1184
		this.campaignStatus = campaignStatus;
1185
	}
1186
 
1187
	public String getStartDate() {
1188
		return startDate;
1189
	}
1190
 
1191
	public void setStartDate(String startDate) {
1192
		this.startDate = startDate;
1193
	}
1194
 
1195
	public String getEndDate() {
1196
		return endDate;
1197
	}
1198
 
1199
	public void setEndDate(String endDate) {
1200
		this.endDate = endDate;
1201
	}
1202
 
1203
	public String getCampaignAmount() {
1204
		return campaignAmount;
1205
	}
1206
 
1207
	public void setCampaignAmount(String campaignAmount) {
1208
		this.campaignAmount = campaignAmount;
1209
	}
1210
 
1211
	public String getGoogleSearchNetwork() {
1212
		return googleSearchNetwork;
1213
	}
1214
 
1215
	public void setGoogleSearchNetwork(String googleSearchNetwork) {
1216
		this.googleSearchNetwork = googleSearchNetwork;
1217
	}
1218
 
1219
	public String getDisplayNetwork() {
1220
		return displayNetwork;
1221
	}
1222
 
1223
	public void setDisplayNetwork(String displayNetwork) {
1224
		this.displayNetwork = displayNetwork;
1225
	}
1226
 
1227
	public String getPartnerSearchNetwork() {
1228
		return partnerSearchNetwork;
1229
	}
1230
 
1231
	public void setPartnerSearchNetwork(String partnerSearchNetwork) {
1232
		this.partnerSearchNetwork = partnerSearchNetwork;
1233
	}
1234
 
1235
	public String getCampaignList1() {
1236
		return campaignList1;
1237
	}
1238
 
1239
	public void setCampaignList1(String campaignList1) {
1240
		this.campaignList1 = campaignList1;
1241
	}
1242
 
1243
	public String getAdgroupName() {
1244
		return adgroupName;
1245
	}
1246
 
1247
	public void setAdgroupName(String adgroupName) {
1248
		this.adgroupName = adgroupName;
1249
	}
1250
 
1251
	public String getAdgroupStatus() {
1252
		return adgroupStatus;
1253
	}
1254
 
1255
	public void setAdgroupStatus(String adgroupStatus) {
1256
		this.adgroupStatus = adgroupStatus;
1257
	}
1258
 
1259
	public String getCatalogItemId() {
1260
		return catalogItemId;
1261
	}
1262
 
1263
	public void setCatalogItemId(String catalogItemId) {
1264
		this.catalogItemId = catalogItemId;
1265
	}
1266
 
1267
	public String getBidAmount() {
1268
		return bidAmount;
1269
	}
1270
 
1271
	public void setBidAmount(String bidAmount) {
1272
		this.bidAmount = bidAmount;
1273
	}
1274
 
1275
	public String getCampaignList2() {
1276
		return campaignList2;
1277
	}
1278
 
1279
	public void setCampaignList2(String campaignList2) {
1280
		this.campaignList2 = campaignList2;
1281
	}
1282
 
1283
	public String getAdgroupList1() {
1284
		return adgroupList1;
1285
	}
1286
 
1287
	public void setAdgroupList1(String adgroupList1) {
1288
		this.adgroupList1 = adgroupList1;
1289
	}
1290
 
1291
	public String getHeadline() {
1292
		return headline;
1293
	}
1294
 
1295
	public void setHeadline(String headline) {
1296
		this.headline = headline;
1297
	}
1298
 
1299
	public String getDescription1() {
1300
		return description1;
1301
	}
1302
 
1303
	public void setDescription1(String description1) {
1304
		this.description1 = description1;
1305
	}
1306
 
1307
	public String getDescription2() {
1308
		return description2;
1309
	}
1310
 
1311
	public void setDescription2(String description2) {
1312
		this.description2 = description2;
1313
	}
1314
 
1315
	public String getUrl() {
1316
		return url;
1317
	}
1318
 
1319
	public void setUrl(String url) {
1320
		this.url = url;
1321
	}
1322
 
1323
	public String getDisplayUrl() {
1324
		return displayUrl;
1325
	}
1326
 
1327
	public void setDisplayUrl(String displayUrl) {
1328
		this.displayUrl = displayUrl;
1329
	}
1330
 
1331
	public String getCampaignList3() {
1332
		return campaignList3;
1333
	}
1334
 
1335
	public void setCampaignList3(String campaignList3) {
1336
		this.campaignList3 = campaignList3;
1337
	}
1338
 
1339
	public String getAdgroupList2() {
1340
		return adgroupList2;
1341
	}
1342
 
1343
	public void setAdgroupList2(String adgroupList2) {
1344
		this.adgroupList2 = adgroupList2;
1345
	}
1346
 
1347
	public String getKeyword1() {
1348
		return keyword1;
1349
	}
1350
 
1351
	public void setKeyword1(String keyword1) {
1352
		this.keyword1 = keyword1;
1353
	}
1354
 
1355
	public String getKeyword2() {
1356
		return keyword2;
1357
	}
1358
 
1359
	public void setKeyword2(String keyword2) {
1360
		this.keyword2 = keyword2;
1361
	}
1362
 
1363
	public String getKeyword3() {
1364
		return keyword3;
1365
	}
1366
 
1367
	public void setKeyword3(String keyword3) {
1368
		this.keyword3 = keyword3;
1369
	}
1370
 
1371
	public String getKeyword4() {
1372
		return keyword4;
1373
	}
1374
 
1375
	public void setKeyword4(String keyword4) {
1376
		this.keyword4 = keyword4;
1377
	}
1378
 
1379
	public String getKeyword5() {
1380
		return keyword5;
1381
	}
1382
 
1383
	public void setKeyword5(String keyword5) {
1384
		this.keyword5 = keyword5;
1385
	}
1386
 
1387
	public String getBid1() {
1388
		return bid1;
1389
	}
1390
 
1391
	public void setBid1(String bid1) {
1392
		this.bid1 = bid1;
1393
	}
1394
 
1395
	public String getBid2() {
1396
		return bid2;
1397
	}
1398
 
1399
	public void setBid2(String bid2) {
1400
		this.bid2 = bid2;
1401
	}
1402
 
1403
	public String getBid3() {
1404
		return bid3;
1405
	}
1406
 
1407
	public void setBid3(String bid3) {
1408
		this.bid3 = bid3;
1409
	}
1410
 
1411
	public String getBid4() {
1412
		return bid4;
1413
	}
1414
 
1415
	public void setBid4(String bid4) {
1416
		this.bid4 = bid4;
1417
	}
1418
 
1419
	public String getBid5() {
1420
		return bid5;
1421
	}
1422
 
1423
	public void setBid5(String bid5) {
1424
		this.bid5 = bid5;
1425
	}
1426
 
1427
	public String getMatchType1() {
1428
		return matchType1;
1429
	}
1430
 
1431
	public void setMatchType1(String matchType1) {
1432
		this.matchType1 = matchType1;
1433
	}
1434
 
1435
	public String getMatchType2() {
1436
		return matchType2;
1437
	}
1438
 
1439
	public void setMatchType2(String matchType2) {
1440
		this.matchType2 = matchType2;
1441
	}
1442
 
1443
	public String getMatchType3() {
1444
		return matchType3;
1445
	}
1446
 
1447
	public void setMatchType3(String matchType3) {
1448
		this.matchType3 = matchType3;
1449
	}
1450
 
1451
	public String getMatchType4() {
1452
		return matchType4;
1453
	}
1454
 
1455
	public void setMatchType4(String matchType4) {
1456
		this.matchType4 = matchType4;
1457
	}
1458
 
1459
	public String getMatchType5() {
1460
		return matchType5;
1461
	}
1462
 
1463
	public void setMatchType5(String matchType5) {
1464
		this.matchType5 = matchType5;
1465
	}
1466
 
1467
	public String getStockLinked() {
1468
		return stockLinked;
1469
	}
1470
 
1471
	public void setStockLinked(String stockLinked) {
1472
		this.stockLinked = stockLinked;
1473
	}
1474
 
9230 manish.sha 1475
	public String getAdwordUpdateRadio() {
1476
		return adwordUpdateRadio;
1477
	}
1478
 
1479
	public void setAdwordUpdateRadio(String adwordUpdateRadio) {
1480
		this.adwordUpdateRadio = adwordUpdateRadio;
1481
	}
1482
 
1483
	public String getCampaignListUpdate1() {
1484
		return campaignListUpdate1;
1485
	}
1486
 
1487
	public void setCampaignListUpdate1(String campaignListUpdate1) {
1488
		this.campaignListUpdate1 = campaignListUpdate1;
1489
	}
1490
 
1491
	public String getCampaignNameUpdate() {
1492
		return campaignNameUpdate;
1493
	}
1494
 
1495
	public void setCampaignNameUpdate(String campaignNameUpdate) {
1496
		this.campaignNameUpdate = campaignNameUpdate;
1497
	}
1498
 
1499
	public String getCampaignStatusUpdate() {
1500
		return campaignStatusUpdate;
1501
	}
1502
 
1503
	public void setCampaignStatusUpdate(String campaignStatusUpdate) {
1504
		this.campaignStatusUpdate = campaignStatusUpdate;
1505
	}
1506
 
1507
	public String getCampaignAmountUpdate() {
1508
		return campaignAmountUpdate;
1509
	}
1510
 
1511
	public void setCampaignAmountUpdate(String campaignAmountUpdate) {
1512
		this.campaignAmountUpdate = campaignAmountUpdate;
1513
	}
1514
 
1515
	public String getCampaignListUpdate2() {
1516
		return campaignListUpdate2;
1517
	}
1518
 
1519
	public void setCampaignListUpdate2(String campaignListUpdate2) {
1520
		this.campaignListUpdate2 = campaignListUpdate2;
1521
	}
1522
 
1523
	public String getAdgroupListUpdate1() {
1524
		return adgroupListUpdate1;
1525
	}
1526
 
1527
	public void setAdgroupListUpdate1(String adgroupListUpdate1) {
1528
		this.adgroupListUpdate1 = adgroupListUpdate1;
1529
	}
1530
 
1531
	public String getAdgroupNameUpdate() {
1532
		return adgroupNameUpdate;
1533
	}
1534
 
1535
	public void setAdgroupNameUpdate(String adgroupNameUpdate) {
1536
		this.adgroupNameUpdate = adgroupNameUpdate;
1537
	}
1538
 
1539
	public String getAdgroupStatusUpdate() {
1540
		return adgroupStatusUpdate;
1541
	}
1542
 
1543
	public void setAdgroupStatusUpdate(String adgroupStatusUpdate) {
1544
		this.adgroupStatusUpdate = adgroupStatusUpdate;
1545
	}
1546
 
1547
	public String getCatalogItemIdUpdate() {
1548
		return catalogItemIdUpdate;
1549
	}
1550
 
1551
	public void setCatalogItemIdUpdate(String catalogItemIdUpdate) {
1552
		this.catalogItemIdUpdate = catalogItemIdUpdate;
1553
	}
1554
 
1555
	public String getBidAmountUpdate() {
1556
		return bidAmountUpdate;
1557
	}
1558
 
1559
	public void setBidAmountUpdate(String bidAmountUpdate) {
1560
		this.bidAmountUpdate = bidAmountUpdate;
1561
	}
1562
 
1563
	public String getCampaignListUpdate3() {
1564
		return campaignListUpdate3;
1565
	}
1566
 
1567
	public void setCampaignListUpdate3(String campaignListUpdate3) {
1568
		this.campaignListUpdate3 = campaignListUpdate3;
1569
	}
1570
 
1571
	public String getAdgroupListUpdate2() {
1572
		return adgroupListUpdate2;
1573
	}
1574
 
1575
	public void setAdgroupListUpdate2(String adgroupListUpdate2) {
1576
		this.adgroupListUpdate2 = adgroupListUpdate2;
1577
	}
1578
 
1579
	public String getKeywordUpdate() {
1580
		return keywordUpdate;
1581
	}
1582
 
1583
	public void setKeywordUpdate(String keywordUpdate) {
1584
		this.keywordUpdate = keywordUpdate;
1585
	}
1586
 
1587
	public String getBidUpdate() {
1588
		return bidUpdate;
1589
	}
1590
 
1591
	public void setBidUpdate(String bidUpdate) {
1592
		this.bidUpdate = bidUpdate;
1593
	}
1594
 
1595
	public String getMatchTypeUpdate() {
1596
		return matchTypeUpdate;
1597
	}
1598
 
1599
	public void setMatchTypeUpdate(String matchTypeUpdate) {
1600
		this.matchTypeUpdate = matchTypeUpdate;
1601
	}
1602
 
1603
	public String getKeywordListUpdate1() {
1604
		return keywordListUpdate1;
1605
	}
1606
 
1607
	public void setKeywordListUpdate1(String keywordListUpdate1) {
1608
		this.keywordListUpdate1 = keywordListUpdate1;
1609
	}
9351 manish.sha 1610
 
1611
	public String getCataLogItemId() {
1612
		return cataLogItemId;
1613
	}
9230 manish.sha 1614
 
9351 manish.sha 1615
	public void setCataLogItemId(String cataLogItemId) {
1616
		this.cataLogItemId = cataLogItemId;
1617
	}
1618
 
1619
	public String getSentFeedResult() {
1620
		return sentFeedResult;
1621
	}
1622
 
1623
	public void setSentFeedResult(String sentFeedResult) {
1624
		this.sentFeedResult = sentFeedResult;
1625
	}
1626
 
9067 manish.sha 1627
}