Merge branch 'feature/RM-5147_Autorename_on_upload' into 'master'

RM-5148 - added support for autoRename for uploadRecord too

See merge request !298
This commit is contained in:
Ramona Popa
2017-05-23 07:24:27 +01:00
4 changed files with 35 additions and 17 deletions

View File

@@ -79,6 +79,7 @@ import org.alfresco.rm.rest.api.RMSites;
import org.alfresco.rm.rest.api.model.RMNode; import org.alfresco.rm.rest.api.model.RMNode;
import org.alfresco.rm.rest.api.model.RMSite; import org.alfresco.rm.rest.api.model.RMSite;
import org.alfresco.rm.rest.api.model.TransferContainer; import org.alfresco.rm.rest.api.model.TransferContainer;
import org.alfresco.rm.rest.api.model.UploadInfo;
import org.alfresco.service.cmr.activities.ActivityInfo; import org.alfresco.service.cmr.activities.ActivityInfo;
import org.alfresco.service.cmr.activities.ActivityPoster; import org.alfresco.service.cmr.activities.ActivityPoster;
import org.alfresco.service.cmr.attributes.DuplicateAttributeException; import org.alfresco.service.cmr.attributes.DuplicateAttributeException;
@@ -672,32 +673,50 @@ public class FilePlanComponentsApiUtils
/** /**
* Upload a record * Upload a record
* *
* @param parentNodeRef the parent of the record * @param parentNodeRef the parent of the record
* @param name the name of the record * @param uploadInfo the infos of the uploaded record
* @param type the type of the record (if null the record's type will be cm:content) * @param parameters the object to get the parameters passed into the request
* @param properties properties to set (can be null)
* @param stream the stream to write
* @return the new record * @return the new record
*/ */
public NodeRef uploadRecord(NodeRef parentNodeRef, String name, String type, Map<String, Object> properties, InputStream stream) public NodeRef uploadRecord(NodeRef parentNodeRef, UploadInfo uploadInfo, Parameters parameters)
{ {
checkNotBlank(RMNode.PARAM_NAME, name); mandatory("parentNodeRef", parentNodeRef);
mandatory("uploadInfo", uploadInfo);
mandatory("parameters", parameters);
String nodeName = uploadInfo.getFileName();
String nodeType = uploadInfo.getNodeType();
InputStream stream = uploadInfo.getContent().getInputStream();
mandatory("stream", stream); mandatory("stream", stream);
checkNotBlank(RMNode.PARAM_NAME, nodeName);
// Create the node // Create the node
QName typeQName = StringUtils.isBlank(type) ? ContentModel.TYPE_CONTENT : nodes.createQName(type); QName typeQName = StringUtils.isBlank(nodeType) ? ContentModel.TYPE_CONTENT : nodes.createQName(nodeType);
if(!dictionaryService.isSubClass(typeQName, ContentModel.TYPE_CONTENT)) if (!dictionaryService.isSubClass(typeQName, ContentModel.TYPE_CONTENT))
{ {
throw new InvalidArgumentException("Can only upload type of cm:content: " + typeQName); throw new InvalidArgumentException("Can only upload type of cm:content: " + typeQName);
} }
NodeRef newNodeRef = fileFolderService.create(parentNodeRef, name, typeQName).getNodeRef();
// Existing file/folder name handling
boolean autoRename = Boolean.valueOf(parameters.getParameter(RMNode.PARAM_AUTO_RENAME));
if (autoRename)
{
NodeRef existingNode = nodeService.getChildByName(parentNodeRef, ContentModel.ASSOC_CONTAINS, nodeName);
if (existingNode != null)
{
// File already exists, find a unique name
nodeName = findUniqueName(parentNodeRef, nodeName);
}
}
NodeRef newNodeRef = fileFolderService.create(parentNodeRef, nodeName, typeQName).getNodeRef();
// Write content // Write content
writeContent(newNodeRef, name, stream, true); writeContent(newNodeRef, nodeName, stream, true);
// Set the provided properties if any // Set the provided properties if any
Map<QName, Serializable> qnameProperties = mapToNodeProperties(properties); Map<QName, Serializable> qnameProperties = mapToNodeProperties(uploadInfo.getProperties());
if(qnameProperties != null) if (qnameProperties != null)
{ {
nodeService.addProperties(newNodeRef, qnameProperties); nodeService.addProperties(newNodeRef, qnameProperties);
} }

View File

@@ -207,8 +207,7 @@ public class RecordFolderChildrenRelation implements RelationshipResourceAction.
{ {
public NodeRef execute() public NodeRef execute()
{ {
return apiUtils.uploadRecord(parentNodeRef, uploadInfo.getFileName(), uploadInfo.getNodeType(), uploadInfo.getProperties(), return apiUtils.uploadRecord(parentNodeRef, uploadInfo, parameters);
uploadInfo.getContent().getInputStream());
} }
}; };
NodeRef newNode = transactionService.getRetryingTransactionHelper().doInTransaction(callback, false, true); NodeRef newNode = transactionService.getRetryingTransactionHelper().doInTransaction(callback, false, true);

View File

@@ -223,7 +223,7 @@ public class UnfiledContainerChildrenRelation implements RelationshipResourceAct
{ {
public NodeRef execute() public NodeRef execute()
{ {
return apiUtils.uploadRecord(parentNodeRef, uploadInfo.getFileName(), uploadInfo.getNodeType(), uploadInfo.getProperties(), uploadInfo.getContent().getInputStream()); return apiUtils.uploadRecord(parentNodeRef, uploadInfo, parameters);
} }
}; };
NodeRef newNode = transactionService.getRetryingTransactionHelper().doInTransaction(callback, false, true); NodeRef newNode = transactionService.getRetryingTransactionHelper().doInTransaction(callback, false, true);

View File

@@ -232,7 +232,7 @@ public class UnfiledRecordFolderChildrenRelation implements RelationshipResource
public Pair<NodeRef,NodeRef> execute() public Pair<NodeRef,NodeRef> execute()
{ {
final NodeRef parentNodeRef = apiUtils.lookupAndValidateNodeType(unfiledRecordFolderId, RecordsManagementModel.TYPE_UNFILED_RECORD_FOLDER, uploadInfo.getRelativePath()); final NodeRef parentNodeRef = apiUtils.lookupAndValidateNodeType(unfiledRecordFolderId, RecordsManagementModel.TYPE_UNFILED_RECORD_FOLDER, uploadInfo.getRelativePath());
NodeRef newNode = apiUtils.uploadRecord(parentNodeRef, uploadInfo.getFileName(), uploadInfo.getNodeType(), uploadInfo.getProperties(), uploadInfo.getContent().getInputStream()); NodeRef newNode = apiUtils.uploadRecord(parentNodeRef, uploadInfo, parameters);
return new Pair<NodeRef, NodeRef>(newNode, parentNodeRef); return new Pair<NodeRef, NodeRef>(newNode, parentNodeRef);
} }
}; };