001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id$
023: */
024: package com.bostechcorp.cbesb.runtime.cbr;
025:
026: import javax.xml.transform.dom.DOMSource;
027: import javax.xml.xpath.XPath;
028: import javax.xml.xpath.XPathExpressionException;
029: import javax.xml.xpath.XPathFactory;
030:
031: import javax.jbi.messaging.MessageExchange;
032:
033: import org.apache.commons.lang.StringUtils;
034: import org.apache.commons.logging.Log;
035: import org.apache.commons.logging.LogFactory;
036: import org.w3c.dom.Document;
037:
038: import com.bostechcorp.cbesb.common.runtime.CbesbException;
039: import com.bostechcorp.cbesb.common.runtime.ConfigurationException;
040: import com.bostechcorp.cbesb.common.util.Dom;
041: import com.bostechcorp.cbesb.runtime.ccsl.lib.CcslUpoc;
042: import com.bostechcorp.cbesb.runtime.ccsl.lib.CcslUtil;
043:
044: public class TrxIDHandler {
045: protected transient Log logger = LogFactory
046: .getLog(TrxIDHandler.class);
047:
048: private static TrxIDHandler _instance;
049:
050: public static synchronized TrxIDHandler instance() {
051: if (_instance == null) {
052:
053: _instance = new TrxIDHandler();
054:
055: }
056: return _instance;
057: }
058:
059: public String processFixed(String inMessage, int offset, int length) {
060:
061: return StringUtils.mid(inMessage, offset, length);
062: }
063:
064: public String processCSV(String inMessage, String delimiter,
065: int index) throws CBRException {
066:
067: String[] strings = StringUtils.split(inMessage, delimiter);
068: try {
069: return strings[index - 1];
070: } catch (Exception e) {
071: //
072: // logger.error("Exception in processCSV(): " + e.getMessage());
073: // if (logger.isDebugEnabled()) {
074: // logger.debug("Exception in processCSV():", e);
075: // }
076: throw new CBRException(
077: "Exception occurred while obtaining TrxId for CSV type - "
078: + e.getMessage(),
079: "Make sure the message is in the correct format and the delimiter is correct.",
080: e);
081: }
082: }
083:
084: public String processX12(String inMessage) throws CBRException {
085: String startSeg = StringUtils.left(inMessage, 2);
086: if ("ST".equals(startSeg)) {
087: try {
088: String separator = StringUtils.mid(inMessage, 2, 1);
089: String[] strings = StringUtils.split(inMessage,
090: separator);
091:
092: return strings[1];
093: } catch (Exception e) {
094: throw new CBRException(
095: "Exception occurred while obtaining TrxId for X12 type - "
096: + e.getMessage(),
097: "Make sure the message is in X12 format.", e);
098: }
099: } else {
100: throw new CBRException(
101: "Failed to obtain X12 TrxId. X12 message starts with invalid segment '"
102: + startSeg + "'.",
103: "Make sure the message is in X12 format.");
104: }
105: }
106:
107: public String processHL7(String inMessage) throws CBRException {
108:
109: int firstCr = inMessage.indexOf(13);
110: String mshTag = StringUtils.left(inMessage, 3);
111: if (firstCr > 2 && "MSH".equals(mshTag)) {
112: String fieldSep = inMessage.substring(3, 4);
113: String[] mshFields = StringUtils.splitPreserveAllTokens(
114: inMessage.substring(0, firstCr), fieldSep);
115:
116: if (mshFields.length < 12) {
117: throw new CBRException(
118: "Failed to obtain HL7 TrxId. Invalid number of subfields in MSH segment: "
119: + mshFields.length,
120: "Make sure that MSH segment contains 12 fields.");
121: }
122: return StringUtils.replace(mshFields[8], "^", "_");
123:
124: } else {
125: throw new CBRException(
126: "Failed to obtain HL7 TrxId. HL7 message starts with invalid segment '"
127: + mshTag + "'.",
128: "Make sure the message is in HL7 format.");
129: }
130: }
131:
132: public String processXPATH(String expression, String inMessage)
133: throws CBRException {
134:
135: if (expression == null
136: || (StringUtils.strip(expression)).equals("")) {
137: return "";
138: } else {
139: XPath xpath = XPathFactory.newInstance().newXPath();
140: Document dom = Dom.createDocumentFromString(inMessage);
141:
142: try {
143: return xpath.evaluate(expression, dom);
144: } catch (XPathExpressionException e) {
145: throw new CBRException(
146: "Failed to obtain TrxId using XPath expression '"
147: + expression + "'.",
148: "Make sure the message is in XML format and the XPath expression is valid.",
149: e);
150: }
151: }
152: }
153:
154: public String processXPATH(String expression, DOMSource src)
155: throws CBRException {
156: if (expression == null
157: || (StringUtils.strip(expression)).equals("")) {
158: return "";
159: } else {
160: XPath xpath = XPathFactory.newInstance().newXPath();
161: Document dom = src.getNode().getOwnerDocument();
162: try {
163: return xpath.evaluate(expression, dom);
164: } catch (XPathExpressionException e) {
165: throw new CBRException(
166: "Failed to obtain TrxId using XPath expression '"
167: + expression + "'.",
168: "Make sure the message is in XML format and XPath expression is valid.",
169: e);
170: }
171: }
172: }
173:
174: public String processScript(MessageExchange exchange,
175: String scriptEngine, String scriptClass,
176: String scriptMethod, String suRootPath)
177: throws CbesbException {
178: try {
179:
180: Object[] calledArgs = { logger, exchange };
181:
182: String rootDir = "";
183:
184: try {
185: String[] result = CcslUtil.getRootScriptPath(
186: suRootPath, scriptClass);
187: rootDir = result[0];
188: scriptClass = result[1];
189: } catch (Exception e) {
190: throw new ConfigurationException(
191: "Failed to obtain TrxID using Script method because script file'"
192: + scriptClass + "' is not found.",
193: "Make sure the file exists in the 'src/scripts' folder.",
194: e);
195: }
196:
197: Object result = CcslUpoc.runScript(rootDir, scriptEngine,
198: scriptClass, scriptMethod, calledArgs, this );
199:
200: return result.toString();
201:
202: } catch (CbesbException e) {
203: throw new CBRException(
204: "Failed to obtain TrxId using script method '"
205: + scriptMethod + "' in class '"
206: + scriptClass + "'.",
207: "Check TrxId script.", e);
208: }
209:
210: }
211: }
|