public Optional<Condition> innerApply(final ExpressionItem item) {
        Optional<Condition> result = Optional.empty();

        if (item != null && item.enabled()) {
            if (item instanceof final ExpressionTerm term) {
                final String fieldName = term.getField();
                if (fieldName == null) {
                    throw new NullPointerException("Term has a null field '" + term + "'");
                }
                if (term.getCondition() == null) {
                    throw new NullPointerException("Term has a null condition '" + term + "'");
                }

                final Function<ExpressionTerm, Condition> termHandler = termHandlers.get(fieldName);
                if (termHandler != null) {
                    final QueryField abstractField = fieldMap.get(fieldName);
                    Objects.requireNonNull(abstractField, () -> LogUtil.message(
                            "abstractField should not be null if we have a termHandler. term: {}", term));

                    // Fields are defined with a list of supported conditions but the code seems to be using
                    // un-supported conditions.
                    if (!abstractField.supportsCondition(term.getCondition())) {
                        LOGGER.debug(() -> LogUtil.message(
                                "Condition '{}' is not supported by field '{}' of type {}. Term: {}",
                                term.getCondition(), fieldName, abstractField.getFldType().getTypeName(), term));
                        if (FieldType.DOC_REF.equals(abstractField.getFldType())) {
                            // https://github.com/gchq/stroom/issues/3074 removed some conditions from DocRefField
                            // instances so log an error
                            LOGGER.error("Condition '{}' is not supported by field '{}' of type {}. Term: {}",
                                    term.getCondition(), fieldName, abstractField.getFldType(), term);
                        }
                    }

                    result = Optional.of(termHandler.apply(term));
                } else if (delegateItemHandler != null) {
                    result = Optional.of(delegateItemHandler.apply(term));
                } else if (!ignoredFields.contains(term.getField())) {
                    throw new RuntimeException("No term handler supplied for term '" + term.getField() + "'");
                }

            } else if (item instanceof final ExpressionOperator operator) {
                List<ExpressionItem> items = operator.getChildren();
                if (items == null) {
                    items = Collections.emptyList();
                }

                final List<Condition> children = items
                        .stream()
                        .map(this::innerApply)
                        .filter(Optional::isPresent)
                        .map(Optional::get)
                        .toList();

                if (children.size() == 1) {
                    final Condition child = children.getFirst();
                    if (Op.NOT.equals(operator.op())) {
                        result = Optional.of(DSL.not(child));
                    } else {
                        result = Optional.of(child);
                    }

                } else if (!children.isEmpty()) {
                    if (Op.NOT.equals(operator.op())) {
                        result = Optional.of(DSL.not(DSL.and(children)));
                    } else if (Op.AND.equals(operator.op())) {
                        result = Optional.of(DSL.and(children));
                    } else if (Op.OR.equals(operator.op())) {
                        result = Optional.of(DSL.or(children));
                    }
                }
            }
        }

        LOGGER.debug("Converted expressionItem {} into condition {}", item, result);
        return result;
    }
Converts the passed {@link ExpressionItem} into a Jooq {@link Condition}. By default it
will simplify expressions that can be simplified, e.g. NOT {NOT{}} becomes true, an OR
with one child that is true becomes true, etc. It will always return a value.
stroom-db-util/src/main/java/stroom/db/util/CommonExpressionMapper.java