View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.iview2.events;
20  
21  import java.io.IOException;
22  import java.nio.file.Files;
23  import java.nio.file.Path;
24  import java.util.List;
25  import java.util.Locale;
26  import java.util.Optional;
27  import java.util.stream.Collectors;
28  
29  import org.apache.logging.log4j.LogManager;
30  import org.apache.logging.log4j.Logger;
31  import org.mycore.common.config.MCRConfiguration2;
32  import org.mycore.common.events.MCREvent;
33  import org.mycore.common.events.MCREventHandlerBase;
34  import org.mycore.datamodel.metadata.MCRDerivate;
35  import org.mycore.datamodel.metadata.MCRMetaClassification;
36  import org.mycore.iview2.backend.MCRDefaultTileFileProvider;
37  import org.mycore.iview2.backend.MCRPDFThumbnailJobAction;
38  import org.mycore.iview2.backend.MCRTileInfo;
39  import org.mycore.services.queuedjob.MCRJob;
40  import org.mycore.services.queuedjob.MCRJobQueue;
41  
42  /**
43   * This event handler creates iview2 files for title pages in PDFs which can be
44   * used as thumbnails in IIIF API
45   *
46   * @author Robert Stephan
47   */
48  public class MCRThumbnailForPdfEventHandler extends MCREventHandlerBase {
49  
50      public static final MCRDefaultTileFileProvider TILE_FILE_PROVIDER = new MCRDefaultTileFileProvider();
51  
52      private static final Logger LOGGER = LogManager.getLogger(MCRThumbnailForPdfEventHandler.class);
53  
54      private static final MCRJobQueue PDF_THUMBNAIL_JOB_QUEUE = initializeJobQueue();
55  
56      private static final List<String> DERIVATE_TYPES_FOR_CONTENT = MCRConfiguration2
57          .getOrThrow("MCR.IIIFImage.Iview.ThumbnailForPdfEventHandler.Derivate.Types",
58              MCRConfiguration2::splitValue)
59          .collect(Collectors.toList());
60  
61      private static MCRJobQueue initializeJobQueue() {
62          LOGGER.info("Initializing jobQueue for PDF Thumbnail generation!");
63          return MCRJobQueue.getInstance(MCRPDFThumbnailJobAction.class);
64      }
65  
66      @Override
67      protected void handleDerivateCreated(MCREvent evt, MCRDerivate der) {
68          updateThumbnail(der);
69      }
70  
71      @Override
72      protected void handleDerivateRepaired(MCREvent evt, MCRDerivate der) {
73          updateThumbnail(der);
74      }
75  
76      @Override
77      protected void handleDerivateUpdated(MCREvent evt, MCRDerivate der) {
78          updateThumbnail(der);
79      }
80  
81      @Override
82      protected void handleDerivateDeleted(MCREvent evt, MCRDerivate der) {
83          deleteThumbnail(der);
84      }
85  
86      private void updateThumbnail(MCRDerivate der) {
87          deleteThumbnail(der);
88          if (isQualifyingDerivate(der)) {
89              final MCRJob job = new MCRJob(MCRPDFThumbnailJobAction.class);
90              job.setParameter(MCRPDFThumbnailJobAction.DERIVATE_PARAMETER, der.getId().toString());
91              PDF_THUMBNAIL_JOB_QUEUE.add(job);
92          }
93      }
94  
95      private void deleteThumbnail(MCRDerivate der) {
96          String mainDoc = der.getDerivate().getInternals().getMainDoc();
97          if (mainDoc != null && mainDoc.toLowerCase(Locale.ROOT).endsWith(".pdf")) {
98              MCRTileInfo tileInfo = new MCRTileInfo(der.getId().toString(), mainDoc, null);
99              Optional<Path> oPIview2File = TILE_FILE_PROVIDER.getTileFile(tileInfo);
100             if (oPIview2File.isPresent()) {
101                 try {
102                     Files.deleteIfExists(oPIview2File.get());
103                 } catch (IOException e) {
104                     LOGGER.error(e);
105                 }
106             }
107         }
108     }
109 
110     private boolean isQualifyingDerivate(MCRDerivate der) {
111         for (MCRMetaClassification c : der.getDerivate().getClassifications()) {
112             String classid = c.getClassId() + ":" + c.getCategId();
113             if (DERIVATE_TYPES_FOR_CONTENT.contains(classid)) {
114                 String mainDoc = der.getDerivate().getInternals().getMainDoc();
115                 return (mainDoc != null && mainDoc.toLowerCase(Locale.ROOT).endsWith(".pdf"));
116             }
117         }
118         return false;
119     }
120 }