// Get DfpUser from "~/dfp.properties".
DfpUser user = new DfpUser();
// Get the LineItemService.
LineItemServiceInterface lineItemService =
user.getService(DfpService.V201208.LINEITEM_SERVICE);
// Get the CustomTargetingService.
CustomTargetingServiceInterface customTargetingService =
user.getService(DfpService.V201208.CUSTOM_TARGETING_SERVICE);
Long lineItemId = Long.parseLong("INSERT_LINE_ITEM_ID_HERE");
Long[] customCriteriaIds1 =
new Long[] {Long.parseLong("INSERT_CUSTOM_TARGETING_KEY_ID_HERE"),
Long.parseLong("INSERT_CUSTOM_TARGETING_VALUE_ID_HERE")};
Long[] customCriteriaIds2 =
new Long[] {Long.parseLong("INSERT_CUSTOM_TARGETING_KEY_ID_HERE"),
Long.parseLong("INSERT_CUSTOM_TARGETING_VALUE_ID_HERE")};
Long[] customCriteriaIds3 =
new Long[] {Long.parseLong("INSERT_CUSTOM_TARGETING_KEY_ID_HERE"),
Long.parseLong("INSERT_CUSTOM_TARGETING_VALUE_ID_HERE")};
// Create custom criteria.
CustomCriteria customCriteria1 = new CustomCriteria();
customCriteria1.setKeyId(customCriteriaIds1[0]);
customCriteria1.setValueIds(new long[] {customCriteriaIds1[1]});
customCriteria1.setOperator(CustomCriteriaComparisonOperator.IS);
CustomCriteria customCriteria2 = new CustomCriteria();
customCriteria2.setKeyId(customCriteriaIds2[0]);
customCriteria2.setValueIds(new long[] {customCriteriaIds2[1]});
customCriteria2.setOperator(CustomCriteriaComparisonOperator.IS_NOT);
CustomCriteria customCriteria3 = new CustomCriteria();
customCriteria3.setKeyId(customCriteriaIds3[0]);
customCriteria3.setValueIds(new long[] {customCriteriaIds3[1]});
customCriteria3.setOperator(CustomCriteriaComparisonOperator.IS);
// Create the custom criteria set that will resemble:
//
// (customCriteria1.key == customCriteria1.value OR
// (customCriteria2.key != customCriteria2.value AND
// customCriteria3.key == customCriteria3.value))
CustomCriteriaSet topCustomCriteriaSet = new CustomCriteriaSet();
topCustomCriteriaSet.setLogicalOperator(CustomCriteriaSetLogicalOperator.OR);
CustomCriteriaSet subCustomCriteriaSet = new CustomCriteriaSet();
subCustomCriteriaSet.setLogicalOperator(CustomCriteriaSetLogicalOperator.AND);
subCustomCriteriaSet.setChildren(
new CustomCriteriaNode[] {customCriteria2, customCriteria3});
topCustomCriteriaSet.setChildren(
new CustomCriteriaNode[] {customCriteria1, subCustomCriteriaSet});
// Set the custom criteria targeting on the line item.
LineItem lineItem = lineItemService.getLineItem(lineItemId);
lineItem.getTargeting().setCustomTargeting(topCustomCriteriaSet);
// Update the line items on the server.
lineItem = lineItemService.updateLineItem(lineItem);
// Display the updated line item.
System.out.printf("Line item with ID %s update with custom criteria targeting \n%s\n",
lineItem.getId(),
getCustomCriteriaSetString(lineItem.getTargeting().getCustomTargeting(), 0));