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