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