001/*
002 * (C) Copyright 2015 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-2.1.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 *     vpasquier <vpasquier@nuxeo.com>
016 */
017package org.nuxeo.binary.metadata.internals;
018
019import java.util.LinkedList;
020
021import org.nuxeo.binary.metadata.api.BinaryMetadataService;
022import org.nuxeo.ecm.core.api.DocumentModel;
023import org.nuxeo.ecm.core.api.IdRef;
024import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
025import org.nuxeo.ecm.core.work.AbstractWork;
026import org.nuxeo.runtime.api.Framework;
027
028/**
029 * Work handling binary metadata updates.
030 *
031 * @since 7.2
032 */
033public class BinaryMetadataWork extends AbstractWork {
034
035    private static final long serialVersionUID = 1L;
036
037    private static final String BINARY_METADATA_WORK = "binary_metadata_work";
038
039    public static final String BINARY_METADATA_WORK_TITLE = "Binary Metadata Update Worker";
040
041    protected final LinkedList<MetadataMappingDescriptor> mappingDescriptors;
042
043    protected final DocumentEventContext docCtx;
044
045    protected final String docId;
046
047    public BinaryMetadataWork(String repositoryName, String docId,
048            LinkedList<MetadataMappingDescriptor> mappingDescriptors, DocumentEventContext docCtx) {
049        super("BinaryMetadataUpdate|docId=" + docId);
050        setDocument(repositoryName, docId);
051        this.mappingDescriptors = mappingDescriptors;
052        this.docCtx = docCtx;
053        this.docId = docId;
054    }
055
056    @Override
057    public String getCategory() {
058        return BINARY_METADATA_WORK;
059    }
060
061    @Override
062    public String getTitle() {
063        return BINARY_METADATA_WORK_TITLE;
064    }
065
066    @Override
067    public void work() {
068        setProgress(Progress.PROGRESS_INDETERMINATE);
069        setStatus("Updating Metadata");
070        initSession();
071        if (!session.exists(new IdRef(docId))) {
072            setStatus("Nothing to process");
073            return;
074        }
075        BinaryMetadataService binaryMetadataService = Framework.getLocalService(BinaryMetadataService.class);
076        DocumentModel workingDocument = session.getDocument(new IdRef(docId));
077        binaryMetadataService.handleUpdate(mappingDescriptors, workingDocument, docCtx);
078        setStatus("Metadata Update Done");
079    }
080
081}