Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
SPB_Exchange_Repo
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Amelin Konstantin
SPB_Exchange_Repo
Commits
a0df5972
Commit
a0df5972
authored
Jul 10, 2019
by
Amelin Konstantin
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'dev'
# Conflicts: # src/main/scala/Main.scala
parents
6a3ff7ca
67dd98ea
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
147 additions
and
108 deletions
+147
-108
src/main/scala/Main.scala
+147
-108
No files found.
src/main/scala/Main.scala
View file @
a0df5972
import
com.typesafe.config.
{
Config
,
ConfigFactory
}
import
scala.xml._
import
java.nio.file.
{
Files
,
Paths
,
StandardOpenOption
}
import
java.io.
{
File
,
FileInputStream
,
InputStream
}
import
java.nio.file.
{
Files
,
Path
,
Paths
,
StandardOpenOption
}
import
java.io.InputStream
import
java.nio.file.attribute.FileTime
import
java.util.zip.
{
ZipEntry
,
ZipFile
}
import
java.time.LocalDateTime
...
...
@@ -30,138 +31,176 @@ object Main {
val
rtsXmlValid
=
XMLValid
(
getClass
.
getResource
(
"/rts/fpml-recordkeeping-merged-schema.xsd"
))
var
logString
=
""
val
files
=
new
File
(
args
(
0
))
.
listFiles
val
paths
=
Files
.
list
(
Paths
.
get
(
args
(
0
)))
.
filter
{
!
_
.
isDirectory
p
=>
!
Files
.
isDirectory
(
p
)
}
.
toArray
.
map
{
_
.
asInstanceOf
[
Path
]
}
val
start
=
LocalDateTime
.
now
.
toString
.
replace
(
":"
,
"."
)
val
sourceNameValidated
=
s
"${args(0)}/${start}_validated"
val
sourceValidated
=
new
File
(
sourceNameValidated
)
val
sourceValidated
=
Try
{
Files
.
createDirectory
(
Paths
.
get
(
sourceNameValidated
))
}
val
sourceNameNotValidated
=
s
"${args(0)}/${start}_not_validated"
val
sourceNotValidated
=
new
File
(
sourceNameNotValidated
)
val
sourceNotValidated
=
Try
{
Files
.
createDirectory
(
Paths
.
get
(
sourceNameNotValidated
))
}
val
sourceNameError
=
s
"${args(0)}/${start}_error"
val
sourceError
=
new
File
(
sourceNameError
)
val
sourceError
=
Try
{
Files
.
createDirectory
(
Paths
.
get
(
sourceNameError
))
}
val
outputNameValidated
=
s
"${args(1)}/${start}_validated"
val
outputValidated
=
new
File
(
outputNameValidated
)
val
outputValidated
=
Try
{
Files
.
createDirectory
(
Paths
.
get
(
outputNameValidated
))
}
val
outputNameNotValidated
=
s
"${args(1)}/${start}_not_validated"
val
outputNotValidated
=
new
File
(
outputNameNotValidated
)
if
(
sourceValidated
.
mkdir
&&
sourceNotValidated
.
mkdir
&&
sourceError
.
mkdir
&&
outputValidated
.
mkdir
&&
outputNotValidated
.
mkdir
)
{
val
sourceArray
=
{
for
(
f
<-
files
)
yield
{
val
name
=
f
.
getName
val
ext
=
name
.
split
(
'.'
).
last
.
toLowerCase
if
(
ext
==
"zip"
)
Try
{
val
zip
=
new
ZipFile
(
f
)
zip
.
stream
.
toArray
.
map
{
z
=>
z
.
asInstanceOf
[
ZipEntry
]
}
.
map
{
e
=>
FileEntry
(
s
"${name}_${e.getName}"
,
//e.getName,
e
.
getLastModifiedTime
.
toMillis
,
Some
(
zip
.
getInputStream
(
e
)),
zip
.
getInputStream
(
e
),
None
)
}
}
match
{
case
Success
(
v
)
=>
v
case
Failure
(
ex
)
=>
Array
(
FileEntry
(
name
,
f
.
lastModified
,
None
,
new
FileInputStream
(
f
),
Some
(
ex
.
toString
)))
}
else
Array
(
FileEntry
(
name
,
f
.
lastModified
,
Some
(
new
FileInputStream
(
f
)),
new
FileInputStream
(
f
),
None
))
}
}
.
flatten
.
sortWith
{
(
a
,
b
)
=>
(
a
.
modified
==
b
.
modified
&&
a
.
name
<=
b
.
name
)
||
(
a
.
modified
<
b
.
modified
)
}
val
outputNotValidated
=
Try
{
Files
.
createDirectory
(
Paths
.
get
(
outputNameNotValidated
))
}
val
createDirRes
=
for
{
sv
<-
sourceValidated
snv
<-
sourceNotValidated
se
<-
sourceError
ov
<-
outputValidated
onv
<-
outputNotValidated
}
yield
onv
Try
{
for
(
f
<-
sourceArray
)
{
val
now
=
LocalDateTime
.
now
.
toString
createDirRes
match
{
case
Success
(
_
)
=>
f
.
msg
match
{
case
Some
(
msg
)
=>
logString
+=
s
"'$now'\nImpossible to process '${f.name}': $msg\n\n"
Files
.
copy
(
f
.
streamMove
,
Paths
.
get
(
sourceNameError
,
f
.
name
))
val
sourceArray
=
{
for
(
p
<-
paths
)
yield
{
val
name
=
p
.
getFileName
.
toString
val
ext
=
name
.
split
(
'.'
).
last
.
toLowerCase
case
None
=>
if
(
ext
==
"zip"
)
Try
{
convert
(
f
.
streamConvert
.
get
,
f
.
name
,
s
"converted_${f.name}"
,
nsdXmlValid
,
rtsXmlValid
,
p
)
val
zip
=
new
ZipFile
(
p
.
toString
)
zip
.
stream
.
toArray
.
map
{
z
=>
z
.
asInstanceOf
[
ZipEntry
]
}
.
map
{
e
=>
FileEntry
(
s
"${name}_${e.getName}"
,
//e.getName,
e
.
getLastModifiedTime
,
Some
(
zip
.
getInputStream
(
e
)),
zip
.
getInputStream
(
e
),
None
)
}
}
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
Success
(
v
)
=>
v
case
Failure
(
ex
)
=>
logString
+=
s
"'$now'\nImpossible to convert '${f.name}': ${ex.toString}\n\n"
Files
.
copy
(
f
.
streamMove
,
Paths
.
get
(
sourceNameError
,
f
.
name
))
Array
(
FileEntry
(
name
,
Files
.
getLastModifiedTime
(
p
),
None
,
Files
.
newInputStream
(
p
),
Some
(
ex
.
toString
))
)
}
else
Array
(
FileEntry
(
name
,
Files
.
getLastModifiedTime
(
p
),
Some
(
Files
.
newInputStream
(
p
)),
Files
.
newInputStream
(
p
),
None
)
)
}
}
.
flatten
.
sortWith
{
(
a
,
b
)
=>
(
a
.
modified
.
compareTo
(
b
.
modified
)
==
0
&&
a
.
name
<=
b
.
name
)
||
(
a
.
modified
.
compareTo
(
b
.
modified
)
<
0
)
}
Try
{
for
(
f
<-
sourceArray
)
{
val
now
=
LocalDateTime
.
now
.
toString
f
.
msg
match
{
case
Some
(
msg
)
=>
logString
+=
s
"'$now'\nImpossible to process '${f.name}': $msg\n\n"
Files
.
copy
(
f
.
streamMove
,
Paths
.
get
(
sourceNameError
,
f
.
name
))
case
None
=>
Try
{
convert
(
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.toString}\n\n"
Files
.
copy
(
f
.
streamMove
,
Paths
.
get
(
sourceNameError
,
f
.
name
))
}
}
}
files
.
foreach
{
_
.
delete
}
sourceArray
.
foreach
{
f
=>
f
.
streamMove
.
close
()
if
(
f
.
streamConvert
.
nonEmpty
)
f
.
streamConvert
.
get
.
close
()
}
Files
.
write
(
Paths
.
get
(
args
(
0
),
logFileName
),
logString
.
getBytes
(
"utf-8"
),
StandardOpenOption
.
CREATE
,
StandardOpenOption
.
TRUNCATE_EXISTING
)
paths
.
foreach
{
p
=>
Files
.
delete
(
p
)
}
if
(
sourceValidated
.
listFiles
.
nonEmpty
)
Files
.
copy
(
Paths
.
get
(
args
(
0
),
logFileName
),
Paths
.
get
(
sourceNameValidated
,
logFileName
))
else
sourceValidated
.
delete
Files
.
write
(
Paths
.
get
(
args
(
0
),
logFileName
),
logString
.
getBytes
(
"utf-8"
),
StandardOpenOption
.
CREATE
,
StandardOpenOption
.
TRUNCATE_EXISTING
)
if
(
sourceNotValidated
.
listFiles
.
isEmpty
)
sourceNotValidated
.
delete
if
(
sourceError
.
listFiles
.
isEmpty
)
sourceError
.
delete
if
(
outputValidated
.
listFiles
.
isEmpty
)
outputValidated
.
delet
e
if
(
outputNotValidated
.
listFiles
.
isEmpty
)
outputNotValidated
.
delete
if
(
Files
.
list
(
sourceValidated
.
get
).
toArray
.
nonEmpty
)
Files
.
copy
(
Paths
.
get
(
args
(
0
),
logFileName
),
Paths
.
get
(
sourceNameValidated
,
logFileName
))
els
e
Files
.
delete
(
sourceValidated
.
get
)
}
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.toString}"
)
}
if
(
Files
.
list
(
sourceNotValidated
.
get
).
toArray
.
isEmpty
)
Files
.
delete
(
sourceNotValidated
.
get
)
if
(
Files
.
list
(
sourceError
.
get
).
toArray
.
isEmpty
)
Files
.
delete
(
sourceError
.
get
)
if
(
Files
.
list
(
outputValidated
.
get
).
toArray
.
isEmpty
)
Files
.
delete
(
outputValidated
.
get
)
if
(
Files
.
list
(
outputNotValidated
.
get
).
toArray
.
isEmpty
)
Files
.
delete
(
outputNotValidated
.
get
)
}
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.toString}"
)
}
}
else
println
(
s
"Error. Converting hasn`t been finished: unable to create directories to move files."
)
case
Failure
(
ex
)
=>
println
(
s
"Error. Converting hasn`t been finished: ${ex.toString}"
)
}
}
}
...
...
@@ -250,7 +289,7 @@ object Main {
case
class
FileEntry
(
name
:
String
,
modified
:
Long
,
modified
:
FileTime
,
streamConvert
:
Option
[
InputStream
],
streamMove
:
InputStream
,
msg
:
Option
[
String
]
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment