use of com.google.api.ads.adwords.axis.utils.v201809.shopping.ProductPartitionNode in project googleads-java-lib by googleads.
the class ProductPartitionTreeImpl method addMutateOperations.
/**
* Adds to the operations list all operations required to mutate {@code originalNode} to the state
* of {@code newNode}.
*
* <p>The returned set of child product dimensions will only <em>potentially</em> be non-empty if
* both {@code originalNode != null} and {@code newNode != null}.
*
* @param originalNode may be null
* @param newNode may be null
* @param ops the operations list to add to
*
* @return the set of child product dimensions that require further processing
*/
private Set<ProductDimension> addMutateOperations(@Nullable ProductPartitionNode originalNode, @Nullable ProductPartitionNode newNode, List<OperationPair> ops) {
Set<ProductDimension> childDimensionsToProcess = Sets.newTreeSet(dimensionComparator);
NodeDifference nodeDifference = ProductPartitionNodeDiffer.diff(originalNode, newNode, dimensionComparator);
boolean isProcessChildren;
switch(nodeDifference) {
case NEW_NODE:
ops.addAll(createAddOperations(newNode));
// No need to further process children. The ADD operations above will include operations
// for all children of newNode.
isProcessChildren = false;
break;
case REMOVED_NODE:
ops.add(createRemoveOperation(originalNode));
// No need to further process children. The REMOVE operation above will perform a
// cascading delete of all children of newNode.
isProcessChildren = false;
break;
case PARTITION_TYPE_CHANGE:
case EXCLUDED_UNIT_CHANGE:
ops.add(createRemoveOperation(originalNode));
ops.addAll(createAddOperations(newNode));
// No need to further process children. The ADD operations above will include operations
// for all children of newNode.
isProcessChildren = false;
break;
case BIDDABLE_UNIT_CHANGE:
// Ensure that the new node has the proper ID (this may have been lost if the node
// was removed and then re-added).
newNode = newNode.setProductPartitionId(originalNode.getProductPartitionId());
ops.add(createSetBidOperation(newNode));
// Process the children of newNode. The SET operation above will only handle changes
// made to newNode, not its children.
isProcessChildren = true;
break;
case NONE:
// Ensure that the new node has the proper ID (this may have been lost if the node
// was removed and then re-added).
newNode = newNode.setProductPartitionId(originalNode.getProductPartitionId());
// This node does not have changes, but its children may.
isProcessChildren = true;
break;
default:
throw new IllegalStateException("Unrecognized difference: " + nodeDifference);
}
if (isProcessChildren) {
for (ProductPartitionNode childNode : Iterables.concat(originalNode.getChildren(), newNode.getChildren())) {
childDimensionsToProcess.add(childNode.getDimension());
}
}
return childDimensionsToProcess;
}
use of com.google.api.ads.adwords.axis.utils.v201809.shopping.ProductPartitionNode in project googleads-java-lib by googleads.
the class ProductPartitionNodeDifferTest method testFindNodeDifference_origNull.
/**
* Test for when the new node is not null and the original node is null.
*/
@Test
public void testFindNodeDifference_origNull() {
ProductPartitionNode newNode = new ProductPartitionNode(null, null, -1L, dimensionComparator);
NodeDifference diff = ProductPartitionNodeDiffer.diff(null, newNode, dimensionComparator);
assertEquals(NodeDifference.NEW_NODE, diff);
}
use of com.google.api.ads.adwords.axis.utils.v201809.shopping.ProductPartitionNode in project googleads-java-lib by googleads.
the class AddProductPartitionTree method runExample.
/**
* Runs the example.
*
* @param adWordsServices the services factory.
* @param session the session.
* @param adGroupId the ID of the ad group.
* @throws ApiException if the API request failed with one or more service errors.
* @throws RemoteException if the API request failed due to other errors.
*/
public static void runExample(AdWordsServicesInterface adWordsServices, AdWordsSession session, Long adGroupId) throws RemoteException {
// Build a new ProductPartitionTree using the ad group's current set of criteria.
ProductPartitionTree partitionTree = ProductPartitionTree.createAdGroupTree(adWordsServices, session, adGroupId);
System.out.printf("Original tree:%n%s%n", partitionTree);
// Clear out any existing criteria.
ProductPartitionNode rootNode = partitionTree.getRoot().removeAllChildren();
// Make the root node a subdivision.
rootNode = rootNode.asSubdivision();
// Add a unit node for condition = NEW.
rootNode.addChild(ProductDimensions.createCanonicalCondition(ProductCanonicalConditionCondition.NEW)).asBiddableUnit().setBid(200000L);
// Add a unit node for condition = USED.
rootNode.addChild(ProductDimensions.createCanonicalCondition(ProductCanonicalConditionCondition.USED)).asBiddableUnit().setBid(100000L);
// Add a subdivision node for condition = null (everything else).
ProductPartitionNode otherConditionNode = rootNode.addChild(ProductDimensions.createCanonicalCondition(null)).asSubdivision();
// Add a unit node under condition = null for brand = "CoolBrand".
otherConditionNode.addChild(ProductDimensions.createBrand("CoolBrand")).asBiddableUnit().setBid(900000L);
// Add a unit node under condition = null for brand = "CheapBrand".
otherConditionNode.addChild(ProductDimensions.createBrand("CheapBrand")).asBiddableUnit().setBid(10000L);
// Add a subdivision node under condition = null for brand = null (everything else).
ProductPartitionNode otherBrandNode = otherConditionNode.addChild(ProductDimensions.createBrand(null)).asSubdivision();
// Add unit nodes under condition = null/brand = null.
// The value for each bidding category is a fixed ID for a specific
// category. You can retrieve IDs for categories from the ConstantDataService.
// See the 'GetProductCategoryTaxonomy' example for more details.
// Add a unit node under condition = null/brand = null for product type
// level 1 = 'Luggage & Bags'.
otherBrandNode.addChild(ProductDimensions.createBiddingCategory(ProductDimensionType.BIDDING_CATEGORY_L1, -5914235892932915235L)).asBiddableUnit().setBid(750000L);
// Add a unit node under condition = null/brand = null for product type
// level 1 = null (everything else).
otherBrandNode.addChild(ProductDimensions.createBiddingCategory(ProductDimensionType.BIDDING_CATEGORY_L1, null)).asBiddableUnit().setBid(110000L);
// Get the ad group criterion service.
AdGroupCriterionServiceInterface adGroupCriterionService = adWordsServices.get(session, AdGroupCriterionServiceInterface.class);
// Make the mutate request, using the operations returned by the ProductPartitionTree.
List<AdGroupCriterionOperation> mutateOperations = partitionTree.getMutateOperations();
if (mutateOperations.isEmpty()) {
System.out.println("Skipping the mutate call because the original tree and the current tree " + "are logically identical.");
} else {
adGroupCriterionService.mutate(mutateOperations.toArray(new AdGroupCriterionOperation[0]));
}
// The request was successful, so create a new ProductPartitionTree based on the updated state
// of the ad group.
partitionTree = ProductPartitionTree.createAdGroupTree(adWordsServices, session, adGroupId);
// Show the tree
System.out.printf("Updated tree:%n%s%n", partitionTree);
}
use of com.google.api.ads.adwords.axis.utils.v201809.shopping.ProductPartitionNode in project googleads-java-lib by googleads.
the class ProductPartitionNodeDifferTest method testFindNodeDifference_neitherNull_logicallyEquivalent.
/**
* Test for when both nodes are not null and are logically equivalent.
*/
@Test
public void testFindNodeDifference_neitherNull_logicallyEquivalent() {
ProductPartitionNode origNode = new ProductPartitionNode(null, ProductDimensions.createOfferId("1234"), -1L, dimensionComparator).asSubdivision();
ProductPartitionNode newNode = new ProductPartitionNode(null, ProductDimensions.createOfferId("1234"), -1L, dimensionComparator).asSubdivision();
NodeDifference diff = ProductPartitionNodeDiffer.diff(origNode, newNode, dimensionComparator);
assertEquals(NodeDifference.NONE, diff);
}
use of com.google.api.ads.adwords.axis.utils.v201809.shopping.ProductPartitionNode in project googleads-java-lib by googleads.
the class ProductPartitionNodeDifferTest method testFindNodeDifference_newNull.
/**
* Test for when the new node is null and the original node is not null.
*/
@Test
public void testFindNodeDifference_newNull() {
ProductPartitionNode origNode = new ProductPartitionNode(null, null, -1L, dimensionComparator);
NodeDifference diff = ProductPartitionNodeDiffer.diff(origNode, null, dimensionComparator);
assertEquals(NodeDifference.REMOVED_NODE, diff);
}
Aggregations