Commit 1755b013 by Amelin Konstantin

Add source moving logic.

parent 5745ddcd
......@@ -2,7 +2,7 @@ import com.typesafe.config.{Config, ConfigFactory}
import scala.xml._
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.time.LocalDateTime
......@@ -30,69 +30,104 @@ object Main {
val rtsXmlValid = XMLValid(getClass.getResource("/rts/fpml-recordkeeping-merged-schema.xsd"))
var logString = ""
for (v <- Files.walk(Paths.get(args(0))).toArray; f = v.asInstanceOf[Path]; if !Files.isDirectory(f)) {
val fSourceName= f.getFileName.toString
val fSourceNameArray = fSourceName.split('.')
val fSourceNameBase = fSourceNameArray.head
val fSourceNameExtension = fSourceNameArray.last.toLowerCase
val now = LocalDateTime.now().toString
if (fSourceNameExtension == "zip") Try {
val fZip = new ZipFile(f.toFile)
val fZipEntries = fZip.entries
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"${fSourceNameBase}_$fZipEntryName",
s"Converted_${fSourceNameBase}_$fZipEntryName",
nsdXmlValid,
rtsXmlValid,
p)
} match {
case Success((oXml, ti)) =>
XML.save(Paths.get(args(1), s"Converted_${fSourceNameBase}_$fZipEntryName").toString, oXml, xmlDecl = true)
logString += s"$nowForZip\n$ti"
case Failure(ex) => logString += s"'$nowForZip\nImpossible to convert '${fSourceNameBase}_$fZipEntryName': ${ex.getMessage}\n\n"
val files = new File(args(0))
.listFiles
.filter {
!_.isDirectory
}
.sortBy {
_.lastModified
}
val start = LocalDateTime.now().toString
val successSourcesName = s"./${start}_success"
val successSources = new File(successSourcesName)
val errorSourcesName = s"./${start}_error"
val errorSources = new File(errorSourcesName)
if (successSources.mkdir() && errorSources.mkdir()) {
for (f <- files) {
val fSourceName = f.getName
val fSourceNameArray = fSourceName.split('.')
val fSourceNameBase = fSourceNameArray.head
val fSourceNameExtension = fSourceNameArray.last.toLowerCase
val now = LocalDateTime.now().toString
if (fSourceNameExtension == "zip") Try {
val fZip = new ZipFile(f)
val fZipEntries = fZip.entries
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 {
case Success(_) =>
case Failure(ex) => logString += s"'$now\nImpossible to convert '$fSourceName': ${ex.getMessage}\n\n"
}
else Try {
convert(
new FileInputStream(f.toFile),
s"$fSourceNameBase",
s"Converted_$fSourceName",
nsdXmlValid,
rtsXmlValid,
p)
} match {
case Success((oXml,ti)) =>
else Try {
convert(
new FileInputStream(f),
s"$fSourceNameBase",
s"Converted_$fSourceName",
nsdXmlValid,
rtsXmlValid,
p)
} match {
case Success((oXml, ti)) =>
XML.save(Paths.get(args(1), s"Converted_$fSourceName").toString, oXml, xmlDecl = true)
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}")
}
}
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}")
}
} else
println(s"Error. Converting hasn`t been finished: unable to create source moving directory.")
}
}
......
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