001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 * $Id: LifeCycleTransitionConfiguration.java 16046 2007-04-12 14:34:58Z fguillaume $
019 */
020
021package org.nuxeo.ecm.core.lifecycle.extensions;
022
023import java.util.ArrayList;
024import java.util.Collection;
025
026import org.nuxeo.ecm.core.lifecycle.LifeCycleTransition;
027import org.nuxeo.ecm.core.lifecycle.impl.LifeCycleTransitionImpl;
028import org.w3c.dom.Element;
029import org.w3c.dom.NodeList;
030
031/**
032 * Configuration helper class for transition.
033 *
034 * @see org.nuxeo.ecm.core.lifecycle.impl.LifeCycleServiceImpl
035 * @see org.nuxeo.ecm.core.lifecycle.LifeCycleTransition
036 * @author <a href="mailto:ja@nuxeo.com">Julien Anguenot</a>
037 */
038public class LifeCycleTransitionConfiguration {
039
040    private static final String TAG_LIFECYCLE = "lifecycle";
041
042    private static final String TAG_TRANSITIONS = "transitions";
043
044    private static final String TAG_TRANSITION = "transition";
045
046    private static final String ATTR_TRANSITION_NAME = "name";
047
048    private static final String TAG_TRANSITION_DESCRIPTION = "description";
049
050    private static final String ATTR_TRANSITION_DESTINATION_STATE = "destinationState";
051
052    /** The DOM element holding the states. */
053    private final Element element;
054
055    public LifeCycleTransitionConfiguration(Element element) {
056        this.element = element;
057    }
058
059    public Collection<LifeCycleTransition> getTransitions() {
060        Collection<LifeCycleTransition> transitions = new ArrayList<>();
061
062        NodeList transitionsElements = element.getElementsByTagName(TAG_TRANSITIONS);
063        if (transitionsElements.getLength() <= 0) {
064            return transitions;
065        }
066        // NXP-1472 : don't get the first element, but the first one attached to <lifecycle>
067        Element transitionsElement = null;
068        for (int i = 0; i < transitionsElements.getLength(); i++) {
069            transitionsElement = (Element) transitionsElements.item(i);
070            if (TAG_LIFECYCLE.equals(transitionsElement.getParentNode().getNodeName())) {
071                break;
072            }
073        }
074        NodeList elements = transitionsElement.getElementsByTagName(TAG_TRANSITION); // NOSONAR
075        int len = elements.getLength();
076        for (int i = 0; i < len; i++) {
077            Element element = (Element) elements.item(i);
078
079            String name = element.getAttribute(ATTR_TRANSITION_NAME);
080            String destinationState = element.getAttribute(ATTR_TRANSITION_DESTINATION_STATE);
081            String description = "";
082
083            if (element.getElementsByTagName(TAG_TRANSITION_DESCRIPTION).getLength() > 0) {
084                description = element.getElementsByTagName(TAG_TRANSITION_DESCRIPTION).item(0).getTextContent();
085            }
086            transitions.add(new LifeCycleTransitionImpl(name, description, destinationState));
087        }
088        return transitions;
089    }
090
091}