public class ArchiveSubtitlesStep extends JobStep {\r
private static final Logger logger = LogManager.getLogger();\r
\r
+ @StepEntry\r
+ public Object[] execute(List<String> files, String syncTarget) {\r
+ int current = 0;\r
+ for (String file : files) {\r
+ Path source = Paths.get(file);\r
+ Path archive = archiveSubtitle(source);\r
+\r
+ if (archive != null) {\r
+ Path sync = Paths.get(syncTarget);\r
+ syncSubtitle(archive, sync);\r
+ }\r
+\r
+ current++;\r
+ int progress = current * 100 / files.size();\r
+ setProgress(progress);\r
+ }\r
+ setProgress(100);\r
+ return null;\r
+ }\r
+\r
+ private Path archiveSubtitle(Path source) {\r
+ Path archiveRoot = Paths.get(source.toString().replace("PB_ORIG", "PB_ARCH")).getParent().getParent();\r
+ String lang = source.getParent().getFileName().toString().toUpperCase();\r
+ // logger.info(getMarker(), "Archive root is {}, lang is {}", archiveRoot,\r
+ // lang);\r
+\r
+ String sourceFileName = source.getFileName().toString();\r
+ Path archive = Paths.get(archiveRoot.toString(), sourceFileName.replace(".stl", "_" + lang + ".stl"));\r
+ boolean isOverride = archive.toFile().exists();\r
+ try {\r
+ ensureFolder(archiveRoot);\r
+ Files.copy(source, archive, StandardCopyOption.REPLACE_EXISTING);\r
+\r
+ boolean isContentEquals = Arrays.equals(Files.readAllBytes(source), Files.readAllBytes(archive));\r
+ logger.info(getMarker(), "Contents equals {}", isContentEquals);\r
+ if (isContentEquals) {\r
+ if (isOverride)\r
+ logger.info(getMarker(), "Successfully replaced {}", archive);\r
+ else\r
+ logger.info(getMarker(), "Successfully archived {}", archive);\r
+\r
+ String doneFileName = sourceFileName.substring(0, sourceFileName.lastIndexOf("."));\r
+ Path done = Paths.get(source.getParent().toString(), doneFileName + "_DONE.stl");\r
+ Files.move(source, done, StandardCopyOption.REPLACE_EXISTING);\r
+ return archive;\r
+ } else\r
+ throw new Exception("Source and target content not equals");\r
+ } catch (Exception e) {\r
+ logger.error(getMarker(), "Error copy {} to {}. System message: {}", source, archive, e.getMessage());\r
+ }\r
+ return null;\r
+ }\r
+\r
+ private void syncSubtitle(Path archive, Path syncTarget) {\r
+ Path sync = Paths.get(syncTarget.toString(), archive.getFileName().toString());\r
+ logger.info(getMarker(), "Sync root {} exists {}", syncTarget, syncTarget.toFile().exists());\r
+\r
+ if (!sync.toFile().exists()) {\r
+ logger.info(getMarker(), "Skipping {} synchronization, target not exists", sync);\r
+ return;\r
+ }\r
+\r
+ logger.info(getMarker(), "Target {} synchronization required", sync);\r
+\r
+ try {\r
+ Files.copy(archive, sync, StandardCopyOption.REPLACE_EXISTING);\r
+ boolean isContentEquals = Arrays.equals(Files.readAllBytes(archive), Files.readAllBytes(sync));\r
+ if (isContentEquals)\r
+ logger.info(getMarker(), "Successfully synchronized {}", archive);\r
+ else\r
+ throw new Exception("Synchronization source and target content not equals");\r
+ } catch (Exception e) {\r
+ logger.error(getMarker(), "Error synchronize {} to {}. System message: {}", archive, sync, e.getMessage());\r
+ }\r
+ }\r
+\r
public void ensureFolder(Path filePath) throws IOException {\r
File folder = filePath.toFile();\r
if (!folder.exists() || !folder.isDirectory()) {\r
}\r
}\r
\r
- //"x:\PB_ORIG\AMC_BLK\BUL\CCEM018865-01_DONE.stl"\r
- //"x:\PB_ARCH\AMC_BLK\CCEM018865-01_BUL.stl"\r
-\r
- @StepEntry\r
- public Object[] execute(List<String> files) {\r
- int current = 0;\r
- for (String file : files) {\r
- Path source = Paths.get(file);\r
- String lang = source.getParent().getFileName().toString().toUpperCase();\r
- Path archiveRoot = Paths.get(file.replace("PB_ORIG", "PB_ARCH/TEST"));\r
- archiveRoot = archiveRoot.getParent().getParent();\r
- //logger.info(getMarker(), "Archive root is {}, lang is {}", archiveRoot, lang);\r
- Path archive = Paths.get(archiveRoot.toString(), source.getFileName().toString().replace(".stl", "_" + lang + ".stl"));\r
- boolean isOverride = archive.toFile().exists();\r
- try {\r
- ensureFolder(archiveRoot);\r
- Files.copy(source, archive, StandardCopyOption.REPLACE_EXISTING);\r
- if (archive.toFile().exists()) {\r
- boolean isContentEquals = Arrays.equals(Files.readAllBytes(source), Files.readAllBytes(archive));\r
- logger.info(getMarker(), "Contents equals {}", isContentEquals);\r
- if (isContentEquals) {\r
- if (isOverride)\r
- logger.info(getMarker(), "Successfully replaced {}", archive);\r
- else\r
- logger.info(getMarker(), "Successfully archived {}", archive);\r
- }\r
- }\r
-\r
- } catch (Exception e) {\r
- logger.error(getMarker(), "Error copy {} to {}. System message: {}", source, archive, e.getMessage());\r
- }\r
- current++;\r
- int progress = current * 100 / files.size();\r
- setProgress(progress);\r
- }\r
- setProgress(100);\r
- return null;\r
- }\r
}\r
package user.jobengine.server.steps;\r
\r
+import java.io.IOException;\r
+import java.nio.file.FileVisitResult;\r
+import java.nio.file.FileVisitor;\r
+import java.nio.file.Files;\r
import java.nio.file.Path;\r
-import java.text.SimpleDateFormat;\r
-import java.util.Calendar;\r
+import java.nio.file.Paths;\r
+import java.nio.file.SimpleFileVisitor;\r
+import java.nio.file.attribute.BasicFileAttributes;\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+import java.util.regex.Matcher;\r
+import java.util.regex.Pattern;\r
\r
import org.apache.logging.log4j.LogManager;\r
import org.apache.logging.log4j.Logger;\r
\r
-import com.ibm.nosql.json.api.BasicDBObject;\r
-\r
-/* DO NOT REMOVE!\r
-import user.jobengine.server.steps.FileSearchFilterOptions;\r
-*/\r
-public class SubtitleFilesCollectorStep extends PathItemsCollectorStep {\r
+public class SubtitleFilesCollectorStep extends JobStep {\r
private static final Logger logger = LogManager.getLogger();\r
\r
- @Override\r
- protected FileSearchFilterOptions createFileFilter(BasicDBObject filter) {\r
- FileSearchFilterOptions result = new FileSearchFilterOptions(filter) {\r
+ @StepEntry\r
+ public Object[] execute(String sourceFolder, String fileNamePattern) throws Exception {\r
+ Path sourcePath = Paths.get(sourceFolder);\r
+ List<String> files = new ArrayList<>();\r
+\r
+ Pattern pattern = Pattern.compile(fileNamePattern, Pattern.CASE_INSENSITIVE);\r
+\r
+ FileVisitor<Path> visitor = new SimpleFileVisitor<Path>() {\r
+ @Override\r
+ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {\r
+ if (!file.getFileName().toString().contains("_DONE")) {\r
+ Matcher matcher = pattern.matcher(file.getFileName().toString());\r
+ if (matcher.find())\r
+ files.add(file.toString());\r
+ }\r
+ return FileVisitResult.CONTINUE;\r
+ }\r
+\r
@Override\r
- public boolean acceptFile(Path file) {\r
- return super.acceptFile(file) && !file.getFileName().toString().contains("_DONE");\r
+ public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {\r
+ return FileVisitResult.CONTINUE;\r
}\r
+\r
};\r
- return result;\r
- }\r
\r
- @Override\r
- @StepEntry\r
- public Object[] execute(String sourceFolder, BasicDBObject filter) throws Exception {\r
- return super.execute(sourceFolder, filter);\r
+ try {\r
+ Files.walkFileTree(sourcePath, visitor);\r
+ } catch (Exception e) {\r
+ logger.error(getMarker(), "Error processing '{}'. System message: {}", sourcePath, e.getMessage());\r
+ logger.catching(e);\r
+ throw e;\r
+ } finally {\r
+ }\r
+\r
+ logger.info(getMarker(), "Found {} files", files.size());\r
+ return new Object[] { files };\r
}\r
+\r
}\r