1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.example.loanbroker.routers; |
12 | |
|
13 | |
import org.mule.DefaultMuleMessage; |
14 | |
import org.mule.api.MuleEvent; |
15 | |
import org.mule.api.MuleMessage; |
16 | |
import org.mule.example.loanbroker.LocaleMessage; |
17 | |
import org.mule.example.loanbroker.messages.LoanQuote; |
18 | |
import org.mule.routing.inbound.EventGroup; |
19 | |
|
20 | |
import java.util.Iterator; |
21 | |
|
22 | |
import org.apache.commons.logging.Log; |
23 | |
import org.apache.commons.logging.LogFactory; |
24 | |
|
25 | 0 | public class BankQuotesAggregationLogic |
26 | |
{ |
27 | |
|
28 | |
|
29 | |
|
30 | 0 | protected static final Log logger = LogFactory.getLog(BankQuotesAggregationLogic.class); |
31 | |
|
32 | |
public static MuleMessage aggregateEvents(EventGroup events) throws Exception |
33 | |
{ |
34 | 0 | LoanQuote lowestQuote = null; |
35 | 0 | LoanQuote quote = null; |
36 | 0 | MuleEvent event = null; |
37 | |
|
38 | 0 | for (Iterator iterator = events.iterator(); iterator.hasNext();) |
39 | |
{ |
40 | 0 | event = (MuleEvent)iterator.next(); |
41 | 0 | Object o = event.transformMessage(); |
42 | 0 | if(o instanceof LoanQuote) |
43 | |
{ |
44 | 0 | quote = (LoanQuote)o; |
45 | |
} |
46 | |
else |
47 | |
{ |
48 | 0 | throw new IllegalArgumentException("Object received by Aggregator is not of expected type. Wanted: " |
49 | |
+ LoanQuote.class.getName() + " Got: " + o); |
50 | |
} |
51 | 0 | logger.info(LocaleMessage.processingQuote(quote)); |
52 | |
|
53 | 0 | if (lowestQuote == null) |
54 | |
{ |
55 | 0 | lowestQuote = quote; |
56 | |
} |
57 | |
else |
58 | |
{ |
59 | 0 | if (quote.getInterestRate() < lowestQuote.getInterestRate()) |
60 | |
{ |
61 | 0 | lowestQuote = quote; |
62 | |
} |
63 | |
} |
64 | 0 | } |
65 | |
|
66 | 0 | logger.info(LocaleMessage.lowestQuote(lowestQuote)); |
67 | 0 | return new DefaultMuleMessage(lowestQuote, event.getMessage()); |
68 | |
} |
69 | |
} |