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