Search in sources :

Example 11 with CustomFieldServiceInterface

use of com.google.api.ads.admanager.axis.v202108.CustomFieldServiceInterface in project googleads-java-lib by googleads.

the class GetAllCustomFields 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 {
    // Get the CustomFieldService.
    CustomFieldServiceInterface customFieldService = adManagerServices.get(session, CustomFieldServiceInterface.class);
    // Create a statement to get all custom fields.
    StatementBuilder statementBuilder = new StatementBuilder().orderBy("id ASC").limit(StatementBuilder.SUGGESTED_PAGE_LIMIT);
    // Default for total result set size.
    int totalResultSetSize = 0;
    do {
        // Get custom fields by statement.
        CustomFieldPage page = customFieldService.getCustomFieldsByStatement(statementBuilder.toStatement());
        if (page.getResults() != null) {
            totalResultSetSize = page.getTotalResultSetSize();
            int i = page.getStartIndex();
            for (CustomField customField : page.getResults()) {
                System.out.printf("%d) Custom field with ID %d and name '%s' was found.%n", i++, customField.getId(), customField.getName());
            }
        }
        statementBuilder.increaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
    } while (statementBuilder.getOffset() < totalResultSetSize);
    System.out.printf("Number of results found: %d%n", totalResultSetSize);
}
Also used : CustomFieldPage(com.google.api.ads.admanager.axis.v202108.CustomFieldPage) CustomFieldServiceInterface(com.google.api.ads.admanager.axis.v202108.CustomFieldServiceInterface) StatementBuilder(com.google.api.ads.admanager.axis.utils.v202108.StatementBuilder) CustomField(com.google.api.ads.admanager.axis.v202108.CustomField)

Example 12 with CustomFieldServiceInterface

use of com.google.api.ads.admanager.axis.v202108.CustomFieldServiceInterface in project googleads-java-lib by googleads.

the class CreateCustomFieldsAndOptions 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 {
    // Get the CustomFieldService.
    CustomFieldServiceInterface customFieldService = adManagerServices.get(session, CustomFieldServiceInterface.class);
    // Create a number custom field that can be used for an external ID in the
    // API.
    CustomField numberCustomField = new CustomField();
    numberCustomField.setName("External ID #" + new Random().nextInt(Integer.MAX_VALUE));
    numberCustomField.setEntityType(CustomFieldEntityType.LINE_ITEM);
    numberCustomField.setDataType(CustomFieldDataType.NUMBER);
    numberCustomField.setVisibility(CustomFieldVisibility.API_ONLY);
    // Create a drop-down custom field that can be used in the UI.
    CustomField dropDownCustomField = new CustomField();
    dropDownCustomField.setName("Internal approval status #" + new Random().nextInt(Integer.MAX_VALUE));
    dropDownCustomField.setEntityType(CustomFieldEntityType.LINE_ITEM);
    dropDownCustomField.setDataType(CustomFieldDataType.DROP_DOWN);
    dropDownCustomField.setVisibility(CustomFieldVisibility.FULL);
    // Create the custom fields on the server.
    CustomField[] customFields = customFieldService.createCustomFields(new CustomField[] { numberCustomField, dropDownCustomField });
    for (CustomField createdCustomField : customFields) {
        System.out.printf("A custom field with ID %d and name '%s' was created.%n", createdCustomField.getId(), createdCustomField.getName());
    }
    // Set the created drop-down custom field.
    dropDownCustomField = customFields[1];
    // Create approved custom field option.
    CustomFieldOption approvedCustomFieldOption = new CustomFieldOption();
    approvedCustomFieldOption.setDisplayName("APPROVED");
    approvedCustomFieldOption.setCustomFieldId(dropDownCustomField.getId());
    // Create unapproved custom field option.
    CustomFieldOption unapprovedCustomFieldOption = new CustomFieldOption();
    unapprovedCustomFieldOption.setDisplayName("UNAPPROVED");
    unapprovedCustomFieldOption.setCustomFieldId(dropDownCustomField.getId());
    // Create the custom field options on the server.
    CustomFieldOption[] customFieldOptions = customFieldService.createCustomFieldOptions(new CustomFieldOption[] { approvedCustomFieldOption, unapprovedCustomFieldOption });
    for (CustomFieldOption createdCustomFieldOption : customFieldOptions) {
        System.out.printf("A custom field option with ID %d and display name '%s' was created.%n", createdCustomFieldOption.getId(), createdCustomFieldOption.getDisplayName());
    }
}
Also used : Random(java.util.Random) CustomFieldOption(com.google.api.ads.admanager.axis.v202202.CustomFieldOption) CustomFieldServiceInterface(com.google.api.ads.admanager.axis.v202202.CustomFieldServiceInterface) CustomField(com.google.api.ads.admanager.axis.v202202.CustomField)

Example 13 with CustomFieldServiceInterface

use of com.google.api.ads.admanager.axis.v202108.CustomFieldServiceInterface in project googleads-java-lib by googleads.

the class GetAllCustomFields 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 {
    // Get the CustomFieldService.
    CustomFieldServiceInterface customFieldService = adManagerServices.get(session, CustomFieldServiceInterface.class);
    // Create a statement to get all custom fields.
    StatementBuilder statementBuilder = new StatementBuilder().orderBy("id ASC").limit(StatementBuilder.SUGGESTED_PAGE_LIMIT);
    // Default for total result set size.
    int totalResultSetSize = 0;
    do {
        // Get custom fields by statement.
        CustomFieldPage page = customFieldService.getCustomFieldsByStatement(statementBuilder.toStatement());
        if (page.getResults() != null) {
            totalResultSetSize = page.getTotalResultSetSize();
            int i = page.getStartIndex();
            for (CustomField customField : page.getResults()) {
                System.out.printf("%d) Custom field with ID %d and name '%s' was found.%n", i++, customField.getId(), customField.getName());
            }
        }
        statementBuilder.increaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
    } while (statementBuilder.getOffset() < totalResultSetSize);
    System.out.printf("Number of results found: %d%n", totalResultSetSize);
}
Also used : CustomFieldPage(com.google.api.ads.admanager.axis.v202202.CustomFieldPage) CustomFieldServiceInterface(com.google.api.ads.admanager.axis.v202202.CustomFieldServiceInterface) StatementBuilder(com.google.api.ads.admanager.axis.utils.v202202.StatementBuilder) CustomField(com.google.api.ads.admanager.axis.v202202.CustomField)

Example 14 with CustomFieldServiceInterface

use of com.google.api.ads.admanager.axis.v202108.CustomFieldServiceInterface 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));
    }
}
Also used : CustomFieldValue(com.google.api.ads.admanager.axis.v202202.CustomFieldValue) DropDownCustomFieldValue(com.google.api.ads.admanager.axis.v202202.DropDownCustomFieldValue) BaseCustomFieldValue(com.google.api.ads.admanager.axis.v202202.BaseCustomFieldValue) DropDownCustomFieldValue(com.google.api.ads.admanager.axis.v202202.DropDownCustomFieldValue) BaseCustomFieldValue(com.google.api.ads.admanager.axis.v202202.BaseCustomFieldValue) LineItemServiceInterface(com.google.api.ads.admanager.axis.v202202.LineItemServiceInterface) CustomFieldServiceInterface(com.google.api.ads.admanager.axis.v202202.CustomFieldServiceInterface) ArrayList(java.util.ArrayList) LineItem(com.google.api.ads.admanager.axis.v202202.LineItem) NumberValue(com.google.api.ads.admanager.axis.v202202.NumberValue) LineItemPage(com.google.api.ads.admanager.axis.v202202.LineItemPage) StatementBuilder(com.google.api.ads.admanager.axis.utils.v202202.StatementBuilder)

Example 15 with CustomFieldServiceInterface

use of com.google.api.ads.admanager.axis.v202108.CustomFieldServiceInterface in project googleads-java-lib by googleads.

the class UpdateCustomFields method runExample.

/**
 * Runs the example.
 *
 * @param adManagerServices the services factory.
 * @param session the session.
 * @param customFieldId the ID of the custom field to update.
 * @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 customFieldId) throws RemoteException {
    // Get the CustomFieldService.
    CustomFieldServiceInterface customFieldService = adManagerServices.get(session, CustomFieldServiceInterface.class);
    // Create a statement to only select a single custom field by ID.
    StatementBuilder statementBuilder = new StatementBuilder().where("id = :id").orderBy("id ASC").limit(1).withBindVariableValue("id", customFieldId);
    // Get the custom field.
    CustomFieldPage page = customFieldService.getCustomFieldsByStatement(statementBuilder.toStatement());
    CustomField customField = Iterables.getOnlyElement(Arrays.asList(page.getResults()));
    // Update the custom field description.
    customField.setDescription("New custom field description");
    // Update the custom field on the server.
    CustomField[] customFields = customFieldService.updateCustomFields(new CustomField[] { customField });
    for (CustomField updatedCustomField : customFields) {
        System.out.printf("Custom field with ID %d and name '%s' was updated.%n", updatedCustomField.getId(), updatedCustomField.getName());
    }
}
Also used : CustomFieldPage(com.google.api.ads.admanager.axis.v202202.CustomFieldPage) CustomFieldServiceInterface(com.google.api.ads.admanager.axis.v202202.CustomFieldServiceInterface) StatementBuilder(com.google.api.ads.admanager.axis.utils.v202202.StatementBuilder) CustomField(com.google.api.ads.admanager.axis.v202202.CustomField)

Aggregations

CustomFieldServiceInterface (com.google.api.ads.admanager.axis.v202108.CustomFieldServiceInterface)6 CustomFieldServiceInterface (com.google.api.ads.admanager.axis.v202111.CustomFieldServiceInterface)6 CustomFieldServiceInterface (com.google.api.ads.admanager.axis.v202202.CustomFieldServiceInterface)6 StatementBuilder (com.google.api.ads.admanager.axis.utils.v202108.StatementBuilder)5 StatementBuilder (com.google.api.ads.admanager.axis.utils.v202111.StatementBuilder)5 StatementBuilder (com.google.api.ads.admanager.axis.utils.v202202.StatementBuilder)5 CustomField (com.google.api.ads.admanager.axis.v202108.CustomField)5 CustomField (com.google.api.ads.admanager.axis.v202111.CustomField)5 CustomField (com.google.api.ads.admanager.axis.v202202.CustomField)5 CustomFieldPage (com.google.api.ads.admanager.axis.v202108.CustomFieldPage)4 CustomFieldPage (com.google.api.ads.admanager.axis.v202111.CustomFieldPage)4 CustomFieldPage (com.google.api.ads.admanager.axis.v202202.CustomFieldPage)4 ArrayList (java.util.ArrayList)3 Random (java.util.Random)3 BaseCustomFieldValue (com.google.api.ads.admanager.axis.v202108.BaseCustomFieldValue)1 CustomFieldOption (com.google.api.ads.admanager.axis.v202108.CustomFieldOption)1 CustomFieldValue (com.google.api.ads.admanager.axis.v202108.CustomFieldValue)1 DropDownCustomFieldValue (com.google.api.ads.admanager.axis.v202108.DropDownCustomFieldValue)1 LineItem (com.google.api.ads.admanager.axis.v202108.LineItem)1 LineItemPage (com.google.api.ads.admanager.axis.v202108.LineItemPage)1