// Get DfpUser from "~/dfp.properties".
DfpUser user = new DfpUser();
// Get the CreativeService.
CreativeServiceInterface creativeService =
user.getService(DfpService.V201308.CREATIVE_SERVICE);
Long[] imageCreativeIds = new Long[] {Long.parseLong("INSERT_IMAGE_CREATIVE_ID_HERE")};
// Create the statement to filter image creatives by id.
StatementBuilder statementBuilder =
new StatementBuilder("WHERE id IN ( " + StringUtils.join(imageCreativeIds, ',')
+ ") and creativeType = :creativeType LIMIT 500").putValue("creativeType",
ImageCreative.class.getSimpleName());
// Retrieve all creatives which match.
CreativePage page =
creativeService.getCreativesByStatement(statementBuilder.toStatement());
if (page.getResults() != null) {
Creative[] creatives = page.getResults();
long[] oldIds = new long[creatives.length];
for (int i = 0; i < creatives.length; i++) {
ImageCreative imageCreative = (ImageCreative) creatives[i];
oldIds[i] = imageCreative.getId();
imageCreative.setId(null);
imageCreative.setAdvertiserId(imageCreative.getAdvertiserId());
imageCreative.setName(
imageCreative.getName() + " (Copy #" + System.currentTimeMillis() + ")");
// Create image asset.
CreativeAsset creativeAsset = new CreativeAsset();
creativeAsset.setFileName("image.jpg");
creativeAsset.setAssetByteArray(
MediaUtils.getAssetDataFromUrl(imageCreative.getPrimaryImageAsset().getAssetUrl()));
creativeAsset.setSize(imageCreative.getPrimaryImageAsset().getSize());
imageCreative.setPrimaryImageAsset(creativeAsset);
creatives[i] = imageCreative;
}
// Create the copied creative.
creatives = creativeService.createCreatives(creatives);
// Display copied creatives.
for (int i = 0; i < creatives.length; i++) {
System.out.println("Image creative with ID \"" + oldIds[i] + "\" copied to ID \""
+ creatives[i].getId() + "\".");