/**
  * Constructor.
  *
  * @param manager is the ToolManager.
  * @param objectDescriptions are the object descriptions.
  */
 public RepositoryConnectionExecutor(
     RepositoryConnectionManager manager, RepositoryConnectionDescription[] objectDescriptions) {
   super();
   thisManager = manager;
   returnValues = new RepositoryConnection[objectDescriptions.length];
   int i = 0;
   while (i < objectDescriptions.length) {
     returnMap.put(objectDescriptions[i].getConnectionName(), new Integer(i));
     i++;
   }
 }
 /**
  * Fetch multiple repository connections at a single time.
  *
  * @param connectionNames are a list of connection names.
  * @return the corresponding repository connection objects.
  */
 protected RepositoryConnection[] getRepositoryConnectionsMultiple(String[] connectionNames)
     throws ManifoldCFException {
   RepositoryConnection[] rval = new RepositoryConnection[connectionNames.length];
   HashMap returnIndex = new HashMap();
   int i = 0;
   while (i < connectionNames.length) {
     rval[i] = null;
     returnIndex.put(connectionNames[i], new Integer(i));
     i++;
   }
   beginTransaction();
   try {
     i = 0;
     ArrayList params = new ArrayList();
     int j = 0;
     int maxIn = maxClauseGetRepositoryConnectionsChunk();
     while (i < connectionNames.length) {
       if (j == maxIn) {
         getRepositoryConnectionsChunk(rval, returnIndex, params);
         params.clear();
         j = 0;
       }
       params.add(connectionNames[i]);
       i++;
       j++;
     }
     if (j > 0) getRepositoryConnectionsChunk(rval, returnIndex, params);
     return rval;
   } catch (Error e) {
     signalRollback();
     throw e;
   } catch (ManifoldCFException e) {
     signalRollback();
     throw e;
   } finally {
     endTransaction();
   }
 }
    /**
     * Notify the implementing class of the existence of a cached version of the object. The object
     * is passed to this method so that the execute() method below will have it available to operate
     * on. This method is also called for all objects that are freshly created as well.
     *
     * @param objectDescription is the unique identifier of the object.
     * @param cachedObject is the cached object.
     */
    public void exists(ICacheDescription objectDescription, Object cachedObject)
        throws ManifoldCFException {
      // Cast what came in as what it really is
      RepositoryConnectionDescription objectDesc =
          (RepositoryConnectionDescription) objectDescription;
      RepositoryConnection ci = (RepositoryConnection) cachedObject;

      // Duplicate it!
      if (ci != null) ci = ci.duplicate();

      // In order to make the indexes line up, we need to use the hashtable built by
      // the constructor.
      returnValues[((Integer) returnMap.get(objectDesc.getConnectionName())).intValue()] = ci;
    }
  /** Install the manager. */
  public void install() throws ManifoldCFException {
    // First, get the authority manager table name and name column
    IAuthorityGroupManager authMgr = AuthorityGroupManagerFactory.make(threadContext);

    // Always use a loop, and no transaction, as we may need to retry due to upgrade
    while (true) {
      Map existing = getTableSchema(null, null);
      if (existing == null) {
        // Install the "objects" table.
        HashMap map = new HashMap();
        map.put(nameField, new ColumnDescription("VARCHAR(32)", true, false, null, null, false));
        map.put(
            descriptionField,
            new ColumnDescription("VARCHAR(255)", false, true, null, null, false));
        map.put(
            classNameField, new ColumnDescription("VARCHAR(255)", false, false, null, null, false));
        map.put(
            groupNameField,
            new ColumnDescription(
                "VARCHAR(32)",
                false,
                true,
                authMgr.getTableName(),
                authMgr.getGroupNameColumn(),
                false));
        map.put(maxCountField, new ColumnDescription("BIGINT", false, false, null, null, false));
        map.put(configField, new ColumnDescription("LONGTEXT", false, true, null, null, false));
        performCreate(map, null);
      } else {
        // Upgrade code
        ColumnDescription cd = (ColumnDescription) existing.get(groupNameField);
        if (cd == null) {
          Map addMap = new HashMap();
          addMap.put(
              groupNameField,
              new ColumnDescription(
                  "VARCHAR(32)",
                  false,
                  true,
                  authMgr.getTableName(),
                  authMgr.getGroupNameColumn(),
                  false));
          performAlter(addMap, null, null, null);
        }
        // Get rid of the authorityName field.  When we do this we need to copy into the group name
        // field, adding groups if they don't yet exist first
        cd = (ColumnDescription) existing.get(authorityNameField);
        if (cd != null) {
          ArrayList params = new ArrayList();
          IResultSet set =
              performQuery(
                  "SELECT " + nameField + "," + authorityNameField + " FROM " + getTableName(),
                  null,
                  null,
                  null);
          for (int i = 0; i < set.getRowCount(); i++) {
            IResultRow row = set.getRow(i);
            String repoName = (String) row.getValue(nameField);
            String authName = (String) row.getValue(authorityNameField);
            if (authName != null && authName.length() > 0) {
              // Attempt to create a matching auth group.  This will fail if the group
              // already exists
              IAuthorityGroup grp = authMgr.create();
              grp.setName(authName);
              try {
                authMgr.save(grp);
              } catch (ManifoldCFException e) {
                if (e.getErrorCode() == ManifoldCFException.INTERRUPTED) throw e;
                // Fall through; the row exists already
              }
              Map<String, String> map = new HashMap<String, String>();
              map.put(groupNameField, authName);
              params.clear();
              String query =
                  buildConjunctionClause(
                      params, new ClauseDescription[] {new UnitaryClause(nameField, repoName)});
              performUpdate(map, " WHERE " + query, params, null);
            }
          }
          List<String> deleteList = new ArrayList<String>();
          deleteList.add(authorityNameField);
          performAlter(null, null, deleteList, null);
        }
      }

      // Install dependent tables.
      historyManager.install(getTableName(), nameField);
      throttleSpecManager.install(getTableName(), nameField);

      // Index management
      IndexDescription authorityIndex = new IndexDescription(false, new String[] {groupNameField});
      IndexDescription classIndex = new IndexDescription(false, new String[] {classNameField});

      // Get rid of indexes that shouldn't be there
      Map indexes = getTableIndexes(null, null);
      Iterator iter = indexes.keySet().iterator();
      while (iter.hasNext()) {
        String indexName = (String) iter.next();
        IndexDescription id = (IndexDescription) indexes.get(indexName);

        if (authorityIndex != null && id.equals(authorityIndex)) authorityIndex = null;
        else if (classIndex != null && id.equals(classIndex)) classIndex = null;
        else if (indexName.indexOf("_pkey") == -1)
          // This index shouldn't be here; drop it
          performRemoveIndex(indexName);
      }

      // Add the ones we didn't find
      if (authorityIndex != null) performAddIndex(null, authorityIndex);

      if (classIndex != null) performAddIndex(null, classIndex);

      break;
    }
  }
  /**
   * Save a repository connection object.
   *
   * @param object is the object to save.
   * @return true if the object was created, false otherwise.
   */
  public boolean save(IRepositoryConnection object) throws ManifoldCFException {
    StringSetBuffer ssb = new StringSetBuffer();
    ssb.add(getRepositoryConnectionsKey());
    ssb.add(getRepositoryConnectionKey(object.getName()));
    StringSet cacheKeys = new StringSet(ssb);
    while (true) {
      // Catch deadlock condition
      long sleepAmt = 0L;
      try {
        ICacheHandle ch = cacheManager.enterCache(null, cacheKeys, getTransactionID());
        try {
          beginTransaction();
          try {
            // performLock();
            // Notify of a change to the configuration
            ManifoldCF.noteConfigurationChange();
            boolean isNew = object.getIsNew();
            // See whether the instance exists
            ArrayList params = new ArrayList();
            String query =
                buildConjunctionClause(
                    params,
                    new ClauseDescription[] {new UnitaryClause(nameField, object.getName())});
            IResultSet set =
                performQuery(
                    "SELECT * FROM " + getTableName() + " WHERE " + query + " FOR UPDATE",
                    params,
                    null,
                    null);
            HashMap values = new HashMap();
            values.put(descriptionField, object.getDescription());
            values.put(classNameField, object.getClassName());
            values.put(groupNameField, object.getACLAuthority());
            values.put(maxCountField, new Long((long) object.getMaxConnections()));
            String configXML = object.getConfigParams().toXML();
            values.put(configField, configXML);
            boolean notificationNeeded = false;
            boolean isCreated;

            if (set.getRowCount() > 0) {
              // If the object is supposedly new, it is bad that we found one that already exists.
              if (isNew)
                throw new ManifoldCFException(
                    "Repository connection '" + object.getName() + "' already exists");
              isCreated = false;
              IResultRow row = set.getRow(0);
              String oldXML = (String) row.getValue(configField);
              if (oldXML == null || !oldXML.equals(configXML)) notificationNeeded = true;

              // Update
              params.clear();
              query =
                  buildConjunctionClause(
                      params,
                      new ClauseDescription[] {new UnitaryClause(nameField, object.getName())});
              performUpdate(values, " WHERE " + query, params, null);
              throttleSpecManager.deleteRows(object.getName());
            } else {
              // If the object is not supposed to be new, it is bad that we did not find one.
              if (!isNew)
                throw new ManifoldCFException(
                    "Repository connection '" + object.getName() + "' no longer exists");
              isCreated = true;
              // Insert
              values.put(nameField, object.getName());
              // We only need the general key because this is new.
              performInsert(values, null);
            }

            // Write secondary table stuff
            throttleSpecManager.writeRows(object.getName(), object);

            // If notification required, do it.
            if (notificationNeeded) {
              IJobManager jobManager = JobManagerFactory.make(threadContext);
              jobManager.noteConnectionChange(object.getName());
            }

            cacheManager.invalidateKeys(ch);
            return isCreated;
          } catch (ManifoldCFException e) {
            signalRollback();
            throw e;
          } catch (Error e) {
            signalRollback();
            throw e;
          } finally {
            endTransaction();
          }
        } finally {
          cacheManager.leaveCache(ch);
        }
      } catch (ManifoldCFException e) {
        // Is this a deadlock exception?  If so, we want to try again.
        if (e.getErrorCode() != ManifoldCFException.DATABASE_TRANSACTION_ABORT) throw e;
        sleepAmt = getSleepAmt();
      } finally {
        sleepFor(sleepAmt);
      }
    }
  }