Subversion Repositories SmartDukaan

Rev

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.v201306.campaignmanagement;
16
 
17
import com.google.api.ads.adwords.axis.factory.AdWordsServices;
18
import com.google.api.ads.adwords.axis.v201306.cm.AdGroupCriterionOperation;
19
import com.google.api.ads.adwords.axis.v201306.cm.ApiError;
20
import com.google.api.ads.adwords.axis.v201306.cm.BasicJobStatus;
21
import com.google.api.ads.adwords.axis.v201306.cm.BiddableAdGroupCriterion;
22
import com.google.api.ads.adwords.axis.v201306.cm.BulkMutateJobPolicy;
23
import com.google.api.ads.adwords.axis.v201306.cm.BulkMutateJobSelector;
24
import com.google.api.ads.adwords.axis.v201306.cm.Job;
25
import com.google.api.ads.adwords.axis.v201306.cm.JobResult;
26
import com.google.api.ads.adwords.axis.v201306.cm.Keyword;
27
import com.google.api.ads.adwords.axis.v201306.cm.KeywordMatchType;
28
import com.google.api.ads.adwords.axis.v201306.cm.MutateJobServiceInterface;
29
import com.google.api.ads.adwords.axis.v201306.cm.Operand;
30
import com.google.api.ads.adwords.axis.v201306.cm.Operation;
31
import com.google.api.ads.adwords.axis.v201306.cm.Operator;
32
import com.google.api.ads.adwords.axis.v201306.cm.SimpleMutateJob;
33
import com.google.api.ads.adwords.lib.client.AdWordsSession;
34
import com.google.api.ads.common.lib.auth.OfflineCredentials;
35
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
36
import com.google.api.client.auth.oauth2.Credential;
37
 
38
import java.util.ArrayList;
39
import java.util.List;
40
import java.util.Random;
41
import java.util.regex.Matcher;
42
import java.util.regex.Pattern;
43
 
44
/**
45
 * This code sample illustrates how to perform asynchronous requests using the
46
 * MutateJobService.
47
 *
48
 * Credentials and properties in {@code fromFile()} are pulled from the
49
 * "ads.properties" file. See README for more info.
50
 *
51
 * Tags: MutateJobService.mutate
52
 * Tags: MutateJobService.get
53
 * Tags: MutateJobService.getResult
54
 *
55
 * Category: adx-exclude
56
 *
57
 * @author Kevin Winter
58
 */
59
public class AddKeywordsInBulk {
60
 
61
  private static final long RETRY_INTERVAL = 5;
62
  private static final long RETRIES_COUNT = 12;
63
  private static final long KEYWORD_NUMBER = 100;
64
  private static final Pattern INDEX_REGEX = Pattern.compile("operations\\[(\\d+)\\].*");
65
 
66
  private static int getOperationIndex(String fieldPath) {
67
    Matcher m = INDEX_REGEX.matcher(fieldPath);
68
    if (!m.matches()) {
69
      throw new IllegalArgumentException("Invalid fieldPath: " + fieldPath);
70
    }
71
    return Integer.valueOf(m.group(1));
72
  }
73
 
74
  public static void main(String[] args) throws Exception {
75
    // Generate a refreshable OAuth2 credential similar to a ClientLogin token
76
    // and can be used in place of a service account.
77
    Credential oAuth2Credential = new OfflineCredentials.Builder()
78
        .forApi(Api.ADWORDS)
79
        .fromFile()
80
        .build()
81
        .generateCredential();
82
 
83
    // Construct an AdWordsSession.
84
    AdWordsSession session = new AdWordsSession.Builder()
85
        .fromFile()
86
        .withOAuth2Credential(oAuth2Credential)
87
        .build();
88
 
89
    long adGroupId = Long.parseLong("INSERT_AD_GROUP_ID_HERE");
90
 
91
    AdWordsServices adWordsServices = new AdWordsServices();
92
 
93
    runExample(adWordsServices, session, adGroupId);
94
  }
95
 
96
  public static void runExample(
97
      AdWordsServices adWordsServices, AdWordsSession session, Long adGroupId) throws Exception {
98
    // Get the MutateJobService.
99
    MutateJobServiceInterface mutateJobService =
100
        adWordsServices.get(session, MutateJobServiceInterface.class);
101
 
102
    Random r = new Random();
103
    List<AdGroupCriterionOperation> operations = new ArrayList<AdGroupCriterionOperation>();
104
 
105
    // Create AdGroupCriterionOperations to add keywords.
106
    for (int i = 0; i < KEYWORD_NUMBER; i++) {
107
      // Create Keyword.
108
      String text = String.format("mars%d", i);
109
 
110
      // Make 10% of keywords invalid to demonstrate error handling.
111
      if (r.nextInt() % 10 == 0) {
112
        text = text + "!!!";
113
      }
114
      Keyword keyword = new Keyword();
115
      keyword.setText(text);
116
      keyword.setMatchType(KeywordMatchType.BROAD);
117
 
118
      // Create BiddableAdGroupCriterion.
119
      BiddableAdGroupCriterion bagc = new BiddableAdGroupCriterion();
120
      bagc.setAdGroupId(adGroupId);
121
      bagc.setCriterion(keyword);
122
 
123
      // Create AdGroupCriterionOperation.
124
      AdGroupCriterionOperation agco = new AdGroupCriterionOperation();
125
      agco.setOperand(bagc);
126
      agco.setOperator(Operator.ADD);
127
 
128
      // Add to list.
129
      operations.add(agco);
130
    }
131
 
132
    // You can specify up to 3 job IDs that must successfully complete before
133
    // this job can be processed.
134
    BulkMutateJobPolicy policy = new BulkMutateJobPolicy();
135
    policy.setPrerequisiteJobIds(new long[] {});
136
 
137
    // Call mutate to create a new job.
138
    SimpleMutateJob response =
139
        mutateJobService.mutate(operations.toArray(new Operation[operations.size()]), policy);
140
 
141
    long jobId = response.getId();
142
    System.out.printf("Job with ID %d was successfully created.\n", jobId);
143
 
144
    // Create selector to retrieve job status and wait for it to complete.
145
    BulkMutateJobSelector selector = new BulkMutateJobSelector();
146
    selector.setJobIds(new long[] {jobId});
147
 
148
    // Poll for job status until it's finished.
149
    System.out.println("Retrieving job status...");
150
    SimpleMutateJob jobStatusResponse = null;
151
    BasicJobStatus status = null;
152
    for (int i = 0; i < RETRIES_COUNT; i++) {
153
      Job[] jobs = mutateJobService.get(selector);
154
      jobStatusResponse = (SimpleMutateJob) jobs[0];
155
      status = jobStatusResponse.getStatus();
156
      if (status == BasicJobStatus.COMPLETED || status == BasicJobStatus.FAILED) {
157
        break;
158
      }
159
      System.out.printf("[%d] Current status is '%s', waiting %d seconds to retry...\n", i,
160
          status.toString(), RETRY_INTERVAL);
161
      Thread.sleep(RETRY_INTERVAL * 1000);
162
    }
163
 
164
    // Handle unsuccessful results.
165
    if (status == BasicJobStatus.FAILED) {
166
      throw new IllegalStateException("Job failed with reason: "
167
          + jobStatusResponse.getFailureReason());
168
    }
169
    if (status == BasicJobStatus.PENDING || status == BasicJobStatus.PROCESSING) {
170
      throw new IllegalAccessException("Job did not complete within "
171
          + ((RETRIES_COUNT - 1) * RETRY_INTERVAL) + " seconds.");
172
    }
173
 
174
    // Status must be COMPLETED.
175
    // Get the job result. Here we re-use the same selector.
176
    JobResult result = mutateJobService.getResult(selector);
177
 
178
    // Output results.
179
    int i = 0;
180
    for (Operand operand : result.getSimpleMutateResult().getResults()) {
181
      String outcome = operand.getPlaceHolder() == null ? "SUCCESS" : "FAILED";
182
      System.out.printf("Operation [%d] - %s\n", i++, outcome);
183
    }
184
 
185
    // Output errors.
186
    for (ApiError error : result.getSimpleMutateResult().getErrors()) {
187
      int index = getOperationIndex(error.getFieldPath());
188
      String reason = error.getErrorString();
189
      System.out.printf("ERROR - keyword '%s' failed due to '%s'\n",
190
          ((Keyword) operations.get(index).getOperand().getCriterion()).getText(), reason);
191
    }
192
  }
193
}