import org.splat.dal.dao.som.ProjectElementDAO;
import org.splat.dal.dao.som.PublicationDAO;
import org.splat.dal.dao.som.TimestampDAO;
+import org.splat.exception.IncompatibleDataException;
import org.splat.exception.InvalidParameterException;
import org.splat.kernel.InvalidPropertyException;
import org.splat.kernel.MismatchException;
step,
dprop.setName(doctitle).setType(type).setFormat(
table[table.length - 1]).setAuthor(user));
- updir = addoc.getSourceFile().asFile();
- if (LOG.isInfoEnabled()) {
- LOG.info("Moving \"" + upfile.getAbsolutePath() + "\" to \""
- + updir.getPath() + "\".");
- }
- upfile.renameTo(updir);
+ moveFile(upfile, addoc);
try {
saveAs(addoc, docstate); // May throw FileNotFound if rename was not done
} catch (FileNotFoundException saverror) {
}
addoc = getStepService().assignDocument(step,
dprop.setReference(reference).setName(doctitle));
- updir = addoc.getSourceFile().asFile();
- if (LOG.isInfoEnabled()) {
- LOG.info("Moving \"" + upfile.getName() + "\" to \""
- + updir.getPath() + "\".");
- }
- upfile.renameTo(updir);
+ moveFile(upfile, addoc);
try {
if (version.length() > 0) {
saveAs(addoc, new Revision(version));
return addoc;
}
+ /**
+ * Move a file into the repository as a source file of the document.
+ *
+ * @param upfile
+ * the uploaded file to move
+ * @param addoc
+ * the document
+ */
+ private void moveFile(final File upfile, final Publication addoc) {
+ File updir = addoc.getSourceFile().asFile();
+ if (LOG.isInfoEnabled()) {
+ LOG.info("Moving \"" + upfile.getAbsolutePath() + "\" to \""
+ + updir.getPath() + "\".");
+ }
+ upfile.renameTo(updir);
+ }
+
/**
* {@inheritDoc}
*
}
next = getStepService().versionDocument(step, current,
dprop.setAuthor(user));
- updir = next.getSourceFile().asFile();
- if (LOG.isInfoEnabled()) {
- LOG.info("Moving \"" + upfile.getName() + "\" to \""
- + updir.getPath() + "\".");
- }
- upfile.renameTo(updir);
+ moveFile(upfile, next);
try {
if (docver.length() == 0) { // Importation of a foreign document
getDocumentService().rename(aPublication.value(), title);
}
- /**
+ /**
* {@inheritDoc}
+ *
* @see org.splat.service.PublicationService#getDocToCompareDTO(long)
*/
@Transactional(readOnly = true)
@Override
- public DocToCompareDTO getDocToCompareDTO(final long publicationId) throws InvalidParameterException {
+ public DocToCompareDTO getDocToCompareDTO(final long publicationId)
+ throws InvalidParameterException {
DocToCompareDTO res = new DocToCompareDTO();
Publication pub = _publicationDAO.get(Long.valueOf(publicationId));
- if(pub == null) {
- throw new InvalidParameterException("id", String.valueOf(publicationId));
+ if (pub == null) {
+ throw new InvalidParameterException("id", String
+ .valueOf(publicationId));
}
-
+
res.setDocumentTitle(pub.value().getTitle());
res.setPathToFile(pub.value().getFile().asFile().getAbsolutePath());
res.setScenarioTitle(pub.getOwner().getTitle());
_userService = userService;
}
- /**
+ /**
* {@inheritDoc}
+ *
* @see org.splat.service.PublicationService#replace(long, java.io.File)
*/
@Override
@Transactional
public boolean replace(final Publication pub, final File newFile,
- final Date modifTime) {
- Document doc = getDocumentService().selectDocument(pub.value().getIndex());
+ final Date modifTime) throws IncompatibleDataException {
+ if (!(ProgressState.EXTERN.equals(pub.getProgressState()) || ProgressState.inWORK
+ .equals(pub.getProgressState()))) {
+ throw new IncompatibleDataException(MessageKeyEnum.DCT_000004
+ .toString());
+ }
+ Document doc = getDocumentService().selectDocument(
+ pub.value().getIndex());
if (LOG.isInfoEnabled()) {
LOG.info("Moving \"" + newFile.getName() + "\" to \""
- + doc.getSourceFile().asFile().getAbsolutePath()
- + "\".");
+ + doc.getSourceFile().asFile().getAbsolutePath() + "\".");
}
// Save a temporary copy of the original file as <old file name>.backup
String oldFilePath = doc.getSourceFile().asFile().getAbsolutePath();
oldFile.renameTo(backupFile);
boolean res = newFile.renameTo(oldFile);
if (res) {
- // Delete the temporary copy of the old file
+ // Delete the temporary copy of the old file
// if the new one is moved into the repository.
backupFile.delete();
// Update the document modification date.
import org.splat.dal.dao.som.ScenarioDAO;
import org.splat.dal.dao.som.StudyDAO;
import org.splat.exception.BusinessException;
+import org.splat.exception.IncompatibleDataException;
import org.splat.kernel.InvalidPropertyException;
import org.splat.kernel.MissedPropertyException;
import org.splat.kernel.MultiplyDefinedException;
* <i>Create a study and a scenario with documents and try to replace source files.<BR>
* </i><BR>
* <B>Action : </B><BR>
- * <i>1. call the method for a document published in the study step.</i><BR>
- * <i>2. call the method for a document published in the scenario step.</i><BR>
+ * <i>1. call the method for a document in the status inDRAFT.</i><BR>
+ * <i>2. call the method for a document in the status inCHECK.</i><BR>
+ * <i>3. call the method for a document in the status APPROVED.</i><BR>
+ * <i>4. call the method for an Extern document published in the study step.</i><BR>
+ * <i>5. call the method for an inWork document published in the scenario step.</i><BR>
* <B>Test data : </B><BR>
* <i>no input parameters</i><BR>
- * <i>no input parameters</i><BR>
*
* <B>Outcome results:</B><BR>
* <i>
* <ul>
- * <li>The document source file content in the study must be replaced.<BR>
+ * <li>1-3: Exception must be thrown. The document source file content must not be replaced.<BR>
+ * </li>
+ * <li>4: The document source file content in the study must be replaced.<BR>
* </li>
- * <li>The document source file content in the scenario must be replaced.<BR>
+ * <li>5: The document source file content in the scenario must be replaced.<BR>
* </li>
* </ul>
* </i>
Map<Integer, org.splat.som.Step> stSteps = _projectElementService
.getStepsMap(aStudy);
org.splat.som.Step aStep = stSteps.get(1);
+ Publication pub0inDraft = addDoc(aStudy, aStep, "document0indraft",
+ dtype);
+ pub0inDraft.value().setProgressState(ProgressState.inDRAFT);
+ Publication pub0inCheck = addDoc(aStudy, aStep, "document0incheck",
+ dtype);
+ pub0inCheck.value().setProgressState(ProgressState.inCHECK);
+ Publication pub0approved = addDoc(aStudy, aStep, "document0approved",
+ dtype);
+ pub0approved.value().setProgressState(ProgressState.APPROVED);
Publication pub1 = addDoc(aStudy, aStep, "document1", dtype);
String format1 = pub1.value().getFormat();
ht.flush();
String format2 = spub1.value().getFormat();
ht.flush();
- Long id1 = pub1.value().getIndex(), id2 = spub1.value().getIndex();
+ // Remember documents ids
+ Long id0inDraft = pub0inDraft.value().getIndex(), id0inCheck = pub0inCheck
+ .value().getIndex(), id0approved = pub0approved.value()
+ .getIndex(), id1 = pub1.value().getIndex(), id2 = spub1.value()
+ .getIndex();
ht.evict(scen);
ht.evict(scen.getOwnerStudy());
ht.clear();
- Assert.assertTrue(ht.find("from File").size() >= 2,
+ Assert.assertTrue(ht.find("from File").size() >= 3,
"Files were not created in the database.");
Date modifTime = Calendar.getInstance().getTime();
- // TEST CALL: Replace a file in the study step
aStudy = _studyService.selectStudy(studyId);
- Publication pub = aStudy.getDocums().toArray(new Publication[] {})[0];
- File newFile = new File(getDownloadPath(goodUser) + "replaced1.ddd");
+ File newFile = new File(getDownloadPath(goodUser) + "replaced0.ddd");
+ createFile(newFile.getAbsolutePath());
+
+ // TEST CALL: Try to replace a file for inDraft document
+ Publication pub = getPublicationByDocId(aStudy, id0inDraft);
+ try {
+ _publicationService.replace(pub, newFile, modifTime);
+ Assert
+ .fail("IncompatibleDataException is expected for a doucment in the status "
+ + pub.getProgressState().name());
+ } catch (IncompatibleDataException e) {
+ LOG.debug("Expected exception: ", e);
+ }
+ ht.flush();
+ // TEST CALL: Try to replace a file for inCheck document
+ pub = getPublicationByDocId(aStudy, id0inCheck);
+ try {
+ _publicationService.replace(pub, newFile, modifTime);
+ Assert
+ .fail("IncompatibleDataException is expected for a doucment in the status "
+ + pub.getProgressState().name());
+ } catch (IncompatibleDataException e) {
+ LOG.debug("Expected exception: ", e);
+ }
+ ht.flush();
+ // TEST CALL: Try to replace a file for Approved document
+ pub = getPublicationByDocId(aStudy, id0approved);
+ try {
+ _publicationService.replace(pub, newFile, modifTime);
+ Assert
+ .fail("IncompatibleDataException is expected for a doucment in the status "
+ + pub.getProgressState().name());
+ } catch (IncompatibleDataException e) {
+ LOG.debug("Expected exception: ", e);
+ }
+ ht.flush();
+ newFile.delete();
+
+ // TEST CALL: Replace a file in the study step
+ pub = getPublicationByDocId(aStudy, id1);
+ newFile = new File(getDownloadPath(goodUser) + "replaced1.ddd");
createFile(newFile.getAbsolutePath());
Assert.assertTrue(_publicationService.replace(pub, newFile, modifTime));
ht.flush();
// TEST CALL: Replace a file in the scenario step
scen = aStudy.getScenariiList().get(0);
- pub = scen.getDocums().toArray(new Publication[] {})[0];
+ pub = getPublicationByDocId(scen, id2);
newFile = new File(getDownloadPath(goodUser) + "replaced2.ddd");
createFile(newFile.getAbsolutePath());
Assert.assertTrue(_publicationService.replace(pub, newFile, modifTime));
Assert.assertEquals(doc.getFile().getFormat(), format2);
Assert.assertTrue(txt.contains("replaced2.ddd"));
Assert.assertEquals(doc.getLastModificationDate(), modifTime);
-
+
rollbackNestedTransaction();
LOG.debug(">>>>> END testReplace()");
}
+ /**
+ * Find a document publication in the project element.
+ *
+ * @param projElem
+ * the project element to scan
+ * @param docId
+ * the document id
+ * @return the document publication if found, otherwise return null
+ */
+ private Publication getPublicationByDocId(final ProjectElement projElem,
+ final Long docId) {
+ Publication res = null;
+ for (Publication pub : projElem.getDocums()) {
+ if (docId.equals(pub.value().getIndex())) {
+ res = pub;
+ break;
+ }
+ }
+ return res;
+ }
+
/**
* Get file content as a string.
*