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 java.util.List;
025
026import javax.ws.rs.GET;
027import javax.ws.rs.POST;
028import javax.ws.rs.Path;
029import javax.ws.rs.PathParam;
030
031import org.nuxeo.ecm.core.api.DocumentModel;
032import org.nuxeo.ecm.core.api.NuxeoException;
033import org.nuxeo.ecm.core.api.VersionModel;
034import org.nuxeo.ecm.webengine.WebException;
035import org.nuxeo.ecm.webengine.model.WebAdapter;
036import org.nuxeo.ecm.webengine.model.exceptions.WebResourceNotFoundException;
037import org.nuxeo.ecm.webengine.model.impl.DefaultAdapter;
038
039/**
040 * Version Service - manage document versions TODO not yet implemented
041 * <p>
042 * Accepts the following methods:
043 * <ul>
044 * <li>GET - get the last document version
045 * <li>DELETE - delete a version
046 * <li>POST - create a new version
047 * </ul>
048 *
049 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
050 */
051@WebAdapter(name = "versions", type = "VersionService", targetType = "Document", targetFacets = { "Versionable" })
052public class VersionService extends DefaultAdapter {
053
054    @GET
055    public Object doGet() {
056        return getTarget().getView("versions");
057    }
058
059    @Path("last")
060    public DocumentObject getLastVersion() {
061        try {
062            DocumentObject dobj = (DocumentObject) getTarget();
063            DocumentModel doc = dobj.getDocument();
064            DocumentModel v = dobj.getCoreSession().getLastDocumentVersion(doc.getRef());
065            if (v != null) {
066                return dobj.newDocument(v);
067            }
068        } catch (NuxeoException e) {
069            throw WebException.wrap(e);
070        }
071        throw new WebResourceNotFoundException("No version found for "
072                + ((DocumentObject) getTarget()).getDocument().getPath());
073    }
074
075    @Path("{label}")
076    public DocumentObject getVersion(@PathParam("label") String label) {
077        try {
078            DocumentObject dobj = (DocumentObject) getTarget();
079            DocumentModel doc = dobj.getDocument();
080            List<VersionModel> versions = dobj.getCoreSession().getVersionsForDocument(doc.getRef());
081            for (VersionModel v : versions) {
082                if (label.equals(v.getLabel())) {
083                    return dobj.newDocument(dobj.getCoreSession().getDocumentWithVersion(doc.getRef(), v));
084                }
085            }
086        } catch (NuxeoException e) {
087            throw WebException.wrap(e);
088        }
089        throw new WebResourceNotFoundException("No such version " + label + " for document" + getTarget().getPath());
090    }
091
092    @POST
093    public Object doPost() {
094        return null;
095    }
096
097}