[1240] | 1 | package gov.va.med.edp.rpc;
|
---|
| 2 |
|
---|
| 3 | import gov.va.med.edp.vo.BigBoardDebugInfoVO;
|
---|
| 4 | import gov.va.med.exception.FoundationsException;
|
---|
| 5 | import gov.va.med.vistalink.adapter.cci.VistaLinkAppProxyConnectionSpec;
|
---|
| 6 | import gov.va.med.vistalink.adapter.cci.VistaLinkConnection;
|
---|
| 7 | import gov.va.med.vistalink.adapter.cci.VistaLinkConnectionSpec;
|
---|
| 8 | import gov.va.med.vistalink.adapter.cci.VistaLinkDuzConnectionSpec;
|
---|
| 9 | import gov.va.med.vistalink.adapter.record.LoginsDisabledFaultException;
|
---|
| 10 | import gov.va.med.vistalink.adapter.record.VistaLinkFaultException;
|
---|
| 11 | import gov.va.med.vistalink.adapter.spi.VistaLinkServerInfo;
|
---|
| 12 | import gov.va.med.vistalink.rpc.*;
|
---|
| 13 | import gov.va.med.vistalink.security.m.SecurityFaultException;
|
---|
| 14 | import gov.va.med.vistalink.security.m.SecurityDivisionDeterminationFaultException;
|
---|
| 15 | import org.apache.log4j.Logger;
|
---|
| 16 | import org.springframework.dao.*;
|
---|
| 17 | import org.springframework.jca.cci.core.CciTemplate;
|
---|
| 18 | import org.springframework.jca.cci.core.ConnectionCallback;
|
---|
| 19 | import org.springframework.util.Assert;
|
---|
| 20 |
|
---|
| 21 | import javax.resource.ResourceException;
|
---|
| 22 | import javax.resource.cci.Connection;
|
---|
| 23 | import javax.resource.cci.ConnectionFactory;
|
---|
| 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 VistaLinkRpcTemplate {
|
---|
| 35 |
|
---|
| 36 | private static final int USER_TYPE_DUZ = 1;
|
---|
| 37 | private static final int USER_TYPE_APPLICATION_PROXY = 2;
|
---|
| 38 |
|
---|
| 39 | static final int DEFAULT_TIMEOUT = 10000;
|
---|
| 40 |
|
---|
| 41 | private static Logger log = Logger.getLogger(VistaLinkRpcTemplate.class);
|
---|
| 42 |
|
---|
| 43 | private int timeOut = DEFAULT_TIMEOUT;
|
---|
| 44 | private String rpcContext;
|
---|
| 45 | private ConnectionFactoryLocator connectionFactoryLocator;
|
---|
| 46 |
|
---|
| 47 | private static final String UNABLE_TO_EXECUTE_RPC = "Unable to execute RPC";
|
---|
| 48 |
|
---|
| 49 | public VistaLinkRpcTemplate(ConnectionFactoryLocator connectionFactoryLocator) {
|
---|
| 50 | this.connectionFactoryLocator = connectionFactoryLocator;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | public String rpcAsUser(final String division, final String duz, final String rpcContext, final String rpcName) throws DataAccessException {
|
---|
| 54 | return rpcAsUser(division, duz, rpcContext, rpcName, null);
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | public String rpcAsUser(final String division, final String duz, final String rpcContext, final String rpcName, final List params) throws DataAccessException {
|
---|
| 58 | return rpc(division, duz, rpcContext, rpcName, params, USER_TYPE_DUZ);
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | public String rpcAsApplication(final String division, final String applicationProxyName, final String rpcContext, final String rpcName) throws DataAccessException {
|
---|
| 62 | return rpcAsApplication(division, applicationProxyName, rpcContext, rpcName, null);
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | public String rpcAsApplication(final String division, final String applicationProxyName, final String rpcContext, final String rpcName, final List params) throws DataAccessException {
|
---|
| 66 | return rpc(division, applicationProxyName, rpcContext, rpcName, params, USER_TYPE_APPLICATION_PROXY);
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | public BigBoardDebugInfoVO getVistaLinkConnectionInfo(final String division, final String user) throws DataAccessException {
|
---|
| 70 | return getConnectionInfo(division, user, USER_TYPE_APPLICATION_PROXY);
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | public String rpc(final String division, final String user, final String rpcContext, final String rpcName, final List params, final int userType) throws DataAccessException {
|
---|
| 74 | if (log.isDebugEnabled())
|
---|
| 75 | log.debug(MessageFormat.format("''{0}'' called in context ''{1}'' by ''{2}'' at facility ''{3}'' with params: {4}", new Object[]{rpcName, rpcContext, user, division, params}));
|
---|
| 76 |
|
---|
| 77 | ConnectionFactory connectionFactory = connectionFactoryLocator.getConnectionFactory(division);
|
---|
| 78 | try {
|
---|
| 79 | CciTemplate t = new CciTemplate(connectionFactory, createConnectionSpec(division, user, userType));
|
---|
| 80 | RpcResponse response = (RpcResponse) t.execute(new ConnectionCallback() {
|
---|
| 81 | public Object doInConnection(Connection connection, ConnectionFactory connectionFactory) throws ResourceException, SQLException, DataAccessException {
|
---|
| 82 | try {
|
---|
| 83 | Assert.isInstanceOf(VistaLinkConnection.class, connection);
|
---|
| 84 | VistaLinkConnection conn = (VistaLinkConnection) connection;
|
---|
| 85 | conn.setTimeOut(getTimeOut());
|
---|
| 86 |
|
---|
| 87 | RpcRequest request = RpcRequestFactory.getRpcRequest(rpcContext, rpcName);
|
---|
| 88 | request.setUseProprietaryMessageFormat(true);
|
---|
| 89 | request.setXmlResponse(false);
|
---|
| 90 |
|
---|
| 91 | if (params != null) {
|
---|
| 92 | request.setParams(params);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | return conn.executeRPC(request);
|
---|
| 96 | } catch (IllegalArgumentException e) {
|
---|
| 97 | throw new InvalidDataAccessApiUsageException(UNABLE_TO_EXECUTE_RPC, e);
|
---|
| 98 | } catch (NoRpcContextFaultException e) {
|
---|
| 99 | throw new VistaLinkRpcInvalidApiUsageException(e);
|
---|
| 100 | } catch (RpcNotOkForProxyUseException e) {
|
---|
| 101 | throw new VistaLinkRpcInvalidApiUsageException(e);
|
---|
| 102 | } catch (RpcNotInContextFaultException e) {
|
---|
| 103 | throw new VistaLinkRpcPermissionDeniedException(e);
|
---|
| 104 | } catch (LoginsDisabledFaultException e) {
|
---|
| 105 | throw new DataAccessResourceFailureException(UNABLE_TO_EXECUTE_RPC, e);
|
---|
| 106 | } catch (SecurityDivisionDeterminationFaultException e) {
|
---|
| 107 | throw new VistaLinkRpcInvalidApiUsageException(e);
|
---|
| 108 | } catch (SecurityFaultException e) {
|
---|
| 109 | throw new VistaLinkRpcPermissionDeniedException(e);
|
---|
| 110 | } catch (VistaLinkFaultException e) {
|
---|
| 111 | throw new DataRetrievalFailureException(UNABLE_TO_EXECUTE_RPC, e);
|
---|
| 112 | } catch (FoundationsException e) {
|
---|
| 113 | throw new DataRetrievalFailureException(UNABLE_TO_EXECUTE_RPC, e);
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 | });
|
---|
| 117 | String result = response.getResults();
|
---|
| 118 | if (log.isDebugEnabled()) {
|
---|
| 119 | log.debug(MessageFormat.format("''{0}'' returned: {1}", new Object[]{rpcName, result}));
|
---|
| 120 | }
|
---|
| 121 | return result;
|
---|
| 122 | } catch (IllegalArgumentException e) {
|
---|
| 123 | throw new InvalidDataAccessApiUsageException(UNABLE_TO_EXECUTE_RPC, e);
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | private BigBoardDebugInfoVO getConnectionInfo(final String division, final String user, final int userType) throws DataAccessException {
|
---|
| 128 | VistaLinkConnection conn = null;
|
---|
| 129 | ConnectionFactory connectionFactory = connectionFactoryLocator.getConnectionFactory(division);
|
---|
| 130 | try {
|
---|
| 131 | conn = (VistaLinkConnection)connectionFactory.getConnection(createConnectionSpec(division, user, userType));
|
---|
| 132 | } catch (ResourceException e) {
|
---|
| 133 | throw new DataAccessResourceFailureException("error getting connection info", e);
|
---|
| 134 | }
|
---|
| 135 | VistaLinkServerInfo vistaLinkServerInfo = conn.getConnectionInfo();
|
---|
| 136 | BigBoardDebugInfoVO bbd = new BigBoardDebugInfoVO(vistaLinkServerInfo.getAddress().toString(), new Integer(vistaLinkServerInfo.getPort()).toString());
|
---|
| 137 | return bbd;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | private VistaLinkConnectionSpec createConnectionSpec(String division, String user, int userType) {
|
---|
| 141 | switch (userType) {
|
---|
| 142 | case USER_TYPE_APPLICATION_PROXY:
|
---|
| 143 | return new VistaLinkAppProxyConnectionSpec(division, user);
|
---|
| 144 | case USER_TYPE_DUZ:
|
---|
| 145 | default:
|
---|
| 146 | return new VistaLinkDuzConnectionSpec(division, user);
|
---|
| 147 | }
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | public int getTimeOut() {
|
---|
| 151 | return timeOut;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | public void setTimeOut(int timeOut) {
|
---|
| 155 | this.timeOut = timeOut;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | public String getRpcContext() {
|
---|
| 159 | return rpcContext;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | public void setRpcContext(String rpcContext) {
|
---|
| 163 | this.rpcContext = rpcContext;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | public ConnectionFactoryLocator getConnectionFactoryLocator() {
|
---|
| 167 | return connectionFactoryLocator;
|
---|
| 168 | }
|
---|
| 169 | }
|
---|