1 /*****************************************************************************
5 * Creation date 05.10.2012
8 *****************************************************************************/
10 package org.splat.service;
13 import java.io.IOException;
14 import java.util.ArrayList;
15 import java.util.Iterator;
16 import java.util.List;
18 import org.apache.log4j.Logger;
19 import org.apache.lucene.index.IndexWriter;
20 import org.apache.lucene.index.Term;
21 import org.apache.lucene.search.BooleanClause;
22 import org.apache.lucene.search.BooleanFilter;
23 import org.apache.lucene.search.BooleanQuery;
24 import org.apache.lucene.search.FilterClause;
25 import org.apache.lucene.search.IndexSearcher;
26 import org.apache.lucene.search.ScoreDoc;
27 import org.apache.lucene.search.Sort;
28 import org.apache.lucene.search.SortField;
29 import org.apache.lucene.search.TermQuery;
30 import org.apache.lucene.search.TermsFilter;
31 import org.apache.lucene.search.TopFieldDocs;
32 import org.apache.lucene.store.Directory;
33 import org.apache.lucene.store.FSDirectory;
34 import org.hibernate.Criteria;
35 import org.hibernate.Hibernate;
36 import org.hibernate.criterion.DetachedCriteria;
37 import org.hibernate.criterion.Disjunction;
38 import org.hibernate.criterion.Junction;
39 import org.hibernate.criterion.Order;
40 import org.hibernate.criterion.Restrictions;
41 import org.hibernate.type.Type;
42 import org.splat.dal.bo.kernel.User;
43 import org.splat.dal.bo.som.KnowledgeElement;
44 import org.splat.dal.bo.som.ProgressState;
45 import org.splat.dal.bo.som.Scenario;
46 import org.splat.dal.bo.som.SimulationContext;
47 import org.splat.dal.bo.som.Study;
48 import org.splat.dal.bo.som.Visibility;
49 import org.splat.dal.dao.som.KnowledgeElementDAO;
50 import org.splat.dal.dao.som.StudyDAO;
51 import org.splat.service.dto.ImportedStudyDTO;
52 import org.splat.service.dto.KnowledgeSearchFilterDTO;
53 import org.splat.service.dto.Proxy;
54 import org.splat.service.dto.SearchFilterDTO;
55 import org.splat.service.dto.StudyDTO;
56 import org.splat.service.dto.StudySearchFilterDTO;
57 import org.splat.service.technical.IndexService;
58 import org.splat.service.technical.IndexServiceImpl;
59 import org.splat.service.technical.RepositoryService;
60 import org.splat.util.BeanHelper;
61 import org.springframework.transaction.annotation.Transactional;
64 * Search service implementation.
66 * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
68 public class SearchServiceImpl implements SearchService {
73 public final static Logger LOG = Logger
74 .getLogger(org.splat.service.SearchServiceImpl.class);
76 * "title" property name.
78 private final static String PROP_TITLE = "title";
81 * Injected repository service.
83 private RepositoryService _repositoryService;
85 * Injected index service.
87 private IndexService _indexService;
89 * Injected study service.
91 private StudyService _studyService;
95 private StudyDAO _studyDAO;
97 * Injected knowledge element DAO.
99 private KnowledgeElementDAO _knowledgeElementDAO;
102 * Get a list of studies which are currently not presented in the lucene index.
104 * @return list of ImportedStudy DTO
106 @Transactional(readOnly = true)
107 public List<ImportedStudyDTO> selectStudies() {
108 List<ImportedStudyDTO> table = new ArrayList<ImportedStudyDTO>();
109 Study.Properties sprop = new Study.Properties();
110 List<Study> dbStudies = getStudyDAO().getAll();
112 for (Study aStudy : dbStudies) {
115 if (selectStudiesWhere(
116 sprop.setReference(aStudy.getReference())).size() != 0) {
117 // If this study was already indexed and found in the lucene index
118 // then skip it to avoid adding to the index it again.
121 } catch (Exception error) {
124 // Add the study to the list of studies which are
125 // currently not presented in the lucene index.
126 table.add(BeanHelper.copyBean(aStudy, ImportedStudyDTO.class));
132 * Refresh lucene index for studies.
135 * list of studies id's
137 @Transactional(readOnly = true)
139 public void reindexStudies(final String[] ridlist) {
140 for (int i = 0; i < ridlist.length; i++) {
141 long index = Long.valueOf(ridlist[i].trim());
142 Study study = getStudyService().selectStudy(index);
150 * @see org.splat.service.SearchService#selectKnowledgeElementsWhere(org.splat.service.dto.KnowledgeSearchFilterDTO)
152 public List<Proxy> selectKnowledgeElementsWhere(
153 final KnowledgeSearchFilterDTO filter) {
154 List<Proxy> result = new ArrayList<Proxy>();
156 // Search matching all criteria
157 DetachedCriteria query = DetachedCriteria.forClass(
158 KnowledgeElement.class, "kelm");
159 query.createAlias("kelm.owner", "scen", Criteria.INNER_JOIN)
160 .createCriteria("scen.owner", "study", Criteria.INNER_JOIN)
161 .add(visibleStudyFilter(filter));
163 // Creation of the query
164 Junction topJunction = initQuery(filter);
166 addByWordsCriteria(topJunction, filter);
168 List<SimulationContext> context = filter.getSimContexts();
169 if (context != null && (!context.isEmpty())) {
170 // Get only studies which have given contexts
171 query.createAlias("study.contex", "ctx", Criteria.INNER_JOIN);
173 if (filter.isMatchAllContexts()) { // AND
174 critctx = Restrictions.conjunction();
176 critctx = Restrictions.disjunction();
178 for (SimulationContext seltext : context) {
179 // (simctxType = seltext.getType() AND simctxValue = seltext.getValue())
180 critctx.add(Restrictions.and(Restrictions.eq("ctx.value",
181 seltext.getValue()), Restrictions.eq("ctx.type",
182 seltext.getType())));
185 topJunction.add(critctx);
188 query.add(topJunction);
190 query.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
191 // Creation of the sort criteria
192 query.addOrder(Order.asc(PROP_TITLE));
194 if (LOG.isInfoEnabled()) {
195 LOG.info("Searching knowledge elements: \"" + query.toString());
199 List<KnowledgeElement> found = getKnowledgeElementDAO()
200 .getFilteredList(query);
202 // Construction of the result list
203 for (KnowledgeElement kelm : found) {
204 result.add(new StudyDTO(kelm.getIndex(), kelm.getReference(), kelm
205 .getProgressState(), kelm.getTitle(), kelm.getAuthor()
212 * Add search criteria for filtering by title contents.
215 * the search condition to be appended
219 private void addByWordsCriteria(final Junction topJunction,
220 final SearchFilterDTO filter) {
221 String title = filter.getWords(); // Title
222 if (title != null && (!title.isEmpty())) {
223 // Look for given words in titles
225 if (filter.isMatchAllCriteria()) { // AND
226 critext = Restrictions.conjunction();
228 critext = Restrictions.disjunction();
231 String[] word = title.split(" ");
232 for (int j = 0; j < word.length; j++) {
233 critext.add(Restrictions.like(PROP_TITLE, "%" + word[j] + "%"));
235 topJunction.add(critext);
242 * @see org.splat.service.SearchService#selectStudiesWhere(org.splat.service.dto.StudySearchFilterDTO)
244 public List<Proxy> selectStudiesWhere(final StudySearchFilterDTO filter) {
245 List<Proxy> result = new ArrayList<Proxy>();
247 DetachedCriteria query = DetachedCriteria
248 .forClass(Study.class, "study").add(visibleStudyFilter(filter));
250 // Creation of the query
251 Junction topJunction = initQuery(filter);
253 addByWordsCriteria(topJunction, filter);
255 List<SimulationContext> context = filter.getSimContexts();
256 if (context != null && (!context.isEmpty())) {
257 // Get only studies which have given contexts
258 query.createAlias("contex", "ctx", Criteria.INNER_JOIN);
260 if (filter.isMatchAllContexts()) { // AND
261 critctx = Restrictions.conjunction();
263 critctx = Restrictions.disjunction();
265 for (SimulationContext seltext : context) {
266 // (simctxType = seltext.getType() AND simctxValue = seltext.getValue())
267 critctx.add(Restrictions.and(Restrictions.eq("ctx.value",
268 seltext.getValue()), Restrictions.eq("ctx.type",
269 seltext.getType())));
272 topJunction.add(critctx);
275 query.add(topJunction);
277 query.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
278 // Creation of the sort criteria
279 query.addOrder(Order.asc(PROP_TITLE));
281 if (LOG.isInfoEnabled()) {
282 LOG.info("Searching studies: \"" + query.toString());
286 List<Study> found = getStudyDAO().getFilteredList(query);
288 // Construction of the result list
289 for (Study std : found) {
290 result.add(new StudyDTO(std.getIndex(), std.getReference(), std
291 .getProgressState(), std.getTitle(), std.getAuthor()
298 * Initialize query with base criteria.
302 * @return top junction of the search filter
304 private Junction initQuery(final StudySearchFilterDTO filter) {
305 Junction topJunction;
306 if (filter.isMatchAllCriteria()) { // AND
307 topJunction = Restrictions.conjunction();
309 topJunction = Restrictions.disjunction();
311 if (!SearchFilterDTO.ANY_STATE.equals(filter.getState())) {
312 ProgressState state = ProgressState.valueOf(filter.getState()); // State
314 topJunction.add(Restrictions.eq("state", state));
317 String refid = filter.getReference(); // Reference
318 if (refid != null && !refid.isEmpty()) {
319 topJunction.add(Restrictions.eq("sid", refid));
322 // Filter by creation date
323 addCreationDateCriteria(topJunction, filter, "credate");
324 // Filter by modification date
325 if (filter.getUpdatedAfter() != null) {
326 topJunction.add(Restrictions
327 .gt("lasdate", filter.getUpdatedAfter()));
329 if (filter.getUpdatedBefore() != null) {
330 topJunction.add(Restrictions.lt("lasdate", filter
331 .getUpdatedBefore()));
334 // Filter by study author
335 long authorId = Long.valueOf(filter.getAuthor());
337 topJunction.add(Restrictions.eq("manager.rid", authorId));
344 * Initialize query with base criteria.
348 * @return top junction of the search filter
350 private Junction initQuery(final KnowledgeSearchFilterDTO filter) {
351 Junction topJunction;
352 if (filter.isMatchAllCriteria()) { // AND
353 topJunction = Restrictions.conjunction();
355 topJunction = Restrictions.disjunction();
358 // Filter by knowledge type
359 long ktypeId = Long.valueOf(filter.getKtype());
361 topJunction.add(Restrictions.eq("kelm.type.rid", ktypeId));
364 if (!SearchFilterDTO.ANY_STATE.equals(filter.getState())) {
365 ProgressState state = ProgressState.valueOf(filter.getState()); // State
367 topJunction.add(Restrictions.eq("kelm.state", state));
371 String refid = filter.getReference(); // Reference
372 if (refid != null && !refid.isEmpty()) {
373 long id = Long.valueOf(refid.replaceAll("^KE(0)*", ""));
375 topJunction.add(Restrictions.eq("kelm.rid", id));
379 addCreationDateCriteria(topJunction, filter, "date");
381 // Filter by knowledge author
382 long authorId = Long.valueOf(filter.getAuthor());
384 topJunction.add(Restrictions.eq("kelm.author.rid", authorId));
391 * Filter for visible studies.
395 * @return search condition for studies to get only visible studies
397 private Junction visibleStudyFilter(final SearchFilterDTO filter) {
398 Junction topJunction;
399 if (filter.isMatchAllCriteria()) { // AND
400 topJunction = Restrictions.conjunction();
402 topJunction = Restrictions.disjunction();
405 // Filter by connected user
406 long actorId = filter.getConnectedUserId(); // Contributor, Reviewer or Approver
408 // User is loggen in - show public studies and studies where he is participating
409 Disjunction orCrit = Restrictions.disjunction();
413 /* If the user is a validation cycle participant */
417 + "select vcrel.owner from cycle_rel vcrel "
418 + "inner join cycle vc on vcrel.refer = vc.rid "
419 + "where {alias}.rid = vcrel.owner "
420 + "AND (vc.publisher = ? OR vc.reviewer = ? OR vc.approver = ? OR vc.signatory = ?) "
421 + "group by vcrel.owner)",
422 new Object[] { actorId,
431 /* If the user is contributor */
435 + "select rel.owner from contributor_rel rel "
436 + "where {alias}.rid = rel.owner AND rel.refer = ?)",
437 actorId, Hibernate.LONG))
439 /* If the user is author */
440 Restrictions.eq("study.manager.rid", actorId)).add(
441 /* If the study is public */
442 Restrictions.eq("study.visibility",
443 Visibility.PUBLIC)));
445 // User is not logged in - show only public studies
446 topJunction.add(Restrictions.eq("study.visibility",
453 * Add search criteria by dates to the junction filter condition.
456 * the junction filter condition
460 * creation date property name
462 private void addCreationDateCriteria(final Junction topJunction,
463 final SearchFilterDTO filter, final String propName) {
464 // Filter by creation date
465 if (filter.getCreatedAfter() != null) {
467 .add(Restrictions.gt(propName, filter.getCreatedAfter()));
469 if (filter.getCreatedBefore() != null) {
470 topJunction.add(Restrictions
471 .lt(propName, filter.getCreatedBefore()));
478 * @see org.splat.service.SearchService#selectStudiesWhere(org.splat.dal.bo.som.Study.Properties[])
481 public List<Proxy> selectStudiesWhere(final Study.Properties... sprop) {
482 List<Proxy> result = new ArrayList<Proxy>();
486 // Creation of the Lucene query
487 File indir = getRepositoryService().getRepositoryIndexDirectory();
488 Directory index = FSDirectory.open(indir);
489 IndexSearcher searcher = new IndexSearcher(index, true);
490 BooleanQuery fulquery = new BooleanQuery();
492 for (int i = 0; i < sprop.length; i++) {
493 BooleanQuery query = new BooleanQuery();
494 Term input; // Supposed initialized below at least by the visibility
496 Visibility area = sprop[i].getVisibility(); // Visibility
498 input = new Term("area");
499 query.add(new TermQuery(input.createTerm(area.toString())),
500 BooleanClause.Occur.MUST);
502 ProgressState state = sprop[i].getProgressState(); // State
504 input = new Term("state");
505 if (state == ProgressState.inPROGRESS) {
506 BooleanQuery cristate = new BooleanQuery();
507 cristate.add(new TermQuery(input.createTerm("inWORK")),
508 BooleanClause.Occur.SHOULD);
510 new TermQuery(input.createTerm("inDRAFT")),
511 BooleanClause.Occur.SHOULD);
513 new TermQuery(input.createTerm("inCHECK")),
514 BooleanClause.Occur.SHOULD);
515 query.add(cristate, BooleanClause.Occur.MUST);
517 query.add(new TermQuery(input.createTerm(state
518 .toString())), BooleanClause.Occur.MUST);
521 String refid = sprop[i].getReference(); // Reference
523 input = new Term("ref");
524 query.add(new TermQuery(input.createTerm(refid)),
525 BooleanClause.Occur.MUST);
527 User manager = sprop[i].getManager(); // Author
528 if (manager != null) {
529 input = new Term("author");
530 query.add(new TermQuery(input
531 .createTerm(manager.toString())),
532 BooleanClause.Occur.MUST);
534 User actor = sprop[i].getActor(); // Contributor, Reviewer or Approver
536 input = new Term("actor");
538 new TermQuery(input.createTerm(actor.toString())),
539 BooleanClause.Occur.MUST);
541 String title = sprop[i].getTitle(); // Title
543 input = new Term("contents");
544 BooleanQuery critext = new BooleanQuery();
545 String operator = "AND"; // Future user input
546 BooleanClause.Occur clause = BooleanClause.Occur.MUST;
547 if (operator.equals("OR")) {
548 clause = BooleanClause.Occur.SHOULD;
550 String[] word = title.split(" ");
551 for (int j = 0; j < word.length; j++) {
552 critext.add(new TermQuery(input.createTerm(word[j])),
555 query.add(critext, BooleanClause.Occur.MUST);
557 List<SimulationContext> context = sprop[i]
558 .getSimulationContexts();
559 if (context != null && context.size() > 0) {
560 BooleanQuery critext = new BooleanQuery();
561 for (Iterator<SimulationContext> j = context.iterator(); j
563 SimulationContext seltext = j.next();
564 input = new Term(String.valueOf(seltext.getType()
566 critext.add(new TermQuery(input.createTerm(seltext
567 .getValue())), BooleanClause.Occur.MUST);
569 query.add(critext, BooleanClause.Occur.MUST);
571 fulquery.add(query, BooleanClause.Occur.SHOULD);
573 if (LOG.isInfoEnabled()) {
574 LOG.info("Searching studies by Lucene query \""
575 + fulquery.toString());
577 // Creation of the studies filter
578 BooleanFilter filter = new BooleanFilter();
579 TermsFilter select = new TermsFilter();
580 Term mytype = new Term("class");
581 select.addTerm(mytype.createTerm("Study"));
582 filter.add(new FilterClause(select, BooleanClause.Occur.SHOULD));
584 // Creation of the sort criteria
585 Sort sort = new Sort(new SortField(PROP_TITLE, SortField.STRING));
588 TopFieldDocs found = searcher.search(fulquery, filter, hitsize,
591 if (found.totalHits < 1) {
592 return result; // No study found
595 // Construction of the result list
596 ScoreDoc[] hits = found.scoreDocs;
597 for (int i = 0; i < hits.length; i++) {
598 result.add(new IndexServiceImpl.ObjectProxy(searcher
602 } catch (Exception error) {
603 LOG.error("Error during Lucene search, reason:", error);
611 * @see org.splat.service.SearchService#indexStudy(org.splat.dal.bo.som.Study)
614 public void indexStudy(final Study study) {
615 LOG.debug("Index study: id=" + study.getRid() + "; reference="
616 + study.getReference());
618 Study.Properties sprop = new Study.Properties();
619 List<Proxy> index = selectStudiesWhere(sprop.setReference(study
622 if (index.size() != 0) {
623 LOG.debug("The given study is already indexed.");
624 return; // The given study is already indexed
627 IndexService lucin = getIndex();
628 Scenario[] scenes = study.getScenarii();
630 LOG.debug("Number of study " + study.getReference() + " actors: "
631 + study.getActor().size());
633 if (study.getProgressState() != ProgressState.inWORK) {
634 for (int i = 0; i < scenes.length; i++) {
635 List<KnowledgeElement> list = scenes[i]
636 .getAllKnowledgeElements();
637 for (Iterator<KnowledgeElement> j = list.iterator(); j
640 LOG.debug("Knowlegge added: id=" + j.next().getIndex());
644 } catch (Exception error) {
645 LOG.error("Unable to index the study '" + study.getIndex()
646 + "', reason:", error);
651 * Get lucene index handler. Create the index if it is not exist.
653 * @return IndexService
654 * @throws IOException
655 * if error when creating a new lucene index
657 private IndexService getIndex() throws IOException {
658 IndexService lucin = getIndexService();
659 if (IndexWriter.isLocked(FSDirectory.open(getRepositoryService()
660 .getRepositoryIndexDirectory()))) {
661 IndexWriter.unlock(FSDirectory.open(getRepositoryService()
662 .getRepositoryIndexDirectory()));
664 if (!lucin.exists()) {
665 lucin.create(); // Happens when re-indexing all studies
671 * Get the repositoryService.
673 * @return the repositoryService
675 public RepositoryService getRepositoryService() {
676 return _repositoryService;
680 * Set the repositoryService.
682 * @param repositoryService
683 * the repositoryService to set
685 public void setRepositoryService(final RepositoryService repositoryService) {
686 _repositoryService = repositoryService;
690 * Get the indexService.
692 * @return the indexService
694 public IndexService getIndexService() {
695 return _indexService;
699 * Set the indexService.
701 * @param indexService
702 * the indexService to set
704 public void setIndexService(final IndexService indexService) {
705 _indexService = indexService;
709 * Get the studyService.
711 * @return the studyService
713 public StudyService getStudyService() {
714 return _studyService;
718 * Set the studyService.
720 * @param studyService
721 * the studyService to set
723 public void setStudyService(final StudyService studyService) {
724 _studyService = studyService;
730 * @return the studyDAO
732 public StudyDAO getStudyDAO() {
740 * the studyDAO to set
742 public void setStudyDAO(final StudyDAO studyDAO) {
743 _studyDAO = studyDAO;
747 * Get the knowledgeElementDAO.
749 * @return the knowledgeElementDAO
751 public KnowledgeElementDAO getKnowledgeElementDAO() {
752 return _knowledgeElementDAO;
756 * Set the knowledgeElementDAO.
758 * @param knowledgeElementDAO
759 * the knowledgeElementDAO to set
761 public void setKnowledgeElementDAO(
762 final KnowledgeElementDAO knowledgeElementDAO) {
763 _knowledgeElementDAO = knowledgeElementDAO;