AnsweredAssumed Answered

Can not update version properties using VersionServicePolicies

Question asked by mtsiak on Oct 2, 2017

Hi all,

I have been trying to update version properties fields using the above behaviour:

public class WatermarkCustomBehaviour
        implements VersionServicePolicies.AfterCreateVersionPolicy,
        VersionServicePolicies.BeforeCreateVersionPolicy {
    private VersionService versionService;
    public void init() {
        getPolicyComponent().bindClassBehaviour(
                VersionServicePolicies.AfterCreateVersionPolicy.QNAME,
                ContentModel.TYPE_CONTENT,
                new JavaBehaviour(this, "afterCreateVersion",  NotificationFrequency.TRANSACTION_COMMIT));
        getPolicyComponent().bindClassBehaviour(
                VersionServicePolicies.BeforeCreateVersionPolicy.QNAME,
                ContentModel.TYPE_CONTENT,
                new JavaBehaviour(this, "beforeCreateVersion", NotificationFrequency.TRANSACTION_COMMIT));    
    }
    @Override
    public void beforeCreateVersion(NodeRef versionableNode) {
        System.out.println("\n=== beforeCreateVersion - start ===");
        Version current = versionService.getCurrentVersion(versionableNode);
        System.out.println("Current Version Name: " + current.getVersionProperties().get("name"));
        VersionHistory h = versionService.getVersionHistory(versionableNode);
        Version previous = h.getPredecessor(current);
        if(previous != null)
            System.out.println("Previous Version Name: " + previous.getVersionProperties().get("name"));
        System.out.println("Changing current name, add watermark...");
        current.getVersionProperties().put("name", "beforeCreateVersionName");
        current.getVersionProperties().put("watermark", "beforeCreateVersionWatermark");
        System.out.println("=== beforeCreateVersion - end ===\n");
    }
    @Override
    public void afterCreateVersion(NodeRef versionableNode, Version version) {
        System.out.println("\n=== afterCreateVersion - start ===");
        System.out.println("Current Version Name: " + version.getVersionProperties().get("name"));
        VersionHistory h = versionService.getVersionHistory(versionableNode);
        Version previous = h.getPredecessor(version);
        if(previous != null) {
            System.out.println("Previous Version Name: " + previous.getVersionProperties().get("name"));
            System.out.println("Previous Version watermark: " + previous.getVersionProperties().get("watermark"));
        }
        System.out.println("Changing current name...");
        version.getVersionProperties().put("name", "afterCreateVersionName");
        version.getVersionProperties().put("watermark", "afterCreateVersionWatermark");
        System.out.println("=== afterCreateVersion - end ===\n");
    }
//setters, getters

}

 

Neither the name property, nor the watermark property is beeing updated. Any ideas? Thanks in advance.

Outcomes