public static void runExample(DfpServices dfpServices, DfpSession session, long orderId,
String targetedVideoAdUnitId, long contentCustomTargetingKeyId,
long contentCustomTargetingValueId) throws Exception {
// Get the LineItemService.
LineItemServiceInterface lineItemService =
dfpServices.get(session, LineItemServiceInterface.class);
// 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 #" + new Random().nextInt(Integer.MAX_VALUE));
lineItem.setOrderId(orderId);
lineItem.setTargeting(targeting);
// Allow the line item to be booked even if there is not enough inventory.
lineItem.setAllowOverbook(true);
// Set the line item type to SPONSORSHIP.
lineItem.setLineItemType(LineItemType.SPONSORSHIP);
// 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(640, 360, false));
// Create companion creative placeholders.
CreativePlaceholder companionCreativePlaceholder = new CreativePlaceholder();
companionCreativePlaceholder.setSize(new Size(300, 250, false));
// Set companion creative placeholders.
creativeMasterPlaceholder.setCompanions(
new CreativePlaceholder[] {companionCreativePlaceholder});
// 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(
DateTimes.toDateTime(Instant.now().plus(Duration.standardDays(30L)), "America/New_York"));
// 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[] lineItems = lineItemService.createLineItems(new LineItem[] {lineItem});
for (LineItem createdLineItem : lineItems) {
System.out.printf("A video line item with ID \"%d\" and name \"%s\" was created.\n",
createdLineItem.getId(), createdLineItem.getName());
}