Commit 1755b013 by Amelin Konstantin

Add source moving logic.

parent 5745ddcd
...@@ -2,7 +2,7 @@ import com.typesafe.config.{Config, ConfigFactory} ...@@ -2,7 +2,7 @@ import com.typesafe.config.{Config, ConfigFactory}
import scala.xml._ import scala.xml._
import java.nio.file.{Files, Path, Paths, StandardOpenOption} import java.nio.file.{Files, Path, Paths, StandardOpenOption}
import java.io.{FileInputStream, InputStream} import java.io.{File, FileInputStream, InputStream}
import java.util.zip.ZipFile import java.util.zip.ZipFile
import java.time.LocalDateTime import java.time.LocalDateTime
...@@ -30,69 +30,104 @@ object Main { ...@@ -30,69 +30,104 @@ object Main {
val rtsXmlValid = XMLValid(getClass.getResource("/rts/fpml-recordkeeping-merged-schema.xsd")) val rtsXmlValid = XMLValid(getClass.getResource("/rts/fpml-recordkeeping-merged-schema.xsd"))
var logString = "" var logString = ""
for (v <- Files.walk(Paths.get(args(0))).toArray; f = v.asInstanceOf[Path]; if !Files.isDirectory(f)) { val files = new File(args(0))
val fSourceName= f.getFileName.toString .listFiles
val fSourceNameArray = fSourceName.split('.') .filter {
val fSourceNameBase = fSourceNameArray.head !_.isDirectory
val fSourceNameExtension = fSourceNameArray.last.toLowerCase }
val now = LocalDateTime.now().toString .sortBy {
_.lastModified
if (fSourceNameExtension == "zip") Try { }
val fZip = new ZipFile(f.toFile)
val fZipEntries = fZip.entries val start = LocalDateTime.now().toString
while (fZipEntries.hasMoreElements) { val successSourcesName = s"./${start}_success"
val e = fZipEntries.nextElement val successSources = new File(successSourcesName)
if (!e.isDirectory) { val errorSourcesName = s"./${start}_error"
val fZipEntryName = e.getName val errorSources = new File(errorSourcesName)
val nowForZip = LocalDateTime.now().toString
if (successSources.mkdir() && errorSources.mkdir()) {
Try {
convert( for (f <- files) {
fZip.getInputStream(e), val fSourceName = f.getName
s"${fSourceNameBase}_$fZipEntryName", val fSourceNameArray = fSourceName.split('.')
s"Converted_${fSourceNameBase}_$fZipEntryName", val fSourceNameBase = fSourceNameArray.head
nsdXmlValid, val fSourceNameExtension = fSourceNameArray.last.toLowerCase
rtsXmlValid, val now = LocalDateTime.now().toString
p)
} match { if (fSourceNameExtension == "zip") Try {
case Success((oXml, ti)) => val fZip = new ZipFile(f)
XML.save(Paths.get(args(1), s"Converted_${fSourceNameBase}_$fZipEntryName").toString, oXml, xmlDecl = true) val fZipEntries = fZip.entries
logString += s"$nowForZip\n$ti"
case Failure(ex) => logString += s"'$nowForZip\nImpossible to convert '${fSourceNameBase}_$fZipEntryName': ${ex.getMessage}\n\n" while (fZipEntries.hasMoreElements) {
val e = fZipEntries.nextElement
if (!e.isDirectory) {
val fZipEntryName = e.getName
val nowForZip = LocalDateTime.now().toString
Try {
convert(
fZip.getInputStream(e),
s"${fSourceName}_$fZipEntryName",
s"Converted_${fSourceName}_$fZipEntryName",
nsdXmlValid,
rtsXmlValid,
p)
} match {
case Success((oXml, ti)) =>
XML.save(Paths.get(args(1), s"Converted_${fSourceName}_$fZipEntryName").toString, oXml, xmlDecl = true)
logString += s"$nowForZip\n$ti"
Files.copy(fZip.getInputStream(e), Paths.get(successSourcesName, s"${fSourceName}_$fZipEntryName"))
case Failure(ex) =>
logString += s"'$nowForZip\nImpossible to convert '${fSourceName}_$fZipEntryName': ${ex.getMessage}\n\n"
Files.copy(fZip.getInputStream(e), Paths.get(errorSourcesName, s"${fSourceName}_$fZipEntryName"))
}
} }
} }
} match {
case Success(_) =>
case Failure(ex) => logString += s"'$now\nImpossible to convert '$fSourceName': ${ex.getMessage}\n\n"
} }
} match { else Try {
case Success(_) => convert(
case Failure(ex) => logString += s"'$now\nImpossible to convert '$fSourceName': ${ex.getMessage}\n\n" new FileInputStream(f),
} s"$fSourceNameBase",
else Try { s"Converted_$fSourceName",
convert( nsdXmlValid,
new FileInputStream(f.toFile), rtsXmlValid,
s"$fSourceNameBase", p)
s"Converted_$fSourceName", } match {
nsdXmlValid, case Success((oXml, ti)) =>
rtsXmlValid,
p)
} match {
case Success((oXml,ti)) =>
XML.save(Paths.get(args(1), s"Converted_$fSourceName").toString, oXml, xmlDecl = true) XML.save(Paths.get(args(1), s"Converted_$fSourceName").toString, oXml, xmlDecl = true)
logString += s"$now\n$ti" logString += s"$now\n$ti"
case Failure(ex) => logString += s"'$now\nImpossible to convert '$fSourceName': ${ex.getMessage}\n\n" Files.copy(f.toPath, Paths.get(successSourcesName, f.getName))
case Failure(ex) =>
logString += s"'$now\nImpossible to convert '$fSourceName': ${ex.getMessage}\n\n"
Files.copy(f.toPath, Paths.get(errorSourcesName, f.getName))
}
}
Try {
//files.foreach { _.delete }
Files.write(
Paths.get(args(0), logFileName),
logString.getBytes("utf-8"),
StandardOpenOption.CREATE,
StandardOpenOption.TRUNCATE_EXISTING)
if (successSources.listFiles.nonEmpty)
Files.copy(Paths.get(args(0), logFileName), Paths.get(successSourcesName, logFileName))
else
successSources.delete
if (errorSources.listFiles.isEmpty) errorSources.delete
} match {
case Success(_) => println(s"Converting has been finished. $logFileName located in ${args(0)}")
case Failure(ex) => println(s"Error. Converting hasn`t been finished: ${ex.getMessage}")
} }
} } else
println(s"Error. Converting hasn`t been finished: unable to create source moving directory.")
Try {
Files.write(
Paths.get(args(1), logFileName),
logString.getBytes("utf-8"),
StandardOpenOption.CREATE,
StandardOpenOption.TRUNCATE_EXISTING)
} match {
case Success(_) => println(s"Converting has been finished. $logFileName located in ${args(1)}")
case Failure(ex) => println(s"Error. Converting hasn`t been finished: ${ex.getMessage}")
}
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment