import org.apache.log4j.Logger;
import org.splat.dal.bo.kernel.User;
-import org.splat.dal.bo.som.Document;
import org.splat.dal.bo.som.IDBuilder;
-import org.splat.dal.bo.som.KnowledgeElement;
import org.splat.dal.bo.som.ProgressState;
import org.splat.dal.bo.som.SimulationContext;
import org.splat.dal.bo.som.SimulationContextType;
-import org.splat.dal.bo.som.Study;
import org.splat.kernel.UserDirectory;
import org.splat.kernel.InvalidPropertyException;
import org.splat.service.technical.IndexService;
public class Database extends org.splat.dal.dao.kernel.Database {
private int uplevel = 0; // Level of database upgrade
- private String basepath = null; // Path of the root directory of repository
private RepositoryService _repositoryService;
private IndexService _indexService;
private SessionFactory _sessionFactory;
private static Database my = null; // Singleton instance
- protected class CreateTables extends
- org.splat.dal.dao.kernel.Database.CreateTables {
- // ---------------------------------------------------------------------------
- public void execute(Connection connex) throws SQLException {
- super.execute(connex);
-
- // Study Entity
- String create = "CREATE TABLE `study` ("
- + "`rid` int(10) UNSIGNED NOT NULL,"
- + "`sid` tinytext NOT NULL,"
- + "`title` tinytext NOT NULL,"
- + "`state` enum('inWORK','inDRAFT','inCHECK','APPROVED', 'TEMPLATE') NOT NULL default 'inWORK',"
- + "`area` enum('PRIVATE','PUBLIC','REFERENCE') NOT NULL default 'PRIVATE',"
- + "`manager` int(10) NOT NULL,"
- + "`version` tinytext NOT NULL,"
- + "`docount` int(10) UNSIGNED NOT NULL,"
- + "`history` int(10) UNSIGNED NOT NULL,"
- + "`credate` date NOT NULL,"
- + "`lasdate` date NOT NULL," + "PRIMARY KEY (`rid`)"
- + ") ENGINE=MyISAM DEFAULT CHARSET=latin1";
- request.execute(create);
-
- // Scenario Entity
- create = "CREATE TABLE `scenario` ("
- + "`rid` int(10) UNSIGNED NOT NULL,"
- + "`sid` int(10) UNSIGNED NOT NULL,"
- + "`owner` int(10) NOT NULL,"
- + "`scendex` int(3) NOT NULL,"
- + "`title` tinytext NOT NULL,"
- + "`manager` int(10) NOT NULL," + "`cuser` int(10),"
- + "`credate` date NOT NULL,"
- + "`lasdate` date NOT NULL," + "PRIMARY KEY (`rid`)"
- + ") ENGINE=MyISAM DEFAULT CHARSET=latin1";
- request.execute(create);
-
- // Document Entity and document tag (Publication)
- create = "CREATE TABLE `document` ("
- + "`rid` int(10) UNSIGNED NOT NULL,"
- + "`did` tinytext NOT NULL,"
- + "`type` int(10) NOT NULL,"
- + "`step` int(10) NOT NULL,"
- + "`state` enum('inWORK','inDRAFT','inCHECK','APPROVED','EXTERN') NOT NULL default 'inWORK',"
- + "`name` tinytext NOT NULL,"
- + "`author` int(10) NOT NULL," + "`version` tinytext,"
- + "`countag` int(10) UNSIGNED NOT NULL,"
- + "`history` int(10) NOT NULL,"
- + "`myfile` int(10) NOT NULL,"
- + "`lasdate` date NOT NULL," + "PRIMARY KEY (`rid`)"
- + ") ENGINE=MyISAM DEFAULT CHARSET=latin1";
- request.execute(create);
- create = "CREATE TABLE `doctag` ("
- + "`rid` int(10) UNSIGNED NOT NULL auto_increment,"
- + "`doc` int(10) NOT NULL,"
- + "`owner` int(10) NOT NULL,"
- + "`isnew` char(1) NOT NULL," + "PRIMARY KEY (`rid`)"
- + ") ENGINE=MyISAM DEFAULT CHARSET=latin1";
- request.execute(create);
- // Document types
- create = "CREATE TABLE `doctype` ("
- + "`rid` int(10) UNSIGNED NOT NULL auto_increment,"
- + "`name` tinytext NOT NULL,"
- + "`state` enum('inCHECK','APPROVED') NOT NULL default 'inCHECK',"
- + "`step` tinytext NOT NULL," + "`result` tinytext,"
- + "PRIMARY KEY (`rid`)"
- + ") ENGINE=MyISAM DEFAULT CHARSET=latin1";
- request.execute(create);
- // Document types dependencies
- create = "CREATE TABLE `docuse` (" + "`owner` int(10) NOT NULL,"
- + "`rid` int(10) NOT NULL"
- + ") ENGINE=MyISAM DEFAULT CHARSET=latin1";
- request.execute(create);
-
- // ValidationCycle related object
- create = "CREATE TABLE `cycle` ("
- + "`rid` int(10) UNSIGNED NOT NULL auto_increment,"
- + "`type` int(10) NOT NULL," + "`publisher` int(10),"
- + "`reviewer` int(10)," + "`approver` int(10),"
- + "`signatory` int(10)," + "PRIMARY KEY (`rid`)"
- + ") ENGINE=MyISAM DEFAULT CHARSET=latin1";
- request.execute(create);
-
- // Timestamp related object
- create = "CREATE TABLE `stamp` ("
- + "`rid` int(10) UNSIGNED NOT NULL auto_increment,"
- + "`type` enum('PROMOTION','REVIEW','APPROVAL','ACCEPTANCE','DISTRIBUTION','REFUSAL') NOT NULL,"
- + "`author` int(10) NOT NULL,"
- + "`date` datetime NOT NULL," + "PRIMARY KEY (`rid`)"
- + ") ENGINE=MyISAM DEFAULT CHARSET=latin1";
- request.execute(create);
-
- // KnowledgeElements objects
- create = "CREATE TABLE `knowelm` ("
- + "`rid` int(10) UNSIGNED NOT NULL auto_increment,"
- + "`type` int(10) NOT NULL,"
- + "`owner` int(10) NOT NULL,"
- + "`state` enum('inWORK','inDRAFT','inCHECK','APPROVED') NOT NULL default 'inDRAFT',"
- + "`title` tinytext NOT NULL,"
- + "`value` text NOT NULL,"
- + "`author` int(10) NOT NULL,"
- + "`date` date NOT NULL," + "PRIMARY KEY (`rid`)"
- + ") ENGINE=MyISAM DEFAULT CHARSET=latin1";
- request.execute(create);
- // KnowledgeElement types
- create = "CREATE TABLE `knowtype` ("
- + "`rid` int(10) UNSIGNED NOT NULL auto_increment,"
- + "`name` tinytext NOT NULL,"
- + "`state` enum('inWORK','inCHECK','APPROVED') NOT NULL default 'inCHECK',"
- + "PRIMARY KEY (`rid`)"
- + ") ENGINE=MyISAM DEFAULT CHARSET=latin1";
- request.execute(create);
-
- // SimulationContext objects
- create = "CREATE TABLE `contelm` ("
- + "`rid` int(10) UNSIGNED NOT NULL auto_increment,"
- + "`type` int(10) NOT NULL,"
- + "`step` int(10) NOT NULL,"
- + "`state` enum('inCHECK','APPROVED') NOT NULL default 'inCHECK',"
- + "`value` text NOT NULL,"
- + "`counter` int(10) UNSIGNED NOT NULL,"
- + "PRIMARY KEY (`rid`)"
- + ") ENGINE=MyISAM DEFAULT CHARSET=latin1";
- request.execute(create);
- // SimulationContext types
- create = "CREATE TABLE `contype` ("
- + "`rid` int(10) UNSIGNED NOT NULL auto_increment,"
- + "`name` tinytext NOT NULL,"
- + "`state` enum('inCHECK','APPROVED') NOT NULL default 'inCHECK',"
- + "`step` int(10) NOT NULL," + "PRIMARY KEY (`rid`)"
- + ") ENGINE=MyISAM DEFAULT CHARSET=latin1";
- request.execute(create);
-
- // Many-to-many association between ProjectElement (Study and Scenario) and SimulationContext
- create = "CREATE TABLE `projext` (" + "`owner` int(10) NOT NULL,"
- + "`ordex` int(10) NOT NULL," + "`rid` int(10) NOT NULL"
- + ") ENGINE=MyISAM DEFAULT CHARSET=latin1";
- request.execute(create);
-
- // File objects
- create = "CREATE TABLE `file` ("
- + "`rid` int(10) UNSIGNED NOT NULL,"
- + "`format` tinytext NOT NULL,"
- + "`path` tinytext NOT NULL,"
- + "`date` date NOT NULL," + "PRIMARY KEY (`rid`)"
- + ") ENGINE=MyISAM DEFAULT CHARSET=latin1";
- request.execute(create);
-
- // Reference objects
- create = "CREATE TABLE `refid` (" + "`cycle` int(10) NOT NULL,"
- + "`base` int(10) NOT NULL," + "PRIMARY KEY (`cycle`)"
- + ") ENGINE=MyISAM DEFAULT CHARSET=latin1";
- request.execute(create);
- }
- }
-
+ /**
+ * The job-class for database schema version ckeck.
+ * @author dbc
+ *
+ */
protected class CheckVersion implements Work {
- // --------------------------------------------
+ /**
+ * {@inheritDoc}
+ * @see org.hibernate.jdbc.Work#execute(java.sql.Connection)
+ */
public void execute(Connection connex) throws SQLException {
DatabaseMetaData dbmdata = connex.getMetaData();
String dbname = connex.getCatalog();
private Database() {
}
+ /**
+ * Check version of the database schema.
+ */
private void checkVersion() {
getSessionFactory().getCurrentSession().doWork(new CheckVersion());
}
// Creation of the SIMER SQL tables
Session session = Database.getSession();
- session.doWork(new CreateTables()); // May throw SQLException if the SIMER database does not exist
- session.flush();
// Population of the database with customized data
this.populate();
public void configure(Properties reprop) throws IOException {
// --------------------------------------------
- basepath = reprop.getProperty("repository");
+ String basepath = reprop.getProperty("repository");
getRepositoryService().setBasepath(basepath);
getIndexService().configure();
}
// Public services
// ==============================================================================================================================
- public static File getDownloadDirectory(User user) {
- return my.getRepositoryService().getDownloadDirectory(user);
- }
-
- public static String getTemplatePath() {
- return my.getRepositoryService().getTemplatePath();
- }
-
public static String getRepositoryVaultPath() {
return my.getRepositoryService().getRepositoryVaultPath();
}
- public static Document selectDocument(int index) {
- // -------------------------------------------------
- StringBuffer query = new StringBuffer("from Document where rid='")
- .append(index).append("'");
- return (Document) Database.getSession().createQuery(query.toString())
- .uniqueResult();
- }
-
- public static Document selectDocument(String refid, String version) {
- // --------------------------------------------------------------------
- StringBuffer query = new StringBuffer("from Document where did='")
- .append(refid).append("' and version='").append(version)
- .append("'");
- return (Document) Database.getSession().createQuery(query.toString())
- .uniqueResult();
- }
-
- public static SimulationContext selectSimulationContext(int index) {
- // -------------------------------------------------------------------
- StringBuffer query = new StringBuffer(
- "from SimulationContext where rid='").append(index).append("'");
- return (SimulationContext) Database.getSession().createQuery(
- query.toString()).uniqueResult();
- }
-
- public static SimulationContext selectSimulationContext(
- SimulationContextType celt, String value) {
- // --------------------------------------------------------------------------------------------------
- SimulationContext result = null;
- try {
- SimulationContext.Properties cprop = new SimulationContext.Properties();
- List<SimulationContext> clist = selectSimulationContextsWhere(cprop
- .setType(celt).setValue(value));
- if (!clist.isEmpty())
- result = clist.get(0); // Supposed being the most used one if many exist
- } catch (InvalidPropertyException error) {
- logger.info("Attempt to select a simulation context \""
- + celt.getName() + "\" with an invalid value.");
- }
- return result;
- }
-
- @SuppressWarnings("unchecked")
- public static List<SimulationContext> selectSimulationContextsWhere(
- SimulationContext.Properties cprop) {
- // --------------------------------------------------------------------------------------------------------
- StringBuffer query = new StringBuffer("from SimulationContext");
- String separator = " where";
- SimulationContextType celt = cprop.getType();
- String value = cprop.getValue();
- ProgressState state = cprop.getProgressState();
- String order = "";
-
- if (celt != null) {
- query = query.append(separator).append(" type='").append(
- celt.getIndex()).append("'");
- separator = " and";
- order = " order by value asc";
- }
- if (value != null) {
- query = query.append(separator).append(" value='").append(value)
- .append("'");
- separator = " and";
- }
- if (state != null) {
- query = query.append(separator).append(" state='").append(state)
- .append("'");
- if (celt == null)
- order = " order by type asc";
- }
- query.append(order);
- return (List<SimulationContext>) Database.getSession().createQuery(
- query.toString()).list();
- }
-
- protected static IDBuilder selectIDBuilder(int cycle) {
- // ------------------------------------------------------
- StringBuffer buffer = new StringBuffer("from IDBuilder where cycle='")
- .append(cycle).append("'");
- String qstring = buffer.toString();
- Query query = Database.getSession().createQuery(qstring);
- IDBuilder result = (IDBuilder) query.uniqueResult();
-
- return result;
- }
-
- public static IDBuilder selectIDBuilder(Date date) {
- // ------------------------------------------------------
- SimpleDateFormat year = new SimpleDateFormat("yyyy");
- String cycle = year.format(date);
- StringBuffer buffer = new StringBuffer("from IDBuilder where cycle='")
- .append(cycle).append("'");
- String qstring = buffer.toString();
- Query query = Database.getSession().createQuery(qstring);
- IDBuilder result = (IDBuilder) query.uniqueResult();
-
- return result;
- }
-
/**
* @return
*/