use of com.google.api.ads.admanager.axis.utils.v202205.StatementBuilder in project googleads-java-lib by googleads.
the class DeleteUserTeamAssociations method runExample.
/**
* Runs the example.
*
* @param adManagerServices the services factory.
* @param session the session.
* @param userId the ID of the user to delete user team associations for.
* @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(AdManagerServices adManagerServices, AdManagerSession session, long userId) throws RemoteException {
// Get the UserTeamAssociationService.
UserTeamAssociationServiceInterface userTeamAssociationService = adManagerServices.get(session, UserTeamAssociationServiceInterface.class);
// Create a statement to get all user team associations for a user.
StatementBuilder statementBuilder = new StatementBuilder().where("WHERE userId = :userId ").orderBy("userId ASC, teamid ASC").limit(StatementBuilder.SUGGESTED_PAGE_LIMIT).withBindVariableValue("userId", userId);
// Default for total result set size.
int totalResultSetSize = 0;
do {
// Get user team associations by statement.
UserTeamAssociationPage page = userTeamAssociationService.getUserTeamAssociationsByStatement(statementBuilder.toStatement());
if (page.getResults() != null) {
totalResultSetSize = page.getTotalResultSetSize();
int i = page.getStartIndex();
for (UserTeamAssociation userTeamAssociation : page.getResults()) {
System.out.printf("%d) User team association with user ID %d and " + "team ID %d will be deleted.%n", i++, userTeamAssociation.getUserId(), userTeamAssociation.getTeamId());
}
}
statementBuilder.increaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
} while (statementBuilder.getOffset() < totalResultSetSize);
System.out.printf("Number of user team associations to be deleted: %d%n", totalResultSetSize);
if (totalResultSetSize > 0) {
// Remove limit and offset from statement.
statementBuilder.removeLimitAndOffset();
// Create action.
com.google.api.ads.admanager.axis.v202108.DeleteUserTeamAssociations action = new com.google.api.ads.admanager.axis.v202108.DeleteUserTeamAssociations();
// Perform action.
UpdateResult result = userTeamAssociationService.performUserTeamAssociationAction(action, statementBuilder.toStatement());
if (result != null && result.getNumChanges() > 0) {
System.out.printf("Number of user team associations deleted: %d%n", result.getNumChanges());
} else {
System.out.println("No user team associations were deleted.");
}
}
}
use of com.google.api.ads.admanager.axis.utils.v202205.StatementBuilder in project googleads-java-lib by googleads.
the class RequestBuyerAcceptance method runExample.
/**
* Runs the example.
*
* @param adManagerServices the services factory.
* @param session the session.
* @param proposalId the proposal ID to send.
* @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(AdManagerServices adManagerServices, AdManagerSession session, long proposalId) throws RemoteException {
// Get the ProposalService.
ProposalServiceInterface proposalService = adManagerServices.get(session, ProposalServiceInterface.class);
// Create a statement to only select a single proposal by ID.
StatementBuilder statementBuilder = new StatementBuilder().where("WHERE id = :id").orderBy("id ASC").limit(StatementBuilder.SUGGESTED_PAGE_LIMIT).withBindVariableValue("id", proposalId);
// Retrieve a small amount of proposals at a time, paging through until all
// proposals have been retrieved.
int totalResultSetSize = 0;
do {
ProposalPage page = proposalService.getProposalsByStatement(statementBuilder.toStatement());
if (page.getResults() != null) {
// Print out some information for each proposal.
totalResultSetSize = page.getTotalResultSetSize();
int i = page.getStartIndex();
for (Proposal proposal : page.getResults()) {
System.out.printf("%d) Proposal with ID %d and name '%s' will be sent to Marketplace for buyer " + "acceptance.%n", i++, proposal.getId(), proposal.getName());
}
}
statementBuilder.increaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
} while (statementBuilder.getOffset() < totalResultSetSize);
System.out.printf("Number of proposals to be sent to Marketplace for buyer acceptance: %d%n", totalResultSetSize);
if (totalResultSetSize > 0) {
// Remove limit and offset from statement.
statementBuilder.removeLimitAndOffset();
// Create action.
com.google.api.ads.admanager.axis.v202108.RequestBuyerAcceptance action = new com.google.api.ads.admanager.axis.v202108.RequestBuyerAcceptance();
// Perform action.
UpdateResult result = proposalService.performProposalAction(action, statementBuilder.toStatement());
if (result != null && result.getNumChanges() > 0) {
System.out.printf("Number of proposals that were sent to Marketplace for buyer acceptance: %d%n", result.getNumChanges());
} else {
System.out.println("No proposals were sent to Marketplace for buyer acceptance.");
}
}
}
use of com.google.api.ads.admanager.axis.utils.v202205.StatementBuilder in project googleads-java-lib by googleads.
the class SetLineItemCustomFieldValue method runExample.
/**
* Runs the example.
*
* @param adManagerServices the services factory.
* @param session the session.
* @param numberCustomFieldId the ID of the number typed custom field to set on the line item.
* @param customFieldOptionId the ID of the option for the drop-down custom field.
* @param lineItemId the ID of the line item to set these custom fields.
* @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(AdManagerServices adManagerServices, AdManagerSession session, long numberCustomFieldId, long customFieldOptionId, long lineItemId) throws RemoteException {
// Get the CustomFieldService.
CustomFieldServiceInterface customFieldService = adManagerServices.get(session, CustomFieldServiceInterface.class);
// Get the LineItemService.
LineItemServiceInterface lineItemService = adManagerServices.get(session, LineItemServiceInterface.class);
// Get the drop-down custom field ID.
long dropDownCustomFieldId = customFieldService.getCustomFieldOption(customFieldOptionId).getCustomFieldId();
// Create a statement to only select a single line item by ID.
StatementBuilder statementBuilder = new StatementBuilder().where("id = :id").orderBy("id ASC").limit(1).withBindVariableValue("id", lineItemId);
// Get the line item.
LineItemPage page = lineItemService.getLineItemsByStatement(statementBuilder.toStatement());
LineItem lineItem = Iterables.getOnlyElement(Arrays.asList(page.getResults()));
// Create number custom field value.
NumberValue numberValue = new NumberValue();
numberValue.setValue("12345");
CustomFieldValue numberCustomFieldValue = new CustomFieldValue();
numberCustomFieldValue.setCustomFieldId(numberCustomFieldId);
numberCustomFieldValue.setValue(numberValue);
// Create drop-down custom field value.
DropDownCustomFieldValue dropDownCustomFieldValue = new DropDownCustomFieldValue();
dropDownCustomFieldValue.setCustomFieldId(dropDownCustomFieldId);
dropDownCustomFieldValue.setCustomFieldOptionId(customFieldOptionId);
// Create a combined custom field value list of existing different custom
// field values and new ones.
List<BaseCustomFieldValue> combinedCustomFieldValues = new ArrayList<>();
if (lineItem.getCustomFieldValues() != null) {
for (BaseCustomFieldValue existingCustomFieldValue : lineItem.getCustomFieldValues()) {
if (!existingCustomFieldValue.getCustomFieldId().equals(numberCustomFieldId) && !existingCustomFieldValue.getCustomFieldId().equals(dropDownCustomFieldId)) {
combinedCustomFieldValues.add(existingCustomFieldValue);
}
}
}
combinedCustomFieldValues.addAll(Arrays.asList(numberCustomFieldValue, dropDownCustomFieldValue));
// Set the combined custom field values.
lineItem.setCustomFieldValues(combinedCustomFieldValues.toArray(new BaseCustomFieldValue[] {}));
// Update the line item on the server.
LineItem[] lineItems = lineItemService.updateLineItems(new LineItem[] { lineItem });
for (LineItem updatedLineItem : lineItems) {
// Get a string representation of the custom field values.
List<String> customFieldValueStrings = Lists.transform(Arrays.asList(updatedLineItem.getCustomFieldValues()), new Function<BaseCustomFieldValue, String>() {
@Override
public String apply(BaseCustomFieldValue baseCustomFieldValue) {
if (baseCustomFieldValue instanceof CustomFieldValue && ((CustomFieldValue) baseCustomFieldValue).getValue() instanceof NumberValue) {
return String.format("{ID: %d, value: %.0f}", baseCustomFieldValue.getCustomFieldId(), Double.parseDouble(((NumberValue) ((CustomFieldValue) baseCustomFieldValue).getValue()).getValue()));
} else if (baseCustomFieldValue instanceof DropDownCustomFieldValue) {
return String.format("{ID: %d, customFieldOptionId: %d}", baseCustomFieldValue.getCustomFieldId(), ((DropDownCustomFieldValue) baseCustomFieldValue).getCustomFieldOptionId());
} else {
return "{Unrecognized value}";
}
}
});
System.out.printf("A line item with ID %d was updated with custom field values '%s'%n", updatedLineItem.getId(), Joiner.on(",").join(customFieldValueStrings));
}
}
use of com.google.api.ads.admanager.axis.utils.v202205.StatementBuilder in project googleads-java-lib by googleads.
the class DeleteCustomTargetingValues method runExample.
/**
* Runs the example.
*
* @param adManagerServices the services factory.
* @param session the session.
* @param customTargetingValueId the ID of the custom targeting value to delete.
* @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(AdManagerServices adManagerServices, AdManagerSession session, long customTargetingValueId) throws RemoteException {
// Get the CustomTargetingService.
CustomTargetingServiceInterface customTargetingService = adManagerServices.get(session, CustomTargetingServiceInterface.class);
// Create a statement to select custom targeting value.
StatementBuilder statementBuilder = new StatementBuilder().where("WHERE id = :id").orderBy("id ASC").limit(StatementBuilder.SUGGESTED_PAGE_LIMIT).withBindVariableValue("id", customTargetingValueId);
// Default for total result set size.
int totalResultSetSize = 0;
do {
// Get custom targeting values by statement.
CustomTargetingValuePage page = customTargetingService.getCustomTargetingValuesByStatement(statementBuilder.toStatement());
if (page.getResults() != null) {
totalResultSetSize = page.getTotalResultSetSize();
int i = page.getStartIndex();
for (CustomTargetingValue customTargetingValue : page.getResults()) {
System.out.printf("%d) Custom targeting value with ID %d" + " will be deleted.%n", i++, customTargetingValue.getId());
}
}
statementBuilder.increaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
} while (statementBuilder.getOffset() < totalResultSetSize);
System.out.printf("Number of custom targeting values to be deleted: %d%n", totalResultSetSize);
if (totalResultSetSize > 0) {
// Remove limit and offset from statement.
statementBuilder.removeLimitAndOffset();
// Create action.
com.google.api.ads.admanager.axis.v202108.DeleteCustomTargetingValues action = new com.google.api.ads.admanager.axis.v202108.DeleteCustomTargetingValues();
// Perform action.
UpdateResult result = customTargetingService.performCustomTargetingValueAction(action, statementBuilder.toStatement());
if (result != null && result.getNumChanges() > 0) {
System.out.printf("Number of custom targeting values deleted: %d%n", result.getNumChanges());
} else {
System.out.println("No custom targeting values deleted.");
}
}
}
use of com.google.api.ads.admanager.axis.utils.v202205.StatementBuilder in project googleads-java-lib by googleads.
the class GetPredefinedCustomTargetingKeysAndValues method runExample.
/**
* Runs the example.
*
* @param adManagerServices the services factory.
* @param session the session.
* @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(AdManagerServices adManagerServices, AdManagerSession session) throws RemoteException {
CustomTargetingServiceInterface customTargetingService = adManagerServices.get(session, CustomTargetingServiceInterface.class);
// Get all predefined custom targeting keys.
List<Long> customTargetingKeyIds = getPredefinedCustomTargetingKeyIds(adManagerServices, session);
// Create a statement to select custom targeting values.
StatementBuilder statementBuilder = new StatementBuilder().where("customTargetingKeyId = :customTargetingKeyId").orderBy("id ASC").limit(StatementBuilder.SUGGESTED_PAGE_LIMIT);
int totalResultsCounter = 0;
for (Long customTargetingKeyId : customTargetingKeyIds) {
// Set the custom targeting key ID to select from.
statementBuilder.withBindVariableValue("customTargetingKeyId", customTargetingKeyId);
// Retrieve a small amount of custom targeting values at a time, paging through
// until all custom targeting values have been retrieved.
int totalResultSetSize = 0;
statementBuilder.offset(0);
do {
CustomTargetingValuePage page = customTargetingService.getCustomTargetingValuesByStatement(statementBuilder.toStatement());
if (page.getResults() != null) {
// Print out some information for each custom targeting value.
totalResultSetSize = page.getTotalResultSetSize();
for (CustomTargetingValue customTargetingValue : page.getResults()) {
System.out.printf("%d) Custom targeting value with ID %d, name '%s', display name '%s', " + "and custom targeting key ID %d was found.%n", totalResultsCounter++, customTargetingValue.getId(), customTargetingValue.getName(), customTargetingValue.getDisplayName(), customTargetingValue.getCustomTargetingKeyId());
}
}
statementBuilder.increaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
} while (statementBuilder.getOffset() < totalResultSetSize);
}
System.out.printf("Number of results found: %d%n", totalResultsCounter);
}
Aggregations