/// <summary>
        /// Creates the default partition.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        /// <param name="adGroupId">The ad group ID.</param>
        private void CreateDefaultPartitionTree(AdWordsUser user, long adGroupId)
        {
            using (AdGroupCriterionService adGroupCriterionService =
                       (AdGroupCriterionService)user.GetService(
                           AdWordsService.v201802.AdGroupCriterionService)) {
                // Build a new ProductPartitionTree using an empty set of criteria.
                ProductPartitionTree partitionTree =
                    ProductPartitionTree.CreateAdGroupTree(adGroupId, new List <AdGroupCriterion>());
                partitionTree.Root.AsBiddableUnit().CpcBid = 1000000;

                try {
                    // Make the mutate request, using the operations returned by the ProductPartitionTree.
                    AdGroupCriterionOperation[] mutateOperations = partitionTree.GetMutateOperations();

                    if (mutateOperations.Length == 0)
                    {
                        Console.WriteLine("Skipping the mutate call because the original tree and the " +
                                          "current tree are logically identical.");
                    }
                    else
                    {
                        adGroupCriterionService.mutate(mutateOperations);
                    }

                    // The request was successful, so create a new ProductPartitionTree based on the updated
                    // state of the ad group.
                    partitionTree = ProductPartitionTree.DownloadAdGroupTree(user, adGroupId);

                    Console.WriteLine("Final tree: {0}", partitionTree);
                } catch (Exception e) {
                    throw new System.ApplicationException("Failed to set shopping product partition.", e);
                }
            }
        }
        /// <summary>
        /// Executes the tree operations.
        /// </summary>
        /// <returns>The synced product partition tree.</returns>
        private ProductPartitionTree ExecuteTreeOperations()
        {
            AdGroupCriterionOperation[] operations = tree.GetMutateOperations();
            AdGroupCriterionService     service    = (AdGroupCriterionService)user.GetService(
                AdWordsService.v201806.AdGroupCriterionService);

            service.mutate(operations);
            return(ProductPartitionTree.DownloadAdGroupTree(user, ADGROUP_ID));
        }
        /// <summary>
        /// Creates a product partition tree.
        /// </summary>
        /// <param name="user">The AdWords user for which the product partition is created.</param>
        /// <param name="adGroupId">Ad group ID.</param>
        /// <returns>The product partition.</returns>
        private static ProductPartitionTree CreateProductPartition(AdWordsUser user, long adGroupId)
        {
            using (AdGroupCriterionService adGroupCriterionService =
                       (AdGroupCriterionService)user.GetService(AdWordsService.v201809
                                                                .AdGroupCriterionService))
            {
                // Build a new ProductPartitionTree using the ad group's current set of criteria.
                ProductPartitionTree partitionTree =
                    ProductPartitionTree.DownloadAdGroupTree(user, adGroupId);

                Console.WriteLine("Original tree: {0}", partitionTree);

                // Clear out any existing criteria.
                ProductPartitionNode rootNode = partitionTree.Root.RemoveAllChildren();

                // Make the root node a subdivision.
                rootNode = rootNode.AsSubdivision();

                // Add a unit node for condition = NEW to include it.
                rootNode.AddChild(
                    ProductDimensions.CreateCanonicalCondition(ProductCanonicalConditionCondition
                                                               .NEW));

                // Add a unit node for condition = USED to include it.
                rootNode.AddChild(
                    ProductDimensions.CreateCanonicalCondition(ProductCanonicalConditionCondition
                                                               .USED));

                // Exclude everything else.
                rootNode.AddChild(ProductDimensions.CreateCanonicalCondition()).AsExcludedUnit();

                // Make the mutate request, using the operations returned by the
                // ProductPartitionTree.
                AdGroupCriterionOperation[] mutateOperations = partitionTree.GetMutateOperations();

                if (mutateOperations.Length == 0)
                {
                    Console.WriteLine(
                        "Skipping the mutate call because the original tree and the current " +
                        "tree are logically identical.");
                }
                else
                {
                    adGroupCriterionService.mutate(mutateOperations);
                }

                // The request was successful, so create a new ProductPartitionTree based on the
                // updated state of the ad group.
                partitionTree = ProductPartitionTree.DownloadAdGroupTree(user, adGroupId);
                return(partitionTree);
            }
        }
        public void TestCreateMultiNodeTreeFromScratch()
        {
            ProductPartitionTree tree =
                ProductPartitionTree.CreateAdGroupTree(new List <AdGroupCriterion>());

            ProductPartitionNode rootNode = tree.Root.AsSubdivision();
            ProductPartitionNode brand1   = rootNode.AddChild(ProductDimensions.CreateBrand("google"))
                                            .AsSubdivision();
            ProductPartitionNode brand1Offer1 =
                brand1.AddChild(ProductDimensions.CreateOfferId("A"));

            brand1Offer1.AsBiddableUnit().CpcBid = 1000000L;
            ProductPartitionNode brand1Offer2    =
                brand1.AddChild(ProductDimensions.CreateOfferId()).AsExcludedUnit();
            ProductPartitionNode brand2 =
                rootNode.AddChild(ProductDimensions.CreateBrand()).AsExcludedUnit();

            ProductPartitionNode[] nodes = new ProductPartitionNode[]
            {
                rootNode,
                brand1,
                brand1Offer1,
                brand1Offer2,
                brand2
            };

            AdGroupCriterionOperation[] mutateOperations = tree.GetMutateOperations();

            for (int i = 0; i < nodes.Length; i++)
            {
                List <AdGroupCriterionOperation> nodeOperations =
                    shoppingTestUtils.GetOperationsForNode(nodes[i], mutateOperations);
                Assert.That(nodeOperations.Count == 1);
                Assert.That(nodeOperations[0].@operator == Operator.ADD);
                ProductPartition partition = (ProductPartition)nodeOperations[0].operand.criterion;
                Assert.That(partition.id == nodes[i].ProductPartitionId);
                if (nodes[i].Parent == null)
                {
                    Assert.That(partition.parentCriterionIdSpecified == false);
                }
                else
                {
                    Assert.That(partition.parentCriterionId == nodes[i].Parent.ProductPartitionId);
                }

                Assert.That(partition.caseValue == nodes[i].Dimension);
            }
        }
        public void TestDownloadAdGroupTree()
        {
            ProductPartitionTree partitionTree = shoppingTestUtils.CreateTestTreeForAddition(ADGROUP_ID);

            AdGroupCriterionOperation[] operations = partitionTree.GetMutateOperations();
            AdGroupCriterionService     adGroupCriterionService =
                (AdGroupCriterionService)user.GetService(
                    AdWordsService.v201607.AdGroupCriterionService);

            AdGroupCriterionReturnValue value = adGroupCriterionService.mutate(operations);

            ProductPartitionTree newPartitionTree = ProductPartitionTree.DownloadAdGroupTree(user,
                                                                                             ADGROUP_ID);

            CompareTree(newPartitionTree.Root, partitionTree.Root);
        }
        public void TestCreateUltimatelyEmptyTree()
        {
            ProductPartitionTree tree =
                ProductPartitionTree.CreateAdGroupTree(new List <AdGroupCriterion>());

            ProductPartitionNode rootNode = tree.Root.AsSubdivision();
            ProductPartitionNode brand1   = rootNode.AddChild(ProductDimensions.CreateBrand("google"))
                                            .AsSubdivision();
            ProductPartitionNode offerNode = brand1.AddChild(ProductDimensions.CreateOfferId("A"));

            offerNode.AsBiddableUnit().CpcBid = 1000000L;

            brand1.AddChild(ProductDimensions.CreateOfferId()).AsExcludedUnit();
            ProductPartitionNode brand2 =
                rootNode.AddChild(ProductDimensions.CreateBrand()).AsExcludedUnit();

            // Now remove the two child nodes under the root and set the root back
            // to a UNIT. This should result in operations that simply create the
            // root node.
            rootNode.RemoveChild(brand1.Dimension);
            rootNode.RemoveChild(brand2.Dimension);
            rootNode = rootNode.AsBiddableUnit();

            AdGroupCriterionOperation[] mutateOperations = tree.GetMutateOperations();

            Assert.AreEqual(mutateOperations.Count(), 1, "Number of operations is incorrect.");
            AdGroupCriterionOperation operation = mutateOperations[0];

            Assert.AreEqual(Operator.ADD, operation.@operator,
                            "Should have a single operation to ADD the root node.");
            BiddableAdGroupCriterion adGroupCriterion =
                (BiddableAdGroupCriterion)operation.operand;

            Assert.Null(((ProductPartition)adGroupCriterion.criterion).caseValue,
                        "Product dimension of operation's operand should be null.");
            Assert.True(adGroupCriterion.criterion.id < 0L,
                        "Partition ID of the operand should be negative.");
        }
        public void TestCreateEmptyTree()
        {
            ProductPartitionTree tree = ProductPartitionTree.CreateAdGroupTree(
                new List <AdGroupCriterion>());

            Assert.NotNull(tree.Root, "Even an empty tree should automatically have a root node.");
            Assert.True(tree.Root.ProductPartitionId < 0L,
                        "The root node for an empty tree should have a negative (temporary) ID.");
            Assert.True(tree.Root.IsUnit, "The root node for an empty tree should be a UNIT.");

            AdGroupCriterionOperation[] mutateOperations = tree.GetMutateOperations();

            Assert.That(mutateOperations.Count() == 1, "Number of operations is incorrect.");
            AdGroupCriterionOperation operation = mutateOperations[0];

            Assert.AreEqual(Operator.ADD, operation.@operator,
                            "Should have a single operation to ADD the root node.");
            BiddableAdGroupCriterion adGroupCriterion = (BiddableAdGroupCriterion)operation.operand;

            Assert.Null(((ProductPartition)adGroupCriterion.criterion).caseValue,
                        "Product dimension of operation's operand should be null.");
            Assert.True(adGroupCriterion.criterion.id < 0L);
        }
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        /// <param name="adGroupId">The ad group to which product partition is
        /// added.</param>
        public void Run(AdWordsUser user, long adGroupId)
        {
            using (AdGroupCriterionService adGroupCriterionService =
                       (AdGroupCriterionService)user.GetService(
                           AdWordsService.v201802.AdGroupCriterionService)) {
                // Build a new ProductPartitionTree using the ad group's current set of criteria.
                ProductPartitionTree partitionTree =
                    ProductPartitionTree.DownloadAdGroupTree(user, adGroupId);

                Console.WriteLine("Original tree: {0}", partitionTree);

                // Clear out any existing criteria.
                ProductPartitionNode rootNode = partitionTree.Root.RemoveAllChildren();

                // Make the root node a subdivision.
                rootNode = rootNode.AsSubdivision();

                // Add a unit node for condition = NEW.
                ProductPartitionNode newConditionNode = rootNode.AddChild(
                    ProductDimensions.CreateCanonicalCondition(ProductCanonicalConditionCondition.NEW));
                newConditionNode.AsBiddableUnit().CpcBid = 200000;

                ProductPartitionNode usedConditionNode = rootNode.AddChild(
                    ProductDimensions.CreateCanonicalCondition(ProductCanonicalConditionCondition.USED));
                usedConditionNode.AsBiddableUnit().CpcBid = 100000;

                // Add a subdivision node for condition = null (everything else).
                ProductPartitionNode otherConditionNode =
                    rootNode.AddChild(ProductDimensions.CreateCanonicalCondition()).AsSubdivision();

                // Add a unit node under condition = null for brand = "CoolBrand".
                ProductPartitionNode coolBrandNode = otherConditionNode.AddChild(
                    ProductDimensions.CreateBrand("CoolBrand"));
                coolBrandNode.AsBiddableUnit().CpcBid = 900000L;

                // Add a unit node under condition = null for brand = "CheapBrand".
                ProductPartitionNode cheapBrandNode = otherConditionNode.AddChild(
                    ProductDimensions.CreateBrand("CheapBrand"));
                cheapBrandNode.AsBiddableUnit().CpcBid = 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'.
                ProductPartitionNode luggageAndBagNode = otherBrandNode.AddChild(
                    ProductDimensions.CreateBiddingCategory(ProductDimensionType.BIDDING_CATEGORY_L1,
                                                            -5914235892932915235L));
                luggageAndBagNode.AsBiddableUnit().CpcBid = 750000L;

                // Add a unit node under condition = null/brand = null for product type
                // level 1 = null (everything else).
                ProductPartitionNode everythingElseNode = otherBrandNode.AddChild(
                    ProductDimensions.CreateBiddingCategory(ProductDimensionType.BIDDING_CATEGORY_L1));
                everythingElseNode.AsBiddableUnit().CpcBid = 110000L;

                try {
                    // Make the mutate request, using the operations returned by the ProductPartitionTree.
                    AdGroupCriterionOperation[] mutateOperations = partitionTree.GetMutateOperations();

                    if (mutateOperations.Length == 0)
                    {
                        Console.WriteLine("Skipping the mutate call because the original tree and the " +
                                          "current tree are logically identical.");
                    }
                    else
                    {
                        adGroupCriterionService.mutate(mutateOperations);
                    }

                    // The request was successful, so create a new ProductPartitionTree based on the updated
                    // state of the ad group.
                    partitionTree = ProductPartitionTree.DownloadAdGroupTree(user, adGroupId);

                    Console.WriteLine("Final tree: {0}", partitionTree);
                } catch (Exception e) {
                    throw new System.ApplicationException("Failed to set shopping product partition.", e);
                }
            }
        }
        public void TestMutateMultiNodeTree()
        {
            ProductPartitionTree tree = shoppingTestUtils.CreateTestTreeForTransformation(ADGROUP_ID);

            Assert.AreEqual(ADGROUP_ID, tree.AdGroupId, "ad group ID is incorrect");

            // Change the bids on leaf nodes.
            ProductPartitionNode brandGoogleNode = tree.Root.GetChild(shoppingTestUtils.BRAND_GOOGLE);
            ProductPartitionNode offerANode      = brandGoogleNode.GetChild(shoppingTestUtils.OFFER_A);

            // This should produce 1 SET operation.
            offerANode.CpcBid = offerANode.CpcBid * 10;

            // Offer B is changed from Exclude to Biddable. This should produce 1
            // REMOVE operation + 1 ADD operation.
            ProductPartitionNode offerBNode = brandGoogleNode.GetChild(shoppingTestUtils.OFFER_B);

            offerBNode.AsBiddableUnit().CpcBid = 5000000L;

            // Other Brand node is changed from Exclude to Biddable. This should
            // produce 1 REMOVE operation + 1 ADD operation.
            ProductPartitionNode brandOtherNode = tree.Root.GetChild(shoppingTestUtils.BRAND_OTHER);

            brandOtherNode = brandOtherNode.AsBiddableUnit();

            // Add an offer C node. This should produce 1 ADD operation.
            ProductPartitionNode offerCNode = brandGoogleNode.AddChild(shoppingTestUtils.OFFER_C);

            offerCNode.AsBiddableUnit().CpcBid = 1500000L;

            // Remove the brand Motorola node. This should produce 1 REMOVE operation.
            ProductPartitionNode brandMotorolaNode = tree.Root.GetChild(
                shoppingTestUtils.BRAND_MOTOROLA);

            tree.Root.RemoveChild(shoppingTestUtils.BRAND_MOTOROLA);

            // Get the mutate operations generated by the modifications made to the tree.
            AdGroupCriterionOperation[] mutateOperations = tree.GetMutateOperations();
            Assert.AreEqual(7, mutateOperations.Length);

            List <AdGroupCriterionOperation> operations = null;

            // Since Offer A node only has modified attributes, there should only be
            // one SET operation.
            operations = shoppingTestUtils.GetOperationsForNode(offerANode, mutateOperations);
            Assert.That(operations.Count == 1);
            Assert.That(operations[0].@operator == Operator.SET);

            // Since Offer B node is being converted from Exclude to Biddable node,
            // there should be one REMOVE operation, and another ADD operation.
            operations = shoppingTestUtils.GetOperationsForNode(offerBNode, mutateOperations);
            Assert.That(operations.Count == 2);
            Assert.That(operations[0].@operator == Operator.REMOVE);
            Assert.That(operations[1].@operator == Operator.ADD);

            // Since Offer C node is being added, there should be one ADD operation.
            operations = shoppingTestUtils.GetOperationsForNode(offerCNode, mutateOperations);
            Assert.That(operations.Count == 1);
            Assert.That(operations[0].@operator == Operator.ADD);

            // Since Other Brand node is being converted from Exclude to Biddable node,
            // there should be one REMOVE operation, and another ADD operation.
            operations = shoppingTestUtils.GetOperationsForNode(brandOtherNode, mutateOperations);
            Assert.That(operations.Count == 2);
            Assert.That(operations[0].@operator == Operator.REMOVE);
            Assert.That(operations[1].@operator == Operator.ADD);

            // Since Offer B node is being removed, there should be one REMOVE
            // operation.
            operations = shoppingTestUtils.GetOperationsForNode(brandMotorolaNode, mutateOperations);
            Assert.That(operations.Count == 1);
            Assert.That(operations[0].@operator == Operator.REMOVE);
        }