001/*
002 * (C) Copyright 2010-2012 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 *     Alexandre Russel
018 *     Florent Guillaume
019 */
020package org.nuxeo.ecm.platform.routing.core.impl;
021
022import java.io.Serializable;
023import java.util.ArrayList;
024import java.util.List;
025import java.util.Map;
026
027import org.nuxeo.ecm.core.api.CoreSession;
028import org.nuxeo.ecm.core.api.DocumentModel;
029import org.nuxeo.ecm.core.api.DocumentModelList;
030import org.nuxeo.ecm.platform.routing.api.DocumentRouteElement;
031
032public abstract class AbstractRunner implements ElementRunner {
033
034    protected List<DocumentRouteElement> getChildrenElement(CoreSession session, DocumentRouteElement element) {
035        DocumentModelList children = session.getChildren(element.getDocument().getRef());
036        List<DocumentRouteElement> elements = new ArrayList<DocumentRouteElement>();
037        for (DocumentModel model : children) {
038            elements.add(model.getAdapter(DocumentRouteElement.class));
039        }
040        return elements;
041    }
042
043    @Override
044    public void run(CoreSession session, DocumentRouteElement element, Map<String, Serializable> map) {
045        run(session, element);
046    }
047
048    @Override
049    public void resume(CoreSession session, DocumentRouteElement element, String nodeId, String taskId,
050            Map<String, Object> data, String status) {
051        throw new UnsupportedOperationException();
052    }
053
054    @Deprecated
055    @Override
056    public void undo(CoreSession session, DocumentRouteElement element) {
057        throw new UnsupportedOperationException();
058    }
059
060    @Override
061    public void cancel(CoreSession session, DocumentRouteElement element) {
062        List<DocumentRouteElement> children = getChildrenElement(session, element);
063        for (DocumentRouteElement child : children) {
064            child.cancel(session);
065        }
066        element.setCanceled(session);
067    }
068}