Commit ff5e7399 by Amelin Konstantin

Change sorting logic and refactor code.

parent a573894d
...@@ -3,7 +3,7 @@ import com.typesafe.config.{Config, ConfigFactory} ...@@ -3,7 +3,7 @@ import com.typesafe.config.{Config, ConfigFactory}
import scala.xml._ import scala.xml._
import java.nio.file.{Files, Paths, StandardOpenOption} import java.nio.file.{Files, Paths, StandardOpenOption}
import java.io.{File, FileInputStream, InputStream} import java.io.{File, FileInputStream, InputStream}
import java.util.zip.ZipFile import java.util.zip.{ZipEntry, ZipFile}
import java.time.LocalDateTime import java.time.LocalDateTime
import scala.util.{Failure, Success, Try} import scala.util.{Failure, Success, Try}
...@@ -35,74 +35,102 @@ object Main { ...@@ -35,74 +35,102 @@ object Main {
.filter { .filter {
!_.isDirectory !_.isDirectory
} }
.sortBy {
_.lastModified
}
val start = LocalDateTime.now().toString val start = LocalDateTime.now.toString
val successSourcesName = s"${args(0)}/${start}_success" val sourceNameValidated = s"${args(0)}/${start}_validated"
val successSources = new File(successSourcesName) val sourceValidated = new File(sourceNameValidated)
val errorSourcesName = s"${args(0)}/${start}_error" val sourceNameNotValidated = s"${args(0)}/${start}_not_validated"
val errorSources = new File(errorSourcesName) val sourceNotValidated = new File(sourceNameNotValidated)
val sourceNameError = s"${args(0)}/${start}_error"
if (successSources.mkdir() && errorSources.mkdir()) { val sourceError = new File(sourceNameError)
for (f <- files) { val outputNameValidated = s"${args(1)}/${start}_validated"
val fSourceName = f.getName val outputValidated = new File(outputNameValidated)
val fSourceNameArray = fSourceName.split('.') val outputNameNotValidated = s"${args(1)}/${start}_not_validated"
val fSourceNameExtension = fSourceNameArray.last.toLowerCase val outputNotValidated = new File(outputNameNotValidated)
val now = LocalDateTime.now().toString
if (sourceValidated.mkdir
if (fSourceNameExtension == "zip") Try { && sourceNotValidated.mkdir
val fZip = new ZipFile(f) && sourceError.mkdir
val fZipEntries = fZip.entries && outputValidated.mkdir
&& outputNotValidated.mkdir) {
while (fZipEntries.hasMoreElements) {
val e = fZipEntries.nextElement val sourceArray = {
if (!e.isDirectory) { for (f <- files) yield {
val fZipEntryName = e.getName val name = f.getName
val nowForZip = LocalDateTime.now().toString val ext = name.split('.').last.toLowerCase
Try { if (ext == "zip") {
convert( Try {
fZip.getInputStream(e), val zip = new ZipFile(f)
s"${fSourceName}_$fZipEntryName",
s"Converted_${fSourceName}_$fZipEntryName", zip.stream.toArray
nsdXmlValid, .map {
rtsXmlValid, z => z.asInstanceOf[ZipEntry]
p) }
} match { .map {
case Success((oXml, ti)) => e =>
XML.save(Paths.get(args(1), s"Converted_${fSourceName}_$fZipEntryName").toString, oXml, xmlDecl = true) FileEntry(
logString += s"$nowForZip\n$ti" s"${name}_${e.getName}",
Files.copy(fZip.getInputStream(e), Paths.get(successSourcesName, s"${fSourceName}_$fZipEntryName")) e.getLastModifiedTime.toMillis,
case Failure(ex) => Some(zip.getInputStream(e)),
logString += s"'$nowForZip\nImpossible to convert '${fSourceName}_$fZipEntryName': ${ex.getMessage}\n\n" zip.getInputStream(e),
Files.copy(fZip.getInputStream(e), Paths.get(errorSourcesName, s"${fSourceName}_$fZipEntryName")) None)
} }
} match {
case Success(v) =>
v
case Failure(ex) =>
Array(FileEntry(name, f.lastModified, None, new FileInputStream(f), Some(ex.getMessage)))
} }
} } else
} match { Array(FileEntry(name, f.lastModified, Some(new FileInputStream(f)), new FileInputStream(f), None))
case Success(_) =>
case Failure(ex) => logString += s"'$now\nImpossible to convert '$fSourceName': ${ex.getMessage}\n\n"
} }
else Try { }
convert( .flatten
new FileInputStream(f), .sortWith { (a, b) => (a.modified == b.modified && a.name <= b.name) || (a.modified < b.modified) }
s"$fSourceName",
s"Converted_$fSourceName", for (f <- sourceArray) {
nsdXmlValid, val now = LocalDateTime.now.toString
rtsXmlValid,
p) f.msg match {
} match { case Some(msg) =>
case Success((oXml, ti)) => logString += s"$now\nImpossible to convert '${f.name}': $msg\n\n"
XML.save(Paths.get(args(1), s"Converted_$fSourceName").toString, oXml, xmlDecl = true) Files.copy(f.streamMove, Paths.get(sourceNameError, f.name))
logString += s"$now\n$ti"
Files.copy(f.toPath, Paths.get(successSourcesName, f.getName)) case None =>
case Failure(ex) => Try {
logString += s"'$now\nImpossible to convert '$fSourceName': ${ex.getMessage}\n\n" convert(
Files.copy(f.toPath, Paths.get(errorSourcesName, f.getName)) f.streamConvert.get,
f.name,
s"converted_${f.name}",
nsdXmlValid,
rtsXmlValid,
p)
} match {
case Success((oXml, ti)) if ti.contains("isn`t wellformed or invalid") =>
XML.save(
Paths.get(outputNameNotValidated, s"converted_${f.name}").toString,
oXml,
xmlDecl = true
)
logString += s"$now\n$ti"
Files.copy(f.streamMove, Paths.get(sourceNameNotValidated, f.name))
case Success((oXml, ti)) =>
XML.save(
Paths.get(outputNameValidated, s"converted_${f.name}").toString,
oXml,
xmlDecl = true
)
logString += s"$now\n$ti"
Files.copy(f.streamMove, Paths.get(sourceNameValidated, f.name))
case Failure(ex) =>
logString += s"$now\nImpossible to convert '${f.name}': ${ex.getMessage}\n\n"
Files.copy(f.streamMove, Paths.get(sourceNameError, f.name))
}
} }
} }
...@@ -115,18 +143,22 @@ object Main { ...@@ -115,18 +143,22 @@ object Main {
StandardOpenOption.CREATE, StandardOpenOption.CREATE,
StandardOpenOption.TRUNCATE_EXISTING) StandardOpenOption.TRUNCATE_EXISTING)
if (successSources.listFiles.nonEmpty) if (sourceValidated.listFiles.nonEmpty)
Files.copy(Paths.get(args(0), logFileName), Paths.get(successSourcesName, logFileName)) Files.copy(Paths.get(args(0), logFileName), Paths.get(sourceNameValidated, logFileName))
else else
successSources.delete sourceValidated.delete
if (sourceNotValidated.listFiles.isEmpty) sourceNotValidated.delete
if (sourceError.listFiles.isEmpty) sourceError.delete
if (outputValidated.listFiles.isEmpty) outputValidated.delete
if (outputNotValidated.listFiles.isEmpty) outputNotValidated.delete
if (errorSources.listFiles.isEmpty) errorSources.delete
} match { } match {
case Success(_) => println(s"Converting has been finished. $logFileName located in ${args(0)}") 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}") case Failure(ex) => println(s"Error. Converting hasn`t been finished: ${ex.getMessage}")
} }
} else } else
println(s"Error. Converting hasn`t been finished: unable to create source moving directory.") println(s"Error. Converting hasn`t been finished: unable to create moving directories.")
} }
} }
...@@ -211,4 +243,12 @@ object Main { ...@@ -211,4 +243,12 @@ object Main {
(outputXml, template.info(s"$msgCheckSource\n$msgCheckOutput") + "\n\n") (outputXml, template.info(s"$msgCheckSource\n$msgCheckOutput") + "\n\n")
} }
case class FileEntry(
name: String,
modified: Long,
streamConvert: Option[InputStream],
streamMove: InputStream,
msg: Option[String]
)
} }
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