use of com.google.api.ads.adwords.axis.v201809.cm.ProductPartition in project googleads-java-lib by googleads.
the class ProductPartitionNodeAdapter method createCriterionForAdd.
/**
* Returns a new AdGroupCriterion configured for an ADD operation.
*
* @param node the node whose criterion should be added
* @param adGroupId the ad group ID of the criterion
* @param biddingConfig the bidding strategy configuration of the criterion
*/
static AdGroupCriterion createCriterionForAdd(ProductPartitionNode node, long adGroupId, BiddingStrategyConfiguration biddingConfig) {
Preconditions.checkNotNull(node, "Null node");
Preconditions.checkNotNull(biddingConfig, "Null bidding configuration");
AdGroupCriterion adGroupCriterion;
if (node.isExcludedUnit()) {
adGroupCriterion = new NegativeAdGroupCriterion();
} else if (node.isBiddableUnit()) {
BiddableAdGroupCriterion biddableCriterion = new BiddableAdGroupCriterion();
if (node.getBid() != null) {
Money bidMoney = new Money();
bidMoney.setMicroAmount(node.getBid());
CpcBid cpcBid = new CpcBid();
cpcBid.setBid(bidMoney);
cpcBid.setCpcBidSource(BidSource.CRITERION);
biddingConfig.setBids(new Bids[] { cpcBid });
biddableCriterion.setBiddingStrategyConfiguration(biddingConfig);
}
if (node.getTrackingUrlTemplate() != null) {
biddableCriterion.setTrackingUrlTemplate(node.getTrackingUrlTemplate());
}
biddableCriterion.setUrlCustomParameters(createCustomParameters(node));
adGroupCriterion = biddableCriterion;
} else {
adGroupCriterion = new BiddableAdGroupCriterion();
}
adGroupCriterion.setAdGroupId(adGroupId);
ProductPartition partition = new ProductPartition();
partition.setId(node.getProductPartitionId());
if (node.getParent() != null) {
partition.setParentCriterionId(node.getParent().getProductPartitionId());
}
partition.setCaseValue(node.getDimension());
partition.setPartitionType(node.isUnit() ? ProductPartitionType.UNIT : ProductPartitionType.SUBDIVISION);
adGroupCriterion.setCriterion(partition);
return adGroupCriterion;
}
use of com.google.api.ads.adwords.axis.v201809.cm.ProductPartition in project googleads-java-lib by googleads.
the class ProductPartitionTreeImpl method createAdGroupTree.
/**
* Returns a new instance of this class by retrieving the product partitions of the
* specified ad group. All parameters are required.
*/
static ProductPartitionTreeImpl createAdGroupTree(AdWordsServicesInterface services, AdWordsSession session, Long adGroupId) throws ApiException, RemoteException {
// Get the AdGroupCriterionService.
AdGroupCriterionServiceInterface criterionService = services.get(session, AdGroupCriterionServiceInterface.class);
SelectorBuilder selectorBuilder = new SelectorBuilder().fields(REQUIRED_SELECTOR_FIELD_ENUMS.toArray(new AdGroupCriterionField[REQUIRED_SELECTOR_FIELD_ENUMS.size()])).equals(AdGroupCriterionField.AdGroupId, adGroupId.toString()).equals(AdGroupCriterionField.CriteriaType, "PRODUCT_PARTITION").in(AdGroupCriterionField.Status, UserStatus.ENABLED.getValue(), UserStatus.PAUSED.getValue()).limit(PAGE_SIZE);
AdGroupCriterionPage adGroupCriterionPage;
// A multimap from each product partition ID to its direct children.
ListMultimap<Long, AdGroupCriterion> parentIdMap = LinkedListMultimap.create();
int offset = 0;
do {
// Get the next page of results.
adGroupCriterionPage = criterionService.get(selectorBuilder.build());
if (adGroupCriterionPage != null && adGroupCriterionPage.getEntries() != null) {
for (AdGroupCriterion adGroupCriterion : adGroupCriterionPage.getEntries()) {
ProductPartition partition = (ProductPartition) adGroupCriterion.getCriterion();
parentIdMap.put(partition.getParentCriterionId(), adGroupCriterion);
}
offset += adGroupCriterionPage.getEntries().length;
selectorBuilder.increaseOffsetBy(PAGE_SIZE);
}
} while (offset < adGroupCriterionPage.getTotalNumEntries());
// Construct the ProductPartitionTree from the parentIdMap.
if (!parentIdMap.containsKey(null)) {
Preconditions.checkState(parentIdMap.isEmpty(), "No root criterion found in the tree but the tree is not empty");
return createEmptyAdGroupTree(adGroupId, getAdGroupBiddingStrategyConfiguration(services, session, adGroupId));
}
return createNonEmptyAdGroupTree(adGroupId, parentIdMap);
}
use of com.google.api.ads.adwords.axis.v201809.cm.ProductPartition in project googleads-java-lib by googleads.
the class ProductPartitionTreeImpl method createAdGroupTree.
/**
* Returns a new instance of this class based on the collection of ad group criteria provided.
* <p>NOTE: If retrieving existing criteria for use with this method, you must include all of the
* fields in {@link #REQUIRED_SELECTOR_FIELD_ENUMS} in your {@link Selector}.
*
* @param adGroupId the ID of the ad group
* @param biddingStrategyConfig the {@link BiddingStrategyConfiguration} for the ad group
* @param adGroupCriteria the non-null (but possibly empty) list of ad group criteria
*
* @throws NullPointerException if any argument is null, any element in {@code adGroupCriteria} is
* null, or any required field from {@link #REQUIRED_SELECTOR_FIELD_ENUMS} is missing from
* an element in {@code adGroupCriteria}
* @throws IllegalArgumentException if {@code adGroupCriteria} does not include the root criterion
* of the product partition tree
*/
static ProductPartitionTreeImpl createAdGroupTree(Long adGroupId, BiddingStrategyConfiguration biddingStrategyConfig, List<AdGroupCriterion> adGroupCriteria) {
Preconditions.checkNotNull(adGroupId, "Null ad group ID");
Preconditions.checkNotNull(biddingStrategyConfig, "Null bidding strategy configuration");
Preconditions.checkNotNull(adGroupCriteria, "Null criteria list");
if (adGroupCriteria.isEmpty()) {
return createEmptyAdGroupTree(adGroupId, biddingStrategyConfig);
}
ListMultimap<Long, AdGroupCriterion> parentIdMap = LinkedListMultimap.create();
for (AdGroupCriterion adGroupCriterion : adGroupCriteria) {
Preconditions.checkNotNull(adGroupCriterion.getCriterion(), "AdGroupCriterion has a null criterion");
if (adGroupCriterion instanceof BiddableAdGroupCriterion) {
BiddableAdGroupCriterion biddableCriterion = (BiddableAdGroupCriterion) adGroupCriterion;
Preconditions.checkNotNull(biddableCriterion.getUserStatus(), "User status is null for criterion ID %s", biddableCriterion.getCriterion().getId());
if (UserStatus.REMOVED.equals(biddableCriterion.getUserStatus())) {
// Skip REMOVED criteria.
continue;
}
}
if (adGroupCriterion.getCriterion() instanceof ProductPartition) {
ProductPartition partition = (ProductPartition) adGroupCriterion.getCriterion();
parentIdMap.put(partition.getParentCriterionId(), adGroupCriterion);
}
}
return createNonEmptyAdGroupTree(adGroupId, parentIdMap);
}
use of com.google.api.ads.adwords.axis.v201809.cm.ProductPartition in project googleads-java-lib by googleads.
the class ProductPartitionTreeImpl method addChildNodes.
/**
* Using the criteria in {@code parentIdMap}, recursively adds all children under the partition ID
* of {@code parentNode} to {@code parentNode}.
*
* @param parentNode required
* @param parentIdMap the multimap from parent partition ID to list of child criteria
*/
private static void addChildNodes(ProductPartitionNode parentNode, ListMultimap<Long, AdGroupCriterion> parentIdMap) {
if (parentIdMap.containsKey(parentNode.getProductPartitionId())) {
parentNode = parentNode.asSubdivision();
}
for (AdGroupCriterion adGroupCriterion : parentIdMap.get(parentNode.getProductPartitionId())) {
ProductPartition partition = (ProductPartition) adGroupCriterion.getCriterion();
ProductPartitionNode childNode = parentNode.addChild(partition.getCaseValue());
childNode = childNode.setProductPartitionId(partition.getId());
if (ProductPartitionType.SUBDIVISION.equals(partition.getPartitionType())) {
childNode = childNode.asSubdivision();
} else {
if (adGroupCriterion instanceof BiddableAdGroupCriterion) {
childNode = childNode.asBiddableUnit();
BiddableAdGroupCriterion biddableAdGroupCriterion = (BiddableAdGroupCriterion) adGroupCriterion;
Money cpcBidAmount = getBid(biddableAdGroupCriterion);
if (cpcBidAmount != null) {
childNode = childNode.setBid(cpcBidAmount.getMicroAmount());
}
childNode = childNode.setTrackingUrlTemplate(biddableAdGroupCriterion.getTrackingUrlTemplate());
childNode = copyCustomParametersToNode(biddableAdGroupCriterion, childNode);
} else {
childNode = childNode.asExcludedUnit();
}
}
addChildNodes(childNode, parentIdMap);
}
}
Aggregations