* @throws Exception
*/
private static void updateFeedItems(AdWordsServices adWordsServices, AdWordsSession session,
Long feedId, FeedAttribute line1Attribute, FeedAttribute line2Attribute,
Map<Long, String[]> feedItemDescriptions) throws Exception {
FeedItemServiceInterface feedItemService =
adWordsServices.get(session, FeedItemServiceInterface.class);
Selector itemSelector = new Selector();
itemSelector.setFields(new String[] {"FeedId", "FeedItemId", "AttributeValues"});
List<String> feedItemIds = Lists.newArrayList(
Iterables.transform(feedItemDescriptions.keySet(), Functions.toStringFunction()));
itemSelector.setPredicates(new Predicate[] {
// Limit FeedItems to the feed.
new Predicate("FeedId", PredicateOperator.EQUALS, new String[] {feedId.toString()}),
// Limit FeedItems to the items in the feedItemDescriptions map.
new Predicate("FeedItemId", PredicateOperator.IN, feedItemIds.toArray(new String[0]))});
FeedItem[] feedItems = feedItemService.get(itemSelector).getEntries();
List<FeedItemOperation> itemOperations = Lists.newArrayListWithCapacity(feedItems.length);
for (FeedItem feedItem : feedItems) {
// Construct a FeedItemOperation that will set the line 1 and line 2
// attribute values for this FeedItem.
FeedItemAttributeValue[] itemAttributeValues = new FeedItemAttributeValue[2];
FeedItemAttributeValue line1AttributeValue = new FeedItemAttributeValue();
line1AttributeValue.setFeedAttributeId(line1Attribute.getId());
line1AttributeValue.setStringValue(feedItemDescriptions.get(feedItem.getFeedItemId())[0]);
itemAttributeValues[0] = line1AttributeValue;
FeedItemAttributeValue line2AttributeValue = new FeedItemAttributeValue();
line2AttributeValue.setFeedAttributeId(line2Attribute.getId());
line2AttributeValue.setStringValue(feedItemDescriptions.get(feedItem.getFeedItemId())[1]);
itemAttributeValues[1] = line2AttributeValue;
feedItem.setAttributeValues(itemAttributeValues);
FeedItemOperation operation = new FeedItemOperation();
operation.setOperator(Operator.SET);
operation.setOperand(feedItem);
itemOperations.add(operation);
}
FeedItemReturnValue itemsUpdateReturnValue =
feedItemService.mutate(itemOperations.toArray(new FeedItemOperation[0]));
System.out.printf("Updated %d items%n", itemsUpdateReturnValue.getValue().length);
}