| 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.accountmanagement;
|
|
|
16 |
|
|
|
17 |
import com.google.api.ads.adwords.axis.factory.AdWordsServices;
|
|
|
18 |
import com.google.api.ads.adwords.axis.v201306.cm.Selector;
|
|
|
19 |
import com.google.api.ads.adwords.axis.v201306.mcm.ManagedCustomer;
|
|
|
20 |
import com.google.api.ads.adwords.axis.v201306.mcm.ManagedCustomerLink;
|
|
|
21 |
import com.google.api.ads.adwords.axis.v201306.mcm.ManagedCustomerPage;
|
|
|
22 |
import com.google.api.ads.adwords.axis.v201306.mcm.ManagedCustomerServiceInterface;
|
|
|
23 |
import com.google.api.ads.adwords.lib.client.AdWordsSession;
|
|
|
24 |
import com.google.api.ads.common.lib.auth.OfflineCredentials;
|
|
|
25 |
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
|
|
|
26 |
import com.google.api.client.auth.oauth2.Credential;
|
|
|
27 |
|
|
|
28 |
import org.apache.commons.lang.StringUtils;
|
|
|
29 |
|
|
|
30 |
import java.util.ArrayList;
|
|
|
31 |
import java.util.HashMap;
|
|
|
32 |
import java.util.List;
|
|
|
33 |
import java.util.Map;
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* This example gets the account hierarchy under the current account.
|
|
|
37 |
*
|
|
|
38 |
* Credentials and properties in {@code fromFile()} are pulled from the
|
|
|
39 |
* "ads.properties" file. See README for more info.
|
|
|
40 |
*
|
|
|
41 |
* Note: this code example won't work with test accounts. See
|
|
|
42 |
* https://developers.google.com/adwords/api/docs/test-accounts
|
|
|
43 |
*
|
|
|
44 |
* Tags: ManagedCustomerService.get
|
|
|
45 |
*
|
|
|
46 |
* Category: adx-exclude
|
|
|
47 |
*
|
|
|
48 |
* @author Adam Rogal
|
|
|
49 |
*/
|
|
|
50 |
public class GetAccountHierarchy {
|
|
|
51 |
|
|
|
52 |
public static void main(String[] args) throws Exception {
|
|
|
53 |
// Generate a refreshable OAuth2 credential similar to a ClientLogin token
|
|
|
54 |
// and can be used in place of a service account.
|
|
|
55 |
Credential oAuth2Credential = new OfflineCredentials.Builder()
|
|
|
56 |
.forApi(Api.ADWORDS)
|
|
|
57 |
.fromFile()
|
|
|
58 |
.build()
|
|
|
59 |
.generateCredential();
|
|
|
60 |
|
|
|
61 |
// Construct an AdWordsSession.
|
|
|
62 |
AdWordsSession session = new AdWordsSession.Builder()
|
|
|
63 |
.fromFile()
|
|
|
64 |
.withOAuth2Credential(oAuth2Credential)
|
|
|
65 |
.build();
|
|
|
66 |
|
|
|
67 |
AdWordsServices adWordsServices = new AdWordsServices();
|
|
|
68 |
|
|
|
69 |
runExample(adWordsServices, session);
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
public static void runExample(AdWordsServices adWordsServices, AdWordsSession session)
|
|
|
73 |
throws Exception {
|
|
|
74 |
// Get the ServicedAccountService.
|
|
|
75 |
ManagedCustomerServiceInterface managedCustomerService =
|
|
|
76 |
adWordsServices.get(session, ManagedCustomerServiceInterface.class);
|
|
|
77 |
|
|
|
78 |
// Create selector.
|
|
|
79 |
Selector selector = new Selector();
|
|
|
80 |
selector.setFields(new String[]{"Login", "CustomerId", "Name"});
|
|
|
81 |
|
|
|
82 |
// Get results.
|
|
|
83 |
ManagedCustomerPage page = managedCustomerService.get(selector);
|
|
|
84 |
|
|
|
85 |
// Display serviced account graph.
|
|
|
86 |
if (page.getEntries() != null) {
|
|
|
87 |
// Create map from customerId to customer node.
|
|
|
88 |
Map<Long, ManagedCustomerTreeNode> customerIdToCustomerNode =
|
|
|
89 |
new HashMap<Long, ManagedCustomerTreeNode>();
|
|
|
90 |
|
|
|
91 |
// Create account tree nodes for each customer.
|
|
|
92 |
for (ManagedCustomer customer : page.getEntries()) {
|
|
|
93 |
ManagedCustomerTreeNode node = new ManagedCustomerTreeNode();
|
|
|
94 |
node.account = customer;
|
|
|
95 |
customerIdToCustomerNode.put(customer.getCustomerId(), node);
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
// For each link, connect nodes in tree.
|
|
|
99 |
if (page.getLinks() != null) {
|
|
|
100 |
for (ManagedCustomerLink link : page.getLinks()) {
|
|
|
101 |
ManagedCustomerTreeNode managerNode = customerIdToCustomerNode.get(
|
|
|
102 |
link.getManagerCustomerId());
|
|
|
103 |
ManagedCustomerTreeNode childNode = customerIdToCustomerNode.get(
|
|
|
104 |
link.getClientCustomerId());
|
|
|
105 |
childNode.parentNode = managerNode;
|
|
|
106 |
if (managerNode != null) {
|
|
|
107 |
managerNode.childAccounts.add(childNode);
|
|
|
108 |
}
|
|
|
109 |
}
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
// Find the root account node in the tree.
|
|
|
113 |
ManagedCustomerTreeNode rootNode = null;
|
|
|
114 |
for (ManagedCustomer account : page.getEntries()) {
|
|
|
115 |
if (customerIdToCustomerNode.get(account.getCustomerId()).parentNode == null) {
|
|
|
116 |
rootNode = customerIdToCustomerNode.get(account.getCustomerId());
|
|
|
117 |
break;
|
|
|
118 |
}
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
// Display account tree.
|
|
|
122 |
System.out.println("Login, CustomerId, Name");
|
|
|
123 |
System.out.println(rootNode.toTreeString(0, new StringBuffer()));
|
|
|
124 |
} else {
|
|
|
125 |
System.out.println("No serviced accounts were found.");
|
|
|
126 |
}
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
/**
|
|
|
130 |
* Example implementation of a node that would exist in an account tree.
|
|
|
131 |
*/
|
|
|
132 |
private static class ManagedCustomerTreeNode {
|
|
|
133 |
protected ManagedCustomerTreeNode parentNode;
|
|
|
134 |
protected ManagedCustomer account;
|
|
|
135 |
protected List<ManagedCustomerTreeNode> childAccounts =
|
|
|
136 |
new ArrayList<ManagedCustomerTreeNode>();
|
|
|
137 |
|
|
|
138 |
/**
|
|
|
139 |
* Default constructor.
|
|
|
140 |
*/
|
|
|
141 |
public ManagedCustomerTreeNode() {}
|
|
|
142 |
|
|
|
143 |
@Override
|
|
|
144 |
public String toString() {
|
|
|
145 |
String login = account.getLogin();
|
|
|
146 |
if (login == null || login.trim().length() < 1) {
|
|
|
147 |
login = "(no login)";
|
|
|
148 |
}
|
|
|
149 |
return String.format("%s, %s, %s", login, account.getCustomerId(), account.getName());
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
/**
|
|
|
153 |
* Returns a string representation of the current level of the tree and
|
|
|
154 |
* recursively returns the string representation of the levels below it.
|
|
|
155 |
*
|
|
|
156 |
* @param depth the depth of the node
|
|
|
157 |
* @param sb the string buffer containing the tree representation
|
|
|
158 |
* @return the tree string representation
|
|
|
159 |
*/
|
|
|
160 |
public StringBuffer toTreeString(int depth, StringBuffer sb) {
|
|
|
161 |
sb.append(StringUtils.repeat("-", depth * 2)).append(this).append("\n");
|
|
|
162 |
for (ManagedCustomerTreeNode childAccount : childAccounts) {
|
|
|
163 |
childAccount.toTreeString(depth + 1, sb);
|
|
|
164 |
}
|
|
|
165 |
return sb;
|
|
|
166 |
}
|
|
|
167 |
}
|
|
|
168 |
}
|