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.api.lock.LockManager;
022import org.nuxeo.ecm.core.io.ExportedDocument;
023import org.nuxeo.ecm.core.io.ImportExtension;
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 */
032// TODO Is it really used ?
033public class DocumentLockImporter implements ImportExtension {
034
035    @Override
036    public void updateImport(CoreSession session, DocumentModel docModel, ExportedDocument xdoc) throws Exception {
037
038        Element lockInfo = xdoc.getDocument().getRootElement().element("lockInfo");
039        if (lockInfo != null) {
040
041            String createdMS = lockInfo.element("created").getText();
042            String owner = lockInfo.element("owner").getText();
043
044            Calendar created = Calendar.getInstance();
045            created.setTimeInMillis(Long.parseLong(createdMS));
046            Lock lock = new Lock(owner, created);
047
048            getLockManager(session).setLock(docModel.getId(), lock);
049        }
050
051    }
052
053    protected LockManager getLockManager(CoreSession session) {
054        LockManagerService lms = Framework.getService(LockManagerService.class);
055        return lms.getLockManager(session.getRepositoryName());
056    }
057}