1 /*****************************************************************************
5 * Creation date 02.10.2012
6 * @author Author: Maria KRUCHININA
8 *****************************************************************************/
10 package org.splat.service;
12 import java.awt.Graphics2D;
14 import java.io.FileNotFoundException;
15 import java.io.FileOutputStream;
16 import java.io.IOException;
17 import java.text.DecimalFormat;
18 import java.text.SimpleDateFormat;
19 import java.util.Calendar;
20 import java.util.Collections;
21 import java.util.Date;
22 import java.util.Iterator;
23 import java.util.List;
25 import java.util.Scanner;
28 import org.apache.lucene.index.IndexWriter;
29 import org.apache.lucene.store.FSDirectory;
30 import org.hibernate.criterion.Restrictions;
31 import org.jfree.chart.ChartFactory;
32 import org.jfree.chart.JFreeChart;
33 import org.jfree.chart.plot.PlotOrientation;
34 import org.jfree.data.xy.XYSeries;
35 import org.jfree.data.xy.XYSeriesCollection;
36 import org.splat.common.properties.MessageKeyEnum;
37 import org.splat.dal.bo.kernel.Relation;
38 import org.splat.dal.bo.kernel.User;
39 import org.splat.dal.bo.som.ActorRelation;
40 import org.splat.dal.bo.som.ContributorRelation;
41 import org.splat.dal.bo.som.DescriptionAttribute;
42 import org.splat.dal.bo.som.DocumentType;
43 import org.splat.dal.bo.som.IDBuilder;
44 import org.splat.dal.bo.som.ProgressState;
45 import org.splat.dal.bo.som.Publication;
46 import org.splat.dal.bo.som.SimulationContext;
47 import org.splat.dal.bo.som.Study;
48 import org.splat.dal.bo.som.Study.Properties;
49 import org.splat.dal.bo.som.ValidationCycle;
50 import org.splat.dal.bo.som.ValidationCycle.Actor;
51 import org.splat.dal.bo.som.ValidationCycleRelation;
52 import org.splat.dal.bo.som.ValidationStep;
53 import org.splat.dal.bo.som.Visibility;
54 import org.splat.dal.dao.som.DescriptionAttributeDAO;
55 import org.splat.dal.dao.som.DocumentDAO;
56 import org.splat.dal.dao.som.IDBuilderDAO;
57 import org.splat.dal.dao.som.PublicationDAO;
58 import org.splat.dal.dao.som.ScenarioDAO;
59 import org.splat.dal.dao.som.StudyDAO;
60 import org.splat.dal.dao.som.ValidationCycleDAO;
61 import org.splat.exception.IncompatibleDataException;
62 import org.splat.exception.InvalidParameterException;
63 import org.splat.kernel.InvalidPropertyException;
64 import org.splat.kernel.MissedPropertyException;
65 import org.splat.kernel.MultiplyDefinedException;
66 import org.splat.log.AppLogger;
67 import org.splat.service.dto.DocToCompareDTO;
68 import org.splat.service.technical.IndexService;
69 import org.splat.service.technical.ProjectSettingsService;
70 import org.splat.service.technical.ProjectSettingsServiceImpl;
71 import org.splat.service.technical.RepositoryService;
72 import org.splat.som.Revision;
73 import org.springframework.transaction.annotation.Transactional;
75 import com.lowagie.text.Document;
76 import com.lowagie.text.DocumentException;
77 import com.lowagie.text.Rectangle;
78 import com.lowagie.text.pdf.DefaultFontMapper;
79 import com.lowagie.text.pdf.PdfContentByte;
80 import com.lowagie.text.pdf.PdfTemplate;
81 import com.lowagie.text.pdf.PdfWriter;
85 * This class defines all methods for creation, modification the study.
87 * @author Maria KRUCHININA
90 public class StudyServiceImpl implements StudyService {
93 * logger for the service.
95 public final static AppLogger LOG = AppLogger
96 .getLogger(StudyServiceImpl.class);
99 * Injected index service.
101 private IndexService _indexService;
104 * Injected step service.
106 private StepService _stepService;
109 * Injected project service.
111 private ProjectSettingsService _projectSettings;
114 * Injected project element service.
116 private ProjectElementService _projectElementService;
119 * Injected study DAO.
121 private StudyDAO _studyDAO;
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;
171 * @see org.splat.service.StudyService#selectStudy(long)
174 public Study selectStudy(final long index) {
175 Study result = getStudyDAO().get(index);
176 loadWorkflow(result);
182 * @see org.splat.service.StudyService#removeStudy(long)
185 public void removeStudy(final long index) {
186 Study study = getStudyDAO().get(index);
188 getStudyDAO().delete(study);
193 * Get study by its reference.
196 * the study reference
197 * @return found study or null
199 @Transactional(readOnly = true)
200 public Study selectStudy(final String refid) {
201 Study result = getStudyDAO().findByCriteria(
202 Restrictions.eq("sid", refid));
203 loadWorkflow(result);
210 * @see org.splat.service.StudyService#createStudy(org.splat.dal.bo.som.Study.Properties)
213 public Study createStudy(final Study.Properties sprop)
214 throws MissedPropertyException, InvalidPropertyException,
215 MultiplyDefinedException {
216 sprop.setReference(getProjectSettings().getReferencePattern());
217 Study study = new Study(sprop);
219 buildReference(study);
220 getStudyDAO().create(study);
222 // IndexService lucin = getIndex();
224 // } catch (IOException error) {
225 // LOG.error("Unable to index the study '" + study.getIndex()
226 // + "', reason:", error);
227 // // Continue and try to index later
235 * @see org.splat.service.StudyService#addProjectContext(org.splat.dal.bo.som.Study, org.splat.dal.bo.som.SimulationContext.Properties)
238 public SimulationContext addProjectContext(final Study aStudy,
239 final SimulationContext.Properties cprop)
240 throws MissedPropertyException, InvalidPropertyException,
241 MultiplyDefinedException {
242 SimulationContext added = getStepService().addSimulationContext(
243 getProjectElementService().getFirstStep(aStudy), cprop);
251 * @see org.splat.service.StudyService#addProjectContext(org.splat.dal.bo.som.Study, org.splat.dal.bo.som.SimulationContext)
254 public SimulationContext addProjectContext(final Study aStudy,
255 final SimulationContext context) {
256 SimulationContext added = getStepService().addSimulationContext(
257 getProjectElementService().getFirstStep(aStudy), context);
265 * @see org.splat.service.StudyService#addContributor(org.splat.dal.bo.som.Study, org.splat.dal.bo.kernel.User)
267 public boolean addContributor(final Study aStudy, final User user) {
268 List<User> contributor = getModifiableContributors(aStudy); // Initializes contributor
269 for (Iterator<User> i = contributor.iterator(); i.hasNext();) {
270 User present = i.next();
271 if (present.equals(user)) {
275 boolean absent = getModifiableActors(aStudy).add(user); // User may already be a reviewer or an approver
277 aStudy.addRelation(new ContributorRelation(aStudy, user));
279 update(aStudy); // Else, useless to re-index the study
281 contributor.add(user);
286 * Moves this study from the Public to the Reference area of the repository. For being moved to the Reference area, the study must
287 * previously be approved.
291 * @return true if the move succeeded.
292 * @see #moveToPublic()
294 * @see Publication#approve(Date)
296 public boolean moveToReference(final Study aStudy) {
297 if (aStudy.getProgressState() != ProgressState.APPROVED) {
300 if (aStudy.getVisibility() != Visibility.PUBLIC) {
304 aStudy.setVisibility(Visibility.REFERENCE);
305 if (update(aStudy)) {
306 return updateKnowledgeElementsIndex(aStudy); // If fails, the database roll-back is under responsibility of the caller
314 * @see org.splat.service.StudyService#update(org.splat.dal.bo.som.Study, org.splat.dal.bo.som.Study.Properties)
316 public boolean update(final Study aStudy, final Properties sprop)
317 throws InvalidPropertyException {
318 if (sprop.getTitle() != null) {
319 aStudy.setTitle(sprop.getTitle());
321 if (sprop.getSummary() != null) {
322 aStudy.setAttribute(new DescriptionAttribute(aStudy, sprop
325 // TODO: To be completed
326 return update(aStudy);
330 * Check if the document is published in the study.
336 * @return true if the document is published in the study
338 /* private boolean publishes(final Study aStudy, final Document doc) {
339 if (!aStudy.publishes(doc)) {
340 Scenario[] scene = aStudy.getScenarii();
341 for (int i = 0; i < scene.length; i++) {
342 if (scene[i].publishes(doc)) {
353 * @see org.splat.service.StudyService#removeContributor(org.splat.dal.bo.som.Study, org.splat.dal.bo.kernel.User[])
355 public boolean removeContributor(final Study aStudy, final User... users) {
356 List<User> contributor = getModifiableContributors(aStudy); // Initializes contributor
357 Boolean done = false;
358 for (int i = 0; i < users.length; i++) {
359 User user = users[i];
360 for (Iterator<User> j = contributor.iterator(); j.hasNext();) {
361 User present = j.next();
362 if (!present.equals(user)) {
366 aStudy.removeRelation(ContributorRelation.class, user);
367 j.remove(); // Updates the contributor shortcut
381 * @see org.splat.service.StudyService#removeProjectContext(org.splat.dal.bo.som.Study, org.splat.dal.bo.som.SimulationContext)
383 public boolean removeProjectContext(final Study aStudy,
384 final SimulationContext context) {
385 boolean done = getStepService().removeSimulationContext(
386 getProjectElementService().getFirstStep(aStudy), context);
394 * @see org.splat.service.StudyService#setValidationCycle(org.splat.dal.bo.som.Study, org.splat.dal.bo.som.DocumentType,
395 * org.splat.dal.bo.som.ValidationCycle.Properties)
398 public void setValidationCycle(final Study aStudyDTO, final DocumentType type,
399 final ValidationCycle.Properties vprop) {
400 Map<String, ValidationCycle> validactor = aStudyDTO.getValidationCycles();
401 if (validactor == null) {
402 setShortCuts(aStudyDTO); // Initializes validactor and actor
405 Study aStudy = selectStudy(aStudyDTO.getIndex());
407 String cname = type.getName();
408 ValidationCycle cycle = validactor.get(cname);
410 if (cycle != null && cycle.isAssigned()) {
411 resetActors(cycle, vprop);
414 cycle = new ValidationCycle(aStudy, vprop.setDocumentType(type));
416 getValidationCycleDAO().create(cycle); // RKV
418 ValidationCycleRelation link = cycle.getContext();
419 aStudy.addRelation(link);
420 aStudyDTO.getAllRelations().add(link); // RKV
422 validactor.put(cname, link.getTo()); // Replaces the cycle if exists as default,
423 } catch (Exception error) {
424 LOG.error("Unable to re-index Knowledge Elements, reason:",
429 resetActorsShortCut(aStudyDTO);
430 update(aStudy); // Re-index the study, just in case
434 * Demotes this study from In-Check to In-Draft then In-Work states. This function is called internally when demoting the final result
435 * document of the study.
439 * @return true if the demotion succeeded.
441 public boolean demote(final Study aStudy) {
442 if (aStudy.getProgressState() == ProgressState.inCHECK) {
443 aStudy.setProgressState(ProgressState.inDRAFT);
444 } else if (aStudy.getProgressState() == ProgressState.inDRAFT) {
445 aStudy.setProgressState(ProgressState.inWORK);
449 return update(aStudy);
455 * @see org.splat.service.StudyService#generateLocalIndex(org.splat.dal.bo.som.Study)
458 public int generateLocalIndex(final Study aStudy) {
459 aStudy.setLastLocalIndex(aStudy.getLastLocalIndex() + 1);
460 return aStudy.getLastLocalIndex();
464 * Promotes this study from In-Work to In-Draft then In-Check and APPROVED states. This function is called internally when promoting the
465 * final result document of the study.
469 * @return true if the demotion succeeded.
472 public boolean promote(final Study aStudy) {
473 if (aStudy.getProgressState() == ProgressState.inWORK) {
474 aStudy.setProgressState(ProgressState.inDRAFT);
475 } else if (aStudy.getProgressState() == ProgressState.inDRAFT) {
476 aStudy.setProgressState(ProgressState.inCHECK);
477 Revision myvers = new Revision(aStudy.getVersion());
478 if (myvers.isMinor()) {
479 aStudy.setVersion(myvers.incrementAs(aStudy.getProgressState())
482 } else if (aStudy.getProgressState() == ProgressState.inCHECK) {
483 aStudy.setProgressState(ProgressState.APPROVED);
488 return update(aStudy);
492 * Moves this study from the Private to the Public area of the repository.
496 * @return true if the move succeeded.
500 public boolean moveToPublic(final Study aStudy) {
501 boolean isOk = false;
502 if (aStudy.getVisibility() == Visibility.PRIVATE) {
503 aStudy.setVisibility(Visibility.PUBLIC);
504 if (update(aStudy)) {
505 isOk = updateKnowledgeElementsIndex(aStudy); // If fails, the database roll-back is under responsibility of the caller
512 * Moves this study from the Public to the Private area of the repository.
516 * @return true if the move succeeded.
519 public boolean moveToPrivate(final Study aStudy) {
520 boolean isOk = false;
521 if (aStudy.getVisibility() == Visibility.PUBLIC) {
522 aStudy.setVisibility(Visibility.PRIVATE);
523 if (update(aStudy)) {
524 isOk = updateKnowledgeElementsIndex(aStudy); // If fails, the database roll-back is under responsibility of the caller
531 * Update a study in the database.
534 * the study to update
535 * @return true if the study is updated successfully
538 private boolean update(final Study aStudy) {
539 boolean isOk = false;
541 getStudyDAO().merge(aStudy); // Update of relational base
542 setShortCuts(aStudy); // RKV: initialize transient actors set
543 //RKV: getIndex().update(aStudy); // Update of Lucene index
545 } catch (Exception e) {
546 LOG.error("STD-000001", e, aStudy.getIndex(), e.getMessage());
552 * Build reference for the study. The reference of the study is stored as a new reference pattern (IDBuilder).
556 * @return true if reference building is succeded
559 private boolean buildReference(final Study aStudy) {
560 String pattern = aStudy.getReference(); // The study being supposed just created, its reference is the reference pattern
561 IDBuilder tool = selectIDBuilder(aStudy.getDate());
563 tool = new IDBuilder(aStudy.getDate());
564 getIDBuilderDAO().create(tool);
566 aStudy.setReference(buildReference(tool, pattern, aStudy));
571 * Build reference for the study. The reference of the study is stored as a new reference pattern (IDBuilder).
578 * the reference pattern
579 * @return true if reference building is succeded
582 public String buildReference(final IDBuilder aBuilder,
583 final String pattern, final Study study) {
584 char[] format = pattern.toCharArray();
585 char[] ref = new char[80]; // Better evaluate the length of the generated string
586 int next = aBuilder.getBase() + 1;
589 for (int i = 0; i < format.length; i++) {
591 // Insertion of attribute values
592 if (format[i] == '%') {
595 if (format[i] == 'y') { // Insertion of year in format 2 (e.g. 09) or 4 (e.g. 2009) digits
597 while (format[i] == 'y') {
599 if (i == format.length) {
603 SimpleDateFormat tostring = new SimpleDateFormat("yyyy"); //RKV: NOPMD: TODO: Use locale here?
604 String year = tostring.format(study.getDate());
605 year = year.substring(4 - (i - n), 4); // 4-(i-n) must be equal to either 0 or 2
606 for (int j = 0; j < year.length(); j++) {
607 ref[count] = year.charAt(j);
610 i -= 1; // Back to the last 'y' character
611 } else if (format[i] == '0') { // Insertion of the index
613 while (format[i] == '0') {
615 if (i == format.length) {
619 DecimalFormat tostring = new DecimalFormat(pattern
621 String number = tostring.format(next);
622 for (int j = 0; j < number.length(); j++) {
623 ref[count] = number.charAt(j);
626 i -= 1; // Back to the last '0' character
628 // Keep the character
630 ref[count] = format[i];
634 // Incrementation of the number of study
635 aBuilder.setBase(next);
636 getIDBuilderDAO().update(aBuilder);
637 return String.copyValueOf(ref, 0, count);
641 * Find an id builder by date.
645 * @return found id builder
647 private IDBuilder selectIDBuilder(final Date date) {
648 Calendar aDate = Calendar.getInstance();
650 return getIDBuilderDAO().findByCriteria(
651 Restrictions.eq("cycle", aDate.get(Calendar.YEAR)));
655 * Fill transient collection ModifiableActors of the study.
660 private void resetActorsShortCut(final Study aStudy) {
661 getModifiableActors(aStudy).clear();
662 // Get all actors involved in validation cycles
663 for (Iterator<ValidationCycle> i = aStudy.getValidationCycles()
664 .values().iterator(); i.hasNext();) {
665 ValidationCycle cycle = i.next();
666 User[] user = cycle.getAllActors();
667 for (int j = 0; j < user.length; j++) {
668 getModifiableActors(aStudy).add(user[j]);
671 // Get all other actors
672 for (Iterator<Relation> i = aStudy.getAllRelations().iterator(); i
674 Relation link = i.next();
675 Class<?> kindof = link.getClass().getSuperclass();
676 if (!kindof.equals(ActorRelation.class)) {
679 getModifiableActors(aStudy).add(((ActorRelation) link).getTo());
684 * Update lucene index for the study knowledge elements.
688 * @return true if reindexing succeeded
690 private boolean updateKnowledgeElementsIndex(final Study aStudy) {
691 // boolean isOk = false;
693 // IndexService lucin = getIndex();
695 // for (Iterator<Scenario> i = aStudy.getScenariiList().iterator(); i
697 // Scenario scene = i.next();
698 // for (Iterator<KnowledgeElement> j = scene
699 // .getAllKnowledgeElements().iterator(); j.hasNext();) {
700 // KnowledgeElement kelm = j.next();
701 // lucin.update(kelm);
705 // } catch (Exception error) {
706 // LOG.error("Unable to re-index Knowledge Elements, reason:",
714 * Get lucene index service. Create a lucene index if it does not exist.
716 * @return index service
717 * @throws IOException
718 * if error occurs during lucene index creation
720 private IndexService getIndex() throws IOException {
721 IndexService lucin = getIndexService();
722 if (IndexWriter.isLocked(FSDirectory.open(getRepositoryService()
723 .getRepositoryIndexDirectory()))) {
724 IndexWriter.unlock(FSDirectory.open(getRepositoryService()
725 .getRepositoryIndexDirectory()));
727 if (!lucin.exists()) {
728 lucin.create(); // Happens when re-indexing all studies
734 * Create a new validation cycle for documents of the given study.
739 * the cycle description
740 * @return the new validation cycle
742 protected ValidationCycle createValidationCycle(
744 final ProjectSettingsServiceImpl.ProjectSettingsValidationCycle cycle) {
745 Actor[] actype = cycle.getActorTypes();
746 User.Properties uprop = new User.Properties();
748 ValidationCycle aValidationCycle = new ValidationCycle();
749 aValidationCycle.setDocumentType(getDocumentTypeService().selectType(
750 cycle.getName())); // Null in case of default validation cycle
751 // context = new ValidationCycleRelation(from, vprop);
752 // RKV aValidationCycle.context = null; // Validation cycle defined in the workflow
753 for (int i = 0; i < actype.length; i++) {
755 if (actype[i] != null) {
757 if (actype[i] == Actor.manager) {
758 actor = from.getAuthor();
759 } else if (actype[i] == Actor.Nx1) {
760 List<User> manager = getUserService().selectUsersWhere(
761 uprop.setOrganizationName("Nx1"));
762 if (manager.size() == 1) {
763 actor = manager.get(0);
765 } else if (actype[i] == Actor.Nx2) {
766 List<User> manager = getUserService().selectUsersWhere(
767 uprop.setOrganizationName("Nx2"));
768 if (manager.size() == 1) {
769 actor = manager.get(0);
771 } else { /* Actor.customer */
772 actor = from.getAuthor();
773 // TODO: Get the customer of the study, if exists
775 } catch (Exception e) { // Should not happen
780 aValidationCycle.setReviewer(actor);
782 aValidationCycle.setApprover(actor);
784 aValidationCycle.setSignatory(actor);
787 return aValidationCycle;
791 * Remove a validation step from the validation cycle.
793 * @param aValidationCycle
794 * the validation cycle
796 * the validation step to remove
799 protected void remove(final ValidationCycle aValidationCycle,
800 final ValidationStep step) {
801 if (step == ValidationStep.REVIEW) {
802 aValidationCycle.setReviewer(null);
803 } else if (step == ValidationStep.APPROVAL) {
804 aValidationCycle.setApprover(null);
805 } else if (step == ValidationStep.ACCEPTANCE
806 || step == ValidationStep.REFUSAL) {
807 aValidationCycle.setSignatory(null);
809 if (aValidationCycle.isSaved()) {
810 getValidationCycleDAO().update(aValidationCycle);
815 * Reset actors for the validation cycle.
817 * @param aValidationCycle
818 * the validation cycle to update
820 * new validation cycle properties containing new actors
823 public void resetActors(final ValidationCycle aValidationCycle,
824 final ValidationCycle.Properties vprop) {
825 aValidationCycle.setPublisher(vprop.getPublisher()); // May be null
826 aValidationCycle.setReviewer(vprop.getReviewer()); // May be null
827 aValidationCycle.setApprover(vprop.getApprover()); // May be null
828 aValidationCycle.setSignatory(vprop.getSignatory()); // May be null
829 if (aValidationCycle.isSaved()) {
830 getValidationCycleDAO().merge(aValidationCycle);
835 * Set actor for the given validation cycle and validation step.
837 * @param aValidationCycle
838 * the validation cycle
840 * the validation step
845 protected void setActor(final ValidationCycle aValidationCycle,
846 final ValidationStep step, final User actor) {
847 if (step == ValidationStep.PROMOTION) {
848 aValidationCycle.setPublisher(actor);
849 } else if (step == ValidationStep.REVIEW) {
850 aValidationCycle.setReviewer(actor);
851 } else if (step == ValidationStep.APPROVAL) {
852 aValidationCycle.setApprover(actor);
853 } else if (step == ValidationStep.ACCEPTANCE
854 || step == ValidationStep.REFUSAL) {
855 aValidationCycle.setSignatory(actor);
857 if (aValidationCycle.isSaved()) {
858 getValidationCycleDAO().update(aValidationCycle);
863 * Returns all actors of this study other than the author, including contributors, reviewers and approvers.
867 * @return the actors of this study
868 * @see #hasActor(User)
870 public Set<User> getActors(final Study aStudy) {
871 if (aStudy.getActor() == null) {
872 setShortCuts(aStudy);
874 return Collections.unmodifiableSet(aStudy.getActor());
878 * Returns all actors of this study other than the author, including contributors, reviewers and approvers.
882 * @return the modifiable set of actors of this study
883 * @see #hasActor(User)
885 public Set<User> getModifiableActors(final Study aStudy) {
886 if (aStudy.getActor() == null) {
887 setShortCuts(aStudy);
889 return aStudy.getActor();
893 * Returns unmodifiable initialized transient list of contributors of this study.
897 * @return the unmodifiable not null transient list of contributors of this study
899 public List<User> getContributors(final Study aStudy) {
900 if (aStudy.getContributor() == null) {
901 setShortCuts(aStudy);
903 return Collections.unmodifiableList(aStudy.getContributor()); // May be empty
907 * Returns modifiable initialized transient list of contributors of this study.
911 * @return the modifiable not null transient list of contributors of this study
913 public List<User> getModifiableContributors(final Study aStudy) {
914 if (aStudy.getContributor() == null) {
915 setShortCuts(aStudy);
917 return aStudy.getContributor(); // May be empty
921 * Returns the validation cycle of the given document type.
926 * the document type being subject of validation
927 * @return the validation cycle of the document, or null if not defined.
929 public ValidationCycle getValidationCycleOf(final Study aStudy,
930 final DocumentType type) {
931 if (aStudy.getValidationCycles() == null || aStudy.getValidationCycles().isEmpty()) {
932 setShortCuts(aStudy);
934 ValidationCycle result = aStudy.getValidationCycles().get(
936 if (result == null) {
937 if (type.isStepResult()) {
938 result = aStudy.getValidationCycles().get("default"); // "default" validation cycle defined in the configuration, if exist
940 if (result == null) {
941 result = aStudy.getValidationCycles().get("built-in");
948 * Checks if the given user is actor of this study. Actors include contributors, reviewers and approvers.
953 * the user to look for
954 * @return true if the given user is actor of this study.
957 public boolean hasActor(final Study aStudy, final User user) {
961 for (Iterator<User> i = getActors(aStudy).iterator(); i.hasNext();) {
962 User involved = i.next();
963 if (involved.equals(user)) {
971 * Checks if the given user participates to this study. The Study staff includes the author and contributors.
976 * the user to look for
977 * @return true if the given user is actor of this study.
978 * @see #getContributors()
980 public boolean isStaffedBy(final Study aStudy, final User user) {
984 if (aStudy == null) {
987 if (aStudy.getAuthor() == null) {
990 if (aStudy.getAuthor().equals(user)) {
993 for (Iterator<User> i = getContributors(aStudy).iterator(); i.hasNext();) {
994 if (i.next().equals(user)) {
1002 * Initialize shortcuts of the study as its transient collections.
1007 public void loadWorkflow(final Study aStudy) {
1008 setShortCuts(aStudy);
1012 * Initialize shortcuts of the study as its transient collections.
1017 public void setShortCuts(final Study aStudy) {
1018 aStudy.getContributor().clear();
1019 aStudy.getValidationCycles().clear();
1020 aStudy.getActor().clear();
1022 // Get the contributors
1023 for (Iterator<Relation> i = aStudy.getRelations(
1024 ContributorRelation.class).iterator(); i.hasNext();) {
1025 ContributorRelation link = (ContributorRelation) i.next();
1026 aStudy.getContributor().add(link.getTo());
1028 // Get the validation cycles specific to this study
1029 for (Iterator<Relation> i = aStudy.getRelations(
1030 ValidationCycleRelation.class).iterator(); i.hasNext();) {
1031 ValidationCycleRelation link = (ValidationCycleRelation) i.next();
1032 aStudy.getValidationCycles().put(link.getDocumentType().getName(),
1033 link.getTo()); // The associated document type is necessarily not null in this
1036 // Get the validation cycles coming from the configured workflow and not overridden in this study
1037 for (Iterator<ProjectSettingsServiceImpl.ProjectSettingsValidationCycle> i = getProjectSettings()
1038 .getAllValidationCycles().iterator(); i.hasNext();) {
1039 ProjectSettingsServiceImpl.ProjectSettingsValidationCycle cycle = i
1041 String type = cycle.getName();
1042 if (!aStudy.getValidationCycles().containsKey(type)) {
1043 aStudy.getValidationCycles().put(type,
1044 createValidationCycle(aStudy, cycle));
1047 // Get all corresponding actors
1048 for (Iterator<ValidationCycle> i = aStudy.getValidationCycles()
1049 .values().iterator(); i.hasNext();) {
1050 ValidationCycle cycle = i.next();
1051 User[] user = cycle.getAllActors();
1052 for (int j = 0; j < user.length; j++) {
1053 aStudy.getActor().add(user[j]);
1056 // Get all other actors
1057 for (Iterator<Relation> i = aStudy.getAllRelations().iterator(); i
1059 Relation link = i.next();
1060 Class<?> kindof = link.getClass().getSuperclass();
1061 if (!kindof.equals(ActorRelation.class)) {
1064 aStudy.getActor().add(((ActorRelation) link).getTo());
1071 * @see org.splat.service.StudyService#markStudyAsReference(org.splat.dal.bo.som.Study)
1075 public void markStudyAsReference (final Study aStudy) {
1077 aStudy.setMarkreference(1);
1078 aStudy.setProgressState(ProgressState.TEMPLATE);
1079 getStudyDAO().merge(aStudy);
1085 * @see org.splat.service.StudyService#removeStudyAsReference(org.splat.dal.bo.som.Study)
1089 public void removeStudyAsReference(final Study aStudy) {
1091 aStudy.setMarkreference(0);
1092 aStudy.setProgressState(ProgressState.APPROVED);
1093 getStudyDAO().merge(aStudy);
1098 * @see org.splat.service.StudyService#getDescription(java.lang.Long)
1101 @Transactional(readOnly = true)
1102 public String getDescription(final Long studyId) throws InvalidParameterException {
1103 if(studyId == null) {
1104 throw new InvalidParameterException("studyId", "null");
1106 Study study = _studyDAO.get(studyId);
1108 throw new InvalidParameterException("studyId", studyId.toString());
1110 return study.getDescription();
1116 * @see org.splat.service.StudyService#setDescription(java.lang.Long, java.lang.String)
1120 public void setDescription(final Long studyId, final String descriptionText)
1121 throws InvalidParameterException {
1122 if(studyId == null) {
1123 throw new InvalidParameterException("studyId", "null");
1125 Study study = _studyDAO.get(studyId);
1127 throw new InvalidParameterException("studyId", studyId.toString());
1129 study.setAttribute(new DescriptionAttribute(study, descriptionText));
1134 * @see org.splat.service.StudyService#removeStudyDescription(java.lang.Long)
1138 public boolean removeDescription(final Long studyId) throws InvalidParameterException {
1139 if(studyId == null) {
1140 throw new InvalidParameterException("studyId", String.valueOf(studyId));
1142 Study study = _studyDAO.get(studyId);
1144 throw new InvalidParameterException("studyId", String.valueOf(studyId));
1146 return study.removeAttribute(study.getAttribute(DescriptionAttribute.class));
1152 * @see org.splat.service.StudyService#compare(java.util.List)
1155 public String compare (final List<DocToCompareDTO> docsList, final String userName) throws IncompatibleDataException {
1157 String axis1Name = "";
1158 String axis2Name = "";
1159 String resultPath = "";
1161 XYSeriesCollection dataset = new XYSeriesCollection();
1163 Iterator<DocToCompareDTO> docListIter = docsList.iterator();
1165 for (; docListIter.hasNext();) {
1167 DocToCompareDTO docDTO = docListIter.next();
1168 String pathToFile = docDTO.getPathToFile();
1169 File compDocFile = new File(pathToFile);
1171 resultPath = pathToFile.substring(0, pathToFile.indexOf("vault")) + "downloads" + File.separator + userName + File.separator + "ComparisonResult.pdf";
1173 XYSeries series = new XYSeries("Study: " + docDTO.getStudyTitle() + " Scenario: " + docDTO.getScenarioTitle());
1175 //read the file and get points information.
1177 Scanner input = new Scanner(compDocFile);
1179 //get the name of the axis.
1180 if (input.hasNext()) {
1181 String[] tokens = input.nextLine().split(",");
1183 if (tokens.length < 2)
1184 throw new IncompatibleDataException(MessageKeyEnum.IDT_000001.toString());
1186 if ("".equals(axis1Name)) {
1187 axis1Name = tokens[0];
1188 } else if (!axis1Name.equals(tokens[0])) {
1189 LOG.debug("Axis must be the same for all documents");
1190 throw new IncompatibleDataException(MessageKeyEnum.IDT_000001.toString());
1193 if ("".equals(axis2Name)) {
1194 axis2Name = tokens[1];
1195 } else if (!axis2Name.equals(tokens[1])) {
1196 LOG.debug("Axis must be the same for all documents");
1197 throw new IncompatibleDataException(MessageKeyEnum.IDT_000001.toString());
1201 //Get the XY points series.
1202 while(input.hasNext()) {
1204 String currentString = input.nextLine();
1206 if ("".equals(currentString)) {
1210 String[] tokens = currentString.split(" ");
1211 series.add(Double.valueOf(tokens[0]), Double.valueOf(tokens[1]));
1216 dataset.addSeries(series);
1218 } catch (FileNotFoundException e) {
1219 // TODO Auto-generated catch block
1220 e.printStackTrace();
1225 JFreeChart chart = ChartFactory.createXYLineChart(
1226 "Comparision of Studies Results", // Title
1227 axis1Name, // x-axis Label
1228 axis2Name, // y-axis Label
1230 PlotOrientation.VERTICAL, // Plot Orientation
1231 true, // Show Legend
1232 true, // Use tooltips
1233 false // Configure chart to generate URLs?
1236 //export to PDF - file.
1239 Rectangle pagesize = new Rectangle(x, y);
1240 Document document = new Document(pagesize, 50, 50, 50, 50);
1243 File resFile = new File(resultPath);
1244 File resFolder = new File(resultPath.substring(0, resultPath.lastIndexOf(File.separator)));
1246 writer = PdfWriter.getInstance(document, new FileOutputStream(resFile));
1249 PdfContentByte cb = writer.getDirectContent();
1250 PdfTemplate tp = cb.createTemplate(x, y);
1251 Graphics2D g2 = tp.createGraphics(x, y, new DefaultFontMapper());
1252 chart.draw(g2, new java.awt.Rectangle(x,y));
1254 cb.addTemplate(tp, 0, 0);
1257 } catch (FileNotFoundException e) {
1258 // TODO Auto-generated catch block
1259 e.printStackTrace();
1260 } catch (DocumentException e) {
1261 // TODO Auto-generated catch block
1262 e.printStackTrace();
1269 * Get project settings.
1271 * @return Project settings service
1273 private ProjectSettingsService getProjectSettings() {
1274 return _projectSettings;
1278 * Set project settings service.
1280 * @param projectSettingsService
1281 * project settings service
1283 public void setProjectSettings(
1284 final ProjectSettingsService projectSettingsService) {
1285 _projectSettings = projectSettingsService;
1289 * Get the projectElementService.
1291 * @return the projectElementService
1293 public ProjectElementService getProjectElementService() {
1294 return _projectElementService;
1298 * Set the projectElementService.
1300 * @param projectElementService
1301 * the projectElementService to set
1303 public void setProjectElementService(
1304 final ProjectElementService projectElementService) {
1305 _projectElementService = projectElementService;
1309 * Get the stepService.
1311 * @return the stepService
1313 public StepService getStepService() {
1314 return _stepService;
1318 * Set the stepService.
1320 * @param stepService
1321 * the stepService to set
1323 public void setStepService(final StepService stepService) {
1324 _stepService = stepService;
1328 * Get the indexService.
1330 * @return the indexService
1332 public IndexService getIndexService() {
1333 return _indexService;
1337 * Set the indexService.
1339 * @param indexService
1340 * the indexService to set
1342 public void setIndexService(final IndexService indexService) {
1343 _indexService = indexService;
1349 * @return the studyDAO
1351 public StudyDAO getStudyDAO() {
1359 * the studyDAO to set
1361 public void setStudyDAO(final StudyDAO studyDAO) {
1362 _studyDAO = studyDAO;
1366 * Get the iDBuilderDAO.
1368 * @return the iDBuilderDAO
1370 public IDBuilderDAO getIDBuilderDAO() {
1371 return _iDBuilderDAO;
1375 * Set the iDBuilderDAO.
1378 * the iDBuilderDAO to set
1380 public void setIDBuilderDAO(final IDBuilderDAO builderDAO) {
1381 _iDBuilderDAO = builderDAO;
1385 * Get the scenarioDAO.
1387 * @return the scenarioDAO
1389 public ScenarioDAO getScenarioDAO() {
1390 return _scenarioDAO;
1394 * Set the scenarioDAO.
1396 * @param scenarioDAO
1397 * the scenarioDAO to set
1399 public void setScenarioDAO(final ScenarioDAO scenarioDAO) {
1400 _scenarioDAO = scenarioDAO;
1404 * Get the validationCycleDAO.
1406 * @return the validationCycleDAO
1408 public ValidationCycleDAO getValidationCycleDAO() {
1409 return _validationCycleDAO;
1413 * Set the validationCycleDAO.
1415 * @param validationCycleDAO
1416 * the validationCycleDAO to set
1418 public void setValidationCycleDAO(
1419 final ValidationCycleDAO validationCycleDAO) {
1420 _validationCycleDAO = validationCycleDAO;
1424 * Get the documentTypeService.
1426 * @return the documentTypeService
1428 public DocumentTypeService getDocumentTypeService() {
1429 return _documentTypeService;
1433 * Set the documentTypeService.
1435 * @param documentTypeService
1436 * the documentTypeService to set
1438 public void setDocumentTypeService(
1439 final DocumentTypeService documentTypeService) {
1440 _documentTypeService = documentTypeService;
1444 * Get the userService.
1446 * @return the userService
1448 public UserService getUserService() {
1449 return _userService;
1453 * Set the userService.
1455 * @param userService
1456 * the userService to set
1458 public void setUserService(final UserService userService) {
1459 _userService = userService;
1463 * Get the publicationDAO.
1464 * @return the publicationDAO
1466 public PublicationDAO getPublicationDAO() {
1467 return _publicationDAO;
1471 * Set the publicationDAO.
1472 * @param publicationDAO the publicationDAO to set
1474 public void setPublicationDAO(final PublicationDAO publicationDAO) {
1475 _publicationDAO = publicationDAO;
1479 * Get the repositoryService.
1480 * @return the repositoryService
1482 public RepositoryService getRepositoryService() {
1483 return _repositoryService;
1487 * Set the repositoryService.
1488 * @param repositoryService the repositoryService to set
1490 public void setRepositoryService(final RepositoryService repositoryService) {
1491 _repositoryService = repositoryService;
1495 * Get the documentDAO.
1496 * @return the documentDAO
1498 public DocumentDAO getDocumentDAO() {
1499 return _documentDAO;
1503 * Set the documentDAO.
1504 * @param documentDAO the documentDAO to set
1506 public void setDocumentDAO(final DocumentDAO documentDAO) {
1507 _documentDAO = documentDAO;
1511 * Get the descriptionAttributeDAO.
1512 * @return the descriptionAttributeDAO
1514 public DescriptionAttributeDAO getDescriptionAttributeDAO() {
1515 return _descriptionAttributeDAO;
1519 * Set the descriptionAttributeDAO.
1520 * @param descriptionAttributeDAO the descriptionAttributeDAO to set
1522 public void setDescriptionAttributeDAO(
1523 final DescriptionAttributeDAO descriptionAttributeDAO) {
1524 _descriptionAttributeDAO = descriptionAttributeDAO;