* @version $Revision$
*****************************************************************************/
-package org.splat.service;
+package org.splat.service;
import java.io.File;
import java.io.IOException;
import org.splat.dal.bo.som.SimulationContext;
import org.splat.dal.bo.som.Study;
import org.splat.dal.bo.som.Visibility;
-import org.splat.dal.dao.som.Database;
import org.splat.service.dto.Proxy;
import org.splat.service.technical.IndexService;
import org.splat.service.technical.IndexServiceImpl;
import org.splat.service.technical.RepositoryService;
+import org.springframework.transaction.annotation.Transactional;
/**
- * @author rkv
- *
+ * Search service implementation.
+ *
+ * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
*/
public class SearchServiceImpl implements SearchService {
- public final static Logger logger = Logger.getLogger(org.splat.service.SearchServiceImpl.class);
-
- private RepositoryService _repositoryService;
+ /**
+ * The service logger.
+ */
+ public final static Logger logger = Logger
+ .getLogger(org.splat.service.SearchServiceImpl.class);
+ /**
+ * Injected repository service.
+ */
+ private RepositoryService _repositoryService;
+ /**
+ * Injected index service.
+ */
private IndexService _indexService;
-
- public List<Proxy> selectKnowledgeElementsWhere (KnowledgeElement.Properties... kprop) {
- // ---------------------------------------------------------------------------------------------
- List<Proxy> result = new ArrayList<Proxy>();
- int hitsize = 20;
- try {
-
- // Creation of the Lucene query
- File indir = getRepositoryService().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);
- }
+
+ /**
+ * Injected study service.
+ */
+ private StudyService _studyService;
+
+ /**
+ * Refresh lucene index for studies.
+ *
+ * @param ridlist
+ * list of studies id's
+ */
+ @Transactional(readOnly = true)
+ public void reindexStudies(String[] ridlist) {
+ for (int i = 0; i < ridlist.length; i++) {
+ long index = Long.valueOf(ridlist[i].trim());
+ Study study = getStudyService().selectStudy(index);
+ indexStudy(study);
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see org.splat.service.SearchService#selectKnowledgeElementsWhere(org.splat.dal.bo.som.KnowledgeElement.Properties[])
+ */
+ public List<Proxy> selectKnowledgeElementsWhere(
+ KnowledgeElement.Properties... kprop) {
+ List<Proxy> result = new ArrayList<Proxy>();
+ int hitsize = 20;
+ try {
+
+ // Creation of the Lucene query
+ File indir = getRepositoryService().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 IndexServiceImpl.ObjectProxy(searcher.doc(hits[i].doc)) );
- }
- searcher.close();
- }
- catch (Exception error) {
- logger.error("Error during Lucene search, reason:", error);
- }
- return result;
+ 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 IndexServiceImpl.ObjectProxy(searcher
+ .doc(hits[i].doc)));
+ }
+ searcher.close();
+ } catch (Exception error) {
+ logger.error("Error during Lucene search, reason:", error);
}
+ return result;
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see org.splat.service.SearchService#selectStudiesWhere(org.splat.dal.bo.som.Study.Properties[])
+ */
+ public List<Proxy> selectStudiesWhere(Study.Properties... sprop) {
+ List<Proxy> result = new ArrayList<Proxy>();
+ int hitsize = 20;
+ try {
- public List<Proxy> selectStudiesWhere (Study.Properties... sprop) {
- // ------------------------------------------------------------------------
- List<Proxy> result = new ArrayList<Proxy>();
- int hitsize = 20;
- try {
-
- // Creation of the Lucene query
- File indir = getRepositoryService().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);
- }
+ // Creation of the Lucene query
+ File indir = getRepositoryService().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 IndexServiceImpl.ObjectProxy(searcher.doc(hits[i].doc)) );
- }
- searcher.close();
- }
- catch (Exception error) {
- logger.error("Error during Lucene search, reason:", error);
- }
- return result;
- }
-
- public void indexStudy (Study study) {
- // -------------------------------------------
- try {
- Study.Properties sprop = new Study.Properties();
- List<Proxy> index = selectStudiesWhere(sprop.setReference(study.getReference()));
-
- if (index.size() != 0) return; // The given study is already indexed
-
- IndexService 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) {
- Database.logger.error("Unable to index the study '" + study.getIndex() + "', reason:", error);
- }
- }
-
- public IndexService getIndex () throws IOException {
+ 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 IndexServiceImpl.ObjectProxy(searcher
+ .doc(hits[i].doc)));
+ }
+ searcher.close();
+ } catch (Exception error) {
+ logger.error("Error during Lucene search, reason:", error);
+ }
+ return result;
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see org.splat.service.SearchService#indexStudy(org.splat.dal.bo.som.Study)
+ */
+ public void indexStudy(Study study) {
+ logger.debug("Index study: id=" + study.getRid() + "; reference=" + study.getReference());
+ try {
+ Study.Properties sprop = new Study.Properties();
+ List<Proxy> index = selectStudiesWhere(sprop.setReference(study
+ .getReference()));
+
+ if (index.size() != 0) {
+ logger.debug("The given study is already indexed.");
+ return; // The given study is already indexed
+ }
+
+ IndexService lucin = getIndex();
+ Scenario[] scenes = study.getScenarii();
+
+ logger.debug("Number of study " + study.getReference() + " actors: " + study.getActor().size());
+ 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());
+ logger.debug("Knowlegge added: id=" + j.next().getIndex());
+ }
+ }
+ } catch (Exception error) {
+ logger.error("Unable to index the study '"
+ + study.getIndex() + "', reason:", error);
+ }
+ }
+
+ /**
+ * Get lucene index handler. Create the index if it is not exist.
+ *
+ * @return IndexService
+ * @throws IOException
+ * if error when creating a new lucene index
+ */
+ private IndexService getIndex() throws IOException {
IndexService lucin = getIndexService();
- if ( !lucin.exists() ) lucin.create(); // Happens when re-indexing all studies
+ if (!lucin.exists())
+ lucin.create(); // Happens when re-indexing all studies
return lucin;
- }
-
+ }
+
/**
* Get the repositoryService.
+ *
* @return the repositoryService
*/
public RepositoryService getRepositoryService() {
/**
* Set the repositoryService.
- * @param repositoryService the repositoryService to set
+ *
+ * @param repositoryService
+ * the repositoryService to set
*/
public void setRepositoryService(RepositoryService repositoryService) {
_repositoryService = repositoryService;
/**
* Get the indexService.
+ *
* @return the indexService
*/
public IndexService getIndexService() {
/**
* Set the indexService.
- * @param indexService the indexService to set
+ *
+ * @param indexService
+ * the indexService to set
*/
public void setIndexService(IndexService indexService) {
_indexService = indexService;
}
+
+ /**
+ * Get the studyService.
+ * @return the studyService
+ */
+ public StudyService getStudyService() {
+ return _studyService;
+ }
+
+ /**
+ * Set the studyService.
+ * @param studyService the studyService to set
+ */
+ public void setStudyService(StudyService studyService) {
+ _studyService = studyService;
+ }
}