001/*
002 * Copyright (c) 2015 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Nuxeo - initial API and implementation
011 *
012 */
013package org.nuxeo.ecm.core.io.impl.extensions;
014
015import java.util.Calendar;
016
017import org.dom4j.Element;
018import org.nuxeo.ecm.core.api.CoreSession;
019import org.nuxeo.ecm.core.api.DocumentModel;
020import org.nuxeo.ecm.core.api.Lock;
021import org.nuxeo.ecm.core.io.ExportedDocument;
022import org.nuxeo.ecm.core.io.ImportExtension;
023import org.nuxeo.ecm.core.model.LockManager;
024import org.nuxeo.ecm.core.storage.lock.LockManagerService;
025import org.nuxeo.runtime.api.Framework;
026
027/**
028 * Allows to import Document Locks
029 *
030 * @since 7.4
031 */
032public class DocumentLockImporter implements ImportExtension {
033
034    @Override
035    public void updateImport(CoreSession session, DocumentModel docModel, ExportedDocument xdoc) throws Exception {
036
037        Element lockInfo = xdoc.getDocument().getRootElement().element("lockInfo");
038        if (lockInfo != null) {
039
040            String createdMS = lockInfo.element("created").getText();
041            String owner = lockInfo.element("owner").getText();
042
043            Calendar created = Calendar.getInstance();
044            created.setTimeInMillis(Long.parseLong(createdMS));
045            Lock lock = new Lock(owner, created);
046
047            getLockManager(session).setLock(docModel.getId(), lock);
048        }
049
050    }
051
052    protected LockManager getLockManager(CoreSession session) {
053        LockManagerService lms = Framework.getService(LockManagerService.class);
054        return lms.getLockManager(session.getRepositoryName());
055    }
056}