View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transformer.simple;
8   
9   import org.mule.DefaultMuleEvent;
10  import org.mule.DefaultMuleMessage;
11  import org.mule.api.MuleEvent;
12  import org.mule.api.MuleException;
13  import org.mule.api.MuleMessage;
14  import org.mule.api.MuleMessageCollection;
15  import org.mule.api.processor.MessageProcessor;
16  
17  import java.util.ArrayList;
18  import java.util.Collection;
19  import java.util.List;
20  
21  /**
22   * 
23   * Takes a payload which is a Collection of Collections and turns into a single List. For example, if the payload is a Collection
24   * which contains a Collection with elements A and B and another Collection with elements C and D, this will turn them into
25   * a single Collection with elements A, B, C and D.
26   *    
27   * This transformer will also work on MuleMessageCollections. In this case, it will take the individual Collection
28   * payloads of each MuleMessage and merge them into a single Collection on a new MuleMessage.
29   */
30  public class CombineCollectionsTransformer implements MessageProcessor
31  {
32  
33      public MuleEvent process(MuleEvent event) throws MuleException
34      {
35          MuleMessage msg = event.getMessage();
36  
37          List<Object> payload = new ArrayList<Object>();
38          if (msg instanceof MuleMessageCollection)
39          {
40              MuleMessageCollection collection = (MuleMessageCollection) msg;
41              collection.getPayload();
42              for (MuleMessage child : collection.getMessagesAsArray())
43              {
44                  Object childPayload = child.getPayload();
45                  if (childPayload instanceof Collection)
46                  {
47                      payload.addAll((Collection) childPayload);
48                  }
49                  else
50                  {
51                      payload.add(childPayload);
52                  }
53              }
54          } 
55          else if (msg.getPayload() instanceof Collection)
56          {
57              add(payload, (Collection)msg.getPayload());
58          }
59          else
60          {
61              payload.add(msg.getPayload());
62          }
63  
64          DefaultMuleMessage listMessage = new DefaultMuleMessage(payload, msg, msg.getMuleContext());
65          return new DefaultMuleEvent(listMessage, event);
66      }
67  
68      private void add(List<Object> newPayload, Collection existingPayload)
69      {
70          for (Object o : existingPayload)
71          {
72              if (o instanceof Collection)
73              {
74                  newPayload.addAll((Collection)o);
75              }
76              else 
77              {
78                  newPayload.add(o);
79              }
80          }
81      }
82  }