|
|
|
|
@ -1,17 +1,21 @@
|
|
|
|
|
package org.leolo.nrdatad.db.mariadb; |
|
|
|
|
|
|
|
|
|
import org.apache.logging.log4j.LogManager; |
|
|
|
|
import org.apache.logging.log4j.Logger; |
|
|
|
|
import org.jetbrains.annotations.NotNull; |
|
|
|
|
import org.leolo.nrdatad.db.DatabaseManager; |
|
|
|
|
import org.leolo.nrdatad.org.leolo.nrdatad.model.CORPUS; |
|
|
|
|
|
|
|
|
|
import java.sql.Connection; |
|
|
|
|
import java.sql.PreparedStatement; |
|
|
|
|
import java.sql.SQLException; |
|
|
|
|
import java.sql.Statement; |
|
|
|
|
import java.sql.*; |
|
|
|
|
import java.util.Collection; |
|
|
|
|
import java.util.HashSet; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.Vector; |
|
|
|
|
|
|
|
|
|
public class CORPUSDao extends org.leolo.nrdatad.db.CORPUSDao { |
|
|
|
|
|
|
|
|
|
Logger log = LogManager.getLogger(); |
|
|
|
|
|
|
|
|
|
public CORPUSDao(DatabaseManager manager) { |
|
|
|
|
super(manager); |
|
|
|
|
} |
|
|
|
|
@ -64,6 +68,65 @@ public class CORPUSDao extends org.leolo.nrdatad.db.CORPUSDao {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void replaceAll(@NotNull Collection<CORPUS> datas) throws SQLException { |
|
|
|
|
int totalCount = 0, insertCount = 0, updateCount = 0; |
|
|
|
|
HashSet<String> pendingInsert = new HashSet<>(); |
|
|
|
|
try( |
|
|
|
|
Connection conn = getConnection(); |
|
|
|
|
PreparedStatement psIns = conn.prepareStatement( |
|
|
|
|
"INSERT INTO corpus (stanox, uic_code, crs_code, tiploc_code, `desc`, short_desc, nlc_code) " + |
|
|
|
|
"VALUES (?,?,?,?,?,?,?)"); |
|
|
|
|
PreparedStatement psUpd = conn.prepareStatement( |
|
|
|
|
"UPDATE corpus " + |
|
|
|
|
"SET stanox=?, uic_code=?, crs_code=?, tiploc_code=?, `desc`=?, short_desc=? " + |
|
|
|
|
"WHERE nlc_code = ?" |
|
|
|
|
) |
|
|
|
|
){ |
|
|
|
|
for(CORPUS corpus:datas){ |
|
|
|
|
totalCount++; |
|
|
|
|
CORPUS existing = searchCORPUSByNlcCode(corpus.getNlcCode()); |
|
|
|
|
PreparedStatement stmt = null; |
|
|
|
|
if(corpus.equals(existing)){ |
|
|
|
|
continue; |
|
|
|
|
}else if(existing==null){ |
|
|
|
|
if(pendingInsert.contains(corpus.getNlcCode())){ |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
pendingInsert.add(corpus.getNlcCode()); |
|
|
|
|
stmt = psIns; |
|
|
|
|
insertCount++; |
|
|
|
|
}else{ |
|
|
|
|
stmt = psUpd; |
|
|
|
|
updateCount++; |
|
|
|
|
} |
|
|
|
|
setString(stmt, 1, corpus.getStanoxCode()); |
|
|
|
|
setString(stmt, 2, corpus.getUicCode()); |
|
|
|
|
setString(stmt, 3, corpus.getCrsCode()); |
|
|
|
|
setString(stmt, 4, corpus.getTiplocCode()); |
|
|
|
|
setString(stmt, 5, corpus.getLongDescription()); |
|
|
|
|
setString(stmt, 6, corpus.getShortDescription()); |
|
|
|
|
setString(stmt, 7, corpus.getNlcCode()); |
|
|
|
|
stmt.addBatch(); |
|
|
|
|
} |
|
|
|
|
if(insertCount>0){ |
|
|
|
|
psIns.executeBatch(); |
|
|
|
|
log.atInfo().log("Inserted {} CORPUS records", insertCount); |
|
|
|
|
} |
|
|
|
|
if(updateCount>0){ |
|
|
|
|
psUpd.executeBatch(); |
|
|
|
|
log.atInfo().log("Updated {} CORPUS records", updateCount); |
|
|
|
|
} |
|
|
|
|
if(insertCount==0 && updateCount==0){ |
|
|
|
|
log.atInfo().log("CORPUS data already up to date. "); |
|
|
|
|
}else{ |
|
|
|
|
conn.commit(); |
|
|
|
|
log.atInfo().log("Done committing CORPUS data"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void truncateTable() throws SQLException { |
|
|
|
|
try( |
|
|
|
|
@ -73,4 +136,175 @@ public class CORPUSDao extends org.leolo.nrdatad.db.CORPUSDao {
|
|
|
|
|
stmt.executeQuery("TRUNCATE TABLE corpus"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public boolean hasNlcCode(String nlcCode) throws SQLException { |
|
|
|
|
try( |
|
|
|
|
Connection conn = getConnection(); |
|
|
|
|
PreparedStatement pstmt = conn.prepareStatement("SELECT 1 FROM corpus WHERE nlc_code = ?") |
|
|
|
|
){ |
|
|
|
|
pstmt.setString(1, nlcCode); |
|
|
|
|
try(ResultSet rs = pstmt.executeQuery()){ |
|
|
|
|
return rs.next(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void update(CORPUS corpus) throws SQLException { |
|
|
|
|
try( |
|
|
|
|
Connection conn = getConnection(); |
|
|
|
|
PreparedStatement pstmt = conn.prepareStatement( |
|
|
|
|
"UPDATE corpus " + |
|
|
|
|
"SET stanox=?, uic_code=?, crs_code=?, tiploc_code=?, `desc`=?, short_desc=? " + |
|
|
|
|
"WHERE nlc_code = ?" |
|
|
|
|
) |
|
|
|
|
){ |
|
|
|
|
setString(pstmt, 1, corpus.getStanoxCode()); |
|
|
|
|
setString(pstmt, 2, corpus.getUicCode()); |
|
|
|
|
setString(pstmt, 3, corpus.getCrsCode()); |
|
|
|
|
setString(pstmt, 4, corpus.getTiplocCode()); |
|
|
|
|
setString(pstmt, 5, corpus.getLongDescription()); |
|
|
|
|
setString(pstmt, 6, corpus.getShortDescription()); |
|
|
|
|
setString(pstmt, 7, corpus.getNlcCode()); |
|
|
|
|
pstmt.executeUpdate(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public Collection<CORPUS> searchCORPUSByStanoxCode(String stanoxCode) throws SQLException { |
|
|
|
|
Collection<CORPUS> corpuses = new Vector<>(); |
|
|
|
|
try( |
|
|
|
|
Connection conn = getConnection(); |
|
|
|
|
PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM corpus WHERE stanox = ?") |
|
|
|
|
){ |
|
|
|
|
pstmt.setString(1, stanoxCode); |
|
|
|
|
try(ResultSet rs = pstmt.executeQuery()){ |
|
|
|
|
while(rs.next()){ |
|
|
|
|
corpuses.add(parseCORPUS(rs)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return corpuses; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public Collection<CORPUS> searchCORPUSByUicCode(String uicCode) throws SQLException { |
|
|
|
|
Collection<CORPUS> corpuses = new Vector<>(); |
|
|
|
|
try( |
|
|
|
|
Connection conn = getConnection(); |
|
|
|
|
PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM corpus WHERE uic_code = ?") |
|
|
|
|
){ |
|
|
|
|
pstmt.setString(1, uicCode); |
|
|
|
|
try(ResultSet rs = pstmt.executeQuery()){ |
|
|
|
|
while(rs.next()){ |
|
|
|
|
corpuses.add(parseCORPUS(rs)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return corpuses; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public Collection<CORPUS> searchCORPUSByCrsCode(String crsCode) throws SQLException { |
|
|
|
|
Collection<CORPUS> corpuses = new Vector<>(); |
|
|
|
|
try( |
|
|
|
|
Connection conn = getConnection(); |
|
|
|
|
PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM corpus WHERE crs_code = ?") |
|
|
|
|
){ |
|
|
|
|
pstmt.setString(1, crsCode); |
|
|
|
|
try(ResultSet rs = pstmt.executeQuery()){ |
|
|
|
|
while(rs.next()){ |
|
|
|
|
corpuses.add(parseCORPUS(rs)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return corpuses; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public Collection<CORPUS> searchCORPUSByTiplocCode(String tiplocCode) throws SQLException { |
|
|
|
|
Collection<CORPUS> corpuses = new Vector<>(); |
|
|
|
|
try( |
|
|
|
|
Connection conn = getConnection(); |
|
|
|
|
PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM corpus WHERE tiploc_code = ?") |
|
|
|
|
){ |
|
|
|
|
pstmt.setString(1, tiplocCode); |
|
|
|
|
try(ResultSet rs = pstmt.executeQuery()){ |
|
|
|
|
while(rs.next()){ |
|
|
|
|
corpuses.add(parseCORPUS(rs)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return corpuses; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public CORPUS searchCORPUSByNlcCode(String nlcCode) throws SQLException { |
|
|
|
|
try( |
|
|
|
|
Connection conn = getConnection(); |
|
|
|
|
PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM corpus WHERE nlc_code = ?") |
|
|
|
|
){ |
|
|
|
|
pstmt.setString(1, nlcCode); |
|
|
|
|
try(ResultSet rs = pstmt.executeQuery()){ |
|
|
|
|
if(rs.next()){ |
|
|
|
|
return parseCORPUS(rs); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public List<CORPUS> searchCORPUSByName(String name) throws SQLException { |
|
|
|
|
|
|
|
|
|
List<CORPUS> corpuses = new Vector<>(); |
|
|
|
|
try( |
|
|
|
|
Connection conn = getConnection(); |
|
|
|
|
PreparedStatement pstmt = conn.prepareStatement( |
|
|
|
|
"SELECT * FROM corpus WHERE " + |
|
|
|
|
"match(`desc`) against (?) OR " + |
|
|
|
|
"match(short_desc) against (?) " + |
|
|
|
|
"ORDER BY match(`desc`) against (?)+match(short_desc) against (?)" |
|
|
|
|
) |
|
|
|
|
){ |
|
|
|
|
pstmt.setString(1, name); |
|
|
|
|
pstmt.setString(2, name); |
|
|
|
|
pstmt.setString(3, name); |
|
|
|
|
pstmt.setString(4, name); |
|
|
|
|
try(ResultSet rs = pstmt.executeQuery()){ |
|
|
|
|
while(rs.next()){ |
|
|
|
|
corpuses.add(parseCORPUS(rs)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return corpuses; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private CORPUS parseCORPUS(ResultSet rs) throws SQLException{ |
|
|
|
|
CORPUS corpus = new CORPUS(); |
|
|
|
|
corpus.setStanoxCode(rs.getString("stanox")); |
|
|
|
|
corpus.setUicCode(rs.getString("uic_code")); |
|
|
|
|
corpus.setCrsCode(rs.getString("crs_code")); |
|
|
|
|
corpus.setTiplocCode(rs.getString("tiploc_code")); |
|
|
|
|
corpus.setNlcCode(rs.getString("nlc_code")); |
|
|
|
|
corpus.setLongDescription(rs.getString("desc")); |
|
|
|
|
corpus.setShortDescription(rs.getString("short_desc")); |
|
|
|
|
return corpus; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public CORPUS delete(String nlcCode) throws SQLException { |
|
|
|
|
CORPUS deletedEntry = searchCORPUSByNlcCode(nlcCode); |
|
|
|
|
if(deletedEntry!=null){ |
|
|
|
|
try( |
|
|
|
|
Connection conn = getConnection(); |
|
|
|
|
PreparedStatement stmt = conn.prepareStatement("DELETE FROM corpus WHERE nlc_code = ?") |
|
|
|
|
){ |
|
|
|
|
stmt.setString(1, nlcCode); |
|
|
|
|
stmt.executeUpdate(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return deletedEntry; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|