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