AdGroupAd exemptableAdGroupAd = new AdGroupAd();
exemptableAdGroupAd.setAdGroupId(adGroupId);
exemptableAdGroupAd.setAd(exemptableTextAd);
// Create operations.
AdGroupAdOperation exemptableOperation = new AdGroupAdOperation();
exemptableOperation.setOperand(exemptableAdGroupAd);
exemptableOperation.setOperator(Operator.ADD);
// Create text ad that violates an non-exemptable policy.
TextAd nonExemptableTextAd = new TextAd();
nonExemptableTextAd.setHeadline("Mars Cruise with too long of a headline.");
nonExemptableTextAd.setDescription1("Visit the Red Planet in style.");
nonExemptableTextAd.setDescription2("Low-gravity fun for everyone.");
nonExemptableTextAd.setDisplayUrl("www.example.com");
nonExemptableTextAd.setUrl("http://www.example.com/");
// Create ad group ad.
AdGroupAd nonExemptableAdGroupAd = new AdGroupAd();
nonExemptableAdGroupAd.setAdGroupId(adGroupId);
nonExemptableAdGroupAd.setAd(nonExemptableTextAd);
// Create operations.
AdGroupAdOperation nonExemptableOperation = new AdGroupAdOperation();
nonExemptableOperation.setOperand(nonExemptableAdGroupAd);
nonExemptableOperation.setOperator(Operator.ADD);
AdGroupAdOperation[] operations =
new AdGroupAdOperation[] {exemptableOperation, nonExemptableOperation};
try {
// Validate the ads.
AdGroupAdReturnValue result = adGroupAdService.mutate(operations);
} catch (ApiException e) {
Set<Integer> indicesToRemove = new HashSet<Integer>();
for (ApiError error : e.getErrors()) {
if (error instanceof PolicyViolationError) {
PolicyViolationError policyViolationError = (PolicyViolationError) error;
Matcher matcher = operationIndexPattern.matcher(error.getFieldPath());
if (matcher.matches()) {
int operationIndex = Integer.parseInt(matcher.group(1));
AdGroupAdOperation operation = operations[operationIndex];
System.out.printf("Ad with headline \"%s\" violated %s policy \"%s\".\n",
((TextAd) operation.getOperand().getAd()).getHeadline(), policyViolationError
.getIsExemptable() ? "exemptable" : "non-exemptable", policyViolationError
.getExternalPolicyName());
if (policyViolationError.getIsExemptable()) {
// Add exemption request to the operation.
System.out.printf(
"Adding exemption request for policy name \"%s\" on text \"%s\".\n",
policyViolationError.getKey().getPolicyName(), policyViolationError.getKey()
.getViolatingText());
List<ExemptionRequest> exemptionRequests =
new ArrayList<ExemptionRequest>(Arrays
.asList(operation.getExemptionRequests() == null
? new ExemptionRequest[] {} : operation.getExemptionRequests()));
exemptionRequests.add(new ExemptionRequest(policyViolationError.getKey()));
operation
.setExemptionRequests(exemptionRequests.toArray(new ExemptionRequest[] {}));
} else {
// Remove non-exemptable operation.
System.out.println("Removing non-exemptable operation at index " + operationIndex
+ ".");
indicesToRemove.add(operationIndex);
}
}
} else {
// Non-policy error returned.
Matcher matcher = operationIndexPattern.matcher(error.getFieldPath());
if (matcher.matches()) {
int operationIndex = Integer.parseInt(matcher.group(1));
AdGroupAdOperation operation = operations[operationIndex];
System.out.printf("Ad with headline \"%s\" created non-policy error \"%s\".\n",
((TextAd) operation.getOperand().getAd()).getHeadline(), error.getErrorString());
System.out.println("Removing non-exemptable operation at index " + operationIndex
+ ".");
indicesToRemove.add(operationIndex);
}
}