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 reader */
443 + "select rel.owner from reader_rel rel "
444 + "where {alias}.rid = rel.owner AND rel.refer = ?)",
445 actorId, Hibernate.LONG))
447 /* If the user is author */
448 Restrictions.eq("study.manager.rid", actorId)).add(
449 /* If the study is public */
450 Restrictions.eq("study.visibility",
451 Visibility.PUBLIC)));
453 // User is not logged in - show only public studies
454 topJunction.add(Restrictions.eq("study.visibility",
461 * Add search criteria by dates to the junction filter condition.
464 * the junction filter condition
468 * creation date property name
470 private void addCreationDateCriteria(final Junction topJunction,
471 final SearchFilterDTO filter, final String propName) {
472 // Filter by creation date
473 if (filter.getCreatedAfter() != null) {
475 .add(Restrictions.gt(propName, filter.getCreatedAfter()));
477 if (filter.getCreatedBefore() != null) {
478 topJunction.add(Restrictions
479 .lt(propName, filter.getCreatedBefore()));
486 * @see org.splat.service.SearchService#selectStudiesWhere(org.splat.dal.bo.som.Study.Properties[])
489 public List<Proxy> selectStudiesWhere(final Study.Properties... sprop) {
490 List<Proxy> result = new ArrayList<Proxy>();
494 // Creation of the Lucene query
495 File indir = getRepositoryService().getRepositoryIndexDirectory();
496 Directory index = FSDirectory.open(indir);
497 IndexSearcher searcher = new IndexSearcher(index, true);
498 BooleanQuery fulquery = new BooleanQuery();
500 for (int i = 0; i < sprop.length; i++) {
501 BooleanQuery query = new BooleanQuery();
502 Term input; // Supposed initialized below at least by the visibility
504 Visibility area = sprop[i].getVisibility(); // Visibility
506 input = new Term("area");
507 query.add(new TermQuery(input.createTerm(area.toString())),
508 BooleanClause.Occur.MUST);
510 ProgressState state = sprop[i].getProgressState(); // State
512 input = new Term("state");
513 if (state == ProgressState.inPROGRESS) {
514 BooleanQuery cristate = new BooleanQuery();
515 cristate.add(new TermQuery(input.createTerm("inWORK")),
516 BooleanClause.Occur.SHOULD);
518 new TermQuery(input.createTerm("inDRAFT")),
519 BooleanClause.Occur.SHOULD);
521 new TermQuery(input.createTerm("inCHECK")),
522 BooleanClause.Occur.SHOULD);
523 query.add(cristate, BooleanClause.Occur.MUST);
525 query.add(new TermQuery(input.createTerm(state
526 .toString())), BooleanClause.Occur.MUST);
529 String refid = sprop[i].getReference(); // Reference
531 input = new Term("ref");
532 query.add(new TermQuery(input.createTerm(refid)),
533 BooleanClause.Occur.MUST);
535 User manager = sprop[i].getManager(); // Author
536 if (manager != null) {
537 input = new Term("author");
538 query.add(new TermQuery(input
539 .createTerm(manager.toString())),
540 BooleanClause.Occur.MUST);
542 User actor = sprop[i].getActor(); // Contributor, Reviewer or Approver
544 input = new Term("actor");
546 new TermQuery(input.createTerm(actor.toString())),
547 BooleanClause.Occur.MUST);
549 String title = sprop[i].getTitle(); // Title
551 input = new Term("contents");
552 BooleanQuery critext = new BooleanQuery();
553 String operator = "AND"; // Future user input
554 BooleanClause.Occur clause = BooleanClause.Occur.MUST;
555 if (operator.equals("OR")) {
556 clause = BooleanClause.Occur.SHOULD;
558 String[] word = title.split(" ");
559 for (int j = 0; j < word.length; j++) {
560 critext.add(new TermQuery(input.createTerm(word[j])),
563 query.add(critext, BooleanClause.Occur.MUST);
565 List<SimulationContext> context = sprop[i]
566 .getSimulationContexts();
567 if (context != null && context.size() > 0) {
568 BooleanQuery critext = new BooleanQuery();
569 for (Iterator<SimulationContext> j = context.iterator(); j
571 SimulationContext seltext = j.next();
572 input = new Term(String.valueOf(seltext.getType()
574 critext.add(new TermQuery(input.createTerm(seltext
575 .getValue())), BooleanClause.Occur.MUST);
577 query.add(critext, BooleanClause.Occur.MUST);
579 fulquery.add(query, BooleanClause.Occur.SHOULD);
581 if (LOG.isInfoEnabled()) {
582 LOG.info("Searching studies by Lucene query \""
583 + fulquery.toString());
585 // Creation of the studies filter
586 BooleanFilter filter = new BooleanFilter();
587 TermsFilter select = new TermsFilter();
588 Term mytype = new Term("class");
589 select.addTerm(mytype.createTerm("Study"));
590 filter.add(new FilterClause(select, BooleanClause.Occur.SHOULD));
592 // Creation of the sort criteria
593 Sort sort = new Sort(new SortField(PROP_TITLE, SortField.STRING));
596 TopFieldDocs found = searcher.search(fulquery, filter, hitsize,
599 if (found.totalHits < 1) {
600 return result; // No study found
603 // Construction of the result list
604 ScoreDoc[] hits = found.scoreDocs;
605 for (int i = 0; i < hits.length; i++) {
606 result.add(new IndexServiceImpl.ObjectProxy(searcher
610 } catch (Exception error) {
611 LOG.error("Error during Lucene search, reason:", error);
619 * @see org.splat.service.SearchService#indexStudy(org.splat.dal.bo.som.Study)
622 public void indexStudy(final Study study) {
623 LOG.debug("Index study: id=" + study.getRid() + "; reference="
624 + study.getReference());
626 Study.Properties sprop = new Study.Properties();
627 List<Proxy> index = selectStudiesWhere(sprop.setReference(study
630 if (index.size() != 0) {
631 LOG.debug("The given study is already indexed.");
632 return; // The given study is already indexed
635 IndexService lucin = getIndex();
636 Scenario[] scenes = study.getScenarii();
638 LOG.debug("Number of study " + study.getReference() + " actors: "
639 + study.getActor().size());
641 if (study.getProgressState() != ProgressState.inWORK) {
642 for (int i = 0; i < scenes.length; i++) {
643 List<KnowledgeElement> list = scenes[i]
644 .getAllKnowledgeElements();
645 for (Iterator<KnowledgeElement> j = list.iterator(); j
648 LOG.debug("Knowlegge added: id=" + j.next().getIndex());
652 } catch (Exception error) {
653 LOG.error("Unable to index the study '" + study.getIndex()
654 + "', reason:", error);
659 * Get lucene index handler. Create the index if it is not exist.
661 * @return IndexService
662 * @throws IOException
663 * if error when creating a new lucene index
665 private IndexService getIndex() throws IOException {
666 IndexService lucin = getIndexService();
667 if (IndexWriter.isLocked(FSDirectory.open(getRepositoryService()
668 .getRepositoryIndexDirectory()))) {
669 IndexWriter.unlock(FSDirectory.open(getRepositoryService()
670 .getRepositoryIndexDirectory()));
672 if (!lucin.exists()) {
673 lucin.create(); // Happens when re-indexing all studies
679 * Get the repositoryService.
681 * @return the repositoryService
683 public RepositoryService getRepositoryService() {
684 return _repositoryService;
688 * Set the repositoryService.
690 * @param repositoryService
691 * the repositoryService to set
693 public void setRepositoryService(final RepositoryService repositoryService) {
694 _repositoryService = repositoryService;
698 * Get the indexService.
700 * @return the indexService
702 public IndexService getIndexService() {
703 return _indexService;
707 * Set the indexService.
709 * @param indexService
710 * the indexService to set
712 public void setIndexService(final IndexService indexService) {
713 _indexService = indexService;
717 * Get the studyService.
719 * @return the studyService
721 public StudyService getStudyService() {
722 return _studyService;
726 * Set the studyService.
728 * @param studyService
729 * the studyService to set
731 public void setStudyService(final StudyService studyService) {
732 _studyService = studyService;
738 * @return the studyDAO
740 public StudyDAO getStudyDAO() {
748 * the studyDAO to set
750 public void setStudyDAO(final StudyDAO studyDAO) {
751 _studyDAO = studyDAO;
755 * Get the knowledgeElementDAO.
757 * @return the knowledgeElementDAO
759 public KnowledgeElementDAO getKnowledgeElementDAO() {
760 return _knowledgeElementDAO;
764 * Set the knowledgeElementDAO.
766 * @param knowledgeElementDAO
767 * the knowledgeElementDAO to set
769 public void setKnowledgeElementDAO(
770 final KnowledgeElementDAO knowledgeElementDAO) {
771 _knowledgeElementDAO = knowledgeElementDAO;