// Get the InventoryService.
InventoryServiceInterface inventoryService =
user.getService(DfpService.V201211.INVENTORY_SERVICE);
// Get the PlacementService.
PlacementServiceInterface placementService =
user.getService(DfpService.V201211.PLACEMENT_SERVICE);
// Create local placement object to store skyscraper ad units.
Placement skyscraperAdUnitPlacement = new Placement();
skyscraperAdUnitPlacement.setName("Skyscraper AdUnit Placement #"
+ System.currentTimeMillis());
skyscraperAdUnitPlacement.setDescription(
"Contains ad units that can hold creatives of size 120x600");
skyscraperAdUnitPlacement.setTargetedAdUnitIds(new String[] {});
// Create local placement object to store medium square ad units.
Placement mediumSquareAdUnitPlacement = new Placement();
mediumSquareAdUnitPlacement.setName("Medium Square AdUnit Placement #"
+ System.currentTimeMillis());
mediumSquareAdUnitPlacement.setDescription(
"Contains ad units that can hold creatives of size 300x250");
mediumSquareAdUnitPlacement.setTargetedAdUnitIds(new String[] {});
// Create local placement object to store banner ad units.
Placement bannerAdUnitPlacement = new Placement();
bannerAdUnitPlacement.setName("Banner AdUnit Placement #"
+ System.currentTimeMillis());
bannerAdUnitPlacement.setDescription(
"Contains ad units that can hold creatives of size 468x60");
bannerAdUnitPlacement.setTargetedAdUnitIds(new String[] {});
List<Placement> placementList = new ArrayList<Placement>();
// Get the first 500 ad units.
AdUnitPage page = inventoryService.getAdUnitsByStatement(new Statement("LIMIT 500", null));
// Separate the ad units by size.
if (page.getResults() != null) {
for (AdUnit adUnit : page.getResults()) {
if (adUnit.getParentId() != null && adUnit.getAdUnitSizes() != null) {
for (AdUnitSize adUnitSize : adUnit.getAdUnitSizes()) {
Size size = adUnitSize.getSize();
if (size.getWidth() == 300 && size.getHeight() == 250) {
Set<String> adUnitIds =
new HashSet<String>(Arrays.asList(
mediumSquareAdUnitPlacement.getTargetedAdUnitIds()));
adUnitIds.add(adUnit.getId());
mediumSquareAdUnitPlacement.setTargetedAdUnitIds(
adUnitIds.toArray(new String[] {}));
} else if (size.getWidth() == 120 && size.getHeight() == 600) {
Set<String> adUnitIds =
new HashSet<String>(Arrays.asList(
skyscraperAdUnitPlacement.getTargetedAdUnitIds()));
adUnitIds.add(adUnit.getId());
skyscraperAdUnitPlacement.setTargetedAdUnitIds(adUnitIds.toArray(new String[] {}));
} else if (size.getWidth() == 468 && size.getHeight() == 60) {
Set<String> adUnitIds =
new HashSet<String>(
Arrays.asList(bannerAdUnitPlacement.getTargetedAdUnitIds()));
adUnitIds.add(adUnit.getId());
bannerAdUnitPlacement.setTargetedAdUnitIds(adUnitIds.toArray(new String[] {}));
}
}
}
}
}
// Only create placements with one or more ad unit.
if (mediumSquareAdUnitPlacement.getTargetedAdUnitIds().length != 0) {
placementList.add(mediumSquareAdUnitPlacement);
}
if (skyscraperAdUnitPlacement.getTargetedAdUnitIds().length != 0) {
placementList.add(skyscraperAdUnitPlacement);
}
if (bannerAdUnitPlacement.getTargetedAdUnitIds().length != 0) {
placementList.add(bannerAdUnitPlacement);
}
// Create the placements on the server.
Placement[] placements =
placementService.createPlacements(placementList.toArray(new Placement[] {}));
// Display results.
if (placements != null) {
for (Placement placement : placements) {
System.out.println("A placement with ID \"" + placement.getId() + "\", name \""