001/*
002 * (C) Copyright 2006-2008 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 *     bstefanescu
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.core.rest;
023
024import javax.servlet.http.HttpServletRequest;
025import javax.ws.rs.DELETE;
026import javax.ws.rs.GET;
027import javax.ws.rs.POST;
028import javax.ws.rs.PUT;
029import javax.ws.rs.Path;
030import javax.ws.rs.PathParam;
031import javax.ws.rs.Produces;
032import javax.ws.rs.core.Response;
033
034import org.nuxeo.common.utils.URIUtils;
035import org.nuxeo.ecm.core.api.CoreSession;
036import org.nuxeo.ecm.core.api.DocumentModel;
037import org.nuxeo.ecm.core.api.DocumentModelList;
038import org.nuxeo.ecm.core.api.DocumentRef;
039import org.nuxeo.ecm.core.api.NuxeoException;
040import org.nuxeo.ecm.core.api.PathRef;
041import org.nuxeo.ecm.webengine.WebException;
042import org.nuxeo.ecm.webengine.model.Resource;
043import org.nuxeo.ecm.webengine.model.WebObject;
044import org.nuxeo.ecm.webengine.model.exceptions.IllegalParameterException;
045import org.nuxeo.ecm.webengine.model.impl.DefaultObject;
046
047/**
048 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
049 */
050@WebObject(type = "Document")
051@Produces("text/html; charset=UTF-8")
052public class DocumentObject extends DefaultObject {
053
054    protected DocumentModel doc;
055
056    @Override
057    public <A> A getAdapter(Class<A> adapter) {
058        if (adapter == DocumentModel.class) {
059            return adapter.cast(doc);
060        }
061        return super.getAdapter(adapter);
062    }
063
064    @Override
065    public void initialize(Object... args) {
066        assert args != null && args.length == 1;
067        doc = (DocumentModel) args[0];
068    }
069
070    @GET
071    public Object doGet() {
072        return getView("index");
073    }
074
075    // simulate a DELETE using GET
076    @GET
077    @Path("@delete")
078    public Response getDelete() {
079        return doDelete();
080    }
081
082    @GET
083    @Path("@search")
084    public Object search() {
085        final HttpServletRequest request = ctx.getRequest();
086        String query = request.getParameter("query");
087        if (query == null) {
088            String fullText = request.getParameter("fullText");
089            if (fullText == null) {
090                throw new IllegalParameterException("Expecting a query or a fullText parameter");
091            }
092            String orderBy = request.getParameter("orderBy");
093            String orderClause = "";
094            if (orderBy != null) {
095                orderClause = " ORDER BY " + orderBy;
096            }
097            String path;
098            if (doc.isFolder()) {
099                path = doc.getPathAsString();
100            } else {
101                path = doc.getPath().removeLastSegments(1).toString();
102            }
103            query = "SELECT * FROM Document WHERE (ecm:fulltext = \"" + fullText
104                    + "\") AND (ecm:isCheckedInVersion = 0) AND (ecm:path STARTSWITH \"" + path + "\")" + orderClause;
105        }
106        try {
107            DocumentModelList docs = ctx.getCoreSession().query(query);
108            return getView("search").arg("query", query).arg("result", docs);
109        } catch (NuxeoException e) {
110            throw WebException.wrap(e);
111        }
112    }
113
114    @DELETE
115    public Response doDelete() {
116        try {
117            CoreSession session = ctx.getCoreSession();
118            session.removeDocument(doc.getRef());
119            session.save();
120        } catch (NuxeoException e) {
121            throw WebException.wrap("Failed to delete document " + doc.getPathAsString(), e);
122        }
123        if (prev != null) { // show parent ? TODO: add getView(method) to be able to change the view method
124            return redirect(prev.getPath());
125        }
126        return redirect(ctx.getBasePath());
127    }
128
129    @POST
130    public Response doPost() {
131        String name = ctx.getForm().getString("name");
132        DocumentModel newDoc = DocumentHelper.createDocument(ctx, doc, name);
133        String pathSegment = URIUtils.quoteURIPathComponent(newDoc.getName(), true);
134        return redirect(getPath() + '/' + pathSegment);
135    }
136
137    @PUT
138    public Response doPut() {
139        doc = DocumentHelper.updateDocument(ctx, doc);
140        return redirect(getPath());
141    }
142
143    @POST
144    @Path("@put")
145    public Response getPut() {
146        return doPut();
147    }
148
149    // TODO implement HEAD
150    public Object doHead() {
151        return null; // TODO
152    }
153
154    @Path("{path}")
155    public Resource traverse(@PathParam("path") String path) {
156        return newDocument(path);
157    }
158
159    public DocumentObject newDocument(String path) {
160        try {
161            PathRef pathRef = new PathRef(doc.getPath().append(path).toString());
162            DocumentModel doc = ctx.getCoreSession().getDocument(pathRef);
163            return (DocumentObject) ctx.newObject(doc.getType(), doc);
164        } catch (NuxeoException e) {
165            throw WebException.wrap(e);
166        }
167    }
168
169    public DocumentObject newDocument(DocumentRef ref) {
170        try {
171            DocumentModel doc = ctx.getCoreSession().getDocument(ref);
172            return (DocumentObject) ctx.newObject(doc.getType(), doc);
173        } catch (NuxeoException e) {
174            throw WebException.wrap(e);
175        }
176    }
177
178    public DocumentObject newDocument(DocumentModel doc) {
179        return (DocumentObject) ctx.newObject(doc.getType(), doc);
180    }
181
182    public CoreSession getCoreSession() {
183        return ctx.getCoreSession();
184    }
185
186    public DocumentModel getDocument() {
187        return doc;
188    }
189
190    public String getTitle() {
191        try {
192            return doc.getTitle();
193        } catch (NuxeoException e) {
194            throw WebException.wrap(e);
195        }
196    }
197
198}