public Filter buildFilter(DBObject ref) {
   AndFilter andFilter = new AndFilter();
   if (ref != null) {
     for (String key : ref.keySet()) {
       Object expression = ref.get(key);
       andFilter.addFilter(buildExpressionFilter(key, expression));
     }
   }
   return andFilter;
 }
 /**
  * Only build the filter for this keys.
  *
  * @param ref query for filter.
  * @param keys must match to build the filter.
  */
 public Filter buildFilter(DBObject ref, Collection<String> keys) {
   AndFilter andFilter = new AndFilter();
   for (String key : ref.keySet()) {
     if (keys.contains(key)) {
       Object expression = ref.get(key);
       andFilter.addFilter(buildExpressionFilter(key, expression));
     }
   }
   return andFilter;
 }
  private Filter buildExpressionFilter(final List<String> path, Object expression) {
    if (OR.equals(path.get(0))) {
      @SuppressWarnings("unchecked")
      Collection<DBObject> queryList = typecast(path + " operator", expression, Collection.class);
      OrFilter orFilter = new OrFilter();
      for (DBObject query : queryList) {
        orFilter.addFilter(buildFilter(query));
      }
      return orFilter;
    } else if (AND.equals(path.get(0))) {
      Collection<DBObject> queryList = typecast(path + " operator", expression, Collection.class);
      AndFilter andFilter = new AndFilter();
      for (DBObject query : queryList) {
        andFilter.addFilter(buildFilter(query));
      }
      return andFilter;
    } else if (WHERE.equals(path.get(0))) {
      return new WhereFilter((String) expression);
    } else if (expression instanceof DBObject || expression instanceof Map) {
      DBObject ref =
          expression instanceof DBObject
              ? (DBObject) expression
              : new BasicDBObject((Map) expression);

      if (ref.containsField(NOT)) {
        return new NotFilter(buildExpressionFilter(path, ref.get(NOT)));
      } else {

        AndFilter andFilter = new AndFilter();
        int matchCount = 0;
        for (FilterFactory filterFactory : filterFactories) {
          if (filterFactory.matchesCommand(ref)) {
            matchCount++;
            andFilter.addFilter(filterFactory.createFilter(path, ref));
          }
        }
        if (matchCount == 0) {
          return simpleFilter(path, expression);
        }
        if (matchCount > 2) {
          throw new FongoException("Invalid expression for key " + path + ": " + expression);
        }
        return andFilter;
      }
    } else if (expression instanceof Pattern) {
      return createPatternFilter(path, (Pattern) expression);
    } else {
      return simpleFilter(path, expression);
    }
  }
 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;
   }
 }
    @Override
    public Rule readFromXml(XmlPullParser parser) throws IOException, XmlPullParserException {
      block = Boolean.parseBoolean(parser.getAttributeValue(null, ATTR_BLOCK));
      log = Boolean.parseBoolean(parser.getAttributeValue(null, ATTR_LOG));

      super.readFromXml(parser);
      return this;
    }
  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;
    }
  }
  @Test
  public void testPresentFilter() {
    PresentFilter filter = new PresentFilter("foo");
    assertEquals("(foo=*)", filter.encode());

    NotFilter notFilter = new NotFilter(new PresentFilter("foo"));
    assertEquals("(!(foo=*))", notFilter.encode());

    AndFilter andFilter = new AndFilter();
    andFilter.and(new PresentFilter("foo"));
    andFilter.and(new PresentFilter("bar"));
    assertEquals("(&(foo=*)(bar=*))", andFilter.encode());

    andFilter = new AndFilter();
    andFilter.and(new PresentFilter("foo"));
    andFilter.and(new NotFilter(new PresentFilter("bar")));
    assertEquals("(&(foo=*)(!(bar=*)))", andFilter.encode());
  }
    @Override
    protected void readChild(XmlPullParser parser) throws IOException, XmlPullParserException {
      String currentTag = parser.getName();

      if (currentTag.equals(TAG_INTENT_FILTER)) {
        FirewallIntentFilter intentFilter = new FirewallIntentFilter(this);
        intentFilter.readFromXml(parser);
        mIntentFilters.add(intentFilter);
      } else if (currentTag.equals(TAG_COMPONENT_FILTER)) {
        String componentStr = parser.getAttributeValue(null, ATTR_NAME);
        if (componentStr == null) {
          throw new XmlPullParserException("Component name must be specified.", parser, null);
        }

        ComponentName componentName = ComponentName.unflattenFromString(componentStr);
        if (componentName == null) {
          throw new XmlPullParserException("Invalid component name: " + componentStr);
        }

        mComponentFilters.add(componentName);
      } else {
        super.readChild(parser);
      }
    }