Salome HOME
Study validation cycle operations are implemented according to the specification.
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / service / SearchServiceImpl.java
1 /*****************************************************************************
2  * Company         OPEN CASCADE
3  * Application     SIMAN
4  * File            $Id$ 
5  * Creation date   05.10.2012
6  * @author         $Author$
7  * @version        $Revision$
8  *****************************************************************************/
9
10 package org.splat.service;
11
12 import java.io.File;
13 import java.io.IOException;
14 import java.util.ArrayList;
15 import java.util.Iterator;
16 import java.util.List;
17
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;
62
63 /**
64  * Search service implementation.
65  * 
66  * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
67  */
68 public class SearchServiceImpl implements SearchService {
69
70         /**
71          * The service logger.
72          */
73         public final static Logger LOG = Logger
74                         .getLogger(org.splat.service.SearchServiceImpl.class);
75         /**
76          * "title" property name.
77          */
78         private final static String PROP_TITLE = "title";
79
80         /**
81          * Injected repository service.
82          */
83         private RepositoryService _repositoryService;
84         /**
85          * Injected index service.
86          */
87         private IndexService _indexService;
88         /**
89          * Injected study service.
90          */
91         private StudyService _studyService;
92         /**
93          * Injected study DAO.
94          */
95         private StudyDAO _studyDAO;
96         /**
97          * Injected knowledge element DAO.
98          */
99         private KnowledgeElementDAO _knowledgeElementDAO;
100
101         /**
102          * Get a list of studies which are currently not presented in the lucene index.
103          * 
104          * @return list of ImportedStudy DTO
105          */
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();
111
112                 for (Study aStudy : dbStudies) {
113                         try {
114                                 sprop.clear();
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.
119                                         continue;
120                                 }
121                         } catch (Exception error) {
122                                 continue;
123                         }
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));
127                 }
128                 return table;
129         }
130
131         /**
132          * Refresh lucene index for studies.
133          * 
134          * @param ridlist
135          *            list of studies id's
136          */
137         @Transactional(readOnly = true)
138         @Deprecated
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);
143                         indexStudy(study);
144                 }
145         }
146
147         /**
148          * {@inheritDoc}
149          * 
150          * @see org.splat.service.SearchService#selectKnowledgeElementsWhere(org.splat.service.dto.KnowledgeSearchFilterDTO)
151          */
152         public List<Proxy> selectKnowledgeElementsWhere(
153                         final KnowledgeSearchFilterDTO filter) {
154                 List<Proxy> result = new ArrayList<Proxy>();
155
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));
162
163                 // Creation of the query
164                 Junction topJunction = initQuery(filter);
165
166                 addByWordsCriteria(topJunction, filter);
167
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);
172                         Junction critctx;
173                         if (filter.isMatchAllContexts()) { // AND
174                                 critctx = Restrictions.conjunction();
175                         } else { // OR
176                                 critctx = Restrictions.disjunction();
177                         }
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())));
183
184                         }
185                         topJunction.add(critctx);
186                 }
187
188                 query.add(topJunction);
189                 // Group by study
190                 query.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
191                 // Creation of the sort criteria
192                 query.addOrder(Order.asc(PROP_TITLE));
193
194                 if (LOG.isInfoEnabled()) {
195                         LOG.info("Searching knowledge elements: \"" + query.toString());
196                 }
197
198                 // Search
199                 List<KnowledgeElement> found = getKnowledgeElementDAO()
200                                 .getFilteredList(query);
201
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()
206                                         .getDisplayName()));
207                 }
208                 return result;
209         }
210
211         /**
212          * Add search criteria for filtering by title contents.
213          * 
214          * @param topJunction
215          *            the search condition to be appended
216          * @param filter
217          *            the criteria
218          */
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
224                         Junction critext;
225                         if (filter.isMatchAllCriteria()) { // AND
226                                 critext = Restrictions.conjunction();
227                         } else { // OR
228                                 critext = Restrictions.disjunction();
229                         }
230
231                         String[] word = title.split(" ");
232                         for (int j = 0; j < word.length; j++) {
233                                 critext.add(Restrictions.like(PROP_TITLE, "%" + word[j] + "%"));
234                         }
235                         topJunction.add(critext);
236                 }
237         }
238
239         /**
240          * {@inheritDoc}
241          * 
242          * @see org.splat.service.SearchService#selectStudiesWhere(org.splat.service.dto.StudySearchFilterDTO)
243          */
244         public List<Proxy> selectStudiesWhere(final StudySearchFilterDTO filter) {
245                 List<Proxy> result = new ArrayList<Proxy>();
246
247                 DetachedCriteria query = DetachedCriteria
248                                 .forClass(Study.class, "study").add(visibleStudyFilter(filter));
249
250                 // Creation of the query
251                 Junction topJunction = initQuery(filter);
252
253                 addByWordsCriteria(topJunction, filter);
254
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);
259                         Junction critctx;
260                         if (filter.isMatchAllContexts()) { // AND
261                                 critctx = Restrictions.conjunction();
262                         } else { // OR
263                                 critctx = Restrictions.disjunction();
264                         }
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())));
270
271                         }
272                         topJunction.add(critctx);
273                 }
274
275                 query.add(topJunction);
276                 // Group by study
277                 query.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
278                 // Creation of the sort criteria
279                 query.addOrder(Order.asc(PROP_TITLE));
280
281                 if (LOG.isInfoEnabled()) {
282                         LOG.info("Searching studies: \"" + query.toString());
283                 }
284
285                 // Search
286                 List<Study> found = getStudyDAO().getFilteredList(query);
287
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()
292                                         .getDisplayName()));
293                 }
294                 return result;
295         }
296
297         /**
298          * Initialize query with base criteria.
299          * 
300          * @param filter
301          *            the criteria
302          * @return top junction of the search filter
303          */
304         private Junction initQuery(final StudySearchFilterDTO filter) {
305                 Junction topJunction;
306                 if (filter.isMatchAllCriteria()) { // AND
307                         topJunction = Restrictions.conjunction();
308                 } else { // OR
309                         topJunction = Restrictions.disjunction();
310                 }
311                 if (!SearchFilterDTO.ANY_STATE.equals(filter.getState())) {
312                         ProgressState state = ProgressState.valueOf(filter.getState()); // State
313                         if (state != null) {
314                                 topJunction.add(Restrictions.eq("state", state));
315                         }
316                 }
317                 String refid = filter.getReference(); // Reference
318                 if (refid != null && !refid.isEmpty()) {
319                         topJunction.add(Restrictions.eq("sid", refid));
320                 }
321
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()));
328                 }
329                 if (filter.getUpdatedBefore() != null) {
330                         topJunction.add(Restrictions.lt("lasdate", filter
331                                         .getUpdatedBefore()));
332                 }
333
334                 // Filter by study author
335                 long authorId = Long.valueOf(filter.getAuthor());
336                 if (authorId > 0) {
337                         topJunction.add(Restrictions.eq("manager.rid", authorId));
338                 }
339
340                 return topJunction;
341         }
342
343         /**
344          * Initialize query with base criteria.
345          * 
346          * @param filter
347          *            the criteria
348          * @return top junction of the search filter
349          */
350         private Junction initQuery(final KnowledgeSearchFilterDTO filter) {
351                 Junction topJunction;
352                 if (filter.isMatchAllCriteria()) { // AND
353                         topJunction = Restrictions.conjunction();
354                 } else { // OR
355                         topJunction = Restrictions.disjunction();
356                 }
357
358                 // Filter by knowledge type
359                 long ktypeId = Long.valueOf(filter.getKtype());
360                 if (ktypeId > 0) {
361                         topJunction.add(Restrictions.eq("kelm.type.rid", ktypeId));
362                 }
363
364                 if (!SearchFilterDTO.ANY_STATE.equals(filter.getState())) {
365                         ProgressState state = ProgressState.valueOf(filter.getState()); // State
366                         if (state != null) {
367                                 topJunction.add(Restrictions.eq("kelm.state", state));
368                         }
369                 }
370
371                 String refid = filter.getReference(); // Reference
372                 if (refid != null && !refid.isEmpty()) {
373                         long id = Long.valueOf(refid.replaceAll("^KE(0)*", ""));
374                         if (id > 0) {
375                                 topJunction.add(Restrictions.eq("kelm.rid", id));
376                         }
377                 }
378
379                 addCreationDateCriteria(topJunction, filter, "date");
380
381                 // Filter by knowledge author
382                 long authorId = Long.valueOf(filter.getAuthor());
383                 if (authorId > 0) {
384                         topJunction.add(Restrictions.eq("kelm.author.rid", authorId));
385                 }
386
387                 return topJunction;
388         }
389
390         /**
391          * Filter for visible studies.
392          * 
393          * @param filter
394          *            search criteria
395          * @return search condition for studies to get only visible studies
396          */
397         private Junction visibleStudyFilter(final SearchFilterDTO filter) {
398                 Junction topJunction;
399                 if (filter.isMatchAllCriteria()) { // AND
400                         topJunction = Restrictions.conjunction();
401                 } else { // OR
402                         topJunction = Restrictions.disjunction();
403                 }
404
405                 // Filter by connected user
406                 long actorId = filter.getConnectedUserId(); // Contributor, Reviewer or Approver
407                 if (actorId > 0) {
408                         // User is loggen in - show public studies and studies where he is participating
409                         Disjunction orCrit = Restrictions.disjunction();
410                         topJunction
411                                         .add(orCrit
412                                                         .add(
413                                                                         /* If the user is a validation cycle participant */
414                                                                         Restrictions
415                                                                                         .sqlRestriction(
416                                                                                                         "{alias}.rid in ("
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,
423                                                                                                                         actorId, actorId,
424                                                                                                                         actorId },
425                                                                                                         new Type[] {
426                                                                                                                         Hibernate.LONG,
427                                                                                                                         Hibernate.LONG,
428                                                                                                                         Hibernate.LONG,
429                                                                                                                         Hibernate.LONG }))
430                                                         .add(
431                                                                         /* If the user is contributor */
432                                                                         Restrictions
433                                                                                         .sqlRestriction(
434                                                                                                         "{alias}.rid in ("
435                                                                                                                         + "select rel.owner from contributor_rel rel "
436                                                                                                                         + "where {alias}.rid = rel.owner AND rel.refer = ?)",
437                                                                                                         actorId, Hibernate.LONG))
438                                                         .add(
439                                                                         /* If the user is reader */
440                                                                         Restrictions
441                                                                                         .sqlRestriction(
442                                                                                                         "{alias}.rid in ("
443                                                                                                                         + "select rel.owner from reader_rel rel "
444                                                                                                                         + "where {alias}.rid = rel.owner AND rel.refer = ?)",
445                                                                                                         actorId, Hibernate.LONG))
446                                                         .add(
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)));
452                 } else {
453                         // User is not logged in - show only public studies
454                         topJunction.add(Restrictions.eq("study.visibility",
455                                         Visibility.PUBLIC));
456                 }
457                 return topJunction;
458         }
459
460         /**
461          * Add search criteria by dates to the junction filter condition.
462          * 
463          * @param topJunction
464          *            the junction filter condition
465          * @param filter
466          *            search criteria
467          * @param propName
468          *            creation date property name
469          */
470         private void addCreationDateCriteria(final Junction topJunction,
471                         final SearchFilterDTO filter, final String propName) {
472                 // Filter by creation date
473                 if (filter.getCreatedAfter() != null) {
474                         topJunction
475                                         .add(Restrictions.gt(propName, filter.getCreatedAfter()));
476                 }
477                 if (filter.getCreatedBefore() != null) {
478                         topJunction.add(Restrictions
479                                         .lt(propName, filter.getCreatedBefore()));
480                 }
481         }
482
483         /**
484          * {@inheritDoc}
485          * 
486          * @see org.splat.service.SearchService#selectStudiesWhere(org.splat.dal.bo.som.Study.Properties[])
487          */
488         @Deprecated
489         public List<Proxy> selectStudiesWhere(final Study.Properties... sprop) {
490                 List<Proxy> result = new ArrayList<Proxy>();
491                 int hitsize = 20;
492                 try {
493
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();
499
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
503
504                                 Visibility area = sprop[i].getVisibility(); // Visibility
505                                 if (area != null) {
506                                         input = new Term("area");
507                                         query.add(new TermQuery(input.createTerm(area.toString())),
508                                                         BooleanClause.Occur.MUST);
509                                 }
510                                 ProgressState state = sprop[i].getProgressState(); // State
511                                 if (state != null) {
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);
517                                                 cristate.add(
518                                                                 new TermQuery(input.createTerm("inDRAFT")),
519                                                                 BooleanClause.Occur.SHOULD);
520                                                 cristate.add(
521                                                                 new TermQuery(input.createTerm("inCHECK")),
522                                                                 BooleanClause.Occur.SHOULD);
523                                                 query.add(cristate, BooleanClause.Occur.MUST);
524                                         } else {
525                                                 query.add(new TermQuery(input.createTerm(state
526                                                                 .toString())), BooleanClause.Occur.MUST);
527                                         }
528                                 }
529                                 String refid = sprop[i].getReference(); // Reference
530                                 if (refid != null) {
531                                         input = new Term("ref");
532                                         query.add(new TermQuery(input.createTerm(refid)),
533                                                         BooleanClause.Occur.MUST);
534                                 }
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);
541                                 }
542                                 User actor = sprop[i].getActor(); // Contributor, Reviewer or Approver
543                                 if (actor != null) {
544                                         input = new Term("actor");
545                                         query.add(
546                                                         new TermQuery(input.createTerm(actor.toString())),
547                                                         BooleanClause.Occur.MUST);
548                                 }
549                                 String title = sprop[i].getTitle(); // Title
550                                 if (title != null) {
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;
557                                         }
558                                         String[] word = title.split(" ");
559                                         for (int j = 0; j < word.length; j++) {
560                                                 critext.add(new TermQuery(input.createTerm(word[j])),
561                                                                 clause);
562                                         }
563                                         query.add(critext, BooleanClause.Occur.MUST);
564                                 }
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
570                                                         .hasNext();) {
571                                                 SimulationContext seltext = j.next();
572                                                 input = new Term(String.valueOf(seltext.getType()
573                                                                 .getIndex()));
574                                                 critext.add(new TermQuery(input.createTerm(seltext
575                                                                 .getValue())), BooleanClause.Occur.MUST);
576                                         }
577                                         query.add(critext, BooleanClause.Occur.MUST);
578                                 }
579                                 fulquery.add(query, BooleanClause.Occur.SHOULD);
580                         }
581                         if (LOG.isInfoEnabled()) {
582                                 LOG.info("Searching studies by Lucene query \""
583                                                 + fulquery.toString());
584                         }
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));
591
592                         // Creation of the sort criteria
593                         Sort sort = new Sort(new SortField(PROP_TITLE, SortField.STRING));
594
595                         // Search
596                         TopFieldDocs found = searcher.search(fulquery, filter, hitsize,
597                                         sort);
598
599                         if (found.totalHits < 1) {
600                                 return result; // No study found
601                         }
602
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
607                                                 .doc(hits[i].doc)));
608                         }
609                         searcher.close();
610                 } catch (Exception error) {
611                         LOG.error("Error during Lucene search, reason:", error);
612                 }
613                 return result;
614         }
615
616         /**
617          * {@inheritDoc}
618          * 
619          * @see org.splat.service.SearchService#indexStudy(org.splat.dal.bo.som.Study)
620          */
621         @Deprecated
622         public void indexStudy(final Study study) {
623                 LOG.debug("Index study: id=" + study.getRid() + "; reference="
624                                 + study.getReference());
625                 try {
626                         Study.Properties sprop = new Study.Properties();
627                         List<Proxy> index = selectStudiesWhere(sprop.setReference(study
628                                         .getReference()));
629
630                         if (index.size() != 0) {
631                                 LOG.debug("The given study is already indexed.");
632                                 return; // The given study is already indexed
633                         }
634
635                         IndexService lucin = getIndex();
636                         Scenario[] scenes = study.getScenarii();
637
638                         LOG.debug("Number of study " + study.getReference() + " actors: "
639                                         + study.getActor().size());
640                         lucin.add(study);
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
646                                                         .hasNext();) {
647                                                 lucin.add(j.next());
648                                                 LOG.debug("Knowlegge added: id=" + j.next().getIndex());
649                                         }
650                                 }
651                         }
652                 } catch (Exception error) {
653                         LOG.error("Unable to index the study '" + study.getIndex()
654                                         + "', reason:", error);
655                 }
656         }
657
658         /**
659          * Get lucene index handler. Create the index if it is not exist.
660          * 
661          * @return IndexService
662          * @throws IOException
663          *             if error when creating a new lucene index
664          */
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()));
671                 }
672                 if (!lucin.exists()) {
673                         lucin.create(); // Happens when re-indexing all studies
674                 }
675                 return lucin;
676         }
677
678         /**
679          * Get the repositoryService.
680          * 
681          * @return the repositoryService
682          */
683         public RepositoryService getRepositoryService() {
684                 return _repositoryService;
685         }
686
687         /**
688          * Set the repositoryService.
689          * 
690          * @param repositoryService
691          *            the repositoryService to set
692          */
693         public void setRepositoryService(final RepositoryService repositoryService) {
694                 _repositoryService = repositoryService;
695         }
696
697         /**
698          * Get the indexService.
699          * 
700          * @return the indexService
701          */
702         public IndexService getIndexService() {
703                 return _indexService;
704         }
705
706         /**
707          * Set the indexService.
708          * 
709          * @param indexService
710          *            the indexService to set
711          */
712         public void setIndexService(final IndexService indexService) {
713                 _indexService = indexService;
714         }
715
716         /**
717          * Get the studyService.
718          * 
719          * @return the studyService
720          */
721         public StudyService getStudyService() {
722                 return _studyService;
723         }
724
725         /**
726          * Set the studyService.
727          * 
728          * @param studyService
729          *            the studyService to set
730          */
731         public void setStudyService(final StudyService studyService) {
732                 _studyService = studyService;
733         }
734
735         /**
736          * Get the studyDAO.
737          * 
738          * @return the studyDAO
739          */
740         public StudyDAO getStudyDAO() {
741                 return _studyDAO;
742         }
743
744         /**
745          * Set the studyDAO.
746          * 
747          * @param studyDAO
748          *            the studyDAO to set
749          */
750         public void setStudyDAO(final StudyDAO studyDAO) {
751                 _studyDAO = studyDAO;
752         }
753
754         /**
755          * Get the knowledgeElementDAO.
756          * 
757          * @return the knowledgeElementDAO
758          */
759         public KnowledgeElementDAO getKnowledgeElementDAO() {
760                 return _knowledgeElementDAO;
761         }
762
763         /**
764          * Set the knowledgeElementDAO.
765          * 
766          * @param knowledgeElementDAO
767          *            the knowledgeElementDAO to set
768          */
769         public void setKnowledgeElementDAO(
770                         final KnowledgeElementDAO knowledgeElementDAO) {
771                 _knowledgeElementDAO = knowledgeElementDAO;
772         }
773 }