protected LumberLog fromResultSet(ResultSet rs) throws SQLException { LumberLog lumberLog = new LumberLog(); lumberLog.setId(rs.getLong("id")); lumberLog.setLength(rs.getDouble("length")); lumberLog.setRealLength(rs.getLong("reallength")); lumberLog.setVolume(rs.getDouble("volume")); lumberLog.setRealVolume(rs.getDouble("realvolume")); lumberLog.setSmallRadius(rs.getDouble("small_diameter")); lumberLog.setBigRadius(rs.getDouble("big_diameter")); lumberLog.setLumberType(rs.getLong("lumbertype")); lumberLog.setLumberClass(rs.getLong("lumberclass")); lumberLog.setCutPlanId(rs.getLong("planId")); LumberStack stack = new LumberStack(); stack.setName(rs.getString("stackName")); stack.setId(rs.getLong("stack")); lumberLog.setStack(stack); IDPlate plate = new IDPlate(); plate.setId(rs.getLong("idplate")); plate.setLabel(rs.getString("plateName")); lumberLog.setPlate(plate); lumberLog.setSupplierId(rs.getLong("SupplierId")); lumberLog.setTransportCertifiateId(rs.getLong("TransportCertificateId")); if (rs.wasNull()) { lumberLog.setTransportCertifiateId(null); } lumberLog.setMarginPercent(rs.getInt("Margin")); lumberLog.setMarginVolume(rs.getDouble("MarginVolume")); lumberLog.setMarginRealVolume(rs.getDouble("RealMarginVolume")); return lumberLog; }
public static boolean markProcessedLumberLog(LumberLog lumberLog, String message) { Logger logger = Logger.getLogger("dao"); boolean status = false; StringBuilder selectSql = new StringBuilder( "select id, small_diameter, medium_diameter, big_diameter, length, volume, lumbertype, lumberclass, stack, idplate, planid, reallength, realvolume, status, SupplierId, TransportCertificateId, margin, MarginVolume, RealMarginVolume, TransportEntryId from lumberlog where id = ") .append(lumberLog.getId()); StringBuilder deleteSql = new StringBuilder("delete from lumberlog where id = ").append(lumberLog.getId()); StringBuilder insertSql = new StringBuilder( "insert into lumberlog_processed(userid, small_diameter, medium_diameter, big_diameter, length, volume, idlumbertype, idlumberclass, idstack, idplate, message, planId, reallength, realvolume, status, SupplierId, TransportCertificateId, Margin, MarginVolume, RealMarginVolume, TransportEntryId) values ("); insertSql.append(ConfigLocalManager.currentUser.getID()); StringBuilder cutPlan = new StringBuilder( " select PlanId, Percentage from CutPlanLumberLogDiagram cp inner join CutPlan plan on cp.PlanId = plan.id where plan.status = 0 and cp.LumberLogIDPlate = '") .append(DataAccess.escapeString(lumberLog.getPlate().getLabel())) .append("'"); StringBuilder checkLastFromPlan = new StringBuilder("select count(*) from lumberlog where planId = "); StringBuilder updateCutPlan = new StringBuilder(" update CutPlan set "); StringBuilder planPiecesUpdate = new StringBuilder( "update cutplanproduct set ProcessedPieces = ProcessedPieces + ? where PlanId= ? and ProductName =?"); Connection con = null; Statement stm = null; ResultSet rs = null; try { GeneralResponse diagramResponse = null; if (lumberLog.getCutPlanId() != null) { diagramResponse = CutPlanDAO.getCutDiagram(lumberLog.getPlate(), lumberLog.getCutPlanId().intValue()); } con = DataAccess.getInstance().getDatabaseConnection(); con.setAutoCommit(false); stm = con.createStatement(); logger.info(selectSql.toString()); rs = stm.executeQuery(selectSql.toString()); if (rs.next()) { insertSql.append(",").append(rs.getDouble(2)); insertSql.append(",").append(rs.getDouble(3)); insertSql.append(",").append(rs.getDouble(4)); insertSql.append(",").append(rs.getDouble(5)); insertSql.append(",").append(rs.getDouble(6)); insertSql.append(",").append(rs.getInt(7)); insertSql.append(",").append(rs.getInt(8)); insertSql.append(",").append(rs.getInt(9)); insertSql.append(",").append(rs.getInt(10)); insertSql.append(",'").append(DataAccess.escapeString(message)).append("'"); int planId = rs.getInt(11); insertSql.append(",").append(planId); insertSql.append(",").append(rs.getDouble(12)); // reallength insertSql.append(",").append(rs.getDouble(13)); // realvolume insertSql.append(",").append(rs.getInt(14)); // status insertSql.append(",").append(rs.getLong(15)); // supplierid insertSql.append(",").append(rs.getLong(16)); // transport certificate id insertSql.append(",").append(rs.getInt(17)); // margin insertSql.append(",").append(rs.getDouble(18)); // marginvolume insertSql.append(",").append(rs.getDouble(19)); // realmarginvolume insertSql.append(",").append(rs.getLong(20)); // transport entry id insertSql.append(")"); } logger.info(insertSql.toString()); int insertRez = stm.executeUpdate(insertSql.toString()); logger.info(deleteSql.toString()); int deleteRez = stm.executeUpdate(deleteSql.toString()); if (insertRez > 0 && deleteRez > 0) { status = true; logger.info(cutPlan.toString()); rs = stm.executeQuery(cutPlan.toString()); Integer planId = null; if (rs.next()) { planId = rs.getInt(1); Double percent = rs.getDouble(2); updateCutPlan .append("complete = complete + ") .append(percent) .append(" where id=" + planId); } if (planId != null) { rs = stm.executeQuery(checkLastFromPlan.append(planId).toString()); if (rs.next()) { int count = rs.getInt(1); if (count == 0) { updateCutPlan.append(",status=1 ").append(" where id = ").append(planId); } } logger.info(updateCutPlan.toString()); stm.executeUpdate(updateCutPlan.toString()); } IDPlateManager.addAvailablePlate(lumberLog.getPlate()); if (diagramResponse != null) { if (diagramResponse.getCode() == 200) { CutDiagram diagram = (CutDiagram) diagramResponse.getData(); PreparedStatement pstm = con.prepareStatement(planPiecesUpdate.toString()); Iterator<Map.Entry<String, Integer>> cutInfo = diagram.cutInfo.cutPieces.entrySet().iterator(); while (cutInfo.hasNext()) { Map.Entry<String, Integer> productInfo = cutInfo.next(); String productName = productInfo.getKey(); Integer pieces = productInfo.getValue(); System.out.println(productName + " " + pieces); pstm.setInt(1, pieces); pstm.setInt(2, lumberLog.getCutPlanId().intValue()); pstm.setString(3, productName); pstm.executeUpdate(); } pstm.close(); } } } con.commit(); } catch (Exception e) { try { con.rollback(); } catch (SQLException ex) { } logger.warning(e.getMessage()); logger.log(Level.INFO, "Error", e); status = false; } finally { if (rs != null) try { rs.close(); } catch (Exception e) { } if (stm != null) try { stm.close(); } catch (Exception e) { } if (con != null) { try { con.setAutoCommit(true); } catch (Exception e) { } } } return status; }