1 package user.jobengine.server.steps;
\r
3 import java.io.OutputStream;
\r
4 import java.net.InetAddress;
\r
6 import java.util.ArrayList;
\r
7 import java.util.Arrays;
\r
8 import java.util.Calendar;
\r
9 import java.util.Date;
\r
10 import java.util.List;
\r
12 import org.apache.commons.lang.StringUtils;
\r
13 import org.apache.commons.net.ftp.FTP;
\r
14 import org.apache.commons.net.ftp.FTPClient;
\r
15 import org.apache.commons.net.ftp.FTPReply;
\r
16 import org.apache.logging.log4j.LogManager;
\r
17 import org.apache.logging.log4j.Logger;
\r
18 import org.apache.logging.log4j.Marker;
\r
20 import com.ibm.nosql.json.api.BasicDBObject;
\r
21 import com.ibm.nosql.json.api.DB;
\r
22 import com.ibm.nosql.json.api.DBCollection;
\r
23 import com.ibm.nosql.json.api.DBCursor;
\r
24 import com.ibm.nosql.json.api.DBObject;
\r
26 import user.commons.CalendarUtils;
\r
27 import user.commons.ListUtils;
\r
28 import user.commons.StoreUri;
\r
29 import user.commons.configuration.SystemConfiguration;
\r
30 import user.commons.nosql.NoSQLUtils;
\r
31 import user.commons.octopus.IOctopusAPI;
\r
32 import user.commons.octopus.OctopusAPI;
\r
33 import user.commons.remotestore.FtpDirectoryLister;
\r
34 import user.commons.remotestore.RemoteStoreProtocol;
\r
35 import user.jobengine.db.IItemManager;
\r
36 import user.jobengine.server.IJobEngine;
\r
37 import user.jobengine.server.IJobRuntime;
\r
38 import user.jobengine.server.steps.shared.EscortFiles;
\r
39 import user.jobengine.server.steps.shared.ItemManagerExtensions;
\r
40 import user.jobengine.server.steps.shared.MetadataType;
\r
41 import user.jobengine.server.steps.shared.MetadataTypeDetector;
\r
43 public class CopyForArchiveNEXIOMaterialsStep extends JobStep {
\r
44 private static final String SCHEDULED_FORMAT = "yyyy.MM.dd HH:mm";
\r
45 private static final Logger logger = LogManager.getLogger("CopyForArchiveNEXIOMaterialsStep");
\r
46 private static final String UTF_8 = "utf-8";
\r
47 private static final String JSON_EXT = ".json";
\r
48 private static final String XML_EXT = ".xml";
\r
49 private static final String DURATION = "duration";
\r
50 private static final String MXFEXT = ".MXF";
\r
51 private static final String NEXIOCLIPS = SystemConfiguration.getInstance().value("services.nexio.collection-name", "nexioclips");
\r
52 private static final String LONGNAMEID = "longnameid";
\r
53 private static final String ARCHIVEDRUNDOWNS = "archivedrundowns";
\r
54 private static final String ID = "id";
\r
55 private static final String MEDIATYPE = "Hír bejátszó";
\r
56 private static String NEXIO_HOST = SystemConfiguration.getInstance().value("services.nexio.host");
\r
57 private OctopusAPI octopusAPI;
\r
58 private IItemManager manager;
\r
61 private FTPClient sourceFtp;
\r
62 private FTPClient targetFtp;
\r
63 private StoreUri sourceUri;
\r
64 private StoreUri targetUri;
\r
65 private int nexioKillDateDays;
\r
66 private String nexioAgency;
\r
67 private Marker systemMarker;
\r
68 private List<String> transferredFileNames = null;
\r
69 private boolean demo = false;
\r
71 private int check(int value, String name) {
\r
73 logger.error(systemMarker, "A folyamat '{}' bemeneti paramétere 0.", name);
\r
74 throw new NullPointerException(String.format("System is not configured properly, missing '%s' input parameter.", name));
\r
79 private String check(String value, String name) {
\r
80 if (value == null) {
\r
81 logger.error(systemMarker, "A folyamat '{}' bemeneti paramétere üres.", name);
\r
82 throw new NullPointerException(String.format("System is not configured properly, missing '%s' input parameter.", name));
\r
87 private void copy(RundownArchive rundownArchive) throws Exception {
\r
88 for (StoryArchive storyArchive : rundownArchive.getStoryArchives()) {
\r
89 for (FileArchive fileArchive : storyArchive.getFileArchives()) {
\r
91 copyFile(fileArchive, rundownArchive, storyArchive);
\r
92 } catch (Exception e) {
\r
93 logger.error(systemMarker, "A '{}' clip archiválása sikertelen. A rendszer üzenete: {}", fileArchive.getFileName(), e.getMessage());
\r
100 private void copyFile(FileArchive fileArchive, RundownArchive rundownArchive, StoryArchive storyArchive) throws Exception {
\r
101 String origFileName = fileArchive.getFileName();
\r
102 String fileName = String.format("%s-%s", origFileName, rundownArchive.getItemHouseId());
\r
103 String videoFileName = fileName + MXFEXT;
\r
105 if (transferredFileNames == null)
\r
106 transferredFileNames = new ArrayList<>();
\r
108 //A mar letezo mozikat nem archivaljuk le ujra, csak a metaadatot
\r
109 long existingMediaId = 0;
\r
110 boolean transferDone = false;
\r
111 if (transferredFileNames.contains(origFileName)) {
\r
112 logger.info(systemMarker, "A '{}' file archiválásra felkészítése egy másik story kapcsán már megtörtént, ezért csak metaadat archiválás szükséges.",
\r
115 transferDone = transferChunk(videoFileName);
\r
117 existingMediaId = ItemManagerExtensions.getExistingRundownMedia(manager, origFileName);
\r
118 if (existingMediaId == 0) {
\r
119 transferredFileNames.add(origFileName);
\r
121 transferDone = transferFile(origFileName + MXFEXT, videoFileName);
\r
122 logger.info(systemMarker, "A '{}' file archiválásra felkészítése sikeres volt.", origFileName);
\r
125 logger.info(systemMarker, "A '{}' file archiválása már megtörtént, ezért csak metaadat archiválás szükséges.", origFileName);
\r
127 transferDone = transferChunk(videoFileName);
\r
131 if (!demo && transferDone) {
\r
132 BasicDBObject metadata = createMetadata(rundownArchive, storyArchive, fileArchive, existingMediaId);
\r
133 transferMetadata(videoFileName, metadata);
\r
134 createSourceKillDateFile(rundownArchive, origFileName);
\r
138 private BasicDBObject createMetadata(RundownArchive rundownArchive, StoryArchive storyArchive, FileArchive fileArchive, long existingMediaId) {
\r
139 BasicDBObject result = new BasicDBObject();
\r
140 result.put("itemHouseId", rundownArchive.getItemHouseId());
\r
141 result.put("itemTitle", rundownArchive.getItemTitle());
\r
142 result.put("itemDescription", rundownArchive.getItemDesc());
\r
143 result.put("userName", "mediacube");
\r
144 result.put("mediaHouseId", storyArchive.getMediaHouseId());
\r
145 result.put("mediaTitle", storyArchive.getMediaTitle());
\r
146 result.put("mediaDescription", storyArchive.getMediaDesc());
\r
147 result.put("mediaType", MEDIATYPE);
\r
148 result.put("duration", fileArchive.getDuration());
\r
149 result.put("existingMediaId", existingMediaId);
\r
153 private void createSourceKillDateFile(RundownArchive rundownArchive, String fileName) throws Exception {
\r
154 logger.info("Create killdate/agency for {}", fileName);
\r
155 OutputStream outStream = null;
\r
157 sourceFtp = ((FtpDirectoryLister) sourceUri.getLister()).connect();
\r
158 Calendar killDate = CalendarUtils.createCalendar(rundownArchive.getScheduleDate());
\r
159 killDate.add(Calendar.DAY_OF_YEAR, nexioKillDateDays);
\r
160 byte[] killDateFile = EscortFiles.createNEXIOKillDateFile(fileName, killDate.getTime(), null, nexioAgency);
\r
161 outStream = sourceFtp.storeFileStream(fileName + XML_EXT);
\r
162 if (outStream == null) {
\r
163 throw new NullPointerException("Can not open: " + fileName + XML_EXT + " Reply:" + sourceFtp.getReplyString());
\r
165 outStream.write(killDateFile);
\r
167 } catch (Exception e) {
\r
168 logger.catching(e);
\r
171 if (outStream != null)
\r
173 sourceUri.cleanUp();
\r
178 public Object[] execute(int nexioPort, String nexioUserName, String nexioPassword, String archiveFtp, String archiveUserName, String archivePassword,
\r
179 int daysBeforeNow, int nexioKillDateDays, String nexioAgency, IJobEngine jobEngine, IJobRuntime jobRuntime) throws Exception {
\r
180 systemMarker = getMarker();
\r
181 logger.debug(systemMarker, "debug test");
\r
182 setAndCheck(nexioPort, nexioUserName, nexioPassword, archiveFtp, archiveUserName, archivePassword, nexioKillDateDays, nexioAgency, jobEngine);
\r
183 octopusAPI = new OctopusAPI();
\r
184 Calendar scheduledDate = Calendar.getInstance();
\r
185 scheduledDate.add(Calendar.DAY_OF_YEAR, -1 * daysBeforeNow);
\r
186 List<DBObject> rundowns = octopusAPI.getRundowns(scheduledDate.getTime());
\r
187 if (rundowns == null) {
\r
188 logger.warn(systemMarker, "Nem található adástükör a {} napra.", CalendarUtils.toDateString(scheduledDate));
\r
192 processRundowns(rundowns);
\r
196 // private String getVersionedFileName(String fileName, String extension) throws Exception {
\r
197 // String result = fileName;
\r
199 // targetFtp = ((FtpDirectoryLister) targetUri.getLister()).connect();
\r
200 // FTPFile[] listFiles = targetFtp.listFiles();
\r
201 // List<String> fileNames = new ArrayList<>();
\r
202 // for (FTPFile ftpFile : listFiles) {
\r
203 // fileNames.add(ftpFile.getName());
\r
205 // while (fileNames.contains(result + extension)) {
\r
209 // } catch (Exception e) {
\r
210 // logger.catching(e);
\r
213 // targetUri.cleanUp();
\r
218 private FileArchive processMosObject(BasicDBObject rundown, BasicDBObject story, BasicDBObject mosObject) throws Exception {
\r
219 String mosID = mosObject.getString(IOctopusAPI.OBJ_ID);
\r
220 if (MetadataTypeDetector.GuessMetadataType(mosID) != MetadataType.OctopusPlaceholder) {
\r
221 logger.info(systemMarker, "Skipping MOS object {}", mosID);
\r
224 DBCollection clips = db.getCollection(NEXIOCLIPS);
\r
225 BasicDBObject clip = (BasicDBObject) clips.findOne(new BasicDBObject(LONGNAMEID, mosID));
\r
226 if (clip == null) {
\r
227 logger.info(systemMarker, "File {} NOT exists", mosID);
\r
229 //throw new Exception(String.format("File NOT exists %s", mosID));
\r
231 logger.debug(systemMarker, "File {} exists", mosID);
\r
233 long duration = NoSQLUtils.asLong(clip, DURATION);
\r
235 if (duration == 0) {
\r
236 logger.info(systemMarker, "File {} exists with 0 frame length", mosID);
\r
239 if (duration == 1) {
\r
240 logger.info(systemMarker, "File {} exists with 1 frame length", mosID);
\r
243 return new FileArchive(mosID, duration);
\r
246 private RundownArchive processRundow(DBObject r) throws Exception {
\r
247 BasicDBObject rundown = (BasicDBObject) r;
\r
248 long rundownID = rundown.getLong(ID);
\r
250 logger.info(systemMarker, "Processing rundown {} {}", rundownID, rundown.getString(IOctopusAPI.NAME));
\r
251 List<DBObject> stories = octopusAPI.getRundownFullStories(rundownID);
\r
252 if (stories == null) {
\r
253 logger.info(systemMarker, "Rundown {} {} is empty", rundownID, rundown.getString(IOctopusAPI.NAME));
\r
256 RundownArchive result = new RundownArchive();
\r
258 long id = NoSQLUtils.asLong(rundown, IOctopusAPI.ID);
\r
261 String name = NoSQLUtils.asString(NoSQLUtils.asDBObject(rundown, IOctopusAPI.RUNDOWN_TYPE), IOctopusAPI.NAME);
\r
262 if (StringUtils.isBlank(name)) {
\r
263 logger.info(systemMarker, "Rundown {} {} type is empty", rundownID, rundown.getString(IOctopusAPI.NAME));
\r
266 String channel = NoSQLUtils.asString(NoSQLUtils.asDBObject(rundown, IOctopusAPI.CHANNEL), IOctopusAPI.NAME);
\r
267 Date scheduledStart = rundown.getDate(IOctopusAPI.SCHEDULED_START);
\r
268 if (scheduledStart == null) {
\r
269 logger.info(systemMarker, "Rundown {} {} schedule is empty", rundownID, rundown.getString(IOctopusAPI.NAME));
\r
272 String start = CalendarUtils.toString(CalendarUtils.createCalendar(scheduledStart), SCHEDULED_FORMAT);
\r
273 result.setScheduleDate(scheduledStart);
\r
274 result.setItemHouseId(String.valueOf(id));
\r
275 result.setItemTitle(String.format("%s %s %s", start, name, channel));
\r
277 logger.info(systemMarker, "Processing stories in rundown {} {}", rundownID, rundown.getString(IOctopusAPI.NAME));
\r
278 for (DBObject s : stories) {
\r
279 StoryArchive storyArchive = processStory(rundown, s);
\r
280 if (storyArchive == null)
\r
282 result.addStoryArchive(storyArchive);
\r
287 private void processRundowns(List<DBObject> rundowns) {
\r
289 //TODO kiveni publikálás előtt
\r
290 //db.getCollection(v).drop();
\r
291 List<BasicDBObject> archivedRundowns = queryArchivedRundowns();
\r
293 logger.info(systemMarker, "Found {} rundowns to archive", archivedRundowns.size());
\r
296 for (DBObject r : rundowns) {
\r
297 BasicDBObject rundown = (BasicDBObject) r;
\r
298 int progress = index * 100 / rundowns.size();
\r
299 setProgress(progress);
\r
300 long rundownID = NoSQLUtils.asLong(rundown, IOctopusAPI.ID);
\r
301 String rundownName = NoSQLUtils.asString(rundown, IOctopusAPI.NAME);
\r
303 BasicDBObject currentRundownID = new BasicDBObject(IOctopusAPI.ID, rundownID);
\r
304 if (!demo && archivedRundowns != null && archivedRundowns.contains(currentRundownID)) {
\r
305 logger.info(systemMarker, "Skipping archived rundown {} {}", rundownID, rundownName);
\r
309 RundownArchive rundownArchive = processRundow(r);
\r
310 if (rundownArchive == null || rundownArchive.isEmpty()) {
\r
312 logger.info(systemMarker, "Skipping rundown {} {}", rundownID, rundownName);
\r
317 logger.info(systemMarker, "Saving rundown {} {}", rundownID, rundownName);
\r
318 copy(rundownArchive);
\r
320 logger.info(systemMarker, "A '{}' tükör {}db bejátszójának archiválása sikeres volt", rundownArchive.getItemTitle(),
\r
321 rundownArchive.getStoryArchives().size());
\r
324 db.getCollection(ARCHIVEDRUNDOWNS).save(currentRundownID);
\r
325 } catch (Exception e) {
\r
326 logger.catching(e);
\r
327 logger.error(systemMarker,
\r
328 String.format("A %s %s tükör archiválása nem lehetséges, mert a annak ellenőrzése hibát jelzett. A rendszer üzenete: %s", rundownID,
\r
329 rundownName, e.getMessage()));
\r
336 private StoryArchive processStory(BasicDBObject rundown, DBObject s) throws Exception {
\r
337 BasicDBObject story = (BasicDBObject) s;
\r
338 String parentStoryID = story.getString(IOctopusAPI.PARENT_STORY_ID);
\r
339 String storyID = story.getString(IOctopusAPI.ID);
\r
341 if (StringUtils.isBlank(parentStoryID)) {
\r
342 logger.warn(systemMarker, "Story parentStoryID is null: {}", story.toPrettyString(null));
\r
345 if ("475048225".equals(storyID))
\r
346 logger.info(systemMarker, "Processing story {}", story.toPrettyString(null));
\r
348 List<BasicDBObject> mosObjects = NoSQLUtils.asList(story, IOctopusAPI.MOS_OBJECTS);
\r
349 if (mosObjects == null) {
\r
350 logger.debug(systemMarker, "No MOS in story {}", storyID);
\r
353 StoryArchive storyArchive = null;
\r
354 for (BasicDBObject mosObject : mosObjects) {
\r
355 FileArchive fileArchive = processMosObject(rundown, story, mosObject);
\r
356 if (fileArchive == null)
\r
358 if (storyArchive == null) {
\r
359 storyArchive = new StoryArchive();
\r
360 storyArchive.setMediaHouseId(parentStoryID);
\r
361 storyArchive.setMediaTitle(story.getString(IOctopusAPI.NAME));
\r
362 storyArchive.setMediaDesc(story.getString(IOctopusAPI.SCRIPT_CONTENT));
\r
365 storyArchive.addFileArchive(fileArchive);
\r
367 return storyArchive;
\r
371 private List<BasicDBObject> queryArchivedRundowns() {
\r
372 List<BasicDBObject> result = null;
\r
373 DBCollection collection = db.getCollection(ARCHIVEDRUNDOWNS);
\r
374 DBCursor find = collection.find(new BasicDBObject(), new BasicDBObject(IOctopusAPI._ID, 0).append(IOctopusAPI.ID, 1));
\r
375 if (find.hasNext())
\r
376 result = ListUtils.cast(find.toArray());
\r
380 private void setAndCheck(int nexioPort, String nexioUserName, String nexioPassword, String archiveFtp, String archiveUserName, String archivePassword,
\r
381 int nexioKillDateDays, String nexioAgency, IJobEngine jobEngine) throws Exception {
\r
382 db = NoSQLUtils.getNoSQLDB();
\r
384 logger.error(systemMarker, "Az NoSQL adatkezelő réteg nem elérhető.");
\r
385 throw new NullPointerException("Internal error, missing NoSQL DB reference.");
\r
388 if (jobEngine == null) {
\r
389 logger.error(systemMarker, "Az folyamatkezelő réteg nem elérhető.");
\r
390 throw new NullPointerException("Internal error, missing JobEngine reference.");
\r
392 manager = jobEngine.getItemManager();
\r
393 if (manager == null) {
\r
394 logger.error(systemMarker, "Az adatbáziskezelő réteg nem elérhető.");
\r
395 throw new NullPointerException("Internal error, missing ItemManager reference.");
\r
398 if (StringUtils.isBlank(NEXIO_HOST)) {
\r
399 logger.error(systemMarker, "A 'nexio.host' rendszer paraméter nem található.");
\r
400 throw new NullPointerException("System is not configured properly, 'jobengine.selenio.address' startup parameter missing.");
\r
402 check(nexioPort, "nexioPort");
\r
403 check(nexioUserName, "nexioUserName");
\r
404 check(nexioPassword, "nexioPassword");
\r
406 check(nexioKillDateDays, "nexioKillDateDays");
\r
407 this.nexioKillDateDays = nexioKillDateDays;
\r
408 check(nexioAgency, "nexioAgency");
\r
409 this.nexioAgency = nexioAgency;
\r
411 sourceUri = manager.createStoreUri(RemoteStoreProtocol.FTP, NEXIO_HOST);
\r
412 sourceUri.setPortNumber(nexioPort);
\r
413 sourceUri.setUserName(nexioUserName);
\r
414 sourceUri.setPassword(nexioPassword);
\r
415 if (sourceUri == null) {
\r
416 logger.error(systemMarker, "A forrás nem elérhető.");
\r
417 throw new NullPointerException("Internal error, missing 'sourceUri'.");
\r
420 check(archiveFtp, "archiveFtp");
\r
421 check(archiveUserName, "archiveUserName");
\r
422 check(archivePassword, "archivePassword");
\r
424 targetUri = manager.createStoreUri(new URI(archiveFtp));
\r
425 targetUri.setUserName(archiveUserName);
\r
426 targetUri.setPassword(archivePassword);
\r
427 if (targetUri == null) {
\r
428 logger.error(systemMarker, "A cél nem elérhető.");
\r
429 throw new NullPointerException("Internal error, missing 'targetUri'.");
\r
434 private boolean transferChunk(String fileName) throws Exception {
\r
435 logger.info("Transfer chunk {}", fileName);
\r
436 OutputStream outStream = null;
\r
438 targetFtp = ((FtpDirectoryLister) targetUri.getLister()).connect();
\r
440 //ha ugyan abban a tukorben szerepel meg 1x a mozi, akkor a chunk felulirja a tenyleges file-t, es az sosem archivalodik
\r
441 String[] names = targetFtp.listNames();
\r
442 if (names != null) {
\r
443 if (Arrays.asList(names).contains(fileName))
\r
447 outStream = targetFtp.storeFileStream(fileName);
\r
448 if (outStream == null) {
\r
449 throw new NullPointerException("Can not open: " + fileName + " Reply:" + targetFtp.getReplyString());
\r
451 } catch (Exception e) {
\r
452 logger.catching(e);
\r
455 if (outStream != null)
\r
457 targetUri.cleanUp();
\r
462 private boolean transferFile(String sourceFileName, String targetFileName) throws Exception {
\r
463 boolean result = true;
\r
465 logger.info("Transfer clip {}", sourceFileName);
\r
467 sourceFtp = ((FtpDirectoryLister) sourceUri.getLister()).connect();
\r
468 targetFtp = ((FtpDirectoryLister) targetUri.getLister()).connect();
\r
469 if (!targetFtp.enterRemotePassiveMode())
\r
470 throw new Exception("!PASV");
\r
472 reply = sourceFtp.port(InetAddress.getByName(targetFtp.getPassiveHost()), targetFtp.getPassivePort());
\r
473 if (!FTPReply.isPositiveCompletion(reply))
\r
474 throw new Exception("PORT parancs válasza: " + sourceFtp.getReplyString());
\r
476 if (!sourceFtp.setFileType(FTP.BINARY_FILE_TYPE))
\r
477 throw new Exception("!SOURCE TYPE");
\r
479 reply = sourceFtp.retr(sourceFileName);
\r
480 if (!FTPReply.isPositivePreliminary(reply))
\r
481 throw new Exception("RETR parancs válasza: " + sourceFtp.getReplyString());
\r
483 if (!targetFtp.setFileType(FTP.BINARY_FILE_TYPE))
\r
484 throw new Exception("!TARGET TYPE");
\r
486 reply = targetFtp.stor(targetFileName);
\r
487 if (!FTPReply.isPositivePreliminary(reply))
\r
488 throw new Exception("STOR parancs válasza: " + sourceFtp.getReplyString());
\r
491 reply = sourceFtp.stat();
\r
492 if (!FTPReply.isPositiveCompletion(reply))
\r
493 throw new Exception("STAT parancs válasza: " + sourceFtp.getReplyString());
\r
495 logger.info("Status: {}", sourceFtp.getReplyString());
\r
496 if (reply == 226) {
\r
499 Thread.sleep(1000);
\r
501 } catch (Exception e) {
\r
502 logger.catching(e);
\r
505 sourceUri.cleanUp();
\r
506 targetUri.cleanUp();
\r
512 private void transferMetadata(String fileName, BasicDBObject metadata) throws Exception {
\r
513 logger.info("Transfer metadata {}", fileName);
\r
514 OutputStream outStream = null;
\r
516 targetFtp = ((FtpDirectoryLister) targetUri.getLister()).connect();
\r
517 if (!targetFtp.changeWorkingDirectory(EscortFiles.STATUSFOLDER)) {
\r
518 targetFtp.makeDirectory(EscortFiles.STATUSFOLDER);
\r
519 if (!targetFtp.changeWorkingDirectory(EscortFiles.STATUSFOLDER))
\r
520 throw new Exception("!STATUSFOLDER");
\r
523 outStream = targetFtp.storeFileStream(fileName + JSON_EXT);
\r
524 if (outStream == null) {
\r
525 throw new NullPointerException("Can not open: " + fileName + JSON_EXT + " Reply:" + targetFtp.getReplyString());
\r
527 outStream.write(metadata.toString().getBytes(UTF_8));
\r
529 //targetFtp.changeToParentDirectory();
\r
530 } catch (Exception e) {
\r
531 logger.catching(e);
\r
534 if (outStream != null)
\r
536 targetUri.cleanUp();
\r