View Javadoc

1   /*
2    * $Id: PaymentValidationResponseAggregator.java 7976 2007-08-21 14:26:13Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.samples.voipservice.routers;
12  
13  import org.mule.config.i18n.MessageFactory;
14  import org.mule.impl.MuleMessage;
15  import org.mule.routing.inbound.EventGroup;
16  import org.mule.routing.response.ResponseCorrelationAggregator;
17  import org.mule.samples.voipservice.to.CreditProfileTO;
18  import org.mule.umo.UMOEvent;
19  import org.mule.umo.UMOMessage;
20  import org.mule.umo.routing.RoutingException;
21  import org.mule.umo.transformer.TransformerException;
22  
23  import java.util.Iterator;
24  import java.util.Map;
25  
26  public class PaymentValidationResponseAggregator extends ResponseCorrelationAggregator
27  {
28  
29      protected UMOMessage aggregateEvents(EventGroup events) throws RoutingException
30      {
31          UMOEvent event = null;
32          boolean one = false;
33          boolean two = false;
34          CreditProfileTO creditProfileTO = null;
35  
36          try
37          {
38              for (Iterator iterator = events.iterator(); iterator.hasNext();)
39              {
40                  event = (UMOEvent)iterator.next();
41                  creditProfileTO = (CreditProfileTO)event.getTransformedMessage();
42  
43                  if (creditProfileTO.getCreditScore() >= CreditProfileTO.CREDIT_LIMIT)
44                  {
45                      one = true;
46                  }
47  
48                  if (creditProfileTO.getCreditAuthorisedStatus() == CreditProfileTO.CREDIT_AUTHORISED)
49                  {
50                      two = true;
51                  }
52              }
53          }
54          catch (TransformerException e)
55          {
56              throw new RoutingException(MessageFactory.createStaticMessage("Failed to validate payment service"),
57                  new MuleMessage(events, (Map)null), null, e);
58          }
59  
60          if (one && two && creditProfileTO != null)
61          {
62              creditProfileTO.setValid(true);
63          }
64  
65          return new MuleMessage(creditProfileTO, event.getMessage());
66      }
67  
68  }