Search in sources :

Example 6 with VersionServiceException

use of org.alfresco.service.cmr.version.VersionServiceException in project alfresco-repository by Alfresco.

the class Version2ServiceImpl method restore.

@Override
@Extend(extensionAPI = VersionServiceExtension.class, traitAPI = VersionServiceTrait.class)
public NodeRef restore(NodeRef nodeRef, NodeRef parentNodeRef, QName assocTypeQName, QName assocQName, boolean deep) {
    if (logger.isDebugEnabled()) {
        logger.debug("Run as user " + AuthenticationUtil.getRunAsUser());
        logger.debug("Fully authenticated " + AuthenticationUtil.getFullyAuthenticatedUser());
    }
    NodeRef restoredNodeRef = null;
    // Check that the node does not exist
    if (this.nodeService.exists(nodeRef) == true) {
        // Error since you can not restore a node that already exists
        throw new VersionServiceException(MSGID_ERR_RESTORE_EXISTS, new Object[] { nodeRef.toString() });
    }
    // Try and get the version details that we want to restore to
    Version version = getHeadVersion(nodeRef);
    if (version == null) {
        // Error since there is no version information available to restore the node from
        throw new VersionServiceException(MSGID_ERR_RESTORE_NO_VERSION, new Object[] { nodeRef.toString() });
    }
    // Set the uuid of the new node
    Map<QName, Serializable> props = new HashMap<QName, Serializable>(1);
    props.put(ContentModel.PROP_NODE_UUID, ((NodeRef) version.getVersionProperty(Version2Model.PROP_FROZEN_NODE_REF)).getId());
    props.put(ContentModel.PROP_VERSION_LABEL, version.getVersionLabel());
    // Get the type of the frozen node
    QName type = (QName) dbNodeService.getType(VersionUtil.convertNodeRef(version.getFrozenStateNodeRef()));
    // Disable auto-version behaviour
    this.policyBehaviourFilter.disableBehaviour(ContentModel.ASPECT_VERSIONABLE);
    try {
        // Create the restored node
        restoredNodeRef = this.nodeService.createNode(parentNodeRef, assocTypeQName, assocQName, type, props).getChildRef();
    } finally {
        // Enable auto-version behaviour
        this.policyBehaviourFilter.enableBehaviour(ContentModel.ASPECT_VERSIONABLE);
    }
    // Now we need to revert the newly restored node
    revert(restoredNodeRef, version, deep);
    return restoredNodeRef;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) Serializable(java.io.Serializable) Version(org.alfresco.service.cmr.version.Version) HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName) VersionServiceException(org.alfresco.service.cmr.version.VersionServiceException) Extend(org.alfresco.traitextender.Extend)

Example 7 with VersionServiceException

use of org.alfresco.service.cmr.version.VersionServiceException 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)

Example 8 with VersionServiceException

use of org.alfresco.service.cmr.version.VersionServiceException in project alfresco-repository by Alfresco.

the class AbstractVersionServiceImpl method invokeCalculateVersionLabel.

/**
 * Invoke the calculate version label policy behaviour
 *
 * @param classRef QName
 * @param preceedingVersion Version
 * @param versionNumber int
 * @return String
 */
protected String invokeCalculateVersionLabel(QName classRef, Version preceedingVersion, int versionNumber, Map<String, Serializable> versionProperties) {
    String versionLabel = null;
    Collection<CalculateVersionLabelPolicy> behaviours = this.calculateVersionLabelDelegate.getList(classRef);
    if (behaviours.size() == 0) {
        // Default the version label to the SerialVersionLabelPolicy
        SerialVersionLabelPolicy defaultVersionLabelPolicy = new SerialVersionLabelPolicy();
        versionLabel = defaultVersionLabelPolicy.calculateVersionLabel(classRef, preceedingVersion, versionNumber, versionProperties);
    } else if (behaviours.size() == 1) {
        // Call the policy behaviour
        CalculateVersionLabelPolicy[] arr = behaviours.toArray(new CalculateVersionLabelPolicy[] {});
        versionLabel = arr[0].calculateVersionLabel(classRef, preceedingVersion, versionNumber, versionProperties);
    } else {
        // Error since we can only deal with a single caculate version label policy
        throw new VersionServiceException("More than one CalculateVersionLabelPolicy behaviour has been registered for the type " + classRef.toString());
    }
    return versionLabel;
}
Also used : CalculateVersionLabelPolicy(org.alfresco.repo.version.VersionServicePolicies.CalculateVersionLabelPolicy) VersionServiceException(org.alfresco.service.cmr.version.VersionServiceException) SerialVersionLabelPolicy(org.alfresco.repo.version.common.versionlabel.SerialVersionLabelPolicy)

Example 9 with VersionServiceException

use of org.alfresco.service.cmr.version.VersionServiceException in project alfresco-repository by Alfresco.

the class VersionServiceImplTest method testRestore.

/**
 * Test restore
 */
@Test
public void testRestore() {
    // Try and restore a node without any version history
    try {
        this.versionService.restore(new NodeRef(this.testStoreRef, "123"), rootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName("{test}MyVersionableNode"));
        fail("An exception should have been raised since this node has no version history.");
    } catch (VersionServiceException exception) {
    // We where expecting this exception
    }
    // Create a versionable node
    NodeRef versionableNode = createNewVersionableNode();
    createComment(versionableNode, "my comment", "Do great work", false);
    // It isn't currently versionable
    assertEquals(null, versionService.getVersionHistory(versionableNode));
    // Store the node details for later
    Set<QName> origAspects = this.dbNodeService.getAspects(versionableNode);
    // Try and restore the node (won't be allowed as it already exists!)
    try {
        this.versionService.restore(versionableNode, rootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName("{test}MyVersionableNode"));
        fail("An exception should have been raised since this node exists and you can't restore a node that exists.");
    } catch (VersionServiceException exception) {
    // We where expecting this exception
    }
    // Version it twice
    this.versionService.createVersion(versionableNode, null);
    this.versionService.createVersion(versionableNode, null);
    // Check we're now have a version history
    VersionHistory history = versionService.getVersionHistory(versionableNode);
    assertEquals("0.2", nodeService.getProperty(versionableNode, ContentModel.PROP_VERSION_LABEL));
    assertEquals("0.2", history.getHeadVersion().getVersionLabel());
    assertEquals(2, history.getAllVersions().size());
    // Delete the node
    this.dbNodeService.deleteNode(versionableNode);
    assertFalse(this.dbNodeService.exists(versionableNode));
    // You can still get the history of the node even though it's deleted
    history = versionService.getVersionHistory(versionableNode);
    assertEquals("0.2", history.getHeadVersion().getVersionLabel());
    assertEquals(2, history.getAllVersions().size());
    // Try and restore the node
    NodeRef restoredNode = this.versionService.restore(versionableNode, this.rootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName("{test}MyVersionableNode"));
    assertNotNull(restoredNode);
    assertTrue(this.dbNodeService.exists(restoredNode));
    // Check that the properties are correct
    assertEquals(VALUE_1, this.dbNodeService.getProperty(restoredNode, PROP_1));
    assertEquals(VALUE_2, this.dbNodeService.getProperty(restoredNode, PROP_2));
    assertEquals(VALUE_3, this.dbNodeService.getProperty(restoredNode, PROP_3));
    // Check that the content is correct
    ContentReader contentReader2 = this.contentService.getReader(restoredNode, ContentModel.PROP_CONTENT);
    assertNotNull(contentReader2);
    assertEquals(TEST_CONTENT, contentReader2.getContentString());
    // Check that the ContentModel.PROP_VERSION_LABEL property is correct
    String versionLabel = (String) this.dbNodeService.getProperty(restoredNode, ContentModel.PROP_VERSION_LABEL);
    assertNotNull(versionLabel);
    assertEquals("0.2", versionLabel);
    // Check that the aspects have been reverted correctly
    Set<QName> aspects2 = this.dbNodeService.getAspects(restoredNode);
    assertEquals(aspects2.size(), origAspects.size());
    // Check the version is back to what it was
    history = versionService.getVersionHistory(restoredNode);
    assertEquals("0.2", history.getHeadVersion().getVersionLabel());
    assertEquals(2, history.getAllVersions().size());
    Version[] versions = history.getAllVersions().toArray(new Version[2]);
    assertEquals("0.2", versions[0].getVersionLabel());
    assertEquals("0.1", versions[1].getVersionLabel());
    // TODO Shouldn't these point to the restored node?
    // assertEquals(restoredNode, versions[0].getFrozenStateNodeRef());
    // assertEquals(restoredNode, versions[1].getFrozenStateNodeRef());
    // TODO Should we really be having reference to version store
    // as the frozen state noderef?
    assertEquals(VersionService.VERSION_STORE_PROTOCOL, versions[0].getFrozenStateNodeRef().getStoreRef().getProtocol());
    assertEquals(VersionService.VERSION_STORE_PROTOCOL, versions[1].getFrozenStateNodeRef().getStoreRef().getProtocol());
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) Version(org.alfresco.service.cmr.version.Version) QName(org.alfresco.service.namespace.QName) ContentReader(org.alfresco.service.cmr.repository.ContentReader) VersionServiceException(org.alfresco.service.cmr.version.VersionServiceException) VersionHistory(org.alfresco.service.cmr.version.VersionHistory) Test(org.junit.Test)

Aggregations

VersionServiceException (org.alfresco.service.cmr.version.VersionServiceException)9 NodeRef (org.alfresco.service.cmr.repository.NodeRef)8 QName (org.alfresco.service.namespace.QName)7 Version (org.alfresco.service.cmr.version.Version)6 Serializable (java.io.Serializable)5 HashMap (java.util.HashMap)4 VersionHistory (org.alfresco.service.cmr.version.VersionHistory)4 ArrayList (java.util.ArrayList)3 AssociationRef (org.alfresco.service.cmr.repository.AssociationRef)3 ChildAssociationRef (org.alfresco.service.cmr.repository.ChildAssociationRef)3 HashSet (java.util.HashSet)2 PolicyScope (org.alfresco.repo.policy.PolicyScope)2 Extend (org.alfresco.traitextender.Extend)2 CalculateVersionLabelPolicy (org.alfresco.repo.version.VersionServicePolicies.CalculateVersionLabelPolicy)1 VersionHistoryImpl (org.alfresco.repo.version.common.VersionHistoryImpl)1 SerialVersionLabelPolicy (org.alfresco.repo.version.common.versionlabel.SerialVersionLabelPolicy)1 AspectDefinition (org.alfresco.service.cmr.dictionary.AspectDefinition)1 TypeDefinition (org.alfresco.service.cmr.dictionary.TypeDefinition)1 ContentReader (org.alfresco.service.cmr.repository.ContentReader)1 Test (org.junit.Test)1