}
@Override
protected void doService(final ToolPageContext page) throws IOException, ServletException {
DatabaseEnvironment environment = Database.Static.getDefault().getEnvironment();
ObjectType type = environment.getTypeById(page.param(UUID.class, TYPE_PARAMETER));
String predicate = page.param(String.class, PREDICATE_PARAMETER);
List<ObjectField> allFields = new ArrayList<ObjectField>();
for (String fieldName : new String[] {
"cms.content.publishDate",
"cms.content.publishUser",
"cms.content.updateDate",
"cms.content.updateUser",
"cms.directory.paths" }) {
allFields.add(environment.getField(fieldName));
}
if (type != null) {
allFields.addAll(type.getFields());
}
List<String> fieldNames = page.params(String.class, FIELDS_PARAMETER);
List<ObjectField> fields = new ArrayList<ObjectField>();
for (ObjectField field : allFields) {
if (fieldNames.contains(field.getInternalName())) {
fields.add(field);
}
}
Collections.sort(allFields);
Collections.sort(fields);
List<UUID> ids = page.params(UUID.class, ITEMS_PARAMETER);
Query<Object> query = (type != null ?
Query.fromType(type) :
Query.fromGroup(Content.SEARCHABLE_GROUP)).
where(predicate);
if (page.param(String.class, "action-download") != null) {
HttpServletResponse response = page.getResponse();
response.setContentType("text/csv");
response.setHeader("Content-Disposition", "attachment; filename=search-result-" + new DateTime(null, page.getUserDateTimeZone()).toString("yyyy-MM-dd-hh-mm-ss") + ".csv");
page.putOverride(Recordable.class, new HtmlFormatter<Recordable>() {
@Override
public void format(HtmlWriter writer, Recordable object) throws IOException {
ToolPageContext page = (ToolPageContext) writer;
writer.write(page.getObjectLabel(object));
}
});
page.putOverride(Metric.class, new HtmlFormatter<Metric>() {
@Override
public void format(HtmlWriter writer, Metric object) throws IOException {
writer.write(new Double(object.getSum()).toString());
}
});
page.putOverride(StorageItem.class, new HtmlFormatter<StorageItem>() {
@Override
public void format(HtmlWriter writer, StorageItem item) throws IOException {
writer.write(item.getPublicUrl());
}
});
page.write('\ufeff');
page.write("\"");
writeCsvItem(page, "Type");
page.write("\",\"");
writeCsvItem(page, "Label");
page.write("\"");
for (ObjectField field : fields) {
page.write(",\"");
writeCsvItem(page, field.getDisplayName());
page.write("\"");
}
page.write("\r\n");
for (Object item : query.iterable(0)) {
State itemState = State.getInstance(item);
if (!ids.isEmpty() && !ids.contains(itemState.getId())) {
continue;
}
page.write("\"");
writeCsvItem(page, page.getTypeLabel(item));
page.write("\",\"");
writeCsvItem(page, page.getObjectLabel(item));
page.write("\"");
for (ObjectField field : fields) {
page.write(",\"");
if ("cms.directory.paths".equals(field.getInternalName())) {
for (Iterator<Directory.Path> i = itemState.as(Directory.ObjectModification.class).getPaths().iterator(); i.hasNext();) {
Directory.Path p = i.next();
String path = p.getPath();
page.writeHtml(path);
page.writeHtml(" (");
page.writeHtml(p.getType());
page.writeHtml(")");
if (i.hasNext()) {
page.write(", ");
}
}
} else {
for (Iterator<Object> i = CollectionUtils.recursiveIterable(itemState.get(field.getInternalName())).iterator(); i.hasNext();) {
Object value = i.next();
page.writeObject(value);
if (i.hasNext()) {
page.write(", ");
}
}
}
page.write("\"");
}
page.write("\r\n");
}
return;
} else if (page.param(String.class, "action-trash") != null) {
Iterator<Object> queryIterator = query.iterable(0).iterator();
try {
while (queryIterator.hasNext()) {
Object item = queryIterator.next();
State itemState = State.getInstance(item);
if (!ids.isEmpty() && !ids.contains(itemState.getId())) {
continue;
}
page.trash(item);
}
} finally {
if (queryIterator instanceof Closeable) {
((Closeable) queryIterator).close();
}
}
page.getResponse().sendRedirect(page.param(String.class, "returnUrl"));
return;
}
long offset = page.param(long.class, "offset");
int limit = page.pageParam(int.class, "limit", 20);
PaginatedResult<Object> result;
try {
result = query.select(offset, limit);
} catch (RuntimeException error) {
result = null;
}
page.putOverride(Recordable.class, new HtmlFormatter<Recordable>() {
@Override
public void format(HtmlWriter writer, Recordable object) throws IOException {
ToolPageContext page = (ToolPageContext) writer;
page.writeObjectLabel(object);
}
});
page.putOverride(Metric.class, new HtmlFormatter<Metric>() {
@Override
public void format(HtmlWriter writer, Metric object) throws IOException {
writer.write(new Double(object.getSum()).toString());
}
});
page.putOverride(Content.class, new HtmlFormatter<Content>() {
@Override
public void format(HtmlWriter writer, Content content) throws IOException {
ToolPageContext page = (ToolPageContext) writer;
page.writeStart("a",
"href", page.objectUrl("/content/edit.jsp", content));
page.writeObjectLabel(content);
page.writeEnd();
}
});
page.putOverride(StorageItem.class, new HtmlFormatter<StorageItem>() {
@Override
public void format(HtmlWriter writer, StorageItem item) throws IOException {
ToolPageContext page = (ToolPageContext) writer;
page.writeElement("img",
"height", 100,
"src", ImageEditor.Static.getDefault() != null ?
new ImageTag.Builder(item).setHeight(100).toUrl() :
item.getPublicUrl());
}
});
page.writeHeader();
page.writeStart("div", "class", "widget");
page.writeStart("h1", "class", "icon icon-search");
page.writeHtml("Advanced Search");
page.writeEnd();
page.writeStart("form",
"method", "get",
"action", page.url(null));
page.writeStart("div", "class", "inputContainer");
page.writeStart("div", "class", "inputLabel");
page.writeStart("label", "for", page.createId());
page.writeHtml("Type");
page.writeEnd();
page.writeEnd();
page.writeStart("div", "class", "inputSmall");
page.writeTypeSelect(
environment.getTypes(),
type,
"Any Types",
"data-bsp-autosubmit", "",
"name", TYPE_PARAMETER,
"data-searchable", true);
page.writeEnd();
page.writeEnd();
page.writeStart("div", "class", "inputContainer");
page.writeStart("div", "class", "inputLabel");
page.writeStart("label", "for", page.createId());
page.writeHtml("Query");
page.writeEnd();
page.writeEnd();
page.writeStart("div", "class", "inputSmall");
page.writeStart("textarea",
"id", page.getId(),
"name", PREDICATE_PARAMETER);
page.writeHtml(predicate);
page.writeEnd();
page.writeEnd();
page.writeEnd();
page.writeStart("div", "class", "inputContainer");
page.writeStart("div", "class", "inputLabel");
page.writeStart("label", "for", page.createId());
page.writeHtml("Fields");
page.writeEnd();
page.writeEnd();
page.writeStart("div", "class", "inputSmall");
page.writeStart("select",
"name", FIELDS_PARAMETER,
"multiple", "multiple");
for (ObjectField field : allFields) {
if (field.as(ToolUi.class).isHidden()) {
continue;
}
String fieldName = field.getInternalName();
page.writeStart("option",
"selected", fieldNames.contains(fieldName) ? "selected" : null,
"value", fieldName);
page.writeHtml(field.getDisplayName());
page.writeEnd();
}
page.writeEnd();
page.writeEnd();
page.writeEnd();
page.writeStart("div", "class", "buttons");
page.writeStart("button", "class", "action action-search");
page.writeHtml("Search");
page.writeEnd();
page.writeEnd();
page.writeEnd();
page.writeStart("h2");
page.writeHtml("Result");
page.writeEnd();
if (result == null) {
page.writeStart("div", "class", "message message-error");
page.writeStart("p");
page.writeHtml("Not a valid query! ");
page.writeHtml(predicate);
page.writeEnd();
page.writeEnd();
} else if (!result.hasPages()) {
page.writeStart("div", "class", "message message-warning");
page.writeStart("p");
page.writeHtml("No matching items!");
page.writeEnd();
page.writeEnd();
} else {
page.writeStart("form",
"class", "searchAdvancedResult",
"method", "post",
"action", page.url(null));
page.writeElement("input", "type", "hidden", "name", TYPE_PARAMETER, "value", type != null ? type.getId() : null);
page.writeElement("input", "type", "hidden", "name", PREDICATE_PARAMETER, "value", predicate);
page.writeElement("input", "type", "hidden", "name", "returnUrl", "value", page.url(""));
for (ObjectField field : fields) {
page.writeElement("input", "type", "hidden", "name", FIELDS_PARAMETER, "value", field.getInternalName());