001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package samples.mediators;
021:
022: import org.apache.synapse.MessageContext;
023: import org.apache.synapse.Mediator;
024: import org.apache.synapse.mediators.AbstractMediator;
025: import org.apache.axiom.om.OMElement;
026: import org.apache.axiom.om.OMAbstractFactory;
027: import org.apache.axiom.om.OMFactory;
028: import org.apache.axiom.soap.SOAPFactory;
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031:
032: import javax.xml.namespace.QName;
033:
034: public class DiscountQuoteMediator extends AbstractMediator {
035:
036: private static final Log log = LogFactory
037: .getLog(DiscountQuoteMediator.class);
038:
039: private String discountFactor = "10";
040:
041: private String bonusFor = "10";
042:
043: private int bonusCount = 0;
044:
045: public DiscountQuoteMediator() {
046: }
047:
048: public boolean mediate(MessageContext mc) {
049:
050: String price = mc
051: .getEnvelope()
052: .getBody()
053: .getFirstElement()
054: .getFirstElement()
055: .getFirstChildWithName(
056: new QName("http://services.samples/xsd", "last"))
057: .getText();
058:
059: //converting String properties into integers
060: int discount = Integer.parseInt(discountFactor);
061: int bonusNo = Integer.parseInt(bonusFor);
062: double currentPrice = Double.parseDouble(price);
063:
064: //discounting factor is deducted from current price form every response
065: Double lastPrice = new Double(currentPrice - currentPrice
066: * discount / 100);
067:
068: //Special discount of 5% offers for the first responses as set in the bonusFor property
069: if (bonusCount <= bonusNo) {
070: lastPrice = new Double(lastPrice.doubleValue()
071: - lastPrice.doubleValue() * 0.05);
072: bonusCount++;
073: }
074:
075: String discountedPrice = lastPrice.toString();
076:
077: mc
078: .getEnvelope()
079: .getBody()
080: .getFirstElement()
081: .getFirstElement()
082: .getFirstChildWithName(
083: new QName("http://services.samples/xsd", "last"))
084: .setText(discountedPrice);
085:
086: System.out.println("Quote value discounted.");
087: System.out.println("Original price: " + price);
088: System.out.println("Discounted price: " + discountedPrice);
089:
090: return true;
091: }
092:
093: public String getType() {
094: return null;
095: }
096:
097: public void setTraceState(int traceState) {
098: traceState = 0;
099: }
100:
101: public int getTraceState() {
102: return 0;
103: }
104:
105: public void setDiscountFactor(String discount) {
106: discountFactor = discount;
107: }
108:
109: public String getDiscountFactor() {
110: return discountFactor;
111: }
112:
113: public void setBonusFor(String bonus) {
114: bonusFor = bonus;
115: }
116:
117: public String getBonusFor() {
118: return bonusFor;
119: }
120: }
|