}
@Override
protected void doService(final ToolPageContext page) throws IOException, ServletException {
String type = page.pageParam(String.class, "type", null);
final ObjectType itemType = Query.findById(ObjectType.class, page.pageParam(UUID.class, "itemType", null));
long offset = page.param(long.class, "offset");
int limit = page.pageParam(Integer.class, "limit", 20);
List<ObjectType> types = new ArrayList<ObjectType>();
boolean typeFound = false;
for (ObjectType t : Database.Static.getDefault().getEnvironment().getTypes()) {
if (t.as(ToolUi.class).isGlobalFilter()) {
types.add(t);
if (type != null && type.equals(t.getId().toString())) {
typeFound = true;
}
}
}
Collections.sort(types);
if (!typeFound) {
type = URL_TYPE;
}
String valueParameter = type + ".value";
final String value = page.pageParam(String.class, valueParameter, null);
Object valueObject = Query.from(Object.class).where("_id = ?", value).first();
PaginatedResult<?> result = null;
Long count = null;
if (type.equals(URL_TYPE)) {
if (!(valueObject instanceof Directory)) {
valueObject = Query.from(Directory.class).where("path = /").first();
}
if (valueObject != null) {
result = new AggregateQueryResult<Object>(offset, limit, new DirectoryQueryIterator(Query.
from(Directory.class).
where("path startsWith ?", ((Directory) valueObject).getPath()).
sortAscending("path")) {
@Override
protected Query<?> createQuery(Directory directory) {
return (itemType != null ? Query.fromType(itemType) : Query.fromAll()).
and(page.siteItemsSearchPredicate()).
and(directory.itemsPredicate(page.getSite())).
sortAscending(Directory.PATHS_FIELD);
}
});
count = null;
}
} else if (valueObject != null) {
Query<?> query = (itemType != null ? Query.fromType(itemType) : Query.fromAll()).
and(page.siteItemsPredicate()).
and("* matches ?", value).
and("cms.directory.paths != missing");
if (query.hasMoreThan(250)) {
result = new AggregateQueryResult<Object>(offset, limit, new DirectoryQueryIterator(Query.
from(Directory.class).
where("path startsWith /").
sortAscending("path")) {
@Override
protected Query<?> createQuery(Directory directory) {
return (itemType != null ? Query.fromType(itemType) : Query.fromAll()).
and(page.siteItemsPredicate()).
and(directory.itemsPredicate(page.getSite())).
and("* matches ?", value).
and("cms.directory.paths != missing");
}
});
count = query.count();
} else {
@SuppressWarnings("unchecked")
List<Object> items = (List<Object>) query.selectAll();
Collections.sort(items, new Comparator<Object>() {
@Override
public int compare(Object x, Object y) {
return ObjectUtils.compare(
State.getInstance(x).as(Directory.ObjectModification.class).getPermalink(),
State.getInstance(y).as(Directory.ObjectModification.class).getPermalink(),
true);
}
});
result = new PaginatedResult<Object>(offset, limit, items);
count = (long) items.size();
}
}
if (count == null && result != null && result.getFirstItemIndex() == 1 && !result.hasNext()) {
count = (long) result.getItems().size();
}
page.writeStart("div", "class", "widget");
page.writeStart("h1", "class", "icon icon-sitemap");
page.writeHtml("Sitemap");
page.writeEnd();
page.writeStart("form",
"method", "get",
"action", page.url(null));
page.writeStart("ul", "class", "oneLine");
page.writeStart("li");
page.writeTypeSelect(
Template.Static.findUsedTypes(page.getSite()),
itemType,
"Any Types",
"name", "itemType",
"data-bsp-autosubmit", "",
"data-searchable", "true");
page.writeEnd();
if (types.isEmpty()) {
page.writeElement("input",
"type", "hidden",
"name", "type",
"value", URL_TYPE);
} else {
page.writeStart("li");
page.writeHtml("with ");
page.writeStart("select",
"data-bsp-autosubmit", "",
"name", "type");
page.writeStart("option",
"selected", type.equals(URL_TYPE) ? "selected" : null,
"value", URL_TYPE);
page.writeHtml("URL");
page.writeEnd();
for (ObjectType t : types) {
String id = t.getId().toString();
page.writeStart("option",
"selected", type.equals(id) ? "selected" : null,
"value", id);
page.writeHtml(t.getDisplayName());
page.writeEnd();
}
page.writeEnd();
page.writeEnd();
}
page.writeStart("li");
page.writeHtml("in ");
Query<?> valueQuery;
ObjectType valueType;
if (type.equals(URL_TYPE)) {
valueType = ObjectType.getInstance(Directory.class);
valueQuery = Query.from(Directory.class).sortAscending("path");
valueQuery.where("path != missing");
} else {
valueType = ObjectType.getInstance(ObjectUtils.to(UUID.class, type));
valueQuery = Query.fromType(valueType);
for (String fieldName : valueType.getLabelFields()) {
ObjectField field = valueType.getField(fieldName);
if (field != null && valueType.getIndex(fieldName) != null) {
valueQuery.sortAscending(valueType.getInternalName() + "/" + fieldName);
}
}
}
if (valueQuery.hasMoreThan(250)) {
page.writeElement("input",
"type", "text",
"class", "objectId",
"data-bsp-autosubmit", "",
"data-editable", false,
"data-label", valueObject != null ? State.getInstance(valueObject).getLabel() : null,
"data-typeIds", valueType.getId(),
"name", valueParameter,
"value", value);
} else {
page.writeStart("select",
"name", valueParameter,
"data-bsp-autosubmit", "",
"data-searchable", "true");
if (!type.equals(URL_TYPE)) {
page.writeStart("option", "value", "").writeEnd();
}
for (Object v : valueQuery.selectAll()) {
State state = State.getInstance(v);
page.writeStart("option",
"value", state.getId(),
"selected", v.equals(valueObject) ? "selected" : null);
page.writeHtml(state.getLabel());
page.writeEnd();
}
page.writeEnd();
}
page.writeEnd();
page.writeEnd();
page.writeEnd();
if (valueObject == null) {
page.writeStart("div", "class", "message message-warning");
page.writeStart("p");
page.writeHtml("Please select a ");
page.writeStart("strong").writeHtml(valueType.getLabel()).writeEnd();
page.writeHtml(".");
page.writeEnd();
page.writeEnd();
} else if (!result.hasPages()) {