001/*
002 * (C) Copyright 2014 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 *     <a href="mailto:grenard@nuxeo.com">Guillaume Renard</a>
018 *
019 */
020
021package org.nuxeo.ecm.restapi.server.jaxrs.routing;
022
023import java.util.List;
024
025import javax.ws.rs.GET;
026import javax.ws.rs.Path;
027import javax.ws.rs.PathParam;
028import javax.ws.rs.Produces;
029import javax.ws.rs.core.Context;
030import javax.ws.rs.core.MediaType;
031import javax.ws.rs.core.UriInfo;
032
033import org.nuxeo.ecm.core.api.NuxeoException;
034import org.nuxeo.ecm.platform.routing.api.DocumentRoute;
035import org.nuxeo.ecm.platform.routing.api.DocumentRoutingService;
036import org.nuxeo.ecm.platform.routing.core.impl.jsongraph.JsonGraphRoute;
037import org.nuxeo.ecm.webengine.model.WebObject;
038import org.nuxeo.ecm.webengine.model.impl.DefaultObject;
039import org.nuxeo.runtime.api.Framework;
040
041/**
042 * @since 7.2
043 */
044@WebObject(type = "workflowModel")
045@Produces(MediaType.APPLICATION_JSON)
046public class WorkflowModelObject extends DefaultObject {
047
048    @GET
049    public List<DocumentRoute> getWorkflowModels(@Context UriInfo uriInfo) {
050        return Framework.getService(DocumentRoutingService.class).getAvailableDocumentRouteModel(
051                getContext().getCoreSession());
052    }
053
054    @GET
055    @Path("{modelName}")
056    public DocumentRoute getWorkflowModel(@PathParam("modelName") String modelName) {
057        DocumentRoute result = Framework.getService(DocumentRoutingService.class).getRouteModelWithId(
058                getContext().getCoreSession(), modelName);
059        return result;
060    }
061
062    @GET
063    @Path("{modelName}/graph")
064    public JsonGraphRoute getWorkflowModelGraph(@PathParam("modelName") String modelName) {
065        try {
066            final String id = Framework.getService(DocumentRoutingService.class).getRouteModelDocIdWithId(
067                    getContext().getCoreSession(), modelName);
068            return new JsonGraphRoute(getContext().getCoreSession(), id, getContext().getLocale());
069        } catch (NuxeoException e) {
070            e.addInfo("Can not get workflow model graph with name: " + modelName);
071            throw e;
072        }
073    }
074
075}