com.microsoft.bingads.v13.bulk.entities.BulkAdGroup Maven / Gradle / Ivy
Show all versions of microsoft.bingads Show documentation
package com.microsoft.bingads.v13.bulk.entities;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.microsoft.bingads.internal.UncheckedParseException;
import com.microsoft.bingads.internal.functionalinterfaces.BiConsumer;
import com.microsoft.bingads.internal.functionalinterfaces.Function;
import com.microsoft.bingads.v13.bulk.BulkFileReader;
import com.microsoft.bingads.v13.bulk.BulkFileWriter;
import com.microsoft.bingads.v13.bulk.BulkOperation;
import com.microsoft.bingads.v13.bulk.BulkServiceManager;
import com.microsoft.bingads.v13.campaignmanagement.AdGroup;
import com.microsoft.bingads.v13.campaignmanagement.AdGroupPrivacyStatus;
import com.microsoft.bingads.v13.campaignmanagement.AdGroupStatus;
import com.microsoft.bingads.v13.campaignmanagement.ArrayOfSetting;
import com.microsoft.bingads.v13.campaignmanagement.ArrayOfTargetSettingDetail;
import com.microsoft.bingads.v13.campaignmanagement.BidOption;
import com.microsoft.bingads.v13.campaignmanagement.BiddingScheme;
import com.microsoft.bingads.v13.campaignmanagement.CoOpSetting;
import com.microsoft.bingads.v13.campaignmanagement.HotelSetting;
import com.microsoft.bingads.v13.campaignmanagement.InheritFromParentBiddingScheme;
import com.microsoft.bingads.v13.campaignmanagement.Network;
import com.microsoft.bingads.v13.campaignmanagement.RateAmount;
import com.microsoft.bingads.v13.campaignmanagement.RateBid;
import com.microsoft.bingads.v13.campaignmanagement.Setting;
import com.microsoft.bingads.v13.campaignmanagement.TargetSetting;
import com.microsoft.bingads.v13.campaignmanagement.TargetSettingDetail;
import com.microsoft.bingads.v13.internal.bulk.BulkMapping;
import com.microsoft.bingads.v13.internal.bulk.ComplexBulkMapping;
import com.microsoft.bingads.v13.internal.bulk.MappingHelpers;
import com.microsoft.bingads.v13.internal.bulk.RowValues;
import com.microsoft.bingads.v13.internal.bulk.SimpleBulkMapping;
import com.microsoft.bingads.v13.internal.bulk.StringExtensions;
import com.microsoft.bingads.v13.internal.bulk.StringTable;
import com.microsoft.bingads.v13.internal.bulk.entities.SingleRecordBulkEntity;
/**
* Represents an Ad Group.
*
*
* This class exposes the {@link #setAdGroup} and {@link #getAdGroup} that can be used to read and write
* fields of the Ad Group record in a bulk file.
*
*
*
* For more information, see Ad Group at
* https://go.microsoft.com/fwlink/?linkid=846127.
*
*
* @see BulkServiceManager
* @see BulkOperation
* @see BulkFileReader
* @see BulkFileWriter
*/
public class BulkAdGroup extends SingleRecordBulkEntity {
private Long campaignId;
private String campaignName;
private AdGroup adGroup;
private QualityScoreData qualityScoreData;
private static final List> MAPPINGS;
static {
List> m = new ArrayList>();
m.add(new SimpleBulkMapping(StringTable.Id,
new Function() {
@Override
public Long apply(BulkAdGroup c) {
return c.getAdGroup().getId();
}
},
new BiConsumer() {
@Override
public void accept(String v, BulkAdGroup c) {
c.getAdGroup().setId(StringExtensions.nullOrLong(v));
}
}
));
m.add(new SimpleBulkMapping(StringTable.Status,
new Function() {
@Override
public String apply(BulkAdGroup c) {
return c.getAdGroup().getStatus() != null ? c.getAdGroup().getStatus().value() : null;
}
},
new BiConsumer() {
@Override
public void accept(String v, BulkAdGroup c) {
c.getAdGroup().setStatus(StringExtensions.parseOptional(v, new Function() {
@Override
public AdGroupStatus apply(String value) {
return AdGroupStatus.fromValue(value);
}
}));
}
}
));
m.add(new SimpleBulkMapping(StringTable.ParentId,
new Function() {
@Override
public Long apply(BulkAdGroup c) {
return c.getCampaignId();
}
},
new BiConsumer() {
@Override
public void accept(String v, BulkAdGroup c) {
c.setCampaignId(StringExtensions.parseOptional(v, new Function() {
@Override
public Long apply(String value) {
return Long.parseLong(value);
}
}));
}
}
));
m.add(new SimpleBulkMapping(StringTable.Campaign,
new Function() {
@Override
public String apply(BulkAdGroup c) {
return c.getCampaignName();
}
},
new BiConsumer() {
@Override
public void accept(String v, BulkAdGroup c) {
c.setCampaignName(v);
}
}
));
m.add(new SimpleBulkMapping(StringTable.AdGroup,
new Function() {
@Override
public String apply(BulkAdGroup c) {
return c.getAdGroup().getName();
}
},
new BiConsumer() {
@Override
public void accept(String v, BulkAdGroup c) {
c.getAdGroup().setName(v);
}
}
));
m.add(new SimpleBulkMapping(StringTable.StartDate,
new Function() {
@Override
public String apply(BulkAdGroup c) {
return StringExtensions.toDateBulkString(c.getAdGroup().getStartDate());
}
},
new BiConsumer() {
@Override
public void accept(String v, BulkAdGroup c) {
try {
c.getAdGroup().setStartDate(StringExtensions.parseDate(v));
} catch (ParseException ex) {
throw new UncheckedParseException(ex);
}
}
}
));
m.add(new SimpleBulkMapping(StringTable.EndDate,
new Function() {
@Override
public String apply(BulkAdGroup c) {
return StringExtensions.toDateBulkString(c.getAdGroup().getEndDate());
}
},
new BiConsumer() {
@Override
public void accept(String v, BulkAdGroup c) {
try {
c.getAdGroup().setEndDate(StringExtensions.parseDate(v));
} catch (ParseException ex) {
throw new UncheckedParseException(ex);
}
}
}
));
m.add(new SimpleBulkMapping(StringTable.NetworkDistribution,
new Function() {
@Override
public String apply(BulkAdGroup c) {
return c.getAdGroup().getNetwork() != null ? c.getAdGroup().getNetwork().value() : null;
}
},
new BiConsumer() {
@Override
public void accept(String v, BulkAdGroup c) {
c.getAdGroup().setNetwork(StringExtensions.parseOptional(v, new Function() {
@Override
public Network apply(String value) {
return Network.fromValue(value);
}
}));
}
}
));
m.add(new SimpleBulkMapping(StringTable.AdRotation,
new Function() {
@Override
public String apply(BulkAdGroup c) {
return StringExtensions.toAdRotationBulkString(c.getAdGroup().getAdRotation());
}
},
new BiConsumer() {
@Override
public void accept(String v, BulkAdGroup c) {
c.getAdGroup().setAdRotation(StringExtensions.parseAdRotation(v));
}
}
));
m.add(new SimpleBulkMapping(StringTable.CpcBid,
new Function() {
@Override
public String apply(BulkAdGroup c) {
return StringExtensions.toAdGroupBidBulkString(c.getAdGroup().getCpcBid(), c.getAdGroup().getId());
}
},
new BiConsumer() {
@Override
public void accept(String v, BulkAdGroup c) {
c.getAdGroup().setCpcBid(StringExtensions.parseAdGroupBid(v));
}
}
));
m.add(new SimpleBulkMapping(StringTable.Language,
new Function() {
@Override
public String apply(BulkAdGroup c) {
return StringExtensions.toOptionalBulkString(c.getAdGroup().getLanguage(), c.getAdGroup().getId());
}
},
new BiConsumer() {
@Override
public void accept(String v, BulkAdGroup c) {
c.getAdGroup().setLanguage(StringExtensions.getValueOrEmptyString(v));
}
}
));
m.add(new SimpleBulkMapping(StringTable.BidAdjustment,
new Function() {
@Override
public Integer apply(BulkAdGroup c) {
return c.getAdGroup().getAudienceAdsBidAdjustment();
}
},
new BiConsumer() {
@Override
public void accept(String v, BulkAdGroup c) {
c.getAdGroup().setAudienceAdsBidAdjustment(StringExtensions.parseOptionalInteger(v));
}
}
));
m.add(new SimpleBulkMapping(StringTable.TrackingTemplate,
new Function() {
@Override
public String apply(BulkAdGroup c) {
return StringExtensions.toOptionalBulkString(c.getAdGroup().getTrackingUrlTemplate(), c.getAdGroup().getId());
}
},
new BiConsumer() {
@Override
public void accept(String v, BulkAdGroup c) {
c.getAdGroup().setTrackingUrlTemplate(StringExtensions.getValueOrEmptyString(v));
}
}
));
m.add(new SimpleBulkMapping(StringTable.CustomParameter,
new Function() {
@Override
public String apply(BulkAdGroup c) {
return StringExtensions.toCustomParaBulkString(c.getAdGroup().getUrlCustomParameters(), c.getAdGroup().getId());
}
},
new BiConsumer() {
@Override
public void accept(String v, BulkAdGroup c) {
try {
c.getAdGroup().setUrlCustomParameters(StringExtensions.parseCustomParameters(v));
} catch (Exception e) {
e.printStackTrace();
}
}
}
));
m.add(new ComplexBulkMapping(
new BiConsumer() {
@Override
public void accept(BulkAdGroup c, RowValues v) {
biddingSchemeToCsv(c, v);
}
},
new BiConsumer() {
@Override
public void accept(RowValues v, BulkAdGroup c) {
csvToBiddingScheme(v, c);
}
}
));
m.add(new ComplexBulkMapping(
new BiConsumer() {
@Override
public void accept(BulkAdGroup c, RowValues v) {
coOpSettingtoCsv(c, v);
}
},
new BiConsumer() {
@Override
public void accept(RowValues v, BulkAdGroup c) {
csvToCoOpSetting(v, c);
}
}
));
m.add(new SimpleBulkMapping(StringTable.TargetSetting,
new Function() {
@Override
public String apply(BulkAdGroup c) {
TargetSetting targetSetting = (TargetSetting)c.getSetting(TargetSetting.class);
return targetSetting == null? null : StringExtensions.toBulkString(targetSetting);
}
},
new BiConsumer() {
@Override
public void accept(String v, BulkAdGroup c) {
List targetSettingDetails = StringExtensions.parseTargetSettingDetails(v);
if (targetSettingDetails != null) {
TargetSetting targetSetting = new TargetSetting();
targetSetting.setType(TargetSetting.class.getSimpleName());
targetSetting.setDetails(new ArrayOfTargetSettingDetail());
targetSetting.getDetails().getTargetSettingDetails().addAll(targetSettingDetails);
c.addAdGroupSetting(targetSetting);
}
}
}
));
m.add(new SimpleBulkMapping(StringTable.PrivacyStatus,
new Function() {
@Override
public String apply(BulkAdGroup c) {
return c.getAdGroup().getPrivacyStatus() != null ? c.getAdGroup().getPrivacyStatus().value() : null;
}
},
new BiConsumer() {
@Override
public void accept(String v, BulkAdGroup c) {
c.getAdGroup().setPrivacyStatus(StringExtensions.parseOptional(v, new Function() {
@Override
public AdGroupPrivacyStatus apply(String value) {
return AdGroupPrivacyStatus.fromValue(value);
}
}));
}
}
));
m.add(new SimpleBulkMapping(StringTable.FinalUrlSuffix,
new Function() {
@Override
public String apply(BulkAdGroup c) {
return StringExtensions.toOptionalBulkString(c.getAdGroup().getFinalUrlSuffix(), c.getAdGroup().getId());
}
},
new BiConsumer() {
@Override
public void accept(String v, BulkAdGroup c) {
c.getAdGroup().setFinalUrlSuffix(StringExtensions.getValueOrEmptyString(v));
}
}
));
m.add(new SimpleBulkMapping(StringTable.AdScheduleUseSearcherTimeZone,
new Function() {
@Override
public String apply(BulkAdGroup t) {
if (t.getAdGroup().getAdScheduleUseSearcherTimeZone()== null) {
return null;
}
return StringExtensions.toUseSearcherTimeZoneBulkString(t.getAdGroup().getAdScheduleUseSearcherTimeZone(), null);
}
},
new BiConsumer() {
@Override
public void accept(String v, BulkAdGroup c) {
c.getAdGroup().setAdScheduleUseSearcherTimeZone(StringExtensions.parseUseSearcherTimeZone(v));
}
}
));
m.add(new SimpleBulkMapping(StringTable.AdGroupType,
new Function() {
@Override
public String apply(BulkAdGroup c) {
return c.getAdGroup().getAdGroupType();
}
},
new BiConsumer() {
@Override
public void accept(String v, BulkAdGroup c) {
c.getAdGroup().setAdGroupType(v);
}
}
));
m.add(new SimpleBulkMapping(StringTable.CpvBid,
new Function() {
@Override
public String apply(BulkAdGroup c) {
return StringExtensions.toAdGroupBidBulkString(c.getAdGroup().getCpvBid(), c.getAdGroup().getId());
}
},
new BiConsumer() {
@Override
public void accept(String v, BulkAdGroup c) {
c.getAdGroup().setCpvBid(StringExtensions.parseAdGroupBid(v));
}
}
));
m.add(new SimpleBulkMapping(StringTable.CpmBid,
new Function() {
@Override
public String apply(BulkAdGroup c) {
return StringExtensions.toAdGroupBidBulkString(c.getAdGroup().getCpmBid(), c.getAdGroup().getId());
}
},
new BiConsumer() {
@Override
public void accept(String v, BulkAdGroup c) {
c.getAdGroup().setCpmBid(StringExtensions.parseAdGroupBid(v));
}
}
));
m.add(new SimpleBulkMapping(StringTable.MultiMediaAdBidAdjustment,
new Function() {
@Override
public Integer apply(BulkAdGroup c) {
return c.getAdGroup().getMultimediaAdsBidAdjustment();
}
},
new BiConsumer() {
@Override
public void accept(String v, BulkAdGroup c) {
c.getAdGroup().setMultimediaAdsBidAdjustment(StringExtensions.parseOptionalInteger(v));
}
}
));
m.add(new SimpleBulkMapping(StringTable.UseOptimizedTargeting,
new Function() {
@Override
public String apply(BulkAdGroup c) {
return StringExtensions.toBooleanBulkString(c.getAdGroup().getUseOptimizedTargeting());
}
},
new BiConsumer() {
@Override
public void accept(String v, BulkAdGroup c) {
c.getAdGroup().setUseOptimizedTargeting(StringExtensions.parseOptional(v, new Function() {
@Override
public Boolean apply(String value) {
return Boolean.parseBoolean(value);
}
}));
}
}
));
m.add(new SimpleBulkMapping(StringTable.HotelAdGroupType,
new Function() {
@Override
public String apply(BulkAdGroup c) {
HotelSetting hotelSetting = (HotelSetting)c.getSetting(HotelSetting.class);
return StringExtensions.toBulkString(hotelSetting);
}
},
new BiConsumer() {
@Override
public void accept(String v, BulkAdGroup c) {
HotelSetting hotelSetting = StringExtensions.parseHotelSetting(v);
if (hotelSetting != null) {
c.addAdGroupSetting(hotelSetting);
}
}
}
));
m.add(new SimpleBulkMapping(StringTable.CommissionRate,
new Function() {
@Override
public String apply(BulkAdGroup c) {
RateBid rateBid = c.getAdGroup().getCommissionRate();
if (rateBid != null) {
RateAmount rateAmount = rateBid.getRateAmount();
if (rateAmount != null) {
return StringExtensions.toBulkString(rateAmount.getAmount());
}
}
return null;
}
},
new BiConsumer() {
@Override
public void accept(String v, BulkAdGroup c) {
Double d = StringExtensions.nullOrDouble(v);
if (d != null) {
RateAmount rateAmount = new RateAmount();
rateAmount.setAmount(d);
RateBid rateBid = new RateBid();
rateBid.setType("RateBid");
rateBid.setRateAmount(rateAmount);
c.getAdGroup().setCommissionRate(rateBid);
}
}
}
));
m.add(new SimpleBulkMapping(StringTable.PercentCpcBid,
new Function() {
@Override
public String apply(BulkAdGroup c) {
RateBid rateBid = c.getAdGroup().getPercentCpcBid();
if (rateBid != null) {
RateAmount rateAmount = rateBid.getRateAmount();
if (rateAmount != null) {
return StringExtensions.toBulkString(rateAmount.getAmount());
}
}
return null;
}
},
new BiConsumer() {
@Override
public void accept(String v, BulkAdGroup c) {
Double d = StringExtensions.nullOrDouble(v);
if (d != null) {
RateAmount rateAmount = new RateAmount();
rateAmount.setAmount(d);
RateBid rateBid = new RateBid();
rateBid.setType("RateBid");
rateBid.setRateAmount(rateAmount);
c.getAdGroup().setPercentCpcBid(rateBid);
}
}
}
));
m.add(new SimpleBulkMapping(StringTable.FrequencyCapSettings,
new Function() {
@Override
public String apply(BulkAdGroup c) {
return StringExtensions.toAdGroupFrequencyCapSettingsBulkString(c.getAdGroup().getFrequencyCapSettings(), c.getAdGroup().getId());
}
},
new BiConsumer() {
@Override
public void accept(String v, BulkAdGroup c) {
c.getAdGroup().setFrequencyCapSettings(StringExtensions.parseFrequencyCapSettings(v));
}
}
));
MAPPINGS = Collections.unmodifiableList(m);
}
public Setting getSetting(Class extends Setting> settingClass) {
if (adGroup.getSettings() == null || adGroup.getSettings().getSettings().size() == 0) {
return null;
}
Setting[] settings = adGroup.getSettings().getSettings().stream().filter(e -> e.getClass() == settingClass).toArray(Setting[]::new);
if (settings.length == 0) {
return null;
}
if (settings.length == 1)
{
return settings[0];
}
return null;
}
public void addAdGroupSetting(Setting setting) {
if (adGroup.getSettings() == null) {
adGroup.setSettings(new ArrayOfSetting());
}
adGroup.getSettings().getSettings().add(setting);
}
private static void csvToBiddingScheme(RowValues values, BulkAdGroup c) {
try {
String bidStrategyTypeRowValue = values.tryGet(StringTable.BidStrategyType);
if (bidStrategyTypeRowValue == null) {
return;
}
BiddingScheme biddingScheme = StringExtensions.parseBiddingScheme(bidStrategyTypeRowValue);
if (biddingScheme == null) {
return;
}
if (biddingScheme instanceof InheritFromParentBiddingScheme) {
InheritFromParentBiddingScheme inheritFromParentBiddingScheme = new InheritFromParentBiddingScheme();
inheritFromParentBiddingScheme.setType("InheritFromParent");
inheritFromParentBiddingScheme.setInheritedBidStrategyType(values.tryGet(StringTable.InheritedBidStrategyType));
c.getAdGroup().setBiddingScheme(inheritFromParentBiddingScheme);
} else {
c.getAdGroup().setBiddingScheme(biddingScheme);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static void biddingSchemeToCsv(BulkAdGroup c, RowValues values) {
BiddingScheme biddingScheme = c.getAdGroup().getBiddingScheme();
if (biddingScheme == null) {
return;
}
try {
values.put(StringTable.BidStrategyType, StringExtensions.toBiddingSchemeBulkString(biddingScheme));
if (biddingScheme instanceof InheritFromParentBiddingScheme) {
values.put(StringTable.InheritedBidStrategyType, ((InheritFromParentBiddingScheme) biddingScheme).getInheritedBidStrategyType());
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static void csvToCoOpSetting(RowValues values, BulkAdGroup c) {
BidOption bo = StringExtensions.parseOptional(values.tryGet(StringTable.BidOption), new Function() {
@Override
public BidOption apply(String value) {
return BidOption.fromValue(value);
}
});
Double boostValue = StringExtensions.parseOptional(values.tryGet(StringTable.BidBoostValue), new Function() {
@Override
public Double apply(String value) {
return Double.parseDouble(value);
}
});
Double maximumBid = StringExtensions.parseOptional(values.tryGet(StringTable.MaximumBid), new Function() {
@Override
public Double apply(String value) {
return Double.parseDouble(value);
}
});
if (bo != null || boostValue != null || maximumBid != null) {
CoOpSetting setting = new CoOpSetting();
setting.setType(CoOpSetting.class.getSimpleName());
setting.setBidOption(bo);
setting.setBidBoostValue(boostValue);
setting.setBidMaxValue(maximumBid);
c.addAdGroupSetting(setting);
}
}
private static void coOpSettingtoCsv(BulkAdGroup c, RowValues values) {
CoOpSetting setting = (CoOpSetting) c.getSetting(CoOpSetting.class);
if (setting == null) {
return;
}
if (setting.getBidOption() != null) {
values.put(StringTable.BidOption, StringExtensions.toBulkString(setting.getBidOption().value()));
}
values.put(StringTable.BidBoostValue, StringExtensions.toBulkString(setting.getBidBoostValue()));
values.put(StringTable.MaximumBid, StringExtensions.toBulkString(setting.getBidMaxValue()));
}
@Override
public void processMappingsFromRowValues(RowValues values) {
this.setAdGroup(new AdGroup());
MappingHelpers.convertToEntity(values, MAPPINGS, this);
qualityScoreData = QualityScoreData.readFromRowValuesOrNull(values);
}
@Override
public void processMappingsToRowValues(RowValues values, boolean excludeReadonlyData) {
validatePropertyNotNull(getAdGroup(), "AdGroup");
MappingHelpers.convertToValues(this, values, MAPPINGS);
if (!excludeReadonlyData) {
QualityScoreData.writeToRowValuesIfNotNull(qualityScoreData, values);
}
}
/**
* Gets the identifier of the campaign that contains the ad group.
*
*
* Corresponds to the 'Parent Id' field in the bulk file.
*
*/
public Long getCampaignId() {
return campaignId;
}
/**
* Sets the identifier of the campaign that contains the ad group.
*
*
* Corresponds to the 'Parent Id' field in the bulk file.
*
*/
public void setCampaignId(Long campaignId) {
this.campaignId = campaignId;
}
/**
* Gets the name of the campaign that contains the ad group.
*
*
* Corresponds to the 'Campaign' field in the bulk file.
*
*/
public String getCampaignName() {
return campaignName;
}
/**
* Sets the name of the campaign that contains the ad group.
*
*
* Corresponds to the 'Campaign' field in the bulk file.
*
*/
public void setCampaignName(String campaignName) {
this.campaignName = campaignName;
}
/**
* Gets the AdGroup Data Object of the Campaign Management Service.
*
*
* A subset of AdGroup properties are available in the Ad Group record.
* For more information, see Ad Group at
* https://go.microsoft.com/fwlink/?linkid=846127.
*
*/
public AdGroup getAdGroup() {
return adGroup;
}
/**
* Sets the AdGroup Data Object of the Campaign Management Service.
*
*
* A subset of AdGroup properties are available in the Ad Group record.
* For more information, see Ad Group at
* https://go.microsoft.com/fwlink/?linkid=846127.
*
*/
public void setAdGroup(AdGroup adGroup) {
this.adGroup = adGroup;
}
/**
* Gets the quality score data for the ad group.
*/
public QualityScoreData getQualityScoreData() {
return qualityScoreData;
}
}