use of com.google.api.ads.admanager.jaxws.v202202.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() + "]");
}
}
}
use of com.google.api.ads.admanager.jaxws.v202202.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() + "]");
}
}
use of com.google.api.ads.admanager.jaxws.v202202.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() + "]");
}
}
}
use of com.google.api.ads.admanager.jaxws.v202202.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() + "]");
}
}
use of com.google.api.ads.admanager.jaxws.v202202.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));
}
}
Aggregations