View Javadoc

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