View Javadoc

1   /*
2    ORDI - Ontology Repository and Data Integration
3   
4    Copyright (c) 2004-2007, OntoText Lab. / SIRMA
5   
6    This library is free software; you can redistribute it and/or modify it under
7    the terms of the GNU Lesser General Public License as published by the Free
8    Software Foundation; either version 2.1 of the License, or (at your option)
9    any later version.
10   This library is distributed in the hope that it will be useful, but WITHOUT
11   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13   details.
14   You should have received a copy of the GNU Lesser General Public License along
15   with this library; if not, write to the Free Software Foundation, Inc.,
16   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17   */
18  package com.ontotext.ordi.mapper.rdbms;
19  
20  import java.sql.PreparedStatement;
21  import java.sql.SQLException;
22  
23  import org.apache.log4j.Logger;
24  import org.openrdf.model.Resource;
25  import org.openrdf.model.URI;
26  import org.openrdf.model.Value;
27  
28  import com.ontotext.ordi.exception.ORDIRuntimeException;
29  import com.ontotext.ordi.mapper.model.DataHandler;
30  import com.ontotext.ordi.mapper.model.PredicatePattern;
31  import com.ontotext.ordi.mapper.processor.RDFResultManager;
32  
33  public class JdbcResultSet extends JdbcRDFResultSetBase {
34  
35      protected DataHandler subjectHandler;
36      protected DataHandler objectHandler;
37  
38      private Resource[] subj;
39      private Value[] obj;
40      private int subjIndex;
41      private int objIndex;
42      private Logger logger = Logger.getLogger(JdbcResultSet.class);
43  
44      public JdbcResultSet(PreparedStatement statement,
45              PredicatePattern predicatePattern, RDFResultManager manager) {
46          super(statement, predicatePattern, manager);
47          subjectHandler = predicatePattern.getSubjectHandler();
48          objectHandler = predicatePattern.getObjectHandler();
49      }
50  
51      public Value getObject() {
52          try {
53              return obj[objIndex];
54          } catch (Exception e) {
55              throw new IllegalStateException(
56                      "The ResullSet is not ready for reading!");
57          }
58      }
59  
60      public Resource getSubject() {
61          try {
62              return subj[subjIndex];
63          } catch (Exception e) {
64              throw new IllegalStateException(
65                      "The ResullSet is not ready for reading!");
66          }
67      }
68  
69      public URI[] getTriplesets() {
70          return new URI[0];
71      }
72  
73      public boolean next() {
74          
75          if (result == null) {
76              return false; // exception occurred in super.run()
77          }
78  
79          if (subj != null && obj != null) {
80  
81              if (++subjIndex < subj.length)
82                  return true;
83              if (++objIndex < obj.length) {
84                  subjIndex = 0;
85                  return true;
86              }
87          }
88  
89          try {
90              do {
91                  boolean hasNext = result.next();
92                  if (hasNext == false) {
93                      return false;
94                  }
95              } while (result.getObject(1) == null || result.getObject(2) == null);
96  
97              subj = (Resource[]) subjectHandler.sqlToValue(result.getObject(1));
98              obj = objectHandler.sqlToValue(result.getObject(2));
99              subjIndex = 0;
100             objIndex = 0;
101             return true;
102         } catch (SQLException se) {
103             String msg = "Error during reading data from the database!";
104             logger.error(msg);
105             throw new ORDIRuntimeException(msg, se);
106         } catch (ClassCastException ce) {
107             String msg = String
108                     .format("The subject are constrained to Resource type only. "
109                             + "Please check the implementation of the DataHandler!");
110             logger.error(msg);
111             throw new ORDIRuntimeException(ce);
112         }
113     }
114     
115     protected void finalize() throws Throwable {
116         if (result != null)
117             result.close();
118     }
119 }