protected String getJoin(String key, Object value) {
    String join = StringPool.BLANK;

    if (key.equals("contactTwitterSn")) {
      join = CustomSQLUtil.get(JOIN_BY_CONTACT_TWITTER_SN);
    } else if (key.equals("noOrganizations")) {
      join = CustomSQLUtil.get(JOIN_BY_NO_ORGANIZATIONS);
    } else if (key.equals("permission")) {
      join = CustomSQLUtil.get(JOIN_BY_PERMISSION);
    } else if (key.equals("userGroupRole")) {
      join = CustomSQLUtil.get(JOIN_BY_USER_GROUP_ROLE);
    } else if (key.equals("usersGroups")) {
      join = CustomSQLUtil.get(JOIN_BY_USERS_GROUPS);
    } else if (key.equals("usersOrgs")) {
      join = CustomSQLUtil.get(JOIN_BY_USERS_ORGS);
    } else if (key.equals("usersOrgsTree")) {
      join = CustomSQLUtil.get(JOIN_BY_USERS_ORGS_TREE);
    } else if (key.equals("usersPasswordPolicies")) {
      join = CustomSQLUtil.get(JOIN_BY_USERS_PASSWORD_POLICIES);
    } else if (key.equals("usersRoles")) {
      join = CustomSQLUtil.get(JOIN_BY_USERS_ROLES);
    } else if (key.equals("usersTeams")) {
      join = CustomSQLUtil.get(JOIN_BY_USERS_TEAMS);
    } else if (key.equals("usersUserGroups")) {
      join = CustomSQLUtil.get(JOIN_BY_USERS_USER_GROUPS);
    } else if (key.equals("announcementsDeliveryEmailOrSms")) {
      join = CustomSQLUtil.get(JOIN_BY_ANNOUNCEMENTS_DELIVERY_EMAIL_OR_SMS);
    } else if (key.equals("socialMutualRelation")) {
      join = CustomSQLUtil.get(JOIN_BY_SOCIAL_MUTUAL_RELATION);
    } else if (key.equals("socialMutualRelationType")) {
      join = CustomSQLUtil.get(JOIN_BY_SOCIAL_MUTUAL_RELATION_TYPE);
    } else if (key.equals("socialRelation")) {
      join = CustomSQLUtil.get(JOIN_BY_SOCIAL_RELATION);
    } else if (key.equals("socialRelationType")) {
      join = CustomSQLUtil.get(JOIN_BY_SOCIAL_RELATION_TYPE);
    } else if (value instanceof CustomSQLParam) {
      CustomSQLParam customSQLParam = (CustomSQLParam) value;

      join = customSQLParam.getSQL();
    }

    if (Validator.isNotNull(join)) {
      int pos = join.indexOf("WHERE");

      if (pos != -1) {
        join = join.substring(0, pos);
      }
    }

    return join;
  }
  protected void setJoin(QueryPos qPos, LinkedHashMap<String, Object> params) {

    if (params == null) {
      return;
    }

    for (Map.Entry<String, Object> entry : params.entrySet()) {
      String key = entry.getKey();

      if (key.equals("expandoAttributes")) {
        continue;
      }

      Object value = entry.getValue();

      if (key.equals("usersOrgsTree")) {
        List<Organization> organizationsTree = (List<Organization>) value;

        if (!organizationsTree.isEmpty()) {
          for (Organization organization : organizationsTree) {
            StringBundler treePath = new StringBundler(5);

            treePath.append(StringPool.PERCENT);
            treePath.append(StringPool.SLASH);
            treePath.append(organization.getOrganizationId());
            treePath.append(StringPool.SLASH);
            treePath.append(StringPool.PERCENT);

            qPos.add(treePath.toString());
          }
        }
      } else if (value instanceof Long) {
        Long valueLong = (Long) value;

        if (Validator.isNotNull(valueLong)) {
          qPos.add(valueLong);
        }
      } else if (value instanceof Long[]) {
        Long[] valueArray = (Long[]) value;

        for (Long element : valueArray) {
          if (Validator.isNotNull(element)) {
            qPos.add(element);
          }
        }
      } else if (value instanceof Long[][]) {
        Long[][] valueDoubleArray = (Long[][]) value;

        for (Long[] valueArray : valueDoubleArray) {
          for (Long valueLong : valueArray) {
            qPos.add(valueLong);
          }
        }
      } else if (value instanceof String) {
        String valueString = (String) value;

        if (Validator.isNotNull(valueString)) {
          qPos.add(valueString);
        }
      } else if (value instanceof String[]) {
        String[] valueArray = (String[]) value;

        for (String element : valueArray) {
          if (Validator.isNotNull(element)) {
            qPos.add(element);
          }
        }
      } else if (value instanceof CustomSQLParam) {
        CustomSQLParam customSQLParam = (CustomSQLParam) value;

        customSQLParam.process(qPos);
      }
    }
  }
  protected String getWhere(String key, Object value) {
    String join = StringPool.BLANK;

    if (key.equals("contactTwitterSn")) {
      join = CustomSQLUtil.get(JOIN_BY_CONTACT_TWITTER_SN);
    } else if (key.equals("noOrganizations")) {
      join = CustomSQLUtil.get(JOIN_BY_NO_ORGANIZATIONS);
    } else if (key.equals("permission")) {
      join = CustomSQLUtil.get(JOIN_BY_PERMISSION);
    } else if (key.equals("userGroupRole")) {
      join = CustomSQLUtil.get(JOIN_BY_USER_GROUP_ROLE);
    } else if (key.equals("usersGroups")) {
      join = CustomSQLUtil.get(JOIN_BY_USERS_GROUPS);
    } else if (key.equals("usersOrgs")) {
      if (value instanceof Long) {
        join = CustomSQLUtil.get(JOIN_BY_USERS_ORGS);
      } else if (value instanceof Long[]) {
        Long[] organizationIds = (Long[]) value;

        if (organizationIds.length == 0) {
          join = "WHERE ((Users_Orgs.organizationId = -1) ))";
        } else {
          StringBundler sb = new StringBundler(organizationIds.length * 2 + 1);

          sb.append("WHERE (");

          for (int i = 0; i < organizationIds.length; i++) {
            sb.append("(Users_Orgs.organizationId = ?) ");

            if ((i + 1) < organizationIds.length) {
              sb.append("OR ");
            }
          }

          sb.append(")");

          join = sb.toString();
        }
      }
    } else if (key.equals("usersOrgsTree")) {
      List<Organization> organizationsTree = (List<Organization>) value;

      int size = organizationsTree.size();

      if (size > 0) {
        StringBundler sb = new StringBundler(size * 2 + 1);

        sb.append("WHERE (");

        for (int i = 0; i < size; i++) {
          sb.append("(Organization_.treePath LIKE ?) ");

          if ((i + 1) < size) {
            sb.append("OR ");
          }
        }

        sb.append(")");

        join = sb.toString();
      }
    } else if (key.equals("usersPasswordPolicies")) {
      join = CustomSQLUtil.get(JOIN_BY_USERS_PASSWORD_POLICIES);
    } else if (key.equals("usersRoles")) {
      join = CustomSQLUtil.get(JOIN_BY_USERS_ROLES);
    } else if (key.equals("usersTeams")) {
      join = CustomSQLUtil.get(JOIN_BY_USERS_TEAMS);
    } else if (key.equals("usersUserGroups")) {
      join = CustomSQLUtil.get(JOIN_BY_USERS_USER_GROUPS);
    } else if (key.equals("announcementsDeliveryEmailOrSms")) {
      join = CustomSQLUtil.get(JOIN_BY_ANNOUNCEMENTS_DELIVERY_EMAIL_OR_SMS);
    } else if (key.equals("socialMutualRelation")) {
      join = CustomSQLUtil.get(JOIN_BY_SOCIAL_MUTUAL_RELATION);
    } else if (key.equals("socialMutualRelationType")) {
      join = CustomSQLUtil.get(JOIN_BY_SOCIAL_MUTUAL_RELATION_TYPE);
    } else if (key.equals("socialRelation")) {
      join = CustomSQLUtil.get(JOIN_BY_SOCIAL_RELATION);
    } else if (key.equals("socialRelationType")) {
      join = CustomSQLUtil.get(JOIN_BY_SOCIAL_RELATION_TYPE);
    } else if (value instanceof CustomSQLParam) {
      CustomSQLParam customSQLParam = (CustomSQLParam) value;

      join = customSQLParam.getSQL();
    }

    if (Validator.isNotNull(join)) {
      int pos = join.indexOf("WHERE");

      if (pos != -1) {
        join = join.substring(pos + 5, join.length()).concat(" AND ");
      } else {
        join = StringPool.BLANK;
      }
    }

    return join;
  }