From 75d2add40cc8a511e10a1c71dca6dcd580045079 Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Fri, 18 Jun 2010 15:13:25 +0000 Subject: [PATCH] fixed loan quote Comparable implementation (for "best" quote) --- .../loan-broker/README.txt | 8 +-- .../loanbroker/LoanBrokerGateway.java | 6 +- .../loanbroker/LoanQuoteAggregator.java | 26 ++++---- .../loanbroker/domain/LoanQuote.java | 59 +++++++++---------- .../loanbroker/stubs/BankStub.java | 6 +- .../loanbroker/stubs/CreditBureauStub.java | 2 +- .../bank-channel-mappings-config.xml | 6 +- .../stubbed-loan-broker-multicast.xml | 2 +- .../bootstrap-config/stubbed-loan-broker.xml | 2 +- .../src/main/resources/loan-broker-config.xml | 2 +- .../main/resources/stub-services-config.xml | 4 +- .../loanbroker/demo/LoanBrokerDemo.java | 29 ++++----- .../demo/LoanBrokerSharkDetectorDemo.java | 9 +-- 13 files changed, 74 insertions(+), 87 deletions(-) diff --git a/spring-integration-samples/loan-broker/README.txt b/spring-integration-samples/loan-broker/README.txt index f4dc00622a..db6960698f 100644 --- a/spring-integration-samples/loan-broker/README.txt +++ b/spring-integration-samples/loan-broker/README.txt @@ -1,8 +1,8 @@ -Loan Broker sample is distributed as valid Eclipse/Maven project, so you can run it in two ways. +The Loan Broker sample is distributed as a valid Eclipse/Maven project, so you can run it in two ways. Maven (command line): 1. Execute 'mvn install' - You should see the output that looks similar to this: + You should see output that looks similar to this: . . . . . INFO : org.springframework.integration.loanbroker.demo.LoanBrokerDemo - ********* Best Quote: @@ -17,5 +17,5 @@ Maven (command line): . . . . . Eclipse/STS (with m2eclipse plug-in) -1. Import project into Eclipse/STS. If m2eclipse plugin is installed the dependency will be downloaded automatically -2. Run 'org.springframework.integration.loanbroker.demo.LoanBrokerDemo' class located in 'src/test/java' \ No newline at end of file +1. Import project into Eclipse/STS. If the m2eclipse plugin is installed, the dependencies will be downloaded automatically. +2. Run the 'org.springframework.integration.loanbroker.demo.LoanBrokerDemo' class located in 'src/test/java'. diff --git a/spring-integration-samples/loan-broker/src/main/java/org/springframework/integration/loanbroker/LoanBrokerGateway.java b/spring-integration-samples/loan-broker/src/main/java/org/springframework/integration/loanbroker/LoanBrokerGateway.java index df40190386..1e10aed099 100644 --- a/spring-integration-samples/loan-broker/src/main/java/org/springframework/integration/loanbroker/LoanBrokerGateway.java +++ b/spring-integration-samples/loan-broker/src/main/java/org/springframework/integration/loanbroker/LoanBrokerGateway.java @@ -22,8 +22,8 @@ import org.springframework.integration.loanbroker.domain.LoanQuote; import org.springframework.integration.loanbroker.domain.LoanRequest; /** - * POJI Gateway which connects method invocations to the request-channel - * completely isolating SI developer from SI API details. + * POJI Gateway that connects method invocations to the request-channel. + * This completely isolates the developer from Spring Integration API details. * * @author Oleg Zhurakousky */ @@ -32,7 +32,7 @@ public interface LoanBrokerGateway { /** * Will return the best {@link LoanQuote} for the given request. */ - public LoanQuote getLoanQuote(LoanRequest loanRequest); + public LoanQuote getBestLoanQuote(LoanRequest loanRequest); /** * Will return all {@link LoanQuote}s for the given request. diff --git a/spring-integration-samples/loan-broker/src/main/java/org/springframework/integration/loanbroker/LoanQuoteAggregator.java b/spring-integration-samples/loan-broker/src/main/java/org/springframework/integration/loanbroker/LoanQuoteAggregator.java index a5dc2eb99a..97ce1d608a 100644 --- a/spring-integration-samples/loan-broker/src/main/java/org/springframework/integration/loanbroker/LoanQuoteAggregator.java +++ b/spring-integration-samples/loan-broker/src/main/java/org/springframework/integration/loanbroker/LoanQuoteAggregator.java @@ -24,30 +24,30 @@ import org.springframework.integration.core.Message; import org.springframework.integration.loanbroker.domain.LoanQuote; /** - * Will aggregate {@link LoanQuote}s based on the value of the 'RESPONSE_TYPE' message header. - * The value of the 'RESPONSE_TYPE' header is set by the 'gateway' and is based on the type of - * {@link LoanBrokerGateway} method that was invoked by the client. - *

- * Will return the best {@link LoanQuote} if 'RESPONSE_TYPE' header value is 'BEST' else it will - * return all quotes. + * Aggregates {@link LoanQuote}s based on the value of the 'RESPONSE_TYPE' message header. + * When only the best quote is desired, the 'RESPONSE_TYPE' header should have a value + * of 'BEST'. In this example, that value is set by the 'gateway' when the + * {@link LoanBrokerGateway#getBestLoanQuote(org.springframework.integration.loanbroker.domain.LoanRequest)} + * method is invoked by the client. * * @author Oleg Zhurakousky */ public class LoanQuoteAggregator { + /** + * Aggregates LoanQuote Messages to return a single reply Message. + * + * @param messages Messages received from upstream lenders. + * @return the best {@link LoanQuote} if the 'RESPONSE_TYPE' header value is 'BEST' else all quotes + */ public Object aggregateQuotes(List> messages) { ArrayList payloads = new ArrayList(messages.size()); for (Message message : messages) { payloads.add(message.getPayload()); } + Collections.sort(payloads); String responseType = messages.get(0).getHeaders().get("RESPONSE_TYPE", String.class); - if ("BEST".equals(responseType)) { - Collections.sort(payloads); - return payloads.get(0); - } - else { - return payloads; - } + return ("BEST".equals(responseType)) ? payloads.get(0) : payloads; } } diff --git a/spring-integration-samples/loan-broker/src/main/java/org/springframework/integration/loanbroker/domain/LoanQuote.java b/spring-integration-samples/loan-broker/src/main/java/org/springframework/integration/loanbroker/domain/LoanQuote.java index 782751c614..5be22f15a4 100644 --- a/spring-integration-samples/loan-broker/src/main/java/org/springframework/integration/loanbroker/domain/LoanQuote.java +++ b/spring-integration-samples/loan-broker/src/main/java/org/springframework/integration/loanbroker/domain/LoanQuote.java @@ -27,10 +27,10 @@ public class LoanQuote implements Comparable{ private String lender; private Date quoteDate; private Date expirationDate; - private double loanAmount; - private int loanTerm; + private double amount; + private int term; private float rate; - + public String getLender() { return lender; } @@ -38,7 +38,7 @@ public class LoanQuote implements Comparable{ public void setLender(String lender) { this.lender = lender; } - + public Date getQuoteDate() { return quoteDate; } @@ -46,56 +46,55 @@ public class LoanQuote implements Comparable{ public void setQuoteDate(Date quoteDate) { this.quoteDate = quoteDate; } - + public Date getExpirationDate() { return expirationDate; } - + public void setExpirationDate(Date expirationDate) { this.expirationDate = expirationDate; } - - public double getLoanAmount() { - return loanAmount; + + public double getAmount() { + return amount; } - - public void setLoanAmount(double loanAmount) { - this.loanAmount = loanAmount; + + public void setAmount(double amount) { + this.amount = amount; } - - public int getLoanTerm() { - return loanTerm; + + public int getTerm() { + return term; } - - public void setLoanTerm(int loanTerm) { - this.loanTerm = loanTerm; + + public void setTerm(int term) { + this.term = term; } - + public float getRate() { return rate; } - + public void setRate(float rate) { this.rate = rate; } public String toString(){ return "\n====== Loan Quote =====\n" + - "Lender: " + lender + "\n" + - "Loan amount: " + new DecimalFormat("$###,###.###").format(loanAmount) + "\n" + - "Quotation Date: " + quoteDate + "\n" + - "Expiration Date: " + expirationDate + "\n" + - "Term: " + loanTerm + " years" + "\n" + - "Rate: " + rate + "%\n" + + "Lender: " + this.lender + "\n" + + "Loan amount: " + new DecimalFormat("$###,###.###").format(this.amount) + "\n" + + "Quote Date: " + this.quoteDate + "\n" + + "Expiration Date: " + this.expirationDate + "\n" + + "Term: " + this.term + " years" + "\n" + + "Rate: " + this.rate + "%\n" + "=======================\n"; - } - public int compareTo(LoanQuote loanQuote) { - if (loanQuote.rate > this.rate) { + public int compareTo(LoanQuote other) { + if (this.rate > other.rate) { return 1; } - else if (loanQuote.rate < this.rate) { + else if (this.rate < other.rate) { return -1; } return 0; diff --git a/spring-integration-samples/loan-broker/src/main/java/org/springframework/integration/loanbroker/stubs/BankStub.java b/spring-integration-samples/loan-broker/src/main/java/org/springframework/integration/loanbroker/stubs/BankStub.java index 008a3bf61b..4817027da5 100644 --- a/spring-integration-samples/loan-broker/src/main/java/org/springframework/integration/loanbroker/stubs/BankStub.java +++ b/spring-integration-samples/loan-broker/src/main/java/org/springframework/integration/loanbroker/stubs/BankStub.java @@ -31,7 +31,7 @@ public class BankStub { * @param loanRequest the loan request * @return a LoanQuote for the given request */ - public LoanQuote quote(LoanRequest loanRequest){ + public LoanQuote quote(LoanRequest loanRequest) { Calendar calendar = Calendar.getInstance(); LoanQuote loanQuote = new LoanQuote(); Random random = new Random(); @@ -39,8 +39,8 @@ public class BankStub { calendar.add(Calendar.DAY_OF_YEAR, random.nextInt(25)); loanQuote.setExpirationDate(calendar.getTime()); loanQuote.setRate(random.nextFloat() + 5); - loanQuote.setLoanTerm(10 + random.nextInt(10)); - loanQuote.setLoanAmount(250000 + random.nextInt(40000)); + loanQuote.setTerm(10 + random.nextInt(10)); + loanQuote.setAmount(250000 + random.nextInt(40000)); loanQuote.setLender("StubBank-" + random.nextInt(30)); return loanQuote; } diff --git a/spring-integration-samples/loan-broker/src/main/java/org/springframework/integration/loanbroker/stubs/CreditBureauStub.java b/spring-integration-samples/loan-broker/src/main/java/org/springframework/integration/loanbroker/stubs/CreditBureauStub.java index 597367a352..946398c33a 100644 --- a/spring-integration-samples/loan-broker/src/main/java/org/springframework/integration/loanbroker/stubs/CreditBureauStub.java +++ b/spring-integration-samples/loan-broker/src/main/java/org/springframework/integration/loanbroker/stubs/CreditBureauStub.java @@ -36,7 +36,7 @@ public class CreditBureauStub { */ public CreditScore getCreditScore(LoanRequest loanRequest){ Random random = new Random(); - int creditScore = 750 + random.nextInt(100); + int creditScore = 700 + random.nextInt(150); logger.info("Credit Score: " + creditScore); return new CreditScore(creditScore); } diff --git a/spring-integration-samples/loan-broker/src/main/resources/bank-channel-mappings-config.xml b/spring-integration-samples/loan-broker/src/main/resources/bank-channel-mappings-config.xml index 2d9be11c8a..8269ec2f9e 100644 --- a/spring-integration-samples/loan-broker/src/main/resources/bank-channel-mappings-config.xml +++ b/spring-integration-samples/loan-broker/src/main/resources/bank-channel-mappings-config.xml @@ -2,8 +2,8 @@ + xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> @@ -12,5 +12,5 @@ - + diff --git a/spring-integration-samples/loan-broker/src/main/resources/bootstrap-config/stubbed-loan-broker-multicast.xml b/spring-integration-samples/loan-broker/src/main/resources/bootstrap-config/stubbed-loan-broker-multicast.xml index 926cf943b2..bb8b2142da 100644 --- a/spring-integration-samples/loan-broker/src/main/resources/bootstrap-config/stubbed-loan-broker-multicast.xml +++ b/spring-integration-samples/loan-broker/src/main/resources/bootstrap-config/stubbed-loan-broker-multicast.xml @@ -1,7 +1,7 @@ + xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> diff --git a/spring-integration-samples/loan-broker/src/main/resources/bootstrap-config/stubbed-loan-broker.xml b/spring-integration-samples/loan-broker/src/main/resources/bootstrap-config/stubbed-loan-broker.xml index fc22b63cb7..5a0ddf442a 100644 --- a/spring-integration-samples/loan-broker/src/main/resources/bootstrap-config/stubbed-loan-broker.xml +++ b/spring-integration-samples/loan-broker/src/main/resources/bootstrap-config/stubbed-loan-broker.xml @@ -1,7 +1,7 @@ + xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> diff --git a/spring-integration-samples/loan-broker/src/main/resources/loan-broker-config.xml b/spring-integration-samples/loan-broker/src/main/resources/loan-broker-config.xml index 451b9f729a..e1dc66245a 100644 --- a/spring-integration-samples/loan-broker/src/main/resources/loan-broker-config.xml +++ b/spring-integration-samples/loan-broker/src/main/resources/loan-broker-config.xml @@ -13,7 +13,7 @@ - +

diff --git a/spring-integration-samples/loan-broker/src/main/resources/stub-services-config.xml b/spring-integration-samples/loan-broker/src/main/resources/stub-services-config.xml index e7a667b075..140babcd49 100644 --- a/spring-integration-samples/loan-broker/src/main/resources/stub-services-config.xml +++ b/spring-integration-samples/loan-broker/src/main/resources/stub-services-config.xml @@ -2,8 +2,8 @@ + xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd"> diff --git a/spring-integration-samples/loan-broker/src/test/java/org/springframework/integration/loanbroker/demo/LoanBrokerDemo.java b/spring-integration-samples/loan-broker/src/test/java/org/springframework/integration/loanbroker/demo/LoanBrokerDemo.java index 39f6df0f0b..93d4dde09b 100644 --- a/spring-integration-samples/loan-broker/src/test/java/org/springframework/integration/loanbroker/demo/LoanBrokerDemo.java +++ b/spring-integration-samples/loan-broker/src/test/java/org/springframework/integration/loanbroker/demo/LoanBrokerDemo.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2008 the original author or authors. + * Copyright 2002-2010 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,13 +13,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.integration.loanbroker.demo; import java.util.List; import org.apache.log4j.Logger; import org.junit.Test; -import org.springframework.context.ConfigurableApplicationContext; + +import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.integration.loanbroker.LoanBrokerGateway; import org.springframework.integration.loanbroker.domain.Customer; @@ -28,39 +30,28 @@ import org.springframework.integration.loanbroker.domain.LoanRequest; /** * @author Oleg Zhurakousky - * */ public class LoanBrokerDemo { + private static Logger logger = Logger.getLogger(LoanBrokerDemo.class); - /** - * @param args - */ public static void main(String[] args) { new LoanBrokerDemo().runDemo(); } - /** - * - */ + @Test public void runDemo() { - - ConfigurableApplicationContext ac = - new ClassPathXmlApplicationContext("bootstrap-config/stubbed-loan-broker.xml"); - - LoanBrokerGateway broker = ac.getBean("loanBrokerGateway", LoanBrokerGateway.class); + ApplicationContext context = new ClassPathXmlApplicationContext("bootstrap-config/stubbed-loan-broker.xml"); + LoanBrokerGateway broker = context.getBean("loanBrokerGateway", LoanBrokerGateway.class); LoanRequest loanRequest = new LoanRequest(); loanRequest.setCustomer(new Customer()); - - LoanQuote loan = broker.getLoanQuote(loanRequest); + LoanQuote loan = broker.getBestLoanQuote(loanRequest); logger.info("\n********* Best Quote: " + loan); - List loanQuotes = broker.getAllLoanQuotes(loanRequest); logger.info("\n********* All Quotes: "); for (LoanQuote loanQuote : loanQuotes) { logger.info(loanQuote); } - - ac.close(); } + } diff --git a/spring-integration-samples/loan-broker/src/test/java/org/springframework/integration/loanbroker/demo/LoanBrokerSharkDetectorDemo.java b/spring-integration-samples/loan-broker/src/test/java/org/springframework/integration/loanbroker/demo/LoanBrokerSharkDetectorDemo.java index 4fadc0d6b2..5d7478d422 100644 --- a/spring-integration-samples/loan-broker/src/test/java/org/springframework/integration/loanbroker/demo/LoanBrokerSharkDetectorDemo.java +++ b/spring-integration-samples/loan-broker/src/test/java/org/springframework/integration/loanbroker/demo/LoanBrokerSharkDetectorDemo.java @@ -32,6 +32,7 @@ import org.springframework.integration.loanbroker.domain.LoanRequest; * @author Gary Russell */ public class LoanBrokerSharkDetectorDemo { + private static Logger logger = Logger.getLogger(LoanBrokerSharkDetectorDemo.class); @Test @@ -39,19 +40,15 @@ public class LoanBrokerSharkDetectorDemo { ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("bootstrap-config/stubbed-loan-broker-multicast.xml"); LoanBrokerGateway broker = context.getBean("loanBrokerGateway", LoanBrokerGateway.class); - LoanRequest loanRequest = new LoanRequest(); loanRequest.setCustomer(new Customer()); - - LoanQuote loan = broker.getLoanQuote(loanRequest); + LoanQuote loan = broker.getBestLoanQuote(loanRequest); logger.info("\n********* Best Quote: " + loan); - List loanQuotes = broker.getAllLoanQuotes(loanRequest); logger.info("\n********* All Quotes: "); for (LoanQuote loanQuote : loanQuotes) { logger.info(loanQuote); } - context.close(); } - + }