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.targeting;
16
 
17
import com.google.api.ads.adwords.axis.factory.AdWordsServices;
18
import com.google.api.ads.adwords.axis.v201306.cm.Location;
19
import com.google.api.ads.adwords.axis.v201306.cm.LocationCriterion;
20
import com.google.api.ads.adwords.axis.v201306.cm.LocationCriterionServiceInterface;
21
import com.google.api.ads.adwords.axis.v201306.cm.Predicate;
22
import com.google.api.ads.adwords.axis.v201306.cm.PredicateOperator;
23
import com.google.api.ads.adwords.axis.v201306.cm.Selector;
24
import com.google.api.ads.adwords.lib.client.AdWordsSession;
25
import com.google.api.ads.common.lib.auth.OfflineCredentials;
26
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
27
import com.google.api.client.auth.oauth2.Credential;
28
 
29
/**
30
 * This example gets location criteria by name.
31
 *
32
 * Credentials and properties in {@code fromFile()} are pulled from the
33
 * "ads.properties" file. See README for more info.
34
 *
35
 * Tags: LocationCriterionSerivce.get
36
 *
37
 * @author Kevin Winter
38
 */
39
public class LookupLocation {
40
 
41
  /**
42
   * Helper function to format a string for parent locations.
43
   *
44
   * @param parents List of parent locations.
45
   * @return Formatted string representing parent locations.
46
   */
47
  public static String getParentLocationString(Location[] parents) {
48
    StringBuilder sb = new StringBuilder();
49
    if (parents != null) {
50
      for (Location parent : parents) {
51
        if (sb.length() > 0) {
52
          sb.append(", ");
53
        }
54
        sb.append(String.format("%s (%s)", parent.getLocationName(), parent.getDisplayType()));
55
      }
56
    } else {
57
      sb.append("N/A");
58
    }
59
    return sb.toString();
60
  }
61
 
62
  public static void main(String[] args) throws Exception {
63
    // Generate a refreshable OAuth2 credential similar to a ClientLogin token
64
    // and can be used in place of a service account.
65
    Credential oAuth2Credential = new OfflineCredentials.Builder()
66
        .forApi(Api.ADWORDS)
67
        .fromFile()
68
        .build()
69
        .generateCredential();
70
 
71
    // Construct an AdWordsSession.
72
    AdWordsSession session = new AdWordsSession.Builder()
73
        .fromFile()
74
        .withOAuth2Credential(oAuth2Credential)
75
        .build();
76
 
77
    String[] locationNames = new String[] {"Paris", "Quebec", "Spain", "Deutschland"};
78
 
79
    AdWordsServices adWordsServices = new AdWordsServices();
80
 
81
    runExample(adWordsServices, session, locationNames);
82
  }
83
 
84
  public static void runExample(
85
      AdWordsServices adWordsServices, AdWordsSession session, String[] locationNames)
86
      throws Exception {
87
    // Get the LocationCriterionService.
88
    LocationCriterionServiceInterface locationCriterionService =
89
        adWordsServices.get(session, LocationCriterionServiceInterface.class);
90
 
91
    Selector selector = new Selector();
92
    selector.setFields(new String[] {"Id", "LocationName", "CanonicalName", "DisplayType",
93
        "ParentLocations", "Reach", "TargetingStatus"});
94
 
95
    selector.setPredicates(new Predicate[] {
96
        // Location names must match exactly, only EQUALS and IN are
97
        // supported.
98
        new Predicate("LocationName", PredicateOperator.IN, locationNames),
99
        // Set the locale of the returned location names.
100
        new Predicate("Locale", PredicateOperator.EQUALS, new String[] {"en"})});
101
 
102
    // Make the get request.
103
    LocationCriterion[] locationCriteria = locationCriterionService.get(selector);
104
 
105
    // Display the resulting location criteria.
106
    for (LocationCriterion locationCriterion : locationCriteria) {
107
      String parentString =
108
          getParentLocationString(locationCriterion.getLocation().getParentLocations());
109
      System.out.printf("The search term '%s' returned the location '%s (%d)' of type '%s' "
110
          + "with parent locations '%s' and reach '%d' (%s).\n", locationCriterion.getSearchTerm(),
111
          locationCriterion.getLocation().getLocationName(), locationCriterion.getLocation()
112
              .getId(), locationCriterion.getLocation().getDisplayType(), parentString,
113
          locationCriterion.getReach(), locationCriterion.getLocation().getTargetingStatus());
114
    }
115
  }
116
}