public class CreateAdUnits {
public static void runExample(DfpServices dfpServices, DfpSession session, String parentId)
throws Exception {
// Get the InventoryService.
InventoryServiceInterface inventoryService =
dfpServices.get(session, InventoryServiceInterface.class);
// Create ad unit size.
Size size = new Size();
size.setWidth(300);
size.setHeight(250);
size.setIsAspectRatio(false);
AdUnitSize adUnitSize = new AdUnitSize();
adUnitSize.setSize(size);
adUnitSize.setEnvironmentType(EnvironmentType.BROWSER);
// Create a web ad unit.
AdUnit webAdUnit = new AdUnit();
webAdUnit.setName("Web_ad_unit_" + new Random().nextLong());
webAdUnit.setDescription(webAdUnit.getName());
webAdUnit.setTargetPlatform(TargetPlatform.WEB);
webAdUnit.setParentId(parentId);
webAdUnit.setTargetWindow(AdUnitTargetWindow.BLANK);
webAdUnit.setAdUnitSizes(new AdUnitSize[]{adUnitSize});
// Create a mobile ad unit.
AdUnit mobileAdUnit = new AdUnit();
mobileAdUnit.setName("Mobile_ad_unit_" + new Random().nextLong());
mobileAdUnit.setDescription(webAdUnit.getName());
mobileAdUnit.setTargetPlatform(TargetPlatform.MOBILE);
mobileAdUnit.setParentId(parentId);
mobileAdUnit.setTargetWindow(AdUnitTargetWindow.BLANK);
mobileAdUnit.setMobilePlatform(MobilePlatform.APPLICATION);
mobileAdUnit.setAdUnitSizes(new AdUnitSize[]{adUnitSize});
// Create the ad units on the server.
AdUnit[] adUnits = inventoryService.createAdUnits(new AdUnit[] {webAdUnit, mobileAdUnit});
for (AdUnit adUnit : adUnits) {
System.out.printf(
"An ad unit with ID \"%s\", name \"%s\", and target platform \"%s\" was created.\n",
adUnit.getId(), adUnit.getName(), adUnit.getTargetPlatform());