Coverage Report - org.mule.transformer.simple.CombineCollectionsTransformer
 
Classes in this File Line Coverage Branch Coverage Complexity
CombineCollectionsTransformer
0%
0/22
0%
0/12
0
 
 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  0
 public class CombineCollectionsTransformer implements MessageProcessor
 31  
 {
 32  
 
 33  
     public MuleEvent process(MuleEvent event) throws MuleException
 34  
     {
 35  0
         MuleMessage msg = event.getMessage();
 36  
 
 37  0
         List<Object> payload = new ArrayList<Object>();
 38  0
         if (msg instanceof MuleMessageCollection)
 39  
         {
 40  0
             MuleMessageCollection collection = (MuleMessageCollection) msg;
 41  0
             collection.getPayload();
 42  0
             for (MuleMessage child : collection.getMessagesAsArray())
 43  
             {
 44  0
                 Object childPayload = child.getPayload();
 45  0
                 if (childPayload instanceof Collection)
 46  
                 {
 47  0
                     payload.addAll((Collection) childPayload);
 48  
                 }
 49  
                 else
 50  
                 {
 51  0
                     payload.add(childPayload);
 52  
                 }
 53  
             }
 54  0
         } 
 55  0
         else if (msg.getPayload() instanceof Collection)
 56  
         {
 57  0
             add(payload, (Collection)msg.getPayload());
 58  
         }
 59  
         else
 60  
         {
 61  0
             payload.add(msg.getPayload());
 62  
         }
 63  
 
 64  0
         DefaultMuleMessage listMessage = new DefaultMuleMessage(payload, msg, msg.getMuleContext());
 65  0
         return new DefaultMuleEvent(listMessage, event);
 66  
     }
 67  
 
 68  
     private void add(List<Object> newPayload, Collection existingPayload)
 69  
     {
 70  0
         for (Object o : existingPayload)
 71  
         {
 72  0
             if (o instanceof Collection)
 73  
             {
 74  0
                 newPayload.addAll((Collection)o);
 75  
             }
 76  
             else 
 77  
             {
 78  0
                 newPayload.add(o);
 79  
             }
 80  
         }
 81  0
     }
 82  
 }