-package org.splat.som;
-/**
- *
- * @author Daniel Brunier-Coulin
- * @copyright OPEN CASCADE 2012
- */
-
-import java.text.SimpleDateFormat;
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Properties;
-import java.io.File;
-import java.io.IOException;
-import java.sql.Connection;
-import java.sql.DatabaseMetaData;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-
-import org.hibernate.Query;
-import org.hibernate.Session;
-import org.hibernate.jdbc.Work;
-import org.apache.log4j.Logger;
-import org.apache.lucene.index.Term;
-import org.apache.lucene.search.BooleanClause;
-import org.apache.lucene.search.BooleanFilter;
-import org.apache.lucene.search.BooleanQuery;
-import org.apache.lucene.search.FilterClause;
-import org.apache.lucene.search.IndexSearcher;
-import org.apache.lucene.search.ScoreDoc;
-import org.apache.lucene.search.Sort;
-import org.apache.lucene.search.SortField;
-import org.apache.lucene.search.TermQuery;
-import org.apache.lucene.search.TermsFilter;
-import org.apache.lucene.search.TopFieldDocs;
-import org.apache.lucene.store.Directory;
-import org.apache.lucene.store.FSDirectory;
-
-import org.splat.kernel.User;
-import org.splat.kernel.UserDirectory;
-import org.splat.kernel.MultiplyDefinedException;
-import org.splat.kernel.InvalidPropertyException;
-import org.splat.kernel.MissedPropertyException;
-
-
-public class Database extends org.splat.kernel.Database {
-
- private int uplevel = 0; // Level of database upgrade
- private String basepath = null; // Path of the root directory of repository
-
- private static Database my = null; // Singleton instance
-
- protected class CreateTables extends org.splat.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);
- }
- }
- protected class CheckVersion implements Work {
-// --------------------------------------------
- public void execute(Connection connex) throws SQLException
- {
- DatabaseMetaData dbmdata = connex.getMetaData();
- String dbname = "simer"; //TODO: Get the name from meta-data
- ResultSet table;
-
- table = dbmdata.getTables(dbname, null, "study", null);
- if (table.next()) return;
- uplevel = -1; // Database not initialized
- }
- }
-
- protected final static Logger logger = org.splat.kernel.Database.logger;
-
-// ==============================================================================================================================
-// Construction
-// ==============================================================================================================================
-
- public static Database getMe () {
-// -------------------------------
- if (my == null) try {
- my = new Database();
- }
- catch (Exception error) {
- logger.fatal("Could not access the database, reason:", error);
- }
- return my;
- }
- private Database () {
-// -------------------
- Database.getSession().doWork(new CheckVersion());
- this.setIDPoolSize(4); // Average number of generated IDs when creating a study and versioning a document
- }
-
-// ==============================================================================================================================
-// Public member functions
-// ==============================================================================================================================
-
- public boolean isInitialized () {
-// -------------------------------
- return (uplevel >= 0);
- }
-
- public void initialize () throws IOException, SQLException {
-// -------------------------
- logger.info("Creation of the database.");
-
-// Creation of the Lucene index
- Index.create(); // May throw IOException if the index repository is improperly configured
-
-// 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();
-
- session.flush();
- uplevel = 0; // The database is now up-to-date
- }
-
-// ==============================================================================================================================
-// Protected member functions
-// ==============================================================================================================================
-
- protected void configure (Properties reprop) {
-// --------------------------------------------
- basepath = reprop.getProperty("repository");
- }
-
- protected void populate () {
-// --------------------------
- try {
-// Initialization of the schema version
- this.setSchemaVersion("D0.3"); //TODO: Get the version name from the configuration file
-
-// Creation of the default system administrator
-//TODO: Get the username password from the Hibernate configuration
- User.Properties uprop = new User.Properties();
- uprop.setUsername("simer")
- .setPassword("admin")
- .setName("Simulation")
- .setFirstName("Manager")
- .setDisplayName("label.sysadmin")
- .addRole("sysadmin")
- .setMailAddress("noreply@salome-platform.org");
- uprop.disableCheck();
- UserDirectory.createUser(uprop);
- }
- catch (Exception e) {
-// Let's continue, hoping the best...
- }
- ProjectSettings.getMe().initialize(); // Populates the database with all necessary stuff
- }
-
-// ==============================================================================================================================
-// Public services
-// ==============================================================================================================================
-
- public static Study createStudy (Study.Properties sprop) throws MissedPropertyException, InvalidPropertyException, MultiplyDefinedException, RuntimeException {
-// --------------------------------------------------------
- Study study = new Study(sprop);
-
- study.buildReference();
- Database.getSession().save(study);
- try {
- Index lucin = getIndex();
- lucin.add(study);
- }
- catch (IOException error) {
- logger.error("Unable to index the study '" + study.getIndex() + "', reason:", error);
-// Continue and try to index later
- }
- return study;
- }
-
- public static void indexStudy (Study study) {
-// -------------------------------------------
- try {
- Study.Properties sprop = new Study.Properties();
- List<Proxy> index = Database.selectStudiesWhere(sprop.setReference(study.getReference()));
-
- if (index.size() != 0) return; // The given study is already indexed
-
- Index lucin = getIndex();
- Scenario[] scenes = study.getScenarii();
-
- lucin.add(study);
- if (study.getProgressState() != ProgressState.inWORK) for (int i=0; i<scenes.length; i++) {
- List<KnowledgeElement> list = scenes[i].getAllKnowledgeElements();
- for (Iterator<KnowledgeElement> j=list.iterator(); j.hasNext(); ) {
- lucin.add(j.next());
- }
- }
- }
- catch (Exception error) {
- logger.error("Unable to index the study '" + study.getIndex() + "', reason:", error);
- }
- }
-
- public static Index getIndex () throws IOException {
-// -------------------------------
- Index lucin = new Index();
- if ( !lucin.exists() ) Index.create(); // Happens when re-indexing all studies
- return lucin;
- }
-
- public static File getDownloadDirectory (User user) {
-// ---------------------------------------------------
- StringBuffer path = new StringBuffer(my.basepath).append("downloads/").append(user.getUsername()).append("/");
- return new File(path.toString());
- }
-
- public static File getRepositoryIndexDirectory () {
-// -------------------------------------------------
- return new File(my.basepath + "lucin/");
- }
-
- public static String getRepositoryVaultPath () {
-// --------------------------------------------
- return (my.basepath + "vault/");
- }
-
- public static String getTemplatePath () {
-// ---------------------------------------
- return (my.basepath + "templates/");
- }
-
- 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 KnowledgeElement selectKnowledgeElement (int index) {
-// -----------------------------------------------------------------
- StringBuffer query = new StringBuffer("from KnowledgeElement where rid='").append(index).append("'");
- KnowledgeElement result = (KnowledgeElement)Database.getSession().createQuery(query.toString()).uniqueResult();
-
- result.getOwnerScenario().getOwnerStudy().loadWorkflow();
- return result;
- }
-
- public static List<Proxy> selectKnowledgeElementsWhere (KnowledgeElement.Properties... kprop) {
-// ---------------------------------------------------------------------------------------------
- List<Proxy> result = new ArrayList<Proxy>();
- int hitsize = 20;
- try {
-
-// Creation of the Lucene query
- File indir = Database.getRepositoryIndexDirectory();
- Directory index = FSDirectory.open(indir);
- IndexSearcher searcher = new IndexSearcher(index, true);
- BooleanQuery fulquery = new BooleanQuery();
-
- for (int i=0; i<kprop.length; i++) {
- BooleanQuery query = new BooleanQuery();
- Term input; // Supposed initialized below at least by the visibility
-
- Visibility area = kprop[i].getVisibility(); // Visibility
- if (area != null) {
- input = new Term("area");
- query.add(new TermQuery(input.createTerm(area.toString())), BooleanClause.Occur.MUST);
- }
- ProgressState state = kprop[i].getProgressState(); // State
- if (state != null) {
- input = new Term("state");
- query.add(new TermQuery(input.createTerm(state.toString())), BooleanClause.Occur.MUST);
- }
- String refid = kprop[i].getReference(); // Reference
- if (refid != null) {
- input = new Term("ref");
- query.add(new TermQuery(input.createTerm(refid)), BooleanClause.Occur.MUST);
- }
- KnowledgeElementType type = kprop[i].getType(); // Type
- if (type != null) {
- input = new Term("type");
- query.add(new TermQuery(input.createTerm(type.getName())), BooleanClause.Occur.MUST);
- }
- User manager = kprop[i].getAuthor(); // Author
- if (manager != null) {
- input = new Term("author");
- query.add(new TermQuery(input.createTerm(manager.toString())), BooleanClause.Occur.MUST);
- }
- User actor = kprop[i].getActor(); // Contributor, Reviewer or Approver of the owner study
- if (actor != null) {
- input = new Term("actor");
- query.add(new TermQuery(input.createTerm(actor.toString())), BooleanClause.Occur.MUST);
- }
- String title = kprop[i].getTitle(); // Title
- if (title != null) {
- input = new Term("contents");
- BooleanQuery critext = new BooleanQuery();
- String operator = "AND"; // Future user input
- BooleanClause.Occur clause = BooleanClause.Occur.MUST;
- if (operator.equals("OR")) clause = BooleanClause.Occur.SHOULD;
- String[] word = title.split(" ");
- for (int j=0; j<word.length; j++) {
- critext.add(new TermQuery(input.createTerm(word[j])), clause);
- }
- query.add(critext, BooleanClause.Occur.MUST);
- }
- List<SimulationContext> context = kprop[i].getSimulationContexts();
- if (context != null && context.size() > 0) {
- BooleanQuery critext = new BooleanQuery();
- for (Iterator<SimulationContext> j=context.iterator(); j.hasNext();) {
- SimulationContext seltext = j.next();
- input = new Term(String.valueOf(seltext.getType().getIndex()));
- critext.add(new TermQuery(input.createTerm(seltext.getValue())), BooleanClause.Occur.MUST);
- }
- query.add(critext, BooleanClause.Occur.MUST);
- }
- fulquery.add(query, BooleanClause.Occur.SHOULD);
- }
- if (logger.isInfoEnabled()) {
- logger.info("Searching knowledges by Lucene query \"" + fulquery.toString() + "\".");
- }
-// Creation of the knowledge filter
- BooleanFilter filter = new BooleanFilter();
- TermsFilter select = new TermsFilter();
- Term mytype = new Term("class");
- select.addTerm( mytype.createTerm("KnowledgeElement") );
- filter.add(new FilterClause(select, BooleanClause.Occur.SHOULD));
-
-// Creation of the sort criteria
- Sort sort = new Sort(new SortField("title", SortField.STRING));
-
-// Search
- TopFieldDocs found = searcher.search(fulquery, filter, hitsize, sort);
-
- if (found.totalHits < 1) return result; // No study found
-
-// Construction of the result list
- ScoreDoc[] hits = found.scoreDocs;
- for (int i=0; i<hits.length; i++) {
- result.add( new Index.ObjectProxy(searcher.doc(hits[i].doc)) );
- }
- searcher.close();
- }
- catch (Exception error) {
- logger.error("Error during Lucene search, reason:", error);
- }
- return result;
- }
-
- 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();
- }
-
- public static Study selectStudy (int index) {
-// -------------------------------------------
- StringBuffer query = new StringBuffer("from Study where rid='").append(index).append("'");
- Study result = (Study)Database.getSession().createQuery(query.toString()).uniqueResult();
-
- result.loadWorkflow();
- return result;
- }
-
- public static Study selectStudy (String refid) {
-// ----------------------------------------------
- StringBuffer query = new StringBuffer("from Study where sid='").append(refid).append("'");
- Study result = (Study)Database.getSession().createQuery(query.toString()).uniqueResult();
-
- result.loadWorkflow();
- return result;
- }
-
- public static List<Proxy> selectStudiesWhere (Study.Properties... sprop) {
-// ------------------------------------------------------------------------
- List<Proxy> result = new ArrayList<Proxy>();
- int hitsize = 20;
- try {
-
-// Creation of the Lucene query
- File indir = Database.getRepositoryIndexDirectory();
- Directory index = FSDirectory.open(indir);
- IndexSearcher searcher = new IndexSearcher(index, true);
- BooleanQuery fulquery = new BooleanQuery();
-
- for (int i=0; i<sprop.length; i++) {
- BooleanQuery query = new BooleanQuery();
- Term input; // Supposed initialized below at least by the visibility
-
- Visibility area = sprop[i].getVisibility(); // Visibility
- if (area != null) {
- input = new Term("area");
- query.add(new TermQuery(input.createTerm(area.toString())), BooleanClause.Occur.MUST);
- }
- ProgressState state = sprop[i].getProgressState(); // State
- if (state != null) {
- input = new Term("state");
- if (state == ProgressState.inPROGRESS) {
- BooleanQuery cristate = new BooleanQuery();
- cristate.add(new TermQuery(input.createTerm("inWORK")), BooleanClause.Occur.SHOULD);
- cristate.add(new TermQuery(input.createTerm("inDRAFT")), BooleanClause.Occur.SHOULD);
- cristate.add(new TermQuery(input.createTerm("inCHECK")), BooleanClause.Occur.SHOULD);
- query.add(cristate, BooleanClause.Occur.MUST);
- } else {
- query.add(new TermQuery(input.createTerm(state.toString())), BooleanClause.Occur.MUST);
- }
- }
- String refid = sprop[i].getReference(); // Reference
- if (refid != null) {
- input = new Term("ref");
- query.add(new TermQuery(input.createTerm(refid)), BooleanClause.Occur.MUST);
- }
- User manager = sprop[i].getManager(); // Author
- if (manager != null) {
- input = new Term("author");
- query.add(new TermQuery(input.createTerm(manager.toString())), BooleanClause.Occur.MUST);
- }
- User actor = sprop[i].getActor(); // Contributor, Reviewer or Approver
- if (actor != null) {
- input = new Term("actor");
- query.add(new TermQuery(input.createTerm(actor.toString())), BooleanClause.Occur.MUST);
- }
- String title = sprop[i].getTitle(); // Title
- if (title != null) {
- input = new Term("contents");
- BooleanQuery critext = new BooleanQuery();
- String operator = "AND"; // Future user input
- BooleanClause.Occur clause = BooleanClause.Occur.MUST;
- if (operator.equals("OR")) clause = BooleanClause.Occur.SHOULD;
- String[] word = title.split(" ");
- for (int j=0; j<word.length; j++) {
- critext.add(new TermQuery(input.createTerm(word[j])), clause);
- }
- query.add(critext, BooleanClause.Occur.MUST);
- }
- List<SimulationContext> context = sprop[i].getSimulationContexts();
- if (context != null && context.size() > 0) {
- BooleanQuery critext = new BooleanQuery();
- for (Iterator<SimulationContext> j=context.iterator(); j.hasNext();) {
- SimulationContext seltext = j.next();
- input = new Term(String.valueOf(seltext.getType().getIndex()));
- critext.add(new TermQuery(input.createTerm(seltext.getValue())), BooleanClause.Occur.MUST);
- }
- query.add(critext, BooleanClause.Occur.MUST);
- }
- fulquery.add(query, BooleanClause.Occur.SHOULD);
- }
- if (logger.isInfoEnabled()) {
- logger.info("Searching studies by Lucene query \"" + fulquery.toString() + "\".");
- }
-// Creation of the studies filter
- BooleanFilter filter = new BooleanFilter();
- TermsFilter select = new TermsFilter();
- Term mytype = new Term("class");
- select.addTerm( mytype.createTerm("Study") );
- filter.add(new FilterClause(select, BooleanClause.Occur.SHOULD));
-
-// Creation of the sort criteria
- Sort sort = new Sort(new SortField("title", SortField.STRING));
-
-// Search
- TopFieldDocs found = searcher.search(fulquery, filter, hitsize, sort);
-
- if (found.totalHits < 1) return result; // No study found
-
-// Construction of the result list
- ScoreDoc[] hits = found.scoreDocs;
- for (int i=0; i<hits.length; i++) {
- result.add( new Index.ObjectProxy(searcher.doc(hits[i].doc)) );
- }
- searcher.close();
- }
- catch (Exception error) {
- logger.error("Error during Lucene search, reason:", error);
- }
- return result;
- }
-
-// ==============================================================================================================================
-// Protected services
-// ==============================================================================================================================
-
- 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;
- }
-
- protected 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;
- }
-}
\ No newline at end of file