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