private static final String PLACEMENT_ID = "INSERT_PLACEMENT_ID_HERE";
public static void runExample(DfaServices dfaServices, DfaSession session, String adName,
long campaignId, long sizeId, long creativeId, long placementId) throws Exception {
// Request the service.
AdRemote service = dfaServices.get(session, AdRemote.class);
// Create a rotation group.
RotationGroup rotationGroup = new RotationGroup();
rotationGroup.setId(0);
rotationGroup.setName(adName);
rotationGroup.setActive(true);
rotationGroup.setArchived(false);
rotationGroup.setCampaignId(campaignId);
rotationGroup.setSizeId(sizeId);
rotationGroup.setTypeId(1);
rotationGroup.setPriority(12);
rotationGroup.setRatio(1);
// Set the ad start date. This example uses tomorrow's date.
Calendar startTime = Calendar.getInstance();
startTime.add(Calendar.DATE, 1);
rotationGroup.setStartTime(startTime);
// Set the ad end date. This example uses one month from today's date.
Calendar endTime = Calendar.getInstance();
endTime.add(Calendar.MONTH, 1);
rotationGroup.setEndTime(endTime);
// Add creatives to the ad.
CreativeAssignment creativeAssignment = new CreativeAssignment();
creativeAssignment.setActive(true);
creativeAssignment.setCreativeId(creativeId);
// Create the click through URL.
ClickThroughUrl clickThroughUrl = new ClickThroughUrl();
clickThroughUrl.setDefaultLandingPageUsed(true);
clickThroughUrl.setLandingPageId(0);
creativeAssignment.setClickThroughUrl(clickThroughUrl);
// Create the creative assignments.
CreativeAssignment[] creativeAssignments = new CreativeAssignment[]{creativeAssignment};
rotationGroup.setCreativeAssignments(creativeAssignments);
rotationGroup.setRotationType(1);
// Assign an ad to the placement.
PlacementAssignment placementAssignment = new PlacementAssignment();
placementAssignment.setActive(true);
placementAssignment.setPlacementId(placementId);
PlacementAssignment[] placementAssignments = new PlacementAssignment[]{placementAssignment};
rotationGroup.setPlacementAssignments(placementAssignments);
// Save the rotation group.
AdSaveResult adSaveResult = service.saveAd(rotationGroup);
// Display the new ad ID.
System.out.printf("Ad with ID \"%s\" was created.%n", adSaveResult.getId());
}