001/* 002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl-2.1.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Anahide Tchertchian 016 */ 017package org.nuxeo.ecm.platform.forms.layout.export; 018 019import java.util.Collections; 020import java.util.List; 021 022import javax.servlet.http.HttpServletRequest; 023import javax.ws.rs.GET; 024import javax.ws.rs.Path; 025import javax.ws.rs.PathParam; 026import javax.ws.rs.QueryParam; 027import javax.ws.rs.core.Context; 028import javax.ws.rs.core.Response; 029import javax.ws.rs.core.UriInfo; 030 031import org.nuxeo.ecm.platform.forms.layout.api.LayoutTypeDefinition; 032import org.nuxeo.ecm.platform.forms.layout.api.impl.LayoutTypeDefinitionComparator; 033import org.nuxeo.ecm.platform.forms.layout.api.service.LayoutStore; 034import org.nuxeo.ecm.webengine.model.exceptions.WebResourceNotFoundException; 035import org.nuxeo.ecm.webengine.model.view.TemplateView; 036import org.nuxeo.runtime.api.Framework; 037 038/** 039 * @since 6.0 040 */ 041public class LayoutTypeResource { 042 043 protected final String category; 044 045 protected LayoutStore service; 046 047 protected final List<LayoutTypeDefinition> layoutTypes; 048 049 public LayoutTypeResource(String category) { 050 this.category = category; 051 service = Framework.getService(LayoutStore.class); 052 layoutTypes = service.getLayoutTypeDefinitions(category); 053 // sort so that order is deterministic 054 Collections.sort(layoutTypes, new LayoutTypeDefinitionComparator()); 055 } 056 057 @GET 058 @Path("layoutTypes") 059 public Object getLayoutTypeDefinitions(@Context HttpServletRequest request, @QueryParam("all") Boolean all) { 060 return new LayoutTypeDefinitions(layoutTypes); 061 } 062 063 @GET 064 @Path("layoutType/{name}") 065 public Object getLayoutTypeDefinition(@Context HttpServletRequest request, @PathParam("name") String name) { 066 LayoutTypeDefinition def = service.getLayoutTypeDefinition(category, name); 067 if (def != null) { 068 return def; 069 } else { 070 return Response.status(401).build(); 071 } 072 } 073 074 public TemplateView getTemplate(@Context UriInfo uriInfo) { 075 return getTemplate("layout-types.ftl", uriInfo); 076 } 077 078 protected TemplateView getTemplate(String name, UriInfo uriInfo) { 079 String baseURL = uriInfo.getAbsolutePath().toString(); 080 if (!baseURL.endsWith("/")) { 081 baseURL += "/"; 082 } 083 TemplateView tv = new TemplateView(this, name); 084 tv.arg("layoutTypeCategory", category); 085 tv.arg("layoutTypes", layoutTypes); 086 tv.arg("baseURL", baseURL); 087 return tv; 088 } 089 090 @GET 091 public Object doGet(@QueryParam("layoutType") String layoutTypeName, @Context UriInfo uriInfo) { 092 if (layoutTypeName == null) { 093 return getTemplate(uriInfo); 094 } else { 095 LayoutTypeDefinition wType = service.getLayoutTypeDefinition(category, layoutTypeName); 096 if (wType == null) { 097 throw new WebResourceNotFoundException("No layout type found with name: " + layoutTypeName); 098 } 099 TemplateView tpl = getTemplate(uriInfo); 100 tpl.arg("layoutType", wType); 101 return tpl; 102 } 103 } 104 105}