package zeta.tool;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DecimalFormat;
import zeta.util.DatabaseUtils;
import zeta.util.Properties;
import zeta.util.StreamUtils;
import zeta.util.ThrowableHandler;
public class ZetaApprove {
public static void main(String[] args) {
try {
if (args.length == 2) {
ZetaApprove approve = new ZetaApprove(args[0], args[1]);
approve.approve();
approve.close();
return;
}
System.err.println("USAGE: java zeta.tool.ZetaApprove <user> <password>");
} catch (Exception e) {
ThrowableHandler.handle(e);
}
}
private ZetaApprove(String user, String password) throws Exception {
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver").newInstance();
connection = DriverManager.getConnection("jdbc:db2:zeta", user, password);
}
private void close() {
DatabaseUtils.close(connection);
}
private void approve() throws SQLException, IOException {
Statement stmt = null;
Statement stmt2 = null;
try {
Properties properties = new Properties();
int serverId = properties.get("server_id", 1);
stmt = connection.createStatement();
stmt2 = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT task_id,work_unit_id,found FROM zeta.found WHERE approved_YN='N' AND type='close zeros'");
while (rs.next()) {
boolean approved = false;
int count = 0;
int taskId = rs.getInt(1);
long workUnitId = rs.getLong(2);
String found = rs.getString(3);
int idx = found.indexOf("This happened at ");
String pos = (idx >= 0)? found.substring(idx+17) : "";
ResultSet rs2 = stmt2.executeQuery("SELECT COUNT(*) FROM zeta.found WHERE approved_YN='N' AND type='close zeros' AND task_id=" + taskId
+ " AND work_unit_id=" + workUnitId
+ " AND found LIKE '%This happened at %'"
+ " AND NOT found LIKE '%This happened at " + pos + "%'");
if (rs2.next()) {
count = rs2.getInt(1);
rs2.close();
rs2 = stmt2.executeQuery("SELECT COUNT(*) FROM zeta.found WHERE approved_YN='N' AND type='close zeros' AND task_id=" + taskId + " AND work_unit_id=" + workUnitId);
int countSame = (rs2.next())? rs2.getInt(1) : 1;
rs2.close();
rs2 = stmt2.executeQuery("SELECT COUNT(*) FROM zeta.found WHERE approved_YN='N' AND type='close zeros' AND task_id=" + taskId + " AND found LIKE '%This happened at " + pos + "%' AND work_unit_id=" + workUnitId);
int countUnique = (rs2.next())? rs2.getInt(1) : 1;
rs2.close();
String search = "... Close pair of zeros between ";
if (found.startsWith(search)) {
if (idx > 0) {
int idx2 = found.indexOf(' ', search.length());
if (idx2 > 0) {
long n = ZetaStatistic.getStartN(Double.parseDouble(found.substring(search.length(), idx2)))-5;
ZetaStatistic.zetaZeros(n, 15, 0);
String filename = "zeta_zeros_" + n + "_15.log";
String[] closeZerosVerified = StreamUtils.startsWith(new String[] { "... Close pair of zeros between ", "This happened at " }, new FileReader(filename), true);
if (closeZerosVerified == null || closeZerosVerified.length <= 1) {
System.out.println("String not found: " + found);
} else if (count > 0 || countUnique > 1 || closeZerosVerified.length > 2) {
System.out.println("String not unique: " + found);
} else {
if (countUnique == 1 && countSame > 1) {
DatabaseUtils.executeAndLogUpdate(serverId, stmt2,
"DELETE FROM zeta.found WHERE approved_YN='N' AND type='close zeros' AND task_id=" + taskId + " AND NOT found LIKE '" + found + "' AND work_unit_id=" + workUnitId);
}
if (!closeZerosVerified[0].equals(found.substring(0, idx)) || !closeZerosVerified[1].equals(pos)) {
DatabaseUtils.executeAndLogUpdate(serverId, stmt2,
"UPDATE zeta.found SET (approved_YN,found)=('Y','" + closeZerosVerified[0] + closeZerosVerified[1]
+ "') WHERE approved_YN='N' AND type='close zeros' AND task_id=" + taskId + " AND work_unit_id=" + workUnitId);
System.out.println("Work unit " + workUnitId + " approved but CHANGED (" + getDistance(found) + ").");
} else {
DatabaseUtils.executeAndLogUpdate(serverId, stmt2,
"UPDATE zeta.found SET approved_YN='Y' WHERE approved_YN='N' AND type='close zeros' AND task_id=" + taskId + " AND work_unit_id=" + workUnitId);
System.out.println("Work unit " + workUnitId + " approved (" + getDistance(found) + ").");
}
approved = true;
}
}
}
}
} else {
rs2.close();
}
if (!approved) {
System.out.println("Work unit " + workUnitId + " cannot be approved (" + count + "): " + pos);
}
}
rs.close();
} finally {
DatabaseUtils.close(stmt);
DatabaseUtils.close(stmt2);
}
}
private String getDistance(String found) {
final int l = found.length();
int i = 0;
while (i < l && !Character.isDigit(found.charAt(i))) {
++i;
}
int j = found.indexOf(' ', i);
if (i > 0 && j > i) {
BigDecimal t1 = new BigDecimal(found.substring(i, j));
for (i = j; i < l && !Character.isDigit(found.charAt(i)); ++i);
j = found.indexOf(' ', i);
if (i > 0 && j > i) {
BigDecimal t2 = new BigDecimal(found.substring(i, j));
i = found.indexOf("This happened at ", j);
if (i > 0) {
return new DecimalFormat("0.00000000000").format(t2.subtract(t1));
}
}
}
return "";
}
private Connection connection = null;
}