root/trunk/org.bridgedb.rdb/src/org/bridgedb/rdb/SimpleGdbImpl2.java @ 308

Revision 308, 20.2 KB (checked in by martijn, 7 months ago)

Big project Reogranization:
- all modules are named with their main package name,

e.g. "picr" becomes org.bridgedb.webservice.picr,
and the jar will be org.bridgedb.webservice.picr.jar

- webservice renamed to org.bridgedb.server
- corelib is split in org.bridgedb, org.bridgedb.rdb,

org.bridgedb.webservice.biomart and org.bridgedb.webservice.bridgerest

  • Property svn:eol-style set to native
Line 
1// BridgeDb,
2// An abstraction layer for identifer mapping services, both local and online.
3// Copyright 2006-2009 BridgeDb developers
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16//
17package org.bridgedb.rdb;
18
19import java.sql.Connection;
20import java.sql.PreparedStatement;
21import java.sql.ResultSet;
22import java.sql.ResultSetMetaData;
23import java.sql.SQLException;
24import java.sql.Statement;
25import java.util.ArrayList;
26import java.util.Arrays;
27import java.util.HashMap;
28import java.util.HashSet;
29import java.util.List;
30import java.util.Map;
31import java.util.Set;
32import java.util.regex.Matcher;
33import java.util.regex.Pattern;
34
35import org.bridgedb.AbstractIDMapperCapabilities;
36import org.bridgedb.DataSource;
37import org.bridgedb.IDMapperCapabilities;
38import org.bridgedb.IDMapperException;
39import org.bridgedb.Xref;
40
41/** {@inheritDoc} */
42class SimpleGdbImpl2 extends SimpleGdb
43{               
44        private static final int GDB_COMPAT_VERSION = 2; //Preferred schema version
45       
46        private final SimpleGdb.LazyPst pstDatasources = new SimpleGdb.LazyPst(
47                        "SELECT codeRight FROM link GROUP BY codeRight"
48                );
49        private final SimpleGdb.LazyPst pstInfo = new SimpleGdb.LazyPst(
50                        "SELECT * FROM info"
51                );
52        private final SimpleGdb.LazyPst pstXrefExists = new SimpleGdb.LazyPst(
53                        "SELECT id FROM " + "datanode" + " WHERE " +
54                        "id = ? AND code = ?"
55                );
56        private final SimpleGdb.LazyPst pstBackpage = new SimpleGdb.LazyPst(
57                        "SELECT backpageText FROM datanode " +
58                        " WHERE id = ? AND code = ?"
59                );
60        private final SimpleGdb.LazyPst pstAttribute = new SimpleGdb.LazyPst(
61                        "SELECT attrvalue FROM attribute " +
62                        " WHERE id = ? AND code = ? AND attrname = ?"
63                );
64        private final SimpleGdb.LazyPst pstAllAttributes = new SimpleGdb.LazyPst(
65                        "SELECT attrname, attrvalue FROM attribute " +
66                        " WHERE id = ? AND code = ?"
67                );
68        private final SimpleGdb.LazyPst pstAttributesSet = new SimpleGdb.LazyPst(
69                        "SELECT attrname FROM attribute GROUP BY attrname"
70                );
71        private final SimpleGdb.LazyPst pstCrossRefs = new SimpleGdb.LazyPst (
72                        "SELECT dest.idRight, dest.codeRight FROM link AS src JOIN link AS dest " +
73                        "ON src.idLeft = dest.idLeft and src.codeLeft = dest.codeLeft " +
74                        "WHERE src.idRight = ? AND src.codeRight = ?"
75                );
76        private final SimpleGdb.LazyPst pstCrossRefsWithCode = new SimpleGdb.LazyPst (
77                        "SELECT dest.idRight, dest.codeRight FROM link AS src JOIN link AS dest " +
78                        "ON src.idLeft = dest.idLeft and src.codeLeft = dest.codeLeft " +
79                        "WHERE src.idRight = ? AND src.codeRight = ? AND dest.codeRight = ?"
80                );
81        private final SimpleGdb.LazyPst pstRefsByAttribute = new SimpleGdb.LazyPst (
82                        "SELECT datanode.id, datanode.code FROM datanode " +
83                        " LEFT JOIN attribute ON attribute.code = datanode.code AND attribute.id = datanode.id " +
84                        "WHERE attrName = ? AND attrValue = ?"
85                );
86        private final SimpleGdb.LazyPst pstFreeSearch = new SimpleGdb.LazyPst (
87                        "SELECT id, code FROM datanode WHERE " +
88                        "LOWER(ID) LIKE ?"
89                );
90        private final SimpleGdb.LazyPst pstAttributeSearch = new SimpleGdb.LazyPst (
91                        "SELECT id, code, attrvalue FROM attribute WHERE " +
92                        "attrname = 'Symbol' AND LOWER(attrvalue) LIKE ?"
93                );
94        private final SimpleGdb.LazyPst pstIdSearchWithAttributes = new SimpleGdb.LazyPst (
95                        "SELECT id, code, attrvalue FROM attribute WHERE " +
96                        "attrname = 'Symbol' AND LOWER(ID) LIKE ?"
97                );
98       
99        /** {@inheritDoc} */
100        public boolean xrefExists(Xref xref) throws IDMapperException
101        {
102                try 
103                {
104                        PreparedStatement pst = pstXrefExists.getPreparedStatement();
105                        pst.setString(1, xref.getId());
106                        pst.setString(2, xref.getDataSource().getSystemCode());
107                        ResultSet r = pst.executeQuery();
108
109                        while(r.next())
110                        {
111                                return true;
112                        }
113                }
114                catch (SQLException e)
115                {
116                        throw new IDMapperException (e);
117                }
118                return false;
119        }
120
121        /**
122         * Read the info table and return as properties.
123         * @return a map where keys are column names and values are the fields in the first row.
124         * @throws IDMapperException when the database became unavailable
125         */
126        private Map<String, String> getInfo() throws IDMapperException
127        {
128                Map<String, String> result = new HashMap<String, String>();
129                try
130                {
131                        PreparedStatement pst = pstInfo.getPreparedStatement();
132                        ResultSet rs = pst.executeQuery();
133                       
134                        if (rs.next())
135                        {
136                                ResultSetMetaData rsmd = rs.getMetaData();
137                                for (int i = 1; i <= rsmd.getColumnCount(); ++i)
138                                {
139                                        String key = rsmd.getColumnName(i);
140                                        String val = rs.getString(i);
141                                        result.put (key, val);
142                                }
143                        }
144                }
145                catch (SQLException ex)
146                {
147                        throw new IDMapperException (ex);
148                }
149               
150                return result;
151        }
152       
153
154        /**
155         * get Backpage info. In Schema v2, this was not stored in
156         * the attribute table but as a separate column, so this is treated
157         * as a special case. This method is called by <pre>getAttribute (ref, "Backpage")</pre>
158         * @param ref the entity to get backpage info for.
159         * @return Backpage info as string
160         * @throws IDMapperException when database is unavailable
161         */
162        private String getBpInfo(Xref ref) throws IDMapperException
163        {
164                try {
165                        PreparedStatement pst = pstBackpage.getPreparedStatement();
166                        pst.setString (1, ref.getId());
167                        pst.setString (2, ref.getDataSource().getSystemCode());
168                        ResultSet r = pst.executeQuery();
169                        String result = null;
170                        if (r.next())
171                        {
172                                result = r.getString(1);
173                        }
174                        return result;
175                } catch (SQLException e) { throw new IDMapperException (e); } //Gene not found
176        }
177
178        /** {@inheritDoc} */
179        public Set<Xref> mapID (Xref idc, DataSource... resultDs) throws IDMapperException
180        {
181                Set<Xref> refs = new HashSet<Xref>();
182               
183                if (idc.getDataSource() == null) return refs;
184                try
185                {
186                        PreparedStatement pst;
187                        if (resultDs.length != 1)
188                        {
189                                pst = pstCrossRefs.getPreparedStatement();
190                        }
191                        else
192                        {
193                                pst = pstCrossRefsWithCode.getPreparedStatement();
194                                pst.setString(3, resultDs[0].getSystemCode());
195                        }
196                       
197                        pst.setString(1, idc.getId());
198                        pst.setString(2, idc.getDataSource().getSystemCode());
199                       
200                        Set<DataSource> dsFilter = new HashSet<DataSource>(Arrays.asList(resultDs));
201
202                        ResultSet rs = pst.executeQuery();
203                        while (rs.next())
204                        {
205                                DataSource ds = DataSource.getBySystemCode(rs.getString(2));
206                                if (resultDs.length == 0 || dsFilter.contains(ds))
207                                {
208                                        refs.add (new Xref (rs.getString(1), ds));
209                                }
210                        }
211                }
212                catch (SQLException e)
213                {
214                        throw new IDMapperException (e);
215                }
216               
217                return refs;
218        }
219
220        /** {@inheritDoc} */
221        public List<Xref> getCrossRefsByAttribute(String attrName, String attrValue) throws IDMapperException {
222//              Logger.log.trace("Fetching cross references by attribute: " + attrName + " = " + attrValue);
223                List<Xref> refs = new ArrayList<Xref>();
224
225                try {
226                        PreparedStatement pst = pstRefsByAttribute.getPreparedStatement();
227                        pst.setString(1, attrName);
228                        pst.setString(2, attrValue);
229                        ResultSet r = pst.executeQuery();
230                        while(r.next()) {
231                                Xref ref = new Xref(r.getString(1), DataSource.getBySystemCode(r.getString(2)));
232                                refs.add(ref);
233                        }
234                } catch(SQLException e) {
235                        throw new IDMapperException (e);
236                }
237//              Logger.log.trace("End fetching cross references by attribute");
238                return refs;
239        }
240
241        /**
242         * Opens a connection to the Gene Database located in the given file.
243         * A new instance of this class is created automatically.
244         * @param dbName The file containing the Gene Database.
245         * @param con An existing SQL Connector.
246         * @param props PROP_RECREATE if you want to create a new database (possibly overwriting an existing one)
247         *      or PROP_NONE if you want to connect read-only
248         * @throws IDMapperException when the database could not be created or connected to
249         */
250        public SimpleGdbImpl2(String dbName, Connection con, int props) throws IDMapperException
251        {
252                super (con);
253               
254                if(dbName == null) throw new NullPointerException();
255                this.dbName = dbName;
256               
257                if ((props & DBConnector.PROP_RECREATE) == 0)
258                {
259                        try
260                        {
261                                con.setReadOnly(true);
262                        }
263                        catch (SQLException e)
264                        {
265                                throw new IDMapperException (e);
266                        }
267                        checkSchemaVersion();
268                }
269               
270                 caps = new SimpleGdbCapabilities();
271        }
272       
273        /**
274         * look at the info table of the current database to determine the schema version.
275         * @throws IDMapperException when looking up the schema version failed
276         */
277        private void checkSchemaVersion() throws IDMapperException
278        {
279                int version = 0;
280                try 
281                {
282                        ResultSet r = con.createStatement().executeQuery("SELECT schemaversion FROM info");
283                        if(r.next()) version = r.getInt(1);
284                }
285                catch (SQLException e)
286                {
287                        //Ignore, older db's don't even have schema version
288                }
289                if(version != GDB_COMPAT_VERSION)
290                {
291                        throw new IDMapperException ("Implementation and schema version mismatch");
292                }
293        }
294
295        /**
296         * Excecutes several SQL statements to create the tables and indexes in the database the given
297         * connection is connected to
298         * Note: Official GDB's are created by AP, not with this code.
299         * This is just here for testing purposes.
300         */
301        public void createGdbTables()
302        {
303//              Logger.log.info("Info:  Creating tables");
304                try 
305                {
306                        Statement sh = con.createStatement();
307                        sh.execute("DROP TABLE info");
308                        sh.execute("DROP TABLE link");
309                        sh.execute("DROP TABLE datanode");
310                        sh.execute("DROP TABLE attribute");
311                }
312                catch(SQLException e)
313                {
314//                      Logger.log.error("Unable to drop gdb tables (ignoring): " + e.getMessage());
315                }
316
317                try
318                {
319                        Statement sh = con.createStatement();
320                        sh.execute(
321                                        "CREATE TABLE                                   " +
322                                        "               info                                                    " +
323                                        "(        schemaversion INTEGER PRIMARY KEY             " +
324                        ")");
325//                      Logger.log.info("Info table created");
326                        sh.execute( //Add compatibility version of GDB
327                                        "INSERT INTO info VALUES ( " + GDB_COMPAT_VERSION + ")");
328//                      Logger.log.info("Version stored in info");
329                        sh.execute(
330                                        "CREATE TABLE                                   " +
331                                        "               link                                                    " +
332                                        " (   idLeft VARCHAR(50) NOT NULL,              " +
333                                        "     codeLeft VARCHAR(50) NOT NULL,    " +
334                                        "     idRight VARCHAR(50) NOT NULL,             " +
335                                        "     codeRight VARCHAR(50) NOT NULL,   " +
336                                        "     bridge VARCHAR(50),                               " +
337                                        "     PRIMARY KEY (idLeft, codeLeft,    " +
338                                        "               idRight, codeRight)                     " +
339                                        " )                                                                             ");
340//                      Logger.log.info("Link table created");
341                        sh.execute(
342                                        "CREATE TABLE                                   " +
343                                        "               datanode                                                " +
344                                        " (   id VARCHAR(50),                                   " +
345                                        "     code VARCHAR(50),                                 " +
346                                        "     backpageText VARCHAR(800),                " +
347                                        "     PRIMARY KEY (id, code)                    " +
348                                        " )                                                                             ");
349//                      Logger.log.info("DataNode table created");
350                        sh.execute(
351                                        "CREATE TABLE                                                   " +
352                                        "               attribute                                               " +
353                                        " (   id VARCHAR(50),                                   " +
354                                        "     code VARCHAR(50),                                 " +
355                                        "     attrname VARCHAR(50),                             " +
356                                        "         attrvalue VARCHAR(255)                        " +
357                                        " )                                                                             ");
358//                      Logger.log.info("Attribute table created");
359                }
360                catch (SQLException e)
361                {
362//                      Logger.log.error("while creating gdb tables: " + e.getMessage(), e);
363                }
364        }
365
366       
367        public static final int NO_LIMIT = 0;
368        public static final int NO_TIMEOUT = 0;
369        public static final int QUERY_TIMEOUT = 20; //seconds
370
371        /** {@inheritDoc} */
372        public Set<Xref> freeSearch (String text, int limit) throws IDMapperException
373        {               
374                Set<Xref> result = new HashSet<Xref>();
375                try {
376                        PreparedStatement ps1 = pstFreeSearch.getPreparedStatement();
377                        ps1.setQueryTimeout(QUERY_TIMEOUT);
378                        if(limit > NO_LIMIT)
379                        {
380                                ps1.setMaxRows(limit);
381                        }
382
383                        ps1.setString(1, "%" + text.toLowerCase() + "%");
384                        ResultSet r = ps1.executeQuery();
385                        while(r.next()) {
386                                String id = r.getString(1);
387                                DataSource ds = DataSource.getBySystemCode(r.getString(2));
388                                Xref ref = new Xref (id, ds);
389                                result.add (ref);
390                        }                       
391                }
392                catch (SQLException e)
393                {
394                        throw new IDMapperException(e);
395                }
396                return result;
397        }
398       
399    private PreparedStatement pstGene = null;
400    private PreparedStatement pstLink = null;
401    private PreparedStatement pstAttr = null;
402
403        /** {@inheritDoc} */
404        public int addGene(Xref ref, String bpText)
405        {
406        if (pstGene == null) throw new NullPointerException();
407                try 
408                {
409                        pstGene.setString(1, ref.getId());
410                        pstGene.setString(2, ref.getDataSource().getSystemCode());
411                        pstGene.setString(3, bpText);
412                        pstGene.executeUpdate();
413                }
414                catch (SQLException e)
415                {
416//                      Logger.log.error("" + ref, e);
417                        return 1;
418                }
419                return 0;
420    }
421   
422        /** {@inheritDoc} */
423    public int addAttribute(Xref ref, String attr, String val)
424    {
425        try {
426                pstAttr.setString(1, attr);
427                        pstAttr.setString(2, val);
428                        pstAttr.setString(3, ref.getId());
429                        pstAttr.setString(4, ref.getDataSource().getSystemCode());
430                        pstAttr.executeUpdate();
431                } catch (SQLException e) {
432//                      Logger.log.error(attr + "\t" + val + "\t" + ref, e);
433                        return 1;
434                }
435                return 0;
436    }
437
438        /** {@inheritDoc} */
439    public int addLink(Xref left, Xref right)
440    {
441        if (pstLink == null) throw new NullPointerException();
442        try 
443        {
444                        pstLink.setString(1, left.getId());
445                        pstLink.setString(2, left.getDataSource().getSystemCode());
446                        pstLink.setString(3, right.getId());
447                        pstLink.setString(4, right.getDataSource().getSystemCode());
448                        pstLink.executeUpdate();
449                }
450                catch (SQLException e)
451                {
452//                      Logger.log.error(left + "\t" + right , e);
453                        return 1;
454                }
455                return 0;
456        }
457
458        /**
459           Create indices on the database
460           You can call this at any time after creating the tables,
461           but it is good to do it only after inserting all data.
462           @throws IDMapperException on failure
463         */
464        public void createGdbIndices() throws IDMapperException
465        {
466                try
467                {
468                        Statement sh = con.createStatement();
469                        sh.execute(
470                                        "CREATE INDEX i_codeLeft" +
471                                        " ON link(codeLeft)"
472                        );
473                        sh.execute(
474                                        "CREATE INDEX i_idRight" +
475                                        " ON link(idRight)"
476                        );
477                        sh.execute(
478                                        "CREATE INDEX i_codeRight" +
479                                        " ON link(codeRight)"
480                        );
481                        sh.execute(
482                                        "CREATE INDEX i_code" +
483                                        " ON " + "datanode" + "(code)"
484                        );
485                }
486                catch (SQLException e)
487                {
488                        throw new IDMapperException (e);
489                }
490        }
491
492        /**
493           prepare for inserting genes and/or links.
494           @throws IDMapperException on failure
495         */
496        public void preInsert() throws IDMapperException
497        {
498                try
499                {
500                        con.setAutoCommit(false);
501                        pstGene = con.prepareStatement(
502                                "INSERT INTO datanode " +
503                                "       (id, code," +
504                                "        backpageText)" +
505                                "VALUES (?, ?, ?)"
506                        );
507                        pstLink = con.prepareStatement(
508                                "INSERT INTO link " +
509                                "       (idLeft, codeLeft," +
510                                "        idRight, codeRight)" +
511                                "VALUES (?, ?, ?, ?)"
512                        );
513                        pstAttr = con.prepareStatement(
514                                        "INSERT INTO attribute " +
515                                        "       (attrname, attrvalue, id, code)" +
516                                        "VALUES (?, ?, ?, ?)"
517                                        );
518                }
519                catch (SQLException e)
520                {
521                        throw new IDMapperException (e);
522                }
523        }
524
525        /**
526         * @return a list of data sources present in this database.
527           @throws IDMapperException when the database is unavailable
528         */
529        private Set<DataSource> getDataSources() throws IDMapperException
530        {
531                Set<DataSource> result = new HashSet<DataSource>();
532        try
533        {
534                PreparedStatement pst = pstDatasources.getPreparedStatement();
535                ResultSet rs = pst.executeQuery();
536                while (rs.next())
537                {
538                        DataSource ds = DataSource.getBySystemCode(rs.getString(1));
539                        result.add (ds);
540                }
541        }
542        catch (SQLException ignore)
543        {
544                throw new IDMapperException(ignore);
545        }
546        return result;
547        }
548       
549        private final IDMapperCapabilities caps;
550
551        private class SimpleGdbCapabilities extends AbstractIDMapperCapabilities
552        {
553                /** default constructor.
554                 * @throws IDMapperException when database is not available */
555                public SimpleGdbCapabilities() throws IDMapperException
556                {
557                        super (SimpleGdbImpl2.this.getDataSources(), true,
558                                        SimpleGdbImpl2.this.getInfo());
559                }
560        }
561       
562        /**
563         * @return the capabilities of this gene database
564         */
565        public IDMapperCapabilities getCapabilities()
566        {
567                return caps;
568        }
569       
570        private static final Map<String, String> ATTRIBUTES_FROM_BACKPAGE;
571       
572        static
573        {
574                ATTRIBUTES_FROM_BACKPAGE = new HashMap<String, String>();
575                ATTRIBUTES_FROM_BACKPAGE.put ("Chromosome", "<TH>Chr:<TH>([^<]*)<");
576                ATTRIBUTES_FROM_BACKPAGE.put ("Description", "<TH>Description:<TH>([^<]*)<");
577                ATTRIBUTES_FROM_BACKPAGE.put ("Synonyms", "<TH>Synonyms:<TH>([^<]*)<");
578                ATTRIBUTES_FROM_BACKPAGE.put ("Symbol", "<TH>(?:Gene Symbol|Metabolite):<TH>([^<]*)<");
579                ATTRIBUTES_FROM_BACKPAGE.put ("BrutoFormula", "<TH>Bruto Formula:<TH>([^<]*)<");
580        }
581
582        /** {@inheritDoc} */
583        public Set<String> getAttributes(Xref ref, String attrname)
584                        throws IDMapperException
585        {
586                Set<String> result = new HashSet<String>();
587               
588                if (ATTRIBUTES_FROM_BACKPAGE.containsKey(attrname))
589                {
590                        String bpInfo = getBpInfo(ref);
591                        if (bpInfo != null)
592                        {
593                                Pattern pat = Pattern.compile(ATTRIBUTES_FROM_BACKPAGE.get (attrname));
594                                Matcher matcher = pat.matcher(bpInfo);
595                                if (matcher.find())
596                                {
597                                        result.add (matcher.group(1));
598                                }
599                        }
600                }
601               
602                try {
603                        PreparedStatement pst = pstAttribute.getPreparedStatement();
604                        pst.setString (1, ref.getId());
605                        pst.setString (2, ref.getDataSource().getSystemCode());
606                        pst.setString (3, attrname);
607                        ResultSet r = pst.executeQuery();
608                        if (r.next())
609                        {
610                                result.add (r.getString(1));
611                        }
612                        return result;
613                } catch (SQLException e) { throw new IDMapperException ("Xref:" + ref + ", Attribute: " + attrname, e); } // Database unavailable
614        }
615
616        /** {@inheritDoc} */
617        public Map<String, Set<String>> getAttributes(Xref ref)
618                        throws IDMapperException
619        {
620                Map<String, Set<String>> result = new HashMap<String, Set<String>>();
621                               
622                String bpInfo = getBpInfo(ref);
623                if (bpInfo != null)
624                {
625                        for (String attrname : ATTRIBUTES_FROM_BACKPAGE.keySet())
626                        {
627                                Pattern pat = Pattern.compile(ATTRIBUTES_FROM_BACKPAGE.get (attrname));
628                                Matcher matcher = pat.matcher(bpInfo);
629                                if (matcher.find())
630                                {
631                                        Set<String> attrSet = new HashSet<String>();
632                                        attrSet.add (matcher.group(1));
633                                        result.put (attrname, attrSet);
634                                }
635                        }
636                }
637               
638                try {
639                        PreparedStatement pst = pstAllAttributes.getPreparedStatement();
640                        pst.setString (1, ref.getId());
641                        pst.setString (2, ref.getDataSource().getSystemCode());
642                        ResultSet r = pst.executeQuery();
643                        if (r.next())
644                        {
645                                String key = r.getString(1);
646                                String value = r.getString(2);
647                                if (result.containsKey (key))
648                                {
649                                        result.get(key).add (value);
650                                }
651                                else
652                                {
653                                        Set<String> valueSet = new HashSet<String>();
654                                        valueSet.add (value);
655                                        result.put (key, valueSet);
656                                }
657                        }
658                        return result;
659                } catch (SQLException e) { throw new IDMapperException ("Xref:" + ref, e); } // Database unavailable
660        }
661
662        /**
663         *
664         * @return true
665         */
666        public boolean isFreeAttributeSearchSupported()
667        {
668                return true;
669        }
670
671        /**
672         * free text search for matching symbols.
673         * @return references that match the query
674         * @param query The text to search for
675         * @param attrType the attribute to look for, e.g. 'Symbol' or 'Description'.
676         * @param limit The number of results to limit the search to
677         * @throws IDMapperException if the mapping service is (temporarily) unavailable
678         */
679        public Map<Xref, String> freeAttributeSearch (String query, String attrType, int limit) throws IDMapperException
680        {
681                Map<Xref, String> result = new HashMap<Xref, String>();
682                try {
683                        PreparedStatement pst = (MATCH_ID.equals (attrType)) ?
684                                        pstIdSearchWithAttributes.getPreparedStatement() : pstAttributeSearch.getPreparedStatement();
685                        pst.setQueryTimeout(QUERY_TIMEOUT);
686                        if(limit > NO_LIMIT) pst.setMaxRows(limit);
687                        pst.setString(1, "%" + query.toLowerCase() + "%");
688                        ResultSet r = pst.executeQuery();
689
690                        while(r.next())
691                        {
692                                String id = r.getString("id");
693                                String code = r.getString("code");
694                                String symbol = r.getString("attrValue");
695                                result.put(new Xref (id, DataSource.getBySystemCode(code)), symbol);
696                        }
697                } catch (SQLException e) {
698                        throw new IDMapperException (e);
699                }
700                return result;         
701        }
702
703        /** {@inheritDoc} */
704        public Set<String> getAttributeSet() throws IDMapperException
705        {
706                Set<String> result = new HashSet<String>();
707        try
708        {
709                PreparedStatement pst = pstAttributesSet.getPreparedStatement();
710                ResultSet rs = pst.executeQuery();
711                while (rs.next())
712                {
713                        result.add (rs.getString(1));
714                }
715        }
716        catch (SQLException ignore)
717        {
718                throw new IDMapperException(ignore);
719        }
720        return result;
721        }
722}
Note: See TracBrowser for help on using the browser.