<commit_before>/*
 * Copyright 2006 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.workflow;

import java.io.InputStream;
import java.util.Date;

import org.apache.log4j.Category;
import org.wyona.yanel.core.Resource;
import org.wyona.yanel.core.api.attributes.VersionableV2;
import org.wyona.yanel.core.api.attributes.ViewableV2;
import org.wyona.yanel.core.api.attributes.WorkflowableV1;
import org.wyona.yanel.core.attributes.versionable.RevisionInformation;
import org.wyona.yanel.core.attributes.viewable.View;
import org.wyona.yanel.core.util.DateUtil;
import org.wyona.yarep.core.Node;
import org.wyona.yarep.core.Property;
import org.wyona.yarep.core.Revision;

/**
 * This class provides some helper methods related to workflow for resources 
 * which are WorkflowableV1, VersionableV2, and ViewableV2 and which implement workflow
 * according to the following conditions:
 * - there is a resource config property 'workflow-schema' which points to the schema
 * - the workflow information is stored as properties of a repository node in the repository
 *   of the resource with the path of the resource.
 *
 */
public class WorkflowHelper {
    
    private static Category log = Category.getInstance(WorkflowHelper.class);

    protected static final String LIVE_REVISION_PROPERTY = "live-revision";
    protected static final String WORKFLOW_DATE_PROPERTY = "workflow-date";
    protected static final String WORKFLOW_STATE_PROPERTY = "workflow-state";

    public static void doTransition(Resource resource, String transitionID, String revision) 
            throws WorkflowException {
        boolean foundTransition = false;
        String currentState = null;
        WorkflowableV1 workflowable = (WorkflowableV1)resource;
        Workflow workflow = getWorkflow(resource);
        currentState = workflowable.getWorkflowState(revision);
        if (currentState == null) {
            currentState = workflow.getInitialState(); 
        }
        Transition[] transitions = workflow.getLeavingTransitions(currentState);
        
        for (int i = 0; i < transitions.length; i++) {
            if (transitions[i].getID().equals(transitionID)) {
                Condition[] conditions = transitions[i].getConditions();
                for (int j = 0; j < conditions.length; j++) {
                    if (!conditions[j].isComplied(workflowable, workflow, revision)) {
                        throw new WorkflowException("Workflow condition not complied for state " + 
                                currentState + " and transition " + transitionID);
                    }
                }
                String newState = transitions[i].getDestinationState();
                
                workflowable.setWorkflowState(newState, revision);
                
                Action[] actions = transitions[i].getActions();
                for (int j = 0; j < actions.length; j++) {
                    actions[j].execute(workflowable, workflow, revision);
                }
                
                foundTransition = true;
            }
        }
        if (!foundTransition) {
            throw new WorkflowException("Invalid workflow transition: " + transitionID + 
                    " for state: " + currentState);
        }
    }
    
    /*public static boolean canDoTransition(Resource resource, String transitionID, String revision) 
            throws WorkflowException {
        WorkflowableV1 workflowable = (WorkflowableV1)resource;
        Workflow workflow = getWorkflow(resource);
        String currentState = workflowable.getWorkflowState(revision);
        if (currentState == null) {
            currentState = workflow.getInitialState(); 
        }
        Transition[] transitions = workflow.getLeavingTransitions(currentState);
        
        for (int j = 0; j < transitions.length; j++) {
            Condition[] conditions = transitions[j].getConditions();
            for (int k = 0; k < conditions.length; k++) {
                if (!conditions[k].isComplied(workflowable, workflow, revision)) {
                    return true;
                }
            }
        }
        return false;
    }*/
    
    public static String getLiveRevision(Resource resource) throws WorkflowException {
        try {
            WorkflowableV1 workflowable = (WorkflowableV1)resource;
            if (getWorkflow(resource) == null) {
                RevisionInformation[] revisions = ((VersionableV2)resource).getRevisions();
                if (revisions.length == 0) {
                    return null;
                } else {
                    return revisions[revisions.length-1].getName();
                }
            } else {
                return workflowable.getWorkflowVariable(LIVE_REVISION_PROPERTY);
            }
        } catch (Exception e) {
            log.error(e, e);
            throw new WorkflowException(e.getMessage(), e);
        }
    }

    /**
     * Indicates whether the given resource is live, i.e. if the workflow variable
     * &quot;live-revision&quot; has a defined value.
     * If a resource does not have an associated workflow, it is considered as live.
     * @param resource
     * @return true if live, false otherwise
     * @throws WorkflowException
     */
    public static boolean isLive(Resource resource) throws WorkflowException {
        try {
            WorkflowableV1 workflowable = (WorkflowableV1)resource;
            if (getWorkflow(resource) == null ||
                    workflowable.getWorkflowVariable(LIVE_REVISION_PROPERTY) != null) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            log.error(e, e);
            throw new WorkflowException(e.getMessage(), e);
        }
    }
    
    public static View getLiveView(Resource resource, String viewid) throws Exception {
        if (((WorkflowableV1)resource).isLive()) {
            String liveRevision = getLiveRevision(resource);
            if (liveRevision != null) {
                return ((VersionableV2)resource).getView(viewid, liveRevision);
            } else {
                // if there are no revisions, show the normal view:
                return ((ViewableV2)resource).getView(viewid);
            }
        } else {
            return null;
        }
    }

    public static Workflow getWorkflow(Resource resource) throws WorkflowException {
        try {
            String workflowSchema = resource.getResourceConfigProperty("workflow-schema");
            if (workflowSchema == null) {
                return null;
            } else {
                WorkflowBuilder builder = new WorkflowBuilder();
                InputStream stream = resource.getRealm().getRepository().getNode(workflowSchema).getInputStream();
                // TODO: cache of workflow
                return builder.buildWorkflow(stream);
            }
        } catch (Exception e) {
            log.error(e, e);
            throw new WorkflowException(e.getMessage(), e);
        }
    }
    
    /**
     * Get introspection part re workflow and versioning
     */
    public static String getWorkflowIntrospection(Resource resource) throws WorkflowException {
        try {
            StringBuffer sb = new StringBuffer();
            RevisionInformation[] revisions = ((VersionableV2)resource).getRevisions();
            WorkflowableV1 workflowable = (WorkflowableV1)resource;
            String liveRevision = getLiveRevision(resource);
            if (revisions != null && revisions.length > 0) {
                sb.append("<versions xmlns=\"http://www.wyona.org/neutron/2.0\">");
                for (int i = revisions.length - 1; i >= 0; i--) {
                    
                    sb.append("<version url=\"?yanel.resource.revision=" + revisions[i].getName() + "\">");
                    sb.append("<comment>" + revisions[i].getComment() + "</comment>");
                    sb.append("<date>" + revisions[i].getDate() + "</date>");
                    sb.append("<user>" + revisions[i].getUser() + "</user>");
                    sb.append("<revision>" + revisions[i].getName() + "</revision>");
                    
                    Workflow workflow = getWorkflow(resource);
                    if (workflow != null) {
                        String state = workflowable.getWorkflowState(revisions[i].getName());
                        if (state == null) {
                            state = workflow.getInitialState();
                        }<commit_msg>check if resource is versionable<commit_after>/*
 * Copyright 2006 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.workflow;

import java.io.InputStream;
import java.util.Date;

import org.apache.log4j.Category;
import org.wyona.yanel.core.Resource;
import org.wyona.yanel.core.api.attributes.VersionableV2;
import org.wyona.yanel.core.api.attributes.ViewableV2;
import org.wyona.yanel.core.api.attributes.WorkflowableV1;
import org.wyona.yanel.core.attributes.versionable.RevisionInformation;
import org.wyona.yanel.core.attributes.viewable.View;
import org.wyona.yanel.core.util.DateUtil;
import org.wyona.yanel.core.util.ResourceAttributeHelper;
import org.wyona.yarep.core.Node;
import org.wyona.yarep.core.Property;
import org.wyona.yarep.core.Revision;

/**
 * This class provides some helper methods related to workflow for resources 
 * which are WorkflowableV1, VersionableV2, and ViewableV2 and which implement workflow
 * according to the following conditions:
 * - there is a resource config property 'workflow-schema' which points to the schema
 * - the workflow information is stored as properties of a repository node in the repository
 *   of the resource with the path of the resource.
 *
 */
public class WorkflowHelper {
    
    private static Category log = Category.getInstance(WorkflowHelper.class);

    protected static final String LIVE_REVISION_PROPERTY = "live-revision";
    protected static final String WORKFLOW_DATE_PROPERTY = "workflow-date";
    protected static final String WORKFLOW_STATE_PROPERTY = "workflow-state";

    public static void doTransition(Resource resource, String transitionID, String revision) 
            throws WorkflowException {
        boolean foundTransition = false;
        String currentState = null;
        WorkflowableV1 workflowable = (WorkflowableV1)resource;
        Workflow workflow = getWorkflow(resource);
        currentState = workflowable.getWorkflowState(revision);
        if (currentState == null) {
            currentState = workflow.getInitialState(); 
        }
        Transition[] transitions = workflow.getLeavingTransitions(currentState);
        
        for (int i = 0; i < transitions.length; i++) {
            if (transitions[i].getID().equals(transitionID)) {
                Condition[] conditions = transitions[i].getConditions();
                for (int j = 0; j < conditions.length; j++) {
                    if (!conditions[j].isComplied(workflowable, workflow, revision)) {
                        throw new WorkflowException("Workflow condition not complied for state " + 
                                currentState + " and transition " + transitionID);
                    }
                }
                String newState = transitions[i].getDestinationState();
                
                workflowable.setWorkflowState(newState, revision);
                
                Action[] actions = transitions[i].getActions();
                for (int j = 0; j < actions.length; j++) {
                    actions[j].execute(workflowable, workflow, revision);
                }
                
                foundTransition = true;
            }
        }
        if (!foundTransition) {
            throw new WorkflowException("Invalid workflow transition: " + transitionID + 
                    " for state: " + currentState);
        }
    }
    
    /*public static boolean canDoTransition(Resource resource, String transitionID, String revision) 
            throws WorkflowException {
        WorkflowableV1 workflowable = (WorkflowableV1)resource;
        Workflow workflow = getWorkflow(resource);
        String currentState = workflowable.getWorkflowState(revision);
        if (currentState == null) {
            currentState = workflow.getInitialState(); 
        }
        Transition[] transitions = workflow.getLeavingTransitions(currentState);
        
        for (int j = 0; j < transitions.length; j++) {
            Condition[] conditions = transitions[j].getConditions();
            for (int k = 0; k < conditions.length; k++) {
                if (!conditions[k].isComplied(workflowable, workflow, revision)) {
                    return true;
                }
            }
        }
        return false;
    }*/
    
    public static String getLiveRevision(Resource resource) throws WorkflowException {
        try {
            WorkflowableV1 workflowable = (WorkflowableV1)resource;
            if (getWorkflow(resource) == null) {
                RevisionInformation[] revisions = ((VersionableV2)resource).getRevisions();
                if (revisions.length == 0) {
                    return null;
                } else {
                    return revisions[revisions.length-1].getName();
                }
            } else {
                return workflowable.getWorkflowVariable(LIVE_REVISION_PROPERTY);
            }
        } catch (Exception e) {
            log.error(e, e);
            throw new WorkflowException(e.getMessage(), e);
        }
    }

    /**
     * Indicates whether the given resource is live, i.e. if the workflow variable
     * &quot;live-revision&quot; has a defined value.
     * If a resource does not have an associated workflow, it is considered as live.
     * @param resource
     * @return true if live, false otherwise
     * @throws WorkflowException
     */
    public static boolean isLive(Resource resource) throws WorkflowException {
        try {
            WorkflowableV1 workflowable = (WorkflowableV1)resource;
            if (getWorkflow(resource) == null ||
                    workflowable.getWorkflowVariable(LIVE_REVISION_PROPERTY) != null) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            log.error(e, e);
            throw new WorkflowException(e.getMessage(), e);
        }
    }
    
    public static View getLiveView(Resource resource, String viewid) throws Exception {
        if (((WorkflowableV1)resource).isLive()) {
            String liveRevision = getLiveRevision(resource);
            if (liveRevision != null) {
                return ((VersionableV2)resource).getView(viewid, liveRevision);
            } else {
                // if there are no revisions, show the normal view:
                return ((ViewableV2)resource).getView(viewid);
            }
        } else {
            return null;
        }
    }

    public static Workflow getWorkflow(Resource resource) throws WorkflowException {
        try {
            String workflowSchema = resource.getResourceConfigProperty("workflow-schema");
            if (workflowSchema == null) {
                return null;
            } else {
                WorkflowBuilder builder = new WorkflowBuilder();
                InputStream stream = resource.getRealm().getRepository().getNode(workflowSchema).getInputStream();
                // TODO: cache of workflow
                return builder.buildWorkflow(stream);
            }
        } catch (Exception e) {
            log.error(e, e);
            throw new WorkflowException(e.getMessage(), e);
        }
    }
    
    /**
     * Get introspection part re workflow and versioning
     */
    public static String getWorkflowIntrospection(Resource resource) throws WorkflowException {
        try {
            if (!ResourceAttributeHelper.hasAttributeImplemented(resource, "Versionable", "2")) throw new WorkflowException("Resource '" + resource.getClass().getName() + "'  has not VersionableV2 interface implemented!");

            RevisionInformation[] revisions = ((VersionableV2)resource).getRevisions();
            WorkflowableV1 workflowable = (WorkflowableV1)resource;
            String liveRevision = getLiveRevision(resource);

            if (revisions != null && revisions.length > 0) {
                StringBuffer sb = new StringBuffer();
                sb.append("<versions xmlns=\"http://www.wyona.org/neutron/2.0\">");
                for (int i = revisions.length - 1; i >= 0; i--) {
                    
                    sb.append("<version url=\"?yanel.resource.revision=" + revisions[i].getName() + "\">");
                    sb.append("<comment>" + revisions[i].getComment() + "</comment>");
                    sb.append("<date>" + revisions[i].getDate() + "</date>");
                    sb.append("<user>" + revisions[i].getUser() + "</user>");
                    sb.append("<revision>" + revisions[i].getName() + "</revision>");
                    
                    Workflow workflow = getWorkflow(resource);
                    if (workflow != null) {
                        String state = workflowable.getWorkflowState(revisions[i].getName());
                        if (state == null) {
                            state = workflow.getInitialState();
                        }
{'fraction_non_alphanumeric': 0.09721322099805574, 'fraction_numerical': 0.004644631669907107, 'mean_word_length': 2.135817104149026, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6035652', 'n_tokens_mistral': 4828, 'n_tokens_neox': 4560, 'n_words': 1225}