ctakes-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From seanfi...@apache.org
Subject svn commit: r1628535 - in /ctakes/trunk/ctakes-dictionary-lookup-fast/src/main/java/org/apache/ctakes/dictionary/lookup2/concept: JdbcConceptFactory.java UmlsJdbcConceptFactory.java
Date Tue, 30 Sep 2014 20:02:55 GMT
Author: seanfinan
Date: Tue Sep 30 20:02:54 2014
New Revision: 1628535

URL: http://svn.apache.org/r1628535
Log:
New UmlsJdbcConceptFactory checks Umls license
JdbcConceptFactory now uses JdbcConnectionFactory

Added:
    ctakes/trunk/ctakes-dictionary-lookup-fast/src/main/java/org/apache/ctakes/dictionary/lookup2/concept/UmlsJdbcConceptFactory.java
Modified:
    ctakes/trunk/ctakes-dictionary-lookup-fast/src/main/java/org/apache/ctakes/dictionary/lookup2/concept/JdbcConceptFactory.java

Modified: ctakes/trunk/ctakes-dictionary-lookup-fast/src/main/java/org/apache/ctakes/dictionary/lookup2/concept/JdbcConceptFactory.java
URL: http://svn.apache.org/viewvc/ctakes/trunk/ctakes-dictionary-lookup-fast/src/main/java/org/apache/ctakes/dictionary/lookup2/concept/JdbcConceptFactory.java?rev=1628535&r1=1628534&r2=1628535&view=diff
==============================================================================
--- ctakes/trunk/ctakes-dictionary-lookup-fast/src/main/java/org/apache/ctakes/dictionary/lookup2/concept/JdbcConceptFactory.java
(original)
+++ ctakes/trunk/ctakes-dictionary-lookup-fast/src/main/java/org/apache/ctakes/dictionary/lookup2/concept/JdbcConceptFactory.java
Tue Sep 30 20:02:54 2014
@@ -1,13 +1,17 @@
 package org.apache.ctakes.dictionary.lookup2.concept;
 
 import org.apache.ctakes.dictionary.lookup2.util.CuiCodeUtil;
+import org.apache.ctakes.dictionary.lookup2.util.JdbcConnectionFactory;
 import org.apache.ctakes.dictionary.lookup2.util.TuiCodeUtil;
 import org.apache.ctakes.dictionary.lookup2.util.collection.CollectionMap;
 import org.apache.ctakes.dictionary.lookup2.util.collection.EnumSetMap;
 import org.apache.log4j.Logger;
 import org.apache.uima.UimaContext;
 
-import java.sql.*;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
 import java.util.Collection;
 import java.util.HashSet;
 import java.util.Properties;
@@ -23,7 +27,7 @@ public class JdbcConceptFactory extends 
 
 
    // LOG4J logger based on class name
-   final private Logger _logger = Logger.getLogger( getClass().getName() );
+   static final private Logger LOGGER = Logger.getLogger( "JdbcConceptFactory" );
 
 
    // TODO move to Constants class
@@ -39,7 +43,7 @@ public class JdbcConceptFactory extends 
    static private final String ICD10_TABLE = "icd10Table";
 
 
-   final private Connection _connection;
+   //   final private Connection _connection;
    private PreparedStatement _selectTuiCall;
    private PreparedStatement _selectPrefTermCall;
    private PreparedStatement _selectSnomedCall;
@@ -49,7 +53,7 @@ public class JdbcConceptFactory extends 
 
 
    public JdbcConceptFactory( final String name, final UimaContext uimaContext, final Properties
properties )
-         throws ClassNotFoundException, InstantiationException, IllegalAccessException {
+         throws SQLException {
       this( name,
             properties.getProperty( JDBC_DRIVER ), properties.getProperty( JDBC_URL ),
             properties.getProperty( JDBC_USER ), properties.getProperty( JDBC_PASS ),
@@ -64,35 +68,28 @@ public class JdbcConceptFactory extends 
                               final String tuiName, final String prefTermName,
                               final String snomedName, final String rxnormName,
                               final String icd9Name, final String icd10Name )
-         throws ClassNotFoundException, InstantiationException, IllegalAccessException {
+         throws SQLException {
       super( name );
+      boolean connected = false;
       try {
-         final Driver driver = (Driver)Class.forName( jdbcDriver ).newInstance();
-         DriverManager.registerDriver( driver );
+         // DO NOT use try with resources here.
+         // Try with resources uses a closable and closes it when exiting the try block
+         final Connection connection = JdbcConnectionFactory.getInstance()
+               .getConnection( jdbcDriver, jdbcUrl, jdbcUser, jdbcPass );
+         connected = connection != null;
+         _selectTuiCall = createSelectCall( connection, tuiName );
+         _selectPrefTermCall = createSelectCall( connection, prefTermName );
+         _selectSnomedCall = createSelectCall( connection, snomedName );
+         _selectRxNormCall = createSelectCall( connection, rxnormName );
+         _selectIcd9Call = createSelectCall( connection, icd9Name );
+         _selectIcd10Call = createSelectCall( connection, icd10Name );
       } catch ( SQLException sqlE ) {
-         _logger.error( "Could not register Driver " + jdbcDriver, sqlE );
-         throw new InstantiationException( "Could not register Driver " + jdbcDriver );
-      } catch ( ClassNotFoundException | InstantiationException | IllegalAccessException
multE ) {
-         _logger.error( "Could not create Driver " + jdbcDriver, multE );
-         throw multE;
-      }
-      Connection connection = null;
-      try {
-         connection = DriverManager.getConnection( jdbcUrl, jdbcUser, jdbcPass );
-      } catch ( SQLException sqlE ) {
-         _logger.error( "Could not create Connection with " + jdbcUrl + " as " + jdbcUser,
sqlE );
-         throw new InstantiationException( "Could not create Connection with " + jdbcUrl
+ " as " + jdbcUser );
-      }
-      _connection = connection;
-      try {
-         _selectTuiCall = createSelectCall( tuiName );
-         _selectPrefTermCall = createSelectCall( prefTermName );
-         _selectSnomedCall = createSelectCall( snomedName );
-         _selectRxNormCall = createSelectCall( rxnormName );
-         _selectIcd9Call = createSelectCall( icd9Name );
-         _selectIcd10Call = createSelectCall( icd10Name );
-      } catch ( SQLException sqlE ) {
-         _logger.error( "Could not create Concept Data Selection Call", sqlE );
+         if ( !connected ) {
+            LOGGER.error( "Could not Connect to Concept Factory " + name );
+         } else {
+            LOGGER.error( "Could not create Concept Data Selection Call", sqlE );
+         }
+         throw sqlE;
       }
    }
 
@@ -136,7 +133,7 @@ public class JdbcConceptFactory extends 
          // it is up to the driver to implement this behavior ...  historically some drivers
have not done so
          resultSet.close();
       } catch ( SQLException e ) {
-         _logger.error( e.getMessage() );
+         LOGGER.error( e.getMessage() );
       }
       return tuis;
    }
@@ -153,7 +150,7 @@ public class JdbcConceptFactory extends 
          // it is up to the driver to implement this behavior ...  historically some drivers
have not done so
          resultSet.close();
       } catch ( SQLException e ) {
-         _logger.error( e.getMessage() );
+         LOGGER.error( e.getMessage() );
       }
       return preferredName;
    }
@@ -171,7 +168,7 @@ public class JdbcConceptFactory extends 
          // it is up to the driver to implement this behavior ...  historically some drivers
have not done so
          resultSet.close();
       } catch ( SQLException e ) {
-         _logger.error( e.getMessage() );
+         LOGGER.error( e.getMessage() );
       }
       return codes;
    }
@@ -189,7 +186,7 @@ public class JdbcConceptFactory extends 
          // it is up to the driver to implement this behavior ...  historically some drivers
have not done so
          resultSet.close();
       } catch ( SQLException e ) {
-         _logger.error( e.getMessage() );
+         LOGGER.error( e.getMessage() );
       }
       return codes;
    }
@@ -198,12 +195,13 @@ public class JdbcConceptFactory extends 
     * @param tableName -
     * @throws SQLException if the {@code PreparedStatement} could not be created or changed
     */
-   private PreparedStatement createSelectCall( final String tableName ) throws SQLException
{
+   private PreparedStatement createSelectCall( final Connection connection, final String
tableName )
+         throws SQLException {
       if ( tableName == null || tableName.isEmpty() || tableName.equalsIgnoreCase( "null"
) ) {
          return null;
       }
       final String lookupSql = "SELECT * FROM " + tableName + " WHERE CUI = ?";
-      return _connection.prepareStatement( lookupSql );
+      return connection.prepareStatement( lookupSql );
    }
 
 

Added: ctakes/trunk/ctakes-dictionary-lookup-fast/src/main/java/org/apache/ctakes/dictionary/lookup2/concept/UmlsJdbcConceptFactory.java
URL: http://svn.apache.org/viewvc/ctakes/trunk/ctakes-dictionary-lookup-fast/src/main/java/org/apache/ctakes/dictionary/lookup2/concept/UmlsJdbcConceptFactory.java?rev=1628535&view=auto
==============================================================================
--- ctakes/trunk/ctakes-dictionary-lookup-fast/src/main/java/org/apache/ctakes/dictionary/lookup2/concept/UmlsJdbcConceptFactory.java
(added)
+++ ctakes/trunk/ctakes-dictionary-lookup-fast/src/main/java/org/apache/ctakes/dictionary/lookup2/concept/UmlsJdbcConceptFactory.java
Tue Sep 30 20:02:54 2014
@@ -0,0 +1,70 @@
+package org.apache.ctakes.dictionary.lookup2.concept;
+
+import org.apache.ctakes.dictionary.lookup2.util.UmlsUserApprover;
+import org.apache.log4j.Logger;
+import org.apache.uima.UimaContext;
+
+import java.sql.SQLException;
+import java.util.Collection;
+import java.util.Map;
+import java.util.Properties;
+
+/**
+ * @author SPF , chip-nlp
+ * @version %I%
+ * @since 9/23/2014
+ */
+final public class UmlsJdbcConceptFactory implements ConceptFactory {
+
+   static private final Logger LOGGER = Logger.getLogger( "UmlsJdbcConceptFactory" );
+
+   private final static String URL_PARAM = "umlsUrl";
+   private final static String VENDOR_PARAM = "umlsVendor";
+   private final static String USER_PARAM = "umlsUser";
+   private final static String PASS_PARAM = "umlsPass";
+
+
+   final private ConceptFactory _delegateConceptFactory;
+
+
+   public UmlsJdbcConceptFactory( final String name, final UimaContext uimaContext, final
Properties properties )
+         throws SQLException {
+      final String umlsUrl = properties.getProperty( URL_PARAM );
+      final String vendor = properties.getProperty( VENDOR_PARAM );
+      final String user = properties.getProperty( USER_PARAM );
+      final String pass = properties.getProperty( PASS_PARAM );
+      final boolean isValidUser = UmlsUserApprover.isValidUMLSUser( umlsUrl, vendor, user,
pass );
+      if ( !isValidUser ) {
+         LOGGER.error( "UMLS Account at " + umlsUrl + " is not valid for user " + user +
" with " + pass );
+         throw new SQLException( "Invalid User for UMLS Concept Factory " + name );
+      }
+      LOGGER.info( "UMLS Account at " + umlsUrl + " for user " + user + " has been validated"
);
+      _delegateConceptFactory = new JdbcConceptFactory( name, uimaContext, properties );
+   }
+
+
+   /**
+    * {@inheritDoc}
+    */
+   @Override
+   public String getName() {
+      return _delegateConceptFactory.getName();
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   @Override
+   public Concept createConcept( final Long cuiCode ) {
+      return _delegateConceptFactory.createConcept( cuiCode );
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   @Override
+   public Map<Long, Concept> createConcepts( final Collection<Long> cuiCodes
) {
+      return _delegateConceptFactory.createConcepts( cuiCodes );
+   }
+
+}



Mime
View raw message