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 mutates and operations.

Parameters:
<T> type of object that the Service works with, for example Campaign, AdGroup, etc.
<OperationT> type of the com.google.api.adwords.v201502.cm.Operation specific for the object T for example: CampaignOperation, AdGroupOperation, 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 AbstractGetMutateDelegate<T, OperationT extends Operation, S>
     extends AbstractGetDelegate<T, S> {
 
   private static final String MUTATE = "mutate";
 
   private final Class<OperationT> classOperationT;
 
   private final String mutateMethodName;

  
Constructor without fields, used in services that do not 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.
classOperationT sub-class of the com.google.api.adwords.v201502.cm.Operation specific for classT
classS the service interface class for the SOAP service
 
   protected AbstractGetMutateDelegate(AdWordsSession adWordsSessionClass<T> classT,
       Class<OperationT> classOperationTClass<S> classS) {
     super(adWordsSessionclassTclassS);
     this. = classOperationT;
     this. = ;
   }

  
Constructor without fields, used in services that do not 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.
classOperationT sub-class of the com.google.api.adwords.v201502.cm.Operation specific for classT
classS the service interface class for the SOAP service
mutateMethodName a custom mutate method name
 
   protected AbstractGetMutateDelegate(AdWordsSession adWordsSessionClass<T> classT,
       Class<OperationT> classOperationTClass<S> classSString mutateMethodName) {
     super(adWordsSessionclassTclassS);
     this. = classOperationT;
     this. = mutateMethodName;
   }

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

Parameters:
adWordsSession the adWordsSession to use with the service
classT type of object that the Service works with, for example Campaign, AdGroup, etc.
classOperationT sub-class of the com.google.api.adwords.v201502.cm.Operation specific for classT
service the custom service class for the SOAP service
  protected AbstractGetMutateDelegate(AdWordsSession adWordsSessionClass<T> classT,
      Class<OperationT> classOperationT, S service) {
    super(adWordsSessionclassTservice);
    this. = classOperationT;
    this. = ;
  }

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

Parameters:
adWordsSession the adWordsSession to use with the service
classT type of object that the Service works with, for example Campaign, AdGroup, etc.
classOperationT sub-class of the com.google.api.adwords.v201502.cm.Operation specific for classT
service the custom service class for the SOAP service
mutateMethodName a custom mutate method name
  protected AbstractGetMutateDelegate(AdWordsSession adWordsSessionClass<T> classT,
      Class<OperationT> classOperationT, S serviceString mutateMethodName) {
    super(adWordsSessionclassTservice);
    this. = classOperationT;
    this. = mutateMethodName;
  }

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

Parameters:
adWordsSession the adWordsSession to use with the service
selectorFields for the Generic Selectors using the SelectorField class
classT type of object that the Service works with, for example Campaign, AdGroup, etc.
classOperationT sub-class of the com.google.api.adwords.v201502.cm.Operation specific for classT
classS the service interface class for the SOAP service
  protected AbstractGetMutateDelegate(AdWordsSession adWordsSession,
      List<? extends FieldType<T>> selectorFields,
      Class<T> classTClass<OperationT> classOperationTClass<S> classS) {
    super(adWordsSessionclassTclassSselectorFields);
    this. = classOperationT;
    this. = ;
  }

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

Parameters:
adWordsSession the adWordsSession to use with the service
selectorFields for the Generic Selectors using the SelectorField class
classT type of object that the Service works with, for example Campaign, AdGroup, etc.
classOperationT sub-class of the com.google.api.adwords.v201502.cm.Operation specific for classT
classS the service interface class for the SOAP service
mutateMethodName a custom mutate method name
  protected AbstractGetMutateDelegate(AdWordsSession adWordsSession,
      List<? extends FieldType<T>> selectorFields,
      Class<T> classTClass<OperationT> classOperationTClass<S> classS,
      String mutateMethodName) {
    super(adWordsSessionclassTclassSselectorFields);
    this. = classOperationT;
    this. = mutateMethodName;
  }

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

Parameters:
adWordsSession the adWordsSession to use with the service
selectorFields for the Generic Selectors using the SelectorField class
classT type of object that the Service works with, for example Campaign, AdGroup, etc.
classOperationT sub-class of the com.google.api.adwords.v201502.cm.Operation specific for classT
service the custom service class for the SOAP service
  protected AbstractGetMutateDelegate(AdWordsSession adWordsSession,
      List<? extends FieldType<T>> selectorFieldsClass<T> classT,
          Class<OperationT> classOperationT, S service) {
    super(adWordsSessionclassTserviceselectorFields);
    this. = classOperationT;
    this. = ;
  }

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

Parameters:
adWordsSession the adWordsSession to use with the service
selectorFields for the Generic Selectors using the SelectorField class
classT type of object that the Service works with, for example Campaign, AdGroup, etc.
classOperationT sub-class of the com.google.api.adwords.v201502.cm.Operation specific for classT
service the custom service class for the SOAP service
mutateMethodName a custom mutate method name
  protected AbstractGetMutateDelegate(AdWordsSession adWordsSession,
      List<? extends FieldType<T>> selectorFieldsClass<T> classT,
          Class<OperationT> classOperationT, S serviceString mutateMethodName) {
    super(adWordsSessionclassTserviceselectorFields);
    this. = classOperationT;
    this. = mutateMethodName;
  }

  
Calls getValue() for ListReturnValue's subclasses OR ManagedCustomerReturnValue.getValue() OR if not needed just converts T[] to List<T>.

Parameters:
object generic Object with ListReturnValue OR ManagedCustomerReturnValue OR T[] the return object depends on the interface S
Returns:
a list of <T> objects
Throws:
java.rmi.RemoteException if there is an error in the reflection call
  private List<T> getValue(Object objectthrows RemoteException {
    @SuppressWarnings("unchecked")
    Class<T[]> classArrayT = (Class<T[]>) Array.newInstance(getClassT(), 0).getClass();
    if (object instanceof ListReturnValue) {
      // Using reflection for listReturnValue.getValue() on Subclasses once
      T[] array =  classArrayT.cast(
          ReflectionUtil.invokeCount("getValue"objectobject.getClass(), 1));
      return ListUtil.asList(array);
    } else if (object instanceof ManagedCustomerReturnValue) {
      // ManagedCustomerServiceInterface is a special case, it does not extend ListReturnValue
      return ListUtil.asList(
          classArrayT.cast(((ManagedCustomerReturnValueobject).getValue()));
    } else if (object.getClass().isArray()
        && object.getClass().getComponentType().equals(getClassT())) {
      return ListUtil.asList(classArrayT.cast(object));
    } else {
      throw new UtilityLibraryException("Error during reflection for "
          + object.getClass().getName() + ".getValue(), input not supported");
    }
  }

  
Creates one operation per item on the list using the operator provided Executes by reflection operation.setOperand(t).

Parameters:
list a list of T API objects
operator the operator to
Returns:
a List of <OperationT extends Operation> specific for the class T
Throws:
com.google.api.ads.adwords.axis.utility.extension.exception.UtilityLibraryException if there is an error in the reflection call
  private List<OperationT> createOperations(List<T> listOperator operator) {
    List<OperationT> operations =  com.google.common.collect.Lists.newArrayList();
    for (T t : list) {
      try {
        // creating an instance of the Operation subClass
        OperationT operation = .newInstance();
        // Using reflection for operation.setOperand(t) once
        ReflectionUtil.invokeCount("setOperand"operationtgetClassT(), 1);
        operation.setOperator(operator);
        operations.add(operation);
      } catch (Exception e) {
        throw new UtilityLibraryException("Error during reflection for "
            + getClassT().getName() + ".setOperand(t)"e);
      }
    }
    return operations;
  }

  
Method to execute by reflection getService().mutate(operations).

Parameters:
operations a List<OperationT> of operations for the mutate call
Returns:
a generic Object for ListReturnValue or T[] the return object depends on the interface S
Throws:
com.google.api.ads.adwords.axis.utility.extension.exception.UtilityLibraryException if there is an error in the reflection call
java.rmi.RemoteException for communication-related exceptions
  protected Object mutate(List<OperationT> operationsthrows RemoteException {
    // Using reflection for getService().mutate(operations), retrying MAX_RETRY_COUNT times
    OperationT[] operationsArray = operations.toArray(
        ObjectArrays.newArray(operations.size()));
    return ReflectionUtil.invokeCount(
        getService(), operationsArray);
  }

  
Encapsulates one ADD mutate operation per item on the list.

Parameters:
list a list of T object to insert
Returns:
a list of T objects
Throws:
com.google.api.ads.adwords.axis.utility.extension.exception.UtilityLibraryException if there is an error in the reflection call
java.rmi.RemoteException for communication-related exceptions
  public List<T> insert(List<T> listthrows RemoteException {
    return getValue(mutate(createOperations(list.)));
  }

  
Encapsulates one ADD mutate operation for the object.

Parameters:
object the T object to insert
Returns:
one T object
Throws:
com.google.api.ads.adwords.axis.utility.extension.exception.UtilityLibraryException if there is an error in the reflection call
java.rmi.RemoteException for communication-related exceptions
  public T insert(T objectthrows RemoteException {
    return Iterables.getOnlyElement(
        getValue(mutate(createOperations(ImmutableList.of(object), .))));
  }

  
Encapsulates one SET mutate operation per item on the list.

Parameters:
list a list of T object to update
Returns:
a list of T objects
Throws:
com.google.api.ads.adwords.axis.utility.extension.exception.UtilityLibraryException if there is an error in the reflection call
java.rmi.RemoteException for communication-related exceptions
  public List<T> update(List<T> listthrows RemoteException {
    return getValue(mutate(createOperations(list.)));
  }

  
Encapsulates one SET mutate operation for the object.

Parameters:
object the T object to update
Returns:
one T object
Throws:
com.google.api.ads.adwords.axis.utility.extension.exception.UtilityLibraryException if there is an error in the reflection call
java.rmi.RemoteException for communication-related exceptions
  public T update(T objectthrows RemoteException {
    return Iterables.getOnlyElement(
        getValue(mutate(createOperations(ImmutableList.of(object), .))));
  }

  
Encapsulates one REMOVE mutate operation per item on the list.

Parameters:
list a list of T object to remove
Returns:
a list of <T> objects
Throws:
com.google.api.ads.adwords.axis.utility.extension.exception.UtilityLibraryException if there is an error in the reflection call
java.rmi.RemoteException for communication-related exceptions
  public List<T> remove(List<T> listthrows RemoteException {
  }

  
Encapsulates one REMOVE mutate operation for the object.

Parameters:
object the T object to remove
Returns:
one T object
Throws:
com.google.api.ads.adwords.axis.utility.extension.exception.UtilityLibraryException if there is an error in the reflection call
java.rmi.RemoteException for communication-related exceptions
  public T remove(T objectthrows RemoteException {
    return Iterables.getOnlyElement(
        getValue(mutate(createOperations(ImmutableList.of(object), .))));
  }
New to GrepCode? Check out our FAQ X