// Get DfpUser from "~/dfp.properties".
DfpUser user = new DfpUser();
// Get the LineItemService.
LineItemServiceInterface lineItemService =
user.getService(DfpService.V201311.LINEITEM_SERVICE);
// Set the order that all created line items will belong to and the
// video ad unit ID to target.
Long orderId = Long.parseLong("INSERT_ORDER_ID_HERE");
String targetedVideoAdUnitId = "INSERT_TARGETED_VIDEO_AD_UNIT_ID_HERE";
// Set the custom targeting key ID and value ID representing the metadata
// on the content to target. This would typically be a key representing
// a "genre" and a value representing something like "comedy".
Long contentCustomTargetingKeyId =
Long.parseLong("INSERT_CONTENT_CUSTOM_TARGETING_KEY_ID_HERE");
Long contentCustomTargetingValueId =
Long.parseLong("INSERT_CONTENT_CUSTOM_TARGETING_VALUE_ID_HERE");
// Create custom criteria for the content metadata targeting.
CustomCriteria contentCustomCriteria = new CustomCriteria();
contentCustomCriteria.setKeyId(contentCustomTargetingKeyId);
contentCustomCriteria.setValueIds(new long[] {contentCustomTargetingValueId});
contentCustomCriteria.setOperator(CustomCriteriaComparisonOperator.IS);
// Create custom criteria set.
CustomCriteriaSet customCriteriaSet = new CustomCriteriaSet();
customCriteriaSet.setChildren(new CustomCriteriaNode[] {contentCustomCriteria});
// Create inventory targeting.
InventoryTargeting inventoryTargeting = new InventoryTargeting();
inventoryTargeting.setTargetedAdUnits(
new AdUnitTargeting[] {new AdUnitTargeting(targetedVideoAdUnitId, true)});
// Create video position targeting.
VideoPosition videoPosition = new VideoPosition();
videoPosition.setPositionType(VideoPositionType.PREROLL);
VideoPositionTarget videoPositionTarget = new VideoPositionTarget();
videoPositionTarget.setVideoPosition(videoPosition);
VideoPositionTargeting videoPositionTargeting = new VideoPositionTargeting();
videoPositionTargeting.setTargetedPositions(
new VideoPositionTarget[] {videoPositionTarget});
// Create targeting.
Targeting targeting = new Targeting();
targeting.setCustomTargeting(customCriteriaSet);
targeting.setInventoryTargeting(inventoryTargeting);
targeting.setVideoPositionTargeting(videoPositionTargeting);
// Create local line item object.
LineItem lineItem = new LineItem();
lineItem.setName("Video line item");
lineItem.setOrderId(orderId);
lineItem.setTargeting(targeting);
lineItem.setLineItemType(LineItemType.SPONSORSHIP);
lineItem.setAllowOverbook(true);
// Set the environment type to video.
lineItem.setEnvironmentType(EnvironmentType.VIDEO_PLAYER);
// Set the creative rotation type to optimized.
lineItem.setCreativeRotationType(CreativeRotationType.OPTIMIZED);
// Create the master creative placeholder.
CreativePlaceholder creativeMasterPlaceholder = new CreativePlaceholder();
creativeMasterPlaceholder.setSize(new Size(400, 300, false));
// Create companion creative placeholders.
CreativePlaceholder companionCreativePlaceholder1 = new CreativePlaceholder();
companionCreativePlaceholder1.setSize(new Size(300, 250, false));
CreativePlaceholder companionCreativePlaceholder2 = new CreativePlaceholder();
companionCreativePlaceholder2.setSize(new Size(728, 90, false));
// Set companion creative placeholders.
creativeMasterPlaceholder.setCompanions(new CreativePlaceholder[] {
companionCreativePlaceholder1, companionCreativePlaceholder2});
// Set the size of creatives that can be associated with this line item.
lineItem.setCreativePlaceholders(new CreativePlaceholder[] {creativeMasterPlaceholder});
// Set delivery of video companions to optional.
lineItem.setCompanionDeliveryOption(CompanionDeliveryOption.OPTIONAL);
// Set the length of the line item to run.
lineItem.setStartDateTimeType(StartDateTimeType.IMMEDIATELY);
lineItem.setEndDateTime(DateTimeUtils.fromString("2013-09-01T00:00:00"));
// Set the cost per day to $1.
lineItem.setCostType(CostType.CPD);
lineItem.setCostPerUnit(new Money("USD", 1000000L));
// Set the percentage to be 100%.
lineItem.setUnitsBought(100L);
// Create the line item on the server.
lineItem = lineItemService.createLineItem(lineItem);
if (lineItem != null) {
System.out.println("A line item with ID \"" + lineItem.getId()
+ "\", belonging to order ID \"" + lineItem.getOrderId() + "\", and named \""
+ lineItem.getName() + "\" was created.");