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