Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
9005 manish.sha 1
// Copyright 2012 Google Inc. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
 
15
package adwords.axis.v201309.basicoperations;
16
 
17
import java.util.ArrayList;
18
import java.util.HashMap;
19
import java.util.List;
20
import java.util.Map;
21
 
22
import com.google.api.ads.adwords.axis.factory.AdWordsServices;
23
import com.google.api.ads.adwords.axis.v201309.cm.AdGroupAd;
24
import com.google.api.ads.adwords.axis.v201309.cm.AdGroupAdPage;
25
import com.google.api.ads.adwords.axis.v201309.cm.AdGroupAdServiceInterface;
26
import com.google.api.ads.adwords.axis.v201309.cm.OrderBy;
27
import com.google.api.ads.adwords.axis.v201309.cm.Paging;
28
import com.google.api.ads.adwords.axis.v201309.cm.Predicate;
29
import com.google.api.ads.adwords.axis.v201309.cm.PredicateOperator;
30
import com.google.api.ads.adwords.axis.v201309.cm.Selector;
31
import com.google.api.ads.adwords.axis.v201309.cm.SortOrder;
32
import com.google.api.ads.adwords.axis.v201309.cm.TextAd;
33
import com.google.api.ads.adwords.lib.client.AdWordsSession;
34
import com.google.api.ads.common.lib.auth.OfflineCredentials;
35
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
36
import com.google.api.client.auth.oauth2.Credential;
37
 
38
/**
39
 * This example gets all text ads for a given ad group. To add an ad, run
40
 * AddTextAds.java.
41
 *
42
 * Credentials and properties in {@code fromFile()} are pulled from the
43
 * "ads.properties" file. See README for more info.
44
 *
45
 * Tags: AdGroupAdService.get
46
 *
47
 * Category: adx-exclude
48
 *
49
 * @author Kevin Winter
50
 */
51
public class GetTextAds {
52
 
53
	private static final int PAGE_SIZE = 100;
54
 
55
	/*public static void main(String[] args) throws Exception {
56
 
57
 
58
		runExample(adWordsServices, session, adGroupId);
59
	}*/
60
 
61
	public static List<Map<String, String>>  runExample(Long adGroupId) throws Exception {
62
		// Generate a refreshable OAuth2 credential similar to a ClientLogin token
63
		// and can be used in place of a service account.
64
		Credential oAuth2Credential = new OfflineCredentials.Builder()
65
		.forApi(Api.ADWORDS)
66
		.fromFile()
67
		.build()
68
		.generateCredential();
69
 
70
		// Construct an AdWordsSession.
71
		AdWordsSession session = new AdWordsSession.Builder()
72
		.fromFile()
73
		.withOAuth2Credential(oAuth2Credential)
74
		.build();
75
 
76
		AdWordsServices adWordsServices = new AdWordsServices();
77
		// Get the AdGroupAdService.
78
		AdGroupAdServiceInterface adGroupAdService =
79
			adWordsServices.get(session, AdGroupAdServiceInterface.class);
80
 
81
		int offset = 0;
82
		boolean morePages = true;
83
 
84
		// Create selector.
85
		Selector selector = new Selector();
86
		selector.setFields(new String[] {"Id", "AdGroupId", "Headline", "Description1", "Description2", "DisplayUrl", "Url"});
87
		selector.setOrdering(new OrderBy[] {new OrderBy("Id", SortOrder.ASCENDING)});
88
		selector.setPaging(new Paging(offset, PAGE_SIZE));
89
 
90
		// Create predicates.
91
		Predicate adGroupIdPredicate =
92
			new Predicate("AdGroupId", PredicateOperator.IN, new String[] {adGroupId.toString()});
93
		// By default disabled ads aren't returned by the selector. To return them
94
		// include the DISABLED status in a predicate.
95
		Predicate statusPredicate =
96
			new Predicate("Status", PredicateOperator.IN,
97
					new String[] {"ENABLED", "PAUSED", "DISABLED"});
98
		Predicate adTypePredicate =
99
			new Predicate("AdType", PredicateOperator.EQUALS, new String[] {"TEXT_AD"});
100
		selector.setPredicates(new Predicate[] {adGroupIdPredicate, statusPredicate, adTypePredicate});
101
		List<Map<String, String>> adgroupAdMainDataList = new ArrayList<Map<String,String>>();
102
 
103
		while (morePages) {
104
			// Get all ads.
105
			AdGroupAdPage page = adGroupAdService.get(selector);
106
 
107
			// Display ads.
108
			if (page.getEntries() != null && page.getEntries().length > 0) {
109
				for (AdGroupAd adGroupAd : page.getEntries()) {
110
					System.out.println("Ad with id  \"" + adGroupAd.getAd().getId() + "\"" + " and type \""
111
							+ adGroupAd.getAd().getAdType() + "\" was found.");
112
					if(("TextAd").equalsIgnoreCase(adGroupAd.getAd().getAdType())){
113
						TextAd ad = (TextAd) adGroupAd.getAd();
114
						Map<String,String> adgroupAdMap = new HashMap<String,String>();
115
						adgroupAdMap.put("AdgroupId", adGroupAd.getAdGroupId()+"");
116
						adgroupAdMap.put("AdgroupAdId", adGroupAd.getAd().getId()+"");
117
						adgroupAdMap.put("Headline", ad.getHeadline());
118
						adgroupAdMap.put("DisplayUrl", ad.getDisplayUrl());
119
						adgroupAdMap.put("Descrption1", ad.getDescription1());
120
						adgroupAdMap.put("Descrption2", ad.getDescription2());
121
						adgroupAdMap.put("Url", ad.getUrl());
122
						adgroupAdMainDataList.add(adgroupAdMap);
123
					}
124
				}
125
			} else {
126
				System.out.println("No ads were found.");
127
			}
128
 
129
			offset += PAGE_SIZE;
130
			selector.getPaging().setStartIndex(offset);
131
			morePages = offset < page.getTotalNumEntries();
132
		}
133
		return adgroupAdMainDataList;
134
	}
135
}