001/*
002 * (C) Copyright 2010 Nuxeo SA (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 *     mcedica
016 */
017package org.nuxeo.ecm.platform.routing.core.impl;
018
019import org.nuxeo.ecm.core.api.CoreSession;
020import org.nuxeo.ecm.core.api.DocumentModel;
021import org.nuxeo.ecm.core.api.DocumentRef;
022import org.nuxeo.ecm.core.api.Lock;
023import org.nuxeo.ecm.core.api.NuxeoPrincipal;
024import org.nuxeo.ecm.platform.routing.api.LockableDocumentRoute;
025
026/**
027 * @author <a href="mailto:mcedica@nuxeo.com">Mariana Cedica</a>
028 */
029public class LockableDocumentRouteImpl implements LockableDocumentRoute {
030    protected final DocumentModel doc;
031
032    public LockableDocumentRouteImpl(DocumentModel document) {
033        this.doc = document;
034    }
035
036    private static final long serialVersionUID = 1L;
037
038    @Override
039    public boolean isLocked(CoreSession session) {
040        return session.getLockInfo(doc.getRef()) != null;
041    }
042
043    @Override
044    public boolean isLockedByCurrentUser(CoreSession session) {
045        Lock lockInfo = session.getLockInfo(doc.getRef());
046        if (lockInfo == null) {
047            return false;
048        }
049        String lockOwner = lockInfo.getOwner();
050        NuxeoPrincipal userName = (NuxeoPrincipal) session.getPrincipal();
051        return userName.getName().equals(lockOwner);
052    }
053
054    @Override
055    public void lockDocument(CoreSession session) {
056        session.setLock(doc.getRef());
057    }
058
059    @Override
060    public void unlockDocument(CoreSession session) {
061        DocumentRef ref = doc.getRef();
062        session.removeLock(ref);
063    }
064
065    @Override
066    public String getLockOwner(CoreSession session) {
067        return session.getPrincipal().getName();
068    }
069
070}