/**
   * Set the Software to query.
   *
   * @param x The Software of the SoftwareCandidate to query.
   * @param exact to use matches or not
   * @exception DataObjectException If a database access error occurs.
   */
  public void setQuerySoftware(jobmatch.data.SoftwareDO x, boolean exact)
      throws DataObjectException, QueryException {
    // Remove from cacheHits any DOs that do not meet this
    // setQuery requirement.
    for (int i = 0; i < cacheHits.size() && !hitDb; i++) {
      SoftwareCandidateDO DO = (SoftwareCandidateDO) cacheHits.elementAt(i);
      if (null == DO) continue;
      boolean equals = true;

      // DOs are compared by their handles..
      jobmatch.data.SoftwareDO m = DO.getSoftware();
      if (null == m && null == x) {
        equals = true;
      } else if (null == m || null == x) {
        equals = false;
      } else {
        equals = (DO.getSoftware().getOId().toString().equals(x.getOId().toString()));
        if (equals && m != x) {
          System.err.println("\n----------------------------------------------------------");
          System.err.println("m =" + m);
          System.err.println("x =" + x);
        }
      }

      if (!equals) cacheHits.removeElementAt(i--);
    }

    // Also prepare the SQL needed to query the database
    // in case there is no cache, or the query involves other tables.
    if (partialCache || hitDb)
      builder.addWhereClause(
          "Software", x, "DECIMAL(19,0)", QueryBuilder.NOT_NULL, exactFlag(exact));
  }
  private Connection getPooledConnection() throws Exception {
    Connection conn = null;
    int size = pool.size();

    if (size > 0) {
      conn = (Connection) (pool.elementAt(0));
      pool.removeElementAt(0);
    } else if (users < maxCon || maxCon == 0) {
      conn = createConnection();
    }

    return conn;
  }
  /**
   * Set the OID to query. WARNING! This method assumes that table <CODE>SoftwareCandidate</CODE>
   * has a column named <CODE>"oid"</CODE>. This method is called from the DO classes to retrieve an
   * object by id.
   *
   * @param oid The object id to query.
   */
  public void setQueryOId(ObjectId oid) {
    // Remove from cacheHits any DOs that do not meet this
    // setQuery requirement.
    if (null == oid) return;
    requireUniqueInstance();
    for (int i = 0; i < cacheHits.size(); i++) {
      SoftwareCandidateDO DO = (SoftwareCandidateDO) cacheHits.elementAt(i);
      if (null == DO) continue;
      boolean equals = true;
      ObjectId id = DO.getOId();
      if (null == id || !id.equals(oid)) cacheHits.removeElementAt(i--);
    }

    // Also prepare the SQL needed to query the database
    // in case there is no cache, or the query involves other tables.
    builder.addWhereClause("oid", oid.toBigDecimal(), QueryBuilder.NOT_NULL);
  }
  /**
   * Set the LeafNumber to query.
   *
   * @param x The LeafNumber of the OperatingsystemProfile to query.
   * @param exact to use matches or not
   * @exception DataObjectException If a database access error occurs.
   */
  public void setQueryLeafNumber(int x, boolean exact) throws DataObjectException, QueryException {
    // Remove from cacheHits any DOs that do not meet this
    // setQuery requirement.
    for (int i = 0; i < cacheHits.size() && !hitDb; i++) {
      OperatingsystemProfileDO DO = (OperatingsystemProfileDO) cacheHits.elementAt(i);
      if (null == DO) continue;
      boolean equals = true;

      // primitive types are compared using the == operator.
      equals = (DO.getLeafNumber() == x);

      if (!equals) cacheHits.removeElementAt(i--);
    }

    // Also prepare the SQL needed to query the database
    // in case there is no cache, or the query involves other tables.
    if (partialCache || hitDb)
      builder.addWhereClause("LeafNumber", x, "INTEGER", QueryBuilder.NULL_OK, exactFlag(exact));
  }
  // Parse cluster info from Clusters table and
  // place all cluster=>plugin info into hashtable
  // Schema :
  // create table Clusters (
  //    Cluster String,
  //    Plugin  String
  // );
  private void parseClusterInfo(
      Hashtable all_clusters, Statement stmt, String community, String node) throws SQLException {

    String sql = null;
    ResultSet rset;
    int number = 0;

    // Grab the plugin groups
    Hashtable plugInGroups = new Hashtable();
    sql = "select GroupName, GroupSequence from GroupMaster";

    number = 0;
    System.out.println(sql);

    rset = stmt.executeQuery(sql);

    while (rset.next()) {
      number++;
      String groupName = rset.getString("GroupName");
      System.out.println(groupName);
      Integer index = new Integer(rset.getInt("GroupSequence"));
      System.out.println(index);
      plugInGroups.put(index, groupName);
    }
    System.out.println("Query returned " + number + " results");

    // Grab the group definitions
    Hashtable groupDefinitions = new Hashtable();
    for (int ind = 0; ind < plugInGroups.size(); ind++) {

      String group = (String) plugInGroups.get(new Integer(ind));
      if (group != null) {
        sql =
            "select InGroupSequence, Plugin from GroupDefinitions"
                + " where GroupName='"
                + group
                + "'";
        rset = stmt.executeQuery(sql);
        System.out.println(sql);
        groupDefinitions.put(group, new Hashtable());

        number = 0;
        while (rset.next()) {
          number++;
          Hashtable plugins = (Hashtable) groupDefinitions.get(group);
          int index = rset.getInt("InGroupSequence");
          String plugIn = rset.getString("Plugin");
          plugins.put(new Integer(index), plugIn);
        }
        System.out.println("Query returned " + number + " results");
      } else {
        break;
      }
    }

    // Add the plugIn groups per cluster to the plugIn vector
    number = 0;
    sql = "select Cluster, PluginGroup from GroupsInCluster";

    if (community != null && node != null) {
      sql =
          sql
              + ", Organizations where Cluster=Organizations.Name and"
              + " Organizations.Community='"
              + community
              + "' and Organizations.Node='"
              + node
              + "'";
    } else if (community != null) {
      sql =
          sql
              + ", Organizations where Cluster=Organizations.Name and"
              + " Organizations.Community='"
              + community
              + "'";
    } else if (node != null) {
      sql = sql + ", Organizations where Organizations.Node='" + node + "'";
    }

    System.out.println(sql);

    number = 0;
    rset = stmt.executeQuery(sql);
    while (rset.next()) {
      number++;
      String cluster_name = rset.getString("Cluster");
      String pluginGroup = rset.getString("PluginGroup");

      Vector current_cluster_plugins = (Vector) all_clusters.get(cluster_name);
      if (current_cluster_plugins == null) {
        current_cluster_plugins = new Vector();
        all_clusters.put(cluster_name, current_cluster_plugins);
      }
      Hashtable groupPlugins = (Hashtable) groupDefinitions.get(pluginGroup);
      for (int ind = 0; ind < groupPlugins.size(); ind++) {
        current_cluster_plugins.addElement(groupPlugins.get(new Integer(ind)));
      }
    }
    System.out.println("Query returned " + number + " results");

    // Grab the plugins that go in every community or every cluster within specific communities
    Vector mdbCommunities = new Vector();

    sql = "select Community from CommunityMaster";
    number = 0;
    System.out.println(sql);

    rset = stmt.executeQuery(sql);

    while (rset.next()) {
      number++;
      String community_name = rset.getString("Community");
      mdbCommunities.addElement(community_name);
    }
    System.out.println("Query returned " + number + " results");

    while (!mdbCommunities.isEmpty()) {
      number = 0;
      String sCommunity = (String) mdbCommunities.firstElement();
      mdbCommunities.removeElementAt(0);

      sql =
          "select Organizations.Name, CommunityPlugins.Plugin from Organizations, CommunityPlugins"
              + " where CommunityPlugins.Community='"
              + sCommunity
              + "'";

      if (!sCommunity.equals("Society")) {
        if (community != null) {
          if (!community.equals(sCommunity)) {
            continue;
          }
        }
        sql = sql + " and Organizations.Community='" + sCommunity + "'";
      } else {
        if (community != null) {
          sql = sql + " and Organizations.Community='" + community + "'";
        }
      }

      if (node != null) {
        sql = sql + " and Organizations.Node='" + node + "'";
      }

      System.out.println(sql);
      number = 0;
      rset = stmt.executeQuery(sql);
      while (rset.next()) {
        number++;
        String cluster_name = rset.getString("Name");
        String plugin = rset.getString("Plugin");

        Vector current_cluster_plugins = (Vector) all_clusters.get(cluster_name);
        if (current_cluster_plugins == null) {
          current_cluster_plugins = new Vector();
          all_clusters.put(cluster_name, current_cluster_plugins);
        }

        current_cluster_plugins.addElement(plugin);
      }
      System.out.println("Query returned " + number + " results");
    }

    // Query for all Cluster/Plugin info and populate 'all_clusters' hashtable

    sql =
        "select Clusters.Cluster, Clusters.Plugin, Clusters.Parameters from Clusters, Organizations"
            + " where Clusters.Cluster=Organizations.Name";

    if (community != null && node != null) {
      sql =
          sql
              + "and Organizations.Node='"
              + node
              + "'"
              + " and Organizations.Community='"
              + community
              + "'";
    } else if (community != null) {
      sql = sql + " and Organizations.Community='" + community + "'";
    } else if (node != null) {
      sql = sql + " and Organizations.Node='" + node + "'";
    }

    System.out.println(sql);

    number = 0;
    rset = stmt.executeQuery(sql);
    while (rset.next()) {
      number++;
      String cluster_name = rset.getString("Cluster");
      String plugin = rset.getString("Plugin");
      String parameters = rset.getString("Parameters");

      // put the parameters into the plugin
      if (parameters != null) {
        plugin = plugin + "(" + parameters + ")";
      }

      Vector current_cluster_plugins = (Vector) all_clusters.get(cluster_name);
      if (current_cluster_plugins == null) {
        current_cluster_plugins = new Vector();
        all_clusters.put(cluster_name, current_cluster_plugins);
      }

      current_cluster_plugins.addElement(plugin);
    }
    System.out.println("Query returned " + number + " results");
  }