Show
Ignore:
Timestamp:
06/11/10 19:05:19 (2 years ago)
Author:
bturner
Message:

Convert all db calls to follow canonical pattern of resource usage.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/ds_pool/src/org/bridgedb/rdb/SimpleGdbImplCommon.java

    r318 r408  
    11package org.bridgedb.rdb; 
    22 
     3import java.sql.Connection; 
     4import java.sql.PreparedStatement; 
    35import java.sql.ResultSet; 
    46import java.sql.ResultSetMetaData; 
     
    1921 
    2022/** 
    21  * Some methods and constants that are shared between SimpleGdbImpl2 and SimpleGdbImpl3 
     23 * Some methods and constants that are shared between SimpleGdbImpl2 and 
     24 * SimpleGdbImpl3 
    2225 */ 
    2326public abstract class SimpleGdbImplCommon extends SimpleGdb 
    2427{ 
    25         SimpleGdbImplCommon(String dbName, String connectionString) throws IDMapperException 
    26         { 
    27                 super(dbName, connectionString); 
    28                 caps = new SimpleGdbCapabilities(); 
    29         } 
    30  
    31         final SimpleGdb.QueryLifeCycle qDatasources = new SimpleGdb.QueryLifeCycle( 
    32                         "SELECT codeRight FROM link GROUP BY codeRight" 
    33                 ); 
    34         final SimpleGdb.QueryLifeCycle qInfo = new SimpleGdb.QueryLifeCycle( 
    35                         "SELECT * FROM info" 
    36                 ); 
    37         final SimpleGdb.QueryLifeCycle qXrefExists = new SimpleGdb.QueryLifeCycle( 
    38                         "SELECT id FROM " + "datanode" + " WHERE " + 
    39                         "id = ? AND code = ?" 
    40                 ); 
    41         final SimpleGdb.QueryLifeCycle qAttribute = new SimpleGdb.QueryLifeCycle( 
    42                         "SELECT attrvalue FROM attribute " + 
    43                         " WHERE id = ? AND code = ? AND attrname = ?" 
    44                 ); 
    45         final SimpleGdb.QueryLifeCycle qAllAttributes = new SimpleGdb.QueryLifeCycle( 
    46                         "SELECT attrname, attrvalue FROM attribute " + 
    47                         " WHERE id = ? AND code = ?" 
    48                 ); 
    49         final SimpleGdb.QueryLifeCycle qAttributesSet = new SimpleGdb.QueryLifeCycle( 
    50                         "SELECT attrname FROM attribute GROUP BY attrname" 
    51                 ); 
    52         final SimpleGdb.QueryLifeCycle qCrossRefs = new SimpleGdb.QueryLifeCycle ( 
    53                         "SELECT dest.idRight, dest.codeRight FROM link AS src JOIN link AS dest " + 
    54                         "ON src.idLeft = dest.idLeft and src.codeLeft = dest.codeLeft " + 
    55                         "WHERE src.idRight = ? AND src.codeRight = ?" 
    56                 ); 
    57         final SimpleGdb.QueryLifeCycle qCrossRefsWithCode = new SimpleGdb.QueryLifeCycle ( 
    58                         "SELECT dest.idRight, dest.codeRight FROM link AS src JOIN link AS dest " + 
    59                         "ON src.idLeft = dest.idLeft and src.codeLeft = dest.codeLeft " + 
    60                         "WHERE src.idRight = ? AND src.codeRight = ? AND dest.codeRight = ?" 
    61                 ); 
    62         final SimpleGdb.QueryLifeCycle qRefsByAttribute = new SimpleGdb.QueryLifeCycle ( 
    63                         "SELECT datanode.id, datanode.code FROM datanode " + 
    64                         " LEFT JOIN attribute ON attribute.code = datanode.code AND attribute.id = datanode.id " + 
    65                         "WHERE attrName = ? AND attrValue = ?" 
    66                 ); 
    67         final SimpleGdb.QueryLifeCycle qFreeSearch = new SimpleGdb.QueryLifeCycle ( 
    68                         "SELECT id, code FROM datanode WHERE " + 
    69                         "LOWER(ID) LIKE ?" 
    70                 ); 
    71         final SimpleGdb.QueryLifeCycle qAttributeSearch = new SimpleGdb.QueryLifeCycle ( 
    72                         "SELECT id, code, attrvalue FROM attribute WHERE " + 
    73                         "attrname = 'Symbol' AND LOWER(attrvalue) LIKE ?" 
    74                 ); 
    75         final SimpleGdb.QueryLifeCycle qIdSearchWithAttributes = new SimpleGdb.QueryLifeCycle ( 
    76                         "SELECT id, code, attrvalue FROM attribute WHERE " + 
    77                         "attrname = 'Symbol' AND LOWER(ID) LIKE ?" 
    78                 ); 
    79  
    80         /** {@inheritDoc} */ 
    81         public boolean xrefExists(Xref xref) throws IDMapperException  
    82         { 
    83                 final QueryLifeCycle pst = qXrefExists; 
    84                 synchronized (pst) { 
    85                         try  
    86                         { 
    87                                 pst.init(); 
    88                                 pst.setString(1, xref.getId()); 
    89                                 pst.setString(2, xref.getDataSource().getSystemCode()); 
    90                                 ResultSet r = pst.executeQuery(); 
    91          
    92                                 while(r.next())  
    93                                 { 
    94                                         return true; 
    95                                 } 
    96                         }  
    97                         catch (SQLException e)  
    98                         { 
    99                                 throw new IDMapperException (e); 
    100                         } 
    101                         finally {pst.cleanup(); } 
    102                         return false; 
    103                 } 
    104         } 
    105  
    106         /** 
    107          * Read the info table and return as properties. 
    108          * @return a map where keys are column names and values are the fields in the first row. 
    109          * @throws IDMapperException when the database became unavailable 
    110          */ 
    111         Map<String, String> getInfo() throws IDMapperException 
    112         { 
    113                 Map<String, String> result = new HashMap<String, String>(); 
    114                 final QueryLifeCycle pst = qInfo; 
    115                 synchronized (pst) { 
    116                         try 
    117                         { 
    118                                 pst.init(); 
    119                                 ResultSet rs = pst.executeQuery(); 
    120                                  
    121                                 if (rs.next()) 
    122                                 { 
    123                                         ResultSetMetaData rsmd = rs.getMetaData(); 
    124                                         for (int i = 1; i <= rsmd.getColumnCount(); ++i) 
    125                                         { 
    126                                                 String key = rsmd.getColumnName(i); 
    127                                                 String val = rs.getString(i); 
    128                                                 result.put (key, val); 
    129                                         } 
    130                                 } 
    131                         } 
    132                         catch (SQLException ex) 
    133                         { 
    134                                 throw new IDMapperException (ex); 
    135                         } 
    136                          
    137                         return result; 
    138                 } 
    139         } 
    140  
    141  
    142         /** {@inheritDoc} */ 
    143         public Set<Xref> mapID (Xref idc, DataSource... resultDs) throws IDMapperException 
    144         { 
    145                 final QueryLifeCycle pst = resultDs.length != 1 ? qCrossRefs : qCrossRefsWithCode; 
    146                 Set<Xref> refs = new HashSet<Xref>(); 
    147                  
    148                 if (idc.getDataSource() == null) return refs; 
    149                 synchronized (pst) { 
    150                         try 
    151                         { 
    152                                 pst.init(); 
    153                                 pst.setString(1, idc.getId()); 
    154                                 pst.setString(2, idc.getDataSource().getSystemCode()); 
    155                                 if (resultDs.length == 1) pst.setString(3, resultDs[0].getSystemCode());                         
    156                                  
    157                                 Set<DataSource> dsFilter = new HashSet<DataSource>(Arrays.asList(resultDs)); 
    158          
    159                                 ResultSet rs = pst.executeQuery(); 
    160                                 while (rs.next()) 
    161                                 { 
    162                                         DataSource ds = DataSource.getBySystemCode(rs.getString(2)); 
    163                                         if (resultDs.length == 0 || dsFilter.contains(ds)) 
    164                                         { 
    165                                                 refs.add (new Xref (rs.getString(1), ds)); 
    166                                         } 
    167                                 } 
    168                         } 
    169                         catch (SQLException e) 
    170                         { 
    171                                 throw new IDMapperException (e); 
    172                         } 
    173                         finally {pst.cleanup(); } 
    174                  
    175                         return refs; 
    176                 } 
    177         } 
    178  
    179         /** {@inheritDoc} */ 
    180         public List<Xref> getCrossRefsByAttribute(String attrName, String attrValue) throws IDMapperException { 
    181 //              Logger.log.trace("Fetching cross references by attribute: " + attrName + " = " + attrValue); 
    182                 List<Xref> refs = new ArrayList<Xref>(); 
    183  
    184                 final QueryLifeCycle pst = qRefsByAttribute; 
    185                 synchronized (pst) {  
    186                         try { 
    187                                 pst.init(); 
    188                                 pst.setString(1, attrName); 
    189                                 pst.setString(2, attrValue); 
    190                                 ResultSet r = pst.executeQuery(); 
    191                                 while(r.next()) { 
    192                                         Xref ref = new Xref(r.getString(1), DataSource.getBySystemCode(r.getString(2))); 
    193                                         refs.add(ref); 
    194                                 } 
    195                         } catch(SQLException e) { 
    196                                 throw new IDMapperException (e); 
    197                         } 
    198                         finally {pst.cleanup(); } 
    199         //              Logger.log.trace("End fetching cross references by attribute"); 
    200                         return refs; 
    201                 } 
    202         } 
    203  
    204         /** {@inheritDoc} */ 
    205         public Set<Xref> freeSearch (String text, int limit) throws IDMapperException  
    206         {                
    207                 Set<Xref> result = new HashSet<Xref>(); 
    208                 final QueryLifeCycle pst = qFreeSearch; 
    209                 synchronized (pst) {  
    210                         try { 
    211                                 pst.init(limit); 
    212                                 pst.setString(1, "%" + text.toLowerCase() + "%"); 
    213                                 ResultSet r = pst.executeQuery(); 
    214                                 while(r.next()) { 
    215                                         String id = r.getString(1); 
    216                                         DataSource ds = DataSource.getBySystemCode(r.getString(2)); 
    217                                         Xref ref = new Xref (id, ds); 
    218                                         result.add (ref); 
    219                                 }                        
    220                         }  
    221                         catch (SQLException e)  
    222                         { 
    223                                 throw new IDMapperException(e); 
    224                         } 
    225                         finally {pst.cleanup(); } 
    226                         return result; 
    227                 } 
    228         } 
    229  
    230         /** 
    231          * @return a list of data sources present in this database.  
    232            @throws IDMapperException when the database is unavailable 
    233          */ 
    234         private Set<DataSource> getDataSources() throws IDMapperException 
    235         { 
    236                 Set<DataSource> result = new HashSet<DataSource>(); 
    237                 final QueryLifeCycle pst = qDatasources; 
    238                 synchronized (pst) {  
    239                         try 
    240                 { 
    241                         pst.init(); 
    242                         ResultSet rs = pst.executeQuery(); 
    243                         while (rs.next()) 
    244                         { 
    245                                 DataSource ds = DataSource.getBySystemCode(rs.getString(1));  
    246                                 result.add (ds); 
    247                         } 
    248                 } 
    249                 catch (SQLException ignore) 
    250                 { 
    251                         throw new IDMapperException(ignore); 
    252                 } 
    253                         finally {pst.cleanup(); } 
    254                 return result; 
    255                 } 
    256         } 
    257  
    258         private final IDMapperCapabilities caps; 
    259  
    260         class SimpleGdbCapabilities extends AbstractIDMapperCapabilities 
    261         { 
    262                 /** default constructor. 
    263                  * @throws IDMapperException when database is not available */ 
    264                 public SimpleGdbCapabilities() throws IDMapperException  
    265                 { 
    266                         super (SimpleGdbImplCommon.this.getDataSources(), true,  
    267                                         SimpleGdbImplCommon.this.getInfo()); 
    268                 } 
    269         } 
    270  
    271         /** 
    272          * @return the capabilities of this gene database 
    273          */ 
    274         public IDMapperCapabilities getCapabilities()  
    275         { 
    276                 return caps; 
    277         } 
    278  
    279         /** 
    280          * 
    281          * @return true 
    282          */ 
    283         public boolean isFreeAttributeSearchSupported() 
    284         { 
    285                 return true; 
    286         } 
    287  
    288         /** 
    289          * free text search for matching symbols. 
    290          * @return references that match the query 
    291          * @param query The text to search for 
    292          * @param attrType the attribute to look for, e.g. 'Symbol' or 'Description'. 
    293          * @param limit The number of results to limit the search to 
    294          * @throws IDMapperException if the mapping service is (temporarily) unavailable  
    295          */ 
    296         public Map<Xref, String> freeAttributeSearch (String query, String attrType, int limit) throws IDMapperException 
    297         { 
    298                 Map<Xref, String> result = new HashMap<Xref, String>(); 
    299                 final QueryLifeCycle pst = (MATCH_ID.equals (attrType)) ?  
    300                                 qIdSearchWithAttributes : qAttributeSearch; 
    301                 synchronized (pst) {  
    302                         try { 
    303                                 pst.init(limit); 
    304                                 pst.setString(1, "%" + query.toLowerCase() + "%"); 
    305                                 ResultSet r = pst.executeQuery(); 
    306          
    307                                 while(r.next())  
    308                                 { 
    309                                         String id = r.getString("id"); 
    310                                         String code = r.getString("code"); 
    311                                         String symbol = r.getString("attrValue"); 
    312                                         result.put(new Xref (id, DataSource.getBySystemCode(code)), symbol); 
    313                                 } 
    314                         } catch (SQLException e) { 
    315                                 throw new IDMapperException (e); 
    316                         } 
    317                         finally {pst.cleanup(); } 
    318                         return result; 
    319                 } 
    320         } 
    321  
    322         /** {@inheritDoc} */ 
    323         public Set<String> getAttributeSet() throws IDMapperException  
    324         { 
    325                 Set<String> result = new HashSet<String>(); 
    326                 final QueryLifeCycle pst = qAttributesSet; 
    327                 synchronized (pst) {  
    328                 try 
    329                 { 
    330                         pst.init(); 
    331                         ResultSet rs = pst.executeQuery(); 
    332                         while (rs.next()) 
    333                         { 
    334                                 result.add (rs.getString(1)); 
    335                         } 
    336                 } 
    337                 catch (SQLException ignore) 
    338                 { 
    339                         throw new IDMapperException(ignore); 
    340                 } 
    341                         finally {pst.cleanup(); } 
    342                 return result; 
    343                 } 
    344         } 
    345  
     28    SimpleGdbImplCommon(String dbName, 
     29                        String connectionString) throws IDMapperException 
     30    { 
     31        super(dbName, connectionString); 
     32        caps = new SimpleGdbCapabilities(); 
     33    } 
     34     
     35    private final static String qDatasources = "SELECT codeRight FROM link GROUP BY codeRight"; 
     36     
     37    private final static String qInfo = "SELECT * FROM info"; 
     38     
     39    private final static String qXrefExists = "SELECT id FROM " 
     40                                                       + "datanode" + " WHERE " 
     41                                                       + "id = ? AND code = ?"; 
     42     
     43    protected final static String qAttribute = "SELECT attrvalue FROM attribute " 
     44                                                      + " WHERE id = ? AND code = ? AND attrname = ?"; 
     45     
     46    protected final static String qAllAttributes = "SELECT attrname, attrvalue FROM attribute " 
     47                                                          + " WHERE id = ? AND code = ?"; 
     48     
     49    private final static String qAttributesSet = "SELECT attrname FROM attribute GROUP BY attrname"; 
     50     
     51    private final static String qCrossRefs = "SELECT dest.idRight, dest.codeRight FROM link AS src JOIN link AS dest " 
     52                                                      + "ON src.idLeft = dest.idLeft and src.codeLeft = dest.codeLeft " 
     53                                                      + "WHERE src.idRight = ? AND src.codeRight = ?"; 
     54     
     55    private final static String qCrossRefsWithCode = "SELECT dest.idRight, dest.codeRight FROM link AS src JOIN link AS dest " 
     56                                                              + "ON src.idLeft = dest.idLeft and src.codeLeft = dest.codeLeft " 
     57                                                              + "WHERE src.idRight = ? AND src.codeRight = ? AND dest.codeRight = ?"; 
     58     
     59    private final static String qRefsByAttribute = "SELECT datanode.id, datanode.code FROM datanode " 
     60                                                            + " LEFT JOIN attribute ON attribute.code = datanode.code AND attribute.id = datanode.id " 
     61                                                            + "WHERE attrName = ? AND attrValue = ?"; 
     62     
     63    private final static String qFreeSearch = "SELECT id, code FROM datanode WHERE " 
     64                                                       + "LOWER(ID) LIKE ?"; 
     65     
     66    private final static String qAttributeSearch = "SELECT id, code, attrvalue FROM attribute WHERE " 
     67                                                            + "attrname = 'Symbol' AND LOWER(attrvalue) LIKE ?"; 
     68     
     69    private final static String qIdSearchWithAttributes = "SELECT id, code, attrvalue FROM attribute WHERE " 
     70                                                                   + "attrname = 'Symbol' AND LOWER(ID) LIKE ?"; 
     71     
     72    /** {@inheritDoc} */ 
     73    public boolean xrefExists(Xref xref) throws IDMapperException 
     74    { 
     75        Connection c = null; 
     76        PreparedStatement pst = null; 
     77        ResultSet r = null; 
     78        boolean exists = false; 
     79        try 
     80        { 
     81            c = getConnection(); 
     82            pst = c.prepareStatement(qXrefExists); 
     83             
     84            pst.setString(1, xref.getId()); 
     85            pst.setString(2, xref.getDataSource().getSystemCode()); 
     86            r = pst.executeQuery(); 
     87             
     88            exists = r.next(); 
     89             
     90        } 
     91        catch (SQLException e) 
     92        { 
     93            throw new IDMapperException(e); 
     94        } 
     95        finally 
     96        { 
     97            closeSilently(r, pst, c); 
     98        } 
     99        return exists; 
     100    } 
     101     
     102    /** 
     103     * Read the info table and return as properties. 
     104     *  
     105     * @return a map where keys are column names and values are the fields in 
     106     *         the first row. 
     107     * @throws IDMapperException 
     108     *             when the database became unavailable 
     109     */ 
     110    Map<String, String> getInfo() throws IDMapperException 
     111    { 
     112        Map<String, String> result = new HashMap<String, String>(); 
     113         
     114        Connection c = null; 
     115        PreparedStatement pst = null; 
     116        ResultSet r = null; 
     117        try 
     118        { 
     119            c = getConnection(); 
     120            pst = c.prepareStatement(qInfo); 
     121             
     122            r = pst.executeQuery(); 
     123             
     124            if (r.next()) 
     125            { 
     126                ResultSetMetaData rsmd = r.getMetaData(); 
     127                for (int i = 1; i <= rsmd.getColumnCount(); ++i) 
     128                { 
     129                    String key = rsmd.getColumnName(i); 
     130                    String val = r.getString(i); 
     131                    result.put(key, val); 
     132                } 
     133            } 
     134        } 
     135        catch (SQLException ex) 
     136        { 
     137            throw new IDMapperException(ex); 
     138        } 
     139        finally 
     140        { 
     141            closeSilently(r, pst, c); 
     142        } 
     143         
     144        return result; 
     145         
     146    } 
     147     
     148    /** {@inheritDoc} */ 
     149    public Set<Xref> mapID(Xref idc, 
     150                           DataSource... resultDs) throws IDMapperException 
     151    { 
     152        String query = resultDs.length != 1 ? qCrossRefs : qCrossRefsWithCode; 
     153        Set<Xref> refs = new HashSet<Xref>(); 
     154         
     155        if (idc.getDataSource() != null) 
     156        { 
     157             
     158            Connection c = null; 
     159            PreparedStatement pst = null; 
     160            ResultSet rs = null; 
     161            try 
     162            { 
     163                c = getConnection(); 
     164                pst =c.prepareStatement(query); 
     165                 
     166                pst.setString(1, idc.getId()); 
     167                pst.setString(2, idc.getDataSource().getSystemCode()); 
     168                if (resultDs.length == 1) 
     169                    pst.setString(3, resultDs[0].getSystemCode()); 
     170                 
     171                Set<DataSource> dsFilter = new HashSet<DataSource>(Arrays.asList(resultDs)); 
     172                 
     173                rs = pst.executeQuery(); 
     174                while (rs.next()) 
     175                { 
     176                    DataSource ds = DataSource.getBySystemCode(rs.getString(2)); 
     177                    if (resultDs.length == 0 || dsFilter.contains(ds)) 
     178                    { 
     179                        refs.add(new Xref(rs.getString(1), ds)); 
     180                    } 
     181                } 
     182            } 
     183            catch (SQLException e) 
     184            { 
     185                throw new IDMapperException(e); 
     186            } 
     187            finally 
     188            { 
     189                closeSilently(rs, pst, c); 
     190            } 
     191        } 
     192        return refs; 
     193         
     194    } 
     195     
     196    /** {@inheritDoc} */ 
     197    public List<Xref> getCrossRefsByAttribute(String attrName, 
     198                                              String attrValue) throws IDMapperException 
     199    { 
     200        // Logger.log.trace("Fetching cross references by attribute: " + 
     201        // attrName + " = " + attrValue); 
     202        List<Xref> refs = new ArrayList<Xref>(); 
     203         
     204        Connection c = null; 
     205        PreparedStatement pst = null; 
     206        ResultSet r = null; 
     207         
     208        try 
     209        { 
     210            c = getConnection(); 
     211            pst = c.prepareStatement(qRefsByAttribute); 
     212             
     213            pst.setString(1, attrName); 
     214            pst.setString(2, attrValue); 
     215            r = pst.executeQuery(); 
     216            while (r.next()) 
     217            { 
     218                Xref ref = new Xref(r.getString(1), 
     219                                    DataSource.getBySystemCode(r.getString(2))); 
     220                refs.add(ref); 
     221            } 
     222        } 
     223        catch (SQLException e) 
     224        { 
     225            throw new IDMapperException(e); 
     226        } 
     227        finally 
     228        { 
     229            closeSilently(r, pst, c); 
     230        } 
     231        // Logger.log.trace("End fetching cross references by attribute"); 
     232        return refs; 
     233         
     234    } 
     235     
     236    /** {@inheritDoc} */ 
     237    public Set<Xref> freeSearch(String text, 
     238                                int limit) throws IDMapperException 
     239    { 
     240        Set<Xref> result = new HashSet<Xref>(); 
     241         
     242        Connection c = null; 
     243        PreparedStatement pst = null; 
     244        ResultSet r = null; 
     245         
     246        try 
     247        { 
     248            c = getConnection(); 
     249            pst = c.prepareStatement(qFreeSearch); 
     250            pst.setString(1, "%" + text.toLowerCase() + "%"); 
     251            r = pst.executeQuery(); 
     252            while (r.next()) 
     253            { 
     254                String id = r.getString(1); 
     255                DataSource ds = DataSource.getBySystemCode(r.getString(2)); 
     256                Xref ref = new Xref(id, ds); 
     257                result.add(ref); 
     258            } 
     259        } 
     260        catch (SQLException e) 
     261        { 
     262            throw new IDMapperException(e); 
     263        } 
     264        finally 
     265        { 
     266            closeSilently(r, pst, c); 
     267        } 
     268        return result; 
     269         
     270    } 
     271     
     272    /** 
     273     * @return a list of data sources present in this database. 
     274     * @throws IDMapperException 
     275     *             when the database is unavailable 
     276     */ 
     277    private Set<DataSource> getDataSources() throws IDMapperException 
     278    { 
     279        Set<DataSource> result = new HashSet<DataSource>(); 
     280         
     281        Connection c = null; 
     282        PreparedStatement pst = null; 
     283        ResultSet rs = null; 
     284         
     285        try 
     286        { 
     287            c = getConnection(); 
     288            pst = c.prepareStatement(qDatasources); 
     289            rs = pst.executeQuery(); 
     290            while (rs.next()) 
     291            { 
     292                DataSource ds = DataSource.getBySystemCode(rs.getString(1)); 
     293                result.add(ds); 
     294            } 
     295        } 
     296        catch (SQLException e) 
     297        { 
     298            throw new IDMapperException(e); 
     299        } 
     300        finally 
     301        { 
     302            closeSilently(rs, pst, c); 
     303        } 
     304        return result; 
     305         
     306    } 
     307     
     308    private final IDMapperCapabilities caps; 
     309     
     310    class SimpleGdbCapabilities extends AbstractIDMapperCapabilities 
     311    { 
     312        /** 
     313         * default constructor. 
     314         *  
     315         * @throws IDMapperException 
     316         *             when database is not available 
     317         */ 
     318        public SimpleGdbCapabilities() throws IDMapperException 
     319        { 
     320            super(SimpleGdbImplCommon.this.getDataSources(), 
     321                  true, 
     322                  SimpleGdbImplCommon.this.getInfo()); 
     323        } 
     324    } 
     325     
     326    /** 
     327     * @return the capabilities of this gene database 
     328     */ 
     329    public IDMapperCapabilities getCapabilities() 
     330    { 
     331        return caps; 
     332    } 
     333     
     334    /** 
     335     *  
     336     * @return true 
     337     */ 
     338    public boolean isFreeAttributeSearchSupported() 
     339    { 
     340        return true; 
     341    } 
     342     
     343    /** 
     344     * free text search for matching symbols. 
     345     *  
     346     * @return references that match the query 
     347     * @param query 
     348     *            The text to search for 
     349     * @param attrType 
     350     *            the attribute to look for, e.g. 'Symbol' or 'Description'. 
     351     * @param limit 
     352     *            The number of results to limit the search to 
     353     * @throws IDMapperException 
     354     *             if the mapping service is (temporarily) unavailable 
     355     */ 
     356    public Map<Xref, String> freeAttributeSearch(String query, 
     357                                                 String attrType, 
     358                                                 int limit) throws IDMapperException 
     359    { 
     360        Map<Xref, String> result = new HashMap<Xref, String>(); 
     361        String querySql = (MATCH_ID.equals(attrType)) ? qIdSearchWithAttributes 
     362                : qAttributeSearch; 
     363         
     364        Connection c = null; 
     365        PreparedStatement pst = null; 
     366        ResultSet r = null; 
     367         
     368        try 
     369        { 
     370            c = getConnection(); 
     371            pst = c.prepareStatement(querySql); 
     372            pst.setString(1, "%" + query.toLowerCase() + "%"); 
     373            r = pst.executeQuery(); 
     374             
     375            while (r.next()) 
     376            { 
     377                String id = r.getString("id"); 
     378                String code = r.getString("code"); 
     379                String symbol = r.getString("attrValue"); 
     380                result.put(new Xref(id, DataSource.getBySystemCode(code)), 
     381                           symbol); 
     382            } 
     383        } 
     384        catch (SQLException e) 
     385        { 
     386            throw new IDMapperException(e); 
     387        } 
     388        finally 
     389        { 
     390            closeSilently(r, pst, c); 
     391        } 
     392        return result; 
     393         
     394    } 
     395     
     396    /** {@inheritDoc} */ 
     397    public Set<String> getAttributeSet() throws IDMapperException 
     398    { 
     399        Set<String> result = new HashSet<String>(); 
     400         
     401        Connection c = null; 
     402        PreparedStatement pst = null; 
     403        ResultSet rs = null; 
     404         
     405        try 
     406        { 
     407            c = getConnection(); 
     408            pst = c.prepareStatement(qAttributesSet); 
     409            rs = pst.executeQuery(); 
     410            while (rs.next()) 
     411            { 
     412                result.add(rs.getString(1)); 
     413            } 
     414        } 
     415        catch (SQLException ignore) 
     416        { 
     417            throw new IDMapperException(ignore); 
     418        } 
     419        finally 
     420        { 
     421            closeSilently(rs, pst, c); 
     422        } 
     423        return result; 
     424    } 
     425     
    346426}