/** Format the 1.6.x.x.x... string into a 32 bits word */
  private void compactDetIds(ArrayList<ArrayList<String>> list) throws Exception {
    for (int i = 0; i < list.size(); i++) {
      ArrayList<String> v = list.get(i);

      DetIdConverter d = null;
      if ((v.get(1)).startsWith("1.6.")) {
        d = new TECDetIdConverter(v.get(1));
      } else {
        if ((v.get(1)).startsWith("1.5.")) d = new TOBDetIdConverter(v.get(1));
        else throw new Exception("The Det_ID should start with 1.6 (TEC) or 1.5 (TOB)");
      }

      v.set(1, d.compact() + "");
    }
  }
  private void updateConstructionDB(ArrayList<ArrayList<String>> list)
      throws java.sql.SQLException, ClassNotSupportedException, java.lang.Exception {
    if (DetIDGenerator.updateCB) {
      c.disconnect();

      configureExportDatabaseConnection();
      c.connect();

      c.beginTransaction();
      c.executeQuery("delete tec_detid");
      c.executeQuery("delete tob_detid");

      for (ArrayList<String> record : list) {
        int dcuID = Integer.parseInt(record.get(0));
        int detID = Integer.parseInt(record.get(1));
        DetIdConverter det = new DetIdConverter(detID);
        if (det.getSubDetector() == 6) { // TEC
          TECDetIdConverter d = new TECDetIdConverter(detID);
          d.compact();
          String query =
              "insert into tec_detid (DETECTOR,DISK,SECTOR,FRONT_BACK,RING,POSITION,STEREO,DCUID,DETID) values (\'TEC"
                  + (d.getTEC() == 1 ? "-" : "+")
                  + "\',"
                  + d.getWheel()
                  + ","
                  + d.getPetal()
                  + ",'"
                  + (d.getFrontBack() == 1 ? "F" : "B")
                  + "',"
                  + d.getRing()
                  + ","
                  + d.getModNumber()
                  + ",'"
                  + ((d.getStereo() == 1) ? "S" : (d.getStereo() == 0 ? "G" : "M"))
                  + "',"
                  + dcuID
                  + ","
                  + detID
                  + ")";
          System.out.println(query);
          c.executeQuery(query);
        }
        if (det.getSubDetector() == 5) { // TOB
          TOBDetIdConverter d = new TOBDetIdConverter(detID);
          d.compact();
          String query =
              "insert into tob_detid (LAYER,ROD,FRONT_BACK,POSITION,STEREO,DCUID,DETID) values ("
                  + d.getLayer()
                  + ","
                  + d.getRod()
                  + ",'"
                  + (d.getFrontBack() == 1 ? "F" : "B")
                  + "',"
                  + d.getModNumber()
                  + ",'"
                  + ((d.getStereo() == 1) ? "S" : (d.getStereo() == 0 ? "U" : "R"))
                  + "',"
                  + dcuID
                  + ","
                  + detID
                  + ")";
          System.out.println(query);
          c.executeQuery(query);
          // System.out.println(dcuID+","+detID+",TOB,"+d.getLayer()+","+d.getRod()+","+d.getFrontBack()+","+d.getModNumber()+","+d.getStereo());
        }
      }
      c.commit();
    }
  }
  private void getDCU(ArrayList<ArrayList<String>> list) throws java.sql.SQLException {
    // 1 method for TOB dcus
    PreparedStatement tob =
        c.createPreparedStatement(
            "select MB.dcuhardid from "
                + "cmstrkdb.TOBTESTINGMODULEBASIC_1_MOD_ MB "
                + "where MB.status='reference' AND MB.object_id=?");

    // 4 different methods for TEC (trye the first, if fails try the second ...)
    PreparedStatement psTec1 =
        c.createPreparedStatement(
            "select MB.dcuid from cmstrkdb.modvalidation_2_mod_ MV, "
                + "cmstrkdb.modulbasic_2_mod_ MB "
                + "where MB.test_id=MV.modulbasic_2_mod_ AND "
                + "MV.status='reference' AND MV.object_id=?");
    PreparedStatement psTec2 =
        c.createPreparedStatement(
            "select distinct FP.dcu_id from "
                + "cmstrkdb.hybproducer_1_hyb_ HP, "
                + "cmstrkdb.object_assembly OA, "
                + "cmstrkdb.fhitproduction_1_hyb_ FP where "
                + "HP.fhitproduction_1_hyb_=FP.test_id AND "
                + "OA.object_id=HP.object_id AND OA.object='HYB'"
                + " AND OA.container_id=?");
    PreparedStatement psTec3 =
        c.createPreparedStatement(
            "select distinct FR.dcu_id from "
                + "cmstrkdb.hybmeasurements_2_hyb_ HM, "
                + "cmstrkdb.object_assembly OA, "
                + "cmstrkdb.fhitreception_1_hyb_ FR where "
                + "HM.fhitreception_1_hyb_=FR.test_id AND "
                + "OA.object_id=HM.object_id AND OA.object='HYB' "
                + "AND OA.container_id=?");
    PreparedStatement psTec4 =
        c.createPreparedStatement(
            "select distinct FP.dcu_id from "
                + "cmstrkdb.object_assembly OA, "
                + "cmstrkdb.fhitproduction_1_hyb_ FP where "
                + "OA.object_id=FP.object_id AND OA.object='HYB' "
                + "AND OA.container_id=?");

    for (int i = 0; i < list.size(); i++) {
      ArrayList<String> v = list.get(i);

      DetIdConverter det = new DetIdConverter(Integer.parseInt(v.get(1)));
      if (det.getSubDetector() == 6) { // TEC
        ArrayList<ArrayList<String>> res = c.preparedSelectQuery(psTec1, v.get(0));
        if (res.size() == 1 && (res.get(0)).get(0) != null && !(res.get(0)).get(0).equals("0")) {
          ArrayList<String> detail = res.get(0);
          v.set(0, detail.get(0));
        } else {
          res = c.preparedSelectQuery(psTec2, v.get(0));
          if (res.size() == 1 && (res.get(0)).get(0) != null && !(res.get(0)).get(0).equals("0")) {
            ArrayList<String> detail = res.get(0);
            v.set(0, detail.get(0));
          } else {
            res = c.preparedSelectQuery(psTec3, v.get(0));
            if (res.size() == 1
                && (res.get(0)).get(0) != null
                && !(res.get(0)).get(0).equals("0")) {
              ArrayList<String> detail = res.get(0);
              v.set(0, detail.get(0));
            } else {
              res = c.preparedSelectQuery(psTec4, v.get(0));
              if (res.size() == 1
                  && (res.get(0)).get(0) != null
                  && !(res.get(0)).get(0).equals("0")) {
                ArrayList<String> detail = res.get(0);
                v.set(0, detail.get(0));
              } else {
                Error("DCU_ID of module " + v.get(0) + " unknown!!");
              }
            }
          }
        }
      }
      if (det.getSubDetector() == 5) { // TOB
        ArrayList<ArrayList<String>> res = c.preparedSelectQuery(tob, v.get(0));
        if (res.size() == 1 && (res.get(0)).get(0) != null && !(res.get(0)).get(0).equals("0")) {
          ArrayList<String> detail = res.get(0);
          int dcuId = reverseDcuId(Integer.parseInt(detail.get(0)));
          v.set(0, dcuId + "");
        } else {
          Error("DCU_ID of module " + v.get(0) + " unknown!!");
          v.set(0, "0");
        }
      }
      if (DetIDGenerator.verbose) System.out.print(((i * 100) / list.size()) + " %\r");
    }
    tob.close();
    psTec1.close();
    psTec2.close();
    psTec3.close();
    psTec4.close();
  }