de.topicmapslab.majortom.importer.file
Class FileWriterMapHandler

java.lang.Object
  extended by de.topicmapslab.majortom.importer.file.FileWriterMapHandler
All Implemented Interfaces:
IHandler

public class FileWriterMapHandler
extends java.lang.Object
implements IHandler

Special Map Handler to write topic map constructs to SQL commands.

Author:
Sven Krosse

Field Summary
private  Sql99QueryBuilder builder
          the query builder of MaJorToM
private  java.sql.Connection connection
          the current connection
private  long id
          current construct Id
private  java.util.Map<java.lang.String,java.lang.Long> knownQueries
          query cache storing the known constructs as SQL Query
private  org.apache.commons.collections.BidiMap locatorCache
          locator cache storing the reference to the locator Id
private  long locatorId
          current locator Id
private static java.lang.String NULL
           
private  java.io.OutputStream os
          the output stream to write the SQL commands
private static java.lang.String PLACEHOLDER
          wildcard variable
private  PostGreSqlConnectionProvider provider
          the PostGreSql connection provider
private  long scopeId
          current scope Id
private  java.util.Map<java.util.Set<java.lang.Long>,java.lang.Long> scopesCache
          scope cache storing the theme Id to the scope id
private  PostGreSqlSession session
          the session
private  java.util.Map<com.semagia.mio.IRef,java.lang.Long> topicCache
          topic cache, storing the topic references to the topic Id
private  long topicMapId
          the topic map id
private static java.lang.String UTF_8
          the UTF-8 constant
 
Constructor Summary
FileWriterMapHandler(java.io.OutputStream os)
          Constructor
FileWriterMapHandler(java.io.OutputStream os, java.util.Properties properties)
          Constructor
 
Method Summary
 void addAssociation(Association assoc)
          Adds the given association to the database.
 void addIdentifier(long topicId, java.lang.String ref, int type)
          adds an identifier
 void addName(Name name)
          Adds a name to the topic map
 void addOccurrence(Occurrence occurrence)
          Adds an occurrence
private  void addRole(long assocID, Role role)
          Adds a role to the association
 void addType(long currTopicId, com.semagia.mio.IRef arg0)
          Adds a type to the topic with the given id
private  void addVariant(long id, Variant variant)
          Adds a variant to database
 void commit()
          Commits the last statements
 void end()
          Closes the connection
private  java.lang.String escape(java.lang.String value)
          Utility method to escape SQL reserved symbols as part of string values
 java.lang.String getLocatorId(java.lang.String ref)
          Returns the locator id of the given reference
private  java.lang.String getScopeId(java.util.List<com.semagia.mio.IRef> themes)
          Returns the scope Id of the given themes
 long getTopic(com.semagia.mio.IRef ref)
          Returns the topic id for the given identifier.
 long getTopicMapId(java.lang.String locator)
          Returns the id of the topic map for the given locator.
private  void init(java.util.Properties properties)
          Initialize the database and get the topic map id
 void preInitialization()
          Method calls the database and load existing content of the topic map
 void start()
          Handler for start event preparing the connection
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NULL

private static final java.lang.String NULL
See Also:
Constant Field Values

UTF_8

private static final java.lang.String UTF_8
the UTF-8 constant

See Also:
Constant Field Values

PLACEHOLDER

private static final java.lang.String PLACEHOLDER
wildcard variable

See Also:
Constant Field Values

id

private long id
current construct Id


locatorId

private long locatorId
current locator Id


scopeId

private long scopeId
current scope Id


provider

private PostGreSqlConnectionProvider provider
the PostGreSql connection provider


session

private PostGreSqlSession session
the session


topicMapId

private long topicMapId
the topic map id


builder

private Sql99QueryBuilder builder
the query builder of MaJorToM


os

private java.io.OutputStream os
the output stream to write the SQL commands


connection

private java.sql.Connection connection
the current connection


topicCache

private java.util.Map<com.semagia.mio.IRef,java.lang.Long> topicCache
topic cache, storing the topic references to the topic Id


locatorCache

private org.apache.commons.collections.BidiMap locatorCache
locator cache storing the reference to the locator Id


scopesCache

private java.util.Map<java.util.Set<java.lang.Long>,java.lang.Long> scopesCache
scope cache storing the theme Id to the scope id


knownQueries

private java.util.Map<java.lang.String,java.lang.Long> knownQueries
query cache storing the known constructs as SQL Query

Constructor Detail

FileWriterMapHandler

public FileWriterMapHandler(java.io.OutputStream os)
                     throws com.semagia.mio.MIOException
Constructor

Parameters:
os - the output stream
Throws:
com.semagia.mio.MIOException

FileWriterMapHandler

public FileWriterMapHandler(java.io.OutputStream os,
                            java.util.Properties properties)
                     throws com.semagia.mio.MIOException
Constructor

Parameters:
os - the output stream
properties - the DB connection properties
Throws:
com.semagia.mio.MIOException
Method Detail

init

private void init(java.util.Properties properties)
           throws com.semagia.mio.MIOException
Initialize the database and get the topic map id

Parameters:
properties - the properties to connect to db
Throws:
com.semagia.mio.MIOException

preInitialization

public void preInitialization()
                       throws com.semagia.mio.MIOException
Method calls the database and load existing content of the topic map

Throws:
com.semagia.mio.MIOException - thrown if anything fails

commit

public void commit()
            throws com.semagia.mio.MIOException
Commits the last statements

Specified by:
commit in interface IHandler
Throws:
com.semagia.mio.MIOException

start

public void start()
           throws com.semagia.mio.MIOException
Handler for start event preparing the connection

Specified by:
start in interface IHandler
Throws:
com.semagia.mio.MIOException

end

public void end()
         throws com.semagia.mio.MIOException
Closes the connection

Specified by:
end in interface IHandler
Throws:
com.semagia.mio.MIOException

getTopicMapId

public long getTopicMapId(java.lang.String locator)
                   throws com.semagia.mio.MIOException
Returns the id of the topic map for the given locator. If it does not exist it will be created.

Specified by:
getTopicMapId in interface IHandler
Parameters:
locator - locator of the topic map
Returns:
the id of the topic map
Throws:
com.semagia.mio.MIOException

addAssociation

public void addAssociation(Association assoc)
                    throws com.semagia.mio.MIOException
Adds the given association to the database.

Specified by:
addAssociation in interface IHandler
Parameters:
assoc - the association to add
Throws:
com.semagia.mio.MIOException

addRole

private void addRole(long assocID,
                     Role role)
              throws com.semagia.mio.MIOException
Adds a role to the association

Parameters:
assocID - the id of the parent association
role - the role helper
Throws:
com.semagia.mio.MIOException

addName

public void addName(Name name)
             throws com.semagia.mio.MIOException
Adds a name to the topic map

Specified by:
addName in interface IHandler
Parameters:
name -
Throws:
com.semagia.mio.MIOException

addOccurrence

public void addOccurrence(Occurrence occurrence)
                   throws com.semagia.mio.MIOException
Adds an occurrence

Specified by:
addOccurrence in interface IHandler
Parameters:
occurrence -
Throws:
com.semagia.mio.MIOException

getTopic

public long getTopic(com.semagia.mio.IRef ref)
              throws com.semagia.mio.MIOException
Returns the topic id for the given identifier. It it isn't in the db, it will be created.

Specified by:
getTopic in interface IHandler
Parameters:
ref - the identifier of the topic
Returns:
the id of the topic
Throws:
com.semagia.mio.MIOException

getLocatorId

public java.lang.String getLocatorId(java.lang.String ref)
                              throws com.semagia.mio.MIOException
Returns the locator id of the given reference

Parameters:
ref - the reference
Returns:
the locator Id as String
Throws:
com.semagia.mio.MIOException

addIdentifier

public void addIdentifier(long topicId,
                          java.lang.String ref,
                          int type)
                   throws com.semagia.mio.MIOException
adds an identifier

Specified by:
addIdentifier in interface IHandler
Parameters:
topicId - id of the topic
ref - the URI
type - the type of identifier
Throws:
com.semagia.mio.MIOException

addType

public void addType(long currTopicId,
                    com.semagia.mio.IRef arg0)
             throws com.semagia.mio.MIOException
Adds a type to the topic with the given id

Specified by:
addType in interface IHandler
Parameters:
currTopicId - the current topic id
arg0 - the reference of the typing topic
Throws:
com.semagia.mio.MIOException

getScopeId

private java.lang.String getScopeId(java.util.List<com.semagia.mio.IRef> themes)
                             throws com.semagia.mio.MIOException
Returns the scope Id of the given themes

Parameters:
themes - the themes
Returns:
the scope Id
Throws:
com.semagia.mio.MIOException

addVariant

private void addVariant(long id,
                        Variant variant)
                 throws com.semagia.mio.MIOException
Adds a variant to database

Parameters:
id - the name Id
variant - the variant container
Throws:
com.semagia.mio.MIOException

escape

private java.lang.String escape(java.lang.String value)
Utility method to escape SQL reserved symbols as part of string values

Parameters:
value - the value
Returns:
the escaped value