private static final String PARENT_AD_UNIT_ID = "INSERT_AD_UNIT_ID_HERE";
public static void runExample(DfpServices dfpServices, DfpSession session, String parentAdUnitId)
throws Exception {
// Get the InventoryService.
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")
.orderBy("id ASC")
.limit(StatementBuilder.SUGGESTED_PAGE_LIMIT)
.withBindVariableValue("parentId", parentAdUnitId);
// Default for total result set size.
int totalResultSetSize = 0;
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(
"%d) Ad unit with ID \"%s\" and name \"%s\" will be archived.\n", i++,
adUnit.getId(), adUnit.getName());
}
}
statementBuilder.increaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
} while (statementBuilder.getOffset() < totalResultSetSize);
System.out.printf("Number of ad units to be archived: %d\n", totalResultSetSize);
if (totalResultSetSize > 0) {
// Remove limit and offset from statement.
statementBuilder.removeLimitAndOffset();
// Create action.
com.google.api.ads.dfp.axis.v201308.ArchiveAdUnits action =
new com.google.api.ads.dfp.axis.v201308.ArchiveAdUnits();
// Perform action.
UpdateResult result =
inventoryService.performAdUnitAction(action, statementBuilder.toStatement());
if (result != null && result.getNumChanges() > 0) {
System.out.printf("Number of ad units archived: %d\n", result.getNumChanges());
} else {
System.out.println("No ad units were archived.");