1 package user.jobengine.server.steps;
\r
3 import java.io.IOException;
\r
4 import java.nio.file.Path;
\r
5 import java.nio.file.Paths;
\r
6 import java.util.Arrays;
\r
7 import java.util.List;
\r
9 import org.apache.commons.lang.StringUtils;
\r
10 import org.apache.commons.net.ftp.FTPClient;
\r
11 import org.apache.logging.log4j.LogManager;
\r
12 import org.apache.logging.log4j.Logger;
\r
14 import com.ibm.nosql.json.api.BasicDBList;
\r
16 import user.commons.DownloadableMedia;
\r
17 import user.commons.StoreUri;
\r
18 import user.commons.harris.HarrisRecord;
\r
19 import user.commons.harris.VICFileParser;
\r
20 import user.commons.remotestore.FtpDirectoryLister;
\r
21 import user.commons.remotestore.RemoteStoreProtocol;
\r
22 import user.jobengine.db.Media;
\r
24 public class HarrisMissingMaterialCheckerStep extends JobStep {
\r
25 private static final Logger logger = LogManager.getLogger(HarrisMissingMaterialCheckerStep.class);
\r
26 private static final String lineFormat = "A TTTTTTTT LLLLLLLLLLL MMMMMMMMMMMMMMMMMMMMMMM X DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD";
\r
27 private static final String validTypeCodes = "SPACE,E";
\r
28 private static final String appendExtension = ".mxf";
\r
31 public Object[] execute(BasicDBList vicFiles, String escortStoreName, String targetStoreName, String targetProtocol) throws Exception {
\r
32 StoreUri escortStoreUri = null;
\r
33 StoreUri targetStoreUri = null;
\r
35 getJobRuntime().setRelated(targetStoreName + " restore");
\r
37 escortStoreUri = getManager().getStoreUri(escortStoreName, RemoteStoreProtocol.LOCAL);
\r
38 targetStoreUri = getManager().getStoreUri(targetStoreName, Enum.valueOf(RemoteStoreProtocol.class, targetProtocol));
\r
39 StoreUri sourceStoreUri = getManager().getStoreUri("TSM", RemoteStoreProtocol.TSM);
\r
40 List<HarrisRecord> records = null;
\r
41 for (Object vicFile : vicFiles) {
\r
42 if (getJobRuntime().isWaitingCancel()) {
\r
47 Path vicFilePath = Paths.get(String.valueOf(vicFile));
\r
49 if (!vicFilePath.toFile().exists()) {
\r
50 logger.warn("File not exists: {}", vicFilePath);
\r
54 logger.info(getMarker(), "Processing {}", vicFilePath);
\r
55 records = processVICFile(records, vicFilePath);
\r
58 if (records != null && !getJobRuntime().isWaitingCancel()) {
\r
59 FTPClient client = ((FtpDirectoryLister) targetStoreUri.getLister()).connect();
\r
60 logger.info(getMarker(), "Reading target file list");
\r
61 List<String> fileNames = Arrays.asList(client.listNames());
\r
62 logger.info(getMarker(), "Target file list reading completed, mediabase size is {}", fileNames.size());
\r
64 for (HarrisRecord record : records) {
\r
66 if (getJobRuntime().isWaitingCancel()) {
\r
71 boolean contains = fileNames.contains(record.getFileName() + ".lxf");
\r
73 logger.info(getMarker(), "File {} already exists on {}", record.getFileName(), targetStoreName);
\r
75 processRecord(record, targetStoreName, sourceStoreUri, targetStoreUri, escortStoreUri);
\r
77 int progress = i * 100 / records.size();
\r
78 setProgress(progress);
\r
82 if (records == null || records.size() == 0)
\r
83 logger.info(getMarker(), "Nothing to do");
\r
85 logger.info(getMarker(), "Completed");
\r
88 } catch (Exception e) {
\r
89 logger.error(e.getMessage());
\r
92 if (escortStoreUri != null)
\r
93 escortStoreUri.cleanUp();
\r
94 if (targetStoreUri != null)
\r
95 targetStoreUri.cleanUp();
\r
101 private boolean processRecord(HarrisRecord record, String targetStoreName, StoreUri sourceStoreUri, StoreUri targetStoreUri, StoreUri escortStoreUri)
\r
104 String fileName = record.getFileName();
\r
105 String outputPath = Paths.get(escortStoreUri.toString(true)).toString();
\r
107 if (StringUtils.isNotBlank(appendExtension))
\r
108 fileName += appendExtension;
\r
110 String escortFileName = targetStoreName + "." + fileName;
\r
112 //ha mar letezik, nem toltjuk fel ujra (a vic fajlbol kezzel torlik valamikor...)
\r
114 if (EscortFiles.isMetadataExists(outputPath, escortFileName)) {
\r
115 logger.debug(getMarker(), "Status file already exists {}", escortFileName);
\r
118 } catch (IOException e1) {
\r
119 logger.info(getMarker(), "Status file check error for {}", fileName);
\r
123 //nincs bent MC-ben
\r
124 Media media = getManager().getMedia(record.getFileName());
\r
125 if (media == null) {
\r
126 logger.error(getSessionMarker(), "File is not available in archive {}", record.getFileName());
\r
130 // if (existsOnNexio(record, targetStoreName, targetStoreUri)) {
\r
131 // logger.info(getMarker(), "File {} already exists on {}", record.getFileName(), targetStoreName);
\r
135 DownloadableMedia downloadable = DownloadableMedia.create(media.getTitle(), fileName, media.getModified(), media.getCreated(), media.getLength(), 0L,
\r
136 sourceStoreUri.getId(), targetStoreUri.getId(), media.getId());
\r
138 //alapbol magasabb (1-99)
\r
139 downloadable.put("priority", 50);
\r
140 downloadable.put("isNexio", true);
\r
143 EscortFiles.createMetadata(outputPath, escortFileName, downloadable.toPrettyString(""));
\r
144 logger.info(getMarker(), "Status file created {}", escortFileName);
\r
145 } catch (Exception e) {
\r
146 logger.error("Can't create escort file {}. System message is: {}", escortFileName, e.getMessage());
\r
151 private List<HarrisRecord> processVICFile(List<HarrisRecord> records, Path vicFilePath) {
\r
152 VICFileParser parser = null;
\r
154 parser = new VICFileParser(vicFilePath, lineFormat, validTypeCodes);
\r
156 List<HarrisRecord> currentRecords = parser.getRecords();
\r
157 if (records == null)
\r
158 records = currentRecords;
\r
160 if (currentRecords != null)
\r
161 records.addAll(currentRecords);
\r
164 } catch (Exception e) {
\r
165 logger.error(e.getMessage());
\r