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