RM-1864: New version is created when "Declare as version record"

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/BRANCHES/V2.3@97108 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Roy Wetherall
2015-02-16 02:08:54 +00:00
parent 2ee573c5ba
commit 552359138e
19 changed files with 1138 additions and 101 deletions

View File

@@ -18,28 +18,20 @@
*/
package org.alfresco.module.org_alfresco_module_rm.action.dm;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.model.ContentModel;
import org.alfresco.module.org_alfresco_module_rm.action.AuditableActionExecuterAbstractBase;
import org.alfresco.module.org_alfresco_module_rm.fileplan.FilePlanService;
import org.alfresco.module.org_alfresco_module_rm.model.RecordsManagementModel;
import org.alfresco.module.org_alfresco_module_rm.version.RecordableVersionServiceImpl;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork;
import org.alfresco.repo.version.VersionModel;
import org.alfresco.module.org_alfresco_module_rm.util.AuthenticationUtil;
import org.alfresco.module.org_alfresco_module_rm.version.RecordableVersionService;
import org.alfresco.service.cmr.action.Action;
import org.alfresco.service.cmr.action.ParameterDefinition;
import org.alfresco.service.cmr.dictionary.DictionaryService;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.version.Version;
import org.alfresco.service.cmr.version.VersionService;
import org.alfresco.service.cmr.version.VersionType;
import org.alfresco.service.namespace.QName;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -52,7 +44,7 @@ import org.apache.commons.logging.LogFactory;
* @author Roy Wetherall
*/
public class DeclareAsVersionRecordAction extends AuditableActionExecuterAbstractBase
implements RecordsManagementModel
implements RecordsManagementModel
{
/** Logger */
private static Log logger = LogFactory.getLog(DeclareAsVersionRecordAction.class);
@@ -64,10 +56,10 @@ public class DeclareAsVersionRecordAction extends AuditableActionExecuterAbstrac
public static final String PARAM_FILE_PLAN = "file-plan";
/** Sync Model URI */
static final String SYNC_MODEL_1_0_URI = "http://www.alfresco.org/model/sync/1.0";
private static final String SYNC_MODEL_1_0_URI = "http://www.alfresco.org/model/sync/1.0";
/** Synced aspect */
static final QName ASPECT_SYNCED = QName.createQName(SYNC_MODEL_1_0_URI, "synced");
private static final QName ASPECT_SYNCED = QName.createQName(SYNC_MODEL_1_0_URI, "synced");
/** Node service */
private NodeService nodeService;
@@ -78,8 +70,11 @@ public class DeclareAsVersionRecordAction extends AuditableActionExecuterAbstrac
/** Dictionary service */
private DictionaryService dictionaryService;
/** version service */
private VersionService versionService;
/** recordable version service */
private RecordableVersionService recordableVersionService;
/** authentication util */
private AuthenticationUtil authenticationUtil;
/**
* @param nodeService node service
@@ -106,12 +101,20 @@ public class DeclareAsVersionRecordAction extends AuditableActionExecuterAbstrac
}
/**
* @param versionService version service
* @param recordableVersionService recordable version service
*/
public void setVersionService(VersionService versionService)
public void setRecordableVersionService(RecordableVersionService recordableVersionService)
{
this.versionService = versionService;
}
this.recordableVersionService = recordableVersionService;
}
/**
* @param authenticationUtil authentication util
*/
public void setAuthenticationUtil(AuthenticationUtil authenticationUtil)
{
this.authenticationUtil = authenticationUtil;
}
/**
* @see org.alfresco.repo.action.executer.ActionExecuterAbstractBase#executeImpl(org.alfresco.service.cmr.action.Action, org.alfresco.service.cmr.repository.NodeRef)
@@ -182,14 +185,14 @@ public class DeclareAsVersionRecordAction extends AuditableActionExecuterAbstrac
{
// TODO .. eventually make the file plan parameter required
filePlan = AuthenticationUtil.runAs(new RunAsWork<NodeRef>()
filePlan = authenticationUtil.runAs(new org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork<NodeRef>()
{
@Override
public NodeRef doWork()
{
return filePlanService.getFilePlanBySiteId(FilePlanService.DEFAULT_RM_SITE_ID);
}
}, AuthenticationUtil.getAdminUserName());
}, authenticationUtil.getAdminUserName());
// if the file plan is still null, raise an exception
if (filePlan == null)
@@ -214,15 +217,8 @@ public class DeclareAsVersionRecordAction extends AuditableActionExecuterAbstrac
}
}
// create version properties
Map<String, Serializable> versionProperties = new HashMap<String, Serializable>(4);
versionProperties.put(Version.PROP_DESCRIPTION, "Recorded version"); // TODO make this a configurable option of the action
versionProperties.put(VersionModel.PROP_VERSION_TYPE, VersionType.MINOR); // TODO make this a configurable option of the action
versionProperties.put(RecordableVersionServiceImpl.KEY_RECORDABLE_VERSION, true);
versionProperties.put(RecordableVersionServiceImpl.KEY_FILE_PLAN, filePlan);
// create recordable version
versionService.createVersion(actionedUponNodeRef, versionProperties);
// create record from latest version
recordableVersionService.createRecordFromLatestVersion(filePlan, actionedUponNodeRef);
}
}

View File

@@ -287,6 +287,7 @@ public class RMAfterInvocationProvider extends RMSecurityCommon
}
@SuppressWarnings("rawtypes")
private List<ConfigAttributeDefintion> extractSupportedDefinitions(ConfigAttributeDefinition config)
{
List<ConfigAttributeDefintion> definitions = new ArrayList<>();

View File

@@ -854,9 +854,6 @@ public class RecordServiceImpl extends BaseBehaviourBean
// add the current owner to the list of extended writers
String owner = ownableService.getOwner(nodeRef);
// remove the owner
ownableService.setOwner(nodeRef, OwnableService.NO_OWNER);
// get the documents primary parent assoc
ChildAssociationRef parentAssoc = nodeService.getPrimaryParent(nodeRef);
@@ -907,9 +904,11 @@ public class RecordServiceImpl extends BaseBehaviourBean
// set the extended security
Set<String> combinedWriters = new HashSet<String>(writers);
combinedWriters.add(owner);
if (owner != null && !owner.isEmpty() && !owner.equals(OwnableService.NO_OWNER))
{
combinedWriters.add(owner);
}
combinedWriters.add(AuthenticationUtil.getFullyAuthenticatedUser());
extendedSecurityService.addExtendedSecurity(nodeRef, readers, combinedWriters);
}
finally
@@ -1187,6 +1186,9 @@ public class RecordServiceImpl extends BaseBehaviourBean
// remove versionable aspect(s)
nodeService.removeAspect(document, RecordableVersionModel.ASPECT_VERSIONABLE);
// remove the owner
ownableService.setOwner(document, OwnableService.NO_OWNER);
}
catch (FileNotFoundException e)
{

View File

@@ -0,0 +1,61 @@
/*
* Copyright (C) 2005-2014 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* 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/>.
*/
package org.alfresco.module.org_alfresco_module_rm.version;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.version.Version;
/**
* Recordable version service interface.
*
* @author Roy Wetherall
* @since 2.3
*/
public interface RecordableVersionService
{
/**
* Indicates whether the current version of a node is recorded or not.
* <p>
* Returns false if not versionable or no version.
*
* @param nodeRef node reference
* @return boolean true if latest version recorded, false otherwise
*/
boolean isCurrentVersionRecorded(NodeRef nodeRef);
/**
* Indicates whether a version is recorded or not.
*
* @param version version
* @return boolean true if recorded version, false otherwise
*/
boolean isRecordedVersion(Version version);
/**
* Creates a record from the latest version, marking it as recorded.
* <p>
* Does not create a record if the node is not versionable or the latest
* version is already recorded.
*
* @param nodeRef node reference
* @return NodeRef node reference to the crated record.
*/
NodeRef createRecordFromLatestVersion(NodeRef filePlan, NodeRef nodeRef);
}

View File

@@ -25,7 +25,9 @@ import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.model.ContentModel;
@@ -34,19 +36,24 @@ import org.alfresco.module.org_alfresco_module_rm.model.rma.type.CmObjectType;
import org.alfresco.module.org_alfresco_module_rm.model.security.ModelSecurityService;
import org.alfresco.module.org_alfresco_module_rm.record.RecordService;
import org.alfresco.module.org_alfresco_module_rm.relationship.RelationshipService;
import org.alfresco.module.org_alfresco_module_rm.security.ExtendedSecurityService;
import org.alfresco.module.org_alfresco_module_rm.util.AuthenticationUtil;
import org.alfresco.repo.policy.PolicyScope;
import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork;
import org.alfresco.repo.security.permissions.impl.ExtendedPermissionService;
import org.alfresco.repo.version.Version2Model;
import org.alfresco.repo.version.Version2ServiceImpl;
import org.alfresco.repo.version.VersionModel;
import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.security.OwnableService;
import org.alfresco.service.cmr.version.ReservedVersionNameException;
import org.alfresco.service.cmr.version.Version;
import org.alfresco.service.cmr.version.VersionHistory;
import org.alfresco.service.cmr.version.VersionType;
import org.alfresco.service.namespace.QName;
import org.alfresco.util.ParameterCheck;
import org.alfresco.util.PropertyMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -58,7 +65,8 @@ import org.apache.commons.logging.LogFactory;
* @since 2.3
*/
public class RecordableVersionServiceImpl extends Version2ServiceImpl
implements RecordableVersionModel
implements RecordableVersionModel,
RecordableVersionService
{
/** share logger with version2ServiceImpl */
private static Log logger = LogFactory.getLog(Version2ServiceImpl.class);
@@ -69,6 +77,23 @@ public class RecordableVersionServiceImpl extends Version2ServiceImpl
/** version record property */
public static final String PROP_VERSION_RECORD = "RecordVersion";
/** version aspect property names */
private static final String[] VERSION_PROPERTY_NAMES = new String[]
{
Version2Model.PROP_CREATED_DATE,
Version2Model.PROP_VERSION_LABEL,
Version2Model.PROP_VERSION_DESCRIPTION,
Version2Model.PROP_FROZEN_NODE_DBID,
Version2Model.PROP_FROZEN_NODE_REF,
Version2Model.PROP_FROZEN_CREATED,
Version2Model.PROP_FROZEN_CREATOR,
Version2Model.PROP_FROZEN_MODIFIED,
Version2Model.PROP_FROZEN_MODIFIER,
Version2Model.PROP_FROZEN_ACCESSED
};
/** file plan service */
private FilePlanService filePlanService;
@@ -87,6 +112,15 @@ public class RecordableVersionServiceImpl extends Version2ServiceImpl
/** cm object type */
private CmObjectType cmObjectType;
/** extended permission service */
private ExtendedPermissionService extendedPermissionService;
/** extended security service */
private ExtendedSecurityService extendedSecurityService;
/** ownable service */
private OwnableService ownableService;
/**
* @param filePlanService file plan service
@@ -135,6 +169,30 @@ public class RecordableVersionServiceImpl extends Version2ServiceImpl
{
this.cmObjectType = cmObjectType;
}
/**
* @param extendedPermissionService extended permission service
*/
public void setExtendedPermissionService(ExtendedPermissionService extendedPermissionService)
{
this.extendedPermissionService = extendedPermissionService;
}
/**
* @param extendedSecurityService extended security service
*/
public void setExtendedSecurityService(ExtendedSecurityService extendedSecurityService)
{
this.extendedSecurityService = extendedSecurityService;
}
/**
* @param ownableService ownable service
*/
public void setOwnableService(OwnableService ownableService)
{
this.ownableService = ownableService;
}
/**
* @see org.alfresco.repo.version.Version2ServiceImpl#createVersion(org.alfresco.service.cmr.repository.NodeRef, java.util.Map, int)
@@ -499,4 +557,245 @@ public class RecordableVersionServiceImpl extends Version2ServiceImpl
dbNodeService.setProperty(nodeRef, PROP_RECORDABLE_VERSION_POLICY, versionPolicy);
}
}
/**
* @see org.alfresco.module.org_alfresco_module_rm.version.RecordableVersionService#isLatestVersionRecorded(org.alfresco.service.cmr.repository.NodeRef)
*/
@Override
public boolean isCurrentVersionRecorded(NodeRef nodeRef)
{
boolean result = false;
Version version = getCurrentVersion(nodeRef);
if (version != null)
{
result = isRecordedVersion(version);
}
return result;
}
/**
* @see org.alfresco.module.org_alfresco_module_rm.version.RecordableVersionService#isRecordedVersion(org.alfresco.service.cmr.version.Version)
*/
@Override
public boolean isRecordedVersion(Version version)
{
boolean result = true;
if (version.getVersionProperties().get(PROP_VERSION_RECORD) == null)
{
result = false;
}
return result;
}
/**
* Create Version Store Ref
*
* @param store ref
* @return store ref for version store
*/
public StoreRef convertStoreRef(StoreRef storeRef)
{
return new StoreRef(StoreRef.PROTOCOL_WORKSPACE, storeRef.getIdentifier());
}
/**
* Convert the incomming node ref (with the version store protocol specified)
* to the internal representation with the workspace protocol.
*
* @param nodeRef the incomming verison protocol node reference
* @return the internal version node reference
*/
public NodeRef convertNodeRef(NodeRef nodeRef)
{
return new NodeRef(convertStoreRef(nodeRef.getStoreRef()), nodeRef.getId());
}
/**
* @see org.alfresco.module.org_alfresco_module_rm.version.RecordableVersionService#createRecordFromLatestVersion(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.cmr.repository.NodeRef)
*/
@Override
public NodeRef createRecordFromLatestVersion(final NodeRef filePlan, final NodeRef nodeRef)
{
ParameterCheck.mandatory("filePlan", filePlan);
ParameterCheck.mandatory("nodeRef", nodeRef);
NodeRef record = null;
// check for versionable aspect
if (nodeService.hasAspect(nodeRef, ContentModel.ASPECT_VERSIONABLE))
{
// get the latest version
final Version currentVersion = getCurrentVersion(nodeRef);
if (currentVersion != null &&
!isRecordedVersion(currentVersion))
{
// create the record from the current frozen state
record = authenticationUtil.runAsSystem(new RunAsWork<NodeRef>()
{
public NodeRef doWork() throws Exception
{
// get the documents readers
Long aclId = nodeService.getNodeAclId(nodeRef);
Set<String> readers = extendedPermissionService.getReaders(aclId);
Set<String> writers = extendedPermissionService.getWriters(aclId);
// add the current owner to the list of extended writers
String owner = ownableService.getOwner(nodeRef);
// grab the frozen state
NodeRef currentFrozenState = currentVersion.getFrozenStateNodeRef();
// determine the type of the object
QName type = nodeService.getType(currentFrozenState);
// grab all the properties
Map<QName, Serializable> properties = nodeService.getProperties(currentFrozenState);
// grab all the aspects
Set<QName> aspects = nodeService.getAspects(currentFrozenState);
// create the record
NodeRef record = recordService.createRecordFromContent(
filePlan,
(String)properties.get(ContentModel.PROP_NAME),
type,
properties,
null);
// apply aspects to record
for (QName aspect : aspects)
{
// add the aspect, properties have already been set
nodeService.addAspect(record, aspect, null);
}
// apply version record aspect to record
PropertyMap versionRecordProps = new PropertyMap(3);
versionRecordProps.put(PROP_VERSIONED_NODEREF, nodeRef);
versionRecordProps.put(RecordableVersionModel.PROP_VERSION_LABEL, currentVersion.getVersionLabel());
versionRecordProps.put(RecordableVersionModel.PROP_VERSION_DESCRIPTION, currentVersion.getDescription());
nodeService.addAspect(record, ASPECT_VERSION_RECORD, versionRecordProps);
// wire record up to previous record
linkToPreviousVersionRecord(nodeRef, record);
// set the extended security
Set<String> combinedWriters = new HashSet<String>(writers);
if (owner != null && !owner.isEmpty() && !owner.equals(OwnableService.NO_OWNER))
{
combinedWriters.add(owner);
}
combinedWriters.add(authenticationUtil.getFullyAuthenticatedUser());
extendedSecurityService.addExtendedSecurity(record, readers, combinedWriters);
return record;
}
});
// get the version history
NodeRef versionHistoryRef = getVersionHistoryNodeRef(nodeRef);
// get details from the version before we remove it
int versionNumber = getVersionNumber(currentVersion);
Map<QName, Serializable> versionProperties = getVersionAspectProperties(currentVersion);
QName sourceTypeRef = getVersionType(currentVersion);
// patch-up owner information, which needs to be frozen for recorded versions
String owner = (String)nodeService.getProperty(currentVersion.getFrozenStateNodeRef(), ContentModel.PROP_OWNER);
if (owner != null)
{
versionProperties.put(PROP_FROZEN_OWNER, owner);
}
// delete the current version
this.dbNodeService.deleteNode(convertNodeRef(currentVersion.getFrozenStateNodeRef()));
// create a new version history if we need to
if (!nodeService.exists(versionHistoryRef))
{
versionHistoryRef = createVersionHistory(nodeRef);
}
// create recorded version nodeRef
ChildAssociationRef childAssocRef = dbNodeService.createNode(
versionHistoryRef,
Version2Model.CHILD_QNAME_VERSIONS,
QName.createQName(Version2Model.NAMESPACE_URI, Version2Model.CHILD_VERSIONS + "-" + versionNumber),
sourceTypeRef,
null);
NodeRef versionNodeRef = childAssocRef.getChildRef();
// add aspect with the standard version properties to the 'version' node
nodeService.addAspect(versionNodeRef, Version2Model.ASPECT_VERSION, versionProperties);
// add the recordedVersion aspect with link to record
nodeService.addAspect(versionNodeRef, ASPECT_RECORDED_VERSION, Collections.singletonMap(PROP_RECORD_NODE_REF, (Serializable)record));
}
}
return record;
}
/**
* Helper method to get the version number of a given version by inspecting the
* name of the parent association.
*
* @param version version
* @return int version number
*/
private int getVersionNumber(Version version)
{
NodeRef versionNodeRef = getVersionNodeRef(version);
ChildAssociationRef assoc = dbNodeService.getPrimaryParent(versionNodeRef);
String fullVersionNumber = assoc.getQName().getLocalName();
String versionNumber = fullVersionNumber.substring(fullVersionNumber.indexOf("-") + 1);
return Integer.parseInt(versionNumber);
}
/**
* Helper method to get all the version aspect properties from an existing version
*
* @param version version
* @return Map<QName, Serializable> property values
*/
private Map<QName, Serializable> getVersionAspectProperties(Version version)
{
NodeRef versionNodeRef = getVersionNodeRef(version);
Map<QName, Serializable> versionProps = dbNodeService.getProperties(versionNodeRef);
Map<QName, Serializable> result = new HashMap<QName, Serializable>(9);
for (String propertyName : VERSION_PROPERTY_NAMES)
{
QName propertyQName = QName.createQName(Version2Model.NAMESPACE_URI, propertyName);
result.put(propertyQName, versionProps.get(propertyQName));
if (propertyName.equals(Version2Model.PROP_FROZEN_NODE_DBID))
{
System.out.println(versionProps.get(propertyQName));
}
}
return result;
}
/**
* Helper method to get the type of a versions frozen state
*
* @param currentVersion
* @return
*/
private QName getVersionType(Version version)
{
return nodeService.getType(getVersionNodeRef(version));
}
/**
* Helper method to get the internal node reference of a version
*
* @param version version
* @return NodeRef internal node reference to version
*/
private NodeRef getVersionNodeRef(Version version)
{
return convertNodeRef(version.getFrozenStateNodeRef());
}
}