use of org.haiku.haikudepotserver.multipage.model.Pagination in project haikudepotserver by haiku.
the class HomeController method home.
/**
* <p>This is the entry point for the home page. It will look at the parameters supplied and will
* establish what should be displayed.</p>
*/
@RequestMapping(method = RequestMethod.GET)
public ModelAndView home(HttpServletRequest httpServletRequest, @RequestParam(value = KEY_OFFSET, defaultValue = "0") Integer offset, @RequestParam(value = KEY_REPOSITORIESCODES, required = false) String repositoryCodes, @RequestParam(value = KEY_ARCHITECTURECODE, required = false) String architectureCode, @RequestParam(value = KEY_PKGCATEGORYCODE, required = false) String pkgCategoryCode, @RequestParam(value = KEY_SEARCHEXPRESSION, required = false) String searchExpression, @RequestParam(value = KEY_VIEWCRITERIATYPECODE, required = false) ViewCriteriaType viewCriteriaType) {
ObjectContext context = serverRuntime.newContext();
if (Strings.isNullOrEmpty(architectureCode)) {
architectureCode = defaultArchitectureCode;
}
if (null == repositoryCodes) {
repositoryCodes = Repository.CODE_DEFAULT;
}
// ------------------------------
// FETCH THE DATA
PkgSearchSpecification searchSpecification = new PkgSearchSpecification();
searchSpecification.setOffset(offset);
searchSpecification.setLimit(PAGESIZE);
searchSpecification.setExpression(searchExpression);
searchSpecification.setExpressionType(AbstractSearchSpecification.ExpressionType.CONTAINS);
Repository repository = StringUtils.isBlank(repositoryCodes) ? null : Repository.getByCode(context, repositoryCodes);
searchSpecification.setRepositories(null == repository ? Repository.getAllActive(context) : Collections.singletonList(repository));
Architecture architecture = Architecture.getByCode(context, architectureCode);
searchSpecification.setArchitectures(ImmutableList.of(architecture, Architecture.getByCode(context, Architecture.CODE_ANY)));
Optional<PkgCategory> pkgCategoryOptional = Optional.empty();
if (null != pkgCategoryCode) {
pkgCategoryOptional = PkgCategory.getByCode(context, pkgCategoryCode);
}
NaturalLanguage naturalLanguage = NaturalLanguageWebHelper.deriveNaturalLanguage(context, httpServletRequest);
searchSpecification.setNaturalLanguage(naturalLanguage);
switch(null == viewCriteriaType ? ViewCriteriaType.FEATURED : viewCriteriaType) {
case FEATURED:
searchSpecification.setSortOrdering(PkgSearchSpecification.SortOrdering.PROMINENCE);
break;
case CATEGORIES:
searchSpecification.setSortOrdering(PkgSearchSpecification.SortOrdering.NAME);
searchSpecification.setPkgCategory(pkgCategoryOptional.orElseThrow(() -> new IllegalStateException("the pkg category code was unable to be found; " + pkgCategoryCode)));
break;
case ALL:
searchSpecification.setSortOrdering(PkgSearchSpecification.SortOrdering.NAME);
break;
case MOSTVIEWED:
searchSpecification.setSortOrdering(PkgSearchSpecification.SortOrdering.VERSIONVIEWCOUNTER);
break;
case MOSTRECENT:
searchSpecification.setSortOrdering(PkgSearchSpecification.SortOrdering.VERSIONCREATETIMESTAMP);
break;
default:
throw new IllegalStateException("unhandled view criteria type");
}
Long totalPkgVersions = pkgService.total(context, searchSpecification);
if (searchSpecification.getOffset() > totalPkgVersions) {
searchSpecification.setOffset(totalPkgVersions.intValue());
}
List<PkgVersion> pkgVersions = pkgService.search(context, searchSpecification);
// ------------------------------
// GENERATE OUTPUT
HomeData data = new HomeData();
data.setNaturalLanguage(naturalLanguage);
final Set<String> excludedArchitectureCode = ImmutableSet.of(Architecture.CODE_ANY, Architecture.CODE_SOURCE);
data.setAllArchitectures(Architecture.getAll(context).stream().filter(a -> !excludedArchitectureCode.contains(a.getCode())).collect(Collectors.toList()));
data.setArchitecture(architecture);
data.setRepository(repository);
data.setAllRepositories(Repository.getAllActive(context));
data.setAllPkgCategories(PkgCategory.getAll(context));
data.setPkgCategory(pkgCategoryOptional.orElseGet(() -> PkgCategory.getAll(context).get(0)));
data.setAllViewCriteriaTypes(ImmutableList.copyOf(ViewCriteriaType.values()));
data.setViewCriteriaType(viewCriteriaType);
data.setSearchExpression(searchExpression);
data.setPkgVersions(pkgVersions);
if (0 != totalPkgVersions.intValue()) {
data.setPagination(new Pagination(totalPkgVersions.intValue(), offset, PAGESIZE));
}
httpServletRequest.setAttribute(MultipageConstants.KEY_PKGLOCALIZATIONLOOKUPSERVICE, new FixedPkgLocalizationLookupServiceImpl(context, pkgVersions, naturalLanguage));
httpServletRequest.setAttribute(MultipageConstants.KEY_SERVERRUNTIME, serverRuntime);
ModelAndView result = new ModelAndView("multipage/home");
result.addObject("data", data);
return result;
}
use of org.haiku.haikudepotserver.multipage.model.Pagination in project haikudepotserver by haiku.
the class PaginationLinksTag method doStartTagInternal.
@Override
protected int doStartTagInternal() throws Exception {
TagWriter tagWriter = new TagWriter(pageContext.getOut());
Pagination p = getPagination();
List<String> ulClasses = new ArrayList<>();
ulClasses.add("pagination-control-container");
if (0 == p.getPage()) {
ulClasses.add("pagination-control-on-first");
}
if (p.getPage() == p.getPages() - 1) {
ulClasses.add("pagination-control-on-last");
}
tagWriter.startTag("ul");
tagWriter.writeAttribute("class", String.join(" ", ulClasses));
writeArrow(tagWriter, "paginationleft.png", "pagination-control-left", "<--", 0 == p.getPage() ? "" : deriveHref(p.getPage() - 1));
int[] pageNumbers = p.generateSuggestedPages(null == getLinkCount() ? LINK_COUNT_DEFAULT : getLinkCount());
for (int pageNumber : pageNumbers) {
tagWriter.startTag("li");
if (pageNumber == p.getPage()) {
tagWriter.startTag("span");
tagWriter.writeAttribute("class", "pagination-control-currentpage");
tagWriter.writeAttribute("href", "");
} else {
tagWriter.startTag("a");
tagWriter.writeAttribute("href", deriveHref(pageNumber));
}
tagWriter.appendValue(Integer.toString(pageNumber + 1));
// a
tagWriter.endTag();
// li
tagWriter.endTag();
}
writeArrow(tagWriter, "paginationright.png", "pagination-control-right", "-->", p.getPage() == p.getPages() - 1 ? "" : deriveHref(p.getPage() + 1));
// ul
tagWriter.endTag();
return SKIP_BODY;
}
Aggregations