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 updateRelations(current, next, docuses, docusedby);
278 * Update relations after creation of a new document version.
281 * the current version
285 * ids of used documents
287 * ids of documents used by the versioned one.
289 private void updateRelations(final Publication current,
290 final Publication next, final String[] docuses,
291 final long[] docusedby) {
292 if (docuses != null) {
293 for (int i = 0; i < docuses.length; i++) {
294 Long index = Long.valueOf(docuses[i].trim());
295 Document used = getDocumentService().selectDocument(index);// RKV: getPublication(index, steps);
296 next.addDependency(used);
299 // Outdating impacted document
300 HashSet<Long> compatible = new HashSet<Long>();
301 if (docusedby != null) {
302 for (int i = 0; i < docusedby.length; i++) {
303 compatible.add(docusedby[i]);
306 List<Publication> relist = current.getRelations(UsedByRelation.class);
307 for (Iterator<Publication> i = relist.iterator(); i.hasNext();) {
308 Publication using = i.next();
309 if (!compatible.contains(using.getIndex())) {
316 * protected Publication getPublication(int index, List<Step> steps) { for (Iterator<Step> i = steps.iterator(); i.hasNext();) { List<Publication>
317 * published = i.next().getAllDocuments(); for (Iterator<Publication> j = published.iterator(); j.hasNext();) { Publication found =
318 * j.next(); // In a given study step, if (found.value().getIndex() == index) return found; // there is only one publication of a given
319 * document } } return null; }
324 * @see org.splat.service.PublicationService#approve(org.splat.dal.bo.som.Publication, java.util.Date)
327 public Timestamp approve(final Publication aPublication, final Date adate) {
328 Timestamp res = null;
329 if (!(aPublication.isOutdated() || (aPublication.value()
330 .getProgressState() != ProgressState.inCHECK))) {
331 DocumentType type = aPublication.value().getType();
332 Study owner = aPublication.getOwnerStudy();
333 ValidationCycle cycle = getStudyService().getValidationCycleOf(
335 User approver = cycle.getActor(ValidationStep.APPROVAL);
336 Timestamp stamp = new Timestamp(ValidationStep.APPROVAL,
337 aPublication.value(), approver, adate);
338 getTimestampDAO().create(stamp);
340 if (getDocumentService().promote(aPublication.value(), stamp)) {
342 // if (getDocumentService().isStudyResult(type)
343 // && owner.getProgressState() == ProgressState.inCHECK) {
344 // getStudyService().promote(owner);
348 return res; // Hoping that promotion of the study succeeded
354 * @see org.splat.service.PublicationService#demote(org.splat.dal.bo.som.Publication)
357 public boolean demote(final Publication aPublication) {
359 DocumentType type = aPublication.value().getType();
360 Study owner = aPublication.getOwnerStudy();
362 if (aPublication.value().getProgressState() == ProgressState.inCHECK) {
363 ValidationCycle cycle = getStudyService().getValidationCycleOf(
365 if (cycle.enables(ValidationStep.REVIEW)) {
366 res = getDocumentService().demote(aPublication.value());
368 res = getDocumentService().demote(aPublication.value());
370 getDocumentService().demote(aPublication.value());
373 } else if (aPublication.value().getProgressState() == ProgressState.inDRAFT) {
374 res = getDocumentService().demote(aPublication.value());
376 // if (res && getDocumentService().isStudyResult(type)
377 // && owner.getProgressState() != ProgressState.inWORK) {
378 // getStudyService().demote(owner);
386 * @see org.splat.service.PublicationService#invalidate(org.splat.dal.bo.som.Publication)
389 public boolean invalidate(final Publication aPublication) {
391 if ((aPublication.value().getProgressState() == ProgressState.inCHECK)) {
392 DocumentType type = aPublication.value().getType();
393 Study owner = aPublication.getOwnerStudy();
395 ValidationCycle cycle = getStudyService().getValidationCycleOf(
397 // Check if the validation cycle allows the review step
398 if (cycle.enables(ValidationStep.REVIEW)) {
399 // if (getDocumentService().demote(aPublication.value())
400 // && getDocumentService().isStudyResult(type)
401 // && owner.getProgressState() == ProgressState.inCHECK) {
402 // getStudyService().demote(owner);
405 } else { // If the validation cycle has no inDraft step
406 res = demote(aPublication);
415 * @see org.splat.service.PublicationService#promote(org.splat.dal.bo.som.Publication, java.util.Date)
418 public Timestamp promote(final Publication aPublication, final Date pdate) {
419 Timestamp res = null;
420 if ((!aPublication.isOutdated())
421 && (aPublication.value().getProgressState() == ProgressState.inWORK)) {
422 DocumentType type = aPublication.value().getType();
423 Study owner = aPublication.getOwnerStudy();
424 ValidationCycle cycle = getStudyService().getValidationCycleOf(
426 User promoter = cycle.getActor(ValidationStep.PROMOTION);
427 if (promoter == null) {
428 promoter = getInvolvedStep(aPublication).getActor();
430 if (promoter == null) {
431 promoter = owner.getAuthor();
433 Timestamp stamp = new Timestamp(ValidationStep.PROMOTION,
434 aPublication.value(), promoter, pdate);
435 getTimestampDAO().create(stamp);
437 if (getDocumentService().promote(aPublication.value(), stamp)) {
439 if (!cycle.enables(ValidationStep.REVIEW)) {
440 getDocumentService().promote(aPublication.value(), null);
442 // if (getDocumentService().isStudyResult(type)
443 // && owner.getProgressState() == ProgressState.inWORK) {
444 // getStudyService().promote(owner);
448 return res; // Hoping that promotion of the study succeeded
454 * @see org.splat.service.PublicationService#review(org.splat.dal.bo.som.Publication, java.util.Date)
457 public Timestamp review(final Publication aPublication, final Date rdate) {
458 Timestamp res = null;
459 if (!aPublication.isOutdated()
460 && !(aPublication.value().getProgressState() != ProgressState.inDRAFT)) {
462 DocumentType type = aPublication.value().getType();
463 Study owner = aPublication.getOwnerStudy();
464 ValidationCycle cycle = getStudyService().getValidationCycleOf(
466 User reviewer = cycle.getActor(ValidationStep.REVIEW);
467 Timestamp stamp = new Timestamp(ValidationStep.REVIEW, aPublication
468 .value(), reviewer, rdate);
469 getTimestampDAO().create(stamp);
471 if (getDocumentService().promote(aPublication.value(), stamp)) {
473 // if (getDocumentService().isStudyResult(type)
474 // && owner.getProgressState() == ProgressState.inDRAFT) {
475 // getStudyService().promote(owner);
479 return res; // Hoping that promotion of the study succeeded
485 * @see org.splat.service.PublicationService#saveAs(org.splat.dal.bo.som.Publication, org.splat.som.Revision)
490 public void saveAs(final Publication aPublication, final Revision newvers)
491 throws FileNotFoundException, NotApplicableException {
492 if (aPublication.value().isUndefined()) {
493 throw new NotApplicableException(
494 "Cannot save a Publication object refering an undefined Document");
496 if (!aPublication.value().getSourceFile().exists()) {
497 throw new FileNotFoundException();
500 getPublicationDAO().create(aPublication); // Must be done before updating the study in order to fix this final (rid-based) hascode
501 getDocumentService().updateAs(aPublication.value(), newvers); // May change the branch name of given revision
502 updateOwner(aPublication);
508 * @see org.splat.service.PublicationService#saveAs(org.splat.dal.bo.som.Publication, org.splat.dal.bo.som.ProgressState)
511 public void saveAs(final Publication aPublication, final ProgressState state)
512 throws FileNotFoundException, NotApplicableException {
513 if (aPublication.value().isUndefined()) {
514 throw new NotApplicableException(
515 "Cannot save a Publication object refering an undefined Document");
517 if (!aPublication.value().getSourceFile().exists()) {
518 throw new FileNotFoundException(aPublication.value()
519 .getSourceFile().asFile().getAbsolutePath());
522 if (state == ProgressState.inWORK || state == ProgressState.EXTERN) {
523 getPublicationDAO().create(aPublication); // Must be done before updating the study in order to fix this final (rid-based)
525 getDocumentService().updateAs(aPublication.value(), state);
527 // Check that the state is applicable for the validation cycle
528 DocumentType mytype = aPublication.value().getType();
529 Study owner = aPublication.getOwnerStudy();
530 ValidationCycle cycle = getStudyService().getValidationCycleOf(
532 boolean review = cycle.enables(ValidationStep.REVIEW);
533 if (!(state == ProgressState.inDRAFT && review)
534 && !(state == ProgressState.inCHECK && !review)) {
535 throw new NotApplicableException(MessageKeyEnum.DCT_000003
536 .toString(), state.toString());
538 getPublicationDAO().create(aPublication); // Must be done before updating the study in order to fix this final (rid-based)
540 getDocumentService().updateAs(aPublication.value(),
541 ProgressState.inWORK);
543 promote(aPublication, aPublication.value()
544 .getLastModificationDate()); // Promotes to the appropriate state in accordance to the validation cycle
546 updateOwner(aPublication);
550 * Update an owner of the publication.
552 * @param aPublication
553 * the document publication
556 private void updateOwner(final Publication aPublication) {
557 Step step = getInvolvedStep(aPublication);
559 // Update of involved step
560 Document previous = aPublication.value().getPreviousVersion();
561 if (previous != null) {
562 Publication oldoc = step.getDocument(previous.getIndex());
563 getStepService().remove(step, oldoc); // Decrements the configuration tag count of document
565 getStepService().add(step, aPublication); // Increments the configuration tag count of document
567 // Import the document properties and update of the study
568 forwardProperties(aPublication, aPublication.value().getSourceFile()
570 getProjectElementDAO().merge(aPublication.getOwner());
574 * Propagate simulation contexts from the given config file to the publication's owner (study or step).
576 * @param aPublication
577 * the document publication
583 private void forwardProperties(final Publication aPublication,
584 final java.io.File from, final Step to) {
585 Reader tool = Toolbox.getReader(from);
586 if (tool != null) { // Properties extractor available for this type of document
587 SimulationContextType.Properties sprop = new SimulationContextType.Properties()
588 .setStep(to.getStep()).setProgressState(
589 ProgressState.APPROVED);
590 List<SimulationContextType> contype = getSimulationContextService()
591 .selectTypesWhere(sprop);
592 if (!contype.isEmpty()) { // There is an approved property type configured at this step
594 SimulationContext.Properties cprop = new SimulationContext.Properties();
595 List<SimulationContext> context = to.getAllSimulationContexts();
597 context = new ArrayList<SimulationContext>(context.size());
598 context.addAll(to.getAllSimulationContexts());
599 cprop.disableCheck();
600 for (Iterator<SimulationContextType> i = contype.iterator(); i
602 SimulationContextType property = i.next();
603 boolean isFound = false;
604 for (Iterator<SimulationContext> j = context.iterator(); j
606 SimulationContext existing = j.next();
607 isFound = existing.getType().equals(property);
609 // Forget this property as it is already set
615 String value = tool.extractProperty(property
618 continue; // Property not defined into the document
621 cprop.setType(property).setValue(value);
622 if (aPublication.getOwner() instanceof Study) {
623 getStudyService().addProjectContext(
624 (Study) aPublication.getOwner(), cprop); // Re-indexes knowledges and the study
627 .addSimulationContext(to, cprop); // Re-indexes knowledges only
629 } catch (Exception e) {
639 * Returns the study Step into which the document version referenced by this publication has been published.
641 * @param aPublication
642 * the document publication
643 * @return the study step where the document is published
645 public Step getInvolvedStep(final Publication aPublication) {
646 if (aPublication.getStep() == null) {
647 Step[] step = getProjectElementService().getSteps(
648 aPublication.getOwner());
649 for (int i = 0; i < step.length; i++) {
650 aPublication.setStep(step[i]); // The involved step necessarily exists
651 if (aPublication.value().isInto(aPublication.getStep())) {
656 return aPublication.getStep();
660 * Undo the out-date operation.
662 * @param aPublication
664 * @return true if the acceptance succeeds
666 * @see DocumentRights#canAccept()
669 public boolean actualize(final Publication aPublication) {
670 boolean res = aPublication.isOutdated();
672 aPublication.setIsnew('Y');
673 getPublicationDAO().update(aPublication);
679 * Out-dates this publication and recursively all publications using this one. Typically, a publication is out-dated when modifying a
680 * document to which it depends.
682 * @param aPublication
685 * @see #getProgressState()
688 public void outdate(final Publication aPublication) {
689 if (aPublication.isOutdated()) {
693 List<Publication> relist = aPublication
694 .getRelations(UsedByRelation.class);
695 for (Iterator<Publication> i = relist.iterator(); i.hasNext();) {
698 aPublication.setIsnew('O');
699 getPublicationDAO().update(aPublication);
703 * Create "Converts" relation for the given document publication and format.
705 * @param aPublication
706 * the document publication
709 * @return the created "Converts" relation
712 public ConvertsRelation attach(final Publication aPublication,
713 final String format) {
714 return getDocumentService().attach(aPublication.value(), format);
718 * Create "Converts" relation for the given document publication, format and description.
720 * @param aPublication
721 * the document publication
725 * the description of the relation
726 * @return the created "Converts" relation
729 public ConvertsRelation attach(final Publication aPublication,
730 final String format, final String description) {
731 return getDocumentService().attach(aPublication.value(), format,
736 * Rename the published document.
738 * @param aPublication
739 * the publication of the document
741 * the new document title
742 * @throws InvalidPropertyException
743 * if the new title is empty
745 public void rename(final Publication aPublication, final String title)
746 throws InvalidPropertyException {
747 getDocumentService().rename(aPublication.value(), title);
753 * @see org.splat.service.PublicationService#getDocToCompareDTO(long)
755 @Transactional(readOnly = true)
757 public DocToCompareDTO getDocToCompareDTO(final long publicationId)
758 throws InvalidParameterException {
759 DocToCompareDTO res = new DocToCompareDTO();
760 Publication pub = _publicationDAO.get(Long.valueOf(publicationId));
762 throw new InvalidParameterException("id", String
763 .valueOf(publicationId));
766 res.setDocumentTitle(pub.value().getTitle());
767 res.setPathToFile(pub.value().getFile().asFile().getAbsolutePath());
768 res.setScenarioTitle(pub.getOwner().getTitle());
769 res.setStudyTitle(pub.getOwnerStudy().getTitle());
774 * Get the projectElementService.
776 * @return the projectElementService
778 public ProjectElementService getProjectElementService() {
779 return _projectElementService;
783 * Set the projectElementService.
785 * @param projectElementService
786 * the projectElementService to set
788 public void setProjectElementService(
789 final ProjectElementService projectElementService) {
790 _projectElementService = projectElementService;
794 * Get the simulationContextService.
796 * @return the simulationContextService
798 public SimulationContextService getSimulationContextService() {
799 return _simulationContextService;
803 * Set the simulationContextService.
805 * @param simulationContextService
806 * the simulationContextService to set
808 public void setSimulationContextService(
809 final SimulationContextService simulationContextService) {
810 _simulationContextService = simulationContextService;
814 * Get the studyService.
816 * @return the studyService
818 public StudyService getStudyService() {
819 return _studyService;
823 * Set the studyService.
825 * @param studyService
826 * the studyService to set
828 public void setStudyService(final StudyService studyService) {
829 _studyService = studyService;
833 * Get the stepService.
835 * @return the stepService
837 public StepService getStepService() {
842 * Set the stepService.
845 * the stepService to set
847 public void setStepService(final StepService stepService) {
848 _stepService = stepService;
852 * Get the documentService.
854 * @return the documentService
856 public DocumentService getDocumentService() {
857 return _documentService;
861 * Set the documentService.
863 * @param documentService
864 * the documentService to set
866 public void setDocumentService(final DocumentService documentService) {
867 _documentService = documentService;
871 * Get the publicationDAO.
873 * @return the publicationDAO
875 public PublicationDAO getPublicationDAO() {
876 return _publicationDAO;
880 * Set the publicationDAO.
882 * @param publicationDAO
883 * the publicationDAO to set
885 public void setPublicationDAO(final PublicationDAO publicationDAO) {
886 _publicationDAO = publicationDAO;
890 * Get the projectElementDAO.
892 * @return the projectElementDAO
894 public ProjectElementDAO getProjectElementDAO() {
895 return _projectElementDAO;
899 * Set the projectElementDAO.
901 * @param projectElementDAO
902 * the projectElementDAO to set
904 public void setProjectElementDAO(final ProjectElementDAO projectElementDAO) {
905 _projectElementDAO = projectElementDAO;
909 * Get the repositoryService.
911 * @return the repositoryService
913 public RepositoryService getRepositoryService() {
914 return _repositoryService;
918 * Set the repositoryService.
920 * @param repositoryService
921 * the repositoryService to set
923 public void setRepositoryService(final RepositoryService repositoryService) {
924 _repositoryService = repositoryService;
928 * Get the timestampDAO.
930 * @return the timestampDAO
932 public TimestampDAO getTimestampDAO() {
933 return _timestampDAO;
937 * Set the timestampDAO.
939 * @param timestampDAO
940 * the timestampDAO to set
942 public void setTimestampDAO(final TimestampDAO timestampDAO) {
943 _timestampDAO = timestampDAO;
947 * Get the documentTypeService.
949 * @return the documentTypeService
951 public DocumentTypeService getDocumentTypeService() {
952 return _documentTypeService;
956 * Set the documentTypeService.
958 * @param documentTypeService
959 * the documentTypeService to set
961 public void setDocumentTypeService(
962 final DocumentTypeService documentTypeService) {
963 _documentTypeService = documentTypeService;
967 * Get the userService.
969 * @return the userService
971 public UserService getUserService() {
976 * Set the userService.
979 * the userService to set
981 public void setUserService(final UserService userService) {
982 _userService = userService;
988 * @see org.splat.service.PublicationService#replace(long, java.io.File)
992 public boolean replace(final Publication pub, final File newFile,
993 final Date modifTime) throws IncompatibleDataException {
994 if (!(ProgressState.EXTERN.equals(pub.getProgressState()) || ProgressState.inWORK
995 .equals(pub.getProgressState()))) {
996 throw new IncompatibleDataException(MessageKeyEnum.DCT_000004
999 Document doc = getDocumentService().selectDocument(
1000 pub.value().getIndex());
1001 if (LOG.isInfoEnabled()) {
1002 LOG.info("Moving \"" + newFile.getName() + "\" to \""
1003 + doc.getSourceFile().asFile().getAbsolutePath() + "\".");
1005 // Save a temporary copy of the original file as <old file name>.backup
1006 String oldFilePath = doc.getSourceFile().asFile().getAbsolutePath();
1007 File oldFile = new File(oldFilePath);
1008 File backupFile = new File(oldFilePath + ".backup");
1009 oldFile.renameTo(backupFile);
1010 boolean res = newFile.renameTo(oldFile);
1012 // Delete the temporary copy of the old file
1013 // if the new one is moved into the repository.
1014 backupFile.delete();
1015 // Update the document modification date.
1016 doc.setLastModificationDate(modifTime);
1017 // Update presentation data
1018 pub.value().setLastModificationDate(modifTime);
1020 // Restore the original file if replacing is failed
1021 backupFile.renameTo(oldFile);