Merged 5.2.N (5.2.1) to HEAD (5.2)

129134 mmuller: Merged RETURN-OF-THE-API (5.2.0) to 5.2.N (5.2.1)
      128207 jvonka: V1 REST API: Node Version History - add "revert version" operation
      - ie. promote a previous version to become a new (most recent) version
      - POST /nodes/nodeId/versions/versionId/revert (with request body, optionally majorVersion &/or comment)
      REPO-236


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@129328 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Alexandru Epure
2016-08-09 14:10:35 +00:00
parent 99ae11c173
commit 8fa8efbee6
6 changed files with 350 additions and 64 deletions

View File

@@ -2269,11 +2269,16 @@ public class NodesImpl implements Nodes
if ((isVersioned) || (versionMajor != null) || (versionComment != null) )
{
VersionType versionType = VersionType.MINOR;
if ((versionMajor != null) && (versionMajor == true))
VersionType versionType = null;
if (versionMajor != null)
{
versionType = VersionType.MAJOR;
versionType = (versionMajor ? VersionType.MAJOR : VersionType.MINOR);
}
else
{
versionType = (isVersioned ? versionType = VersionType.MINOR : VersionType.MAJOR);
}
createVersion(nodeRef, isVersioned, versionType, versionComment);
}
@@ -2368,20 +2373,22 @@ public class NodesImpl implements Nodes
{
if (! isVersioned)
{
// Ensure the file is versionable (autoVersion = true, autoVersionProps = false)
ensureVersioningEnabled(nodeRef, true, false);
}
else
{
Map<String, Serializable> versionProperties = new HashMap<>(2);
versionProperties.put(VersionModel.PROP_VERSION_TYPE, versionType);
if (reason != null)
{
versionProperties.put(VersionModel.PROP_DESCRIPTION, reason);
}
// Ensure versioning is enabled for the file (autoVersion = true, autoVersionProps = false)
Map<QName, Serializable> props = new HashMap<>(2);
props.put(ContentModel.PROP_AUTO_VERSION, true);
props.put(ContentModel.PROP_AUTO_VERSION_PROPS, false);
versionService.createVersion(nodeRef, versionProperties);
nodeService.addAspect(nodeRef, ContentModel.ASPECT_VERSIONABLE, props);
}
Map<String, Serializable> versionProperties = new HashMap<>(2);
versionProperties.put(VersionModel.PROP_VERSION_TYPE, versionType);
if (reason != null)
{
versionProperties.put(VersionModel.PROP_DESCRIPTION, reason);
}
versionService.createVersion(nodeRef, versionProperties);
}
@Override
@@ -2533,7 +2540,7 @@ public class NodesImpl implements Nodes
}
// Create a new file.
final Node fileNode = createNewFile(parentNodeRef, fileName, nodeTypeQName, content, properties, assocTypeQName, parameters);
final Node fileNode = createNewFile(parentNodeRef, fileName, nodeTypeQName, content, properties, assocTypeQName, parameters, majorVersion, versionComment);
// RA-1052
try
@@ -2585,7 +2592,8 @@ public class NodesImpl implements Nodes
}
}
private Node createNewFile(NodeRef parentNodeRef, String fileName, QName nodeType, Content content, Map<QName, Serializable> props, QName assocTypeQName, Parameters params)
private Node createNewFile(NodeRef parentNodeRef, String fileName, QName nodeType, Content content, Map<QName, Serializable> props, QName assocTypeQName, Parameters params,
Boolean versionMajor, String versionComment)
{
if (nodeType == null)
{
@@ -2597,11 +2605,24 @@ public class NodesImpl implements Nodes
// Write content
writeContent(nodeRef, fileName, content.getInputStream(), true);
// Ensure the file is versionable (autoVersion = true, autoVersionProps = false)
ensureVersioningEnabled(nodeRef, true, false);
behaviourFilter.disableBehaviour(nodeRef, ContentModel.ASPECT_VERSIONABLE);
try
{
// by default, first version is major, unless specified otherwise
VersionType versionType = VersionType.MAJOR;
if ((versionMajor != null) && (! versionMajor))
{
versionType = VersionType.MINOR;
}
// Extract the metadata
extractMetadata(nodeRef);
createVersion(nodeRef, false, versionType, versionComment);
extractMetadata(nodeRef);
}
finally
{
behaviourFilter.enableBehaviour(nodeRef, ContentModel.ASPECT_VERSIONABLE);
}
// Create the response
return getFolderOrDocumentFullInfo(nodeRef, parentNodeRef, nodeType, params);
@@ -2701,25 +2722,6 @@ public class NodesImpl implements Nodes
}
}
/**
* Ensures the given node has the {@code cm:versionable} aspect applied to it, and
* that it has the initial version in the version store.
*
* @param nodeRef the reference to the node to be checked
* @param autoVersion If the {@code cm:versionable} aspect is applied, should auto
* versioning be requested?
* @param autoVersionProps If the {@code cm:versionable} aspect is applied, should
* auto versioning of properties be requested?
*/
protected void ensureVersioningEnabled(NodeRef nodeRef, boolean autoVersion, boolean autoVersionProps)
{
Map<QName, Serializable> props = new HashMap<>(2);
props.put(ContentModel.PROP_AUTO_VERSION, autoVersion);
props.put(ContentModel.PROP_AUTO_VERSION_PROPS, autoVersionProps);
versionService.ensureVersioningEnabled(nodeRef, props);
}
/**
* Extracts the given node metadata asynchronously.
*

View File

@@ -0,0 +1,63 @@
/*
* #%L
* Alfresco Remote API
* %%
* Copyright (C) 2005 - 2016 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L%
*/
package org.alfresco.rest.api.model;
/**
* Version Options
*
* Initially, used by version revert. Should also be consistent with version options used with Node Create & Update.
*
* @author janv
*/
public class VersionOptions
{
private Boolean majorVersion;
private String comment;
public VersionOptions()
{
}
public String getComment()
{
return comment;
}
public void setComment(String comment)
{
this.comment = comment;
}
public Boolean getMajorVersion()
{
return majorVersion;
}
public void setMajorVersion(Boolean majorVersion)
{
this.majorVersion = majorVersion;
}
}

View File

@@ -25,11 +25,16 @@
*/
package org.alfresco.rest.api.nodes;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.version.Version2Model;
import org.alfresco.repo.version.VersionModel;
import org.alfresco.rest.api.Nodes;
import org.alfresco.rest.api.model.Node;
import org.alfresco.rest.api.model.NodeTarget;
import org.alfresco.rest.api.model.UserInfo;
import org.alfresco.rest.api.model.VersionOptions;
import org.alfresco.rest.framework.BinaryProperties;
import org.alfresco.rest.framework.Operation;
import org.alfresco.rest.framework.WebApiDescription;
import org.alfresco.rest.framework.core.exceptions.EntityNotFoundException;
import org.alfresco.rest.framework.resource.RelationshipResource;
@@ -39,15 +44,20 @@ import org.alfresco.rest.framework.resource.content.BinaryResource;
import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo;
import org.alfresco.rest.framework.resource.parameters.Paging;
import org.alfresco.rest.framework.resource.parameters.Parameters;
import org.alfresco.rest.framework.webscripts.WithResponse;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.coci.CheckOutCheckInService;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.version.Version;
import org.alfresco.service.cmr.version.VersionHistory;
import org.alfresco.service.cmr.version.VersionService;
import org.alfresco.service.cmr.version.VersionType;
import org.alfresco.util.ParameterCheck;
import org.alfresco.util.PropertyCheck;
import org.springframework.beans.factory.InitializingBean;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -171,21 +181,63 @@ public class NodeVersionsRelation implements
throw new EntityNotFoundException(nodeId+"-"+versionId);
}
private Version findVersion(String nodeId, String versionLabelId)
{
// note: sub-optimal
NodeRef nodeRef = nodes.validateOrLookupNode(nodeId, null);
VersionHistory vh = versionService.getVersionHistory(nodeRef);
if (vh != null)
@Operation("revert")
@WebApiDescription(title = "Revert Version",
description="Reverts (ie. promotes) specified version to become a new, most recent, version",
successStatus = HttpServletResponse.SC_OK)
public Node revertById(String nodeId, String versionId, VersionOptions versionOptions, Parameters parameters, WithResponse withResponse)
{
Version v = findVersion(nodeId, versionId);
if (v != null)
{
for (Version v : vh.getAllVersions())
CheckOutCheckInService cociService = sr.getCheckOutCheckInService();
NodeRef nodeRef = v.getVersionedNodeRef();
String versionComment = versionOptions.getComment();
VersionType versionType = VersionType.MINOR;
Boolean versionMajor = versionOptions.getMajorVersion();
if ((versionMajor != null) && (versionMajor))
{
if (v.getVersionLabel().equals(versionLabelId))
{
return v;
}
versionType = VersionType.MAJOR;
}
Map<String, Serializable> versionProperties = new HashMap<>(2);
versionProperties.put(VersionModel.PROP_VERSION_TYPE, versionType);
if (versionComment != null)
{
versionProperties.put(VersionModel.PROP_DESCRIPTION, versionComment);
}
//cancel editing if we want to revert
if (sr.getNodeService().hasAspect(nodeRef, ContentModel.ASPECT_WORKING_COPY))
{
nodeRef = cociService.cancelCheckout(nodeRef);
}
// TODO review default for deep and/or whether we should make it an option
versionService.revert(nodeRef, v, false);
// Checkout/Checkin the node - to store the new version in version history
NodeRef wcNodeRef = cociService.checkout(nodeRef);
cociService.checkin(wcNodeRef, versionProperties);
// get latest version
v = versionService.getVersionHistory(nodeRef).getHeadVersion();
Node node = nodes.getFolderOrDocumentFullInfo(v.getFrozenStateNodeRef(), null, null, parameters, null);
mapVersionInfo(v, node);
return node;
}
return null;
throw new EntityNotFoundException(nodeId+"-"+versionId);
}
private Version findVersion(String nodeId, String versionLabelId)
{
NodeRef nodeRef = nodes.validateOrLookupNode(nodeId, null);
return versionService.getVersionHistory(nodeRef).getVersion(versionLabelId);
}
}