Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
R
rest-api-examples
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
snac
rest-api-examples
Commits
6c1f3154
Commit
6c1f3154
authored
Mar 07, 2017
by
Robbie Hott
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixed Eclipse formatting errors
parent
8f62992f
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
140 additions
and
140 deletions
+140
-140
CBWReconcileUI.java
reconciliation/cbw_java_example/src/CBWReconcileUI.java
+32
-32
CBWReconcileWorker.java
reconciliation/cbw_java_example/src/CBWReconcileWorker.java
+108
-108
No files found.
reconciliation/cbw_java_example/src/CBWReconcileUI.java
View file @
6c1f3154
...
...
@@ -35,12 +35,12 @@ import javax.swing.filechooser.FileNameExtensionFilter;
*
*/
public
class
CBWReconcileUI
extends
javax
.
swing
.
JFrame
{
/**
* Serial ID
*/
private
static
final
long
serialVersionUID
=
-
8115653654144568030L
;
/**
* GUI Variables
*/
...
...
@@ -61,20 +61,20 @@ public class CBWReconcileUI extends javax.swing.JFrame {
private
JLabel
toCSVFileLabel
;
private
JLabel
toCSVFileLocationLabel
;
private
JButton
toCSVFileButton
;
/**
* Reconciliation File Variables
*/
private
String
fromCSVFile
;
private
String
toCSVFile
;
{
//Set Look & Feel
try
{
javax
.
swing
.
UIManager
.
setLookAndFeel
(
javax
.
swing
.
UIManager
.
getSystemLookAndFeelClassName
());
}
catch
(
Exception
e
)
{
//
e.printStackTrace();
//
Silently ignoring errors
}
}
...
...
@@ -89,7 +89,7 @@ public class CBWReconcileUI extends javax.swing.JFrame {
//Mac Niceness
System
.
setProperty
(
"com.apple.mrj.application.apple.menu.about.name"
,
"CBW-SNAC Reconciler"
);
SwingUtilities
.
invokeLater
(
new
Runnable
()
{
public
void
run
()
{
CBWReconcileUI
inst
=
new
CBWReconcileUI
();
...
...
@@ -98,7 +98,7 @@ public class CBWReconcileUI extends javax.swing.JFrame {
}
});
}
/**
* Constructor
*
...
...
@@ -108,7 +108,7 @@ public class CBWReconcileUI extends javax.swing.JFrame {
super
();
initGUI
();
}
/**
* Initialize GUI
*
...
...
@@ -116,7 +116,7 @@ public class CBWReconcileUI extends javax.swing.JFrame {
*/
private
void
initGUI
()
{
try
{
BorderLayout
thisLayout
=
new
BorderLayout
();
getContentPane
().
setLayout
(
thisLayout
);
{
...
...
@@ -139,7 +139,7 @@ public class CBWReconcileUI extends javax.swing.JFrame {
bodyPanel
.
add
(
reconcilePanel
);
reconcilePanel
.
setSize
(
700
,
200
);
reconcilePanel
.
setPreferredSize
(
new
java
.
awt
.
Dimension
(
700
,
200
));
// lookup buttons
{
fromCSVFileLabel
=
new
JLabel
();
...
...
@@ -159,13 +159,13 @@ public class CBWReconcileUI extends javax.swing.JFrame {
public
void
actionPerformed
(
ActionEvent
evt
)
{
// Pop up a file chooser for the user to pick a CSV file
JFileChooser
chooser2
=
new
JFileChooser
();
chooser2
.
setDialogTitle
(
"Choose a CSV File."
);
chooser2
.
setFileFilter
(
new
FileNameExtensionFilter
(
"CSV Files"
,
"csv"
));
int
returnVal
=
chooser2
.
showOpenDialog
(
null
);
if
(
returnVal
==
JFileChooser
.
APPROVE_OPTION
)
{
fromCSVFile
=
chooser2
.
getSelectedFile
().
getAbsolutePath
();
chooser2
.
setDialogTitle
(
"Choose a CSV File."
);
chooser2
.
setFileFilter
(
new
FileNameExtensionFilter
(
"CSV Files"
,
"csv"
));
int
returnVal
=
chooser2
.
showOpenDialog
(
null
);
if
(
returnVal
==
JFileChooser
.
APPROVE_OPTION
)
{
fromCSVFile
=
chooser2
.
getSelectedFile
().
getAbsolutePath
();
fromCSVFileLocationLabel
.
setText
(
fromCSVFile
);
}
}
}
});
...
...
@@ -194,18 +194,18 @@ public class CBWReconcileUI extends javax.swing.JFrame {
// Pop up a file chooser for the user to find a directory and
// enter a filename for the destination CSV.
JFileChooser
chooser2
=
new
JFileChooser
();
chooser2
.
setDialogTitle
(
"Choose a Destination CSV File."
);
chooser2
.
setFileFilter
(
new
FileNameExtensionFilter
(
"CSV Files"
,
"csv"
));
int
returnVal
=
chooser2
.
showOpenDialog
(
null
);
if
(
returnVal
==
JFileChooser
.
APPROVE_OPTION
)
{
toCSVFile
=
chooser2
.
getSelectedFile
().
getAbsolutePath
();
chooser2
.
setDialogTitle
(
"Choose a Destination CSV File."
);
chooser2
.
setFileFilter
(
new
FileNameExtensionFilter
(
"CSV Files"
,
"csv"
));
int
returnVal
=
chooser2
.
showOpenDialog
(
null
);
if
(
returnVal
==
JFileChooser
.
APPROVE_OPTION
)
{
toCSVFile
=
chooser2
.
getSelectedFile
().
getAbsolutePath
();
toCSVFileLocationLabel
.
setText
(
toCSVFile
);
}
}
}
});
}
{
jSeparator2
=
new
JSeparator
();
reconcilePanel
.
add
(
jSeparator2
);
...
...
@@ -230,14 +230,14 @@ public class CBWReconcileUI extends javax.swing.JFrame {
@Override
public
void
propertyChange
(
PropertyChangeEvent
evt
)
{
if
(
"progress"
==
evt
.
getPropertyName
())
{
int
progress
=
(
Integer
)
evt
.
getNewValue
();
reconcileProgressBar
.
setValue
(
progress
);
reconcileProgressLabel
.
setText
(
rw
.
getProgressText
());
}
if
(
"progress"
==
evt
.
getPropertyName
())
{
int
progress
=
(
Integer
)
evt
.
getNewValue
();
reconcileProgressBar
.
setValue
(
progress
);
reconcileProgressLabel
.
setText
(
rw
.
getProgressText
());
}
}
});
rw
.
execute
();
rw
.
execute
();
}
catch
(
Exception
e
)
{
// Silently ignoring errors
}
...
...
@@ -267,7 +267,7 @@ public class CBWReconcileUI extends javax.swing.JFrame {
reconcilePanel
.
add
(
reconcileProgressLabel
);
}
}
this
.
setSize
(
700
,
400
);
this
.
setDefaultCloseOperation
(
JFrame
.
EXIT_ON_CLOSE
);
...
...
@@ -275,8 +275,8 @@ public class CBWReconcileUI extends javax.swing.JFrame {
// Silently ignoring errors
}
}
}
reconciliation/cbw_java_example/src/CBWReconcileWorker.java
View file @
6c1f3154
...
...
@@ -37,18 +37,18 @@ import com.opencsv.CSVWriter;
*
*/
public
class
CBWReconcileWorker
extends
SwingWorker
<
Void
,
Void
>
{
/**
* Filenames to use
*/
private
String
fromFile
;
private
String
toFile
;
/**
* Progress of the reconciliation
*/
double
progress
;
/**
* Where the application is currently looking
*/
...
...
@@ -68,7 +68,7 @@ public class CBWReconcileWorker extends SwingWorker<Void, Void> {
progress
=
0.0
;
progressText
=
""
;
}
/**
* Background worker
*
...
...
@@ -76,16 +76,16 @@ public class CBWReconcileWorker extends SwingWorker<Void, Void> {
* then calls the actual reconcile method to perform the reconcilation.
*/
public
Void
doInBackground
()
{
try
{
reconcile
();
}
catch
(
Exception
e
)
{
// Silently ignoring errors
}
return
null
;
}
/**
* Set the progress text
*
...
...
@@ -94,7 +94,7 @@ public class CBWReconcileWorker extends SwingWorker<Void, Void> {
private
void
setProgressText
(
String
text
)
{
progressText
=
text
;
}
/**
* Get progress text
*
...
...
@@ -102,101 +102,101 @@ public class CBWReconcileWorker extends SwingWorker<Void, Void> {
*
* @return The progress text
*/
public
String
getProgressText
()
{
return
progressText
;
}
/**
* Main Reconcile Method
*
* This method performs the heart of the client-side reconciliation process.
*
* @throws Exception
*/
public
String
getProgressText
()
{
return
progressText
;
}
/**
* Main Reconcile Method
*
* This method performs the heart of the client-side reconciliation process.
*
* @throws Exception
*/
private
void
reconcile
()
throws
Exception
{
// Use a pre-packaged reader to read the given CSV file
CSVReader
reader
=
new
CSVReader
(
new
FileReader
(
fromFile
));
List
<
String
[]>
toReconcile
=
reader
.
readAll
();
// Number of lines in the CSV file (minus the header)
int
reconcileCount
=
toReconcile
.
size
()
-
1
;
// Use a pre-packaged writer to write out the CSV file
CSVWriter
writer
=
new
CSVWriter
(
new
FileWriter
(
toFile
));
// Write out header of the CSV
String
[]
headers
=
{
"CBW Name"
,
"CBW ID"
,
"Snac Name"
,
"Snac ARK"
,
"Overall Reconciliation Score"
,
"Elastic Full Name Score"
,
"Elastic Name-Only Score"
,
"Elastic75 Score"
,
"Original Length Score"
,
"Original Length Difference Score"
,
"Entity Type Filter Score"
,
"SNAC Degree Score"
};
// Write out header of the CSV
String
[]
headers
=
{
"CBW Name"
,
"CBW ID"
,
"Snac Name"
,
"Snac ARK"
,
"Overall Reconciliation Score"
,
"Elastic Full Name Score"
,
"Elastic Name-Only Score"
,
"Elastic75 Score"
,
"Original Length Score"
,
"Original Length Difference Score"
,
"Entity Type Filter Score"
,
"SNAC Degree Score"
};
writer
.
writeNext
(
headers
);
// Step through the input data lines
for
(
int
i
=
1
;
i
<
toReconcile
.
size
();
i
++)
{
// Calculate a "percent done" maxing out at 95%
int
percentage
=
(
i
*
95
)
/
reconcileCount
;
// Pull the current line of the CSV as array
String
[]
data
=
toReconcile
.
get
(
i
);
// grab the name components from the CSV file and create a snac-like name heading
String
nameOnly
=
data
[
4
].
trim
()
+
", "
+
data
[
2
].
trim
()
+
" "
+
data
[
3
].
trim
();
nameOnly
=
nameOnly
.
trim
();
if
(
nameOnly
.
endsWith
(
","
))
{
nameOnly
=
nameOnly
.
substring
(
0
,
nameOnly
.
length
()-
1
);
}
// Create the given name
String
name
=
nameOnly
;
// If the input line has a 12th column (dates), then use a regex to grab 4-digit years
// and add them to the name
if
(
data
.
length
>=
13
)
{
String
pattern
=
"[0-9][0-9][0-9][0-9]"
;
Pattern
r
=
Pattern
.
compile
(
pattern
);
Matcher
m
=
r
.
matcher
(
data
[
12
]);
String
date
=
""
;
while
(
m
.
find
())
{
date
+=
data
[
12
].
substring
(
m
.
start
(),
m
.
end
())
+
"-"
;
}
if
(
date
.
length
()
>
0
)
{
date
=
date
.
substring
(
0
,
date
.
length
()-
1
);
name
=
nameOnly
+
", "
+
date
;
}
}
// Update the progress for this run of the reconciliation
String
nameOnly
=
data
[
4
].
trim
()
+
", "
+
data
[
2
].
trim
()
+
" "
+
data
[
3
].
trim
();
nameOnly
=
nameOnly
.
trim
();
if
(
nameOnly
.
endsWith
(
","
))
{
nameOnly
=
nameOnly
.
substring
(
0
,
nameOnly
.
length
()-
1
);
}
// Create the given name
String
name
=
nameOnly
;
// If the input line has a 12th column (dates), then use a regex to grab 4-digit years
// and add them to the name
if
(
data
.
length
>=
13
)
{
String
pattern
=
"[0-9][0-9][0-9][0-9]"
;
Pattern
r
=
Pattern
.
compile
(
pattern
);
Matcher
m
=
r
.
matcher
(
data
[
12
]);
String
date
=
""
;
while
(
m
.
find
())
{
date
+=
data
[
12
].
substring
(
m
.
start
(),
m
.
end
())
+
"-"
;
}
if
(
date
.
length
()
>
0
)
{
date
=
date
.
substring
(
0
,
date
.
length
()-
1
);
name
=
nameOnly
+
", "
+
date
;
}
}
// Update the progress for this run of the reconciliation
setProgress
(
percentage
);
setProgressText
(
name
);
setProgressText
(
name
);
// Create the JSON query string for the SNAC RestAPI
String
query
=
"{"
+
"\"command\" : \"reconcile\","
+
"\"constellation\" : { "
+
"\"dataType\" : \"Constellation\","
+
"\"entityType\" : {"
+
"\"term\" : \"person\""
+
"},"
+
"\"nameEntries\" : ["
+
"{"
+
"\"dataType\" : \"NameEntry\","
+
"\"original\" : \""
+
name
+
"\","
+
"\"preferenceScore\" : 1"
+
"}"
+
"]"
+
"}"
+
"}"
;
"\"term\" : \"person\""
+
"},"
+
"\"nameEntries\" : ["
+
"{"
+
"\"dataType\" : \"NameEntry\","
+
"\"original\" : \""
+
name
+
"\","
+
"\"preferenceScore\" : 1"
+
"}"
+
"]"
+
"}"
+
"}"
;
// Perform connection to SNAC
HttpURLConnection
httpcon
=
(
HttpURLConnection
)
((
new
URL
(
"http://snac-web.iath.virginia.edu:81/"
).
openConnection
()));
...
...
@@ -219,47 +219,47 @@ public class CBWReconcileWorker extends SwingWorker<Void, Void> {
// Close the connection
httpcon
.
disconnect
();
// If reconciliation succeeded, then process the results
if
(
resultObj
.
has
(
"reconciliation"
))
{
for
(
int
j
=
0
;
j
<
resultObj
.
getJSONArray
(
"reconciliation"
).
length
();
j
++)
{
JSONObject
result
=
(
JSONObject
)
resultObj
.
getJSONArray
(
"reconciliation"
).
get
(
j
);
// only grab the first 6 results
if
(
j
>
5
)
break
;
if
(!
result
.
has
(
"vector"
))
continue
;
JSONObject
vector
=
result
.
getJSONObject
(
"vector"
);
// Create the result data to add to the CSV output file
String
[]
output
=
{
name
,
toReconcile
.
get
(
i
)[
0
],
((
JSONObject
)
result
.
getJSONObject
(
"identity"
).
getJSONArray
(
"nameEntries"
).
get
(
0
)).
getString
(
"original"
),
result
.
getJSONObject
(
"identity"
).
getString
(
"ark"
),
String
.
format
(
"%.2f"
,
result
.
getDouble
(
"strength"
)),
vector
.
has
(
"ElasticOriginalNameEntry"
)
?
JSONObject
.
doubleToString
(
vector
.
getDouble
(
"ElasticOriginalNameEntry"
))
:
"0"
,
vector
.
has
(
"ElasticNameOnly"
)
?
JSONObject
.
doubleToString
(
vector
.
getDouble
(
"ElasticNameOnly"
))
:
"0"
,
vector
.
has
(
"ElasticSeventyFive"
)
?
JSONObject
.
doubleToString
(
vector
.
getDouble
(
"ElasticSeventyFive"
))
:
"0"
,
vector
.
has
(
"OriginalLength"
)
?
JSONObject
.
doubleToString
(
vector
.
getDouble
(
"OriginalLength"
))
:
"0"
,
vector
.
has
(
"MultiStage:ElasticNameOnly:OriginalLengthDifference"
)
?
JSONObject
.
doubleToString
(
vector
.
getDouble
(
"MultiStage:ElasticNameOnly:OriginalLengthDifference"
))
:
"0"
,
vector
.
has
(
"MultiStage:ElasticNameOnly:EntityTypeFilter"
)
?
JSONObject
.
doubleToString
(
vector
.
getDouble
(
"MultiStage:ElasticNameOnly:EntityTypeFilter"
))
:
"0"
,
vector
.
has
(
"MultiStage:ElasticNameOnly:SNACDegree"
)
?
JSONObject
.
doubleToString
(
vector
.
getDouble
(
"MultiStage:ElasticNameOnly:SNACDegree"
))
:
"0"
};
// Write the line to the CSV file
writer
.
writeNext
(
output
);
}
}
// only grab the first 6 results
if
(
j
>
5
)
break
;
if
(!
result
.
has
(
"vector"
))
continue
;
JSONObject
vector
=
result
.
getJSONObject
(
"vector"
);
// Create the result data to add to the CSV output file
String
[]
output
=
{
name
,
toReconcile
.
get
(
i
)[
0
],
((
JSONObject
)
result
.
getJSONObject
(
"identity"
).
getJSONArray
(
"nameEntries"
).
get
(
0
)).
getString
(
"original"
),
result
.
getJSONObject
(
"identity"
).
getString
(
"ark"
),
String
.
format
(
"%.2f"
,
result
.
getDouble
(
"strength"
)),
vector
.
has
(
"ElasticOriginalNameEntry"
)
?
JSONObject
.
doubleToString
(
vector
.
getDouble
(
"ElasticOriginalNameEntry"
))
:
"0"
,
vector
.
has
(
"ElasticNameOnly"
)
?
JSONObject
.
doubleToString
(
vector
.
getDouble
(
"ElasticNameOnly"
))
:
"0"
,
vector
.
has
(
"ElasticSeventyFive"
)
?
JSONObject
.
doubleToString
(
vector
.
getDouble
(
"ElasticSeventyFive"
))
:
"0"
,
vector
.
has
(
"OriginalLength"
)
?
JSONObject
.
doubleToString
(
vector
.
getDouble
(
"OriginalLength"
))
:
"0"
,
vector
.
has
(
"MultiStage:ElasticNameOnly:OriginalLengthDifference"
)
?
JSONObject
.
doubleToString
(
vector
.
getDouble
(
"MultiStage:ElasticNameOnly:OriginalLengthDifference"
))
:
"0"
,
vector
.
has
(
"MultiStage:ElasticNameOnly:EntityTypeFilter"
)
?
JSONObject
.
doubleToString
(
vector
.
getDouble
(
"MultiStage:ElasticNameOnly:EntityTypeFilter"
))
:
"0"
,
vector
.
has
(
"MultiStage:ElasticNameOnly:SNACDegree"
)
?
JSONObject
.
doubleToString
(
vector
.
getDouble
(
"MultiStage:ElasticNameOnly:SNACDegree"
))
:
"0"
};
// Write the line to the CSV file
writer
.
writeNext
(
output
);
}
}
}
// Close the CSV Writer
writer
.
close
();
// Close the CSV Reader
reader
.
close
();
// Update the progress to 100%
progressText
=
"DONE!"
;
setProgress
(
100
);
...
...
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