gate.db
Class DocumentWrapper

java.lang.Object
  |
  +--gate.util.AbstractFeatureBearer
        |
        +--gate.creole.AbstractResource
              |
              +--gate.creole.AbstractLanguageResource
                    |
                    +--gate.corpora.DocumentImpl
                          |
                          +--gate.db.DocumentWrapper
All Implemented Interfaces:
Comparable, Document, FeatureBearer, LanguageResource, LRDBWrapper, Resource, Serializable

public class DocumentWrapper
extends DocumentImpl
implements LRDBWrapper

This class extends DocumentImpl adding database persistence via Jdbc.

See Also:
Serialized Form

Field Summary
protected  Connection con
           
protected  Database database
           
protected static Table[] DDL
           
private static boolean DEBUG
          Debug flag
protected  long id
           
protected  boolean isNew
           
protected  PreparedStatement psAnnotation
           
protected  PreparedStatement psNode
           
protected  ResultSet rsAnnotations
           
protected  ResultSet rsDocument
           
protected  ResultSet rsFeatures
           
protected  Statement stmt
           
 
Fields inherited from class gate.corpora.DocumentImpl
content, defaultAnnots, documentListeners, encoding, features, gateListeners, markupAware, namedAnnotSets, nextAnnotationId, nextNodeId, serialVersionUID, sourceUrl, sourceUrlEndOffset, sourceUrlStartOffset, statusListeners, stringContent
 
Fields inherited from class gate.creole.AbstractLanguageResource
dataStore
 
Constructor Summary
DocumentWrapper(Connection con, Database db, DocumentImpl doc)
          Construction from Document.
DocumentWrapper(Connection con, Database ds, long id)
          Construction from ID.
DocumentWrapper(DataStore ds, DocumentImpl doc)
          Construction from Document.
DocumentWrapper(DataStore ds, long id)
          Construction from ID.
DocumentWrapper(DataStore ds, String urlString)
          Construction from String representing URL
DocumentWrapper(DataStore ds, URL u)
          Construction from URL; content read over the net.
DocumentWrapper(DataStore ds, URL u, Long start, Long end)
          Construction from URL and offsets.
DocumentWrapper(URL u, String encoding, Long start, Long end)
          Construction from URL and offsets.
 
Method Summary
(package private) static void ()
           
 void beginTransaction()
          Starts a transaction
 void commit()
          Commits the reads/writes
private  void connectToDb()
          Gets a new connection and starts a transaction on that connection
private  void DBInit()
          This method is actually the persistent part of any of the constructors.
 void disconnect()
          Disconnects from the Datastore object, i.e.
 DataStore getDataStore()
          Returns the Datastore object associated to this persistent document.
 long getId()
          Gets the database ID of this persistent object.
 boolean isNew()
          Can be used to check if this document is an old one that has been retrieved from the databse or is a new one that has just been saved.
 void rollback()
          Rolls back all the actions performed since the last beginTransaction call
static boolean setupDatabase(DataStore ds)
          Checks if the provided datastore id proper for storing persistent documents of this type.
protected  void writeAnnotation(String setName, Annotation ann)
          This method writes an annotation into the database.
 
Methods inherited from class gate.corpora.DocumentImpl
addDocumentListener, addGateListener, addStatusListener, annotationSetToXml, check, compareTo, edit, equals, featuresToXml, fireAnnotationSetAdded, fireAnnotationSetRemoved, fireGateEvent, fireStatusChanged, getAnnotations, getAnnotations, getContent, getEncoding, getFeatures, getMarkupAware, getNamedAnnotationSets, getNextAnnotationId, getNextNodeId, getOrderingString, getSourceUrl, getSourceUrlEndOffset, getSourceUrlOffsets, getSourceUrlStartOffset, hashCode, init, isValidOffset, isValidOffsetRange, removeAnnotationSet, removeDocumentListener, removeGateListener, removeStatusListener, setContent, setEncoding, setFeatures, setMarkupAware, setSourceUrl, setSourceUrlEndOffset, setSourceUrlStartOffset, setStringContent, textWithNodes, toString, toXml
 
Methods inherited from class gate.creole.AbstractLanguageResource
setDataStore, sync
 
Methods inherited from class gate.creole.AbstractResource
getName, setName
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, registerNatives, wait, wait, wait
 
Methods inherited from interface gate.LanguageResource
setDataStore, sync
 
Methods inherited from interface gate.Resource
init
 
Methods inherited from interface gate.util.FeatureBearer
getFeatures, getName, setFeatures, setName
 

Field Detail

DEBUG

private static final boolean DEBUG
Debug flag

id

protected long id

database

protected Database database

con

protected Connection con

stmt

protected Statement stmt

rsDocument

protected ResultSet rsDocument

rsAnnotations

protected ResultSet rsAnnotations

rsFeatures

protected ResultSet rsFeatures

psAnnotation

protected PreparedStatement psAnnotation

psNode

protected PreparedStatement psNode

isNew

protected boolean isNew

DDL

protected static Table[] DDL
Constructor Detail

DocumentWrapper

public DocumentWrapper(URL u,
                       String encoding,
                       Long start,
                       Long end)
                throws IOException
Construction from URL and offsets. Both offsets must be non-null, or they are both ignored. Content is read over the net between the offsets.

DocumentWrapper

public DocumentWrapper(DataStore ds,
                       URL u,
                       Long start,
                       Long end)
                throws IOException,
                       SQLException
Construction from URL and offsets. Both offsets must be non-null, or they are both ignored. Content is read over the net between the offsets.

DocumentWrapper

public DocumentWrapper(DataStore ds,
                       URL u)
                throws IOException,
                       SQLException
Construction from URL; content read over the net.

DocumentWrapper

public DocumentWrapper(DataStore ds,
                       String urlString)
                throws IOException,
                       SQLException
Construction from String representing URL

DocumentWrapper

public DocumentWrapper(DataStore ds,
                       DocumentImpl doc)
                throws IOException,
                       SQLException
Construction from Document. This is the recommended way for making a transient document become persistent.

DocumentWrapper

public DocumentWrapper(Connection con,
                       Database db,
                       DocumentImpl doc)
                throws IOException
Construction from Document. This is the recommended way for making a transient document become persistent. Unlike the other constructor that gets a document, thus one receives an open connection rather than a DataStore. This is useful for clients that want this document to be read/written to the database inside their open transaction.

DocumentWrapper

public DocumentWrapper(DataStore ds,
                       long id)
                throws IOException,
                       NoSuchObjectException,
                       SQLException
Construction from ID. This is the way to get the document when the Id is already known.

DocumentWrapper

public DocumentWrapper(Connection con,
                       Database ds,
                       long id)
                throws IOException,
                       NoSuchObjectException,
                       SQLException
Construction from ID. This is the way to get the document when the Id is already known.
Method Detail

DBInit

private void DBInit()
             throws IOException,
                    NoSuchObjectException
This method is actually the persistent part of any of the constructors.
Throws:
NoSuchObjectException - when an attempt to read an inexistant object from the database(e.g. when an invalid ID is used to retrieve a document).
IOException - if an error occurs while trying to read the document content.

connectToDb

private void connectToDb()
                  throws SQLException
Gets a new connection and starts a transaction on that connection

writeAnnotation

protected void writeAnnotation(String setName,
                               Annotation ann)
                        throws SQLException,
                               IOException
This method writes an annotation into the database.
Parameters:
setName - The name of the Annotation Set this annotation belongs to.
ann - The annotation to be written into DB.
Throws:
SQLException - if an error occurs

rollback

public void rollback()
              throws SQLException
Rolls back all the actions performed since the last beginTransaction call
Specified by:
rollback in interface LRDBWrapper

beginTransaction

public void beginTransaction()
                      throws SQLException
Starts a transaction
Specified by:
beginTransaction in interface LRDBWrapper

commit

public void commit()
            throws SQLException
Commits the reads/writes
Specified by:
commit in interface LRDBWrapper

disconnect

public void disconnect()
                throws GateException
Disconnects from the Datastore object, i.e. returns the connection
Specified by:
disconnect in interface LRDBWrapper

getDataStore

public DataStore getDataStore()
Returns the Datastore object associated to this persistent document.
Specified by:
getDataStore in interface LanguageResource
Overrides:
getDataStore in class AbstractLanguageResource
Returns:
a value of type 'DataStore'

isNew

public boolean isNew()
Can be used to check if this document is an old one that has been retrieved from the databse or is a new one that has just been saved.
Returns:
a value of type 'boolean'

setupDatabase

public static boolean setupDatabase(DataStore ds)
Checks if the provided datastore id proper for storing persistent documents of this type.
Parameters:
ds - a value of type 'DataStore'
Returns:
a value of type 'boolean'

getId

public long getId()
Gets the database ID of this persistent object.
Returns:
a value of type 'long'

static void ()