adUnitTargeting.setIncludeDescendants(true);
inventoryTargeting.setTargetedAdUnits(new AdUnitTargeting[] {adUnitTargeting});
// Create targeting.
Targeting targeting = new Targeting();
targeting.setInventoryTargeting(inventoryTargeting);
// Create the expression:
//
// CUSTOM_TARGETING_KEY_ID_1 == CUSTOM_TARGETING_VALUE_ID_1
CustomCriteria customCriteria1 = new CustomCriteria();
customCriteria1.setKeyId(customTargetingKeyId1);
customCriteria1.setOperator(CustomCriteriaComparisonOperator.IS);
customCriteria1.setValueIds(new long[] {customTargetingValueId1});
// Create the expression:
//
// CUSTOM_TARGETING_KEY_ID_2 !=
// (CUSTOM_TARGETING_VALUE_ID_2 OR CUSTOM_TARGETING_VALUE_ID_3)
CustomCriteria customCriteria2 = new CustomCriteria();
customCriteria2.setKeyId(customTargetingKeyId2);
customCriteria2.setOperator(CustomCriteriaComparisonOperator.IS_NOT);
customCriteria2.setValueIds(new long[] {customTargetingValueId2, customTargetingValueId3});
// Create the expression:
//
// CUSTOM_TARGETING_KEY_ID_3 = CUSTOM_TARGETING_VALUE_ID_4
CustomCriteria customCriteria3 = new CustomCriteria();
customCriteria3.setKeyId(customTargetingKeyId3);
customCriteria3.setOperator(CustomCriteriaComparisonOperator.IS);
customCriteria3.setValueIds(new long[] {customTargetingValueId4});
// Create the custom criteria set that will resemble:
//
// (CUSTOM_TARGETING_KEY_ID_1 == CUSTOM_TARGETING_VALUE_ID_1 AND
// (CUSTOM_TARGETING_KEY_ID_2 !=
// (CUSTOM_TARGETING_VALUE_ID_2 OR CUSTOM_TARGETING_VALUE_ID_3))
// OR
// (CUSTOM_TARGETING_KEY_ID_3 = CUSTOM_TARGETING_VALUE_ID_4)
CustomCriteriaSet topCustomCriteriaSet = new CustomCriteriaSet();
topCustomCriteriaSet.setLogicalOperator(CustomCriteriaSetLogicalOperator.OR);
// Create the sub expression:
//
// (CUSTOM_TARGETING_KEY_ID_1 == CUSTOM_TARGETING_VALUE_ID_1 AND
// (CUSTOM_TARGETING_KEY_ID_2 !=
// (CUSTOM_TARGETING_VALUE_ID_2 OR CUSTOM_TARGETING_VALUE_ID_3))
CustomCriteriaSet subCustomCriteriaSet = new CustomCriteriaSet();
subCustomCriteriaSet.setLogicalOperator(CustomCriteriaSetLogicalOperator.AND);
subCustomCriteriaSet.setChildren(
new CustomCriteriaNode[] {customCriteria1, customCriteria2});
// Combine the expression
// (CUSTOM_TARGETING_KEY_ID_3 = CUSTOM_TARGETING_VALUE_ID_4) with
// subCustomCriteriaSet.
topCustomCriteriaSet.setChildren(
new CustomCriteriaNode[] {subCustomCriteriaSet, customCriteria3});
// Set the custom targeting.
targeting.setCustomTargeting(topCustomCriteriaSet);
// Create a line item.
LineItem lineItem = new LineItem();
lineItem.setName("Line item #" + new Random().nextInt(Integer.MAX_VALUE));
lineItem.setOrderId(orderId);