[1227] | 1 | package gov.va.med.edp.vistalink;
|
---|
| 2 |
|
---|
| 3 | import gov.va.med.exception.FoundationsException;
|
---|
| 4 | import gov.va.med.vistalink.adapter.cci.VistaLinkAppProxyConnectionSpec;
|
---|
| 5 | import gov.va.med.vistalink.adapter.cci.VistaLinkConnection;
|
---|
| 6 | import gov.va.med.vistalink.adapter.cci.VistaLinkDuzConnectionSpec;
|
---|
| 7 | import gov.va.med.vistalink.adapter.record.LoginsDisabledFaultException;
|
---|
| 8 | import gov.va.med.vistalink.adapter.record.VistaLinkFaultException;
|
---|
| 9 | import gov.va.med.vistalink.rpc.*;
|
---|
| 10 | import gov.va.med.vistalink.security.m.SecurityFaultException;
|
---|
| 11 | import org.springframework.dao.*;
|
---|
| 12 | import org.springframework.jca.cci.core.CciTemplate;
|
---|
| 13 | import org.springframework.jca.cci.core.ConnectionCallback;
|
---|
| 14 | import org.springframework.util.Assert;
|
---|
| 15 | import org.slf4j.LoggerFactory;
|
---|
| 16 | import org.slf4j.Logger;
|
---|
| 17 |
|
---|
| 18 | import javax.resource.ResourceException;
|
---|
| 19 | import javax.resource.cci.Connection;
|
---|
| 20 | import javax.resource.cci.ConnectionFactory;
|
---|
| 21 | import javax.resource.cci.ConnectionSpec;
|
---|
| 22 | import java.sql.SQLException;
|
---|
| 23 | import java.text.MessageFormat;
|
---|
| 24 | import java.util.List;
|
---|
| 25 |
|
---|
| 26 | /**
|
---|
| 27 | * This is the central class in the rpc package. It simplifies the use of VistaLink RPCs and helps to avoid common errors.
|
---|
| 28 | * It executes core RPC workflow, leaving application code to provide RPC names and parameters and extract results. This
|
---|
| 29 | * class executes RPCs with VistaLink, initiating iteration over RPC results and catching VistaLink exceptions and
|
---|
| 30 | * translating them to the generic data access exception hierarchy defined in the org.springframework.dao package.
|
---|
| 31 | */
|
---|
| 32 | public class VistaLinkTemplate {
|
---|
| 33 |
|
---|
| 34 | static final int DEFAULT_TIMEOUT = 10000;
|
---|
| 35 |
|
---|
| 36 | private static Logger log = LoggerFactory.getLogger(VistaLinkTemplate.class);
|
---|
| 37 |
|
---|
| 38 | private int timeOut = DEFAULT_TIMEOUT;
|
---|
| 39 | private ConnectionFactoryLocator connectionFactoryLocator;
|
---|
| 40 | private static final String UNABLE_TO_EXECUTE_RPC = "Unable to execute RPC";
|
---|
| 41 |
|
---|
| 42 | public VistaLinkTemplate(ConnectionFactoryLocator connectionFactoryLocator) {
|
---|
| 43 | this.connectionFactoryLocator = connectionFactoryLocator;
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | public String rpcAsUser(final String division, final String duz, final String rpcContext, final String rpcName) throws DataAccessException {
|
---|
| 47 | return rpcAsUser(division, duz, rpcContext, rpcName, (List) null);
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | // TODO: Uncomment me on java 5
|
---|
| 51 | // public String rpcAsUser(final String division, final String duz, final String rpcContext, final String rpcName, Object... params) throws DataAccessException {
|
---|
| 52 | // return rpcAsUser(division, duz, rpcContext, rpcName, createParameterList(params));
|
---|
| 53 | // }
|
---|
| 54 |
|
---|
| 55 | public String rpcAsUser(final String division, final String duz, final String rpcContext, final String rpcName, final List params) throws DataAccessException {
|
---|
| 56 | return rpc(new VistaLinkDuzConnectionSpec(division, duz), division, duz, rpcContext, rpcName, params);
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | public String rpcAsApplication(final String division, final String applicationProxyName, final String rpcContext, final String rpcName) throws DataAccessException {
|
---|
| 60 | return rpcAsApplication(division, applicationProxyName, rpcContext, rpcName, (List) null);
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | // TODO: Uncomment me on java 5
|
---|
| 64 | // public String rpcAsApplication(final String division, final String applicationProxyName, final String rpcContext, final String rpcName, Object... params) throws DataAccessException {
|
---|
| 65 | // return rpcAsApplication(division, applicationProxyName, rpcContext, rpcName, createParameterList(params));
|
---|
| 66 | // }
|
---|
| 67 |
|
---|
| 68 | public String rpcAsApplication(final String division, final String applicationProxyName, final String rpcContext, final String rpcName, final List params) throws DataAccessException {
|
---|
| 69 | return rpc(new VistaLinkAppProxyConnectionSpec(division, applicationProxyName), division, applicationProxyName, rpcContext, rpcName, params);
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | public String rpc(ConnectionSpec connectionSpec, final String division, final String user, final String rpcContext, final String rpcName, final List params) throws DataAccessException {
|
---|
| 73 | if (log.isDebugEnabled())
|
---|
| 74 | log.debug(MessageFormat.format("''{0}'' called in context ''{1}'' by ''{2}'' at facility ''{3}'' with params: {4}", new Object[]{rpcName, rpcContext, user, division, params}));
|
---|
| 75 |
|
---|
| 76 | ConnectionFactory connectionFactory = connectionFactoryLocator.getConnectionFactory(division);
|
---|
| 77 | try {
|
---|
| 78 | CciTemplate t = new CciTemplate(connectionFactory, connectionSpec);
|
---|
| 79 | RpcResponse response = (RpcResponse) t.execute(new ConnectionCallback() {
|
---|
| 80 | public Object doInConnection(Connection connection, ConnectionFactory connectionFactory) throws ResourceException, SQLException, DataAccessException {
|
---|
| 81 | try {
|
---|
| 82 | Assert.isInstanceOf(VistaLinkConnection.class, connection);
|
---|
| 83 | VistaLinkConnection conn = (VistaLinkConnection) connection;
|
---|
| 84 | conn.setTimeOut(getTimeOut());
|
---|
| 85 |
|
---|
| 86 | RpcRequest request = RpcRequestFactory.getRpcRequest(rpcContext, rpcName);
|
---|
| 87 | request.setUseProprietaryMessageFormat(true);
|
---|
| 88 | request.setXmlResponse(false);
|
---|
| 89 |
|
---|
| 90 | if (params != null) {
|
---|
| 91 | request.setParams(params);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | return conn.executeRPC(request);
|
---|
| 95 | } catch (IllegalArgumentException e) {
|
---|
| 96 | throw new InvalidDataAccessApiUsageException(UNABLE_TO_EXECUTE_RPC, e);
|
---|
| 97 | } catch (NoRpcContextFaultException e) {
|
---|
| 98 | throw new VistaLinkNoRpcContextException(e);
|
---|
| 99 | } catch (RpcNotOkForProxyUseException e) {
|
---|
| 100 | throw new VistaLinkRpcNotOkForProxyUseException(e);
|
---|
| 101 | } catch (RpcNotInContextFaultException e) {
|
---|
| 102 | throw new VistaLinkRpcNotInContextException(e);
|
---|
| 103 | } catch (LoginsDisabledFaultException e) {
|
---|
| 104 | throw new VistaLinkLoginsDisabledException(e);
|
---|
| 105 | } catch (SecurityFaultException e) {
|
---|
| 106 | throw new VistaLinkPermissionDeniedException(e);
|
---|
| 107 | } catch (VistaLinkFaultException e) {
|
---|
| 108 | throw new VistaLinkDataRetrievalFailureException(e);
|
---|
| 109 | } catch (FoundationsException e) {
|
---|
| 110 | throw new DataRetrievalFailureException(UNABLE_TO_EXECUTE_RPC, e);
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 | });
|
---|
| 114 | String result = response.getResults();
|
---|
| 115 | if (log.isDebugEnabled()) {
|
---|
| 116 | log.debug(MessageFormat.format("''{0}'' returned: {1}", new Object[]{rpcName, result}));
|
---|
| 117 | }
|
---|
| 118 | return result;
|
---|
| 119 | } catch (IllegalArgumentException e) {
|
---|
| 120 | throw new InvalidDataAccessApiUsageException(UNABLE_TO_EXECUTE_RPC, e);
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | public int getTimeOut() {
|
---|
| 125 | return timeOut;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | public void setTimeOut(int timeOut) {
|
---|
| 129 | this.timeOut = timeOut;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | public ConnectionFactoryLocator getConnectionFactoryLocator() {
|
---|
| 133 | return connectionFactoryLocator;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | // TODO: Uncomment me for java 5
|
---|
| 137 | // private List createParameterList(Object... params) {
|
---|
| 138 | // List paramList = new ArrayList();
|
---|
| 139 | // for (Object param : params) {
|
---|
| 140 | // paramList.add(param);
|
---|
| 141 | // }
|
---|
| 142 | // return paramList;
|
---|
| 143 | // }
|
---|
| 144 | }
|
---|