Search in sources :

Example 1 with PolicyScope

use of org.alfresco.repo.policy.PolicyScope in project alfresco-repository by Alfresco.

the class VersionServiceImpl method createVersion.

/**
 * Creates a new version of the passed node assigning the version properties
 * accordingly.
 *
 * @param  nodeRef              a node reference
 * @param  origVersionProperties    the version properties
 * @param  versionNumber        the version number
 * @return                      the newly created version
 * @throws ReservedVersionNameException
 *                              thrown if there is a name clash in the version properties
 */
protected Version createVersion(NodeRef nodeRef, Map<String, Serializable> origVersionProperties, int versionNumber) throws ReservedVersionNameException {
    long startTime = System.currentTimeMillis();
    // Copy the version properties (to prevent unexpected side effects to the caller)
    Map<String, Serializable> versionProperties = new HashMap<String, Serializable>();
    if (origVersionProperties != null) {
        versionProperties.putAll(origVersionProperties);
    }
    // If the version aspect is not there then add it
    if (this.nodeService.hasAspect(nodeRef, ContentModel.ASPECT_VERSIONABLE) == false) {
        this.nodeService.addAspect(nodeRef, ContentModel.ASPECT_VERSIONABLE, null);
    }
    // Call the policy behaviour
    invokeBeforeCreateVersion(nodeRef);
    // Check that the supplied additional version properties do not clash with the reserved ones
    VersionUtil.checkVersionPropertyNames(versionProperties.keySet());
    // Check the repository for the version history for this node
    NodeRef versionHistoryRef = getVersionHistoryNodeRef(nodeRef);
    NodeRef currentVersionRef = null;
    if (versionHistoryRef == null) {
        // Create the version history
        versionHistoryRef = createVersionHistory(nodeRef);
    } else {
        // Since we have an exisiting version history we should be able to lookup
        // the current version
        currentVersionRef = getCurrentVersionNodeRef(versionHistoryRef, nodeRef);
        if (currentVersionRef == null) {
            throw new VersionServiceException(MSGID_ERR_NOT_FOUND);
        }
        // Need to check that we are not about to create branch since this is not currently supported
        VersionHistory versionHistory = buildVersionHistory(versionHistoryRef, nodeRef);
        Version currentVersion = getVersion(currentVersionRef);
        if (versionHistory.getSuccessors(currentVersion).size() != 0) {
            throw new VersionServiceException(MSGID_ERR_NO_BRANCHES);
        }
    }
    // Create the node details
    QName classRef = this.nodeService.getType(nodeRef);
    PolicyScope nodeDetails = new PolicyScope(classRef);
    // Get the node details by calling the onVersionCreate policy behaviour
    invokeOnCreateVersion(nodeRef, versionProperties, nodeDetails);
    // Create the new version node (child of the version history)
    NodeRef newVersionRef = createNewVersion(nodeRef, versionHistoryRef, getStandardVersionProperties(versionProperties, nodeRef, currentVersionRef, versionNumber), versionProperties, nodeDetails);
    if (currentVersionRef == null) {
        // Set the new version to be the root version in the version history
        this.dbNodeService.createAssociation(versionHistoryRef, newVersionRef, VersionServiceImpl.ASSOC_ROOT_VERSION);
    } else {
        // Relate the new version to the current version as its successor
        this.dbNodeService.createAssociation(currentVersionRef, newVersionRef, VersionServiceImpl.ASSOC_SUCCESSOR);
    }
    // Create the version data object
    Version version = this.getVersion(newVersionRef);
    // Set the new version label on the versioned node
    this.nodeService.setProperty(nodeRef, ContentModel.PROP_VERSION_LABEL, version.getVersionLabel());
    // Freeze the version label property
    Map<QName, Serializable> versionLabelAsMap = new HashMap<QName, Serializable>(1);
    versionLabelAsMap.put(ContentModel.PROP_VERSION_LABEL, version.getVersionLabel());
    this.freezeProperties(newVersionRef, versionLabelAsMap);
    // Invoke the policy behaviour
    invokeAfterCreateVersion(nodeRef, version);
    if (logger.isTraceEnabled()) {
        logger.trace("created Version (" + getVersionStoreReference() + ") " + nodeRef + " in " + (System.currentTimeMillis() - startTime) + " ms");
    }
    // Return the data object representing the newly created version
    return version;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) Serializable(java.io.Serializable) HashMap(java.util.HashMap) Version(org.alfresco.service.cmr.version.Version) QName(org.alfresco.service.namespace.QName) VersionServiceException(org.alfresco.service.cmr.version.VersionServiceException) VersionHistory(org.alfresco.service.cmr.version.VersionHistory) PolicyScope(org.alfresco.repo.policy.PolicyScope)

Example 2 with PolicyScope

use of org.alfresco.repo.policy.PolicyScope in project alfresco-repository by Alfresco.

the class Version2ServiceImpl method createVersion.

@Override
protected Version createVersion(NodeRef nodeRef, Map<String, Serializable> origVersionProperties, int versionNumber) throws ReservedVersionNameException {
    if (logger.isDebugEnabled()) {
        logger.debug("Run as user " + AuthenticationUtil.getRunAsUser());
        logger.debug("Fully authenticated " + AuthenticationUtil.getFullyAuthenticatedUser());
    }
    long startTime = System.currentTimeMillis();
    // Copy the version properties (to prevent unexpected side effects to the caller)
    Map<String, Serializable> versionProperties = new HashMap<String, Serializable>();
    if (origVersionProperties != null) {
        versionProperties.putAll(origVersionProperties);
    }
    // We don't want either the adding of the Versionable aspect, or the setting
    // of the version label, to affect the auditable properties on the node that
    // is being versioned.
    // So, disable the auditable aspect on that node for now
    policyBehaviourFilter.disableBehaviour(nodeRef, ContentModel.ASPECT_AUDITABLE);
    // If the version aspect is not there then add it to the 'live' (versioned) node
    if (this.nodeService.hasAspect(nodeRef, ContentModel.ASPECT_VERSIONABLE) == false) {
        // Add the versionable aspect to the node
        this.nodeService.addAspect(nodeRef, ContentModel.ASPECT_VERSIONABLE, null);
    }
    // Call the policy behaviour
    invokeBeforeCreateVersion(nodeRef);
    // version "description" property is added as a standard version property (if not null) rather than a metadata version property
    String versionDescription = (String) versionProperties.get(Version.PROP_DESCRIPTION);
    versionProperties.remove(Version.PROP_DESCRIPTION);
    // don'tĀ freeze previous version label
    versionProperties.remove(ContentModel.PROP_VERSION_LABEL);
    // Check that the supplied additional version properties do not clash with the reserved ones
    VersionUtil.checkVersionPropertyNames(versionProperties.keySet());
    // Check the repository for the version history for this node
    NodeRef versionHistoryRef = getVersionHistoryNodeRef(nodeRef);
    NodeRef currentVersionRef = null;
    Version currentVersion = null;
    if (versionHistoryRef == null) {
        // Create the version history
        versionHistoryRef = createVersionHistory(nodeRef);
    } else {
        // ALF-3962 fix
        // check for corrupted version histories that are marked with version label "0"
        checkForCorruptedVersions(versionHistoryRef, nodeRef);
        // Since we have an existing version history we should be able to lookup
        // the current version
        Pair<Boolean, Version> result = getCurrentVersionImpl(versionHistoryRef, nodeRef);
        boolean headVersion = false;
        if (result != null) {
            currentVersion = result.getSecond();
            headVersion = result.getFirst();
        }
        if (currentVersion == null) {
            throw new VersionServiceException(MSGID_ERR_NOT_FOUND);
        }
        // Need to check that we are not about to create branch since this is not currently supported
        if (!headVersion) {
            // belt-and-braces - remove extra check at some point
            // although child assocs should be in ascending time (hence version creation) order
            VersionHistory versionHistory = buildVersionHistory(versionHistoryRef, nodeRef);
            if (versionHistory.getSuccessors(currentVersion).size() == 0) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Belt-and-braces: current version does seem to be head version [" + versionHistoryRef + ", " + nodeRef + "]");
                }
            } else {
                throw new VersionServiceException(MSGID_ERR_NO_BRANCHES);
            }
        }
    }
    // Create the node details
    QName classRef = this.nodeService.getType(nodeRef);
    PolicyScope nodeDetails = new PolicyScope(classRef);
    // Get the node details by calling the onVersionCreate policy behaviour
    invokeOnCreateVersion(nodeRef, versionProperties, nodeDetails);
    // Calculate the version label
    String versionLabel = invokeCalculateVersionLabel(classRef, currentVersion, versionNumber, versionProperties);
    // Extract Type Definition
    QName sourceTypeRef = nodeService.getType(nodeRef);
    long nodeDbId = (Long) this.nodeService.getProperty(nodeRef, ContentModel.PROP_NODE_DBID);
    Set<QName> nodeAspects = this.nodeService.getAspects(nodeRef);
    // Create the new version node (child of the version history)
    NodeRef newVersionRef = createNewVersion(sourceTypeRef, versionHistoryRef, getStandardVersionProperties(nodeRef, nodeDbId, nodeAspects, versionNumber, versionLabel, versionDescription), versionProperties, versionNumber, nodeDetails);
    if (currentVersionRef == null) {
        // Set the new version to be the root version in the version history
        this.dbNodeService.createAssociation(versionHistoryRef, newVersionRef, Version2Model.ASSOC_ROOT_VERSION);
    }
    // Create the version data object
    Version version = getVersion(newVersionRef);
    // Disabling behavior to be able to create properties for a locked node, see ALF-16540
    policyBehaviourFilter.disableBehaviour(nodeRef, ContentModel.ASPECT_LOCKABLE);
    try {
        // Set the new version label on the 'live' (versioned) node
        this.nodeService.setProperty(nodeRef, ContentModel.PROP_VERSION_LABEL, version.getVersionLabel());
        // Set the version type (MNT-14681 fix)
        this.nodeService.setProperty(nodeRef, ContentModel.PROP_VERSION_TYPE, version.getVersionType());
    } finally {
        policyBehaviourFilter.enableBehaviour(nodeRef, ContentModel.ASPECT_LOCKABLE);
    }
    // Re-enable the auditable aspect (if we turned it off earlier)
    policyBehaviourFilter.enableBehaviour(nodeRef, ContentModel.ASPECT_AUDITABLE);
    // Invoke the policy behaviour
    invokeAfterCreateVersion(nodeRef, version);
    if (logger.isTraceEnabled()) {
        logger.trace("created version (" + getVersionStoreReference() + ") " + newVersionRef + " " + (System.currentTimeMillis() - startTime) + " ms");
    }
    // Return the data object representing the newly created version
    return version;
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName) VersionHistory(org.alfresco.service.cmr.version.VersionHistory) PolicyScope(org.alfresco.repo.policy.PolicyScope) NodeRef(org.alfresco.service.cmr.repository.NodeRef) Version(org.alfresco.service.cmr.version.Version) VersionServiceException(org.alfresco.service.cmr.version.VersionServiceException)

Aggregations

Serializable (java.io.Serializable)2 HashMap (java.util.HashMap)2 PolicyScope (org.alfresco.repo.policy.PolicyScope)2 NodeRef (org.alfresco.service.cmr.repository.NodeRef)2 Version (org.alfresco.service.cmr.version.Version)2 VersionHistory (org.alfresco.service.cmr.version.VersionHistory)2 VersionServiceException (org.alfresco.service.cmr.version.VersionServiceException)2 QName (org.alfresco.service.namespace.QName)2