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
67dd98ea
Commit
67dd98ea
authored
Jul 10, 2019
by
Amelin Konstantin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor code to nio.file
parent
a244794b
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
72 additions
and
33 deletions
+72
-33
src/main/scala/Main.scala
+72
-33
No files found.
src/main/scala/Main.scala
View file @
67dd98ea
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,42 +31,55 @@ 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
)
val
outputNotValidated
=
Try
{
Files
.
createDirectory
(
Paths
.
get
(
outputNameNotValidated
))
}
if
(
sourceValidated
.
mkdir
&&
sourceNotValidated
.
mkdir
&&
sourceError
.
mkdir
&&
outputValidated
.
mkdir
&&
outputNotValidated
.
mkdir
)
{
val
createDirRes
=
for
{
sv
<-
sourceValidated
snv
<-
sourceNotValidated
se
<-
sourceError
ov
<-
outputValidated
onv
<-
outputNotValidated
}
yield
onv
createDirRes
match
{
case
Success
(
_
)
=>
val
sourceArray
=
{
for
(
f
<-
file
s
)
yield
{
val
name
=
f
.
getName
for
(
p
<-
path
s
)
yield
{
val
name
=
p
.
getFileName
.
toString
val
ext
=
name
.
split
(
'.'
).
last
.
toLowerCase
if
(
ext
==
"zip"
)
Try
{
val
zip
=
new
ZipFile
(
f
)
val
zip
=
new
ZipFile
(
p
.
toString
)
zip
.
stream
.
toArray
zip
.
stream
.
toArray
.
map
{
z
=>
z
.
asInstanceOf
[
ZipEntry
]
}
...
...
@@ -74,7 +88,7 @@ object Main {
FileEntry
(
s
"${name}_${e.getName}"
,
//e.getName,
e
.
getLastModifiedTime
.
toMillis
,
e
.
getLastModifiedTime
,
Some
(
zip
.
getInputStream
(
e
)),
zip
.
getInputStream
(
e
),
None
)
...
...
@@ -83,14 +97,30 @@ object Main {
case
Success
(
v
)
=>
v
case
Failure
(
ex
)
=>
Array
(
FileEntry
(
name
,
f
.
lastModified
,
None
,
new
FileInputStream
(
f
),
Some
(
ex
.
toString
)))
Array
(
FileEntry
(
name
,
Files
.
getLastModifiedTime
(
p
),
None
,
Files
.
newInputStream
(
p
),
Some
(
ex
.
toString
))
)
}
else
Array
(
FileEntry
(
name
,
f
.
lastModified
,
Some
(
new
FileInputStream
(
f
)),
new
FileInputStream
(
f
),
None
))
Array
(
FileEntry
(
name
,
Files
.
getLastModifiedTime
(
p
),
Some
(
Files
.
newInputStream
(
p
)),
Files
.
newInputStream
(
p
),
None
)
)
}
}
.
flatten
.
sortWith
{
(
a
,
b
)
=>
(
a
.
modified
==
b
.
modified
&&
a
.
name
<=
b
.
name
)
||
(
a
.
modified
<
b
.
modified
)
}
.
sortWith
{
(
a
,
b
)
=>
(
a
.
modified
.
compareTo
(
b
.
modified
)
==
0
&&
a
.
name
<=
b
.
name
)
||
(
a
.
modified
.
compareTo
(
b
.
modified
)
<
0
)
}
Try
{
...
...
@@ -137,7 +167,15 @@ object Main {
}
}
//files.foreach { _.delete }
sourceArray
.
foreach
{
f
=>
f
.
streamMove
.
close
()
if
(
f
.
streamConvert
.
nonEmpty
)
f
.
streamConvert
.
get
.
close
()
}
paths
.
foreach
{
p
=>
Files
.
delete
(
p
)
}
Files
.
write
(
Paths
.
get
(
args
(
0
),
logFileName
),
...
...
@@ -145,23 +183,24 @@ object Main {
StandardOpenOption
.
CREATE
,
StandardOpenOption
.
TRUNCATE_EXISTING
)
if
(
sourceValidated
.
listFiles
.
nonEmpty
)
if
(
Files
.
list
(
sourceValidated
.
get
).
toArray
.
nonEmpty
)
Files
.
copy
(
Paths
.
get
(
args
(
0
),
logFileName
),
Paths
.
get
(
sourceNameValidated
,
logFileName
))
else
sourceValidated
.
delete
Files
.
delete
(
sourceValidated
.
get
)
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
(
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