Search in sources :

Example 1 with DataTablesResponseDto

use of uk.ac.bbsrc.tgac.miso.dto.DataTablesResponseDto in project miso-lims by miso-lims.

the class JQueryDataTableBackend method get.

public DataTablesResponseDto<Dto> get(HttpServletRequest request, AdvancedSearchParser advancedSearchParser, PaginationFilter... filters) throws IOException {
    if (request.getParameterMap().size() > 0) {
        long numItems = getSource().count(filters);
        // get request params from DataTables
        Integer iDisplayStart = Integer.parseInt(request.getParameter("iDisplayStart"));
        Integer iDisplayLength = Integer.parseInt(request.getParameter("iDisplayLength"));
        String sSearch = request.getParameter("sSearch");
        String sSortDir = request.getParameter("sSortDir_0");
        String sortColIndex = request.getParameter("iSortCol_0");
        String sortCol = request.getParameter("mDataProp_" + sortColIndex);
        // get requested subset of item
        Long numMatches;
        DataTablesResponseDto<Dto> dtResponse = new DataTablesResponseDto<>();
        List<PaginationFilter> additionalFilters = new ArrayList<>(Arrays.asList(filters));
        StringWriter errorBuffer = new StringWriter();
        Consumer<String> errorHandler = message -> {
            if (errorBuffer.getBuffer().length() > 0) {
                errorBuffer.append("<br/>");
            }
            try {
                StringEscapeUtils.escapeHtml(errorBuffer, message);
            } catch (IOException e) {
                log.error("Failed to write to string writer.", e);
            }
        };
        if (!isStringEmptyOrNull(sSearch)) {
            additionalFilters.addAll(Arrays.asList(advancedSearchParser.parseQuery(sSearch, SecurityContextHolder.getContext().getAuthentication().getName(), errorHandler)));
            numMatches = getSource().count(additionalFilters.toArray(filters));
        } else {
            numMatches = numItems;
        }
        Collection<Model> models = getSource().list(errorHandler, iDisplayStart, iDisplayLength, "asc".equalsIgnoreCase(sSortDir), sortCol, additionalFilters.toArray(filters));
        List<Dto> dtos = new ArrayList<>();
        for (Model model : models) {
            Dto dto = asDto(model);
            dtos.add(dto);
        }
        dtResponse.setITotalRecords(numItems);
        dtResponse.setITotalDisplayRecords(numMatches);
        dtResponse.setAaData(dtos);
        dtResponse.setSEcho(Long.valueOf(request.getParameter("sEcho")));
        String errorMessage = errorBuffer.toString();
        if (errorMessage.length() > 0) {
            dtResponse.setSError(errorMessage);
        }
        return dtResponse;
    } else {
        throw new RestException("Request must specify DataTables parameters.");
    }
}
Also used : Arrays(java.util.Arrays) Logger(org.slf4j.Logger) PaginationFilter(uk.ac.bbsrc.tgac.miso.core.util.PaginationFilter) AdvancedSearchParser(uk.ac.bbsrc.tgac.miso.webapp.controller.component.AdvancedSearchParser) StringWriter(java.io.StringWriter) Collection(java.util.Collection) DataTablesResponseDto(uk.ac.bbsrc.tgac.miso.dto.DataTablesResponseDto) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) ArrayList(java.util.ArrayList) Consumer(java.util.function.Consumer) List(java.util.List) HttpServletRequest(javax.servlet.http.HttpServletRequest) LimsUtils.isStringEmptyOrNull(uk.ac.bbsrc.tgac.miso.core.util.LimsUtils.isStringEmptyOrNull) PaginatedDataSource(uk.ac.bbsrc.tgac.miso.core.util.PaginatedDataSource) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) StringEscapeUtils(org.apache.commons.lang.StringEscapeUtils) DataTablesResponseDto(uk.ac.bbsrc.tgac.miso.dto.DataTablesResponseDto) ArrayList(java.util.ArrayList) IOException(java.io.IOException) StringWriter(java.io.StringWriter) PaginationFilter(uk.ac.bbsrc.tgac.miso.core.util.PaginationFilter) DataTablesResponseDto(uk.ac.bbsrc.tgac.miso.dto.DataTablesResponseDto)

Aggregations

IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 List (java.util.List)1 Consumer (java.util.function.Consumer)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 StringEscapeUtils (org.apache.commons.lang.StringEscapeUtils)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 SecurityContextHolder (org.springframework.security.core.context.SecurityContextHolder)1 LimsUtils.isStringEmptyOrNull (uk.ac.bbsrc.tgac.miso.core.util.LimsUtils.isStringEmptyOrNull)1 PaginatedDataSource (uk.ac.bbsrc.tgac.miso.core.util.PaginatedDataSource)1 PaginationFilter (uk.ac.bbsrc.tgac.miso.core.util.PaginationFilter)1 DataTablesResponseDto (uk.ac.bbsrc.tgac.miso.dto.DataTablesResponseDto)1 AdvancedSearchParser (uk.ac.bbsrc.tgac.miso.webapp.controller.component.AdvancedSearchParser)1