/*
 * Copyright 2009 Wyona
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.wyona.org/licenses/APACHE-LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.wyona.yanel.core.util;

import org.wyona.yanel.core.Resource;
import org.wyona.yanel.core.api.attributes.ModifiableV2;
import org.wyona.yanel.core.api.attributes.VersionableV2;
import org.wyona.yanel.core.attributes.versionable.RevisionInformation;

import org.apache.log4j.Logger;

/**
 *
 */
public class VersioningUtil {

    private static Logger log = Logger.getLogger(VersioningUtil.class);

    /**
     * @param resource Resource which shall be rolled back
     * @param revisionName Revision name to which shall be rolled back
     * @param userName User who is executing the roll back
     */
    public static void rollBack(Resource resource, String revisionName, String userName) {
        if (ResourceAttributeHelper.hasAttributeImplemented(resource, "Versionable", "2")) {
            try {
                VersionableV2 versionableRes = (VersionableV2) resource;
                if (!versionableRes.isCheckedOut()) {
                    versionableRes.checkout(userName);
                    versionableRes.restore(revisionName);
                    versionableRes.checkin("Rolled back to revision '" + revisionName + "'");
                } else {
                    log.warn("Resource is already checked out by user '" + versionableRes.getCheckoutUserID() + "' and hence cannot be rolled back at the moment!");
                }
            } catch(Exception e) {
                log.error(e, e);
            }
        } else {
            log.warn("Cannot be rolled back, because resource is not VersionableV2");
        }
    }
}

@@ -779,32 +779,91 @@
 .VersionableV2;%0A
+import org.wyona.yanel.core.api.attributes.WorkflowableV1;%0A
 import org.wyona
@@ -958,16 +958,77 @@
 %0A%0A/**%0A *
+ Utility class in order to roll-back to a particular revision
 %0A */%0Apub
@@ -1865,24 +1865,751 @@
 ame + %22'%22);%0A
+                    if (ResourceAttributeHelper.hasAttributeImplemented(resource, %22Workflowable%22, %221%22)) %7B%0A                        WorkflowableV1 workflowableRes = (WorkflowableV1) resource;%0A                        // TBD: Should the workflow state of the old revision be used (as is) or rather workflow.getInitialState()%0A                        //log.debug(%22Revision head: %22 + getRevisionHead(versionableRes));%0A                        workflowableRes.setWorkflowState(workflowableRes.getWorkflowState(revisionName), getRevisionHead(versionableRes));%0A                    %7D else %7B%0A                        log.info(%22Cannot set workflow, because resource '%22 + resource.getPath() + %22' is not WorkflowableV1%22);%0A                    %7D%0A
             
@@ -2960,24 +2960,53 @@
 use resource
+ '%22 + resource.getPath() + %22'
  is not Vers
@@ -3033,11 +3033,340 @@
  %7D%0A    %7D
+%0A%0A    /**%0A     * Get revision number/name of head (most recent) revision (WARN: Peformance/Scalability)%0A     */%0A    private static String getRevisionHead(VersionableV2 resource) throws Exception %7B%0A        RevisionInformation%5B%5D ri = resource.getRevisions();%0A        java.util.Arrays.sort(ri);%0A        return ri%5B0%5D.getName();%0A    %7D
 %0A%7D%0A

set workflow if applicable