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.Term;
20 import org.apache.lucene.search.BooleanClause;
21 import org.apache.lucene.search.BooleanFilter;
22 import org.apache.lucene.search.BooleanQuery;
23 import org.apache.lucene.search.FilterClause;
24 import org.apache.lucene.search.IndexSearcher;
25 import org.apache.lucene.search.ScoreDoc;
26 import org.apache.lucene.search.Sort;
27 import org.apache.lucene.search.SortField;
28 import org.apache.lucene.search.TermQuery;
29 import org.apache.lucene.search.TermsFilter;
30 import org.apache.lucene.search.TopFieldDocs;
31 import org.apache.lucene.store.Directory;
32 import org.apache.lucene.store.FSDirectory;
33 import org.hibernate.Criteria;
34 import org.hibernate.Hibernate;
35 import org.hibernate.criterion.DetachedCriteria;
36 import org.hibernate.criterion.Disjunction;
37 import org.hibernate.criterion.Junction;
38 import org.hibernate.criterion.Order;
39 import org.hibernate.criterion.Restrictions;
40 import org.hibernate.type.Type;
41 import org.splat.dal.bo.kernel.User;
42 import org.splat.dal.bo.som.KnowledgeElement;
43 import org.splat.dal.bo.som.KnowledgeElementType;
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.bo.som.Study.Properties;
50 import org.splat.dal.dao.som.StudyDAO;
51 import org.splat.service.dto.ImportedStudyDTO;
52 import org.splat.service.dto.Proxy;
53 import org.splat.service.dto.StudyDTO;
54 import org.splat.service.technical.IndexService;
55 import org.splat.service.technical.IndexServiceImpl;
56 import org.splat.service.technical.RepositoryService;
57 import org.splat.util.BeanHelper;
58 import org.springframework.transaction.annotation.Transactional;
61 * Search service implementation.
63 * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
65 public class SearchServiceImpl implements SearchService {
70 public final static Logger LOG = Logger
71 .getLogger(org.splat.service.SearchServiceImpl.class);
74 * Injected repository service.
76 private RepositoryService _repositoryService;
78 * Injected index service.
80 private IndexService _indexService;
82 * Injected study service.
84 private StudyService _studyService;
88 private StudyDAO _studyDAO;
91 * Get a list of studies which are currently not presented in the lucene index.
93 * @return list of ImportedStudy DTO
95 @Transactional(readOnly = true)
96 public List<ImportedStudyDTO> selectStudies() {
97 List<ImportedStudyDTO> table = new ArrayList<ImportedStudyDTO>();
98 Study.Properties sprop = new Study.Properties();
99 List<Study> dbStudies = getStudyDAO().getAll();
101 for (Study aStudy : dbStudies) {
104 if (selectStudiesWhere(
105 sprop.setReference(aStudy.getReference())).size() != 0) {
106 // If this study was already indexed and found in the lucene index
107 // then skip it to avoid adding to the index it again.
110 } catch (Exception error) {
113 // Add the study to the list of studies which are
114 // currently not presented in the lucene index.
115 table.add(BeanHelper.copyBean(aStudy, ImportedStudyDTO.class));
121 * Refresh lucene index for studies.
124 * list of studies id's
126 @Transactional(readOnly = true)
128 public void reindexStudies(final String[] ridlist) {
129 for (int i = 0; i < ridlist.length; i++) {
130 long index = Long.valueOf(ridlist[i].trim());
131 Study study = getStudyService().selectStudy(index);
139 * @see org.splat.service.SearchService#selectKnowledgeElementsWhere(org.splat.dal.bo.som.KnowledgeElement.Properties[])
141 public List<Proxy> selectKnowledgeElementsWhere(
142 final KnowledgeElement.Properties... kprop) {
143 List<Proxy> result = new ArrayList<Proxy>();
147 // Creation of the Lucene query
148 File indir = getRepositoryService().getRepositoryIndexDirectory();
149 Directory index = FSDirectory.open(indir);
150 IndexSearcher searcher = new IndexSearcher(index, true);
151 BooleanQuery fulquery = new BooleanQuery();
153 for (int i = 0; i < kprop.length; i++) {
154 BooleanQuery query = new BooleanQuery();
155 Term input; // Supposed initialized below at least by the visibility
157 Visibility area = kprop[i].getVisibility(); // Visibility
159 input = new Term("area");
160 query.add(new TermQuery(input.createTerm(area.toString())),
161 BooleanClause.Occur.MUST);
163 ProgressState state = kprop[i].getProgressState(); // State
165 input = new Term("state");
167 new TermQuery(input.createTerm(state.toString())),
168 BooleanClause.Occur.MUST);
170 String refid = kprop[i].getReference(); // Reference
172 input = new Term("ref");
173 query.add(new TermQuery(input.createTerm(refid)),
174 BooleanClause.Occur.MUST);
176 KnowledgeElementType type = kprop[i].getType(); // Type
178 input = new Term("type");
179 query.add(new TermQuery(input.createTerm(type.getName())),
180 BooleanClause.Occur.MUST);
182 User manager = kprop[i].getAuthor(); // Author
183 if (manager != null) {
184 input = new Term("author");
185 query.add(new TermQuery(input
186 .createTerm(manager.toString())),
187 BooleanClause.Occur.MUST);
189 User actor = kprop[i].getActor(); // Contributor, Reviewer or Approver of the owner study
191 input = new Term("actor");
193 new TermQuery(input.createTerm(actor.toString())),
194 BooleanClause.Occur.MUST);
196 String title = kprop[i].getTitle(); // Title
198 input = new Term("contents");
199 BooleanQuery critext = new BooleanQuery();
200 String operator = "AND"; // Future user input
201 BooleanClause.Occur clause = BooleanClause.Occur.MUST;
202 if (operator.equals("OR")) {
203 clause = BooleanClause.Occur.SHOULD;
205 String[] word = title.split(" ");
206 for (int j = 0; j < word.length; j++) {
207 critext.add(new TermQuery(input.createTerm(word[j])),
210 query.add(critext, BooleanClause.Occur.MUST);
212 List<SimulationContext> context = kprop[i]
213 .getSimulationContexts();
214 if (context != null && context.size() > 0) {
215 BooleanQuery critext = new BooleanQuery();
216 for (Iterator<SimulationContext> j = context.iterator(); j
218 SimulationContext seltext = j.next();
219 input = new Term(String.valueOf(seltext.getType()
221 critext.add(new TermQuery(input.createTerm(seltext
222 .getValue())), BooleanClause.Occur.MUST);
224 query.add(critext, BooleanClause.Occur.MUST);
226 fulquery.add(query, BooleanClause.Occur.SHOULD);
228 if (LOG.isInfoEnabled()) {
229 LOG.info("Searching knowledges by Lucene query \""
230 + fulquery.toString() + "\".");
232 // Creation of the knowledge filter
233 BooleanFilter filter = new BooleanFilter();
234 TermsFilter select = new TermsFilter();
235 Term mytype = new Term("class");
236 select.addTerm(mytype.createTerm("KnowledgeElement"));
237 filter.add(new FilterClause(select, BooleanClause.Occur.SHOULD));
239 // Creation of the sort criteria
240 Sort sort = new Sort(new SortField("title", SortField.STRING));
243 TopFieldDocs found = searcher.search(fulquery, filter, hitsize,
246 if (found.totalHits < 1) {
247 return result; // No study found
250 // Construction of the result list
251 ScoreDoc[] hits = found.scoreDocs;
252 for (int i = 0; i < hits.length; i++) {
253 result.add(new IndexServiceImpl.ObjectProxy(searcher
257 } catch (Exception error) {
258 LOG.error("Error during Lucene search, reason:", error);
266 * @see org.splat.service.SearchService#selectStudiesWhere(org.splat.dal.bo.som.Study.Properties[])
268 public List<Proxy> selectStudiesWhere(final boolean allCriteria,
269 final boolean allContexts, final Study.Properties sprop) {
270 List<Proxy> result = new ArrayList<Proxy>();
272 DetachedCriteria query = DetachedCriteria
273 .forClass(Study.class, "study");
274 // Creation of the query
275 initQuery(query, sprop);
277 String title = sprop.getTitle(); // Title
279 // Look for given words in study titles
281 if (allCriteria) { // AND
282 critext = Restrictions.conjunction();
284 critext = Restrictions.disjunction();
287 String[] word = title.split(" ");
288 for (int j = 0; j < word.length; j++) {
289 critext.add(Restrictions.like("title", "%" + word[j] + "%"));
294 List<SimulationContext> context = sprop.getSimulationContexts();
295 if (context != null && (!context.isEmpty())) {
296 // Get only studies which have given contexts
297 query.createAlias("contex", "ctx", Criteria.INNER_JOIN);
299 if (allContexts) { // AND
300 critctx = Restrictions.conjunction();
302 critctx = Restrictions.disjunction();
304 for (SimulationContext seltext : context) {
305 // (simctxType = seltext.getType() AND simctxValue = seltext.getValue())
306 critctx.add(Restrictions.and(Restrictions.eq("ctx.value",
307 seltext.getValue()), Restrictions.eq("ctx.type",
308 seltext.getType())));
315 query.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
316 // Creation of the sort criteria
317 query.addOrder(Order.asc("title"));
319 if (LOG.isInfoEnabled()) {
320 LOG.info("Searching studies: \"" + query.toString() + "\".");
324 List<Study> found = getStudyDAO().getFilteredList(query);
326 // Construction of the result list
327 for (Study std : found) {
328 result.add(new StudyDTO(std.getIndex(), std.getReference(), std
329 .getProgressState(), std.getTitle(), std.getAuthor()
336 * Initialize query with base criteria.
343 private void initQuery(final DetachedCriteria query, final Properties sprop) {
344 ProgressState state = sprop.getProgressState(); // State
346 query.add(Restrictions.eq("state", state));
348 String refid = sprop.getReference(); // Reference
350 query.add(Restrictions.eq("sid", refid));
352 User manager = sprop.getManager(); // Author
353 if (manager != null) {
354 query.add(Restrictions.eq("manager", manager));
357 User actor = sprop.getActor(); // Contributor, Reviewer or Approver
359 // User is not logged in - show only public studies
360 query.add(Restrictions.eq("visibility", Visibility.PUBLIC));
362 // User is loggen in - show public studies and studies where he is participating
363 Disjunction orCrit = Restrictions.disjunction();
367 /* If the user is a validation cycle participant */
370 "{alias}.rid in (select vcrel.owner from cycle_rel vcrel inner join cycle vc on vcrel.refer = vc.rid where {alias}.rid = vcrel.owner AND (vc.publisher = ? OR vc.reviewer = ? OR vc.approver = ? OR vc.signatory = ?) group by vcrel.owner)",
382 /* If the user is contributor */
385 "{alias}.rid in (select rel.owner from contributor_rel rel where {alias}.rid = rel.owner AND rel.refer = ?)",
387 Hibernate.LONG)).add(
388 /* If the user is author */
389 Restrictions.eq("study.manager", actor)).add(
390 /* If the study is public */
391 Restrictions.eq("study.visibility",
392 Visibility.PUBLIC)));
399 * @see org.splat.service.SearchService#selectStudiesWhere(org.splat.dal.bo.som.Study.Properties[])
402 public List<Proxy> selectStudiesWhere(final Study.Properties... sprop) {
403 List<Proxy> result = new ArrayList<Proxy>();
407 // Creation of the Lucene query
408 File indir = getRepositoryService().getRepositoryIndexDirectory();
409 Directory index = FSDirectory.open(indir);
410 IndexSearcher searcher = new IndexSearcher(index, true);
411 BooleanQuery fulquery = new BooleanQuery();
413 for (int i = 0; i < sprop.length; i++) {
414 BooleanQuery query = new BooleanQuery();
415 Term input; // Supposed initialized below at least by the visibility
417 Visibility area = sprop[i].getVisibility(); // Visibility
419 input = new Term("area");
420 query.add(new TermQuery(input.createTerm(area.toString())),
421 BooleanClause.Occur.MUST);
423 ProgressState state = sprop[i].getProgressState(); // State
425 input = new Term("state");
426 if (state == ProgressState.inPROGRESS) {
427 BooleanQuery cristate = new BooleanQuery();
428 cristate.add(new TermQuery(input.createTerm("inWORK")),
429 BooleanClause.Occur.SHOULD);
431 new TermQuery(input.createTerm("inDRAFT")),
432 BooleanClause.Occur.SHOULD);
434 new TermQuery(input.createTerm("inCHECK")),
435 BooleanClause.Occur.SHOULD);
436 query.add(cristate, BooleanClause.Occur.MUST);
438 query.add(new TermQuery(input.createTerm(state
439 .toString())), BooleanClause.Occur.MUST);
442 String refid = sprop[i].getReference(); // Reference
444 input = new Term("ref");
445 query.add(new TermQuery(input.createTerm(refid)),
446 BooleanClause.Occur.MUST);
448 User manager = sprop[i].getManager(); // Author
449 if (manager != null) {
450 input = new Term("author");
451 query.add(new TermQuery(input
452 .createTerm(manager.toString())),
453 BooleanClause.Occur.MUST);
455 User actor = sprop[i].getActor(); // Contributor, Reviewer or Approver
457 input = new Term("actor");
459 new TermQuery(input.createTerm(actor.toString())),
460 BooleanClause.Occur.MUST);
462 String title = sprop[i].getTitle(); // Title
464 input = new Term("contents");
465 BooleanQuery critext = new BooleanQuery();
466 String operator = "AND"; // Future user input
467 BooleanClause.Occur clause = BooleanClause.Occur.MUST;
468 if (operator.equals("OR")) {
469 clause = BooleanClause.Occur.SHOULD;
471 String[] word = title.split(" ");
472 for (int j = 0; j < word.length; j++) {
473 critext.add(new TermQuery(input.createTerm(word[j])),
476 query.add(critext, BooleanClause.Occur.MUST);
478 List<SimulationContext> context = sprop[i]
479 .getSimulationContexts();
480 if (context != null && context.size() > 0) {
481 BooleanQuery critext = new BooleanQuery();
482 for (Iterator<SimulationContext> j = context.iterator(); j
484 SimulationContext seltext = j.next();
485 input = new Term(String.valueOf(seltext.getType()
487 critext.add(new TermQuery(input.createTerm(seltext
488 .getValue())), BooleanClause.Occur.MUST);
490 query.add(critext, BooleanClause.Occur.MUST);
492 fulquery.add(query, BooleanClause.Occur.SHOULD);
494 if (LOG.isInfoEnabled()) {
495 LOG.info("Searching studies by Lucene query \""
496 + fulquery.toString() + "\".");
498 // Creation of the studies filter
499 BooleanFilter filter = new BooleanFilter();
500 TermsFilter select = new TermsFilter();
501 Term mytype = new Term("class");
502 select.addTerm(mytype.createTerm("Study"));
503 filter.add(new FilterClause(select, BooleanClause.Occur.SHOULD));
505 // Creation of the sort criteria
506 Sort sort = new Sort(new SortField("title", SortField.STRING));
509 TopFieldDocs found = searcher.search(fulquery, filter, hitsize,
512 if (found.totalHits < 1) {
513 return result; // No study found
516 // Construction of the result list
517 ScoreDoc[] hits = found.scoreDocs;
518 for (int i = 0; i < hits.length; i++) {
519 result.add(new IndexServiceImpl.ObjectProxy(searcher
523 } catch (Exception error) {
524 LOG.error("Error during Lucene search, reason:", error);
532 * @see org.splat.service.SearchService#indexStudy(org.splat.dal.bo.som.Study)
535 public void indexStudy(final Study study) {
536 LOG.debug("Index study: id=" + study.getRid() + "; reference="
537 + study.getReference());
539 Study.Properties sprop = new Study.Properties();
540 List<Proxy> index = selectStudiesWhere(sprop.setReference(study
543 if (index.size() != 0) {
544 LOG.debug("The given study is already indexed.");
545 return; // The given study is already indexed
548 IndexService lucin = getIndex();
549 Scenario[] scenes = study.getScenarii();
551 LOG.debug("Number of study " + study.getReference() + " actors: "
552 + study.getActor().size());
554 if (study.getProgressState() != ProgressState.inWORK) {
555 for (int i = 0; i < scenes.length; i++) {
556 List<KnowledgeElement> list = scenes[i]
557 .getAllKnowledgeElements();
558 for (Iterator<KnowledgeElement> j = list.iterator(); j
561 LOG.debug("Knowlegge added: id=" + j.next().getIndex());
565 } catch (Exception error) {
566 LOG.error("Unable to index the study '" + study.getIndex()
567 + "', reason:", error);
572 * Get lucene index handler. Create the index if it is not exist.
574 * @return IndexService
575 * @throws IOException
576 * if error when creating a new lucene index
578 private IndexService getIndex() throws IOException {
579 IndexService lucin = getIndexService();
580 if (!lucin.exists()) {
581 lucin.create(); // Happens when re-indexing all studies
587 * Get the repositoryService.
589 * @return the repositoryService
591 public RepositoryService getRepositoryService() {
592 return _repositoryService;
596 * Set the repositoryService.
598 * @param repositoryService
599 * the repositoryService to set
601 public void setRepositoryService(final RepositoryService repositoryService) {
602 _repositoryService = repositoryService;
606 * Get the indexService.
608 * @return the indexService
610 public IndexService getIndexService() {
611 return _indexService;
615 * Set the indexService.
617 * @param indexService
618 * the indexService to set
620 public void setIndexService(final IndexService indexService) {
621 _indexService = indexService;
625 * Get the studyService.
627 * @return the studyService
629 public StudyService getStudyService() {
630 return _studyService;
634 * Set the studyService.
636 * @param studyService
637 * the studyService to set
639 public void setStudyService(final StudyService studyService) {
640 _studyService = studyService;
646 * @return the studyDAO
648 public StudyDAO getStudyDAO() {
656 * the studyDAO to set
658 public void setStudyDAO(final StudyDAO studyDAO) {
659 _studyDAO = studyDAO;