001/*
002 * (C) Copyright 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 *     Thierry Delprat
018 */
019package org.nuxeo.template.jaxrs;
020
021import javax.ws.rs.GET;
022import javax.ws.rs.Path;
023import javax.ws.rs.PathParam;
024import javax.ws.rs.Produces;
025
026import org.nuxeo.ecm.core.api.Blob;
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.core.api.DocumentRef;
029import org.nuxeo.ecm.core.api.IdRef;
030import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
031import org.nuxeo.ecm.webengine.model.WebObject;
032import org.nuxeo.ecm.webengine.model.impl.DefaultObject;
033import org.nuxeo.template.api.adapters.TemplateBasedDocument;
034import org.nuxeo.template.api.adapters.TemplateSourceDocument;
035
036@WebObject(type = "templateBasedResource")
037public class TemplateBasedResource extends DefaultObject {
038
039    protected String uuid;
040
041    public TemplateBasedResource() {
042        uuid = null;
043    }
044
045    public TemplateBasedResource(String uuid) {
046        this.uuid = uuid;
047    }
048
049    @Override
050    protected void initialize(Object... args) {
051        if (args != null && args.length > 0) {
052            this.uuid = (String) args[0];
053        }
054    }
055
056    protected TemplateBasedDocument resolve() {
057        if (uuid != null) {
058            IdRef idRef = new IdRef(uuid);
059            DocumentModel doc = getContext().getCoreSession().getDocument(idRef);
060            return doc.getAdapter(TemplateBasedDocument.class);
061        } else {
062            return null;
063        }
064    }
065
066    @GET
067    public Object get() {
068        TemplateBasedDocument tmpl = resolve();
069        if (tmpl == null) {
070            return "";
071        } else {
072            return tmpl.getAdaptedDoc().getId() + " - " + tmpl.getAdaptedDoc().getTitle();
073        }
074    }
075
076    @GET
077    @Path("templates")
078    public String getAssociatedTemplates() {
079
080        IdRef idRef = new IdRef(uuid);
081        DocumentModel doc = getContext().getCoreSession().getDocument(idRef);
082        TemplateBasedDocument tmpl = doc.getAdapter(TemplateBasedDocument.class);
083        if (tmpl == null) {
084            return "This document is not template based";
085        }
086        return tmpl.getTemplateNames().toString();
087    }
088
089    @Path("template/{name}")
090    public Object getAssociatedTemplate(@PathParam(value = "name") String name) {
091
092        IdRef idRef = new IdRef(uuid);
093        DocumentModel doc = getContext().getCoreSession().getDocument(idRef);
094        TemplateBasedDocument tmpl = doc.getAdapter(TemplateBasedDocument.class);
095        if (tmpl == null) {
096            return "This document is not template based";
097        }
098
099        DocumentRef sourceRef = tmpl.getSourceTemplateDocRef(name);
100        if (sourceRef != null) {
101            return getContext().newObject("templateResource", sourceRef.toString());
102        }
103        return null;
104    }
105
106    @GET
107    @Path("resource/{templateName}/{resourceName}")
108    @Produces("*/*")
109    public Blob getResource(@PathParam(value = "templateName") String templateName,
110            @PathParam(value = "resourceName") String resourceName) {
111
112        TemplateBasedDocument tmpl = resolve();
113
114        BlobHolder bh = tmpl.getAdaptedDoc().getAdapter(BlobHolder.class);
115        if (bh != null) {
116            for (Blob blob : bh.getBlobs()) {
117                if (resourceName.equalsIgnoreCase(blob.getFilename())) {
118                    return blob;
119                }
120            }
121        }
122
123        TemplateSourceDocument template = tmpl.getSourceTemplate(templateName);
124        if (template != null) {
125            return TemplateResource.getResource(template, resourceName);
126        }
127        return null;
128    }
129}