Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  // Copyright 2012 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.adwords.axis.utility.extension.delegates;
 
 
 import java.util.List;

Abstract class that encapsulates gets, fields and selectors.

Parameters:
<T> type of object that the Service works with, for example Campaign, AdGroup, etc.
<S> the service interface class. This is a class representing a SOAP service.

Implementation is not thread-safe, because AdWordsSession and Apache Axis service objects are not thread-safe.

Author(s):
Julian Toledo
 
 public abstract class AbstractGetDelegate<T, S> extends AbstractBaseDelegate<S> {
 
   private final Class<T> classT;
 
   private final String[] fields;
 
   private String getMethodName = "get";

  
Constructor without fields, used in services that do not support Generic Selectors.

Fields are not necessary for Services that do not use Generic Selectors.

Parameters:
adWordsSession the adWordsSession to use with the service
classT type of object that the Service works with, for example Campaign, AdGroup, etc.
classS the service interface class for the SOAP service
 
   protected AbstractGetDelegate(AdWordsSession adWordsSessionClass<T> classTClass<S> classS) {
     super(adWordsSessionclassS);
     this. = classT;
     this. = null;
   }

  
Constructor without fields, used in services that do not support Generic Selectors and with custom service.

Fields are not necessary for Services that do not use Generic Selectors.

Parameters:
adWordsSession the adWordsSession to use with the service
classT type of object that the Service works with, for example Campaign, AdGroup, etc.
service the custom service class for the SOAP service
 
   protected AbstractGetDelegate(AdWordsSession adWordsSessionClass<T> classT, S service) {
     super(adWordsSessionservice);
     this. = classT;
     this. = null;
   }

  
Constructor with fields, used in services that support Generic Selectors.

Parameters:
adWordsSession the adWordsSession to use with the service
classT type of object that the Service works with, for example Campaign, AdGroup, etc.
classS the service interface class for the SOAP service
selectorFields for the Generic Selectors using the SelectorField class
 
   protected AbstractGetDelegate(AdWordsSession adWordsSessionClass<T> classT,
       Class<S> classSList<? extends FieldType<T>> selectorFields) {
     super(adWordsSessionclassS);
     this. = classT;
     this. = SelectorFields.getFields(selectorFields);
   }

  
Constructor with fields, used in services that support Generic Selectors.

Parameters:
adWordsSession the adWordsSession to use with the service
classT type of object that the Service works with, for example Campaign, AdGroup, etc.
service the custom service class for the SOAP service
selectorFields for the Generic Selectors using the SelectorField class
  protected AbstractGetDelegate(AdWordsSession adWordsSessionClass<T> classT, S service,
      List<? extends FieldType<T>> selectorFields) {
    super(adWordsSessionservice);
    this. = classT;
    this. = SelectorFields.getFields(selectorFields);
  }

  
Returns the classT type.

Returns:
classT type of object that the Service works with, for example Campaign, AdGroup, etc.
  public Class<T> getClassT() {
    return ;
  }

  
Returns the name of the "get" method defined in the AdWords Service.

Parameters:
getMethodName the name of the "get" method defined in the AdWords Service
  protected void setGetMethodName(String getMethodName) {
    this. = getMethodName;
  }

  
Calls the appropriate Get method declared by the ServiceInterface S.

This method should be overridden by delegates for Services that do not support Generic Selectors.

Parameters:
selector should be a Generic Selector (com.google.api.adwords.v201506.cm.Selector) or a specific Selector appropriate to the ServiceInterface used that does not support Generic Selectors
Returns:
a list of <T>
Throws:
java.rmi.RemoteException for communication-related exceptions
  public List<T> get(Selector selectorthrows RemoteException {    
    // Using reflection to execute Page.getEntries() once
    @SuppressWarnings("unchecked")
    Class<T[]> classArrayT = (Class<T[]>) Array.newInstance(getClassT(), 0).getClass();
    T[] array = classArrayT.cast(ReflectionUtil.invokeCount(
        "getEntries"getPage(selector), selector.getClass(), 1));
    return ListUtil.asList(array);
  }

  
Calls the appropriate Get method declared by the ServiceInterface S. Gets the full page of elements without calling getEntries.

Parameters:
selector should be a Generic Selector (com.google.api.adwords.v201506.cm.Selector) or a specific Selector appropriate to the ServiceInterface used that does not support Generic Selectors
Returns:
Object, because AdParamPage does not extend Page
Throws:
java.rmi.RemoteException for communication-related exceptions
  protected Object getPage(Selector selectorthrows RemoteException {
    // Using reflection to execute getService().get(selector), retrying MAX_RETRY_COUNT times
    return ReflectionUtil.invokeCount(
        getService(), selector);
  }

  
Creates a Generic Selector Builder with all the fields declared in the subClass.
    return new SelectorBuilder().fields();
  }

  
Creates a Generic Selector Builder with all the fields declared in the subClass and adds a Predicate that says that the given field must have one of the given values.

Parameters:
selectorField field to be included in the predicate
fieldValues values for the field
startIndex index of the first result
numberResults number of results
Returns:
com.google.api.ads.adwords.axis.utils.v201506.SelectorBuilder
  private SelectorBuilder createSelectorBuilder(FieldType<T> selectorFieldList<?> fieldValues,
      int startIndexint numberResults) {
    SelectorBuilder selectorBuilder = createSelectorBuilder()
      .in(selectorField.getField(), ListUtil.asStringArray(fieldValues))
      .offset(startIndex).limit(numberResults);
    return selectorBuilder;
  }

  
Creates a Generic Selector Builder with all the fields declared in the subClass and adds a Predicate that says that the given field must have one of the given values.

Parameters:
selectorField field to be included in the predicate
fieldValues values for the field
Returns:
com.google.api.ads.adwords.axis.utils.v201506.SelectorBuilder
  private SelectorBuilder createSelectorBuilder(FieldType<T> selectorFieldList<?> fieldValues) {
    SelectorBuilder selectorBuilder = createSelectorBuilder()
      .in(selectorField.getField(), ListUtil.asStringArray(fieldValues));
    return selectorBuilder;
  }

  
Gets all <T> objects using a Generic Selector without predicates.

Returns:
all the <T> objects for the ManagedCustomer
Throws:
java.rmi.RemoteException for communication-related exceptions
  public List<T> get() throws RemoteException {
    return get(createSelectorBuilder().build());
  }

  
Gets all <T> objects using a Generic Selector with Paging without predicates.

Parameters:
startIndex index of the first result to retrieve
numberResults total number of result to retrieve
Returns:
all the <T> objects for the ManagedCustomer
Throws:
java.rmi.RemoteException for communication-related exceptions
  public List<T> get(int startIndexint numberResultsthrows RemoteException {
    SelectorBuilder builder = createSelectorBuilder().offset(startIndex).limit(numberResults);
    return get(builder.build());
  }

  
Gets a single <T> object using a Generic Selector with a predicate for one field value, null if the object does not exist and an exception if there are more than one matches.

Parameters:
selectorField name of the field
fieldValue value for the field in the predicate
Returns:
the first <T> object matching the fieldValue
Throws:
java.rmi.RemoteException for communication-related exceptions
  protected T getOneByField(FieldType<T> selectorFieldObject fieldValue)
      throws RemoteException {
    List<T> list = getByField(selectorFieldfieldValue);
    if (!list.isEmpty()) {
      return Iterables.getOnlyElement(list);
    } else {
      return null;
    }
  }

  
Gets all the <T> objects using a Generic Selector with a predicate for one field value.

Parameters:
selectorField name of the field
fieldValue value for the field in the predicate
Returns:
all the <T> objects matching the fieldValue
Throws:
java.rmi.RemoteException for communication-related exceptions
  protected List<T> getByField(FieldType<T> selectorFieldObject fieldValue)
      throws RemoteException {
    return get(createSelectorBuilder(selectorField, ImmutableList.of(fieldValue)).build());
  }

  
Gets all the <T> objects using a Generic Selector with a predicate for a list of values.

Parameters:
selectorField name of the field
fieldValues list of values for the field in the predicate
Returns:
all the <T> objects matching the fieldValues
Throws:
java.rmi.RemoteException for communication-related exceptions
  protected List<T> getByField(FieldType<T> selectorFieldList<LongfieldValues)
      throws RemoteException {
    return get(createSelectorBuilder(selectorFieldfieldValues).build());
  }

  
Gets all the <T> objects using a Generic Selector with a single predicate for a list of values using pagination.

Parameters:
selectorField name of the field
fieldValue list of values for the field in the predicate
startIndex index of the first result
numberResults number of results
Returns:
all the <T> objects matching the fieldValues in the range (startIndex, numberResults)
Throws:
java.rmi.RemoteException for communication-related exceptions
  protected List<T> getByField(FieldType<T> selectorFieldlong fieldValueint startIndex,
      int numberResultsthrows RemoteException {
    return get(createSelectorBuilder(
        selectorField, ImmutableList.of(fieldValue), startIndexnumberResults).build());
  }

  
Gets all the <T> objects using a Generic Selector with a predicate for a list of values using pagination.

Parameters:
selectorField name of the field
fieldValue list of values for the field in the predicate
startIndex index of the first result
numberResults number of results
Returns:
all the <T> objects matching the fieldValues in the range (startIndex, numberResults)
Throws:
java.rmi.RemoteException for communication-related exceptions
  protected List<T> getByField(FieldType<T> selectorFieldString fieldValueint startIndex,
      int numberResultsthrows RemoteException {
    return get(createSelectorBuilder(
        selectorField, ImmutableList.of(fieldValue), startIndexnumberResults).build());
  }

  
Gets all the <T> objects using a Generic Selector with a containsAny predicate for a list of values.

Parameters:
selectorField name of the field
fieldValues list of values for the field in the predicate
Returns:
all the <T> objects matching the fieldValues
Throws:
java.rmi.RemoteException for communication-related exceptions
  protected List<T> getByFieldContainsAny(FieldType<T> selectorFieldList<LongfieldValues)
      throws RemoteException {
    return get(createSelectorBuilder()
      .containsAny(selectorField.getField(), ListUtil.asStringArray(fieldValues)).build());
  }
New to GrepCode? Check out our FAQ X