Package dfp.axis.v201308.activityservice

Source Code of dfp.axis.v201308.activityservice.GetAllActivities

// Copyright 2013 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 dfp.axis.v201308.activityservice;

import com.google.api.ads.common.lib.auth.OfflineCredentials;
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
import com.google.api.ads.dfp.axis.factory.DfpServices;
import com.google.api.ads.dfp.axis.utils.v201308.StatementBuilder;
import com.google.api.ads.dfp.axis.v201308.Activity;
import com.google.api.ads.dfp.axis.v201308.ActivityGroup;
import com.google.api.ads.dfp.axis.v201308.ActivityGroupPage;
import com.google.api.ads.dfp.axis.v201308.ActivityGroupServiceInterface;
import com.google.api.ads.dfp.axis.v201308.ActivityPage;
import com.google.api.ads.dfp.axis.v201308.ActivityServiceInterface;
import com.google.api.ads.dfp.lib.client.DfpSession;
import com.google.api.client.auth.oauth2.Credential;
import com.google.common.collect.Lists;

import java.rmi.RemoteException;
import java.util.List;

/**
* This example gets all activities. To create activities, run
* CreateActivities.java.
*
* Credentials and properties in {@code fromFile()} are pulled from the
* "ads.properties" file. See README for more info.
*
* Tags: ActivityService.getActivitiesByStatement
* Tags: ActivityGroupService.getActivityGroupsByStatement
*
* @author Adam Rogal
*/
public class GetAllActivities {

  public static void runExample(DfpServices dfpServices, DfpSession session) throws Exception {
    // Get the ActivityService.
    ActivityServiceInterface activityService =
        dfpServices.get(session, ActivityServiceInterface.class);

    // Get all activity groups
    List<Integer> activityGroupIds = getAllActivityGroupIds(dfpServices, session);

    // Create a statement to get all activities for an activity group.
    StatementBuilder statementBuilder = new StatementBuilder()
        .where("activityGroupId = :activityGroupId")
        .orderBy("id ASC")
        .limit(StatementBuilder.SUGGESTED_PAGE_LIMIT);

    int totalResultsCounter = 0;

    for (Integer activityGroupId : activityGroupIds) {
      // Set the activity group ID to select from.
      statementBuilder.withBindVariableValue("activityGroupId", activityGroupId);

      // Default for total result set size and offset.
      int totalResultSetSize = 0;
      statementBuilder.offset(0);

      do {
        // Get activities by statement.
        ActivityPage page =
            activityService.getActivitiesByStatement(statementBuilder.toStatement());

        if (page.getResults() != null) {
          totalResultSetSize = page.getTotalResultSetSize();
          for (Activity activity : page.getResults()) {
            System.out.printf(
                "%d) Activity with ID \"%d\", name \"%s\", and type \"%s\" was found.\n",
                totalResultsCounter++, activity.getId(), activity.getName(), activity.getType());
          }
        }

        statementBuilder.increaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
      } while (statementBuilder.getOffset() < totalResultSetSize);
    }

    System.out.printf("Number of results found: %d\n", totalResultsCounter);
  }

  /**
   * Gets all activity group IDs.
   */
  private static List<Integer> getAllActivityGroupIds(DfpServices dfpServices, DfpSession session)
      throws RemoteException {
    List<Integer> activityGroupIds = Lists.newArrayList();

    // Get the ActivityGroupService.
    ActivityGroupServiceInterface activityGroupService =
        dfpServices.get(session, ActivityGroupServiceInterface.class);

    // Create a statement to get all activity groups.
    StatementBuilder statementBuilder = new StatementBuilder()
        .orderBy("id ASC")
        .limit(StatementBuilder.SUGGESTED_PAGE_LIMIT);

    // Default for total result set size.
    int totalResultSetSize = 0;

    do {
      // Get activity groups by statement.
      ActivityGroupPage page =
          activityGroupService.getActivityGroupsByStatement(statementBuilder.toStatement());

      if (page.getResults() != null) {
        totalResultSetSize = page.getTotalResultSetSize();
        for (ActivityGroup activityGroup : page.getResults()) {
          activityGroupIds.add(activityGroup.getId());
        }
      }

      statementBuilder.increaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
    } while (statementBuilder.getOffset() < totalResultSetSize);

    return activityGroupIds;
  }

  public static void main(String[] args) throws Exception {
    // Generate a refreshable OAuth2 credential similar to a ClientLogin token
    // and can be used in place of a service account.
    Credential oAuth2Credential = new OfflineCredentials.Builder()
        .forApi(Api.DFP)
        .fromFile()
        .build()
        .generateCredential();

    // Construct a DfpSession.
    DfpSession session = new DfpSession.Builder()
        .fromFile()
        .withOAuth2Credential(oAuth2Credential)
        .build();

    DfpServices dfpServices = new DfpServices();

    runExample(dfpServices, session);
  }
}
TOP

Related Classes of dfp.axis.v201308.activityservice.GetAllActivities

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.