import org.splat.dal.bo.som.Study;
import org.splat.dal.bo.som.UsedByRelation;
import org.splat.dal.bo.som.UsesRelation;
+import org.splat.dal.bo.som.Document.Properties;
import org.splat.dal.dao.kernel.RoleDAO;
import org.splat.dal.dao.kernel.UserDAO;
import org.splat.dal.dao.som.KnowledgeElementDAO;
// Attach the file via ConvertsRelation, create a new document or
// create a new version of the document
dprop.setAuthor(aUser).setDate(aDate).setFormat(fileFormat);
- Publication pub, newPub;
if (doc.getId() > 0) {
- // If the document already exists then
- // Attach the file via ConvertsRelation if the extension of the
- // new file differs from the old one.
- // If file format (i.e. extension) is the same then create a new
- // version of the document.
- // Find the document publication
- pub = step.getDocument(doc.getId());
- if (pub == null) {
- throw new InvalidPropertyException(
- MessageKeyEnum.SCN_000002.toString(), doc.getId());
- }
- if (pub.value() == null) {
- throw new MismatchException(MessageKeyEnum.SCN_000002
- .toString(), doc.getId());
- }
- if (LOG.isDebugEnabled()) {
- LOG.debug("Old format: " + pub.value().getFormat()
- + " => New format: " + fileFormat);
- }
- // If formats are same then create a new document version
- if (pub.value().getFormat() != null
- && pub.value().getFormat().equals(fileFormat)) {
- newPub = getStepService().versionDocument(step, pub, dprop);
- if (LOG.isDebugEnabled()) {
- LOG.debug("Created document type: "
- + newPub.value().getType().getName()
- + ", format: " + newPub.value().getFormat());
- }
- // Remeber the link from the old document to the new document version
- newVersion.put(pub.value(), newPub.value());
- // Remember the new version publication
- newVers.add(newPub);
-
- saveFile(newPub, step, file);
-
- } else { // If formats are different then attach the new file via ConvertsRelation
- ConvertsRelation export = getPublicationService().attach(
- pub, fileFormat);
- if (LOG.isDebugEnabled()) {
- LOG.debug("Moving " + upfile.getName() + " to "
- + export.getTo().asFile().getPath());
- }
- upfile.renameTo(export.getTo().asFile());
- }
+ checkinExistingDoc(step, doc, dprop, fileFormat, upfile,
+ newVersion, newVers);
} else {
// Otherwise create a new document of the result type
docname += "_" + i; // The generated new document title
dprop.setDescription("Checked in").setName(docname);
- newPub = getStepService().createDocument(step, dprop);
+ Publication newPub = getStepService().createDocument(step,
+ dprop);
// Remeber the new document
newDocs.add(newPub);
- saveFile(newPub, step, file);
+ saveFile(newPub, step, upfile);
+ }
+ }
+ }
+
+ /**
+ * Check in existing document.
+ *
+ * @param step
+ * study step to check in
+ * @param doc
+ * document DTO to check in
+ * @param dprop
+ * document properties
+ * @param fileFormat
+ * checked in file format
+ * @param upfile
+ * the file to check in
+ * @param newVersion
+ * the map of created versions during this check in
+ * @param newVers
+ * the list of new versions created during this check in
+ * @throws InvalidPropertyException
+ * if publication of the document is not found in the step
+ * @throws MismatchException
+ * if the found publication does not point to a document
+ * @throws IOException
+ * if can not move the file into the vault
+ * @throws MultiplyDefinedException
+ * thrown by versionDocument
+ * @throws MissedPropertyException
+ * thrown by versionDocument
+ * @throws NotApplicableException
+ * if failed saving of a new publication with a given state
+ */
+ private void checkinExistingDoc(final Step step, final DocumentDTO doc,
+ final Properties dprop, final String fileFormat,
+ final java.io.File upfile,
+ final Map<Document, Document> newVersion,
+ final List<Publication> newVers) throws InvalidPropertyException,
+ MismatchException, MissedPropertyException,
+ MultiplyDefinedException, IOException, NotApplicableException {
+ // If the document already exists then
+ // Attach the file via ConvertsRelation if the extension of the
+ // new file differs from the old one.
+ // If file format (i.e. extension) is the same then create a new
+ // version of the document.
+ // Find the document publication
+ Publication pub = step.getDocument(doc.getId());
+ if (pub == null) {
+ throw new InvalidPropertyException(MessageKeyEnum.SCN_000002
+ .toString(), doc.getId());
+ }
+ if (pub.value() == null) {
+ throw new MismatchException(MessageKeyEnum.SCN_000002.toString(),
+ doc.getId());
+ }
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Old format: " + pub.value().getFormat()
+ + " => New format: " + fileFormat);
+ }
+ // If formats are same then create a new document version
+ if (pub.value().getFormat() != null
+ && pub.value().getFormat().equals(fileFormat)) {
+ Publication newPub = getStepService().versionDocument(step, pub,
+ dprop);
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Created document type: "
+ + newPub.value().getType().getName() + ", format: "
+ + newPub.value().getFormat());
+ }
+ // Remeber the link from the old document to the new document version
+ newVersion.put(pub.value(), newPub.value());
+ // Remember the new version publication
+ newVers.add(newPub);
+
+ saveFile(newPub, step, upfile);
+
+ } else { // If formats are different then attach the new file via ConvertsRelation
+ File attach = pub.value().getAttachedFile(fileFormat);
+ if (attach == null) {
+ // If there is no attachment with this extension then attach the new one
+ ConvertsRelation export = getPublicationService().attach(pub,
+ fileFormat);
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Moving " + upfile.getName() + " to "
+ + export.getTo().asFile().getPath());
+ }
+ upfile.renameTo(export.getTo().asFile());
+ } else {
+ // If an attachment with this extension already exists then
+ // replace it by the new one
+ upfile.renameTo(attach.asFile());
+ // Update attached file modification date
+ attach.setDate(new Date());
}
}
}
* the new publication to save
* @param step
* the study step to publish the document
- * @param file
- * the downloaded file DTO
+ * @param upfile
+ * the downloaded file
* @throws IOException
* if a file can't be moved into the vault
* @throws NotApplicableException
* if failed saving of a new publication with a given state
*/
private void saveFile(final Publication newPub, final Step step,
- final FileDTO file) throws IOException, NotApplicableException {
+ final java.io.File upfile) throws IOException,
+ NotApplicableException {
// Attach the file to the created document
java.io.File updir = newPub.getSourceFile().asFile();
- java.io.File upfile = new java.io.File(file.getPath());
if (LOG.isDebugEnabled()) {
LOG.debug("Moving \"" + upfile.getName() + "\" to \""
+ updir.getPath() + "\".");
} else {
throw new IOException(
"Can't delete the existing destination file to move file from "
- + file.getPath() + " to "
+ + upfile.getAbsolutePath() + " to "
+ updir.getAbsolutePath());
}
}
// The old publication is removed from the scenario here.
getPublicationService().saveAs(newPub, ProgressState.inWORK); // May throw FileNotFound if rename was not done
} else {
- throw new IOException("Can't move file from " + file.getPath()
- + " to " + updir.getAbsolutePath());
+ throw new IOException("Can't move file from "
+ + upfile.getAbsolutePath() + " to "
+ + updir.getAbsolutePath());
}
}
}
return isOk;
}
-
+
/**
*
* {@inheritDoc}
+ *
* @see org.splat.service.ScenarioService#renameScenario(java.lang.String)
*/
@Transactional
import org.splat.dal.bo.kernel.Relation;
import org.splat.dal.bo.kernel.User;
+import org.splat.dal.bo.som.ConvertsRelation;
import org.splat.dal.bo.som.Document;
import org.splat.dal.bo.som.DocumentType;
import org.splat.dal.bo.som.KnowledgeElementType;
User user = aScen.getAuthor();
long userId = user.getIndex();
-
// ////////////////////////////////////////////////////////
// Call checkin method for empty list of modules.
aScen = _scenarioDAO.get(scenarioId);
Assert.assertFalse(aScen.isCheckedout(),
"Scenario is still marked as checked out after checkin.");
-
-
-
+
// ////////////////////////////////////////////////////////
// Call checkin method for good prepared transient data.
steps = _scenarioService.getScenarioInfo(scenarioId);
_scenarioService.checkout(aScen, user);
+ // Remember modification dates of all attached files
+ Map<Long, Date> dates = new HashMap<Long, Date>();
+ for (Publication p : aScen.getDocums()) {
+ for (Relation r : p.value().getRelations(ConvertsRelation.class)) {
+ org.splat.dal.bo.som.File attach = ((ConvertsRelation) r)
+ .getTo();
+ dates.put(attach.getIndex(), attach.getDate());
+ }
+ }
+
// Prepare test data for checkin
// Checkin only two first steps (geom and mesh)
for (StepDTO step : steps) {
aScen = _scenarioDAO.get(scenarioId);
Assert.assertFalse(aScen.isCheckedout(),
"Scenario is still marked as checked out after checkin.");
+ boolean modifDatesChecked = false;
// Check that new document versions are created for checked in documents
for (StepDTO step : stepsToCheckin) {
for (DocumentDTO docDTO : step.getDocs()) {
Assert.assertFalse(aScen.publishes(prevDoc));
// Check that presentation of the previous version is removed
checkFiles(docDTO, newPub);
-
+
// Formats of files are new if they are according to the document's type on the study step
if ("py".equals(prevDoc.getFormat())
- && "geometry".equals(prevDoc.getType().getName())) {
- Assert.assertEquals(newPub.value().getFormat(), "brep");
- Assert.assertEquals(newPub.getSourceFile().getFormat(),
+ && "geometry".equals(prevDoc.getType()
+ .getName())) {
+ Assert.assertEquals(newPub.value().getFormat(),
"brep");
+ Assert.assertEquals(newPub.getSourceFile()
+ .getFormat(), "brep");
Assert.assertEquals(newPub.getSourceFile()
.getRelativePath().substring(
newPub.getSourceFile()
- .getRelativePath().lastIndexOf(
- '.') + 1), "brep");
+ .getRelativePath()
+ .lastIndexOf('.') + 1),
+ "brep");
}
// Check that uses relations are copied correctly
if ((lastPub.value().getPreviousVersion() != null)
&& (lastPub.value()
.getPreviousVersion()
- .getIndex() == used.getIndex())) {
+ .getIndex() == used
+ .getIndex())) {
toBeUsed = lastPub;
break;
}
Assert.assertTrue(attFile.asFile().exists(), "File "
+ docDTO.getFiles().get(0).getPath()
+ " attached to the document "
- + docDTO.getTitle() + "#" + docDTO.getId() + " doesn't exist");
- LOG.debug("Source format: " + curDoc.getFormat() + ", new format: " + newFormat);
+ + docDTO.getTitle() + "#" + docDTO.getId()
+ + " doesn't exist");
+ LOG.debug("Source format: " + curDoc.getFormat()
+ + ", new format: " + newFormat);
+ // Check that attachment with the same format is not duplicated.
+ int attachNb = 0;
+ for (Relation conv : curDoc
+ .getRelations(ConvertsRelation.class)) {
+ if (newFormat.equals(((ConvertsRelation) conv)
+ .getTo().getFormat())) {
+ attachNb++;
+ }
+ }
+ Assert
+ .assertEquals(attachNb, 1,
+ "Attachment with the same format must be only one.");
+
+ // Check that the attached file date is updated
+ if (dates.containsKey(attFile.getIndex())) {
+ Assert
+ .assertTrue(attFile.getDate().compareTo(
+ dates.get(attFile.getIndex())) > 0,
+ "Attachment modification date is not updated.");
+ modifDatesChecked = true;
+ }
}
-
} else {
// Check that new documents are created for new data
boolean found = false;
}
}
+ Assert
+ .assertTrue(
+ modifDatesChecked,
+ "No modification date is checked because no files were attached when attachment with same extension already exists.");
+
// ///////////////////////////////////////////////////////////
// Call checkin method for a not existing id.
try {
for (FileDTO file : doc.getFiles()) {
if (file.getPath().endsWith(format)
|| (file.getPath().endsWith("py") && (format
- .equals("brep") || format
- .equals("med")))) {
+ .equals("brep") || format.equals("med")))) {
// Create a file in the download directory
docToCheckin.addFile(createDownloadedFile(userId,
doc.getTitle() + "_result", format));
// <source-file>.brep
// <attached-file>.med
dprop.setName("document" + i++).setType(dtype);
-/* if (step.getNumber() > 3) {
- dprop.setFormat("med");
- } else {
-*/ dprop.setFormat("py");
-// }
+ /*
+ * if (step.getNumber() > 3) { dprop.setFormat("med"); } else {
+ */dprop.setFormat("py");
+ // }
dprop.setLocalPath(dprop.getName() + "." + dprop.getFormat());
Publication pub = createDoc(aScenario, aScStep, dprop, "med",
false);