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;
18
import com.google.api.ads.adwords.axis.v201309.cm.AdGroup;
19
import com.google.api.ads.adwords.axis.v201309.cm.AdGroupOperation;
20
import com.google.api.ads.adwords.axis.v201309.cm.AdGroupReturnValue;
21
import com.google.api.ads.adwords.axis.v201309.cm.AdGroupServiceInterface;
22
import com.google.api.ads.adwords.axis.v201309.cm.AdGroupStatus;
9227 manish.sha 23
import com.google.api.ads.adwords.axis.v201309.cm.BiddingStrategyConfiguration;
24
import com.google.api.ads.adwords.axis.v201309.cm.Bids;
25
import com.google.api.ads.adwords.axis.v201309.cm.CpcBid;
26
import com.google.api.ads.adwords.axis.v201309.cm.Money;
9005 manish.sha 27
import com.google.api.ads.adwords.axis.v201309.cm.Operator;
28
import com.google.api.ads.adwords.lib.client.AdWordsSession;
29
import com.google.api.ads.common.lib.auth.OfflineCredentials;
30
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
31
import com.google.api.client.auth.oauth2.Credential;
32
 
33
/**
34
 * This example updates status for a given ad group. To get ad groups, run
35
 * GetAdGroups.java.
36
 *
37
 * Credentials and properties in {@code fromFile()} are pulled from the
38
 * "ads.properties" file. See README for more info.
39
 *
40
 * Tags: AdGroupService.mutate
41
 *
42
 * @author Kevin Winter
43
 */
44
public class UpdateAdGroup {
45
 
9227 manish.sha 46
	public static Long runExample(String name, String status, Long bidAmount,Long adGroupId) throws Exception {
47
		// Generate a refreshable OAuth2 credential similar to a ClientLogin token
48
		// and can be used in place of a service account.
49
		Credential oAuth2Credential = new OfflineCredentials.Builder()
50
		.forApi(Api.ADWORDS)
51
		.fromFile()
52
		.build()
53
		.generateCredential();
9005 manish.sha 54
 
9227 manish.sha 55
		// Construct an AdWordsSession.
56
		AdWordsSession session = new AdWordsSession.Builder()
57
		.fromFile()
58
		.withOAuth2Credential(oAuth2Credential)
59
		.build();
9005 manish.sha 60
 
9227 manish.sha 61
		AdWordsServices adWordsServices = new AdWordsServices();
62
		// Get the AdGroupService.
63
		AdGroupServiceInterface adGroupService =
64
			adWordsServices.get(session, AdGroupServiceInterface.class);
9005 manish.sha 65
 
9227 manish.sha 66
		// Create ad group with updated status.
67
		AdGroup adGroup = new AdGroup();
68
		adGroup.setId(adGroupId);
69
		if(status!=null && !("").equalsIgnoreCase(status)){
70
			adGroup.setStatus(AdGroupStatus.fromString(status));
71
		}
72
		if(name!=null && !("").equalsIgnoreCase(name)){
73
			adGroup.setName(name);
74
		}
75
 
76
		if(bidAmount>-1l){
77
			BiddingStrategyConfiguration biddingStrategyConfiguration = new BiddingStrategyConfiguration();
78
			CpcBid bid = new CpcBid();
79
			bid.setBid(new Money(null, bidAmount));
80
 
81
			biddingStrategyConfiguration.setBids(new Bids[] {bid});
82
			adGroup.setBiddingStrategyConfiguration(biddingStrategyConfiguration);
83
		}
84
		// Create operations.
85
		AdGroupOperation operation = new AdGroupOperation();
86
		operation.setOperand(adGroup);
87
		operation.setOperator(Operator.SET);
9005 manish.sha 88
 
9227 manish.sha 89
		AdGroupOperation[] operations = new AdGroupOperation[] {operation};
90
		if((status!=null && !("").equalsIgnoreCase(status)) || (name!=null && !("").equalsIgnoreCase(name)) || (bidAmount>-1l)){
91
			// Update ad group.
92
			AdGroupReturnValue result = adGroupService.mutate(operations);
93
 
94
			Long updatedAdGroupId = 0L;
95
			// Display ad groups.
96
			for (AdGroup adGroupResult : result.getValue()) {
97
				System.out.println("Ad group with name \"" + adGroupResult.getName() + "\", id \""
98
						+ adGroupResult.getId() + "\", and status \"" + adGroupResult.getStatus()
99
						+ "\" was updated.");
100
				updatedAdGroupId = adGroupResult.getId();
101
			}
102
			return updatedAdGroupId;
103
		}
104
		return 0l;
105
	}
9005 manish.sha 106
}