Package com.psddev.dari.db

Examples of com.psddev.dari.db.Predicate


    public static Predicate getVisibilitiesPredicate(ObjectType selectedType, Collection<String> visibilities, Set<UUID> validTypeIds, boolean showDrafts) {

        Set<UUID> visibilityTypeIds = new HashSet<UUID>();

        Predicate visibilitiesPredicate = null;

        for (String visibility : visibilities) {
            if ("p".equals(visibility)) {
                Set<String> comparisonKeys = new HashSet<String>();
                DatabaseEnvironment environment = Database.Static.getDefault().getEnvironment();
View Full Code Here


                    }
                    break;
                }

            } else {
                Predicate predicate = null;

                for (ObjectType type : validTypes) {
                    String prefix = type.getInternalName() + "/";

                    for (String field : type.getLabelFields()) {
                        if (type.getIndex(field) != null) {
                            predicate = CompoundPredicate.combine(
                                    PredicateParser.OR_OPERATOR,
                                    predicate,
                                    PredicateParser.Static.parse(prefix + field + " ^=[c] ?", queryString));
                        }
                        break;
                    }
                }

                query.and(predicate);
            }
        }

        if (isOnlyPathed()) {
            query.and(Directory.Static.hasPathPredicate());
        }

        if (isAllSearchable && selectedType == null) {
            DatabaseEnvironment environment = Database.Static.getDefault().getEnvironment();
            String q = getQueryString();

            if (!ObjectUtils.isBlank(q)) {
                q = q.replaceAll("\\s+", "").toLowerCase(Locale.ENGLISH);

                for (ObjectType t : environment.getTypes()) {
                    String name = t.getDisplayName();

                    if (!ObjectUtils.isBlank(name) &&
                            q.contains(name.replaceAll("\\s+", "").toLowerCase(Locale.ENGLISH))) {
                        query.sortRelevant(20.0, "_type = ?", t.as(ToolUi.class).findDisplayTypes());
                    }
                }
            }

            query.sortRelevant(10.0, "_type = ?", environment.getTypeByClass(Singleton.class).as(ToolUi.class).findDisplayTypes());
        }

        String additionalPredicate = getAdditionalPredicate();

        if (!ObjectUtils.isBlank(additionalPredicate)) {
            Object parent = Query.
                    from(Object.class).
                    where("_id = ?", getParentId()).
                    first();
            if (parent == null && getParentTypeId() != null) {
                ObjectType parentType = ObjectType.getInstance(getParentTypeId());
                parent = parentType.createObject(null);
            }
            query.and(additionalPredicate, parent);
        }

        String advancedQuery = getAdvancedQuery();

        if (!ObjectUtils.isBlank(advancedQuery)) {
            query.and(advancedQuery);
        }

        for (String filter : getGlobalFilters().values()) {
            if (filter != null) {
                query.and("* matches ?", filter);
            }
        }

        for (Map.Entry<String, Map<String, String>> entry : getFieldFilters().entrySet()) {
            Map<String, String> value = entry.getValue();

            if (value == null) {
                continue;
            }

            String fieldName = selectedType != null ?
                    selectedType.getInternalName() + "/" + entry.getKey() :
                    entry.getKey();

            if (ObjectUtils.to(boolean.class, value.get("m"))) {
                query.and(fieldName + " = missing");

            } else {
                String fieldValue = value.get("");
                String queryType = value.get("t");

                if ("d".equals(queryType)) {
                    Date start = ObjectUtils.to(Date.class, fieldValue);
                    Date end = ObjectUtils.to(Date.class, value.get("x"));

                    if (start != null) {
                        query.and(fieldName + " >= ?", start);
                    }

                    if (end != null) {
                        query.and(fieldName + " <= ?", end);
                    }

                } else if ("n".equals(queryType)) {
                    Double minimum = ObjectUtils.to(Double.class, fieldValue);
                    Double maximum = ObjectUtils.to(Double.class, value.get("x"));

                    if (minimum != null) {
                        query.and(fieldName + " >= ?", fieldValue);
                    }

                    if (maximum != null) {
                        query.and(fieldName + " <= ?", maximum);
                    }

                } else {
                    if (fieldValue == null) {
                        continue;
                    }

                    if ("t".equals(queryType)) {
                        query.and(fieldName + " matches ?", fieldValue);

                    } else if ("b".equals(queryType)) {
                        query.and(fieldName + ("true".equals(fieldValue) ? " = true" : " != true"));

                    } else {
                        query.and(fieldName + " = ?", fieldValue);
                    }
                }
            }
        }

        if (site != null &&
                !site.isAllSitesAccessible()) {
            Set<ObjectType> globalTypes = new HashSet<ObjectType>();

            if (selectedType != null) {
                addGlobalTypes(globalTypes, selectedType);

            } else {
                for (ObjectType type : validTypes) {
                    addGlobalTypes(globalTypes, type);
                }
            }

            query.and(CompoundPredicate.combine(
                    PredicateParser.OR_OPERATOR,
                    site.itemsPredicate(),
                    PredicateParser.Static.parse("_type = ?", globalTypes)));
        }

        Collection<String> visibilities = getVisibilities();

        if (!visibilities.isEmpty()) {

            Predicate visibilitiesPredicate = getVisibilitiesPredicate(selectedType, visibilities, validTypeIds, isShowDrafts());

            query.and(visibilitiesPredicate);

        } else if (selectedType == null &&
                isAllSearchable) {
View Full Code Here

                return;
            }
        }

        int lastIndex = -1;
        Predicate globalPredicate = null;
        SearchAdvancedPredicate.Compound globalPredicateType = findSearchAdvancedPredicate(SearchAdvancedPredicate.Compound.class, page.param(String.class, "gpt"));

        Collections.sort(paramNames);

        page.writeHeader();
            page.writeStart("div", "class", "widget widget-searchAdvancedQuery");
                page.writeStart("h1", "class", "icon icon-wrench");
                    page.writeHtml("Advanced Query Builder");
                page.writeEnd();

                page.writeStart("style", "type", "text/css");
                    page.writeCss(".form-submit",
                            "opacity", "0.5",
                            "pointer-events", "none");
                page.writeEnd();

                page.writeStart("form",
                        "method", "get",
                        "action", page.url(null),
                        "onsubmit", "$(this).addClass('form-submit');return true;");
                    page.writeStart("select",
                            "data-bsp-autosubmit", "",
                            "name", "gpt");
                        for (SearchAdvancedPredicate.Compound pt : Query.
                                from(SearchAdvancedPredicate.Compound.class).
                                sortAscending("dari.singleton.key").
                                selectAll()) {
                            if (globalPredicateType == null) {
                                globalPredicateType = pt;
                            }

                            page.writeStart("option",
                                    "selected", pt.equals(globalPredicateType) ? "selected" : null,
                                    "value", pt.getParameterValue());
                                page.writeHtml(pt.getLabel());
                                page.writeHtml(":");
                            page.writeEnd();
                        }
                    page.writeEnd();

                    page.writeStart("div", "class", "fixedScrollable");
                        page.writeStart("ul");
                            for (String paramName : paramNames) {
                                if (paramName.startsWith("p")) {
                                    Integer index = ObjectUtils.to(Integer.class, paramName.substring(1));

                                    if (index != null) {
                                        if (lastIndex < index) {
                                            lastIndex = index;
                                        }

                                        page.writeStart("li");
                                            globalPredicate = CompoundPredicate.combine(
                                                    globalPredicateType.getOperator(),
                                                    globalPredicate,
                                                    writeSearchAdvancedPredicate(page, paramNames, paramName, String.valueOf(index)));
                                        page.writeEnd();
                                    }
                                }
                            }
                        page.writeEnd();
                    page.writeEnd();

                    page.writeStart("button",
                            "class", "icon icon-action-add link",
                            "name", "p" + (lastIndex + 1),
                            "value", 1);
                        page.writeHtml("Add Another ");
                        page.writeHtml(globalPredicateType.getLabel());
                    page.writeEnd();

                    page.writeStart("div", "class", "actions");
                        page.writeStart("button",
                                "class", "icon icon-action-search",
                                "name", "action-search",
                                "value", true);
                            page.writeHtml("Search");
                        page.writeEnd();
                    page.writeEnd();
                page.writeEnd();
            page.writeEnd();

            String pageId = page.createId();

            page.writeStart("div", "id", pageId);
            page.writeEnd();

            page.writeStart("script", "type", "text/javascript");
                page.writeRaw("var $page = $('#" + pageId + "'),");
                page.writeRaw("$edit = $page.popup('source');");

                page.writeRaw("$edit.attr('href', '");
                page.writeRaw(StringUtils.escapeJavaScript(page.url("", "action-search", null)));
                page.writeRaw("');");

                if (page.param(String.class, "action-search") != null) {
                    page.writeRaw("var $input = $edit.closest('.searchFilter-advancedQuery').find('input[type=\"text\"]');");

                    page.writeRaw("$input.val('" + StringUtils.escapeJavaScript(globalPredicate != null ? globalPredicate.toString() : "") + "');");
                    page.writeRaw("$input.change();");
                    page.writeRaw("$page.popup('close');");
                }
            page.writeEnd();
        page.writeFooter();
View Full Code Here

        return null;
    }

    public Predicate siteItemsSearchPredicate() {
        Predicate predicate = siteItemsPredicate();

        if (predicate != null) {
            predicate = CompoundPredicate.combine(
                    PredicateParser.AND_OPERATOR,
                    predicate,
View Full Code Here

            dropDownSearch.setParentTypeId(param(UUID.class, TYPE_ID_PARAMETER));

            List<?> items;
            if (field.getTypes().contains(ObjectType.getInstance(ObjectType.class))) {
                List<ObjectType> types = new ArrayList<ObjectType>();
                Predicate predicate = dropDownSearch.toQuery(getSite()).getPredicate();

                for (ObjectType t : Database.Static.getDefault().getEnvironment().getTypes()) {
                    if (t.is(predicate)) {
                        types.add(t);
                    }
View Full Code Here

            this.sortField = selectedType.getFieldGlobally(search.getSort());
            this.showTypeLabel = selectedType.as(ToolUi.class).findDisplayTypes().size() != 1;

            if (ObjectType.getInstance(ObjectType.class).equals(selectedType)) {
                List<ObjectType> types = new ArrayList<ObjectType>();
                Predicate predicate = search.toQuery(page.getSite()).getPredicate();

                for (ObjectType t : Database.Static.getDefault().getEnvironment().getTypes()) {
                    if (t.is(predicate)) {
                        types.add(t);
                    }
View Full Code Here

            int nextLevel = level + 1;
            Collection<Taxon> taxonResults = null;
            UUID taxonParentUuid = page.paramOrDefault(UUID.class, TAXON_PARENT_ID_PARAMETER, null);
            Site site = page.getSite();
            Predicate predicate = search.toQuery(page.getSite()).getPredicate();

            if (!ObjectUtils.isBlank(taxonParentUuid)) {

                Taxon parent = Query.findById(Taxon.class, taxonParentUuid);
                taxonResults = (Collection<Taxon>) Taxon.Static.getChildren(parent, predicate);
View Full Code Here

                renderAfterItem(taxon);
            } else {
                writeTaxonLabel(taxon);
            }

            Predicate predicate = search.toQuery(page.getSite()).getPredicate();
            Collection<? extends Taxon> children = Taxon.Static.getChildren(taxon, predicate);

            if (children != null && !children.isEmpty()) {
                page.writeStart("a",
                        "href", page.url("", TAXON_PARENT_ID_PARAMETER, taxon.as(Taxon.Data.class).getId(), TAXON_LEVEL_PARAMETER, nextLevel),
View Full Code Here

                default :
                    break;
            }

            Predicate visibilitiesPredicate = Search.getVisibilitiesPredicate(itemType, visibilities, null, false);

            if (visibilitiesPredicate != null) {
                contentQuery.and(visibilitiesPredicate);
            }
View Full Code Here

                List<String> paramNames,
                String predicateParam,
                String paramPrefix)
                throws IOException {

            Predicate predicate = null;
            String subPredicateIndexParam = paramPrefix + ".p";
            int lastSubPredicateIndex = -1;

            page.writeStart("ul");
                for (String paramName : paramNames) {
View Full Code Here

TOP

Related Classes of com.psddev.dari.db.Predicate

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.