@QueryParam("offset") @DefaultValue("0") int offset,
@QueryParam("details") @DefaultValue("false") boolean details) {
// Create search query
Site site = getSite(request);
SearchQuery q = new SearchQueryImpl(site);
q.withTypes(Page.TYPE);
if (version != -1)
q.withVersion(version);
if (preferredVersion != -1)
q.withPreferredVersion(preferredVersion);
// Path
if (StringUtils.isNotBlank(path))
q.withPath(path);
// Subjects
if (StringUtils.isNotBlank(subjectstring)) {
StringTokenizer subjects = new StringTokenizer(subjectstring, ",");
while (subjects.hasMoreTokens())
q.withSubject(subjects.nextToken());
}
// Search terms
if (StringUtils.isNotBlank(searchterms))
q.withText(true, searchterms);
Calendar today = Calendar.getInstance();
today.set(Calendar.HOUR_OF_DAY, 0);
today.set(Calendar.MINUTE, 0);
today.set(Calendar.SECOND, 0);
today.set(Calendar.MILLISECOND, 0);
Calendar yesterday = Calendar.getInstance();
yesterday.add(Calendar.DATE, -1);
yesterday.set(Calendar.HOUR_OF_DAY, 0);
yesterday.set(Calendar.MINUTE, 0);
yesterday.set(Calendar.SECOND, 0);
yesterday.set(Calendar.MILLISECOND, 0);
Calendar tomorrow = Calendar.getInstance();
tomorrow.add(Calendar.DATE, 1);
tomorrow.set(Calendar.HOUR_OF_DAY, 0);
tomorrow.set(Calendar.MINUTE, 0);
tomorrow.set(Calendar.SECOND, 0);
tomorrow.set(Calendar.MILLISECOND, 0);
// Filter query
if (StringUtils.isNotBlank(filter)) {
if ("/".equals(filter)) {
q.withPath("/");
} else if (filter.contains("state:work")) {
q.withVersion(Resource.WORK);
q.withPreferredVersion(-1);
} else if (filter.contains("state:live")) {
q.withVersion(Resource.LIVE);
q.withPreferredVersion(-1);
} else if (filter.contains("state:locked")) {
q.withLockOwner();
}
// by user
else if (filter.startsWith("locked:") && filter.length() > "locked:".length()) {
String lockOwner = StringUtils.trim(filter.substring("locked:".length()));
if ("me".equals(lockOwner))
q.withLockOwner(securityService.getUser());
else
q.withLockOwner(new UserImpl(lockOwner));
} else if (filter.startsWith("creator:") && filter.length() > "creator:".length()) {
String creator = StringUtils.trim(filter.substring("creator:".length()));
if ("me".equals(creator))
q.withCreator(securityService.getUser());
else
q.withCreator(new UserImpl(creator));
} else if (filter.startsWith("modifier:") && filter.length() > "modifier:".length()) {
String modifier = StringUtils.trim(filter.substring("modifier:".length()));
if ("me".equals(modifier))
q.withModifier(securityService.getUser());
else
q.withModifier(new UserImpl(modifier));
} else if (filter.startsWith("publisher:") && filter.length() > "publisher:".length()) {
String publisher = StringUtils.trim(filter.substring("publisher:".length()));
if ("me".equals(publisher))
q.withPublisher(securityService.getUser());
else
q.withPublisher(new UserImpl(publisher));
}
// by date
else if (filter.startsWith("created:") && filter.length() > "created:".length()) {
String created = StringUtils.trim(filter.substring("created:".length()));
if ("today".equals(created))
q.withCreationDateBetween(today.getTime()).and(tomorrow.getTime());
else if ("yesterday".equals(created))
q.withCreationDateBetween(yesterday.getTime()).and(today.getTime());
else
q.withCreationDate(tomorrow.getTime());
} else if (filter.startsWith("modified:") && filter.length() > "modified:".length()) {
String modified = StringUtils.trim(filter.substring("modified:".length()));
if ("today".equals(modified))
q.withModificationDateBetween(today.getTime()).and(tomorrow.getTime());
else if ("yesterday".equals(modified))
q.withModificationDateBetween(yesterday.getTime()).and(today.getTime());
else
q.withCreationDate(tomorrow.getTime());
} else if (filter.startsWith("publisher:") && filter.length() > "publisher:".length()) {
String published = StringUtils.trim(filter.substring("published:".length()));
if ("today".equals(published))
q.withPublishingDateBetween(today.getTime()).and(tomorrow.getTime());
else if ("yesterday".equals(published))
q.withPublishingDateBetween(yesterday.getTime()).and(today.getTime());
else
q.withCreationDate(tomorrow.getTime());
}
// by id
else if (filter.contains("id:")) {
String[] searchTerms = StringUtils.split(filter);
for (String searchTerm : searchTerms) {
if (searchTerm.startsWith("id:") && filter.length() > "id:".length()) {
q.withIdentifier(StringUtils.trim(searchTerm.substring("id:".length())));
}
}
}
// simple filter
else if (filter.contains("/")) {
q.withPathPrefix(filter);
} else {
q.withFulltext(true, filter);
}
}
// Limit and Offset
q.withLimit(limit);
q.withOffset(offset);
// Sort order
if (StringUtils.equalsIgnoreCase("modified-asc", sort)) {
q.sortByModificationDate(Order.Ascending);
} else if (StringUtils.equalsIgnoreCase("modified-desc", sort)) {
q.sortByModificationDate(Order.Descending);
} else if (StringUtils.equalsIgnoreCase("created-asc", sort)) {
q.sortByCreationDate(Order.Ascending);
} else if (StringUtils.equalsIgnoreCase("created-desc", sort)) {
q.sortByCreationDate(Order.Descending);
} else if (StringUtils.equalsIgnoreCase("published-asc", sort)) {
q.sortByPublishingDate(Order.Ascending);
} else if (StringUtils.equalsIgnoreCase("published-desc", sort)) {
q.sortByPublishingDate(Order.Descending);
}
// Load the result
String result = loadResultSet(q, details);