* @throws Exception
*/
private static void updateFeedMapping(AdWordsServices adWordsServices, AdWordsSession session,
Long feedId, FeedAttribute line1FeedAttribute, FeedAttribute line2FeedAttribute)
throws Exception {
FeedMappingServiceInterface mappingService =
adWordsServices.get(session, FeedMappingServiceInterface.class);
Selector selector = new Selector();
selector.setFields(
new String[] {"FeedId", "FeedMappingId", "PlaceholderType", "AttributeFieldMappings"});
selector.setPredicates(new Predicate[] {
new Predicate("FeedId", PredicateOperator.EQUALS, new String[] {feedId.toString()})});
FeedMapping feedMapping = mappingService.get(selector).getEntries()[0];
// Remove the existing mapping (FeedMapping is immutable).
feedMapping = mappingService.mutate(new FeedMappingOperation[] {
new FeedMappingOperation(Operator.REMOVE, null, feedMapping)}).getValue(0);
// Create line 1 and line 2 attribute field mappings.
AttributeFieldMapping line1FieldMapping = new AttributeFieldMapping();
line1FieldMapping.setFeedAttributeId(line1FeedAttribute.getId());
line1FieldMapping.setFieldId(PLACEHOLDER_FIELD_LINE_1_TEXT);
AttributeFieldMapping line2FieldMapping = new AttributeFieldMapping();
line2FieldMapping.setFeedAttributeId(line2FeedAttribute.getId());
line2FieldMapping.setFieldId(PLACEHOLDER_FIELD_LINE_2_TEXT);
// Combine the existing field mappings with the new mappings.
List<AttributeFieldMapping> allFieldMappings = new ArrayList<AttributeFieldMapping>();
// Include existing mappings.
allFieldMappings.addAll(Arrays.asList(feedMapping.getAttributeFieldMappings()));
// Add the new mappings for line 1 and line 2.
allFieldMappings.add(line1FieldMapping);
allFieldMappings.add(line2FieldMapping);
feedMapping.setAttributeFieldMappings(allFieldMappings.toArray(new AttributeFieldMapping[0]));
FeedMappingReturnValue mappingReturnValue = mappingService.mutate(
new FeedMappingOperation[] {new FeedMappingOperation(Operator.ADD, null, feedMapping)});
FeedMapping mutatedMapping = mappingReturnValue.getValue()[0];
System.out.printf("Updated field mappings for feedId %d and feedMappingId %d to:%n",