package pickMaint; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.logging.Logger; /** * This represents my attempts to recreate the incentive pay calculations done on the AS400 * This is for research purposes. Magic numbers are constants pulled from the DB. * @author Wes */ public class EfficiencyBonusCalc { private static final int[] DATE = new int[]{ 180219, 180220, 180221, 180222 }; private static final int WEAVER = 183; private static final double[] MAX_BONUS = new double[]{ 0.0, 0.0, 0.0, 0.0, 2.5, 3.0, 3.75, 4.0, 4.25, 4.75, 7.0, 10.25, 33.0, 33.0 }; private static final Logger LOGGER = Logger.getLogger(EfficiencyBonusCalc.class.getName()); private static final String EFF_BONUS_BY_DATE = "SELECT y.lddate, " + " y.ldshif, " + " y.looms, " + " y.picks, " + " y.pickspossible, " + " ((((CAST(y.picks AS FLOAT) / y.pickspossible) * (y.loomhours * LMRTLH)) - (y.shiftlen * LMRATE)) * lmpct) EFF_BON " + "FROM (SELECT x.lddate, " + " x.ldshif, " + " CAST(ROUND(SUM(x.ldhrs) / max(x.ldhrs), 0) AS INT) looms, " + " SUM(x.ldhrs) loomhours, " + " max(x.ldhrs) shiftlen, " + " sum(x.ldpick) picks, " + " sum(x.maxpicks) pickspossible " + " FROM (SELECT lddate, " + " ldshif, " + " ldloom, " + " ldweav, " + " ldhrs, " + " ldpick, " + " CAST(ROUND((lmppm * ldhrs), 0) AS INT) maxpicks " + " FROM stmdata.loomds " + " INNER JOIN stmdata.loomms ON stmdata.loomds.ldloom = stmdata.loomms.lmlomn " + " WHERE lddate = ? AND " + " ldshif = ? AND " + " ldweav = ?) x " + " GROUP BY x.lddate,x.ldshif) y " + " LEFT JOIN stmdata.lmprir ON stmdata.lmprir.lmloom = 'D' AND " + " stmdata.lmprir.lmnolm = y.looms "; private static final String WEAVER_INFO = "select wvrnam, shift from stmdata.iewvms where WVR# = " + WEAVER; public static void main(String[] args) throws SQLException, IOException { System.out.println(EFF_BONUS_BY_DATE); // Register AS400 DB2 driver and open a connection with the AS400 LOGGER.info("Connecting to AS400 as user: WRAY\n"); DriverManager.registerDriver(new com.ibm.as400.access.AS400JDBCDriver()); Connection conn = DriverManager.getConnection("jdbc:as400://0.0.0.0", "USER", "PASSWORD"); LOGGER.info("Pre-compiling SQL statements...\n"); PreparedStatement effBonusGet = conn.prepareStatement(EFF_BONUS_BY_DATE); LOGGER.info("Looking up weaver name and shift for number " + WEAVER +"...\n"); int shift = 0; String name = ""; double total = 0; ResultSet weavInfo = conn.createStatement().executeQuery(WEAVER_INFO); if(weavInfo.next()) { shift = weavInfo.getInt(2); name = weavInfo.getString(1).trim(); } else { throw new RuntimeException("Weaver doesn't exist!"); } System.out.println("Efficiency Bonus Calculations for " + name + " (" + WEAVER + ") " + ((shift == 1) ? "1st" : (shift == 2) ? "2nd" : (shift == 3) ? "3rd" : "") + " shift.\n"); for(int d : DATE){ effBonusGet.setInt(1, d); effBonusGet.setInt(2, shift); effBonusGet.setInt(3, WEAVER); ResultSet rs = effBonusGet.executeQuery(); if(rs.next()){ int looms = rs.getInt(3); if(looms > 13) looms = 13; double bonus = rs.getDouble(6); bonus = Math.min(bonus, MAX_BONUS[looms]); System.out.println(d + "_" + shift + " - " + looms + " looms. $" + bonus); total += bonus; rs.close(); } else { System.out.println(d + "_" + shift + " - NO DATA"); } } System.out.println("\nTotal Efficiency Bonus: $" + total); effBonusGet.close(); effBonusGet = null; conn.close(); conn = null; System.exit(0); } /** * The ResultSet you pass to this method must be from a PreparedStatement created with these args: * prepareStatement("select * from ...", ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); * * This will throw an exception with FORWARD_ONLY ResultSets (as in the default one you get with no extra arguments) * * ResultSet.CONCUR_READ_ONLY may actually be something else if you so desire. TYPE_SCROLL_INSENSITIVE is what is important. * * @param set - The ResultSet to count rows in * @return The number of rows in the ResultSet * @throws SQLException */ public static int getRowCount(ResultSet set) throws SQLException { int rowCount; int currentRow = set.getRow(); // Get current row rowCount = set.last() ? set.getRow() : 0; // Determine number of rows if (currentRow == 0) // If there was no current row set.beforeFirst(); // We want next() to go to first row else // If there WAS a current row set.absolute(currentRow); // Restore it return rowCount; } }