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 javax.ws.rs.DELETE;
023import javax.ws.rs.GET;
024import javax.ws.rs.POST;
025
026import org.joda.time.DateTime;
027import org.joda.time.format.ISODateTimeFormat;
028import org.nuxeo.ecm.core.api.DocumentModel;
029import org.nuxeo.ecm.core.api.Lock;
030import org.nuxeo.ecm.core.api.NuxeoException;
031import org.nuxeo.ecm.webengine.WebException;
032import org.nuxeo.ecm.webengine.model.WebAdapter;
033import org.nuxeo.ecm.webengine.model.impl.DefaultAdapter;
034
035/**
036 * Lock Service - manages locks on documents.
037 * <p>
038 * Accepts the following methods:
039 * <ul>
040 * <li>GET - get the Lock Owner if any
041 * <li>POST - Lock the document using current login information as the lock owner
042 * <li>DELETE - Delete the lock
043 * </ul>
044 *
045 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
046 */
047@WebAdapter(name = "lock", type = "LockService", targetType = "Document")
048public class LockService extends DefaultAdapter {
049
050    @GET
051    public Object doGet() {
052        try {
053            DocumentModel doc = getTarget().getAdapter(DocumentModel.class);
054            Lock lock = ctx.getCoreSession().getLockInfo(doc.getRef());
055            return lock.getOwner() + '/' + ISODateTimeFormat.dateTime().print(new DateTime(lock.getCreated()));
056        } catch (NuxeoException e) {
057            throw WebException.wrap("Failed to get lock on document", e);
058        }
059    }
060
061    @DELETE
062    public Object removeLock() {
063        try {
064            DocumentModel doc = getTarget().getAdapter(DocumentModel.class);
065            ctx.getCoreSession().removeLock(doc.getRef());
066            doc.refresh();
067            return null; // TODO
068        } catch (NuxeoException e) {
069            throw WebException.wrap("Failed to unlock document", e);
070        }
071    }
072
073    @POST
074    public Object doPost() {
075        try {
076            DocumentModel doc = getTarget().getAdapter(DocumentModel.class);
077            ctx.getCoreSession().setLock(doc.getRef());
078            doc.refresh();
079            return null; // TODO
080        } catch (NuxeoException e) {
081            throw WebException.wrap("Failed to lock document", e);
082        }
083    }
084
085}