001/* 002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * bstefanescu 016 * 017 * $Id$ 018 */ 019 020package org.nuxeo.ecm.core.rest; 021 022import java.util.List; 023 024import javax.ws.rs.GET; 025import javax.ws.rs.POST; 026import javax.ws.rs.Path; 027import javax.ws.rs.PathParam; 028 029import org.nuxeo.ecm.core.api.DocumentModel; 030import org.nuxeo.ecm.core.api.NuxeoException; 031import org.nuxeo.ecm.core.api.VersionModel; 032import org.nuxeo.ecm.webengine.WebException; 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 try { 060 DocumentObject dobj = (DocumentObject) getTarget(); 061 DocumentModel doc = dobj.getDocument(); 062 DocumentModel v = dobj.getCoreSession().getLastDocumentVersion(doc.getRef()); 063 if (v != null) { 064 return dobj.newDocument(v); 065 } 066 } catch (NuxeoException e) { 067 throw WebException.wrap(e); 068 } 069 throw new WebResourceNotFoundException("No version found for " 070 + ((DocumentObject) getTarget()).getDocument().getPath()); 071 } 072 073 @Path("{label}") 074 public DocumentObject getVersion(@PathParam("label") String label) { 075 try { 076 DocumentObject dobj = (DocumentObject) getTarget(); 077 DocumentModel doc = dobj.getDocument(); 078 List<VersionModel> versions = dobj.getCoreSession().getVersionsForDocument(doc.getRef()); 079 for (VersionModel v : versions) { 080 if (label.equals(v.getLabel())) { 081 return dobj.newDocument(dobj.getCoreSession().getDocumentWithVersion(doc.getRef(), v)); 082 } 083 } 084 } catch (NuxeoException e) { 085 throw WebException.wrap(e); 086 } 087 throw new WebResourceNotFoundException("No such version " + label + " for document" + getTarget().getPath()); 088 } 089 090 @POST 091 public Object doPost() { 092 return null; 093 } 094 095}