+ /**
+ * {@inheritDoc}
+ *
+ * @see org.splat.service.ScenarioService#getStudyScenarios(java.lang.Long)
+ */
+ @Override
+ @Transactional(readOnly = true)
+ public List<ScenarioDTO> getStudyScenarios(final Long studyId) {
+ DetachedCriteria query = DetachedCriteria
+ .forClass(Scenario.class, "scen")
+ .add(Restrictions.eq("owner.rid", studyId))
+ .setProjection(
+ Projections.projectionList().add(
+ Projections.property("scen.title"), "title")
+ .add(Projections.property("scen.rid"), "index"))
+ .setResultTransformer(
+ Transformers.aliasToBean(ScenarioDTO.class));
+ return getScenarioDAO().getFilteredDTOList(query);
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @see org.splat.service.ScenarioService#copyStudyContent(long, long, int, long)
+ */
+ @Override
+ @Transactional
+ public void copyStudyContent(final long fromStudyId, final long fromScenId,
+ final int finalStepNum, final long toStudyId)
+ throws InvalidParameterException, MissedPropertyException,
+ InvalidPropertyException, MultiplyDefinedException,
+ NotApplicableException, IOException {
+ Study fromStudy = getStudyService().selectStudy(fromStudyId);
+ if (fromStudy == null) {
+ throw new InvalidParameterException(MessageKeyEnum.STD_000002
+ .toString(), String.valueOf(fromStudyId));
+ }
+ Scenario fromScen = null;
+ for (Scenario scen : fromStudy.getScenariiList()) {
+ if (scen.getIndex() == fromScenId) {
+ fromScen = scen;
+ }
+ }
+
+ Study toStudy = getStudyService().selectStudy(toStudyId);
+ if (toStudy == null) {
+ throw new InvalidParameterException(MessageKeyEnum.STD_000002
+ .toString(), String.valueOf(toStudy));
+ }
+
+ // Check if the step is applied to a scenario and scenario is defined
+ if (fromScen == null
+ && getStepsConfigService().stepInvolves(finalStepNum,
+ Scenario.class)) {
+ throw new InvalidParameterException(MessageKeyEnum.SCN_000006
+ .toString(), String.valueOf(fromScenId));
+ }
+
+ // Copy validation cycles
+ for (ValidationCycle fromCycle : fromStudy.getValidationCycles()
+ .values()) {
+ ValidationCycle cycle = new ValidationCycle(toStudy, fromCycle);
+ getValidationCycleDAO().create(cycle);
+ toStudy.addRelation(cycle.getContext());
+ toStudy.getValidationCycles().put(
+ cycle.getDocumentType().getName(), cycle); // Replaces the cycle if exists as default,
+ }
+
+ // Copy content of the study up to the given step
+ Map<Publication, Publication> oldToNewPub = new HashMap<Publication, Publication>();
+ copyDocs(fromStudy, toStudy, finalStepNum, oldToNewPub);
+ if (fromScen != null) {
+ copyDocs(fromScen, toStudy.getScenariiList().get(0), finalStepNum,
+ oldToNewPub);
+ }
+ copyDependencies(fromStudy, finalStepNum, oldToNewPub);
+ if (fromScen != null) {
+ copyDependencies(fromScen, finalStepNum, oldToNewPub);
+ }
+ }
+
+ /**
+ * Copy dependencies between documents from the given project element up to <BR>
+ * the given step according to the given map of old publications to new publications.
+ *
+ * @param from
+ * the source project element
+ * @param finalStepNum
+ * the final step for copy processing
+ * @param oldToNewPub
+ * the old to new publications map
+ */
+ private void copyDependencies(final ProjectElement from,
+ final int finalStepNum,
+ final Map<Publication, Publication> oldToNewPub) {
+ // Copy dependencies between copied documents
+ for (Publication pub : from.getDocums()) {
+ // If the document in the step before the final one
+ if (pub.value().getStep() <= finalStepNum) {
+ Publication newPub = oldToNewPub.get(pub);
+ for (Publication used : pub.getRelations(UsesRelation.class)) {
+ newPub.addDependency(oldToNewPub.get(used));
+ }
+ }
+ }
+ }
+
+ /**
+ * Copy documents with dependencies up to the given step.
+ *
+ * @param from
+ * the source project element
+ * @param to
+ * the destination project element
+ * @param finalStepNum
+ * the final step for copy process
+ * @param oldToNewPub2
+ * @throws MissedPropertyException
+ * if document creation is failed
+ * @throws InvalidPropertyException
+ * if document creation is failed
+ * @throws MultiplyDefinedException
+ * if document creation is failed
+ * @throws IOException
+ * if document file creation is failed
+ * @throws NotApplicableException
+ * if document state is not applicable
+ * @param oldToNewPub
+ * the old to new publications map
+ *
+ */
+ private void copyDocs(final ProjectElement from, final ProjectElement to,
+ final int finalStepNum,
+ final Map<Publication, Publication> oldToNewPub)
+ throws MissedPropertyException, InvalidPropertyException,
+ MultiplyDefinedException, NotApplicableException, IOException {
+ Map<Integer, Step> steps = getProjectElementService().getStepsMap(to);
+ // Copy publications without old versions and relations to not copied steps documents
+ for (Publication pub : from.getDocums()) {
+ // If the document in the step before the final one
+ if (pub.value().getStep() <= finalStepNum) {
+ // Copy the document
+ oldToNewPub.put(pub, createDoc(pub.value(), steps.get(pub
+ .value().getStep())));
+ }
+ }
+ }
+
+ /**
+ * Create a copy of the given document and publish it in the given step.
+ *
+ * @param fromDoc
+ * the source document
+ * @param step
+ * the destination step
+ * @return the created publication
+ * @throws MissedPropertyException
+ * if document creation is failed
+ * @throws InvalidPropertyException
+ * if document creation is failed
+ * @throws MultiplyDefinedException
+ * if document creation is failed
+ * @throws IOException
+ * if document file creation is failed
+ * @throws NotApplicableException
+ * if document state is not applicable
+ */
+ private Publication createDoc(final Document fromDoc, final Step step)
+ throws MissedPropertyException, InvalidPropertyException,
+ MultiplyDefinedException, IOException, NotApplicableException {
+
+ java.io.File upfile = fromDoc.getSourceFile().asFile();
+ // Creation of the document
+ Document.Properties dprop = new Document.Properties().setName(
+ fromDoc.getTitle()).setType(fromDoc.getType()).setFormat(
+ fromDoc.getFormat()).setAuthor(fromDoc.getAuthor());
+ dprop.setLocalPath(upfile.getPath());
+ Publication addoc = getStepService().createDocument(step, dprop);
+ copyFile(upfile, addoc.getSourceFile());
+ getPublicationService().saveAs(addoc, fromDoc.getProgressState());
+
+ // Copy attached files
+ for (Relation rel : addoc.value().getRelations(ConvertsRelation.class)) {
+ File attach = ((ConvertsRelation) rel).getTo();
+ ConvertsRelation export = getPublicationService().attach(addoc,
+ attach.getFormat());
+ copyFile(attach.asFile(), export.getTo());
+ }
+ return addoc;
+ }
+
+ /**
+ * Copy a file. Print info message.
+ *
+ * @param upfile
+ * the source file.
+ * @param targetFile
+ * the target file
+ * @throws IOException
+ * if failed
+ */
+ private void copyFile(final java.io.File upfile, final File targetFile)
+ throws IOException {
+ if (LOG.isInfoEnabled()) {
+ LOG.info("Copy " + upfile.getAbsolutePath() + TO
+ + targetFile.asFile().getPath());
+ }
+ IOUtils.copy(upfile, targetFile.asFile());
+ }
+