Subversion Repositories SmartDukaan

Rev

Rev 9005 | Blame | Compare with Previous | Last modification | View Log | RSS feed

// 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 adwords.axis.v201309.basicoperations;

import com.google.api.ads.adwords.axis.factory.AdWordsServices;
import com.google.api.ads.adwords.axis.v201309.cm.Budget;
import com.google.api.ads.adwords.axis.v201309.cm.BudgetBudgetDeliveryMethod;
import com.google.api.ads.adwords.axis.v201309.cm.BudgetBudgetPeriod;
import com.google.api.ads.adwords.axis.v201309.cm.BudgetOperation;
import com.google.api.ads.adwords.axis.v201309.cm.BudgetServiceInterface;
import com.google.api.ads.adwords.axis.v201309.cm.Campaign;
import com.google.api.ads.adwords.axis.v201309.cm.CampaignOperation;
import com.google.api.ads.adwords.axis.v201309.cm.CampaignReturnValue;
import com.google.api.ads.adwords.axis.v201309.cm.CampaignServiceInterface;
import com.google.api.ads.adwords.axis.v201309.cm.CampaignStatus;
import com.google.api.ads.adwords.axis.v201309.cm.Money;
import com.google.api.ads.adwords.axis.v201309.cm.Operator;
import com.google.api.ads.adwords.lib.client.AdWordsSession;
import com.google.api.ads.common.lib.auth.OfflineCredentials;
import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
import com.google.api.client.auth.oauth2.Credential;

/**
 * This example updates a campaign by setting the status to PAUSED. To get
 * campaigns, run GetCampaigns.java.
 *
 * Credentials and properties in {@code fromFile()} are pulled from the
 * "ads.properties" file. See README for more info.
 *
 * Tags: CampaignService.mutate
 *
 * @author Kevin Winter
 */
public class UpdateCampaign {

        public static Long runExample(Long campaignId, String status, String name, Long amount ) 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.ADWORDS)
                .fromFile()
                .build()
                .generateCredential();

                // Construct an AdWordsSession.
                AdWordsSession session = new AdWordsSession.Builder()
                .fromFile()
                .withOAuth2Credential(oAuth2Credential)
                .build();

                AdWordsServices adWordsServices = new AdWordsServices();
                // Get the CampaignService.
                CampaignServiceInterface campaignService =
                        adWordsServices.get(session, CampaignServiceInterface.class);

                // Create campaign with updated status.
                Campaign campaign = new Campaign();
                campaign.setId(campaignId);
                if(status!=null && !("").equalsIgnoreCase(status)){
                        campaign.setStatus(CampaignStatus.fromString(status));
                }
                if(name!=null && !("").equalsIgnoreCase(name)){
                        campaign.setName(name);
                }
                
                if(amount>-1l){
                        // Get the BudgetService.
                        BudgetServiceInterface budgetService =
                                adWordsServices.get(session, BudgetServiceInterface.class);
        
                        // Create a budget, which can be shared by multiple campaigns.
                        Budget sharedBudget = new Budget();
                        sharedBudget.setName(name +"#" + System.currentTimeMillis());
                        Money budgetAmount = new Money();
                        budgetAmount.setMicroAmount(amount);
                        sharedBudget.setAmount(budgetAmount);
                        sharedBudget.setDeliveryMethod(BudgetBudgetDeliveryMethod.STANDARD);
                        sharedBudget.setPeriod(BudgetBudgetPeriod.DAILY);
        
                        BudgetOperation budgetOperation = new BudgetOperation();
                        budgetOperation.setOperand(sharedBudget);
                        budgetOperation.setOperator(Operator.ADD);
        
                        // Add the budget
                        Long budgetId =
                                budgetService.mutate(new BudgetOperation[] {budgetOperation}).getValue(0).getBudgetId();
                        
                        Budget budget = new Budget();
                        budget.setBudgetId(budgetId);
                        campaign.setBudget(budget);
                }
                // Create operations.
                CampaignOperation operation = new CampaignOperation();
                operation.setOperand(campaign);
                operation.setOperator(Operator.SET);

                CampaignOperation[] operations = new CampaignOperation[] {operation};
                if((status!=null && !("").equalsIgnoreCase(status)) || (name!=null && !("").equalsIgnoreCase(name)) || (amount>-1l)){
                        // Update campaign.
                        CampaignReturnValue result = campaignService.mutate(operations);
                        Long updatedCampaignId = 0l;
                        // Display campaigns.
                        for (Campaign campaignResult : result.getValue()) {
                                System.out.println("Campaign with name \"" + campaignResult.getName() + "\", id \""
                                                + campaignResult.getId() + "\", and budget delivery method \""
                                                + campaignResult.getBudget().getDeliveryMethod() + "\" was updated.");
                                updatedCampaignId = campaignResult.getId();
                        }
                        return updatedCampaignId;
                }
                                
                return 0l;
        }
}