001/*
002 * (C) Copyright 2015-2016 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 *     <a href="mailto:grenard@nuxeo.com">Guillaume Renard</a>
018 *
019 */
020package org.nuxeo.ecm.liveconnect.update.worker;
021
022import java.util.List;
023import java.util.stream.Collectors;
024
025import org.apache.commons.logging.Log;
026import org.apache.commons.logging.LogFactory;
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.core.api.IdRef;
029import org.nuxeo.ecm.core.blob.BlobManager;
030import org.nuxeo.ecm.core.work.AbstractWork;
031import org.nuxeo.ecm.liveconnect.update.BatchUpdateBlobProvider;
032import org.nuxeo.runtime.api.Framework;
033
034public class BlobProviderDocumentsUpdateWork extends AbstractWork {
035
036    private static final Log log = LogFactory.getLog(BlobProviderDocumentsUpdateWork.class);
037
038    private static final long serialVersionUID = 1L;
039
040    protected static final String TITLE = "Live Connect Update Documents Work";
041
042    public static final String CATEGORY = "blobProviderDocumentsUpdate";
043
044    protected String providerName;
045
046    public BlobProviderDocumentsUpdateWork(final String id, final String providerName) {
047        super(id);
048        this.providerName = providerName;
049    }
050
051    @Override
052    public String getCategory() {
053        return CATEGORY;
054    }
055
056    @Override
057    public String getTitle() {
058        return TITLE;
059    }
060
061    @Override
062    public void work() {
063        BatchUpdateBlobProvider blobProvider = (BatchUpdateBlobProvider) Framework.getService(
064                BlobManager.class).getBlobProvider(providerName);
065        setStatus("Updating");
066        if (session == null) {
067            openSystemSession();
068        }
069        final List<DocumentModel> results = docIds.stream().map(IdRef::new).map(session::getDocument)
070                .collect(Collectors.toList());
071        log.trace("Updating");
072        List<DocumentModel> changedDocuments = blobProvider.checkChangesAndUpdateBlob(results);
073        if (changedDocuments != null) {
074            for (DocumentModel doc : changedDocuments) {
075                session.saveDocument(doc);
076            }
077        }
078        log.trace("Updating done");
079        setStatus("Done");
080    }
081
082}