001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.loanbroker.webservice.version;
018    
019    import org.apache.camel.Exchange;
020    import org.apache.camel.loanbroker.webservice.version.bank.BankQuote;
021    import org.apache.camel.processor.aggregate.AggregationStrategy;
022    
023    //START SNIPPET: aggregating
024    public class BankResponseAggregationStrategy implements AggregationStrategy {
025    
026        public static final String BANK_QUOTE = "bank_quote";
027    
028        public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
029            // the first time we only have the new exchange
030            if (oldExchange == null) {
031                return newExchange;
032            }
033    
034            // Get the bank quote instance from the exchange
035            BankQuote oldQuote = oldExchange.getProperty(BANK_QUOTE, BankQuote.class);
036            // Get the oldQute from out message body if we can't get it from the exchange
037            if (oldQuote == null) {
038                oldQuote = oldExchange.getIn().getBody(BankQuote.class);
039            }
040            // Get the newQuote
041            BankQuote newQuote = newExchange.getIn().getBody(BankQuote.class);
042            Exchange result = null;
043            BankQuote bankQuote;
044    
045            if (newQuote.getRate() >= oldQuote.getRate()) {
046                result = oldExchange;
047                bankQuote = oldQuote;
048            } else {
049                result = newExchange;
050                bankQuote = newQuote;
051            }
052            // Set the lower rate BankQuote instance back to aggregated exchange
053            result.setProperty(BANK_QUOTE, bankQuote);
054            // Set the return message for the client
055            result.getOut().setBody("The best rate is " + bankQuote.toString());
056    
057            return result;
058        }
059    
060    }
061    //END SNIPPET: aggregating