private static final String PLACEMENT_ID = "INSERT_PLACEMENT_ID_HERE";
public static void runExample(DfpServices dfpServices, DfpSession session,
String adUnitId, long placementId) throws Exception {
// Get the InventoryService.
InventoryServiceInterface inventoryService =
dfpServices.get(session, InventoryServiceInterface.class);
// Create a statement to select ad units.
StatementBuilder statementBuilder = new StatementBuilder()
.where("id = :id")
.orderBy("id ASC")
.limit(StatementBuilder.SUGGESTED_PAGE_LIMIT)
.withBindVariableValue("id", adUnitId);
// 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 added to placement.\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 added to placement: %d\n", totalResultSetSize);
if (totalResultSetSize > 0) {
// Remove limit and offset from statement.
statementBuilder.removeLimitAndOffset();
// Create action.
com.google.api.ads.dfp.axis.v201308.AssignAdUnitsToPlacement action =
new com.google.api.ads.dfp.axis.v201308.AssignAdUnitsToPlacement();
action.setPlacementId(placementId);
// Perform action.
UpdateResult result =
inventoryService.performAdUnitAction(action, statementBuilder.toStatement());
if (result != null && result.getNumChanges() > 0) {
System.out.printf(
"Number of ad units to be added to placement: %d\n", result.getNumChanges());
} else {