/** Merges the two provided arguments into one AND filter in the most efficient way. */
 public static ObjectFilter filterAnd(ObjectFilter origFilter, ObjectFilter additionalFilter) {
   if (origFilter == additionalFilter) {
     // AND with itself
     return origFilter;
   }
   if (origFilter == null) {
     return additionalFilter;
   }
   if (additionalFilter == null) {
     return origFilter;
   }
   if (origFilter instanceof NoneFilter) {
     return origFilter;
   }
   if (additionalFilter instanceof NoneFilter) {
     return additionalFilter;
   }
   if (origFilter instanceof AllFilter) {
     return additionalFilter;
   }
   if (additionalFilter instanceof AllFilter) {
     return origFilter;
   }
   if (origFilter instanceof AndFilter) {
     if (!((AndFilter) origFilter).contains(additionalFilter)) {
       ((AndFilter) origFilter).addCondition(additionalFilter);
     }
     return origFilter;
   }
   return AndFilter.createAnd(origFilter, additionalFilter);
 }
  public static ObjectFilter simplify(ObjectFilter filter) {
    if (filter == null) {
      return null;
    }
    if (filter instanceof AndFilter) {
      List<ObjectFilter> conditions = ((AndFilter) filter).getConditions();
      AndFilter simplifiedFilter = ((AndFilter) filter).cloneEmpty();
      for (ObjectFilter subfilter : conditions) {
        if (subfilter instanceof NoneFilter) {
          // AND with "false"
          return NoneFilter.createNone();
        } else if (subfilter instanceof AllFilter) {
          // AND with "true", just skip it
        } else {
          ObjectFilter simplifiedSubfilter = simplify(subfilter);
          simplifiedFilter.addCondition(simplifiedSubfilter);
        }
      }
      if (simplifiedFilter.isEmpty()) {
        return AllFilter.createAll();
      }
      return simplifiedFilter;

    } else if (filter instanceof OrFilter) {
      List<ObjectFilter> conditions = ((OrFilter) filter).getConditions();
      OrFilter simplifiedFilter = ((OrFilter) filter).cloneEmpty();
      for (ObjectFilter subfilter : conditions) {
        if (subfilter instanceof NoneFilter) {
          // OR with "false", just skip it
        } else if (subfilter instanceof AllFilter) {
          // OR with "true"
          return AllFilter.createAll();
        } else {
          ObjectFilter simplifiedSubfilter = simplify(subfilter);
          simplifiedFilter.addCondition(simplifiedSubfilter);
        }
      }
      if (simplifiedFilter.isEmpty()) {
        return AllFilter.createAll();
      }
      return simplifiedFilter;

    } else if (filter instanceof NotFilter) {
      ObjectFilter subfilter = ((NotFilter) filter).getFilter();
      ObjectFilter simplifiedSubfilter = simplify(subfilter);
      if (subfilter instanceof NoneFilter) {
        return AllFilter.createAll();
      } else if (subfilter instanceof AllFilter) {
        return NoneFilter.createNone();
      } else {
        NotFilter simplifiedFilter = ((NotFilter) filter).cloneEmpty();
        simplifiedFilter.setFilter(simplifiedSubfilter);
        return simplifiedFilter;
      }
    } else {
      // Cannot simplify
      return filter.clone();
    }
  }
 private ObjectQuery getAssociationsSearchQuery(
     PrismContext prismContext,
     PrismObject resource,
     QName objectClass,
     ShadowKindType kind,
     String intent) {
   try {
     ObjectFilter andFilter =
         AndFilter.createAnd(
             EqualFilter.createEqual(
                 ShadowType.F_OBJECT_CLASS, ShadowType.class, prismContext, objectClass),
             EqualFilter.createEqual(ShadowType.F_KIND, ShadowType.class, prismContext, kind),
             //                    EqualFilter.createEqual(ShadowType.F_INTENT, ShadowType.class,
             // prismContext, intent),
             RefFilter.createReferenceEqual(
                 new ItemPath(ShadowType.F_RESOURCE_REF),
                 ShadowType.class,
                 prismContext,
                 resource.getOid()));
     ObjectQuery query = ObjectQuery.createObjectQuery(andFilter);
     return query;
   } catch (SchemaException ex) {
     LoggingUtils.logUnexpectedException(LOGGER, "Unable to create associations search query", ex);
     return null;
   }
 }
 public static ObjectFilter createResourceAndObjectClassFilter(
     String resourceOid, QName objectClass, PrismContext prismContext) throws SchemaException {
   Validate.notNull(resourceOid, "Resource where to search must not be null.");
   Validate.notNull(objectClass, "Object class to search must not be null.");
   Validate.notNull(prismContext, "Prism context must not be null.");
   AndFilter and =
       AndFilter.createAnd(
           createResourceFilter(resourceOid, prismContext),
           createObjectClassFilter(objectClass, prismContext));
   return and;
 }
  private ObjectQuery createQuery() {
    ReportOutputSearchDto dto = searchModel.getObject();

    try {
      List<ObjectFilter> ands = new ArrayList<>();

      if (StringUtils.isNotEmpty(dto.getText())) {
        PolyStringNormalizer normalizer = getPrismContext().getDefaultPolyStringNormalizer();
        String normalizedString = normalizer.normalize(dto.getText());

        SubstringFilter substring =
            SubstringFilter.createSubstring(
                ReportOutputType.F_NAME,
                ReportOutputType.class,
                getPrismContext(),
                PolyStringNormMatchingRule.NAME,
                normalizedString);
        ands.add(substring);
      }

      String oid = dto.getReportTypeMap().get(dto.getReportType());
      if (StringUtils.isNotEmpty(oid)) {
        RefFilter ref =
            RefFilter.createReferenceEqual(
                ReportOutputType.F_REPORT_REF, ReportOutputType.class, getPrismContext(), oid);
        ands.add(ref);
      }

      switch (ands.size()) {
        case 0:
          return null;
        case 1:
          return ObjectQuery.createObjectQuery(ands.get(0));
        default:
          AndFilter and = AndFilter.createAnd(ands);
          return ObjectQuery.createObjectQuery(and);
      }
    } catch (Exception e) {
      error(getString("pageCreatedReports.message.queryError") + " " + e.getMessage());
      LoggingUtils.logException(LOGGER, "Couldn't create query filter.", e);
      return null;
    }
  }