/* (non-Javadoc)
   * @see edu.ku.brc.af.core.expresssearch.QueryAdjusterForDomain#getJoinClause(edu.ku.brc.dbsupport.DBTableInfo, boolean)
   */
  @Override
  public String getJoinClause(
      DBTableInfo tableInfo, boolean isHQL, final String aliasArg, boolean useLeftJoin) {
    String alias;
    if (aliasArg == null) {
      alias = tableInfo.getAbbrev();
    } else {
      alias = aliasArg;
    }
    String join;
    if (useLeftJoin) {
      join = "left join ";
    } else if (isHQL) {
      join = "join ";
    } else {
      join = "inner join ";
    }

    if (tableInfo.getTableId() == Agent.getClassTableId()
        || tableInfo.getTableId() == Accession.getClassTableId()
        || tableInfo.getTableId() == RepositoryAgreement.getClassTableId()
        || tableInfo.getTableId() == ExchangeIn.getClassTableId()
        || tableInfo.getTableId() == ExchangeOut.getClassTableId()) {
      if (isHQL) {
        return join + alias + ".division as dv" + (aliasArg == null ? "" : alias);
      }
      if (aliasArg != null) {
        return "";
        // throw new RuntimeException("SpecifyQueryAdjuster.getJoinClause does not work for SQL with
        // non-null alias.");
      }
      return join;

    } else if (tableInfo.getRelationshipByName("discipline") != null) {
      DBRelationshipInfo ri = tableInfo.getRelationshipByName("discipline");
      if (ri.getType() != DBRelationshipInfo.RelationshipType.OneToOne) {
        if (isHQL) {
          return join + alias + ".discipline as dsp" + (aliasArg == null ? "" : alias);
        }
        // if (aliasArg != null)
        // {
        // throw new RuntimeException("SpecifyQueryAdjuster.getJoinClause does not work for SQL with
        // non-null alias.");
        // }
        return join + "discipline as dsp ON " + aliasArg + ".DisciplineID = dsp.DisciplineID";
      }
    }
    return super.getJoinClause(tableInfo, isHQL, alias, useLeftJoin);
  }
  /* (non-Javadoc)
   * @see edu.ku.brc.af.core.expresssearch.QueryAdjusterForDomain#getSpecialColumns(edu.ku.brc.dbsupport.DBTableInfo, boolean, boolean, java.lang.String)
   */
  @Override
  public String getSpecialColumns(
      final DBTableInfo tableInfo,
      final boolean isHQL,
      final boolean isLeftJoin,
      final String tblAlias) {
    if (tableInfo != null) {
      String prefix = tblAlias == null ? "" : tblAlias + ".";
      String criterion = null;
      String fld = null;
      boolean adjustFldToSQL = true;

      if (tableInfo.getFieldByName("collectionMemberId") != null) {
        fld = isHQL ? "collectionMemberId" : "CollectionMemberId";
        criterion = COLMEMID;

      } else if (tableInfo.getTableId() == Agent.getClassTableId()
          || tableInfo.getTableId() == Accession.getClassTableId()
          || tableInfo.getTableId() == RepositoryAgreement.getClassTableId()
          || tableInfo.getTableId() == ExchangeIn.getClassTableId()
          || tableInfo.getTableId() == ExchangeOut.getClassTableId()) {
        if (prefix.equals("")) {
          prefix = isHQL ? "dv." : "";
        } else {
          prefix = isHQL ? ("dv" + prefix) : prefix;
        }
        fld = isHQL ? "divisionId" : "DivisionID";
        criterion = DIVID;

      } else if (tableInfo.getRelationshipByName("discipline") != null) {
        DBRelationshipInfo ri = tableInfo.getRelationshipByName("discipline");
        if (ri.getType() != DBRelationshipInfo.RelationshipType.OneToOne) {
          if (prefix.equals("")) {
            // rods - 9/29/08 - This was messing up the Geography Count (SQL) but is needed for HQL
            prefix = isHQL ? "dsp." : "";
          } else {
            prefix = isHQL ? ("dsp" + prefix) : prefix;
          }
          fld = isHQL ? "disciplineId" : "DisciplineID";
          criterion = DSPLNID;
        }

        //            } else if (tableInfo.getTableId() == DeterminationStatus.getClassTableId())
        //            {
        //                fld = isHQL ? "disciplineId" : "DisciplineID";
        //                criterion = DSPLNID;
        //
      } else if (tableInfo.getTableId() == Geography.getClassTableId()) {
        fld = isHQL ? "definition" : "GeographyTreeDefID";
        criterion = GEOTREEDEFID;

      } else if (tableInfo.getTableId() == GeologicTimePeriod.getClassTableId()) {
        fld = isHQL ? "definition" : "GeologicTimePeriodTreeDefID";
        criterion = GTPTREEDEFID;

      } else if (tableInfo.getTableId() == LithoStrat.getClassTableId()) {
        fld = isHQL ? "definition" : "LithoStratTreeDefID";
        criterion = LITHOTREEDEFID;

      } else if (tableInfo.getTableId() == Storage.getClassTableId()) {
        fld = isHQL ? "definition" : "StorageTreeDefID";
        criterion = STORTREEDEFID;

      } else if (tableInfo.getTableId() == PrepType.getClassTableId()) {
        fld = isHQL ? "collection" : "CollectionID";
        criterion = COLLID;

      } else if (tableInfo.getTableId() == Taxon.getClassTableId()) {
        fld = isHQL ? "definition" : "TaxonTreeDefID";
        criterion = TAXTREEDEFID;

      } else if (tableInfo.getTableId() == Locality.getClassTableId()) {
        fld = isHQL ? "discipline" : "DisciplineID";
        criterion = DSPLNID;

      } else if (tableInfo.getTableId() == Locality.getClassTableId()) {
        fld = isHQL ? "discipline" : "DisciplineID";
        criterion = DSPLNID;

      } else if (tableInfo.getTableId() == Locality.getClassTableId()) {
        fld = isHQL ? "discipline" : "DisciplineID";
        criterion = DSPLNID;
      } /*else if (tableInfo.getTableId() == Agent.getClassTableId())
        {
            if (StringUtils.isEmpty(prefix))
            {
                prefix = "ag.";
            }
            criterion = DIVID;
            if (isHQL)
            {
                fld = criterion + " = " + prefix + "division.id";
                adjustFldToSQL = false;
            }
            else
            {
                //this probably won't actually work without additional
                //changes to the from clause for the query
                fld = criterion + " = " + prefix + "D";
                prefix = "";
                //throw new RuntimeException("Fix me I am probably broken!");
            }
        }*/

      if (criterion != null && fld != null) {
        String sql;
        if (adjustFldToSQL) {
          sql = "(" + prefix + fld + " = " + criterion + ")";
        } else {
          sql = fld;
        }
        if (isLeftJoin) {
          if (isHQL) {
            if (tblAlias != null) {
              sql = "(" + sql + " or " + tblAlias + " is null)";
            } else {
              log.error("can't adjust hql for left join without a tblAlias.");
            }
          } else {
            sql = "(" + sql + " or " + prefix + fld + " is null)";
          }
        }
        return adjustSQL(sql);
      }
    }
    return null;
  }