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 *     Anahide Tchertchian
018 */
019package org.nuxeo.ecm.platform.forms.layout.export;
020
021import java.util.Collections;
022import java.util.List;
023
024import javax.servlet.http.HttpServletRequest;
025import javax.ws.rs.GET;
026import javax.ws.rs.Path;
027import javax.ws.rs.PathParam;
028import javax.ws.rs.QueryParam;
029import javax.ws.rs.core.Context;
030import javax.ws.rs.core.Response;
031import javax.ws.rs.core.UriInfo;
032
033import org.nuxeo.ecm.platform.forms.layout.api.LayoutTypeDefinition;
034import org.nuxeo.ecm.platform.forms.layout.api.impl.LayoutTypeDefinitionComparator;
035import org.nuxeo.ecm.platform.forms.layout.api.service.LayoutStore;
036import org.nuxeo.ecm.webengine.model.exceptions.WebResourceNotFoundException;
037import org.nuxeo.ecm.webengine.model.view.TemplateView;
038import org.nuxeo.runtime.api.Framework;
039
040/**
041 * @since 6.0
042 */
043public class LayoutTypeResource {
044
045    protected final String category;
046
047    protected LayoutStore service;
048
049    protected final List<LayoutTypeDefinition> layoutTypes;
050
051    public LayoutTypeResource(String category) {
052        this.category = category;
053        service = Framework.getService(LayoutStore.class);
054        layoutTypes = service.getLayoutTypeDefinitions(category);
055        // sort so that order is deterministic
056        Collections.sort(layoutTypes, new LayoutTypeDefinitionComparator());
057    }
058
059    @GET
060    @Path("layoutTypes")
061    public Object getLayoutTypeDefinitions(@Context HttpServletRequest request, @QueryParam("all") Boolean all) {
062        return new LayoutTypeDefinitions(layoutTypes);
063    }
064
065    @GET
066    @Path("layoutType/{name}")
067    public Object getLayoutTypeDefinition(@Context HttpServletRequest request, @PathParam("name") String name) {
068        LayoutTypeDefinition def = service.getLayoutTypeDefinition(category, name);
069        if (def != null) {
070            return def;
071        } else {
072            return Response.status(401).build();
073        }
074    }
075
076    public TemplateView getTemplate(@Context UriInfo uriInfo) {
077        return getTemplate("layout-types.ftl", uriInfo);
078    }
079
080    protected TemplateView getTemplate(String name, UriInfo uriInfo) {
081        String baseURL = uriInfo.getAbsolutePath().toString();
082        if (!baseURL.endsWith("/")) {
083            baseURL += "/";
084        }
085        TemplateView tv = new TemplateView(this, name);
086        tv.arg("layoutTypeCategory", category);
087        tv.arg("layoutTypes", layoutTypes);
088        tv.arg("baseURL", baseURL);
089        return tv;
090    }
091
092    @GET
093    public Object doGet(@QueryParam("layoutType") String layoutTypeName, @Context UriInfo uriInfo) {
094        if (layoutTypeName == null) {
095            return getTemplate(uriInfo);
096        } else {
097            LayoutTypeDefinition wType = service.getLayoutTypeDefinition(category, layoutTypeName);
098            if (wType == null) {
099                throw new WebResourceNotFoundException("No layout type found with name: " + layoutTypeName);
100            }
101            TemplateView tpl = getTemplate(uriInfo);
102            tpl.arg("layoutType", wType);
103            return tpl;
104        }
105    }
106
107}