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.ValidationCycle;
49 import org.splat.dal.bo.som.ValidationCycleRelation;
50 import org.splat.dal.bo.som.ValidationStep;
51 import org.splat.dal.bo.som.Visibility;
52 import org.splat.dal.bo.som.Study.Properties;
53 import org.splat.dal.bo.som.ValidationCycle.Actor;
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);
181 * Get study by its reference.
184 * the study reference
185 * @return found study or null
187 @Transactional(readOnly = true)
188 public Study selectStudy(final String refid) {
189 Study result = getStudyDAO().findByCriteria(
190 Restrictions.eq("sid", refid));
191 loadWorkflow(result);
198 * @see org.splat.service.StudyService#createStudy(org.splat.dal.bo.som.Study.Properties)
201 public Study createStudy(final Study.Properties sprop)
202 throws MissedPropertyException, InvalidPropertyException,
203 MultiplyDefinedException {
204 sprop.setReference(getProjectSettings().getReferencePattern());
205 Study study = new Study(sprop);
207 buildReference(study);
208 getStudyDAO().create(study);
210 // IndexService lucin = getIndex();
212 // } catch (IOException error) {
213 // LOG.error("Unable to index the study '" + study.getIndex()
214 // + "', reason:", error);
215 // // Continue and try to index later
223 * @see org.splat.service.StudyService#addProjectContext(org.splat.dal.bo.som.Study, org.splat.dal.bo.som.SimulationContext.Properties)
226 public SimulationContext addProjectContext(final Study aStudy,
227 final SimulationContext.Properties cprop)
228 throws MissedPropertyException, InvalidPropertyException,
229 MultiplyDefinedException {
230 SimulationContext added = getStepService().addSimulationContext(
231 getProjectElementService().getFirstStep(aStudy), cprop);
239 * @see org.splat.service.StudyService#addProjectContext(org.splat.dal.bo.som.Study, org.splat.dal.bo.som.SimulationContext)
242 public SimulationContext addProjectContext(final Study aStudy,
243 final SimulationContext context) {
244 SimulationContext added = getStepService().addSimulationContext(
245 getProjectElementService().getFirstStep(aStudy), context);
253 * @see org.splat.service.StudyService#addContributor(org.splat.dal.bo.som.Study, org.splat.dal.bo.kernel.User)
255 public boolean addContributor(final Study aStudy, final User user) {
256 List<User> contributor = getModifiableContributors(aStudy); // Initializes contributor
257 for (Iterator<User> i = contributor.iterator(); i.hasNext();) {
258 User present = i.next();
259 if (present.equals(user)) {
263 boolean absent = getModifiableActors(aStudy).add(user); // User may already be a reviewer or an approver
265 aStudy.addRelation(new ContributorRelation(aStudy, user));
267 update(aStudy); // Else, useless to re-index the study
269 contributor.add(user);
274 * Moves this study from the Public to the Reference area of the repository. For being moved to the Reference area, the study must
275 * previously be approved.
279 * @return true if the move succeeded.
280 * @see #moveToPublic()
282 * @see Publication#approve(Date)
284 public boolean moveToReference(final Study aStudy) {
285 if (aStudy.getProgressState() != ProgressState.APPROVED) {
288 if (aStudy.getVisibility() != Visibility.PUBLIC) {
292 aStudy.setVisibility(Visibility.REFERENCE);
293 if (update(aStudy)) {
294 return updateKnowledgeElementsIndex(aStudy); // If fails, the database roll-back is under responsibility of the caller
302 * @see org.splat.service.StudyService#update(org.splat.dal.bo.som.Study, org.splat.dal.bo.som.Study.Properties)
304 public boolean update(final Study aStudy, final Properties sprop)
305 throws InvalidPropertyException {
306 if (sprop.getTitle() != null) {
307 aStudy.setTitle(sprop.getTitle());
309 if (sprop.getSummary() != null) {
310 aStudy.setAttribute(new DescriptionAttribute(aStudy, sprop
313 // TODO: To be completed
314 return update(aStudy);
318 * Check if the document is published in the study.
324 * @return true if the document is published in the study
326 /* private boolean publishes(final Study aStudy, final Document doc) {
327 if (!aStudy.publishes(doc)) {
328 Scenario[] scene = aStudy.getScenarii();
329 for (int i = 0; i < scene.length; i++) {
330 if (scene[i].publishes(doc)) {
341 * @see org.splat.service.StudyService#removeContributor(org.splat.dal.bo.som.Study, org.splat.dal.bo.kernel.User[])
343 public boolean removeContributor(final Study aStudy, final User... users) {
344 List<User> contributor = getModifiableContributors(aStudy); // Initializes contributor
345 Boolean done = false;
346 for (int i = 0; i < users.length; i++) {
347 User user = users[i];
348 for (Iterator<User> j = contributor.iterator(); j.hasNext();) {
349 User present = j.next();
350 if (!present.equals(user)) {
354 aStudy.removeRelation(ContributorRelation.class, user);
355 j.remove(); // Updates the contributor shortcut
369 * @see org.splat.service.StudyService#removeProjectContext(org.splat.dal.bo.som.Study, org.splat.dal.bo.som.SimulationContext)
371 public boolean removeProjectContext(final Study aStudy,
372 final SimulationContext context) {
373 boolean done = getStepService().removeSimulationContext(
374 getProjectElementService().getFirstStep(aStudy), context);
382 * @see org.splat.service.StudyService#setValidationCycle(org.splat.dal.bo.som.Study, org.splat.dal.bo.som.DocumentType,
383 * org.splat.dal.bo.som.ValidationCycle.Properties)
386 public void setValidationCycle(final Study aStudyDTO, final DocumentType type,
387 final ValidationCycle.Properties vprop) {
388 Map<String, ValidationCycle> validactor = aStudyDTO.getValidationCycles();
389 if (validactor == null) {
390 setShortCuts(aStudyDTO); // Initializes validactor and actor
393 Study aStudy = selectStudy(aStudyDTO.getIndex());
395 String cname = type.getName();
396 ValidationCycle cycle = validactor.get(cname);
398 if (cycle != null && cycle.isAssigned()) {
399 resetActors(cycle, vprop);
402 cycle = new ValidationCycle(aStudy, vprop.setDocumentType(type));
404 getValidationCycleDAO().create(cycle); // RKV
406 ValidationCycleRelation link = cycle.getContext();
407 aStudy.addRelation(link);
408 aStudyDTO.getAllRelations().add(link); // RKV
410 validactor.put(cname, link.getTo()); // Replaces the cycle if exists as default,
411 } catch (Exception error) {
412 LOG.error("Unable to re-index Knowledge Elements, reason:",
417 resetActorsShortCut(aStudyDTO);
418 update(aStudy); // Re-index the study, just in case
422 * Demotes this study from In-Check to In-Draft then In-Work states. This function is called internally when demoting the final result
423 * document of the study.
427 * @return true if the demotion succeeded.
429 public boolean demote(final Study aStudy) {
430 if (aStudy.getProgressState() == ProgressState.inCHECK) {
431 aStudy.setProgressState(ProgressState.inDRAFT);
432 } else if (aStudy.getProgressState() == ProgressState.inDRAFT) {
433 aStudy.setProgressState(ProgressState.inWORK);
437 return update(aStudy);
443 * @see org.splat.service.StudyService#generateLocalIndex(org.splat.dal.bo.som.Study)
446 public int generateLocalIndex(final Study aStudy) {
447 aStudy.setLastLocalIndex(aStudy.getLastLocalIndex() + 1);
448 return aStudy.getLastLocalIndex();
452 * Promotes this study from In-Work to In-Draft then In-Check and APPROVED states. This function is called internally when promoting the
453 * final result document of the study.
457 * @return true if the demotion succeeded.
460 public boolean promote(final Study aStudy) {
461 if (aStudy.getProgressState() == ProgressState.inWORK) {
462 aStudy.setProgressState(ProgressState.inDRAFT);
463 } else if (aStudy.getProgressState() == ProgressState.inDRAFT) {
464 aStudy.setProgressState(ProgressState.inCHECK);
465 Revision myvers = new Revision(aStudy.getVersion());
466 if (myvers.isMinor()) {
467 aStudy.setVersion(myvers.incrementAs(aStudy.getProgressState())
470 } else if (aStudy.getProgressState() == ProgressState.inCHECK) {
471 aStudy.setProgressState(ProgressState.APPROVED);
476 return update(aStudy);
480 * Moves this study from the Private to the Public area of the repository.
484 * @return true if the move succeeded.
488 public boolean moveToPublic(final Study aStudy) {
489 boolean isOk = false;
490 if (aStudy.getVisibility() == Visibility.PRIVATE) {
491 aStudy.setVisibility(Visibility.PUBLIC);
492 if (update(aStudy)) {
493 isOk = updateKnowledgeElementsIndex(aStudy); // If fails, the database roll-back is under responsibility of the caller
500 * Moves this study from the Public to the Private area of the repository.
504 * @return true if the move succeeded.
507 public boolean moveToPrivate(final Study aStudy) {
508 boolean isOk = false;
509 if (aStudy.getVisibility() == Visibility.PUBLIC) {
510 aStudy.setVisibility(Visibility.PRIVATE);
511 if (update(aStudy)) {
512 isOk = updateKnowledgeElementsIndex(aStudy); // If fails, the database roll-back is under responsibility of the caller
519 * Update a study in the database.
522 * the study to update
523 * @return true if the study is updated successfully
526 private boolean update(final Study aStudy) {
527 boolean isOk = false;
529 getStudyDAO().merge(aStudy); // Update of relational base
530 setShortCuts(aStudy); // RKV: initialize transient actors set
531 //RKV: getIndex().update(aStudy); // Update of Lucene index
533 } catch (Exception e) {
534 LOG.error("STD-000001", e, aStudy.getIndex(), e.getMessage());
540 * Build reference for the study. The reference of the study is stored as a new reference pattern (IDBuilder).
544 * @return true if reference building is succeded
547 private boolean buildReference(final Study aStudy) {
548 String pattern = aStudy.getReference(); // The study being supposed just created, its reference is the reference pattern
549 IDBuilder tool = selectIDBuilder(aStudy.getDate());
551 tool = new IDBuilder(aStudy.getDate());
552 getIDBuilderDAO().create(tool);
554 aStudy.setReference(buildReference(tool, pattern, aStudy));
559 * Build reference for the study. The reference of the study is stored as a new reference pattern (IDBuilder).
566 * the reference pattern
567 * @return true if reference building is succeded
570 public String buildReference(final IDBuilder aBuilder,
571 final String pattern, final Study study) {
572 char[] format = pattern.toCharArray();
573 char[] ref = new char[80]; // Better evaluate the length of the generated string
574 int next = aBuilder.getBase() + 1;
577 for (int i = 0; i < format.length; i++) {
579 // Insertion of attribute values
580 if (format[i] == '%') {
583 if (format[i] == 'y') { // Insertion of year in format 2 (e.g. 09) or 4 (e.g. 2009) digits
585 while (format[i] == 'y') {
587 if (i == format.length) {
591 SimpleDateFormat tostring = new SimpleDateFormat("yyyy"); //RKV: NOPMD: TODO: Use locale here?
592 String year = tostring.format(study.getDate());
593 year = year.substring(4 - (i - n), 4); // 4-(i-n) must be equal to either 0 or 2
594 for (int j = 0; j < year.length(); j++) {
595 ref[count] = year.charAt(j);
598 i -= 1; // Back to the last 'y' character
599 } else if (format[i] == '0') { // Insertion of the index
601 while (format[i] == '0') {
603 if (i == format.length) {
607 DecimalFormat tostring = new DecimalFormat(pattern
609 String number = tostring.format(next);
610 for (int j = 0; j < number.length(); j++) {
611 ref[count] = number.charAt(j);
614 i -= 1; // Back to the last '0' character
616 // Keep the character
618 ref[count] = format[i];
622 // Incrementation of the number of study
623 aBuilder.setBase(next);
624 getIDBuilderDAO().update(aBuilder);
625 return String.copyValueOf(ref, 0, count);
629 * Find an id builder by date.
633 * @return found id builder
635 private IDBuilder selectIDBuilder(final Date date) {
636 Calendar aDate = Calendar.getInstance();
638 return getIDBuilderDAO().findByCriteria(
639 Restrictions.eq("cycle", aDate.get(Calendar.YEAR)));
643 * Fill transient collection ModifiableActors of the study.
648 private void resetActorsShortCut(final Study aStudy) {
649 getModifiableActors(aStudy).clear();
650 // Get all actors involved in validation cycles
651 for (Iterator<ValidationCycle> i = aStudy.getValidationCycles()
652 .values().iterator(); i.hasNext();) {
653 ValidationCycle cycle = i.next();
654 User[] user = cycle.getAllActors();
655 for (int j = 0; j < user.length; j++) {
656 getModifiableActors(aStudy).add(user[j]);
659 // Get all other actors
660 for (Iterator<Relation> i = aStudy.getAllRelations().iterator(); i
662 Relation link = i.next();
663 Class<?> kindof = link.getClass().getSuperclass();
664 if (!kindof.equals(ActorRelation.class)) {
667 getModifiableActors(aStudy).add(((ActorRelation) link).getTo());
672 * Update lucene index for the study knowledge elements.
676 * @return true if reindexing succeeded
678 private boolean updateKnowledgeElementsIndex(final Study aStudy) {
679 // boolean isOk = false;
681 // IndexService lucin = getIndex();
683 // for (Iterator<Scenario> i = aStudy.getScenariiList().iterator(); i
685 // Scenario scene = i.next();
686 // for (Iterator<KnowledgeElement> j = scene
687 // .getAllKnowledgeElements().iterator(); j.hasNext();) {
688 // KnowledgeElement kelm = j.next();
689 // lucin.update(kelm);
693 // } catch (Exception error) {
694 // LOG.error("Unable to re-index Knowledge Elements, reason:",
702 * Get lucene index service. Create a lucene index if it does not exist.
704 * @return index service
705 * @throws IOException
706 * if error occurs during lucene index creation
708 private IndexService getIndex() throws IOException {
709 IndexService lucin = getIndexService();
710 if (IndexWriter.isLocked(FSDirectory.open(getRepositoryService()
711 .getRepositoryIndexDirectory()))) {
712 IndexWriter.unlock(FSDirectory.open(getRepositoryService()
713 .getRepositoryIndexDirectory()));
715 if (!lucin.exists()) {
716 lucin.create(); // Happens when re-indexing all studies
722 * Create a new validation cycle for documents of the given study.
727 * the cycle description
728 * @return the new validation cycle
730 protected ValidationCycle createValidationCycle(
732 final ProjectSettingsServiceImpl.ProjectSettingsValidationCycle cycle) {
733 Actor[] actype = cycle.getActorTypes();
734 User.Properties uprop = new User.Properties();
736 ValidationCycle aValidationCycle = new ValidationCycle();
737 aValidationCycle.setDocumentType(getDocumentTypeService().selectType(
738 cycle.getName())); // Null in case of default validation cycle
739 // context = new ValidationCycleRelation(from, vprop);
740 // RKV aValidationCycle.context = null; // Validation cycle defined in the workflow
741 for (int i = 0; i < actype.length; i++) {
743 if (actype[i] != null) {
745 if (actype[i] == Actor.manager) {
746 actor = from.getAuthor();
747 } else if (actype[i] == Actor.Nx1) {
748 List<User> manager = getUserService().selectUsersWhere(
749 uprop.setOrganizationName("Nx1"));
750 if (manager.size() == 1) {
751 actor = manager.get(0);
753 } else if (actype[i] == Actor.Nx2) {
754 List<User> manager = getUserService().selectUsersWhere(
755 uprop.setOrganizationName("Nx2"));
756 if (manager.size() == 1) {
757 actor = manager.get(0);
759 } else { /* Actor.customer */
760 actor = from.getAuthor();
761 // TODO: Get the customer of the study, if exists
763 } catch (Exception e) { // Should not happen
768 aValidationCycle.setReviewer(actor);
770 aValidationCycle.setApprover(actor);
772 aValidationCycle.setSignatory(actor);
775 return aValidationCycle;
779 * Remove a validation step from the validation cycle.
781 * @param aValidationCycle
782 * the validation cycle
784 * the validation step to remove
787 protected void remove(final ValidationCycle aValidationCycle,
788 final ValidationStep step) {
789 if (step == ValidationStep.REVIEW) {
790 aValidationCycle.setReviewer(null);
791 } else if (step == ValidationStep.APPROVAL) {
792 aValidationCycle.setApprover(null);
793 } else if (step == ValidationStep.ACCEPTANCE
794 || step == ValidationStep.REFUSAL) {
795 aValidationCycle.setSignatory(null);
797 if (aValidationCycle.isSaved()) {
798 getValidationCycleDAO().update(aValidationCycle);
803 * Reset actors for the validation cycle.
805 * @param aValidationCycle
806 * the validation cycle to update
808 * new validation cycle properties containing new actors
811 public void resetActors(final ValidationCycle aValidationCycle,
812 final ValidationCycle.Properties vprop) {
813 aValidationCycle.setPublisher(vprop.getPublisher()); // May be null
814 aValidationCycle.setReviewer(vprop.getReviewer()); // May be null
815 aValidationCycle.setApprover(vprop.getApprover()); // May be null
816 aValidationCycle.setSignatory(vprop.getSignatory()); // May be null
817 if (aValidationCycle.isSaved()) {
818 getValidationCycleDAO().merge(aValidationCycle);
823 * Set actor for the given validation cycle and validation step.
825 * @param aValidationCycle
826 * the validation cycle
828 * the validation step
833 protected void setActor(final ValidationCycle aValidationCycle,
834 final ValidationStep step, final User actor) {
835 if (step == ValidationStep.PROMOTION) {
836 aValidationCycle.setPublisher(actor);
837 } else if (step == ValidationStep.REVIEW) {
838 aValidationCycle.setReviewer(actor);
839 } else if (step == ValidationStep.APPROVAL) {
840 aValidationCycle.setApprover(actor);
841 } else if (step == ValidationStep.ACCEPTANCE
842 || step == ValidationStep.REFUSAL) {
843 aValidationCycle.setSignatory(actor);
845 if (aValidationCycle.isSaved()) {
846 getValidationCycleDAO().update(aValidationCycle);
851 * Returns all actors of this study other than the author, including contributors, reviewers and approvers.
855 * @return the actors of this study
856 * @see #hasActor(User)
858 public Set<User> getActors(final Study aStudy) {
859 if (aStudy.getActor() == null) {
860 setShortCuts(aStudy);
862 return Collections.unmodifiableSet(aStudy.getActor());
866 * Returns all actors of this study other than the author, including contributors, reviewers and approvers.
870 * @return the modifiable set of actors of this study
871 * @see #hasActor(User)
873 public Set<User> getModifiableActors(final Study aStudy) {
874 if (aStudy.getActor() == null) {
875 setShortCuts(aStudy);
877 return aStudy.getActor();
881 * Returns unmodifiable initialized transient list of contributors of this study.
885 * @return the unmodifiable not null transient list of contributors of this study
887 public List<User> getContributors(final Study aStudy) {
888 if (aStudy.getContributor() == null) {
889 setShortCuts(aStudy);
891 return Collections.unmodifiableList(aStudy.getContributor()); // May be empty
895 * Returns modifiable initialized transient list of contributors of this study.
899 * @return the modifiable not null transient list of contributors of this study
901 public List<User> getModifiableContributors(final Study aStudy) {
902 if (aStudy.getContributor() == null) {
903 setShortCuts(aStudy);
905 return aStudy.getContributor(); // May be empty
909 * Returns the validation cycle of the given document type.
914 * the document type being subject of validation
915 * @return the validation cycle of the document, or null if not defined.
917 public ValidationCycle getValidationCycleOf(final Study aStudy,
918 final DocumentType type) {
919 if (aStudy.getValidationCycles() == null || aStudy.getValidationCycles().isEmpty()) {
920 setShortCuts(aStudy);
922 ValidationCycle result = aStudy.getValidationCycles().get(
924 if (result == null) {
925 if (type.isStepResult()) {
926 result = aStudy.getValidationCycles().get("default"); // "default" validation cycle defined in the configuration, if exist
928 if (result == null) {
929 result = aStudy.getValidationCycles().get("built-in");
936 * Checks if the given user is actor of this study. Actors include contributors, reviewers and approvers.
941 * the user to look for
942 * @return true if the given user is actor of this study.
945 public boolean hasActor(final Study aStudy, final User user) {
949 for (Iterator<User> i = getActors(aStudy).iterator(); i.hasNext();) {
950 User involved = i.next();
951 if (involved.equals(user)) {
959 * Checks if the given user participates to this study. The Study staff includes the author and contributors.
964 * the user to look for
965 * @return true if the given user is actor of this study.
966 * @see #getContributors()
968 public boolean isStaffedBy(final Study aStudy, final User user) {
972 if (aStudy == null) {
975 if (aStudy.getAuthor() == null) {
978 if (aStudy.getAuthor().equals(user)) {
981 for (Iterator<User> i = getContributors(aStudy).iterator(); i.hasNext();) {
982 if (i.next().equals(user)) {
990 * Initialize shortcuts of the study as its transient collections.
995 public void loadWorkflow(final Study aStudy) {
996 setShortCuts(aStudy);
1000 * Initialize shortcuts of the study as its transient collections.
1005 public void setShortCuts(final Study aStudy) {
1006 aStudy.getContributor().clear();
1007 aStudy.getValidationCycles().clear();
1008 aStudy.getActor().clear();
1010 // Get the contributors
1011 for (Iterator<Relation> i = aStudy.getRelations(
1012 ContributorRelation.class).iterator(); i.hasNext();) {
1013 ContributorRelation link = (ContributorRelation) i.next();
1014 aStudy.getContributor().add(link.getTo());
1016 // Get the validation cycles specific to this study
1017 for (Iterator<Relation> i = aStudy.getRelations(
1018 ValidationCycleRelation.class).iterator(); i.hasNext();) {
1019 ValidationCycleRelation link = (ValidationCycleRelation) i.next();
1020 aStudy.getValidationCycles().put(link.getDocumentType().getName(),
1021 link.getTo()); // The associated document type is necessarily not null in this
1024 // Get the validation cycles coming from the configured workflow and not overridden in this study
1025 for (Iterator<ProjectSettingsServiceImpl.ProjectSettingsValidationCycle> i = getProjectSettings()
1026 .getAllValidationCycles().iterator(); i.hasNext();) {
1027 ProjectSettingsServiceImpl.ProjectSettingsValidationCycle cycle = i
1029 String type = cycle.getName();
1030 if (!aStudy.getValidationCycles().containsKey(type)) {
1031 aStudy.getValidationCycles().put(type,
1032 createValidationCycle(aStudy, cycle));
1035 // Get all corresponding actors
1036 for (Iterator<ValidationCycle> i = aStudy.getValidationCycles()
1037 .values().iterator(); i.hasNext();) {
1038 ValidationCycle cycle = i.next();
1039 User[] user = cycle.getAllActors();
1040 for (int j = 0; j < user.length; j++) {
1041 aStudy.getActor().add(user[j]);
1044 // Get all other actors
1045 for (Iterator<Relation> i = aStudy.getAllRelations().iterator(); i
1047 Relation link = i.next();
1048 Class<?> kindof = link.getClass().getSuperclass();
1049 if (!kindof.equals(ActorRelation.class)) {
1052 aStudy.getActor().add(((ActorRelation) link).getTo());
1059 * @see org.splat.service.StudyService#markStudyAsReference(org.splat.dal.bo.som.Study)
1063 public void markStudyAsReference (final Study aStudy) {
1065 aStudy.setMarkreference(1);
1066 aStudy.setProgressState(ProgressState.TEMPLATE);
1067 getStudyDAO().merge(aStudy);
1073 * @see org.splat.service.StudyService#removeStudyAsReference(org.splat.dal.bo.som.Study)
1077 public void removeStudyAsReference(final Study aStudy) {
1079 aStudy.setMarkreference(0);
1080 aStudy.setProgressState(ProgressState.APPROVED);
1081 getStudyDAO().merge(aStudy);
1086 * @see org.splat.service.StudyService#getDescription(java.lang.Long)
1089 @Transactional(readOnly = true)
1090 public String getDescription(final Long studyId) throws InvalidParameterException {
1091 if(studyId == null) {
1092 throw new InvalidParameterException("studyId", "null");
1094 Study study = _studyDAO.get(studyId);
1096 throw new InvalidParameterException("studyId", studyId.toString());
1098 return study.getDescription();
1104 * @see org.splat.service.StudyService#setDescription(java.lang.Long, java.lang.String)
1108 public void setDescription(final Long studyId, final String descriptionText)
1109 throws InvalidParameterException {
1110 if(studyId == null) {
1111 throw new InvalidParameterException("studyId", "null");
1113 Study study = _studyDAO.get(studyId);
1115 throw new InvalidParameterException("studyId", studyId.toString());
1117 study.setAttribute(new DescriptionAttribute(study, descriptionText));
1122 * @see org.splat.service.StudyService#removeStudyDescription(java.lang.Long)
1126 public boolean removeDescription(final Long studyId) throws InvalidParameterException {
1127 if(studyId == null) {
1128 throw new InvalidParameterException("studyId", String.valueOf(studyId));
1130 Study study = _studyDAO.get(studyId);
1132 throw new InvalidParameterException("studyId", String.valueOf(studyId));
1134 return study.removeAttribute(study.getAttribute(DescriptionAttribute.class));
1140 * @see org.splat.service.StudyService#compare(java.util.List)
1143 public String compare (final List<DocToCompareDTO> docsList, final String userName) throws IncompatibleDataException{
1145 String axis1Name = "";
1146 String axis2Name = "";
1147 String resultPath = "";
1149 XYSeriesCollection dataset = new XYSeriesCollection();
1151 Iterator<DocToCompareDTO> docListIter = docsList.iterator();
1153 for (; docListIter.hasNext();) {
1155 DocToCompareDTO docDTO = docListIter.next();
1156 String pathToFile = docDTO.getPathToFile();
1157 File compDocFile = new File(pathToFile);
1159 resultPath = pathToFile.substring(0, pathToFile.indexOf("vault")) + "downloads" + File.separator + userName + File.separator + "ComparisonResult.pdf";
1161 XYSeries series = new XYSeries("Study: " + docDTO.getStudyTitle() + " Scenario: " + docDTO.getScenarioTitle());
1163 //read the file and get points information.
1165 Scanner input = new Scanner(compDocFile);
1167 //get the name of the axis.
1168 if (input.hasNext()) {
1169 String[] tokens = input.nextLine().split(",");
1170 if ("".equals(axis1Name)) {
1171 axis1Name = tokens[0];
1172 } else if (!axis1Name.equals(tokens[0])) {
1173 LOG.debug("Axis must be the same for all documents");
1174 throw new IncompatibleDataException(MessageKeyEnum.IDT_000001.toString());
1177 if ("".equals(axis2Name)) {
1178 axis2Name = tokens[1];
1179 } else if (!axis2Name.equals(tokens[1])) {
1180 LOG.debug("Axis must be the same for all documents");
1181 throw new IncompatibleDataException(MessageKeyEnum.IDT_000001.toString());
1185 //Get the XY points series.
1186 while(input.hasNext()) {
1188 String currentString = input.nextLine();
1190 if ("".equals(currentString)) {
1194 String[] tokens = currentString.split(" ");
1195 series.add(Double.valueOf(tokens[0]), Double.valueOf(tokens[1]));
1200 dataset.addSeries(series);
1202 } catch (FileNotFoundException e) {
1203 // TODO Auto-generated catch block
1204 e.printStackTrace();
1209 JFreeChart chart = ChartFactory.createXYLineChart(
1210 "Comparision of Studies Results", // Title
1211 axis1Name, // x-axis Label
1212 axis2Name, // y-axis Label
1214 PlotOrientation.VERTICAL, // Plot Orientation
1215 true, // Show Legend
1216 true, // Use tooltips
1217 false // Configure chart to generate URLs?
1220 //export to PDF - file.
1223 Rectangle pagesize = new Rectangle(x, y);
1224 Document document = new Document(pagesize, 50, 50, 50, 50);
1227 File resFile = new File(resultPath);
1228 File resFolder = new File(resultPath.substring(0, resultPath.lastIndexOf(File.separator)));
1230 writer = PdfWriter.getInstance(document, new FileOutputStream(resFile));
1233 PdfContentByte cb = writer.getDirectContent();
1234 PdfTemplate tp = cb.createTemplate(x, y);
1235 Graphics2D g2 = tp.createGraphics(x, y, new DefaultFontMapper());
1236 chart.draw(g2, new java.awt.Rectangle(x,y));
1238 cb.addTemplate(tp, 0, 0);
1241 } catch (FileNotFoundException e) {
1242 // TODO Auto-generated catch block
1243 e.printStackTrace();
1244 } catch (DocumentException e) {
1245 // TODO Auto-generated catch block
1246 e.printStackTrace();
1253 * Get project settings.
1255 * @return Project settings service
1257 private ProjectSettingsService getProjectSettings() {
1258 return _projectSettings;
1262 * Set project settings service.
1264 * @param projectSettingsService
1265 * project settings service
1267 public void setProjectSettings(
1268 final ProjectSettingsService projectSettingsService) {
1269 _projectSettings = projectSettingsService;
1273 * Get the projectElementService.
1275 * @return the projectElementService
1277 public ProjectElementService getProjectElementService() {
1278 return _projectElementService;
1282 * Set the projectElementService.
1284 * @param projectElementService
1285 * the projectElementService to set
1287 public void setProjectElementService(
1288 final ProjectElementService projectElementService) {
1289 _projectElementService = projectElementService;
1293 * Get the stepService.
1295 * @return the stepService
1297 public StepService getStepService() {
1298 return _stepService;
1302 * Set the stepService.
1304 * @param stepService
1305 * the stepService to set
1307 public void setStepService(final StepService stepService) {
1308 _stepService = stepService;
1312 * Get the indexService.
1314 * @return the indexService
1316 public IndexService getIndexService() {
1317 return _indexService;
1321 * Set the indexService.
1323 * @param indexService
1324 * the indexService to set
1326 public void setIndexService(final IndexService indexService) {
1327 _indexService = indexService;
1333 * @return the studyDAO
1335 public StudyDAO getStudyDAO() {
1343 * the studyDAO to set
1345 public void setStudyDAO(final StudyDAO studyDAO) {
1346 _studyDAO = studyDAO;
1350 * Get the iDBuilderDAO.
1352 * @return the iDBuilderDAO
1354 public IDBuilderDAO getIDBuilderDAO() {
1355 return _iDBuilderDAO;
1359 * Set the iDBuilderDAO.
1362 * the iDBuilderDAO to set
1364 public void setIDBuilderDAO(final IDBuilderDAO builderDAO) {
1365 _iDBuilderDAO = builderDAO;
1369 * Get the scenarioDAO.
1371 * @return the scenarioDAO
1373 public ScenarioDAO getScenarioDAO() {
1374 return _scenarioDAO;
1378 * Set the scenarioDAO.
1380 * @param scenarioDAO
1381 * the scenarioDAO to set
1383 public void setScenarioDAO(final ScenarioDAO scenarioDAO) {
1384 _scenarioDAO = scenarioDAO;
1388 * Get the validationCycleDAO.
1390 * @return the validationCycleDAO
1392 public ValidationCycleDAO getValidationCycleDAO() {
1393 return _validationCycleDAO;
1397 * Set the validationCycleDAO.
1399 * @param validationCycleDAO
1400 * the validationCycleDAO to set
1402 public void setValidationCycleDAO(
1403 final ValidationCycleDAO validationCycleDAO) {
1404 _validationCycleDAO = validationCycleDAO;
1408 * Get the documentTypeService.
1410 * @return the documentTypeService
1412 public DocumentTypeService getDocumentTypeService() {
1413 return _documentTypeService;
1417 * Set the documentTypeService.
1419 * @param documentTypeService
1420 * the documentTypeService to set
1422 public void setDocumentTypeService(
1423 final DocumentTypeService documentTypeService) {
1424 _documentTypeService = documentTypeService;
1428 * Get the userService.
1430 * @return the userService
1432 public UserService getUserService() {
1433 return _userService;
1437 * Set the userService.
1439 * @param userService
1440 * the userService to set
1442 public void setUserService(final UserService userService) {
1443 _userService = userService;
1447 * Get the publicationDAO.
1448 * @return the publicationDAO
1450 public PublicationDAO getPublicationDAO() {
1451 return _publicationDAO;
1455 * Set the publicationDAO.
1456 * @param publicationDAO the publicationDAO to set
1458 public void setPublicationDAO(final PublicationDAO publicationDAO) {
1459 _publicationDAO = publicationDAO;
1463 * Get the repositoryService.
1464 * @return the repositoryService
1466 public RepositoryService getRepositoryService() {
1467 return _repositoryService;
1471 * Set the repositoryService.
1472 * @param repositoryService the repositoryService to set
1474 public void setRepositoryService(final RepositoryService repositoryService) {
1475 _repositoryService = repositoryService;
1479 * Get the documentDAO.
1480 * @return the documentDAO
1482 public DocumentDAO getDocumentDAO() {
1483 return _documentDAO;
1487 * Set the documentDAO.
1488 * @param documentDAO the documentDAO to set
1490 public void setDocumentDAO(final DocumentDAO documentDAO) {
1491 _documentDAO = documentDAO;
1495 * Get the descriptionAttributeDAO.
1496 * @return the descriptionAttributeDAO
1498 public DescriptionAttributeDAO getDescriptionAttributeDAO() {
1499 return _descriptionAttributeDAO;
1503 * Set the descriptionAttributeDAO.
1504 * @param descriptionAttributeDAO the descriptionAttributeDAO to set
1506 public void setDescriptionAttributeDAO(
1507 final DescriptionAttributeDAO descriptionAttributeDAO) {
1508 _descriptionAttributeDAO = descriptionAttributeDAO;