Salome HOME
Fix of checkin and removeDocument.
[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 author */
440                                                         Restrictions.eq("study.manager.rid", actorId)).add(
441                                                                         /* If the study is public */
442                                                                         Restrictions.eq("study.visibility",
443                                                                                         Visibility.PUBLIC)));
444                 } else {
445                         // User is not logged in - show only public studies
446                         topJunction.add(Restrictions.eq("study.visibility",
447                                         Visibility.PUBLIC));
448                 }
449                 return topJunction;
450         }
451
452         /**
453          * Add search criteria by dates to the junction filter condition.
454          * 
455          * @param topJunction
456          *            the junction filter condition
457          * @param filter
458          *            search criteria
459          * @param propName
460          *            creation date property name
461          */
462         private void addCreationDateCriteria(final Junction topJunction,
463                         final SearchFilterDTO filter, final String propName) {
464                 // Filter by creation date
465                 if (filter.getCreatedAfter() != null) {
466                         topJunction
467                                         .add(Restrictions.gt(propName, filter.getCreatedAfter()));
468                 }
469                 if (filter.getCreatedBefore() != null) {
470                         topJunction.add(Restrictions
471                                         .lt(propName, filter.getCreatedBefore()));
472                 }
473         }
474
475         /**
476          * {@inheritDoc}
477          * 
478          * @see org.splat.service.SearchService#selectStudiesWhere(org.splat.dal.bo.som.Study.Properties[])
479          */
480         @Deprecated
481         public List<Proxy> selectStudiesWhere(final Study.Properties... sprop) {
482                 List<Proxy> result = new ArrayList<Proxy>();
483                 int hitsize = 20;
484                 try {
485
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();
491
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
495
496                                 Visibility area = sprop[i].getVisibility(); // Visibility
497                                 if (area != null) {
498                                         input = new Term("area");
499                                         query.add(new TermQuery(input.createTerm(area.toString())),
500                                                         BooleanClause.Occur.MUST);
501                                 }
502                                 ProgressState state = sprop[i].getProgressState(); // State
503                                 if (state != null) {
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);
509                                                 cristate.add(
510                                                                 new TermQuery(input.createTerm("inDRAFT")),
511                                                                 BooleanClause.Occur.SHOULD);
512                                                 cristate.add(
513                                                                 new TermQuery(input.createTerm("inCHECK")),
514                                                                 BooleanClause.Occur.SHOULD);
515                                                 query.add(cristate, BooleanClause.Occur.MUST);
516                                         } else {
517                                                 query.add(new TermQuery(input.createTerm(state
518                                                                 .toString())), BooleanClause.Occur.MUST);
519                                         }
520                                 }
521                                 String refid = sprop[i].getReference(); // Reference
522                                 if (refid != null) {
523                                         input = new Term("ref");
524                                         query.add(new TermQuery(input.createTerm(refid)),
525                                                         BooleanClause.Occur.MUST);
526                                 }
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);
533                                 }
534                                 User actor = sprop[i].getActor(); // Contributor, Reviewer or Approver
535                                 if (actor != null) {
536                                         input = new Term("actor");
537                                         query.add(
538                                                         new TermQuery(input.createTerm(actor.toString())),
539                                                         BooleanClause.Occur.MUST);
540                                 }
541                                 String title = sprop[i].getTitle(); // Title
542                                 if (title != null) {
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;
549                                         }
550                                         String[] word = title.split(" ");
551                                         for (int j = 0; j < word.length; j++) {
552                                                 critext.add(new TermQuery(input.createTerm(word[j])),
553                                                                 clause);
554                                         }
555                                         query.add(critext, BooleanClause.Occur.MUST);
556                                 }
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
562                                                         .hasNext();) {
563                                                 SimulationContext seltext = j.next();
564                                                 input = new Term(String.valueOf(seltext.getType()
565                                                                 .getIndex()));
566                                                 critext.add(new TermQuery(input.createTerm(seltext
567                                                                 .getValue())), BooleanClause.Occur.MUST);
568                                         }
569                                         query.add(critext, BooleanClause.Occur.MUST);
570                                 }
571                                 fulquery.add(query, BooleanClause.Occur.SHOULD);
572                         }
573                         if (LOG.isInfoEnabled()) {
574                                 LOG.info("Searching studies by Lucene query \""
575                                                 + fulquery.toString());
576                         }
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));
583
584                         // Creation of the sort criteria
585                         Sort sort = new Sort(new SortField(PROP_TITLE, SortField.STRING));
586
587                         // Search
588                         TopFieldDocs found = searcher.search(fulquery, filter, hitsize,
589                                         sort);
590
591                         if (found.totalHits < 1) {
592                                 return result; // No study found
593                         }
594
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
599                                                 .doc(hits[i].doc)));
600                         }
601                         searcher.close();
602                 } catch (Exception error) {
603                         LOG.error("Error during Lucene search, reason:", error);
604                 }
605                 return result;
606         }
607
608         /**
609          * {@inheritDoc}
610          * 
611          * @see org.splat.service.SearchService#indexStudy(org.splat.dal.bo.som.Study)
612          */
613         @Deprecated
614         public void indexStudy(final Study study) {
615                 LOG.debug("Index study: id=" + study.getRid() + "; reference="
616                                 + study.getReference());
617                 try {
618                         Study.Properties sprop = new Study.Properties();
619                         List<Proxy> index = selectStudiesWhere(sprop.setReference(study
620                                         .getReference()));
621
622                         if (index.size() != 0) {
623                                 LOG.debug("The given study is already indexed.");
624                                 return; // The given study is already indexed
625                         }
626
627                         IndexService lucin = getIndex();
628                         Scenario[] scenes = study.getScenarii();
629
630                         LOG.debug("Number of study " + study.getReference() + " actors: "
631                                         + study.getActor().size());
632                         lucin.add(study);
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
638                                                         .hasNext();) {
639                                                 lucin.add(j.next());
640                                                 LOG.debug("Knowlegge added: id=" + j.next().getIndex());
641                                         }
642                                 }
643                         }
644                 } catch (Exception error) {
645                         LOG.error("Unable to index the study '" + study.getIndex()
646                                         + "', reason:", error);
647                 }
648         }
649
650         /**
651          * Get lucene index handler. Create the index if it is not exist.
652          * 
653          * @return IndexService
654          * @throws IOException
655          *             if error when creating a new lucene index
656          */
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()));
663                 }
664                 if (!lucin.exists()) {
665                         lucin.create(); // Happens when re-indexing all studies
666                 }
667                 return lucin;
668         }
669
670         /**
671          * Get the repositoryService.
672          * 
673          * @return the repositoryService
674          */
675         public RepositoryService getRepositoryService() {
676                 return _repositoryService;
677         }
678
679         /**
680          * Set the repositoryService.
681          * 
682          * @param repositoryService
683          *            the repositoryService to set
684          */
685         public void setRepositoryService(final RepositoryService repositoryService) {
686                 _repositoryService = repositoryService;
687         }
688
689         /**
690          * Get the indexService.
691          * 
692          * @return the indexService
693          */
694         public IndexService getIndexService() {
695                 return _indexService;
696         }
697
698         /**
699          * Set the indexService.
700          * 
701          * @param indexService
702          *            the indexService to set
703          */
704         public void setIndexService(final IndexService indexService) {
705                 _indexService = indexService;
706         }
707
708         /**
709          * Get the studyService.
710          * 
711          * @return the studyService
712          */
713         public StudyService getStudyService() {
714                 return _studyService;
715         }
716
717         /**
718          * Set the studyService.
719          * 
720          * @param studyService
721          *            the studyService to set
722          */
723         public void setStudyService(final StudyService studyService) {
724                 _studyService = studyService;
725         }
726
727         /**
728          * Get the studyDAO.
729          * 
730          * @return the studyDAO
731          */
732         public StudyDAO getStudyDAO() {
733                 return _studyDAO;
734         }
735
736         /**
737          * Set the studyDAO.
738          * 
739          * @param studyDAO
740          *            the studyDAO to set
741          */
742         public void setStudyDAO(final StudyDAO studyDAO) {
743                 _studyDAO = studyDAO;
744         }
745
746         /**
747          * Get the knowledgeElementDAO.
748          * 
749          * @return the knowledgeElementDAO
750          */
751         public KnowledgeElementDAO getKnowledgeElementDAO() {
752                 return _knowledgeElementDAO;
753         }
754
755         /**
756          * Set the knowledgeElementDAO.
757          * 
758          * @param knowledgeElementDAO
759          *            the knowledgeElementDAO to set
760          */
761         public void setKnowledgeElementDAO(
762                         final KnowledgeElementDAO knowledgeElementDAO) {
763                 _knowledgeElementDAO = knowledgeElementDAO;
764         }
765 }