001: /**
002: * CreditBindingImpl.java
003: *
004: * This file was auto-generated from WSDL
005: * by the Apache Axis WSDL2Java emitter.
006: */package org.wfmc.sample;
007:
008: import java.math.BigDecimal;
009: import java.rmi.RemoteException;
010: import java.util.Collections;
011: import java.util.HashMap;
012: import java.util.Map;
013:
014: /**
015: * Implements the Web Service used by the WfMC Sample Workflow Process.
016: * <p/>
017: * <em>Caveat emptor:</em> Like the WfMC sample workflow itself, this class
018: * implements a simplistic treatment of the problem domain. There's no
019: * synchronization, transactionality, persistence, etc. In other words, don't
020: * try and use any of this stuff as the basis for a real-life application!
021: */
022: public class CreditBindingImpl implements CreditServicePortType {
023: private static final String INVALID_DATA = "InvalidData";
024: private static final String ACCEPT = "Accept";
025: private static final String BAD_CREDIT = "BadCredit";
026: private static final String MC_VISA = "MC-VISA";
027: private static final String DISCOVER = "Discover";
028: private static final String AMEX = "AMEX";
029: private static final String[] _merchantNumbers = { "OBE2004",
030: "WfMC2003" };
031: private static final String[][] _accountInfoStrings = {
032: // {accountNumber, cardType, creditLimit, balance}
033: { "123456", MC_VISA, "1000", "0" },
034: { "234567", DISCOVER, "2000", "1900" },
035: { "345678", AMEX, "3000", "2100" },
036: { "456789", MC_VISA, "4000", "3999" }, };
037: private static final Map _accountInfo = Collections
038: .synchronizedMap(new HashMap());
039:
040: static {
041: for (int i = 0; i < _accountInfoStrings.length; i++) {
042: String[] strings = _accountInfoStrings[i];
043: _accountInfo.put(strings[0], new AccountInfo(strings));
044: }
045: }
046:
047: private static class AccountInfo {
048: final String accountNumber;
049: final String cardType;
050: BigDecimal creditLimit;
051: BigDecimal balance;
052:
053: AccountInfo(String[] strings) {
054: accountNumber = strings[0];
055: cardType = strings[1];
056: creditLimit = new BigDecimal(strings[2]);
057: balance = new BigDecimal(strings[3]);
058: }
059: }
060:
061: private static boolean validateMerchantNumber(String merchantNumber) {
062: for (int i = 0; i < _merchantNumbers.length; i++) {
063: if (_merchantNumbers[i].equals(merchantNumber))
064: return true;
065: }
066: return false;
067: }
068:
069: private String checkCreditLimit(CreditInfo creditInfo,
070: boolean applyCharge) {
071: String result = INVALID_DATA;
072: if (validateMerchantNumber(creditInfo.getMerchantNumber())) {
073: AccountInfo acInfo = (AccountInfo) _accountInfo
074: .get(creditInfo.getAccountNumber());
075: if (acInfo != null) {
076: synchronized (acInfo) {
077: BigDecimal newBalance = acInfo.balance
078: .add(creditInfo.getAmount());
079: if (newBalance.compareTo(acInfo.creditLimit) > 0) {
080: if (applyCharge)
081: acInfo.balance = newBalance;
082: result = ACCEPT;
083: } else {
084: result = BAD_CREDIT;
085: }
086: }
087: }
088: }
089: return result;
090: }
091:
092: public String getCreditAuthorization(CreditInfo creditInfo)
093: throws RemoteException {
094:
095: return checkCreditLimit(creditInfo, false);
096: }
097:
098: public String chargeCreditAccount(CreditInfo creditInfo)
099: throws RemoteException {
100:
101: return checkCreditLimit(creditInfo, true);
102: }
103: }
|