// Get DfpUser from "~/dfp.properties".
DfpUser user = new DfpUser();
// Get the InventoryService.
InventoryServiceInterface inventoryService =
user.getService(DfpService.V201308.INVENTORY_SERVICE);
// Create statement text to select active ad units.
String statementText = "WHERE status = :status LIMIT 500";
Statement filterStatement =
new StatementBuilder("")
.putValue("status", InventoryStatus.ACTIVE.toString())
.toStatement();
// Set defaults for page and offset.
AdUnitPage page = new AdUnitPage();
int offset = 0;
List<String> adUnitIds = new ArrayList<String>();
do {
// Create a statement to page through active ad units.
filterStatement.setQuery(statementText + " OFFSET " + offset);
// Get ad units by statement.
page = inventoryService.getAdUnitsByStatement(filterStatement);
if (page.getResults() != null) {
int i = page.getStartIndex();
for (AdUnit adUnit : page.getResults()) {
System.out.println(i + ") Ad unit with ID \"" + adUnit.getId()
+ "\", name \"" + adUnit.getName()
+ "\", and status \"" + adUnit.getStatus() + "\" will be deactivated.");
adUnitIds.add(adUnit.getId());
i++;
}
}
offset += 500;
} while (offset < page.getTotalResultSetSize());
System.out.println("Number of ad units to be deactivated: " + adUnitIds.size());
if (adUnitIds.size() > 0) {
// Modify statement for action.
filterStatement.setQuery("WHERE id IN (" + StringUtils.join(adUnitIds, ",") + ")");
// Create action.
DeactivateAdUnits action = new DeactivateAdUnits();
// Perform action.
UpdateResult result = inventoryService.performAdUnitAction(action, filterStatement);
// Display results.
if (result != null && result.getNumChanges() > 0) {
System.out.println("Number of ad units deactivated: " + result.getNumChanges());
} else {