Subversion Repositories SmartDukaan

Rev

Rev 9005 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 9005 Rev 9227
Line 13... Line 13...
13
// limitations under the License.
13
// limitations under the License.
14
 
14
 
15
package adwords.axis.v201309.basicoperations;
15
package adwords.axis.v201309.basicoperations;
16
 
16
 
17
import com.google.api.ads.adwords.axis.factory.AdWordsServices;
17
import com.google.api.ads.adwords.axis.factory.AdWordsServices;
-
 
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;
18
import com.google.api.ads.adwords.axis.v201309.cm.Campaign;
23
import com.google.api.ads.adwords.axis.v201309.cm.Campaign;
19
import com.google.api.ads.adwords.axis.v201309.cm.CampaignOperation;
24
import com.google.api.ads.adwords.axis.v201309.cm.CampaignOperation;
20
import com.google.api.ads.adwords.axis.v201309.cm.CampaignReturnValue;
25
import com.google.api.ads.adwords.axis.v201309.cm.CampaignReturnValue;
21
import com.google.api.ads.adwords.axis.v201309.cm.CampaignServiceInterface;
26
import com.google.api.ads.adwords.axis.v201309.cm.CampaignServiceInterface;
22
import com.google.api.ads.adwords.axis.v201309.cm.CampaignStatus;
27
import com.google.api.ads.adwords.axis.v201309.cm.CampaignStatus;
-
 
28
import com.google.api.ads.adwords.axis.v201309.cm.Money;
23
import com.google.api.ads.adwords.axis.v201309.cm.Operator;
29
import com.google.api.ads.adwords.axis.v201309.cm.Operator;
24
import com.google.api.ads.adwords.lib.client.AdWordsSession;
30
import com.google.api.ads.adwords.lib.client.AdWordsSession;
25
import com.google.api.ads.common.lib.auth.OfflineCredentials;
31
import com.google.api.ads.common.lib.auth.OfflineCredentials;
26
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
32
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
27
import com.google.api.client.auth.oauth2.Credential;
33
import com.google.api.client.auth.oauth2.Credential;
Line 37... Line 43...
37
 *
43
 *
38
 * @author Kevin Winter
44
 * @author Kevin Winter
39
 */
45
 */
40
public class UpdateCampaign {
46
public class UpdateCampaign {
41
 
47
 
42
	public static void main(String[] args) throws Exception {
48
	public static Long runExample(Long campaignId, String status, String name, Long amount ) throws Exception {
43
		// Generate a refreshable OAuth2 credential similar to a ClientLogin token
49
		// Generate a refreshable OAuth2 credential similar to a ClientLogin token
44
		// and can be used in place of a service account.
50
		// and can be used in place of a service account.
45
		Credential oAuth2Credential = new OfflineCredentials.Builder()
51
		Credential oAuth2Credential = new OfflineCredentials.Builder()
46
		.forApi(Api.ADWORDS)
52
		.forApi(Api.ADWORDS)
47
		.fromFile()
53
		.fromFile()
Line 52... Line 58...
52
		AdWordsSession session = new AdWordsSession.Builder()
58
		AdWordsSession session = new AdWordsSession.Builder()
53
		.fromFile()
59
		.fromFile()
54
		.withOAuth2Credential(oAuth2Credential)
60
		.withOAuth2Credential(oAuth2Credential)
55
		.build();
61
		.build();
56
 
62
 
57
		long campaignId = Long.parseLong("INSERT_CAMPAIGN_ID_HERE");
-
 
58
 
-
 
59
		AdWordsServices adWordsServices = new AdWordsServices();
63
		AdWordsServices adWordsServices = new AdWordsServices();
60
 
-
 
61
		runExample(adWordsServices, session, campaignId);
-
 
62
	}
-
 
63
 
-
 
64
	public static void runExample(
-
 
65
			AdWordsServices adWordsServices, AdWordsSession session, Long campaignId) throws Exception {
-
 
66
		// Get the CampaignService.
64
		// Get the CampaignService.
67
		CampaignServiceInterface campaignService =
65
		CampaignServiceInterface campaignService =
68
			adWordsServices.get(session, CampaignServiceInterface.class);
66
			adWordsServices.get(session, CampaignServiceInterface.class);
69
 
67
 
70
		// Create campaign with updated status.
68
		// Create campaign with updated status.
71
		Campaign campaign = new Campaign();
69
		Campaign campaign = new Campaign();
72
		campaign.setId(campaignId);
70
		campaign.setId(campaignId);
-
 
71
		if(status!=null && !("").equalsIgnoreCase(status)){
73
		campaign.setStatus(CampaignStatus.PAUSED);
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);
74
 
91
	
-
 
92
			BudgetOperation budgetOperation = new BudgetOperation();
-
 
93
			budgetOperation.setOperand(sharedBudget);
-
 
94
			budgetOperation.setOperator(Operator.ADD);
75
 
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
		}
76
		// Create operations.
104
		// Create operations.
77
		CampaignOperation operation = new CampaignOperation();
105
		CampaignOperation operation = new CampaignOperation();
78
		operation.setOperand(campaign);
106
		operation.setOperand(campaign);
79
		operation.setOperator(Operator.SET);
107
		operation.setOperator(Operator.SET);
80
 
108
 
81
		CampaignOperation[] operations = new CampaignOperation[] {operation};
109
		CampaignOperation[] operations = new CampaignOperation[] {operation};
82
 
-
 
-
 
110
		if((status!=null && !("").equalsIgnoreCase(status)) || (name!=null && !("").equalsIgnoreCase(name)) || (amount>-1l)){
83
		// Update campaign.
111
			// Update campaign.
84
		CampaignReturnValue result = campaignService.mutate(operations);
112
			CampaignReturnValue result = campaignService.mutate(operations);
85
 
-
 
-
 
113
			Long updatedCampaignId = 0l;
86
		// Display campaigns.
114
			// Display campaigns.
87
		for (Campaign campaignResult : result.getValue()) {
115
			for (Campaign campaignResult : result.getValue()) {
88
			System.out.println("Campaign with name \"" + campaignResult.getName() + "\", id \""
116
				System.out.println("Campaign with name \"" + campaignResult.getName() + "\", id \""
89
					+ campaignResult.getId() + "\", and budget delivery method \""
117
						+ campaignResult.getId() + "\", and budget delivery method \""
90
					+ campaignResult.getBudget().getDeliveryMethod() + "\" was updated.");
118
						+ campaignResult.getBudget().getDeliveryMethod() + "\" was updated.");
-
 
119
				updatedCampaignId = campaignResult.getId();
-
 
120
			}
-
 
121
			return updatedCampaignId;
91
		}
122
		}
-
 
123
				
-
 
124
		return 0l;
92
	}
125
	}
93
}
126
}