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    protected void initialize(Object... args) {
050        if (args != null && args.length > 0) {
051            this.uuid = (String) args[0];
052        }
053    }
054
055    protected TemplateBasedDocument resolve() {
056        if (uuid != null) {
057            IdRef idRef = new IdRef(uuid);
058            DocumentModel doc = getContext().getCoreSession().getDocument(idRef);
059            return doc.getAdapter(TemplateBasedDocument.class);
060        } else {
061            return null;
062        }
063    }
064
065    @GET
066    public Object get() {
067        TemplateBasedDocument tmpl = resolve();
068        if (tmpl == null) {
069            return "";
070        } else {
071            StringBuffer sb = new StringBuffer();
072            sb.append(tmpl.getAdaptedDoc().getId() + " - " + tmpl.getAdaptedDoc().getTitle());
073            return sb.toString();
074        }
075    }
076
077    @GET
078    @Path("templates")
079    public String getAssociatedTemplates() {
080
081        IdRef idRef = new IdRef(uuid);
082        DocumentModel doc = getContext().getCoreSession().getDocument(idRef);
083        TemplateBasedDocument tmpl = doc.getAdapter(TemplateBasedDocument.class);
084        if (tmpl == null) {
085            return "This document is not template based";
086        }
087        return tmpl.getTemplateNames().toString();
088    }
089
090    @Path("template/{name}")
091    public Object getAssociatedTemplate(@PathParam(value = "name") String name) {
092
093        IdRef idRef = new IdRef(uuid);
094        DocumentModel doc = getContext().getCoreSession().getDocument(idRef);
095        TemplateBasedDocument tmpl = doc.getAdapter(TemplateBasedDocument.class);
096        if (tmpl == null) {
097            return "This document is not template based";
098        }
099
100        DocumentRef sourceRef = tmpl.getSourceTemplateDocRef(name);
101        if (sourceRef != null) {
102            return getContext().newObject("templateResource", sourceRef.toString());
103        }
104        return null;
105    }
106
107    @GET
108    @Path("resource/{templateName}/{resourceName}")
109    @Produces("*/*")
110    public Blob getResource(@PathParam(value = "templateName") String templateName,
111            @PathParam(value = "resourceName") String resourceName) {
112
113        TemplateBasedDocument tmpl = resolve();
114
115        BlobHolder bh = tmpl.getAdaptedDoc().getAdapter(BlobHolder.class);
116        if (bh != null) {
117            for (Blob blob : bh.getBlobs()) {
118                if (resourceName.equalsIgnoreCase(blob.getFilename())) {
119                    return blob;
120                }
121            }
122        }
123
124        TemplateSourceDocument template = tmpl.getSourceTemplate(templateName);
125        if (template != null) {
126            return TemplateResource.getResource(template, resourceName);
127        }
128        return null;
129    }
130}