InventoryServiceInterface inventoryService =
dfpServices.get(session, InventoryServiceInterface.class);
// Create a statement to select ad units under the parent ad unit and the
// parent ad unit.
StatementBuilder statementBuilder = new StatementBuilder()
.where("parentId = :parentId or id = :parentId")
.limit(StatementBuilder.SUGGESTED_PAGE_LIMIT)
.withBindVariableValue("parentId", parentAdUnitId);
// Default for total result set size.
int totalResultSetSize = 0;
List<String> adUnitIds = new ArrayList<String>();
do {
// Get ad units by statement.
AdUnitPage page = inventoryService.getAdUnitsByStatement(statementBuilder.toStatement());
if (page.getResults() != null) {
totalResultSetSize = page.getTotalResultSetSize();
int i = page.getStartIndex();
for (AdUnit adUnit : page.getResults()) {
System.out.printf(
"%s) Ad unit with ID \"%s\" and name \"%s\" will be archived.\n", i,
adUnit.getId(), adUnit.getName());
adUnitIds.add(adUnit.getId());
i++;
}
}
statementBuilder.increaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
} while (statementBuilder.getOffset() < totalResultSetSize);
System.out.printf("Number of ad units to be archived: %s\n", totalResultSetSize);
if (!adUnitIds.isEmpty()) {
// Remove limit and offset from statement.
statementBuilder.removeLimitAndOffset();
// Create action.
com.google.api.ads.dfp.axis.v201208.ArchiveAdUnits action =
new com.google.api.ads.dfp.axis.v201208.ArchiveAdUnits();
// Perform action.
UpdateResult result =
inventoryService.performAdUnitAction(action, statementBuilder.toStatement());
if (result != null && result.getNumChanges() > 0) {
System.out.println("Number of ad units archived: " + result.getNumChanges());
} else {
System.out.println("No ad units were archived.");