001/* 002 * (C) Copyright 2013-2019 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 * Thomas Roger 018 */ 019package org.nuxeo.ecm.platform.picture; 020 021import static org.nuxeo.ecm.core.api.CoreSession.ALLOW_VERSION_WRITE; 022import static org.nuxeo.ecm.platform.picture.listener.PictureViewsGenerationListener.DISABLE_PICTURE_VIEWS_GENERATION_LISTENER; 023 024import java.io.IOException; 025 026import org.nuxeo.ecm.core.api.Blob; 027import org.nuxeo.ecm.core.api.DocumentModel; 028import org.nuxeo.ecm.core.api.DocumentNotFoundException; 029import org.nuxeo.ecm.core.api.IdRef; 030import org.nuxeo.ecm.core.api.model.Property; 031import org.nuxeo.ecm.core.api.versioning.VersioningService; 032import org.nuxeo.ecm.core.event.Event; 033import org.nuxeo.ecm.core.event.EventService; 034import org.nuxeo.ecm.core.event.impl.DocumentEventContext; 035import org.nuxeo.ecm.core.work.AbstractWork; 036import org.nuxeo.ecm.platform.picture.api.adapters.PictureResourceAdapter; 037import org.nuxeo.ecm.platform.picture.recompute.RecomputeViewsAction; 038import org.nuxeo.runtime.api.Framework; 039 040/** 041 * Work generating the different picture views for a Picture. 042 * 043 * @since 5.7 044 * @deprecated since 11.1 use {@link RecomputeViewsAction} instead 045 */ 046@Deprecated 047public class PictureViewsGenerationWork extends AbstractWork { 048 049 private static final long serialVersionUID = 1L; 050 051 public static final String CATEGORY_PICTURE_GENERATION = "pictureViewsGeneration"; 052 053 public static final String PICTURE_VIEWS_GENERATION_DONE_EVENT = "pictureViewsGenerationDone"; 054 055 protected final String xpath; 056 057 public PictureViewsGenerationWork(String repositoryName, String docId, String xpath) { 058 super(repositoryName + ':' + docId + ':' + xpath + ":pictureView"); 059 setDocument(repositoryName, docId); 060 this.xpath = xpath; 061 } 062 063 @Override 064 public String getCategory() { 065 return CATEGORY_PICTURE_GENERATION; 066 } 067 068 @Override 069 public String getTitle() { 070 return "Picture views generation"; 071 } 072 073 @Override 074 public int getRetryCount() { 075 // we could fail to get the doc due to a concurrent delete, so allow to retry 076 return 2; 077 } 078 079 @Override 080 public boolean isIdempotent() { 081 return false; 082 } 083 084 @Override 085 public boolean isGroupJoin() { 086 // This is a GroupJoin work with a trigger that can be used on the last work execution 087 return true; 088 } 089 090 @Override 091 public String getPartitionKey() { 092 return docId; 093 } 094 095 @Override 096 public void onGroupJoinCompletion() { 097 firePictureViewsGenerationDoneEvent(session.getDocument(new IdRef(docId))); 098 } 099 100 @Override 101 public void work() { 102 setProgress(Progress.PROGRESS_INDETERMINATE); 103 setStatus("Extracting"); 104 105 openSystemSession(); 106 if (!session.exists(new IdRef(docId))) { 107 setStatus("Nothing to process"); 108 return; 109 } 110 111 DocumentModel workingDocument = session.getDocument(new IdRef(docId)); 112 Property fileProp = workingDocument.getProperty(xpath); 113 Blob blob = (Blob) fileProp.getValue(); 114 if (blob == null) { 115 // do nothing 116 return; 117 } 118 119 String title = workingDocument.getTitle(); 120 setStatus("Generating views"); 121 try { 122 PictureResourceAdapter picture = workingDocument.getAdapter(PictureResourceAdapter.class); 123 picture.fillPictureViews(blob, blob.getFilename(), title, null); 124 } catch (DocumentNotFoundException e) { 125 // a parent of the document may have been deleted. 126 setStatus("Nothing to process"); 127 return; 128 } catch (IOException e) { 129 throw new RuntimeException(e); 130 } 131 132 if (!session.exists(new IdRef(docId))) { 133 setStatus("Nothing to process"); 134 return; 135 } 136 setStatus("Saving"); 137 if (workingDocument.isVersion()) { 138 workingDocument.putContextData(ALLOW_VERSION_WRITE, Boolean.TRUE); 139 } 140 workingDocument.putContextData("disableNotificationService", Boolean.TRUE); 141 workingDocument.putContextData("disableAuditLogger", Boolean.TRUE); 142 workingDocument.putContextData(VersioningService.DISABLE_AUTO_CHECKOUT, Boolean.TRUE); 143 workingDocument.putContextData(DISABLE_PICTURE_VIEWS_GENERATION_LISTENER, Boolean.TRUE); 144 session.saveDocument(workingDocument); 145 146 setStatus("Done"); 147 } 148 149 /** 150 * Fire a {@code PICTURE_VIEWS_GENERATION_DONE_EVENT} 151 * 152 * @since 5.8 153 */ 154 protected void firePictureViewsGenerationDoneEvent(DocumentModel doc) { 155 DocumentEventContext ctx = new DocumentEventContext(session, session.getPrincipal(), doc); 156 Event event = ctx.newEvent(PICTURE_VIEWS_GENERATION_DONE_EVENT); 157 Framework.getService(EventService.class).fireEvent(event); 158 } 159 160}