1 /*****************************************************************************
5 * Creation date 02.10.2012
6 * @author Author: Maria KRUCHININA
8 *****************************************************************************/
10 package org.splat.service;
12 import java.io.IOException;
13 import java.text.DecimalFormat;
14 import java.text.SimpleDateFormat;
15 import java.util.ArrayList;
16 import java.util.Calendar;
17 import java.util.Collections;
18 import java.util.Date;
19 import java.util.HashSet;
20 import java.util.Iterator;
21 import java.util.List;
25 import org.apache.lucene.index.IndexWriter;
26 import org.apache.lucene.store.FSDirectory;
27 import org.hibernate.Criteria;
28 import org.hibernate.criterion.DetachedCriteria;
29 import org.hibernate.criterion.Order;
30 import org.hibernate.criterion.Restrictions;
31 import org.splat.dal.bo.kernel.Relation;
32 import org.splat.dal.bo.kernel.User;
33 import org.splat.dal.bo.som.ActorRelation;
34 import org.splat.dal.bo.som.ContributorRelation;
35 import org.splat.dal.bo.som.DescriptionAttribute;
36 import org.splat.dal.bo.som.DocumentType;
37 import org.splat.dal.bo.som.IDBuilder;
38 import org.splat.dal.bo.som.KnowledgeElement;
39 import org.splat.dal.bo.som.ProgressState;
40 import org.splat.dal.bo.som.Publication;
41 import org.splat.dal.bo.som.ReaderRelation;
42 import org.splat.dal.bo.som.Scenario;
43 import org.splat.dal.bo.som.SimulationContext;
44 import org.splat.dal.bo.som.Study;
45 import org.splat.dal.bo.som.ValidationCycle;
46 import org.splat.dal.bo.som.ValidationCycleRelation;
47 import org.splat.dal.bo.som.ValidationStep;
48 import org.splat.dal.bo.som.Visibility;
49 import org.splat.dal.bo.som.Study.Properties;
50 import org.splat.dal.bo.som.ValidationCycle.Actor;
51 import org.splat.dal.dao.som.DescriptionAttributeDAO;
52 import org.splat.dal.dao.som.DocumentDAO;
53 import org.splat.dal.dao.som.DocumentTypeDAO;
54 import org.splat.dal.dao.som.IDBuilderDAO;
55 import org.splat.dal.dao.som.PublicationDAO;
56 import org.splat.dal.dao.som.ScenarioDAO;
57 import org.splat.dal.dao.som.StudyDAO;
58 import org.splat.dal.dao.som.UsedByRelationDAO;
59 import org.splat.dal.dao.som.ValidationCycleDAO;
60 import org.splat.exception.BusinessException;
61 import org.splat.exception.InvalidParameterException;
62 import org.splat.kernel.InvalidPropertyException;
63 import org.splat.kernel.MissedPropertyException;
64 import org.splat.kernel.MultiplyDefinedException;
65 import org.splat.log.AppLogger;
66 import org.splat.service.dto.UserDTO;
67 import org.splat.service.technical.IndexService;
68 import org.splat.service.technical.ProjectSettingsService;
69 import org.splat.service.technical.ProjectSettingsServiceImpl;
70 import org.splat.service.technical.RepositoryService;
71 import org.splat.som.Revision;
72 import org.splat.util.BeanHelper;
73 import org.springframework.transaction.annotation.Transactional;
76 * This class defines all methods for creation, modification the study.
78 * @author Maria KRUCHININA
81 public class StudyServiceImpl implements StudyService {
84 * logger for the service.
86 public final static AppLogger LOG = AppLogger
87 .getLogger(StudyServiceImpl.class);
89 * "studyId" parameter name.
91 public final static String PARAM_STUDY_ID = "studyId";
94 * Injected index service.
96 private IndexService _indexService;
99 * Injected step service.
101 private StepService _stepService;
104 * Injected project service.
106 private ProjectSettingsService _projectSettings;
109 * Injected project element service.
111 private ProjectElementService _projectElementService;
114 * Injected study DAO.
116 private StudyDAO _studyDAO;
119 * Injected usedBy relations DAO.
121 private UsedByRelationDAO _usedByRelationDAO;
124 * Injected scenario DAO.
126 private ScenarioDAO _scenarioDAO;
129 * Injected validation cycle DAO.
131 private ValidationCycleDAO _validationCycleDAO;
134 * Injected IDBuilder DAO.
136 private IDBuilderDAO _iDBuilderDAO;
139 * Injected document type service.
141 private DocumentTypeService _documentTypeService;
144 * Injected user service.
146 private UserService _userService;
149 * Injected publication DAO.
151 private PublicationDAO _publicationDAO;
154 * Injected repository service.
156 private RepositoryService _repositoryService;
159 * Injected document DAO.
161 private DocumentDAO _documentDAO;
164 * Injected description attribute DAO.
166 private DescriptionAttributeDAO _descriptionAttributeDAO;
168 * Injected document type DAO.
170 private DocumentTypeDAO _documentTypeDAO;
175 * @see org.splat.service.StudyService#selectStudy(long)
178 public Study selectStudy(final long index) {
179 Study result = getStudyDAO().get(index);
180 if (result != null) {
181 loadWorkflow(result);
189 * @see org.splat.service.StudyService#removeStudy(long)
192 public void removeStudy(final Long index) {
193 Study study = getStudyDAO().get(index);
194 Set<org.splat.dal.bo.som.Document> docums = new HashSet<org.splat.dal.bo.som.Document>();
196 // Select all documents published in the study and study's scenarios.
197 DetachedCriteria query = DetachedCriteria.forClass(
198 Publication.class, "pub");
200 .createCriteria("pub.owner", "projelem",
202 .createAlias("projelem.owner", "study", Criteria.LEFT_JOIN)
205 .or(Restrictions.eq("projelem.rid", index),
206 Restrictions.eq("study.rid", index)))
207 .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
209 if (LOG.isDebugEnabled()) {
210 LOG.debug("Find study documents: " + query.toString());
212 for (Publication pub : getPublicationDAO().getFilteredList(query)) {
213 docums.add(pub.value());
214 // Find also all previous versions of the document
215 for (org.splat.dal.bo.som.Document prev = pub.value()
216 .getPreviousVersion(); prev != null; prev = prev
217 .getPreviousVersion()) {
222 // Delete the study with its relations, scenarios and publications
223 getStudyDAO().delete(study);
225 // Remove all relations of study documents
226 for (org.splat.dal.bo.som.Document doc : docums) {
227 LOG.debug("Found doc: " + doc.getTitle() + " ["
228 + doc.getReference() + "]" + " [" + doc.getRid() + "]");
229 doc.getAllRelations().clear();
231 getDocumentDAO().flush();
233 // Remove all documents of the study
234 for (org.splat.dal.bo.som.Document doc : docums) {
235 LOG.debug("Remove doc: " + doc.getTitle() + " ["
236 + doc.getReference() + "]" + " [" + doc.getRid() + "]");
237 getDocumentDAO().delete(doc);
243 * Get study by its reference.
246 * the study reference
247 * @return found study or null
249 @Transactional(readOnly = true)
250 public Study selectStudy(final String refid) {
251 Study result = getStudyDAO().findByCriteria(
252 Restrictions.eq("sid", refid));
253 loadWorkflow(result);
260 * @see org.splat.service.StudyService#createStudy(org.splat.dal.bo.som.Study.Properties)
263 public Study createStudy(final Study.Properties sprop)
264 throws MissedPropertyException, InvalidPropertyException,
265 MultiplyDefinedException {
266 sprop.setReference(getProjectSettings().getReferencePattern());
267 Study study = new Study(sprop);
269 buildReference(study);
270 getStudyDAO().create(study);
272 // IndexService lucin = getIndex();
274 // } catch (IOException error) {
275 // LOG.error("Unable to index the study '" + study.getIndex()
276 // + "', reason:", error);
277 // // Continue and try to index later
285 * @see org.splat.service.StudyService#addProjectContext(org.splat.dal.bo.som.Study, org.splat.dal.bo.som.SimulationContext.Properties)
288 public SimulationContext addProjectContext(final Study aStudy,
289 final SimulationContext.Properties cprop)
290 throws MissedPropertyException, InvalidPropertyException,
291 MultiplyDefinedException {
292 SimulationContext added = getStepService().addSimulationContext(
293 getProjectElementService().getFirstStep(aStudy), cprop);
301 * @see org.splat.service.StudyService#addProjectContext(org.splat.dal.bo.som.Study, org.splat.dal.bo.som.SimulationContext)
304 public SimulationContext addProjectContext(final Study aStudy,
305 final SimulationContext context) {
306 SimulationContext added = getStepService().addSimulationContext(
307 getProjectElementService().getFirstStep(aStudy), context);
315 * @see org.splat.service.StudyService#addContributor(org.splat.dal.bo.som.Study, org.splat.dal.bo.kernel.User)
318 public boolean addContributor(final Study aStudy, final User user) {
319 List<User> contributors = getModifiableContributors(aStudy); // Initializes contributor
321 if (contributors.contains(user)) {
325 // Remove user from readers
327 List<UserDTO> readers = getReaders(aStudy.getIndex());
328 for (UserDTO reader : readers) {
329 if (reader.getIndex() == user.getIndex()) {
330 // user must be the actual user in the relationship object in the aStudy object for this to work
331 aStudy.removeRelation(ReaderRelation.class, user);
334 } catch (InvalidParameterException e) {
335 LOG.error(e.getMessage(), e);
338 boolean absent = getModifiableActors(aStudy).add(user); // User may already be a reviewer or an approver
340 aStudy.addRelation(new ContributorRelation(aStudy, user));
342 update(aStudy); // Else, useless to re-index the study
350 * @see org.splat.service.StudyService#update(org.splat.dal.bo.som.Study, org.splat.dal.bo.som.Study.Properties)
353 public boolean update(final Study aStudy, final Properties sprop)
354 throws InvalidPropertyException {
355 if (sprop.getTitle() != null) {
356 aStudy.setTitle(sprop.getTitle());
358 if (sprop.getSummary() != null) {
359 aStudy.setAttribute(new DescriptionAttribute(aStudy, sprop
362 // TODO: To be completed
363 return update(aStudy);
367 * Check if the document is published in the study.
373 * @return true if the document is published in the study
376 * private boolean publishes(final Study aStudy, final Document doc) { if (!aStudy.publishes(doc)) { Scenario[] scene =
377 * aStudy.getScenarii(); for (int i = 0; i < scene.length; i++) { if (scene[i].publishes(doc)) { return true; } } } return false; }
382 * @see org.splat.service.StudyService#removeContributor(org.splat.dal.bo.som.Study, org.splat.dal.bo.kernel.User[])
385 public boolean removeContributor(final Study aStudy, final User... users) {
386 List<User> contributors = getModifiableContributors(aStudy); // Initializes contributor
387 Boolean done = false;
388 for (User user : users) {
389 if (contributors.contains(user)) {
390 aStudy.removeRelation(ContributorRelation.class, user);
391 contributors.remove(user);
404 * @see org.splat.service.StudyService#removeProjectContext(org.splat.dal.bo.som.Study, org.splat.dal.bo.som.SimulationContext)
407 public boolean removeProjectContext(final Study aStudy,
408 final SimulationContext context) {
409 boolean done = getStepService().removeSimulationContext(
410 getProjectElementService().getFirstStep(aStudy), context);
418 * @see org.splat.service.StudyService#setValidationCycle(org.splat.dal.bo.som.Study, org.splat.dal.bo.som.DocumentType,
419 * org.splat.dal.bo.som.ValidationCycle.Properties)
422 public void setValidationCycle(final Study aStudyDTO,
423 final DocumentType type, final ValidationCycle.Properties vprop) {
424 Map<String, ValidationCycle> validactor = aStudyDTO
425 .getValidationCycles();
426 if (validactor == null) {
427 setShortCuts(aStudyDTO); // Initializes validactor and actor
430 Study aStudy = selectStudy(aStudyDTO.getIndex());
432 String cname = type.getName();
433 ValidationCycle cycle = validactor.get(cname);
435 if (cycle != null && cycle.isAssigned()) {
436 resetActors(cycle, vprop);
439 cycle = new ValidationCycle(aStudy, vprop.setDocumentType(type));
441 getValidationCycleDAO().create(cycle); // RKV
443 ValidationCycleRelation link = cycle.getContext();
444 aStudy.addRelation(link);
445 getValidationCycleDAO().flush();
446 aStudyDTO.getAllRelations().add(link); // RKV
448 validactor.put(cname, link.getTo()); // Replaces the cycle if exists as default,
449 } catch (BusinessException error) {
450 LOG.error("Unable to create validation cycle, reason:", error);
454 resetActorsShortCut(aStudyDTO);
455 update(aStudy); // Re-index the study, just in case
461 * @see org.splat.service.StudyService#generateLocalIndex(org.splat.dal.bo.som.Study)
464 public int generateLocalIndex(final Study aStudy) {
465 aStudy.setLastLocalIndex(aStudy.getLastLocalIndex() + 1);
466 return aStudy.getLastLocalIndex();
470 * Promotes this study from In-Work to In-Draft then In-Check and APPROVED <BR>
471 * states. This function takes into account statuses of final result<BR>
472 * documents of the study.
476 * @return true if the promotion succeeded.
479 public boolean promote(final Study aStudy) {
481 ValidationCycle cycle = getValidationCycleOf(aStudy,
482 getStudyResultType(aStudy));
484 if (aStudy.getProgressState() == ProgressState.inWORK
485 && cycle.enables(ValidationStep.REVIEW)
486 && canBePromoted(aStudy)) {
487 aStudy.setProgressState(ProgressState.inDRAFT);
488 } else if (((!cycle.enables(ValidationStep.REVIEW) && ((aStudy
489 .getProgressState() == ProgressState.inWORK) || (aStudy
490 .getProgressState() == ProgressState.inDRAFT))) || (cycle
491 .enables(ValidationStep.REVIEW) && (aStudy.getProgressState() == ProgressState.inDRAFT)))
492 && canBeReviewed(aStudy)) {
493 aStudy.setProgressState(ProgressState.inCHECK);
494 Revision myvers = new Revision(aStudy.getVersion());
495 if (myvers.isMinor()) {
496 aStudy.setVersion(myvers.incrementAs(aStudy.getProgressState())
499 } else if (aStudy.getProgressState() == ProgressState.inCHECK
500 && canBeApproved(aStudy)) {
501 aStudy.setProgressState(ProgressState.APPROVED);
502 updateKnowledgeElementsState(aStudy);
507 res = update(aStudy);
514 * Demotes this study from In-Check or In-Draft to In-Work states.
518 * @return true if the demotion succeeded.
521 public boolean demote(final Study aStudy) {
524 if (aStudy.getProgressState() == ProgressState.inCHECK
525 || aStudy.getProgressState() == ProgressState.inDRAFT) {
526 aStudy.setProgressState(ProgressState.inWORK);
527 res = update(aStudy);
535 * Moves this study from the Private to the Public area of the repository.
539 * @return true if the move succeeded.
543 public boolean moveToPublic(final Study aStudy) {
544 boolean isOk = false;
545 if (aStudy.getVisibility() == Visibility.PRIVATE) {
546 aStudy.setVisibility(Visibility.PUBLIC);
547 if (update(aStudy)) {
548 isOk = updateKnowledgeElementsState(aStudy); // If fails, the database roll-back is under responsibility of the caller
555 * Moves this study from the Public to the Private area of the repository.
559 * @return true if the move succeeded.
562 public boolean moveToPrivate(final Study aStudy) {
563 boolean isOk = false;
564 if (aStudy.getVisibility() == Visibility.PUBLIC) {
565 aStudy.setVisibility(Visibility.PRIVATE);
566 if (update(aStudy)) {
567 isOk = updateKnowledgeElementsState(aStudy); // If fails, the database roll-back is under responsibility of the caller
574 * Update a study in the database.
577 * the study to update
578 * @return true if the study is updated successfully
580 private boolean update(final Study aStudy) {
581 boolean isOk = false;
583 getStudyDAO().merge(aStudy); // Update of relational base
584 setShortCuts(aStudy); // RKV: initialize transient actors set
585 // RKV: getIndex().update(aStudy); // Update of Lucene index
587 } catch (Exception e) {
588 LOG.error("STD-000001", e, aStudy.getIndex(), e.getMessage());
594 * Build reference for the study. The reference of the study is stored as a new reference pattern (IDBuilder).
598 * @return true if reference building is succeded
601 private boolean buildReference(final Study aStudy) {
602 String pattern = aStudy.getReference(); // The study being supposed just created, its reference is the reference pattern
603 IDBuilder tool = selectIDBuilder(aStudy.getDate());
605 tool = new IDBuilder(aStudy.getDate());
606 getIDBuilderDAO().create(tool);
608 aStudy.setReference(buildReference(tool, pattern, aStudy));
613 * Build reference for the study. The reference of the study is stored as a new reference pattern (IDBuilder).
620 * the reference pattern
621 * @return true if reference building is succeded
624 public String buildReference(final IDBuilder aBuilder,
625 final String pattern, final Study study) {
626 char[] format = pattern.toCharArray();
627 char[] ref = new char[80]; // Better evaluate the length of the generated string
628 int next = aBuilder.getBase() + 1;
631 for (int i = 0; i < format.length; i++) {
633 // Insertion of attribute values
634 if (format[i] == '%') {
637 if (format[i] == 'y') { // Insertion of year in format 2 (e.g. 09) or 4 (e.g. 2009) digits
639 while (format[i] == 'y') {
641 if (i == format.length) {
645 SimpleDateFormat tostring = new SimpleDateFormat("yyyy"); // RKV: NOPMD: TODO: Use locale here?
646 String year = tostring.format(study.getDate());
647 year = year.substring(4 - (i - n), 4); // 4-(i-n) must be equal to either 0 or 2
648 for (int j = 0; j < year.length(); j++) {
649 ref[count] = year.charAt(j);
652 i -= 1; // Back to the last 'y' character
653 } else if (format[i] == '0') { // Insertion of the index
655 while (format[i] == '0') {
657 if (i == format.length) {
661 DecimalFormat tostring = new DecimalFormat(pattern
663 String number = tostring.format(next);
664 for (int j = 0; j < number.length(); j++) {
665 ref[count] = number.charAt(j);
668 i -= 1; // Back to the last '0' character
670 // Keep the character
672 ref[count] = format[i];
676 // Incrementation of the number of study
677 aBuilder.setBase(next);
678 getIDBuilderDAO().update(aBuilder);
679 return String.copyValueOf(ref, 0, count);
683 * Find an id builder by date.
687 * @return found id builder
689 private IDBuilder selectIDBuilder(final Date date) {
690 Calendar aDate = Calendar.getInstance();
692 return getIDBuilderDAO().findByCriteria(
693 Restrictions.eq("cycle", aDate.get(Calendar.YEAR)));
697 * Fill transient collection ModifiableActors of the study.
702 private void resetActorsShortCut(final Study aStudy) {
703 getModifiableActors(aStudy).clear();
704 // Get all actors involved in validation cycles
705 for (Iterator<ValidationCycle> i = aStudy.getValidationCycles()
706 .values().iterator(); i.hasNext();) {
707 ValidationCycle cycle = i.next();
708 User[] user = cycle.getAllActors();
709 for (int j = 0; j < user.length; j++) {
710 getModifiableActors(aStudy).add(user[j]);
713 // Get all other actors
714 for (Iterator<Relation> i = aStudy.getAllRelations().iterator(); i
716 Relation link = i.next();
717 Class<?> kindof = link.getClass().getSuperclass();
718 if (!kindof.equals(ActorRelation.class)) {
721 getModifiableActors(aStudy).add(((ActorRelation) link).getTo());
726 * Update knowledge elements states.
730 * @return true if succeeded
732 private boolean updateKnowledgeElementsState(final Study aStudy) {
733 for (Scenario scenario : aStudy.getScenariiList()) {
734 for (KnowledgeElement element : scenario.getAllKnowledgeElements()) {
735 element.setProgressState(aStudy.getProgressState());
742 * Get lucene index service. Create a lucene index if it does not exist.
744 * @return index service
745 * @throws IOException
746 * if error occurs during lucene index creation
748 private IndexService getIndex() throws IOException {
749 IndexService lucin = getIndexService();
750 if (IndexWriter.isLocked(FSDirectory.open(getRepositoryService()
751 .getRepositoryIndexDirectory()))) {
752 IndexWriter.unlock(FSDirectory.open(getRepositoryService()
753 .getRepositoryIndexDirectory()));
755 if (!lucin.exists()) {
756 lucin.create(); // Happens when re-indexing all studies
762 * Create a new validation cycle for documents of the given study.
767 * the cycle description
768 * @return the new validation cycle
770 protected ValidationCycle createValidationCycle(
772 final ProjectSettingsServiceImpl.ProjectSettingsValidationCycle cycle) {
773 Actor[] actype = cycle.getActorTypes();
774 User.Properties uprop = new User.Properties();
776 ValidationCycle aValidationCycle = new ValidationCycle();
777 aValidationCycle.setDocumentType(getDocumentTypeService().selectType(
778 cycle.getName())); // Null in case of default validation cycle
779 // context = new ValidationCycleRelation(from, vprop);
780 // RKV aValidationCycle.context = null; // Validation cycle defined in the workflow
781 for (int i = 0; i < actype.length; i++) {
783 if (actype[i] != null) {
785 if (actype[i] == Actor.manager) {
786 actor = from.getAuthor();
787 } else if (actype[i] == Actor.Nx1) {
788 List<User> manager = getUserService().selectUsersWhere(
789 uprop.setOrganizationName("Nx1"));
790 if (manager.size() == 1) {
791 actor = manager.get(0);
793 } else if (actype[i] == Actor.Nx2) {
794 List<User> manager = getUserService().selectUsersWhere(
795 uprop.setOrganizationName("Nx2"));
796 if (manager.size() == 1) {
797 actor = manager.get(0);
799 } else { /* Actor.customer */
800 actor = from.getAuthor();
801 // TODO: Get the customer of the study, if exists
803 } catch (Exception e) { // Should not happen
808 aValidationCycle.setReviewer(actor);
810 aValidationCycle.setApprover(actor);
812 aValidationCycle.setSignatory(actor);
815 return aValidationCycle;
819 * Remove a validation step from the validation cycle.
821 * @param aValidationCycle
822 * the validation cycle
824 * the validation step to remove
827 protected void remove(final ValidationCycle aValidationCycle,
828 final ValidationStep step) {
829 if (step == ValidationStep.REVIEW) {
830 aValidationCycle.setReviewer(null);
831 } else if (step == ValidationStep.APPROVAL) {
832 aValidationCycle.setApprover(null);
833 } else if (step == ValidationStep.ACCEPTANCE
834 || step == ValidationStep.REFUSAL) {
835 aValidationCycle.setSignatory(null);
837 if (aValidationCycle.isSaved()) {
838 getValidationCycleDAO().update(aValidationCycle);
843 * Reset actors for the validation cycle.
845 * @param aValidationCycle
846 * the validation cycle to update
848 * new validation cycle properties containing new actors
851 public void resetActors(final ValidationCycle aValidationCycle,
852 final ValidationCycle.Properties vprop) {
853 aValidationCycle.setPublisher(vprop.getPublisher()); // May be null
854 aValidationCycle.setReviewer(vprop.getReviewer()); // May be null
855 aValidationCycle.setApprover(vprop.getApprover()); // May be null
856 aValidationCycle.setSignatory(vprop.getSignatory()); // May be null
857 if (aValidationCycle.isSaved()) {
858 getValidationCycleDAO().merge(aValidationCycle);
863 * Set actor for the given validation cycle and validation step.
865 * @param aValidationCycle
866 * the validation cycle
868 * the validation step
873 protected void setActor(final ValidationCycle aValidationCycle,
874 final ValidationStep step, final User actor) {
875 if (step == ValidationStep.PROMOTION) {
876 aValidationCycle.setPublisher(actor);
877 } else if (step == ValidationStep.REVIEW) {
878 aValidationCycle.setReviewer(actor);
879 } else if (step == ValidationStep.APPROVAL) {
880 aValidationCycle.setApprover(actor);
881 } else if (step == ValidationStep.ACCEPTANCE
882 || step == ValidationStep.REFUSAL) {
883 aValidationCycle.setSignatory(actor);
885 if (aValidationCycle.isSaved()) {
886 getValidationCycleDAO().update(aValidationCycle);
891 * Returns all actors of this study other than the author, including contributors, reviewers and approvers.
895 * @return the actors of this study
896 * @see #hasActor(User)
898 public Set<User> getActors(final Study aStudy) {
899 if (aStudy.getActor() == null) {
900 setShortCuts(aStudy);
902 return Collections.unmodifiableSet(aStudy.getActor());
906 * Returns all actors of this study other than the author, including contributors, reviewers and approvers.
910 * @return the modifiable set of actors of this study
911 * @see #hasActor(User)
913 public Set<User> getModifiableActors(final Study aStudy) {
914 if (aStudy.getActor() == null) {
915 setShortCuts(aStudy);
917 return aStudy.getActor();
921 * Returns unmodifiable initialized transient list of contributors of this study.
925 * @return the unmodifiable not null transient list of contributors of this study
927 public List<User> getContributors(final Study aStudy) {
928 if (aStudy.getContributor() == null) {
929 setShortCuts(aStudy);
931 return Collections.unmodifiableList(aStudy.getContributor()); // May be empty
935 * Returns modifiable initialized transient list of contributors of this study.
939 * @return the modifiable not null transient list of contributors of this study
941 public List<User> getModifiableContributors(final Study aStudy) {
942 if (aStudy.getContributor() == null) {
943 setShortCuts(aStudy);
945 return aStudy.getContributor(); // May be empty
949 * Returns the validation cycle of the given document type.
954 * the document type being subject of validation
955 * @return the validation cycle of the document, or null if not defined.
957 public ValidationCycle getValidationCycleOf(final Study aStudy,
958 final DocumentType type) {
959 ValidationCycle result = null;
960 if (aStudy != null) {
961 if (aStudy.getValidationCycles() == null
962 || aStudy.getValidationCycles().isEmpty()) {
963 setShortCuts(aStudy);
966 result = aStudy.getValidationCycles().get(type.getName());
969 if ((result == null) && (aStudy != null)) {
970 if ((type == null) || type.isStepResult()) {
971 result = aStudy.getValidationCycles().get("default"); // "default" validation cycle defined in the configuration, if exist
973 if (result == null) {
974 result = aStudy.getValidationCycles().get("built-in");
981 * Checks if the given user is actor of this study. Actors include contributors, reviewers and approvers.
986 * the user to look for
987 * @return true if the given user is actor of this study.
990 public boolean hasActor(final Study aStudy, final User user) {
994 for (Iterator<User> i = getActors(aStudy).iterator(); i.hasNext();) {
995 User involved = i.next();
996 if (involved.equals(user)) {
1004 * Checks if the given user participates to this study. The Study staff includes the author and contributors.
1009 * the user to look for
1010 * @return true if the given user is actor of this study.
1011 * @see #getContributors()
1013 public boolean isStaffedBy(final Study aStudy, final User user) {
1017 if (aStudy == null) {
1020 if (aStudy.getAuthor() == null) {
1023 if (aStudy.getAuthor().equals(user)) {
1026 for (Iterator<User> i = getContributors(aStudy).iterator(); i.hasNext();) {
1027 if (i.next().equals(user)) {
1035 * Initialize shortcuts of the study as its transient collections.
1040 public void loadWorkflow(final Study aStudy) {
1041 setShortCuts(aStudy);
1045 * Initialize shortcuts of the study as its transient collections.
1050 public void setShortCuts(final Study aStudy) {
1051 aStudy.getContributor().clear();
1052 aStudy.getValidationCycles().clear();
1053 aStudy.getActor().clear();
1055 // Get the contributors
1056 for (Iterator<Relation> i = aStudy.getRelations(
1057 ContributorRelation.class).iterator(); i.hasNext();) {
1058 ContributorRelation link = (ContributorRelation) i.next();
1059 aStudy.getContributor().add(link.getTo());
1061 // Get the validation cycles specific to this study
1062 for (Iterator<Relation> i = aStudy.getRelations(
1063 ValidationCycleRelation.class).iterator(); i.hasNext();) {
1064 ValidationCycleRelation link = (ValidationCycleRelation) i.next();
1065 aStudy.getValidationCycles().put(link.getDocumentType().getName(),
1066 link.getTo()); // The associated document type is necessarily not null in this
1069 // Get the validation cycles coming from the configured workflow and not overridden in this study
1070 for (Iterator<ProjectSettingsServiceImpl.ProjectSettingsValidationCycle> i = getProjectSettings()
1071 .getAllValidationCycles().iterator(); i.hasNext();) {
1072 ProjectSettingsServiceImpl.ProjectSettingsValidationCycle cycle = i
1074 String type = cycle.getName();
1075 if (!aStudy.getValidationCycles().containsKey(type)) {
1076 aStudy.getValidationCycles().put(type,
1077 createValidationCycle(aStudy, cycle));
1080 // Get all corresponding actors
1081 for (Iterator<ValidationCycle> i = aStudy.getValidationCycles()
1082 .values().iterator(); i.hasNext();) {
1083 ValidationCycle cycle = i.next();
1084 User[] user = cycle.getAllActors();
1085 for (int j = 0; j < user.length; j++) {
1086 aStudy.getActor().add(user[j]);
1089 // Get all other actors
1090 for (Iterator<Relation> i = aStudy.getAllRelations().iterator(); i
1092 Relation link = i.next();
1093 Class<?> kindof = link.getClass().getSuperclass();
1094 if (!kindof.equals(ActorRelation.class)) {
1097 aStudy.getActor().add(((ActorRelation) link).getTo());
1105 * @see org.splat.service.StudyService#markStudyAsReference(org.splat.dal.bo.som.Study)
1108 public boolean markStudyAsReference(final Study aStudy) {
1109 boolean res = false;
1110 if (aStudy.getProgressState() == ProgressState.APPROVED) {
1111 aStudy.setMarkreference(1);
1112 aStudy.setProgressState(ProgressState.TEMPLATE);
1113 res = updateKnowledgeElementsState(aStudy);
1114 getStudyDAO().merge(aStudy);
1123 * @see org.splat.service.StudyService#removeStudyAsReference(org.splat.dal.bo.som.Study)
1126 public void removeStudyAsReference(final Study aStudy) {
1127 aStudy.setMarkreference(0);
1128 aStudy.setProgressState(ProgressState.APPROVED);
1129 updateKnowledgeElementsState(aStudy);
1130 getStudyDAO().merge(aStudy);
1136 * @see org.splat.service.StudyService#getDescription(java.lang.Long)
1139 @Transactional(readOnly = true)
1140 public String getDescription(final Long studyId)
1141 throws InvalidParameterException {
1142 if (studyId == null) {
1143 throw new InvalidParameterException(PARAM_STUDY_ID, "null");
1145 Study study = _studyDAO.get(studyId);
1146 if (study == null) {
1147 throw new InvalidParameterException(PARAM_STUDY_ID, studyId
1150 return study.getDescription();
1156 * @see org.splat.service.StudyService#setDescription(java.lang.Long, java.lang.String)
1159 public void setDescription(final Long studyId, final String descriptionText)
1160 throws InvalidParameterException {
1161 if (studyId == null) {
1162 throw new InvalidParameterException(PARAM_STUDY_ID, "null");
1164 Study study = _studyDAO.get(studyId);
1165 if (study == null) {
1166 throw new InvalidParameterException(PARAM_STUDY_ID, studyId
1169 study.setAttribute(new DescriptionAttribute(study, descriptionText));
1175 * @see org.splat.service.StudyService#removeStudyDescription(java.lang.Long)
1178 public boolean removeDescription(final Long studyId)
1179 throws InvalidParameterException {
1180 if (studyId == null) {
1181 throw new InvalidParameterException(PARAM_STUDY_ID, String
1184 Study study = _studyDAO.get(studyId);
1185 if (study == null) {
1186 throw new InvalidParameterException(PARAM_STUDY_ID, String
1189 return study.removeAttribute(study
1190 .getAttribute(DescriptionAttribute.class));
1196 * @see org.splat.service.StudyService#getReaders(long)
1198 @Transactional(readOnly = true)
1199 public List<UserDTO> getReaders(final long studyId)
1200 throws InvalidParameterException {
1201 Study aStudy = selectStudy(studyId);
1202 if (aStudy == null) {
1203 throw new InvalidParameterException(PARAM_STUDY_ID, String
1206 List<Relation> relations = aStudy.getRelations(ReaderRelation.class);
1207 List<UserDTO> result = new ArrayList<UserDTO>();
1208 for (Relation relation : relations) {
1209 result.add(BeanHelper.copyBean(relation.getTo(), UserDTO.class));
1211 return Collections.unmodifiableList(result);
1217 * @see org.splat.service.StudyService#addReader(long, long)
1220 public boolean addReader(final long studyId, final long userId)
1221 throws InvalidParameterException {
1222 Study aStudy = selectStudy(studyId);
1223 if (aStudy == null) {
1224 throw new InvalidParameterException(PARAM_STUDY_ID, String
1227 User user = _userService.selectUser(userId);
1229 throw new InvalidParameterException("userId", String
1233 for (Relation relation : aStudy.getRelations(ReaderRelation.class)) {
1234 if (user.equals(relation.getTo())) {
1238 aStudy.addRelation(new ReaderRelation(aStudy, user));
1246 * @see org.splat.service.StudyService#removeReader(long, long)
1249 public boolean removeReader(final long studyId, final long userId)
1250 throws InvalidParameterException {
1251 Study aStudy = selectStudy(studyId);
1252 if (aStudy == null) {
1253 throw new InvalidParameterException(PARAM_STUDY_ID, String
1256 User user = _userService.selectUser(userId);
1258 throw new InvalidParameterException("userId", String
1262 Relation relation = aStudy.removeRelation(ReaderRelation.class, user);
1264 return relation != null;
1268 * Get project settings.
1270 * @return Project settings service
1272 private ProjectSettingsService getProjectSettings() {
1273 return _projectSettings;
1277 * Set project settings service.
1279 * @param projectSettingsService
1280 * project settings service
1282 public void setProjectSettings(
1283 final ProjectSettingsService projectSettingsService) {
1284 _projectSettings = projectSettingsService;
1288 * Get the projectElementService.
1290 * @return the projectElementService
1292 public ProjectElementService getProjectElementService() {
1293 return _projectElementService;
1297 * Set the projectElementService.
1299 * @param projectElementService
1300 * the projectElementService to set
1302 public void setProjectElementService(
1303 final ProjectElementService projectElementService) {
1304 _projectElementService = projectElementService;
1308 * Get the stepService.
1310 * @return the stepService
1312 public StepService getStepService() {
1313 return _stepService;
1317 * Set the stepService.
1319 * @param stepService
1320 * the stepService to set
1322 public void setStepService(final StepService stepService) {
1323 _stepService = stepService;
1327 * Get the indexService.
1329 * @return the indexService
1331 public IndexService getIndexService() {
1332 return _indexService;
1336 * Set the indexService.
1338 * @param indexService
1339 * the indexService to set
1341 public void setIndexService(final IndexService indexService) {
1342 _indexService = indexService;
1348 * @return the studyDAO
1350 public StudyDAO getStudyDAO() {
1358 * the studyDAO to set
1360 public void setStudyDAO(final StudyDAO studyDAO) {
1361 _studyDAO = studyDAO;
1365 * Get the iDBuilderDAO.
1367 * @return the iDBuilderDAO
1369 public IDBuilderDAO getIDBuilderDAO() {
1370 return _iDBuilderDAO;
1374 * Set the iDBuilderDAO.
1377 * the iDBuilderDAO to set
1379 public void setIDBuilderDAO(final IDBuilderDAO builderDAO) {
1380 _iDBuilderDAO = builderDAO;
1384 * Get the scenarioDAO.
1386 * @return the scenarioDAO
1388 public ScenarioDAO getScenarioDAO() {
1389 return _scenarioDAO;
1393 * Set the scenarioDAO.
1395 * @param scenarioDAO
1396 * the scenarioDAO to set
1398 public void setScenarioDAO(final ScenarioDAO scenarioDAO) {
1399 _scenarioDAO = scenarioDAO;
1403 * Get the validationCycleDAO.
1405 * @return the validationCycleDAO
1407 public ValidationCycleDAO getValidationCycleDAO() {
1408 return _validationCycleDAO;
1412 * Set the validationCycleDAO.
1414 * @param validationCycleDAO
1415 * the validationCycleDAO to set
1417 public void setValidationCycleDAO(
1418 final ValidationCycleDAO validationCycleDAO) {
1419 _validationCycleDAO = validationCycleDAO;
1423 * Get the documentTypeService.
1425 * @return the documentTypeService
1427 public DocumentTypeService getDocumentTypeService() {
1428 return _documentTypeService;
1432 * Set the documentTypeService.
1434 * @param documentTypeService
1435 * the documentTypeService to set
1437 public void setDocumentTypeService(
1438 final DocumentTypeService documentTypeService) {
1439 _documentTypeService = documentTypeService;
1443 * Get the userService.
1445 * @return the userService
1447 public UserService getUserService() {
1448 return _userService;
1452 * Set the userService.
1454 * @param userService
1455 * the userService to set
1457 public void setUserService(final UserService userService) {
1458 _userService = userService;
1462 * Get the publicationDAO.
1464 * @return the publicationDAO
1466 public PublicationDAO getPublicationDAO() {
1467 return _publicationDAO;
1471 * Set the publicationDAO.
1473 * @param publicationDAO
1474 * the publicationDAO to set
1476 public void setPublicationDAO(final PublicationDAO publicationDAO) {
1477 _publicationDAO = publicationDAO;
1481 * Get the repositoryService.
1483 * @return the repositoryService
1485 public RepositoryService getRepositoryService() {
1486 return _repositoryService;
1490 * Set the repositoryService.
1492 * @param repositoryService
1493 * the repositoryService to set
1495 public void setRepositoryService(final RepositoryService repositoryService) {
1496 _repositoryService = repositoryService;
1500 * Get the documentDAO.
1502 * @return the documentDAO
1504 public DocumentDAO getDocumentDAO() {
1505 return _documentDAO;
1509 * Set the documentDAO.
1511 * @param documentDAO
1512 * the documentDAO to set
1514 public void setDocumentDAO(final DocumentDAO documentDAO) {
1515 _documentDAO = documentDAO;
1519 * Get the descriptionAttributeDAO.
1521 * @return the descriptionAttributeDAO
1523 public DescriptionAttributeDAO getDescriptionAttributeDAO() {
1524 return _descriptionAttributeDAO;
1528 * Set the descriptionAttributeDAO.
1530 * @param descriptionAttributeDAO
1531 * the descriptionAttributeDAO to set
1533 public void setDescriptionAttributeDAO(
1534 final DescriptionAttributeDAO descriptionAttributeDAO) {
1535 _descriptionAttributeDAO = descriptionAttributeDAO;
1539 * Get the usedByRelationDAO.
1541 * @return the usedByRelationDAO
1543 public UsedByRelationDAO getUsedByRelationDAO() {
1544 return _usedByRelationDAO;
1548 * Set the usedByRelationDAO.
1550 * @param usedByRelationDAO
1551 * the usedByRelationDAO to set
1553 public void setUsedByRelationDAO(final UsedByRelationDAO usedByRelationDAO) {
1554 _usedByRelationDAO = usedByRelationDAO;
1560 * @see org.splat.service.StudyService#getStudyResultType(org.splat.dal.bo.som.Study)
1563 @Transactional(readOnly = true)
1564 public DocumentType getStudyResultType(final Study study) {
1565 DetachedCriteria query = DetachedCriteria.forClass(DocumentType.class)
1566 .addOrder(Order.desc("result"));
1567 return getDocumentTypeDAO().getFirstResult(query);
1571 * Get the documentTypeDAO.
1573 * @return the documentTypeDAO
1575 public DocumentTypeDAO getDocumentTypeDAO() {
1576 return _documentTypeDAO;
1580 * Set the documentTypeDAO.
1582 * @param documentTypeDAO
1583 * the documentTypeDAO to set
1585 public void setDocumentTypeDAO(final DocumentTypeDAO documentTypeDAO) {
1586 _documentTypeDAO = documentTypeDAO;
1592 * @see org.splat.service.StudyService#canBeApproved(org.splat.dal.bo.som.Study)
1595 @Transactional(readOnly = true)
1596 public boolean canBeApproved(final Study study) {
1597 return resultDocsAtLeast(study, ProgressState.APPROVED);
1603 * @see org.splat.service.StudyService#canBePromoted(org.splat.dal.bo.som.Study)
1606 @Transactional(readOnly = true)
1607 public boolean canBePromoted(final Study study) {
1609 if (study.getProgressState().compareTo(ProgressState.inDRAFT) < 0) {
1610 res = resultDocsAtLeast(study, ProgressState.inDRAFT);
1612 res = canBeReviewed(study);
1620 * @see org.splat.service.StudyService#canBeReviewed(org.splat.dal.bo.som.Study)
1623 @Transactional(readOnly = true)
1624 public boolean canBeReviewed(final Study study) {
1625 return resultDocsAtLeast(study, ProgressState.inCHECK);
1629 * Check that all result documents of the study are at least in the given state.
1632 * the study to check
1634 * the minimal acceptable state
1635 * @return true if study result documents have acceptable states
1637 private boolean resultDocsAtLeast(final Study study,
1638 final ProgressState state) {
1640 // Check that all study result documents have the state APPROVED or more.
1641 for (Publication pub : getProjectElementService().getLastStep(study)
1642 .getResultDocuments()) {
1643 res = pub.getProgressState().compareTo(state) >= 0;