Subversion Repositories SmartDukaan

Rev

Rev 9018 | Go to most recent revision | 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.AdGroup;
24
import com.google.api.ads.adwords.axis.v201309.cm.AdGroupPage;
25
import com.google.api.ads.adwords.axis.v201309.cm.AdGroupServiceInterface;
26
import com.google.api.ads.adwords.axis.v201309.cm.Bid;
27
import com.google.api.ads.adwords.axis.v201309.cm.Bids;
28
import com.google.api.ads.adwords.axis.v201309.cm.CpcBid;
29
import com.google.api.ads.adwords.axis.v201309.cm.OrderBy;
30
import com.google.api.ads.adwords.axis.v201309.cm.Paging;
31
import com.google.api.ads.adwords.axis.v201309.cm.Predicate;
32
import com.google.api.ads.adwords.axis.v201309.cm.PredicateOperator;
33
import com.google.api.ads.adwords.axis.v201309.cm.Selector;
34
import com.google.api.ads.adwords.axis.v201309.cm.SortOrder;
35
import com.google.api.ads.adwords.lib.client.AdWordsSession;
36
import com.google.api.ads.common.lib.auth.OfflineCredentials;
37
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
38
import com.google.api.client.auth.oauth2.Credential;
39
 
40
/**
41
 * This example gets all ad groups in a campaign. To add an ad group, run
42
 * AddAdGroup.java. To get campaigns, run GetCampaigns.java.
43
 *
44
 * Credentials and properties in {@code fromFile()} are pulled from the
45
 * "ads.properties" file. See README for more info.
46
 *
47
 * Tags: AdGroupService.get
48
 *
49
 * @author Kevin Winter
50
 */
51
public class GetAdGroups {
52
 
53
	private static final int PAGE_SIZE = 100;
54
 
55
	public static List<Map<String,String>> runExample(Long campaignId) throws Exception {
56
		Credential oAuth2Credential = new OfflineCredentials.Builder()
57
		.forApi(Api.ADWORDS)
58
		.fromFile()
59
		.build()
60
		.generateCredential();
61
 
62
		// Construct an AdWordsSession.
63
		AdWordsSession session = new AdWordsSession.Builder()
64
		.fromFile()
65
		.withOAuth2Credential(oAuth2Credential)
66
		.build();
67
 
68
		AdWordsServices adWordsServices = new AdWordsServices();
69
 
70
		// Get the AdGroupService.
71
		AdGroupServiceInterface adGroupService =
72
			adWordsServices.get(session, AdGroupServiceInterface.class);
73
 
74
		int offset = 0;
75
		boolean morePages = true;
76
 
77
		// Create selector.
78
		Selector selector = new Selector();
79
		selector.setFields(new String[] {"Id", "Name", "CampaignId", "CpcBid" , "Status"});
80
		selector.setOrdering(new OrderBy[] {new OrderBy("Name", SortOrder.ASCENDING)});
81
		selector.setPaging(new Paging(offset, PAGE_SIZE));
82
 
83
		// Create predicates.
84
		Predicate campaignIdPredicate =
85
			new Predicate("CampaignId", PredicateOperator.IN, new String[] {campaignId.toString()});
86
		selector.setPredicates(new Predicate[] {campaignIdPredicate});
87
		List<Map<String,String>> adgroupDataMainList = new ArrayList<Map<String,String>>();
88
		while (morePages) {
89
			// Get all ad groups.
90
			AdGroupPage page = adGroupService.get(selector);			
91
			// Display ad groups.
92
			if (page.getEntries() != null) {
93
				for (AdGroup adGroup : page.getEntries()) {
94
					System.out.println("Ad group with name \"" + adGroup.getName() + "\" and id \""
95
							+ adGroup.getId() + "\" was found.");
96
					if(adGroup.getBiddingStrategyConfiguration()!=null){
97
						Bids bid = adGroup.getBiddingStrategyConfiguration().getBids(0);
98
						if(("CpcBid").equalsIgnoreCase(bid.getBidsType())){
99
							CpcBid new_bid = (CpcBid)bid;
100
							Map<String,String> adgroupMap = new HashMap<String,String>();
101
							adgroupMap.put("CampaignId", adGroup.getCampaignId()+"");
102
							adgroupMap.put("AdgroupId", adGroup.getId()+"");
103
							adgroupMap.put("Name", adGroup.getName());
104
							adgroupMap.put("Status", adGroup.getStatus().getValue());
105
							adgroupMap.put("BidAmount", new_bid.getBid().getMicroAmount()+"");
106
							adgroupMap.put("CatalogItemId", "0");
107
							adgroupDataMainList.add(adgroupMap);
108
						}						
109
					}
110
				}
111
			} else {
112
				System.out.println("No ad groups were found.");
113
			}
114
 
115
			offset += PAGE_SIZE;
116
			selector.getPaging().setStartIndex(offset);
117
			morePages = offset < page.getTotalNumEntries();
118
		}
119
		return adgroupDataMainList;
120
	}
121
}