/**
   * Perform the treatment :
   *
   * <ul>
   *   <li>Get the Det IDs for TEC and TOB
   *   <li>Get the DCU IDs associated to the module IDs
   *   <li>Get the fiber's length
   *   <li>Get the number of APVs
   *   <li>Export the data to a DB or print them on the screen
   * </ul>
   */
  public void go() {
    try {
      ArrayList<ArrayList<String>> list = new ArrayList<ArrayList<String>>();
      IDetIdGenerator tec = new TECAnalyzer();
      IDetIdGenerator tob = new TOBAnalyzer();

      list.addAll(tec.getDetIds());
      list.addAll(tob.getDetIds());

      if (DetIDGenerator.verbose) System.out.println(list.size() + " modules found");

      if (!DetIDGenerator.updateCB) {
        if (DetIDGenerator.verbose) System.out.println("Retrieving the fibers length...");
        getFiberLength(list);
      }

      if (DetIDGenerator.verbose) System.out.println("Converting DetIds to 32 bits...");
      compactDetIds(list);

      if (!DetIDGenerator.updateCB) {
        if (DetIDGenerator.verbose) System.out.println("Retrieving the number of APVs...");
        getApvNumber(list);
      }

      if (DetIDGenerator.verbose) System.out.println("Searching the DCU ids...");
      getDCU(list);

      if (DetIDGenerator.verbose) System.out.println("Reversing the DCU ids...");
      reverseDcuIds(list);

      if (!DetIDGenerator.updateCB) {
        if (DetIDGenerator.verbose) System.out.println("Exporting...");
        exportData(list);
      } else {
        System.out.println("updating the construction DB...");
        updateConstructionDB(list);
      }

      c.disconnect();

    } catch (java.sql.SQLException e) {
      Error("SQL Error : \n" + query + "\n" + e.getMessage());
    } catch (ClassNotSupportedException e) {
      Error("ClassNotSupportedException :\n" + e.getMessage());
    } catch (java.lang.ClassNotFoundException e) {
      Error("Can not find Oracle driver");
    } catch (Exception e) {
      Error("Error : \n" + e.getMessage());
    }
  }
  private void getFiberLength(ArrayList<ArrayList<String>> list) throws java.sql.SQLException {

    PreparedStatement modules =
        c.createPreparedStatement(
            "select OA2.object_id, OA2.number_in_container from cmstrkdb.object_assembly OA, cmstrkdb.object_assembly OA2 WHERE OA2.object='AOH' AND OA2.number_in_container=OA.number_in_container AND OA2.container_id=OA.container_id AND OA.object_id=?");
    PreparedStatement aoh =
        c.createPreparedStatement(
            "select G.fiber_length from cmstrkdb.aohgeneral_1_aoh_ G, cmstrkdb.aohcomp_1_aoh_ C where C.aohgeneral_1_aoh_=G.test_id AND C.object_id=?");
    PreparedStatement length =
        c.createPreparedStatement(
            "select distinct D.fanout_length from cmstrkdb.manufacture_1_optfanout_ M, cmstrkdb.diamond_1_optfanout_ D, cmstrkdb.object_assembly FANOUT, cmstrkdb.object_assembly AOH, cmstrkdb.object_assembly LASTRANS, cmstrkdb.link L WHERE M.diamond_1_optfanout_=D.test_id AND M.object_id=FANOUT.object_id AND L.object_id_b=FANOUT.object_id AND L.object_id_a=LASTRANS.object_id AND LASTRANS.container_id=AOH.object_id AND AOH.object_id=?");

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

        String cDetID = new String(v.get(1));

        String[] det_id = cDetID.split("\\.");
        int tec = Integer.parseInt(det_id[2]);
        int disk = Integer.parseInt(det_id[3]);
        int front = Integer.parseInt(det_id[4]);
        int sector = Integer.parseInt(det_id[5]);
        // System.out.println(tec+" "+disk+" "+front+" "+sector);

        ArrayList<ArrayList<String>> res = c.preparedSelectQuery(modules, v.get(0));
        if (res.size() > 0) {
          String aoh_id = (res.get(0)).get(0);
          int aoh_position = Integer.parseInt((res.get(0)).get(1));

          ArrayList<ArrayList<String>> lengthRes = c.preparedSelectQuery(aoh, aoh_id);
          if (lengthRes.size() > 0) {
            int aoh_length = Integer.parseInt((lengthRes.get(0)).get(0));

            ArrayList<ArrayList<String>> lengthRes2 = c.preparedSelectQuery(length, aoh_id);
            if (lengthRes2.size() == 1) {
              float total_length = aoh_length + Integer.parseInt((lengthRes2.get(0)).get(0));
              v.add((total_length / 10) + ""); // The size should be in centimeters
            } else {
              v.add("1");
              if (lengthRes2.size() > 1)
                throw new Exception("Can not find unique length for the aoh " + aoh_id);
              else throw new Exception("Data are missing for this AOH : " + aoh_id);
            }
          } else {
            v.add("1");
            throw new Exception("Can not find the length of the aoh " + aoh_id);
          }
        } else {
          v.add("1");
          throw new Exception("Can not find the AOH corresponding to module " + v.get(0));
        }
        if (DetIDGenerator.verbose) System.out.print(((i * 100) / list.size()) + " %\r");

      } catch (java.sql.SQLException e) {
        throw e;
      } catch (Exception e) {
        Error(e.getMessage());
      }
    }
    // Close the statements
    modules.close();
    aoh.close();
    length.close();
  }