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