private void initNextConceptualRelation(int relationTypeId, int artifactAId, int artiafctBId)
     throws OseeCoreException {
   obsoleteGammas.clear();
   previousRelationTypeId = relationTypeId;
   previousArtifactAId = artifactAId;
   previousArtiafctBId = artiafctBId;
   netGamma = chStmt.getInt("gamma_id");
   netRationale = chStmt.getString("rationale");
   materiallyDifferent = false;
 }
 private void relationMateriallyDiffers(IOseeStatement chStmt) throws OseeCoreException {
   if (!materiallyDifferent) {
     String currentRationale = chStmt.getString("rationale");
     materiallyDifferent |=
         Strings.isValid(currentRationale) && !currentRationale.equals(netRationale);
   }
 }
  private void determineAffectedAddressing() throws OseeCoreException {
    gammaJoin.store();

    try {
      console.writeln("counter: [%s]", counter);
      console.writeln("query id: [%s]", gammaJoin.getQueryId());
      chStmt.runPreparedQuery(10000, SELECT_RELATION_ADDRESSING, gammaJoin.getQueryId());

      while (chStmt.next()) {
        long obsoleteGammaId = chStmt.getLong("gamma_id");
        int transactionId = chStmt.getInt("transaction_id");
        long netGammaId = chStmt.getLong("net_gamma_id");
        int modType = chStmt.getInt("mod_type");
        TxChange txCurrent = TxChange.getChangeType(chStmt.getInt("tx_current"));

        if (isNextAddressing(netGammaId, transactionId)) {
          if (updateAddressing) {
            updateAddressingData.add(
                new Object[] {
                  previousNetGammaId,
                  netModType.getValue(),
                  netTxCurrent.getValue(),
                  previousTransactionId,
                  previousObsoleteGammaId
                });
          }
          updateAddressing = obsoleteGammaId != netGammaId;
          previousNetGammaId = netGammaId;
          previousObsoleteGammaId = obsoleteGammaId;
          previousTransactionId = transactionId;
          netModType = ModificationType.getMod(modType);
          netTxCurrent = txCurrent;
        } else {
          addressingToDelete.add(
              new Object[] {chStmt.getInt("branch_id"), transactionId, obsoleteGammaId});
          computeNetAddressing(ModificationType.getMod(modType), txCurrent);
        }

        writeAddressingBackup(obsoleteGammaId, transactionId, netGammaId, modType, txCurrent);
      }
    } finally {
      chStmt.close();
    }
    gammaJoin.delete();
  }
  private void findObsoleteRelations() throws OseeCoreException {
    try {
      chStmt.runPreparedQuery(10000, SELECT_RELATIONS);
      while (chStmt.next()) {
        int relationTypeId = chStmt.getInt("rel_link_type_id");
        int artifactAId = chStmt.getInt("a_art_id");
        int artiafctBId = chStmt.getInt("b_art_id");

        if (isNextConceptualRelation(relationTypeId, artifactAId, artiafctBId)) {
          consolidate();
          initNextConceptualRelation(relationTypeId, artifactAId, artiafctBId);
        } else {
          obsoleteGammas.add(chStmt.getLong("gamma_id"));
          relationMateriallyDiffers(chStmt);
        }
      }
    } finally {
      chStmt.close();
    }
  }
  @Override
  public ArrayList<ChangeBuilder> acquireChanges() throws OseeCoreException {
    Map<Integer, ChangeBuilder> attributesWasValueCache = new HashMap<Integer, ChangeBuilder>();
    Map<Integer, ModificationType> artModTypes = new HashMap<Integer, ModificationType>();
    Set<Integer> modifiedArtifacts = new HashSet<Integer>();
    IOseeStatement chStmt = ConnectionHandler.getStatement();
    boolean hasBranch = getSourceBranch() != null;
    long time = System.currentTimeMillis();
    try {
      if (getMonitor() != null) {
        getMonitor().subTask("Gathering Attribute Changes");
      }
      TransactionRecord fromTransactionId;
      TransactionRecord toTransaction;
      boolean hasSpecificArtifact = getSpecificArtifact() != null;

      for (ChangeBuilder changeBuilder :
          getChangeBuilders()) { // cache in map for performance look ups
        artModTypes.put(changeBuilder.getArtId(), changeBuilder.getModType());
      }
      // Changes per a branch
      if (hasBranch) {
        fromTransactionId = getSourceBranch().getBaseTransaction();
        toTransaction = TransactionManager.getHeadTransaction(getSourceBranch());
        chStmt.runPreparedQuery(
            ClientSessionManager.getSql(OseeSql.CHANGE_BRANCH_ATTRIBUTE_IS),
            getSourceBranch().getId(),
            fromTransactionId.getId());

      } // Changes per transaction number
      else {
        toTransaction = getTransaction();
        if (hasSpecificArtifact) {
          chStmt.runPreparedQuery(
              ClientSessionManager.getSql(OseeSql.CHANGE_TX_ATTRIBUTE_IS_FOR_SPECIFIC_ARTIFACT),
              toTransaction.getBranchId(),
              toTransaction.getId(),
              getSpecificArtifact().getArtId());
          fromTransactionId = getTransaction();
        } else {
          chStmt.runPreparedQuery(
              ClientSessionManager.getSql(OseeSql.CHANGE_TX_ATTRIBUTE_IS),
              toTransaction.getBranchId(),
              toTransaction.getId());
          fromTransactionId = TransactionManager.getPriorTransaction(toTransaction);
        }
      }
      loadIsValues(
          getSourceBranch(),
          getArtIds(),
          getChangeBuilders(),
          getNewAndDeletedArtifactIds(),
          getMonitor(),
          attributesWasValueCache,
          artModTypes,
          modifiedArtifacts,
          chStmt,
          hasBranch,
          time,
          fromTransactionId,
          toTransaction,
          hasSpecificArtifact);
      loadAttributeWasValues(
          getSourceBranch(),
          getTransaction(),
          getArtIds(),
          getMonitor(),
          attributesWasValueCache,
          hasBranch);
    } finally {
      chStmt.close();
    }
    return getChangeBuilders();
  }
  private void loadAttributeWasValues(
      Branch sourceBranch,
      TransactionRecord transactionId,
      Set<Integer> artIds,
      IProgressMonitor monitor,
      Map<Integer, ChangeBuilder> attributesWasValueCache,
      boolean hasBranch)
      throws OseeCoreException, OseeDataStoreException {
    if (!artIds.isEmpty()) {
      int sqlParamter; // Will either be a branch id or transaction id
      Branch wasValueBranch;
      String sql;

      if (hasBranch) {
        wasValueBranch = sourceBranch;
        sql = ClientSessionManager.getSql(OseeSql.CHANGE_BRANCH_ATTRIBUTE_WAS);
        sqlParamter = wasValueBranch.getId();
      } else {
        wasValueBranch = transactionId.getBranch();
        sql = ClientSessionManager.getSql(OseeSql.CHANGE_TX_ATTRIBUTE_WAS);
        sqlParamter = transactionId.getId();
      }

      int queryId = ArtifactLoader.getNewQueryId();
      Timestamp insertTime = GlobalTime.GreenwichMeanTimestamp();
      List<Object[]> datas = new LinkedList<Object[]>();
      IOseeStatement chStmt = ConnectionHandler.getStatement();

      try {
        // insert into the artifact_join_table
        for (int artId : artIds) {
          datas.add(
              new Object[] {
                queryId, insertTime, artId, wasValueBranch.getId(), SQL3DataType.INTEGER
              });
        }
        ArtifactLoader.insertIntoArtifactJoin(datas);
        chStmt.runPreparedQuery(sql, sqlParamter, queryId);
        int previousAttrId = -1;

        while (chStmt.next()) {
          int attrId = chStmt.getInt("attr_id");

          if (previousAttrId != attrId) {
            String wasValue = chStmt.getString("was_value");
            if (attributesWasValueCache.containsKey(attrId)
                && attributesWasValueCache.get(attrId) instanceof AttributeChangeBuilder) {
              AttributeChangeBuilder changeBuilder =
                  (AttributeChangeBuilder) attributesWasValueCache.get(attrId);

              if (changeBuilder.getArtModType() != ModificationType.NEW) {
                if (changeBuilder.getModType() != ModificationType.DELETED
                    && changeBuilder.getModType() != ModificationType.ARTIFACT_DELETED) {
                  changeBuilder.setModType(ModificationType.MODIFIED);
                }
                changeBuilder.setWasValue(wasValue);
              }
            }
            previousAttrId = attrId;
          }
        }
      } finally {
        ArtifactLoader.clearQuery(queryId);
        chStmt.close();
      }
      if (getMonitor() != null) {
        monitor.worked(12);
      }
    }
  }
  private void loadIsValues(
      Branch sourceBranch,
      Set<Integer> artIds,
      ArrayList<ChangeBuilder> changeBuilders,
      Set<Integer> newAndDeletedArtifactIds,
      IProgressMonitor monitor,
      Map<Integer, ChangeBuilder> attributesWasValueCache,
      Map<Integer, ModificationType> artModTypes,
      Set<Integer> modifiedArtifacts,
      IOseeStatement chStmt,
      boolean hasBranch,
      long time,
      TransactionRecord fromTransactionId,
      TransactionRecord toTransactionId,
      boolean hasSpecificArtifact)
      throws OseeCoreException {
    ModificationType artModType;
    AttributeChangeBuilder attributeChangeBuilder;

    try {
      TransactionDelta txDelta = new TransactionDelta(fromTransactionId, toTransactionId);

      while (chStmt.next()) {
        int attrId = chStmt.getInt("attr_id");
        int artId = chStmt.getInt("art_id");
        int sourceGamma = chStmt.getInt("gamma_id");
        int attrTypeId = chStmt.getInt("attr_type_id");
        int artTypeId = chStmt.getInt("art_type_id");
        String isValue = chStmt.getString("is_value");
        ModificationType modificationType = ModificationType.getMod(chStmt.getInt("mod_type"));

        if (artModTypes.containsKey(artId)) {
          artModType = artModTypes.get(artId);
        } else {
          artModType = ModificationType.MODIFIED;
        }

        // This will be false iff the artifact was new and then deleted
        if (!newAndDeletedArtifactIds.contains(artId)) {
          // Want to add an artifact changed item once if any attribute was modified && artifact was
          // not
          // NEW or DELETED and these changes are not for a specific artifact
          if (artModType == ModificationType.MODIFIED && !modifiedArtifacts.contains(artId)) {

            ArtifactChangeBuilder artifactChangeBuilder =
                new ArtifactChangeBuilder(
                    sourceBranch,
                    ArtifactTypeManager.getType(artTypeId),
                    -1,
                    artId,
                    txDelta,
                    ModificationType.MODIFIED,
                    !hasBranch);

            changeBuilders.add(artifactChangeBuilder);
            modifiedArtifacts.add(artId);
          }

          // ModTypes will be temporarily set to new and then revised for based on the existence of
          // a was value
          if (modificationType == ModificationType.MODIFIED
              && artModType != ModificationType.INTRODUCED) {
            modificationType = ModificationType.NEW;
          }
          IArtifactType artifactType = ArtifactTypeManager.getType(artTypeId);
          AttributeType attributeType = AttributeTypeManager.getType(attrTypeId);
          attributeChangeBuilder =
              new AttributeChangeBuilder(
                  sourceBranch,
                  artifactType,
                  sourceGamma,
                  artId,
                  txDelta,
                  modificationType,
                  !hasBranch,
                  isValue,
                  "",
                  attrId,
                  attributeType,
                  artModType);

          changeBuilders.add(attributeChangeBuilder);
          attributesWasValueCache.put(attrId, attributeChangeBuilder);
          artIds.add(artId);
        }
      }

      if (getMonitor() != null) {
        monitor.worked(13);
        monitor.subTask("Gathering Was values");
      }
    } finally {
      chStmt.close();
    }
  }