3c53af0f1f68f92cb4ebe5dfe920c496481f1255
[mediacube.git] /
1 package user.jobengine.server.steps;\r
2 \r
3 import java.nio.file.Paths;\r
4 import java.util.ArrayList;\r
5 import java.util.LinkedHashSet;\r
6 import java.util.List;\r
7 \r
8 import org.apache.commons.lang.StringUtils;\r
9 import org.apache.logging.log4j.LogManager;\r
10 import org.apache.logging.log4j.Logger;\r
11 \r
12 import user.commons.DownloadableMedia;\r
13 import user.commons.RemoteFile;\r
14 import user.commons.StoreUri;\r
15 import user.commons.peablebeach.PBMissingMaterialSrc;\r
16 import user.commons.remotestore.RemoteStoreProtocol;\r
17 import user.jobengine.db.Media;\r
18 \r
19 public class PeableBeachMissingMaterialCheckerStep extends JobStep {\r
20         private static final Logger logger = LogManager.getLogger();\r
21         private static final String appendExtension = System.getProperty("missingmaterial.appendextension", ".mxf");\r
22 \r
23         @StepEntry\r
24         public Object[] execute(String escortStoreName, String targetStoreName, String targetProtocol, String primaryEndPoint, String primaryUserName,\r
25                         String primaryPassword, String secondaryEndPoint, String secondaryUserName, String secondaryPassword, int rangeForwardHours) throws Exception {\r
26                 StoreUri escortStoreUri = null;\r
27                 try {\r
28                         escortStoreUri = getManager().getStoreUri(escortStoreName, RemoteStoreProtocol.LOCAL);\r
29                         StoreUri targetStoreUri = getManager().getStoreUri(targetStoreName, Enum.valueOf(RemoteStoreProtocol.class, targetProtocol));\r
30 \r
31                         PBMissingMaterialSrc source = new PBMissingMaterialSrc();\r
32                         source.init(primaryEndPoint, primaryUserName, primaryPassword, secondaryEndPoint, secondaryUserName, secondaryPassword, rangeForwardHours);\r
33 \r
34                         List<RemoteFile> remoteFiles = targetStoreUri.getRemoteFiles();\r
35 \r
36                         List<String> poolContent = new ArrayList<>();\r
37                         remoteFiles.forEach(i -> {\r
38                                 String fileName = i.getName();\r
39                                 String title = fileName.substring(0, fileName.lastIndexOf("."));\r
40                                 poolContent.add(title);\r
41                         });\r
42                         logger.info("Pool contains {} items", poolContent.size());\r
43                         LinkedHashSet<String> fileNames = source.getPossibelMissingMaterialNames(poolContent);\r
44                         logger.info("API returns {} items", fileNames.size());\r
45 \r
46                         for (String fileName : fileNames) {\r
47                                 processRecord(fileName, targetStoreName, targetStoreUri, escortStoreUri);\r
48                         }\r
49                 } catch (Exception e) {\r
50                         logger.error(getSessionMarker(), e.getMessage());\r
51                         throw e;\r
52                 } finally {\r
53                         if (escortStoreUri != null)\r
54                                 escortStoreUri.cleanUp();\r
55                 }\r
56 \r
57                 return null;\r
58         }\r
59 \r
60         private void processRecord(String fileName, String targetStoreName, StoreUri targetStoreUri, StoreUri escortStoreUri) {\r
61                 String title = fileName.substring(0, fileName.lastIndexOf("."));\r
62 \r
63                 Media media = getManager().getMedia(title);\r
64 \r
65                 if (StringUtils.isNotBlank(appendExtension))\r
66                         fileName += appendExtension;\r
67 \r
68                 if (media == null) {\r
69                         logger.error(getSessionMarker(), "File {} not archived yet", fileName);\r
70                         return;\r
71                 }\r
72 \r
73                 DownloadableMedia downloadable = DownloadableMedia.create(title, fileName, media.getModified(), media.getCreated(), media.getLength(), 0L,\r
74                                 targetStoreUri.getId(), media.getId());\r
75                 String escortFileName = targetStoreName + "." + downloadable.getString("fileName");\r
76                 String outputPath = null;\r
77                 try {\r
78                         outputPath = Paths.get(escortStoreUri.toString(true)).toString();\r
79                         EscortFiles.createMetadataIfNotExists(outputPath, escortFileName, downloadable.toPrettyString(""));\r
80                 } catch (Exception e) {\r
81                         logger.error("Can't create escort file {}", Paths.get(outputPath.toString(), escortFileName));\r
82                 }\r
83 \r
84         }\r
85 \r
86 }\r