}
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) {