Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.api.ads.admanager.jaxws.utils.v202311;
import com.google.api.ads.admanager.jaxws.v202311.BooleanValue;
import com.google.api.ads.admanager.jaxws.v202311.ColumnType;
import com.google.api.ads.admanager.jaxws.v202311.Date;
import com.google.api.ads.admanager.jaxws.v202311.DateTime;
import com.google.api.ads.admanager.jaxws.v202311.DateTimeValue;
import com.google.api.ads.admanager.jaxws.v202311.DateValue;
import com.google.api.ads.admanager.jaxws.v202311.NumberValue;
import com.google.api.ads.admanager.jaxws.v202311.ResultSet;
import com.google.api.ads.admanager.jaxws.v202311.Row;
import com.google.api.ads.admanager.jaxws.v202311.SetValue;
import com.google.api.ads.admanager.jaxws.v202311.Targeting;
import com.google.api.ads.admanager.jaxws.v202311.TargetingValue;
import com.google.api.ads.admanager.jaxws.v202311.TextValue;
import com.google.api.ads.admanager.jaxws.v202311.Value;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import org.apache.commons.lang.StringEscapeUtils;
/**
* A utility class for handling PQL objects. A typical use case is to combine result sets from the
* PublisherQueryLanguageService to then create a CSV such as:
*
*
*/
public final class Pql {
/** {@code Pql} is meant to be used statically. */
private Pql() {}
/**
* 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}, and {@link DateTimeValue} for type {@link DateTime}.
* 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 values = new LinkedHashSet();
for (Object entry : (Set>) value) {
validateSetValueEntryForSet(createValue(entry), values);
values.add(createValue(entry));
}
setValue.getValues().addAll(values);
return setValue;
} else {
throw new IllegalArgumentException("Unsupported Value type [" + value.getClass() + "]");
}
}
}
/**
* Validates that an Object is a valid entry for a SetValue
*
* @param entry the Object to validate
* @throws IllegalArgumentException if the Object is an unsupported type
*/
private static void validateSetValueEntryForSet(Object entry, Set> set) {
if (entry instanceof Set> || entry instanceof SetValue) {
throw new IllegalArgumentException("Unsupported Value type [nested sets]");
}
if (!set.isEmpty()) {
Object existingEntry = set.iterator().next();
if (!existingEntry.getClass().isAssignableFrom(entry.getClass())) {
throw new IllegalArgumentException(
String.format(
"Unsupported Value type [SetValue with " + "mixed types %s and %s]",
existingEntry.getClass(), entry.getClass()));
}
}
}
/**
* Creates a String from the Value. Date and DateTime values are converted using the rules of
* {@link DateTimes#toString(Date)} and {@link DateTimes#toStringWithTimeZone(DateTime)}
* respectively.
*
* @param value the value to convert
* @return the string representation of the value or an empty string for null
* @throws IllegalArgumentException if value cannot be converted
*/
public static String toString(Value value) {
Object unwrappedValue = getCsvValue(value);
if (unwrappedValue == null) {
return "";
} else {
return unwrappedValue.toString();
}
}
/**
* 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 setValues = ((SetValue) value).getValues();
Set