Search in sources :

Example 6 with NumberValue

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

the class Pql method createValue.

/**
 * Creates a {@link Value} from the value i.e. a {@link TextValue} for a value of type {@code
 * String}, {@link BooleanValue} for type {@code Boolean}, {@link NumberValue} for type {@code
 * Double}, {@code Long}, or {@code Integer}, {@link DateTimeValue} for type {@link DateTime}, and
 * {@link DateValue} for type {@link Date}. If the value is a {@code Value}, the value is
 * returned. If the value is {@code null}, an empty {@link TextValue} is returned.
 *
 * @param value the value to convert
 * @return the constructed value of the appropriate type
 * @throws IllegalArgumentException if value cannot be converted
 */
public static Value createValue(Object value) {
    if (value instanceof Value) {
        return (Value) value;
    } else if (value == null) {
        return new TextValue();
    } else {
        if (value instanceof Boolean) {
            BooleanValue booleanValue = new BooleanValue();
            booleanValue.setValue((Boolean) value);
            return booleanValue;
        } else if (value instanceof Double || value instanceof Long || value instanceof Integer) {
            NumberValue numberValue = new NumberValue();
            numberValue.setValue(value.toString());
            return numberValue;
        } else if (value instanceof String) {
            TextValue textValue = new TextValue();
            textValue.setValue((String) value);
            return textValue;
        } else if (value instanceof DateTime) {
            DateTimeValue dateTimeValue = new DateTimeValue();
            dateTimeValue.setValue((DateTime) value);
            return dateTimeValue;
        } else if (value instanceof Date) {
            DateValue dateValue = new DateValue();
            dateValue.setValue((Date) value);
            return dateValue;
        } else if (value instanceof Targeting) {
            TargetingValue targetingValue = new TargetingValue();
            targetingValue.setValue((Targeting) value);
            return targetingValue;
        } else if (value instanceof Set<?>) {
            SetValue setValue = new SetValue();
            Set<Value> values = new LinkedHashSet<Value>();
            for (Object entry : (Set<?>) value) {
                validateSetValueEntryForSet(createValue(entry), values);
                values.add(createValue(entry));
            }
            setValue.setValues(values.toArray(new Value[] {}));
            return setValue;
        } else {
            throw new IllegalArgumentException("Unsupported Value type [" + value.getClass() + "]");
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ResultSet(com.google.api.ads.admanager.axis.v202105.ResultSet) Set(java.util.Set) DateTimeValue(com.google.api.ads.admanager.axis.v202105.DateTimeValue) Targeting(com.google.api.ads.admanager.axis.v202105.Targeting) DateTime(com.google.api.ads.admanager.axis.v202105.DateTime) Date(com.google.api.ads.admanager.axis.v202105.Date) NumberValue(com.google.api.ads.admanager.axis.v202105.NumberValue) TextValue(com.google.api.ads.admanager.axis.v202105.TextValue) DateValue(com.google.api.ads.admanager.axis.v202105.DateValue) BooleanValue(com.google.api.ads.admanager.axis.v202105.BooleanValue) TextValue(com.google.api.ads.admanager.axis.v202105.TextValue) BooleanValue(com.google.api.ads.admanager.axis.v202105.BooleanValue) DateTimeValue(com.google.api.ads.admanager.axis.v202105.DateTimeValue) Value(com.google.api.ads.admanager.axis.v202105.Value) DateValue(com.google.api.ads.admanager.axis.v202105.DateValue) TargetingValue(com.google.api.ads.admanager.axis.v202105.TargetingValue) NumberValue(com.google.api.ads.admanager.axis.v202105.NumberValue) SetValue(com.google.api.ads.admanager.axis.v202105.SetValue) TargetingValue(com.google.api.ads.admanager.axis.v202105.TargetingValue) SetValue(com.google.api.ads.admanager.axis.v202105.SetValue)

Example 7 with NumberValue

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

the class Pql method getApiValue.

/**
 * Gets the underlying value of the {@code Value} object that's comparable to what would be
 * returned in any other API object (i.e. DateTimeValue will return an API DateTime, not a Joda
 * DateTime).
 *
 * @param value the value to convert
 * @return the native value of {@code Value} or {@code null} if the underlying value is null
 * @throws IllegalArgumentException if value cannot be converted
 */
public static Object getApiValue(Value value) {
    if (value instanceof BooleanValue) {
        return ((BooleanValue) value).getValue();
    } else if (value instanceof NumberValue) {
        if (Strings.isNullOrEmpty(((NumberValue) value).getValue())) {
            return null;
        } else {
            try {
                return NumberFormat.getInstance().parse(((NumberValue) value).getValue());
            } catch (ParseException e) {
                throw new IllegalStateException("Received invalid number format from API: " + ((NumberValue) value).getValue());
            }
        }
    } else if (value instanceof TextValue) {
        return ((TextValue) value).getValue();
    } else if (value instanceof DateTimeValue) {
        return ((DateTimeValue) value).getValue();
    } else if (value instanceof DateValue) {
        return ((DateValue) value).getValue();
    } else if (value instanceof TargetingValue) {
        return ((TargetingValue) value).getValue();
    } else if (value instanceof SetValue) {
        Value[] setValues = ((SetValue) value).getValues();
        Set<Object> apiValue = new LinkedHashSet<Object>();
        if (setValues != null) {
            for (Value setValue : setValues) {
                validateSetValueEntryForSet(getApiValue(setValue), apiValue);
                apiValue.add(getApiValue(setValue));
            }
        }
        return apiValue;
    } else {
        throw new IllegalArgumentException("Unsupported Value type [" + value.getClass() + "]");
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) DateTimeValue(com.google.api.ads.admanager.axis.v202111.DateTimeValue) NumberValue(com.google.api.ads.admanager.axis.v202111.NumberValue) TextValue(com.google.api.ads.admanager.axis.v202111.TextValue) DateValue(com.google.api.ads.admanager.axis.v202111.DateValue) BooleanValue(com.google.api.ads.admanager.axis.v202111.BooleanValue) DateTimeValue(com.google.api.ads.admanager.axis.v202111.DateTimeValue) TextValue(com.google.api.ads.admanager.axis.v202111.TextValue) Value(com.google.api.ads.admanager.axis.v202111.Value) NumberValue(com.google.api.ads.admanager.axis.v202111.NumberValue) TargetingValue(com.google.api.ads.admanager.axis.v202111.TargetingValue) BooleanValue(com.google.api.ads.admanager.axis.v202111.BooleanValue) DateValue(com.google.api.ads.admanager.axis.v202111.DateValue) SetValue(com.google.api.ads.admanager.axis.v202111.SetValue) ParseException(java.text.ParseException) TargetingValue(com.google.api.ads.admanager.axis.v202111.TargetingValue) SetValue(com.google.api.ads.admanager.axis.v202111.SetValue)

Example 8 with NumberValue

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

the class Pql method createValue.

/**
 * Creates a {@link Value} from the value i.e. a {@link TextValue} for a value of type {@code
 * String}, {@link BooleanValue} for type {@code Boolean}, {@link NumberValue} for type {@code
 * Double}, {@code Long}, or {@code Integer}, {@link DateTimeValue} for type {@link DateTime}, and
 * {@link DateValue} for type {@link Date}. If the value is a {@code Value}, the value is
 * returned. If the value is {@code null}, an empty {@link TextValue} is returned.
 *
 * @param value the value to convert
 * @return the constructed value of the appropriate type
 * @throws IllegalArgumentException if value cannot be converted
 */
public static Value createValue(Object value) {
    if (value instanceof Value) {
        return (Value) value;
    } else if (value == null) {
        return new TextValue();
    } else {
        if (value instanceof Boolean) {
            BooleanValue booleanValue = new BooleanValue();
            booleanValue.setValue((Boolean) value);
            return booleanValue;
        } else if (value instanceof Double || value instanceof Long || value instanceof Integer) {
            NumberValue numberValue = new NumberValue();
            numberValue.setValue(value.toString());
            return numberValue;
        } else if (value instanceof String) {
            TextValue textValue = new TextValue();
            textValue.setValue((String) value);
            return textValue;
        } else if (value instanceof DateTime) {
            DateTimeValue dateTimeValue = new DateTimeValue();
            dateTimeValue.setValue((DateTime) value);
            return dateTimeValue;
        } else if (value instanceof Date) {
            DateValue dateValue = new DateValue();
            dateValue.setValue((Date) value);
            return dateValue;
        } else if (value instanceof Targeting) {
            TargetingValue targetingValue = new TargetingValue();
            targetingValue.setValue((Targeting) value);
            return targetingValue;
        } else if (value instanceof Set<?>) {
            SetValue setValue = new SetValue();
            Set<Value> values = new LinkedHashSet<Value>();
            for (Object entry : (Set<?>) value) {
                validateSetValueEntryForSet(createValue(entry), values);
                values.add(createValue(entry));
            }
            setValue.setValues(values.toArray(new Value[] {}));
            return setValue;
        } else {
            throw new IllegalArgumentException("Unsupported Value type [" + value.getClass() + "]");
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ResultSet(com.google.api.ads.admanager.axis.v202111.ResultSet) Set(java.util.Set) DateTimeValue(com.google.api.ads.admanager.axis.v202111.DateTimeValue) Targeting(com.google.api.ads.admanager.axis.v202111.Targeting) DateTime(com.google.api.ads.admanager.axis.v202111.DateTime) Date(com.google.api.ads.admanager.axis.v202111.Date) NumberValue(com.google.api.ads.admanager.axis.v202111.NumberValue) TextValue(com.google.api.ads.admanager.axis.v202111.TextValue) DateValue(com.google.api.ads.admanager.axis.v202111.DateValue) BooleanValue(com.google.api.ads.admanager.axis.v202111.BooleanValue) DateTimeValue(com.google.api.ads.admanager.axis.v202111.DateTimeValue) TextValue(com.google.api.ads.admanager.axis.v202111.TextValue) Value(com.google.api.ads.admanager.axis.v202111.Value) NumberValue(com.google.api.ads.admanager.axis.v202111.NumberValue) TargetingValue(com.google.api.ads.admanager.axis.v202111.TargetingValue) BooleanValue(com.google.api.ads.admanager.axis.v202111.BooleanValue) DateValue(com.google.api.ads.admanager.axis.v202111.DateValue) SetValue(com.google.api.ads.admanager.axis.v202111.SetValue) TargetingValue(com.google.api.ads.admanager.axis.v202111.TargetingValue) SetValue(com.google.api.ads.admanager.axis.v202111.SetValue)

Example 9 with NumberValue

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

the class Pql method getApiValue.

/**
 * Gets the underlying value of the {@code Value} object that's comparable to what would be
 * returned in any other API object (i.e. DateTimeValue will return an API DateTime, not a Joda
 * DateTime).
 *
 * @param value the value to convert
 * @return the native value of {@code Value} or {@code null} if the underlying value is null
 * @throws IllegalArgumentException if value cannot be converted
 */
public static Object getApiValue(Value value) {
    if (value instanceof BooleanValue) {
        return ((BooleanValue) value).isValue();
    } else if (value instanceof NumberValue) {
        if (((NumberValue) value).getValue() == null) {
            return null;
        } else {
            try {
                return NumberFormat.getInstance().parse(((NumberValue) value).getValue());
            } catch (ParseException e) {
                throw new IllegalStateException("Received invalid number format from API: " + ((NumberValue) value).getValue());
            }
        }
    } else if (value instanceof TextValue) {
        return ((TextValue) value).getValue();
    } else if (value instanceof DateTimeValue) {
        return ((DateTimeValue) value).getValue();
    } else if (value instanceof DateValue) {
        return ((DateValue) value).getValue();
    } else if (value instanceof TargetingValue) {
        return ((TargetingValue) value).getValue();
    } else if (value instanceof SetValue) {
        List<Value> setValues = ((SetValue) value).getValues();
        Set<Object> apiValue = new LinkedHashSet<Object>();
        if (setValues != null) {
            for (Value setValue : setValues) {
                validateSetValueEntryForSet(getApiValue(setValue), apiValue);
                apiValue.add(getApiValue(setValue));
            }
        }
        return apiValue;
    } else {
        throw new IllegalArgumentException("Unsupported Value type [" + value.getClass() + "]");
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) DateTimeValue(com.google.api.ads.admanager.jaxws.v202202.DateTimeValue) NumberValue(com.google.api.ads.admanager.jaxws.v202202.NumberValue) TextValue(com.google.api.ads.admanager.jaxws.v202202.TextValue) DateValue(com.google.api.ads.admanager.jaxws.v202202.DateValue) BooleanValue(com.google.api.ads.admanager.jaxws.v202202.BooleanValue) SetValue(com.google.api.ads.admanager.jaxws.v202202.SetValue) DateValue(com.google.api.ads.admanager.jaxws.v202202.DateValue) TextValue(com.google.api.ads.admanager.jaxws.v202202.TextValue) BooleanValue(com.google.api.ads.admanager.jaxws.v202202.BooleanValue) DateTimeValue(com.google.api.ads.admanager.jaxws.v202202.DateTimeValue) NumberValue(com.google.api.ads.admanager.jaxws.v202202.NumberValue) Value(com.google.api.ads.admanager.jaxws.v202202.Value) TargetingValue(com.google.api.ads.admanager.jaxws.v202202.TargetingValue) ParseException(java.text.ParseException) TargetingValue(com.google.api.ads.admanager.jaxws.v202202.TargetingValue) SetValue(com.google.api.ads.admanager.jaxws.v202202.SetValue)

Example 10 with NumberValue

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

Aggregations

LinkedHashSet (java.util.LinkedHashSet)24 Test (org.junit.Test)9 ParseException (java.text.ParseException)8 Set (java.util.Set)8 Before (org.junit.Before)8 NumberValue (com.google.api.ads.admanager.axis.v202108.NumberValue)5 NumberValue (com.google.api.ads.admanager.axis.v202111.NumberValue)5 NumberValue (com.google.api.ads.admanager.axis.v202202.NumberValue)5 BooleanValue (com.google.api.ads.admanager.axis.v202105.BooleanValue)4 DateTimeValue (com.google.api.ads.admanager.axis.v202105.DateTimeValue)4 DateValue (com.google.api.ads.admanager.axis.v202105.DateValue)4 NumberValue (com.google.api.ads.admanager.axis.v202105.NumberValue)4 SetValue (com.google.api.ads.admanager.axis.v202105.SetValue)4 TargetingValue (com.google.api.ads.admanager.axis.v202105.TargetingValue)4 TextValue (com.google.api.ads.admanager.axis.v202105.TextValue)4 BooleanValue (com.google.api.ads.admanager.axis.v202202.BooleanValue)4 DateTimeValue (com.google.api.ads.admanager.axis.v202202.DateTimeValue)4 DateValue (com.google.api.ads.admanager.axis.v202202.DateValue)4 SetValue (com.google.api.ads.admanager.axis.v202202.SetValue)4 TargetingValue (com.google.api.ads.admanager.axis.v202202.TargetingValue)4