Subversion Repositories SmartDukaan

Rev

Rev 9005 | 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
 
17
import com.google.api.ads.adwords.axis.factory.AdWordsServices;
9227 manish.sha 18
import com.google.api.ads.adwords.axis.v201309.cm.Budget;
19
import com.google.api.ads.adwords.axis.v201309.cm.BudgetBudgetDeliveryMethod;
20
import com.google.api.ads.adwords.axis.v201309.cm.BudgetBudgetPeriod;
21
import com.google.api.ads.adwords.axis.v201309.cm.BudgetOperation;
22
import com.google.api.ads.adwords.axis.v201309.cm.BudgetServiceInterface;
9005 manish.sha 23
import com.google.api.ads.adwords.axis.v201309.cm.Campaign;
24
import com.google.api.ads.adwords.axis.v201309.cm.CampaignOperation;
25
import com.google.api.ads.adwords.axis.v201309.cm.CampaignReturnValue;
26
import com.google.api.ads.adwords.axis.v201309.cm.CampaignServiceInterface;
27
import com.google.api.ads.adwords.axis.v201309.cm.CampaignStatus;
9227 manish.sha 28
import com.google.api.ads.adwords.axis.v201309.cm.Money;
9005 manish.sha 29
import com.google.api.ads.adwords.axis.v201309.cm.Operator;
30
import com.google.api.ads.adwords.lib.client.AdWordsSession;
31
import com.google.api.ads.common.lib.auth.OfflineCredentials;
32
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
33
import com.google.api.client.auth.oauth2.Credential;
34
 
35
/**
36
 * This example updates a campaign by setting the status to PAUSED. To get
37
 * campaigns, run GetCampaigns.java.
38
 *
39
 * Credentials and properties in {@code fromFile()} are pulled from the
40
 * "ads.properties" file. See README for more info.
41
 *
42
 * Tags: CampaignService.mutate
43
 *
44
 * @author Kevin Winter
45
 */
46
public class UpdateCampaign {
47
 
9227 manish.sha 48
	public static Long runExample(Long campaignId, String status, String name, Long amount ) throws Exception {
9005 manish.sha 49
		// Generate a refreshable OAuth2 credential similar to a ClientLogin token
50
		// and can be used in place of a service account.
51
		Credential oAuth2Credential = new OfflineCredentials.Builder()
52
		.forApi(Api.ADWORDS)
53
		.fromFile()
54
		.build()
55
		.generateCredential();
56
 
57
		// Construct an AdWordsSession.
58
		AdWordsSession session = new AdWordsSession.Builder()
59
		.fromFile()
60
		.withOAuth2Credential(oAuth2Credential)
61
		.build();
62
 
63
		AdWordsServices adWordsServices = new AdWordsServices();
64
		// Get the CampaignService.
65
		CampaignServiceInterface campaignService =
66
			adWordsServices.get(session, CampaignServiceInterface.class);
67
 
68
		// Create campaign with updated status.
69
		Campaign campaign = new Campaign();
70
		campaign.setId(campaignId);
9227 manish.sha 71
		if(status!=null && !("").equalsIgnoreCase(status)){
72
			campaign.setStatus(CampaignStatus.fromString(status));
73
		}
74
		if(name!=null && !("").equalsIgnoreCase(name)){
75
			campaign.setName(name);
76
		}
77
 
78
		if(amount>-1l){
79
			// Get the BudgetService.
80
			BudgetServiceInterface budgetService =
81
				adWordsServices.get(session, BudgetServiceInterface.class);
82
 
83
			// Create a budget, which can be shared by multiple campaigns.
84
			Budget sharedBudget = new Budget();
85
			sharedBudget.setName(name +"#" + System.currentTimeMillis());
86
			Money budgetAmount = new Money();
87
			budgetAmount.setMicroAmount(amount);
88
			sharedBudget.setAmount(budgetAmount);
89
			sharedBudget.setDeliveryMethod(BudgetBudgetDeliveryMethod.STANDARD);
90
			sharedBudget.setPeriod(BudgetBudgetPeriod.DAILY);
91
 
92
			BudgetOperation budgetOperation = new BudgetOperation();
93
			budgetOperation.setOperand(sharedBudget);
94
			budgetOperation.setOperator(Operator.ADD);
95
 
96
			// Add the budget
97
			Long budgetId =
98
				budgetService.mutate(new BudgetOperation[] {budgetOperation}).getValue(0).getBudgetId();
99
 
100
			Budget budget = new Budget();
101
			budget.setBudgetId(budgetId);
102
			campaign.setBudget(budget);
103
		}
9005 manish.sha 104
		// Create operations.
105
		CampaignOperation operation = new CampaignOperation();
106
		operation.setOperand(campaign);
107
		operation.setOperator(Operator.SET);
108
 
109
		CampaignOperation[] operations = new CampaignOperation[] {operation};
9227 manish.sha 110
		if((status!=null && !("").equalsIgnoreCase(status)) || (name!=null && !("").equalsIgnoreCase(name)) || (amount>-1l)){
111
			// Update campaign.
112
			CampaignReturnValue result = campaignService.mutate(operations);
113
			Long updatedCampaignId = 0l;
114
			// Display campaigns.
115
			for (Campaign campaignResult : result.getValue()) {
116
				System.out.println("Campaign with name \"" + campaignResult.getName() + "\", id \""
117
						+ campaignResult.getId() + "\", and budget delivery method \""
118
						+ campaignResult.getBudget().getDeliveryMethod() + "\" was updated.");
119
				updatedCampaignId = campaignResult.getId();
120
			}
121
			return updatedCampaignId;
9005 manish.sha 122
		}
9227 manish.sha 123
 
124
		return 0l;
9005 manish.sha 125
	}
126
}