コード例 #1
0
  public static boolean saveLumberLog(
      LumberLog lumberLog, LumberStack stack, LumberLogEntry stockEntry) {

    Logger logger = Logger.getLogger("dao");
    boolean status = false;
    boolean saveNewEntry = false;
    StringBuilder insertStockEntry = null;
    if (stockEntry.getId() == null) {
      saveNewEntry = true;
      insertStockEntry = new StringBuilder("insert into lumberentry(user) values(");
      insertStockEntry.append(stockEntry.getUser().getID()).append(")");
    }

    StringBuilder insertLumberLog =
        new StringBuilder(
            "insert into lumberlog( "
                + "small_diameter, medium_diameter, big_diameter, length, reallength, volume, realvolume, lumbertype, lumberclass,"
                + " idplate, stack, Status, SupplierId, TransportCertificateId, Margin, MarginVolume, RealMarginVolume, TransportEntryId) values (");
    insertLumberLog.append(lumberLog.getSmallRadius()).append(",");
    insertLumberLog.append("0").append(",");
    insertLumberLog.append(lumberLog.getBigRadius()).append(",");
    insertLumberLog.append(lumberLog.getLength()).append(",");
    insertLumberLog.append(lumberLog.getRealLength()).append(",");
    insertLumberLog.append(lumberLog.getVolume()).append(",");
    insertLumberLog.append(lumberLog.getRealVolume()).append(",");
    insertLumberLog.append(lumberLog.getLumberType()).append(",");
    insertLumberLog.append(lumberLog.getLumberClass()).append(",");
    insertLumberLog.append(lumberLog.getPlate().getId()).append(",");
    insertLumberLog.append(stack.getId()).append(",").append(lumberLog.getStatus()).append(",");
    insertLumberLog.append(lumberLog.getSupplierId()).append(",");
    insertLumberLog
        .append(
            lumberLog.getTransportCertifiateId() != null
                ? lumberLog.getTransportCertifiateId()
                : "NULL")
        .append(",");
    insertLumberLog.append(lumberLog.getMarginPercent()).append(",");
    insertLumberLog.append(lumberLog.getMarginVolume()).append(",");
    insertLumberLog.append(lumberLog.getMarginRealVolume()).append(",");
    insertLumberLog.append(lumberLog.getTransportEntryId()).append(")");

    StringBuilder insertMapEntrySql =
        new StringBuilder("insert into lumberentry_to_lumberlog(entryid, lumberlogid) values (");

    StringBuilder insertLumberLogDiameter =
        new StringBuilder(
            "insert into lumberlog_diameter(lumberlog_id, diameter, metric) values(?, ?, 1)");

    Connection con = null;
    Statement stm = null;
    PreparedStatement pstm = null;
    ResultSet rs = null;
    try {
      con = DataAccess.getInstance().getDatabaseConnection();
      con.setAutoCommit(false);
      stm = con.createStatement();
      logger.info(insertLumberLog.toString());
      int rez = stm.executeUpdate(insertLumberLog.toString(), Statement.RETURN_GENERATED_KEYS);
      if (rez > 0) {
        rs = stm.getGeneratedKeys();
        if (rs.next()) {
          lumberLog.setId(rs.getLong(1));
        }
      }
      if (rs != null) {
        rs.close();
      }
      if (saveNewEntry) {
        logger.info(insertStockEntry.toString());
        rez = stm.executeUpdate(insertStockEntry.toString(), Statement.RETURN_GENERATED_KEYS);
        if (rez > 0) {
          rs = stm.getGeneratedKeys();
          if (rs.next()) {
            stockEntry.setId(rs.getLong(1));
          }
        }
      }
      if (lumberLog.getMediumRadius() != null && !lumberLog.getMediumRadius().isEmpty()) {
        pstm = con.prepareStatement(insertLumberLogDiameter.toString());
        for (Double radius : lumberLog.getMediumRadius()) {
          pstm.setLong(1, lumberLog.getId());
          pstm.setDouble(2, radius);
          pstm.executeUpdate();
        }
      }
      if (lumberLog.getId() != null && stockEntry.getId() != null) {
        insertMapEntrySql
            .append(stockEntry.getId())
            .append(",")
            .append(lumberLog.getId())
            .append(")");
        logger.info(insertMapEntrySql.toString());
        rez = stm.executeUpdate(insertMapEntrySql.toString());
        if (rez > 0) {
          status = true;
          IDPlateManager.removeAvailablePlate(lumberLog.getPlate());
        }
      }
    } catch (Exception e) {
      try {
        con.rollback();
      } catch (Exception ex) {
      }
      logger.warning(e.getMessage());
      logger.log(Level.INFO, "Error", e);
      status = false;
    } finally {
      if (con != null) {
        try {
          con.setAutoCommit(true);
        } catch (Exception ex) {
        }
      }
      if (rs != null)
        try {
          rs.close();
        } catch (Exception e) {
        }
      if (stm != null)
        try {
          stm.close();
        } catch (Exception e) {
        }
    }

    return status;
  }