Subversion Repositories SmartDukaan

Rev

Rev 9302 | Details | Compare with Previous | 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
 
9306 manish.sha 17
import java.rmi.RemoteException;
9005 manish.sha 18
import java.util.ArrayList;
19
import java.util.HashMap;
20
import java.util.List;
21
import java.util.Map;
22
 
23
import com.google.api.ads.adwords.axis.factory.AdWordsServices;
9306 manish.sha 24
import com.google.api.ads.adwords.axis.v201309.cm.ApiException;
9005 manish.sha 25
import com.google.api.ads.adwords.axis.v201309.cm.Campaign;
26
import com.google.api.ads.adwords.axis.v201309.cm.CampaignPage;
27
import com.google.api.ads.adwords.axis.v201309.cm.CampaignServiceInterface;
28
import com.google.api.ads.adwords.axis.v201309.cm.OrderBy;
29
import com.google.api.ads.adwords.axis.v201309.cm.Paging;
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.lib.client.AdWordsSession;
33
import com.google.api.ads.common.lib.auth.OfflineCredentials;
34
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
9306 manish.sha 35
import com.google.api.ads.common.lib.conf.ConfigurationLoadException;
36
import com.google.api.ads.common.lib.exception.OAuthException;
37
import com.google.api.ads.common.lib.exception.ValidationException;
9005 manish.sha 38
import com.google.api.client.auth.oauth2.Credential;
39
 
40
/**
41
 * This example gets all campaigns. To add a campaign, run AddCampaign.java.
42
 *
43
 * Credentials and properties in {@code fromFile()} are pulled from the
44
 * "ads.properties" file. See README for more info.
45
 *
46
 * Tags: CampaignService.get
47
 *
48
 * @author Adam Rogal
49
 */
50
public class GetCampaigns {
51
 
52
	private static final int PAGE_SIZE = 100;
53
 
54
	public static void main(String args[]){
55
		try{
56
			runExample();
57
		}
58
		catch(Exception e){
59
			System.out.println(e.getMessage());
60
		}
61
	}
62
 
9306 manish.sha 63
	public static List<Map<String,String>> runExample() {
9005 manish.sha 64
		// Generate a refreshable OAuth2 credential similar to a ClientLogin token
65
		// and can be used in place of a service account.
9306 manish.sha 66
		Credential oAuth2Credential = null;
67
		try {
68
			oAuth2Credential = new OfflineCredentials.Builder()
69
			.forApi(Api.ADWORDS)
70
			.fromFile()
71
			.build()
72
			.generateCredential();
73
		} catch (OAuthException e) {
74
			// TODO Auto-generated catch block
75
			e.printStackTrace();
76
		} catch (ValidationException e) {
77
			// TODO Auto-generated catch block
78
			e.printStackTrace();
79
		} catch (ConfigurationLoadException e) {
80
			// TODO Auto-generated catch block
81
			e.printStackTrace();
82
		}
9005 manish.sha 83
 
84
		// Construct an AdWordsSession.
9306 manish.sha 85
		AdWordsSession session= null;
86
		try {
87
			session = new AdWordsSession.Builder()
88
			.fromFile()
89
			.withOAuth2Credential(oAuth2Credential)
90
			.build();
91
		} catch (ValidationException e) {
92
			// TODO Auto-generated catch block
93
			e.printStackTrace();
94
		} catch (ConfigurationLoadException e) {
95
			// TODO Auto-generated catch block
96
			e.printStackTrace();
97
		}
9005 manish.sha 98
 
99
		AdWordsServices adWordsServices = new AdWordsServices();  
100
 
101
		// Get the CampaignService.
102
		CampaignServiceInterface campaignService =
103
			adWordsServices.get(session, CampaignServiceInterface.class);
104
 
105
		int offset = 0;
106
 
107
		// Create selector.
108
		Selector selector = new Selector();
109
		selector.setFields(new String[] {"Id", "Name", "Amount","StartDate", "EndDate", "Status","TargetGoogleSearch","TargetPartnerSearchNetwork","TargetSearchNetwork","TargetContentNetwork"});
110
		selector.setOrdering(new OrderBy[] {new OrderBy("Name", SortOrder.ASCENDING)});
111
		selector.setPaging(new Paging(offset, PAGE_SIZE));
112
		List<Map<String,String>> campaignDataMainList = new ArrayList<Map<String,String>>();
113
		CampaignPage page = null;
114
		do {
115
			// Get all campaigns.
9306 manish.sha 116
			try {
117
				page = campaignService.get(selector);
118
			} catch (ApiException e) {
119
				// TODO Auto-generated catch block
120
				e.printStackTrace();
121
			} catch (RemoteException e) {
122
				// TODO Auto-generated catch block
123
				e.printStackTrace();
124
			}
9005 manish.sha 125
 
126
			// Display campaigns.
127
			if (page.getEntries() != null) {
128
				for (Campaign campaign : page.getEntries()) {
9302 manish.sha 129
					//if(campaign.getId() > 0l && campaign.getName()!=null && !("").equalsIgnoreCase(campaign.getName()) && campaign.getEndDate()!=null && !("").equalsIgnoreCase(campaign.getEndDate()) && campaign.getStartDate()!=null && !("").equalsIgnoreCase(campaign.getStartDate())){
9292 manish.sha 130
						System.out.println("Campaign with name \"" + campaign.getName() + "\" and id \""
131
								+ campaign.getId() +"With Budget \""+ campaign.getBudget().getAmount().getMicroAmount()+ "\" was found.");
132
						System.out.println("Null Comes Here");
133
						Map<String,String> campaignMap = new HashMap<String,String>();
134
						campaignMap.put("CampaignId", campaign.getId()+"");
135
						campaignMap.put("Status", campaign.getStatus().getValue());
136
						campaignMap.put("StartDate", campaign.getStartDate());
137
						campaignMap.put("EndDate",campaign.getEndDate());
138
						campaignMap.put("Name",campaign.getName());
139
						campaignMap.put("Amount",campaign.getBudget().getAmount().getMicroAmount()+"");
140
						System.out.println("Null Comes Here 2");
9302 manish.sha 141
						//if(campaign.getNetworkSetting().getTargetGoogleSearch()!=null){
142
						campaignMap.put("TargetGoogleSearch",campaign.getNetworkSetting().getTargetGoogleSearch()+"");
143
						//}
144
						//else{
145
						//	campaignMap.put("TargetGoogleSearch","false");
146
						//}
147
						//if(campaign.getNetworkSetting().getTargetSearchNetwork()!=null){
148
						campaignMap.put("TargetSearchNetwork",campaign.getNetworkSetting().getTargetSearchNetwork()+"");
149
						//}
150
						//else{
151
							//campaignMap.put("TargetSearchNetwork","false");
152
						//}
153
						//if(campaign.getNetworkSetting().getTargetPartnerSearchNetwork()!=null){
154
						campaignMap.put("TargetPartnerSearchNetwork",campaign.getNetworkSetting().getTargetPartnerSearchNetwork()+"");
155
						//}
156
						//else{
157
							//campaignMap.put("TargetPartnerSearchNetwork","false");
158
						//}
159
						//if(campaign.getNetworkSetting().getTargetContentNetwork()!=null){
160
						campaignMap.put("TargetContentNetwork",campaign.getNetworkSetting().getTargetContentNetwork()+"");
161
						//}
162
						//else{
163
						//	campaignMap.put("TargetContentNetwork","false");
164
						//}
9292 manish.sha 165
						campaignDataMainList.add(campaignMap);
9289 manish.sha 166
					}
9302 manish.sha 167
				//}
9005 manish.sha 168
			} else {
169
				System.out.println("No campaigns were found.");
170
			}
171
 
172
			offset += PAGE_SIZE;
173
			selector.getPaging().setStartIndex(offset);
174
		} while (offset < page.getTotalNumEntries());
9296 manish.sha 175
		return campaignDataMainList;
9005 manish.sha 176
	}
177
}