private static final String NUM_REQUESTS = "INSERT_NUMBER_OF_REQUESTS_HERE";
public static void runExample(DfpServices dfpServices, DfpSession session, long numRequests)
throws Exception {
// Get the SuggestedAdUnitService.
SuggestedAdUnitServiceInterface suggestedAdUnitService =
dfpServices.get(session, SuggestedAdUnitServiceInterface.class);
// Create a statement to only select suggested ad units that are highly
// requested.
StatementBuilder statementBuilder = new StatementBuilder()
.where("numRequests >= :numRequests")
.orderBy("id ASC")
.limit(StatementBuilder.SUGGESTED_PAGE_LIMIT)
.withBindVariableValue("numRequests", numRequests);
// Default for total result set size.
int totalResultSetSize = 0;
do {
// Get suggested ad units by statement.
SuggestedAdUnitPage page =
suggestedAdUnitService.getSuggestedAdUnitsByStatement(statementBuilder.toStatement());
if (page.getResults() != null) {
totalResultSetSize = page.getTotalResultSetSize();
int i = page.getStartIndex();
for (SuggestedAdUnit suggestedAdUnit : page.getResults()) {
System.out.printf("%d) Suggsted ad unit with ID \"%s\" will be approved.\n", i++,
suggestedAdUnit.getId());
}
}
statementBuilder.increaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
} while (statementBuilder.getOffset() < totalResultSetSize);
System.out.printf("Number of suggested ad units to be approved: %d\n", totalResultSetSize);
if (totalResultSetSize > 0) {
// Remove limit and offset from statement.
statementBuilder.removeLimitAndOffset();
// Create action.
com.google.api.ads.dfp.axis.v201302.ApproveSuggestedAdUnit action =
new com.google.api.ads.dfp.axis.v201302.ApproveSuggestedAdUnit();
// Perform action.
SuggestedAdUnitUpdateResult result = suggestedAdUnitService.performSuggestedAdUnitAction(
action, statementBuilder.toStatement());
if (result != null && result.getNumChanges() > 0) {
System.out.printf(
"Number of suggested ad units approved: %d\n", result.getNewAdUnitIds().length);