View Javadoc

1   /*
2    * $Id: LowestQuoteProcessor.java 19574 2010-09-10 16:42:37Z aperepel $
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.example.loanbroker.processor;
12  
13  import org.mule.DefaultMuleEvent;
14  import org.mule.DefaultMuleMessage;
15  import org.mule.api.MuleEvent;
16  import org.mule.api.processor.MessageProcessor;
17  import org.mule.api.transformer.TransformerException;
18  import org.mule.example.loanbroker.model.LoanQuote;
19  
20  import java.util.List;
21  
22  public class LowestQuoteProcessor implements MessageProcessor
23  {
24  
25      public MuleEvent process(MuleEvent event) throws TransformerException
26      {
27  
28          Object payload = event.getMessage().getPayload();
29          LoanQuote lowestQuote = null;
30  
31          if (payload instanceof LoanQuote)
32          {
33              lowestQuote = (LoanQuote) payload;
34          }
35          else
36          {
37              @SuppressWarnings("unchecked")
38              List<LoanQuote> loanQuotes = (List<LoanQuote>) payload;
39              for (LoanQuote loanQuote : loanQuotes)
40              {
41  
42                  if (lowestQuote == null)
43                  {
44                      lowestQuote = loanQuote;
45                  }
46                  else
47                  {
48                      if (loanQuote.getInterestRate() < lowestQuote.getInterestRate())
49                      {
50                          lowestQuote = loanQuote;
51                      }
52                  }
53              }
54          }
55          return new DefaultMuleEvent(new DefaultMuleMessage(lowestQuote, event.getMuleContext()), event);
56      }
57  
58  }