1 /*****************************************************************************
5 * Creation date 06.10.2012
8 *****************************************************************************/
10 package org.splat.service;
13 import java.io.FileNotFoundException;
14 import java.io.IOException;
15 import java.text.ParseException;
16 import java.util.ArrayList;
17 import java.util.Date;
18 import java.util.HashSet;
19 import java.util.Iterator;
20 import java.util.List;
22 import org.apache.log4j.Logger;
23 import org.splat.common.properties.MessageKeyEnum;
24 import org.splat.dal.bo.kernel.User;
25 import org.splat.dal.bo.som.ConvertsRelation;
26 import org.splat.dal.bo.som.Document;
27 import org.splat.dal.bo.som.DocumentType;
28 import org.splat.dal.bo.som.ProgressState;
29 import org.splat.dal.bo.som.ProjectElement;
30 import org.splat.dal.bo.som.Publication;
31 import org.splat.dal.bo.som.SimulationContext;
32 import org.splat.dal.bo.som.SimulationContextType;
33 import org.splat.dal.bo.som.Study;
34 import org.splat.dal.bo.som.Timestamp;
35 import org.splat.dal.bo.som.UsedByRelation;
36 import org.splat.dal.bo.som.ValidationCycle;
37 import org.splat.dal.bo.som.ValidationStep;
38 import org.splat.dal.dao.som.ProjectElementDAO;
39 import org.splat.dal.dao.som.PublicationDAO;
40 import org.splat.dal.dao.som.TimestampDAO;
41 import org.splat.exception.IncompatibleDataException;
42 import org.splat.exception.InvalidParameterException;
43 import org.splat.kernel.InvalidPropertyException;
44 import org.splat.kernel.MismatchException;
45 import org.splat.kernel.MissedPropertyException;
46 import org.splat.kernel.MultiplyDefinedException;
47 import org.splat.kernel.NotApplicableException;
48 import org.splat.manox.Reader;
49 import org.splat.manox.Toolbox;
50 import org.splat.service.dto.DocToCompareDTO;
51 import org.splat.service.technical.RepositoryService;
52 import org.splat.som.DocumentRights;
53 import org.splat.som.Revision;
54 import org.splat.som.Step;
55 import org.springframework.transaction.annotation.Transactional;
58 * Publication service implementation.
60 * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
62 public class PublicationServiceImpl implements PublicationService {
65 * Logger for this class.
67 protected final static Logger LOG = Logger
68 .getLogger(PublicationServiceImpl.class);
71 * Injected study service.
73 private StudyService _studyService;
75 * Injected step service.
77 private StepService _stepService;
79 * Injected document service.
81 private DocumentService _documentService;
83 * Injected document type service.
85 private DocumentTypeService _documentTypeService;
87 * Injected user service.
89 private UserService _userService;
91 * Injected project element service.
93 private ProjectElementService _projectElementService;
95 * Injected simulation context service.
97 private SimulationContextService _simulationContextService;
99 * Injected publication DAO.
101 private PublicationDAO _publicationDAO;
103 * Injected timestamp DAO.
105 private TimestampDAO _timestampDAO;
107 * Injected project element DAO.
109 private ProjectElementDAO _projectElementDAO;
111 * Injected repository service.
113 private RepositoryService _repositoryService;
118 * @see org.splat.service.PublicationService#copy(org.splat.dal.bo.som.Publication, org.splat.dal.bo.som.ProjectElement)
120 public Publication copy(final Publication aPublication,
121 final ProjectElement publisher) {
122 Publication copy = new Publication();
123 copy.setValue(aPublication.value());
124 copy.setStep(aPublication.getStep()); // May not be initialized yet
125 copy.setOwner(publisher);
126 copy.setIsnew(aPublication.getIsnew());
127 if (!copy.getOwnerStudy().equals(aPublication.getOwnerStudy())) {
128 copy.setIsnew('N'); // The referenced document is not new for the given study
136 * @see org.splat.service.PublicationService#createDoc(long, org.splat.som.Step, long, long, java.lang.String, java.lang.String,
137 * org.splat.dal.bo.som.ProgressState, java.lang.String, java.lang.String, java.util.Date, java.util.List)
140 public Publication createDoc(final long ownerId, final Step step,
141 final long documentTypeId, final long userId, final String fname,
142 final String doctitle, final ProgressState docstate,
143 final String reference, final String version, final Date docDate,
144 final List<Long> docuses) throws MissedPropertyException,
145 InvalidPropertyException, MultiplyDefinedException, IOException,
146 NotApplicableException, InterruptedException, ParseException {
147 DocumentType type = getDocumentTypeService().selectType(
148 (int) documentTypeId);
149 User user = getUserService().selectUser(userId);
150 File updir = getRepositoryService().getDownloadDirectory(user);
151 File upfile = new File(updir.getPath() + "/" + fname);
152 String[] table = fname.split("\\x2E");
154 // Creation of the document
155 Document.Properties dprop = new Document.Properties();
156 dprop.setLocalPath(upfile.getPath());
159 if (reference.length() == 0) { // Importation of a foreign document
160 // TODO: Extract property of supported documents (DOCX, ODT...)
161 addoc = getStepService().createDocument(
163 dprop.setName(doctitle).setType(type).setFormat(
164 table[table.length - 1]).setAuthor(user));
165 moveFile(upfile, addoc);
167 saveAs(addoc, docstate); // May throw FileNotFound if rename was not done
168 } catch (FileNotFoundException saverror) {
170 LOG.info("Waiting for the file.");
171 upfile.renameTo(updir);
172 saveAs(addoc, docstate); // Forget it if throw again FileNotFound
174 } else { // Importation of a previously created template-based document
175 if (docDate != null) {
176 dprop.setDate(docDate);
178 addoc = getStepService().assignDocument(step,
179 dprop.setReference(reference).setName(doctitle));
180 moveFile(upfile, addoc);
182 if (version.length() > 0) {
183 saveAs(addoc, new Revision(version));
185 saveAs(addoc, docstate);
187 } catch (FileNotFoundException saverror) {
189 LOG.info("Waiting for the file.");
190 upfile.renameTo(updir);
191 if (version.length() > 0) {
192 saveAs(addoc, new Revision(version));
194 saveAs(addoc, docstate);
198 // Creation of uses relations
199 if (docuses != null) {
200 for (Long index : docuses) {
201 Document used = getDocumentService().selectDocument(index);
202 addoc.addDependency(used);
209 * Move a file into the repository as a source file of the document.
212 * the uploaded file to move
216 private void moveFile(final File upfile, final Publication addoc) {
217 File updir = addoc.getSourceFile().asFile();
218 if (LOG.isInfoEnabled()) {
219 LOG.info("Moving \"" + upfile.getAbsolutePath() + "\" to \""
220 + updir.getPath() + "\".");
222 upfile.renameTo(updir);
228 * @see org.splat.service.PublicationService#versionDocument(org.splat.som.Step, org.splat.dal.bo.kernel.User, java.lang.String, long,
229 * java.lang.String, java.lang.String, org.splat.dal.bo.som.ProgressState, java.util.Date, java.lang.String[], long[])
232 public void versionDocument(final Step step, final User user,
233 final String filename, final long docIndex, final String docver,
234 final String summary, final ProgressState state, final Date date,
235 final String[] docuses, final long[] docusedby)
236 throws MissedPropertyException, InvalidPropertyException,
237 MultiplyDefinedException, IOException, MismatchException,
238 NotApplicableException, InterruptedException {
239 File updir = getRepositoryService().getDownloadDirectory(user);
240 File upfile = new File(updir.getPath() + "/" + filename);
242 // Versioning of the document
243 Document.Properties dprop = new Document.Properties();
244 dprop.setLocalPath(upfile.getPath());
245 Publication current = step.getDocument(docIndex);
248 if ((docver.length() != 0) && // Importation of a not foreign document
252 if ((summary != null) && (summary.length() > 0)) {
253 dprop.setDescription(summary);
255 next = getStepService().versionDocument(step, current,
256 dprop.setAuthor(user));
257 moveFile(upfile, next);
260 if (docver.length() == 0) { // Importation of a foreign document
261 saveAs(next, state); // May throw FileNotFound if rename was not done
263 saveAs(next, new Revision(docver));
265 } catch (FileNotFoundException saverror) {
267 LOG.info("Waiting for the file.");
268 upfile.renameTo(updir);
271 // TODO: Remove current document details from the contents of open study
273 // Creation of uses relations
274 Publication syncronizedCurrent = _publicationDAO.get(current.getIndex());
275 updateRelations(syncronizedCurrent, next, docuses, docusedby);
279 * Update relations after creation of a new document version.
282 * the current version
286 * ids of used documents
288 * ids of documents used by the versioned one.
290 private void updateRelations(final Publication current,
291 final Publication next, final String[] docuses,
292 final long[] docusedby) {
293 if (docuses != null) {
294 for (int i = 0; i < docuses.length; i++) {
295 Long index = Long.valueOf(docuses[i].trim());
296 Document used = getDocumentService().selectDocument(index);// RKV: getPublication(index, steps);
297 next.addDependency(used);
300 // Outdating impacted document
301 HashSet<Long> compatible = new HashSet<Long>();
302 if (docusedby != null) {
303 for (int i = 0; i < docusedby.length; i++) {
304 compatible.add(docusedby[i]);
307 List<Publication> relist = current.getRelations(UsedByRelation.class);
308 for (Iterator<Publication> i = relist.iterator(); i.hasNext();) {
309 Publication using = i.next();
310 if (!compatible.contains(using.getIndex())) {
317 * protected Publication getPublication(int index, List<Step> steps) { for (Iterator<Step> i = steps.iterator(); i.hasNext();) { List<Publication>
318 * published = i.next().getAllDocuments(); for (Iterator<Publication> j = published.iterator(); j.hasNext();) { Publication found =
319 * j.next(); // In a given study step, if (found.value().getIndex() == index) return found; // there is only one publication of a given
320 * document } } return null; }
325 * @see org.splat.service.PublicationService#approve(org.splat.dal.bo.som.Publication, java.util.Date)
328 public Timestamp approve(final Publication aPublication, final Date adate) {
329 Timestamp res = null;
330 if (!(aPublication.isOutdated() || (aPublication.value()
331 .getProgressState() != ProgressState.inCHECK))) {
332 DocumentType type = aPublication.value().getType();
333 Study owner = aPublication.getOwnerStudy();
334 ValidationCycle cycle = getStudyService().getValidationCycleOf(
336 User approver = cycle.getActor(ValidationStep.APPROVAL);
337 Timestamp stamp = new Timestamp(ValidationStep.APPROVAL,
338 aPublication.value(), approver, adate);
339 getTimestampDAO().create(stamp);
341 if (getDocumentService().promote(aPublication.value(), stamp)) {
343 // if (getDocumentService().isStudyResult(type)
344 // && owner.getProgressState() == ProgressState.inCHECK) {
345 // getStudyService().promote(owner);
349 return res; // Hoping that promotion of the study succeeded
355 * @see org.splat.service.PublicationService#demote(org.splat.dal.bo.som.Publication)
358 public boolean demote(final Publication aPublication) {
360 DocumentType type = aPublication.value().getType();
361 Study owner = aPublication.getOwnerStudy();
363 if (aPublication.value().getProgressState() == ProgressState.inCHECK) {
364 ValidationCycle cycle = getStudyService().getValidationCycleOf(
366 if (cycle.enables(ValidationStep.REVIEW)) {
367 res = getDocumentService().demote(aPublication.value());
369 res = getDocumentService().demote(aPublication.value());
371 getDocumentService().demote(aPublication.value());
374 } else if (aPublication.value().getProgressState() == ProgressState.inDRAFT) {
375 res = getDocumentService().demote(aPublication.value());
377 // if (res && getDocumentService().isStudyResult(type)
378 // && owner.getProgressState() != ProgressState.inWORK) {
379 // getStudyService().demote(owner);
387 * @see org.splat.service.PublicationService#invalidate(org.splat.dal.bo.som.Publication)
390 public boolean invalidate(final Publication aPublication) {
392 if ((aPublication.value().getProgressState() == ProgressState.inCHECK)) {
393 DocumentType type = aPublication.value().getType();
394 Study owner = aPublication.getOwnerStudy();
396 ValidationCycle cycle = getStudyService().getValidationCycleOf(
398 // Check if the validation cycle allows the review step
399 if (cycle.enables(ValidationStep.REVIEW)) {
400 // if (getDocumentService().demote(aPublication.value())
401 // && getDocumentService().isStudyResult(type)
402 // && owner.getProgressState() == ProgressState.inCHECK) {
403 // getStudyService().demote(owner);
406 } else { // If the validation cycle has no inDraft step
407 res = demote(aPublication);
416 * @see org.splat.service.PublicationService#promote(org.splat.dal.bo.som.Publication, java.util.Date)
419 public Timestamp promote(final Publication aPublication, final Date pdate) {
420 Timestamp res = null;
421 if ((!aPublication.isOutdated())
422 && (aPublication.value().getProgressState() == ProgressState.inWORK)) {
423 DocumentType type = aPublication.value().getType();
424 Study owner = aPublication.getOwnerStudy();
425 ValidationCycle cycle = getStudyService().getValidationCycleOf(
427 User promoter = cycle.getActor(ValidationStep.PROMOTION);
428 if (promoter == null) {
429 promoter = getInvolvedStep(aPublication).getActor();
431 if (promoter == null) {
432 promoter = owner.getAuthor();
434 Timestamp stamp = new Timestamp(ValidationStep.PROMOTION,
435 aPublication.value(), promoter, pdate);
436 getTimestampDAO().create(stamp);
438 if (getDocumentService().promote(aPublication.value(), stamp)) {
440 if (!cycle.enables(ValidationStep.REVIEW)) {
441 getDocumentService().promote(aPublication.value(), null);
443 // if (getDocumentService().isStudyResult(type)
444 // && owner.getProgressState() == ProgressState.inWORK) {
445 // getStudyService().promote(owner);
449 return res; // Hoping that promotion of the study succeeded
455 * @see org.splat.service.PublicationService#review(org.splat.dal.bo.som.Publication, java.util.Date)
458 public Timestamp review(final Publication aPublication, final Date rdate) {
459 Timestamp res = null;
460 if (!aPublication.isOutdated()
461 && !(aPublication.value().getProgressState() != ProgressState.inDRAFT)) {
463 DocumentType type = aPublication.value().getType();
464 Study owner = aPublication.getOwnerStudy();
465 ValidationCycle cycle = getStudyService().getValidationCycleOf(
467 User reviewer = cycle.getActor(ValidationStep.REVIEW);
468 Timestamp stamp = new Timestamp(ValidationStep.REVIEW, aPublication
469 .value(), reviewer, rdate);
470 getTimestampDAO().create(stamp);
472 if (getDocumentService().promote(aPublication.value(), stamp)) {
474 // if (getDocumentService().isStudyResult(type)
475 // && owner.getProgressState() == ProgressState.inDRAFT) {
476 // getStudyService().promote(owner);
480 return res; // Hoping that promotion of the study succeeded
486 * @see org.splat.service.PublicationService#saveAs(org.splat.dal.bo.som.Publication, org.splat.som.Revision)
491 public void saveAs(final Publication aPublication, final Revision newvers)
492 throws FileNotFoundException, NotApplicableException {
493 if (aPublication.value().isUndefined()) {
494 throw new NotApplicableException(
495 "Cannot save a Publication object refering an undefined Document");
497 if (!aPublication.value().getSourceFile().exists()) {
498 throw new FileNotFoundException();
501 getPublicationDAO().create(aPublication); // Must be done before updating the study in order to fix this final (rid-based) hascode
502 getDocumentService().updateAs(aPublication.value(), newvers); // May change the branch name of given revision
503 updateOwner(aPublication);
509 * @see org.splat.service.PublicationService#saveAs(org.splat.dal.bo.som.Publication, org.splat.dal.bo.som.ProgressState)
512 public void saveAs(final Publication aPublication, final ProgressState state)
513 throws FileNotFoundException, NotApplicableException {
514 if (aPublication.value().isUndefined()) {
515 throw new NotApplicableException(
516 "Cannot save a Publication object refering an undefined Document");
518 if (!aPublication.value().getSourceFile().exists()) {
519 throw new FileNotFoundException(aPublication.value()
520 .getSourceFile().asFile().getAbsolutePath());
523 if (state == ProgressState.inWORK || state == ProgressState.EXTERN) {
524 getPublicationDAO().create(aPublication); // Must be done before updating the study in order to fix this final (rid-based)
526 getDocumentService().updateAs(aPublication.value(), state);
528 // Check that the state is applicable for the validation cycle
529 DocumentType mytype = aPublication.value().getType();
530 Study owner = aPublication.getOwnerStudy();
531 ValidationCycle cycle = getStudyService().getValidationCycleOf(
533 boolean review = cycle.enables(ValidationStep.REVIEW);
534 if (!(state == ProgressState.inDRAFT && review)
535 && !(state == ProgressState.inCHECK && !review)) {
536 throw new NotApplicableException(MessageKeyEnum.DCT_000003
537 .toString(), state.toString());
539 getPublicationDAO().create(aPublication); // Must be done before updating the study in order to fix this final (rid-based)
541 getDocumentService().updateAs(aPublication.value(),
542 ProgressState.inWORK);
544 promote(aPublication, aPublication.value()
545 .getLastModificationDate()); // Promotes to the appropriate state in accordance to the validation cycle
547 updateOwner(aPublication);
551 * Update an owner of the publication.
553 * @param aPublication
554 * the document publication
557 private void updateOwner(final Publication aPublication) {
558 Step step = getInvolvedStep(aPublication);
560 // Update of involved step
561 Document previous = aPublication.value().getPreviousVersion();
562 if (previous != null) {
563 Publication oldoc = step.getDocument(previous.getIndex());
564 getStepService().remove(step, oldoc); // Decrements the configuration tag count of document
566 getStepService().add(step, aPublication); // Increments the configuration tag count of document
568 // Import the document properties and update of the study
569 forwardProperties(aPublication, aPublication.value().getSourceFile()
571 getProjectElementDAO().merge(aPublication.getOwner());
575 * Propagate simulation contexts from the given config file to the publication's owner (study or step).
577 * @param aPublication
578 * the document publication
584 private void forwardProperties(final Publication aPublication,
585 final java.io.File from, final Step to) {
586 Reader tool = Toolbox.getReader(from);
587 if (tool != null) { // Properties extractor available for this type of document
588 SimulationContextType.Properties sprop = new SimulationContextType.Properties()
589 .setStep(to.getStep()).setProgressState(
590 ProgressState.APPROVED);
591 List<SimulationContextType> contype = getSimulationContextService()
592 .selectTypesWhere(sprop);
593 if (!contype.isEmpty()) { // There is an approved property type configured at this step
595 SimulationContext.Properties cprop = new SimulationContext.Properties();
596 List<SimulationContext> context = to.getAllSimulationContexts();
598 context = new ArrayList<SimulationContext>(context.size());
599 context.addAll(to.getAllSimulationContexts());
600 cprop.disableCheck();
601 for (Iterator<SimulationContextType> i = contype.iterator(); i
603 SimulationContextType property = i.next();
604 boolean isFound = false;
605 for (Iterator<SimulationContext> j = context.iterator(); j
607 SimulationContext existing = j.next();
608 isFound = existing.getType().equals(property);
610 // Forget this property as it is already set
616 String value = tool.extractProperty(property
619 continue; // Property not defined into the document
622 cprop.setType(property).setValue(value);
623 if (aPublication.getOwner() instanceof Study) {
624 getStudyService().addProjectContext(
625 (Study) aPublication.getOwner(), cprop); // Re-indexes knowledges and the study
628 .addSimulationContext(to, cprop); // Re-indexes knowledges only
630 } catch (Exception e) {
640 * Returns the study Step into which the document version referenced by this publication has been published.
642 * @param aPublication
643 * the document publication
644 * @return the study step where the document is published
646 public Step getInvolvedStep(final Publication aPublication) {
647 if (aPublication.getStep() == null) {
648 Step[] step = getProjectElementService().getSteps(
649 aPublication.getOwner());
650 for (int i = 0; i < step.length; i++) {
651 aPublication.setStep(step[i]); // The involved step necessarily exists
652 if (aPublication.value().isInto(aPublication.getStep())) {
657 return aPublication.getStep();
661 * Undo the out-date operation.
663 * @param aPublication
665 * @return true if the acceptance succeeds
667 * @see DocumentRights#canAccept()
670 public boolean actualize(final Publication aPublication) {
671 boolean res = aPublication.isOutdated();
673 aPublication.setIsnew('Y');
674 getPublicationDAO().update(aPublication);
680 * Out-dates this publication and recursively all publications using this one. Typically, a publication is out-dated when modifying a
681 * document to which it depends.
683 * @param aPublication
686 * @see #getProgressState()
689 public void outdate(final Publication aPublication) {
690 if (aPublication.isOutdated()) {
694 List<Publication> relist = aPublication
695 .getRelations(UsedByRelation.class);
696 for (Iterator<Publication> i = relist.iterator(); i.hasNext();) {
699 aPublication.setIsnew('O');
700 getPublicationDAO().update(aPublication);
704 * Create "Converts" relation for the given document publication and format.
706 * @param aPublication
707 * the document publication
710 * @return the created "Converts" relation
713 public ConvertsRelation attach(final Publication aPublication,
714 final String format) {
715 return getDocumentService().attach(aPublication.value(), format);
719 * Create "Converts" relation for the given document publication, format and description.
721 * @param aPublication
722 * the document publication
726 * the description of the relation
727 * @return the created "Converts" relation
730 public ConvertsRelation attach(final Publication aPublication,
731 final String format, final String description) {
732 return getDocumentService().attach(aPublication.value(), format,
737 * Rename the published document.
739 * @param aPublication
740 * the publication of the document
742 * the new document title
743 * @throws InvalidPropertyException
744 * if the new title is empty
746 public void rename(final Publication aPublication, final String title)
747 throws InvalidPropertyException {
748 getDocumentService().rename(aPublication.value(), title);
754 * @see org.splat.service.PublicationService#getDocToCompareDTO(long)
756 @Transactional(readOnly = true)
758 public DocToCompareDTO getDocToCompareDTO(final long publicationId)
759 throws InvalidParameterException {
760 DocToCompareDTO res = new DocToCompareDTO();
761 Publication pub = _publicationDAO.get(Long.valueOf(publicationId));
763 throw new InvalidParameterException("id", String
764 .valueOf(publicationId));
767 res.setDocumentTitle(pub.value().getTitle());
768 res.setPathToFile(pub.value().getFile().asFile().getAbsolutePath());
769 res.setScenarioTitle(pub.getOwner().getTitle());
770 res.setStudyTitle(pub.getOwnerStudy().getTitle());
775 * Get the projectElementService.
777 * @return the projectElementService
779 public ProjectElementService getProjectElementService() {
780 return _projectElementService;
784 * Set the projectElementService.
786 * @param projectElementService
787 * the projectElementService to set
789 public void setProjectElementService(
790 final ProjectElementService projectElementService) {
791 _projectElementService = projectElementService;
795 * Get the simulationContextService.
797 * @return the simulationContextService
799 public SimulationContextService getSimulationContextService() {
800 return _simulationContextService;
804 * Set the simulationContextService.
806 * @param simulationContextService
807 * the simulationContextService to set
809 public void setSimulationContextService(
810 final SimulationContextService simulationContextService) {
811 _simulationContextService = simulationContextService;
815 * Get the studyService.
817 * @return the studyService
819 public StudyService getStudyService() {
820 return _studyService;
824 * Set the studyService.
826 * @param studyService
827 * the studyService to set
829 public void setStudyService(final StudyService studyService) {
830 _studyService = studyService;
834 * Get the stepService.
836 * @return the stepService
838 public StepService getStepService() {
843 * Set the stepService.
846 * the stepService to set
848 public void setStepService(final StepService stepService) {
849 _stepService = stepService;
853 * Get the documentService.
855 * @return the documentService
857 public DocumentService getDocumentService() {
858 return _documentService;
862 * Set the documentService.
864 * @param documentService
865 * the documentService to set
867 public void setDocumentService(final DocumentService documentService) {
868 _documentService = documentService;
872 * Get the publicationDAO.
874 * @return the publicationDAO
876 public PublicationDAO getPublicationDAO() {
877 return _publicationDAO;
881 * Set the publicationDAO.
883 * @param publicationDAO
884 * the publicationDAO to set
886 public void setPublicationDAO(final PublicationDAO publicationDAO) {
887 _publicationDAO = publicationDAO;
891 * Get the projectElementDAO.
893 * @return the projectElementDAO
895 public ProjectElementDAO getProjectElementDAO() {
896 return _projectElementDAO;
900 * Set the projectElementDAO.
902 * @param projectElementDAO
903 * the projectElementDAO to set
905 public void setProjectElementDAO(final ProjectElementDAO projectElementDAO) {
906 _projectElementDAO = projectElementDAO;
910 * Get the repositoryService.
912 * @return the repositoryService
914 public RepositoryService getRepositoryService() {
915 return _repositoryService;
919 * Set the repositoryService.
921 * @param repositoryService
922 * the repositoryService to set
924 public void setRepositoryService(final RepositoryService repositoryService) {
925 _repositoryService = repositoryService;
929 * Get the timestampDAO.
931 * @return the timestampDAO
933 public TimestampDAO getTimestampDAO() {
934 return _timestampDAO;
938 * Set the timestampDAO.
940 * @param timestampDAO
941 * the timestampDAO to set
943 public void setTimestampDAO(final TimestampDAO timestampDAO) {
944 _timestampDAO = timestampDAO;
948 * Get the documentTypeService.
950 * @return the documentTypeService
952 public DocumentTypeService getDocumentTypeService() {
953 return _documentTypeService;
957 * Set the documentTypeService.
959 * @param documentTypeService
960 * the documentTypeService to set
962 public void setDocumentTypeService(
963 final DocumentTypeService documentTypeService) {
964 _documentTypeService = documentTypeService;
968 * Get the userService.
970 * @return the userService
972 public UserService getUserService() {
977 * Set the userService.
980 * the userService to set
982 public void setUserService(final UserService userService) {
983 _userService = userService;
989 * @see org.splat.service.PublicationService#replace(long, java.io.File)
993 public boolean replace(final Publication pub, final File newFile,
994 final Date modifTime) throws IncompatibleDataException {
995 if (!(ProgressState.EXTERN.equals(pub.getProgressState()) || ProgressState.inWORK
996 .equals(pub.getProgressState()))) {
997 throw new IncompatibleDataException(MessageKeyEnum.DCT_000004
1000 Document doc = getDocumentService().selectDocument(
1001 pub.value().getIndex());
1002 if (LOG.isInfoEnabled()) {
1003 LOG.info("Moving \"" + newFile.getName() + "\" to \""
1004 + doc.getSourceFile().asFile().getAbsolutePath() + "\".");
1006 // Save a temporary copy of the original file as <old file name>.backup
1007 String oldFilePath = doc.getSourceFile().asFile().getAbsolutePath();
1008 File oldFile = new File(oldFilePath);
1009 File backupFile = new File(oldFilePath + ".backup");
1010 oldFile.renameTo(backupFile);
1011 boolean res = newFile.renameTo(oldFile);
1013 // Delete the temporary copy of the old file
1014 // if the new one is moved into the repository.
1015 backupFile.delete();
1016 // Update the document modification date.
1017 doc.setLastModificationDate(modifTime);
1018 // Update presentation data
1019 pub.value().setLastModificationDate(modifTime);
1021 // Restore the original file if replacing is failed
1022 backupFile.renameTo(oldFile);