1 package user.jobengine.server.steps;
\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
8 import org.apache.commons.lang.StringUtils;
\r
9 import org.apache.logging.log4j.LogManager;
\r
10 import org.apache.logging.log4j.Logger;
\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
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
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
28 escortStoreUri = getManager().getStoreUri(escortStoreName, RemoteStoreProtocol.LOCAL);
\r
29 StoreUri targetStoreUri = getManager().getStoreUri(targetStoreName, Enum.valueOf(RemoteStoreProtocol.class, targetProtocol));
\r
31 PBMissingMaterialSrc source = new PBMissingMaterialSrc();
\r
32 source.init(primaryEndPoint, primaryUserName, primaryPassword, secondaryEndPoint, secondaryUserName, secondaryPassword, rangeForwardHours);
\r
34 List<RemoteFile> remoteFiles = targetStoreUri.getRemoteFiles();
\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
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
46 for (String fileName : fileNames) {
\r
47 processRecord(fileName, targetStoreName, targetStoreUri, escortStoreUri);
\r
49 } catch (Exception e) {
\r
50 logger.error(getSessionMarker(), e.getMessage());
\r
53 if (escortStoreUri != null)
\r
54 escortStoreUri.cleanUp();
\r
60 private void processRecord(String fileName, String targetStoreName, StoreUri targetStoreUri, StoreUri escortStoreUri) {
\r
61 String title = fileName.substring(0, fileName.lastIndexOf("."));
\r
63 Media media = getManager().getMedia(title);
\r
65 if (StringUtils.isNotBlank(appendExtension))
\r
66 fileName += appendExtension;
\r
68 if (media == null) {
\r
69 logger.error(getSessionMarker(), "File {} not archived yet", fileName);
\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
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