if (LOG.isInfoEnabled()) {
LOG.info("Move " + upfile.getAbsolutePath() + TO + file.getPath());
}
+ file.delete(); // necessary on some platforms if the file exists.
return upfile.renameTo(file);
}
if (LOG.isDebugEnabled()) {
LOG.debug("Checkin the step:\n" + stepDTO);
}
+ if (stepDTO.getDocs().size() == 0) {
+ break;
+ }
// Find a result document type of the step
int i = 0;
resType = null;
// Find the appropriate scenario step
Step step = findStep(stepDTO, steps);
- // Process each document of the step
- for (DocumentDTO doc : stepDTO.getDocs()) {
- checkinDoc(step, doc, aUser, resType, aDate, newVersion,
+ List<FileDTO> filesToBeAttached = new ArrayList<FileDTO>();
+ FileDTO fileToAttachTo = null; //all the rest documents will be attached to it
+
+ //The id is the same for all DocumentDTOs of a step
+ Long currentDocId = stepDTO.getDocs().get(0).getId();
+ if(currentDocId != null && currentDocId > 0) { //there is a result document in the step
+ String currentFormat = step.getDocument(currentDocId).value().getFormat();
+
+ // Process each document of the step
+ for (DocumentDTO doc : stepDTO.getDocs()) {
+ for (FileDTO fileDTO : doc.getFiles()) {
+
+ String path = fileDTO.getPath();
+ String format = path.substring(path.lastIndexOf('.') + 1);
+
+ // If it has the same format as current doc (there can be just one such file)
+ // then this doc will be DocToAttachTo.
+ if (format != null && format.equals(currentFormat)) {
+ fileToAttachTo = fileDTO;
+ } else {
+ // Else, put it in the list of docs that will be attached to DocToAttachTo
+ filesToBeAttached.add(fileDTO);
+ }
+ }
+ }
+ } else {
+ // Process each document of the step
+ for (DocumentDTO doc : stepDTO.getDocs()) {
+ for (FileDTO fileDTO : doc.getFiles()) {
+ String path = doc.getFiles().get(0).getPath();
+ String format = path.substring(path.lastIndexOf('.') + 1);
+
+ ProjectSettingsService.Step stepType = step.getStep();
+ // If the default type for its format in the current step is the result type,
+ // then this doc will be DocToAttachTo.
+ DocumentType defaultDocType = _projectSettings.getDefaultDocumentType(stepType, format);
+ if (defaultDocType != null && defaultDocType.isResultOf(stepType)) {
+ //It is possible that there is not just one such doc
+ if(fileToAttachTo != null){
+ filesToBeAttached.add(fileToAttachTo);
+ }
+ fileToAttachTo = fileDTO;
+ } else {
+ // Else, put it in the list of docs that will be attached to DocToAttachTo
+ filesToBeAttached.add(fileDTO);
+ }
+ }
+ }
+
+ //could not find any file with appropriate format
+ if (fileToAttachTo == null && !filesToBeAttached.isEmpty()) {
+ //All the rest documents will be attached to the first in the list
+ fileToAttachTo = filesToBeAttached.get(0);
+ filesToBeAttached.remove(0);
+ }
+ }
+
+ if (fileToAttachTo != null) { // true if the DocumentDTO list is not empty
+
+ //Process docToAttachTo
+ Publication pub = checkinDoc(step, fileToAttachTo, currentDocId,
+ aUser, resType, aDate,
+ newVersion, newVers, newDocs);
+ currentDocId = pub.value().getIndex();
+ }
+
+ //Process rest docs
+ for (FileDTO file : filesToBeAttached) {
+ checkinDoc(step, file, currentDocId, aUser, resType, aDate, newVersion,
newVers, newDocs);
}
}
for (Publication pub : aScenario.getDocums()) {
if ((pub.getStep().getNumber() <= newPub.getStep().getNumber())
&& (!pub.isOutdated())
- && usedTypes.contains(pub.value().getType())) {
+ && usedTypes.contains(pub.value().getType())
+ && !newPub.equals(pub)) {
// Create uses relation from the new document
// to the found document in the previous step.
newPub.addDependency(pub);
*
* @param step
* the destination scenario step
- * @param doc
- * the DTO of the document to checkin
+ * @param file
+ * the FilDTO to checkin
+ * @param docId
+ * target document id
* @param aUser
* the user who performs checkin
* @param resType
* the list of publications of new created versions of existing documents
* @param newDocs
* the list of publications of new created documents not existing before the checkin
+ * @return
+ * the newly created publication, if exists (the document has been created or versioned),
+ * null otherwise (the doc has been attached to the old one)
* @throws InvalidPropertyException
* if the scenario hasn't some of given steps or documents
* @throws IOException
* @throws NotApplicableException
* if failed saving of a new publication with a given state
*/
- private void checkinDoc(final Step step, final DocumentDTO doc,
- final User aUser, final DocumentType resType, final Date aDate,
- final Map<Document, Document> newVersion,
+ private Publication checkinDoc(final Step step, final FileDTO file,
+ final long docId, final User aUser, final DocumentType resType,
+ final Date aDate, final Map<Document, Document> newVersion,
final List<Publication> newVers, final List<Publication> newDocs)
throws InvalidPropertyException, MismatchException,
MissedPropertyException, MultiplyDefinedException, IOException,
NotApplicableException {
- if (doc.getFiles().size() > 0) {
+ Publication newPub = null;
+ if (file != null) {
Document.Properties dprop = new Document.Properties();
// NOTE: Process only the first attached file for each document
- FileDTO file = doc.getFiles().get(0);
dprop.setLocalPath(file.getPath());
// Get document title as the file name
MessageKeyEnum.DCT_000005.toString(), authorName);
dprop.setDescription(summary);
- if (doc.getId() > 0) {
- checkinExistingDoc(step, doc, dprop, fileFormat, upfile,
+ if (docId > 0) {
+ newPub = checkinExistingDoc(step, docId, dprop, fileFormat, upfile,
newVersion, newVers);
} else {
docname += "_" + i; // The generated new document title
dprop.setName(docname);
- Publication newPub = getStepService().createDocument(step,
+ newPub = getStepService().createDocument(step,
dprop);
// Remember the new document
saveFile(newPub, step, upfile);
}
- }
+ }
+ return newPub;
}
/**
*
* @param step
* study step to check in
- * @param doc
- * document DTO to check in
+ * @param docId
+ * target document id
* @param dprop
* document properties
* @param fileFormat
* the map of created versions during this check in
* @param newVers
* the list of new versions created during this check in
+ * @return
+ * the newly created publication, if exists (the document has been versioned),
+ * null otherwise (the doc has been attached to the old one)
* @throws InvalidPropertyException
* if publication of the document is not found in the step
* @throws MismatchException
* @throws NotApplicableException
* if failed saving of a new publication with a given state
*/
- private void checkinExistingDoc(final Step step, final DocumentDTO doc,
+ private Publication checkinExistingDoc(final Step step, final long docId,
final Properties dprop, final String fileFormat,
final java.io.File upfile,
final Map<Document, Document> newVersion,
// 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());
+ //Publication pub = step.getDocument(doc.getId());
+ Publication pub = step.getDocument(docId);
+ Publication newPub = null;
if (pub == null) {
throw new InvalidPropertyException(MessageKeyEnum.SCN_000002
- .toString(), doc.getId());
+ .toString(), docId);
}
if (pub.value() == null) {
throw new MismatchException(MessageKeyEnum.SCN_000002.toString(),
- doc.getId());
+ docId);
}
if (LOG.isDebugEnabled()) {
LOG.debug("Old format: " + pub.value().getFormat()
// 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,
+ newPub = getStepService().versionDocument(step, pub,
dprop);
if (LOG.isDebugEnabled()) {
LOG.debug("Created document type: "
attach.setDate(new Date());
}
}
+ return newPub;
}
/**