Example usage for java.util List sort

List of usage examples for java.util List sort

Introduction

In this page you can find the example usage for java.util List sort.

Prototype

@SuppressWarnings({ "unchecked", "rawtypes" })
default void sort(Comparator<? super E> c) 

Source Link

Document

Sorts this list according to the order induced by the specified Comparator .

Usage

From source file:sg.ncl.MainController.java

/**
 * Allows admins to://from   w  w w  .  ja v a 2s . c o m
 * view reservations
 * reserve nodes
 * release nodes
 */
@GetMapping("/admin/nodesReservation")
public String adminNodesReservation(
        @ModelAttribute("reservationStatusForm") ReservationStatusForm reservationStatusForm, Model model,
        HttpSession session) {
    if (!validateIfAdmin(session)) {
        return NO_PERMISSION_PAGE;
    }

    List<Team2> allTeams = new ArrayList<>(getTeamMap().values());
    allTeams.sort(Comparator.comparing(Team2::getName, String.CASE_INSENSITIVE_ORDER));
    model.addAttribute(ALL_TEAMS, allTeams);
    model.addAttribute(RESERVATION_STATUS_FORM, reservationStatusForm);

    return "node_reservation";
}

From source file:sg.ncl.MainController.java

@PostMapping("/admin/nodesReservation")
public String adminNodesReservation(
        @ModelAttribute("reservationStatusForm") ReservationStatusForm reservationStatusForm,
        BindingResult bindingResult, RedirectAttributes redirectAttributes, Model model) {
    List<Team2> allTeams = new ArrayList<>(getTeamMap().values());
    allTeams.sort(Comparator.comparing(Team2::getName, String.CASE_INSENSITIVE_ORDER));
    model.addAttribute(ALL_TEAMS, allTeams);

    // sanitization
    if (bindingResult.hasErrors()) {
        model.addAttribute(ALL_TEAMS, allTeams);
        model.addAttribute(RESERVATION_STATUS_FORM, reservationStatusForm);
        model.addAttribute(STATUS, NODES_RESERVATION_FAIL);
        model.addAttribute(MESSAGE, "form errors");
        return "node_reservation";
    }/*from   ww w.  ja  v  a  2s.com*/

    switch (reservationStatusForm.getAction()) {
    case "release":
        releaseNodes(reservationStatusForm, redirectAttributes);
        break;
    case "reserve":
        reserveNodes(reservationStatusForm, redirectAttributes);
        break;
    case "check":
        checkReservation(reservationStatusForm, redirectAttributes);
        break;
    default:
        // error
        redirectAttributes.addFlashAttribute(STATUS, NODES_RESERVATION_FAIL);
        redirectAttributes.addFlashAttribute(MESSAGE, ERR_SERVER_OVERLOAD);
        break;
    }

    redirectAttributes.addFlashAttribute(RESERVATION_STATUS_FORM, reservationStatusForm);
    redirectAttributes.addFlashAttribute(ALL_TEAMS, allTeams);

    return "redirect:/admin/nodesReservation";
}

From source file:org.cgiar.ccafs.marlo.action.BaseAction.java

public List<GlobalUnit> getCrpCategoryList(String category) {
    List<GlobalUnit> globalUnits = crpManager.findAll().stream()
            .filter(c -> c.isMarlo() && c.getGlobalUnitType().getId().intValue() == Integer.parseInt(category))
            .collect(Collectors.toList());
    globalUnits.sort((gu1, gu2) -> gu1.getAcronym().compareTo(gu2.getAcronym()));
    return globalUnits;
}

From source file:org.cgiar.ccafs.marlo.action.BaseAction.java

/**
 * get all phases per CRP/*from  ww w  . j a v  a  2  s  .co  m*/
 * 
 * @return the list of the phases for the crp
 */
public Map<Long, Phase> getAllPhases() {
    if (this.getSession() != null) {
        if (!this.getSession().containsKey(APConstants.ALL_PHASES)) {
            List<Phase> phases = phaseManager.findAll().stream()
                    .filter(c -> c.getCrp().getId().longValue() == this.getCrpID().longValue())
                    .collect(Collectors.toList());
            phases.sort((p1, p2) -> p1.getStartDate().compareTo(p2.getStartDate()));
            Map<Long, Phase> allPhasesMap = new HashMap<>();
            for (Phase phase : phases) {
                allPhasesMap.put(phase.getId(), phase);
            }
            this.getSession().put(APConstants.ALL_PHASES, allPhasesMap);
        }

    }
    return (Map<Long, Phase>) this.getSession().get(APConstants.ALL_PHASES);
}

From source file:sg.ncl.MainController.java

@RequestMapping("/admin/usage")
public String adminTeamUsage(Model model, @RequestParam(value = "start", required = false) String start,
        @RequestParam(value = "end", required = false) String end,
        @RequestParam(value = "organizationType", required = false) String organizationType,
        @RequestParam(value = "team", required = false) String team,
        final RedirectAttributes redirectAttributes, HttpSession session)
        throws IOException, WebServiceRuntimeException {
    if (!validateIfAdmin(session)) {
        return NO_PERMISSION_PAGE;
    }// w w  w  . j av a 2s  .  com

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    ZonedDateTime nowDate = ZonedDateTime.now();
    String now = nowDate.format(formatter);
    if (start == null) {
        start = nowDate.with(firstDayOfMonth()).format(formatter);
    }
    if (end == null) {
        end = now;
    }
    if (now.compareTo(start) < 0 || now.compareTo(end) < 0) {
        redirectAttributes.addFlashAttribute(MESSAGE, "Period selected is beyond current date (today).");
        return REDIRECT_TEAM_USAGE;
    }

    // get list of teamids
    HttpEntity<String> request = createHttpEntityHeaderOnly();
    ResponseEntity responseEntity = restTemplate.exchange(properties.getSioTeamsUrl(), HttpMethod.GET, request,
            String.class);
    JSONArray jsonArray = new JSONArray(responseEntity.getBody().toString());

    List<Team2> searchTeams = new ArrayList<>();
    TeamManager2 teamManager2 = new TeamManager2();
    getSearchTeams(organizationType, team, jsonArray, searchTeams, teamManager2);

    if (!searchTeams.isEmpty()) {
        List<String> dates = getDates(start, end, formatter);

        Map<String, List<Long>> teamUsages = new HashMap<>();
        Long totalUsage = 0L;
        for (Team2 team2 : searchTeams) {
            try {
                List<Long> usages = new ArrayList<>();
                totalUsage += getTeamUsageStatistics(team2, start, end, request, usages);
                teamUsages.put(team2.getName(), usages);
            } catch (RestClientException rce) {
                log.warn("Error connecting to sio analytics service for team usage: {}", rce);
                redirectAttributes.addFlashAttribute(MESSAGE, ERR_SERVER_OVERLOAD);
                return REDIRECT_TEAM_USAGE;
            } catch (StartDateAfterEndDateException sde) {
                redirectAttributes.addFlashAttribute(MESSAGE, ERR_START_DATE_AFTER_END_DATE);
                return REDIRECT_TEAM_USAGE;
            }
        }
        model.addAttribute("dates", dates);
        model.addAttribute("teamUsages", teamUsages);
        model.addAttribute("totalUsage", totalUsage);
    }

    List<Team2> allTeams = new ArrayList<>(teamManager2.getTeamMap().values());
    allTeams.sort(Comparator.comparing(Team2::getName, String.CASE_INSENSITIVE_ORDER));
    model.addAttribute(ALL_TEAMS, allTeams);
    model.addAttribute("start", start);
    model.addAttribute("end", end);
    model.addAttribute("organizationType", organizationType);
    model.addAttribute("team", team);
    return "usage_statistics";
}

From source file:org.cgiar.ccafs.marlo.action.BaseAction.java

public Phase getActualPhase(Map<String, Object> session, long crpID) {

    try {/*from www .  j  av  a  2  s. c  o m*/
        Map<Long, Phase> allPhases = null;
        if (session != null) {
            if (session.containsKey(APConstants.ALL_PHASES)) {
                List<Phase> phases = phaseManager.findAll().stream()
                        .filter(c -> c.getCrp().getId().longValue() == this.getCrpID().longValue())
                        .collect(Collectors.toList());
                phases.sort((p1, p2) -> p1.getStartDate().compareTo(p2.getStartDate()));
                Map<Long, Phase> allPhasesMap = new HashMap<>();
                for (Phase phase : phases) {
                    allPhasesMap.put(phase.getId(), phase);
                }
                session.put(APConstants.ALL_PHASES, allPhasesMap);
            }
            allPhases = (Map<Long, Phase>) session.get(APConstants.ALL_PHASES);
        }

        Map<String, Parameter> parameters = this.getParameters();
        if (this.getPhaseID() != null) {
            long phaseID = Long
                    .parseLong(StringUtils.trim(parameters.get(APConstants.PHASE_ID).getMultipleValues()[0]));
            Phase phase = allPhases.get(new Long(phaseID));
            return phase;
        }
        if (parameters != null && parameters.containsKey(APConstants.PHASE_ID)) {
            long phaseID = Long
                    .parseLong(StringUtils.trim(parameters.get(APConstants.PHASE_ID).getMultipleValues()[0]));
            Phase phase = allPhases.get(new Long(phaseID));
            return phase;
        }
        Phase phase = phaseManager.findCycle(this.getCurrentCycleParam(), this.getCurrentCycleYearParam(),
                this.getCrpID());
        return phase;
    } catch (Exception e) {
        return new Phase(null, "", -1);
    }

}

From source file:org.cgiar.ccafs.marlo.action.BaseAction.java

/**
 * validate if the list of phases are on session if not, will be find on bd
 * /*  w ww  .ja v a  2s .com*/
 * @return the list of the phases for the crp
 */
public List<Phase> getPhases() {
    if (this.getSession().containsKey(APConstants.PHASES)) {
        return (List<Phase>) this.getSession().get(APConstants.PHASES);
    } else {
        List<Phase> phases = phaseManager.findAll().stream()
                .filter(c -> c.getCrp().getId().longValue() == this.getCrpID().longValue()
                        && c.getVisible() != null && c.getVisible())
                .collect(Collectors.toList());
        phases.sort((p1, p2) -> p1.getStartDate().compareTo(p2.getStartDate()));
        this.getSession().put(APConstants.PHASES, phases);
        return phases;
    }
}

From source file:org.cgiar.ccafs.marlo.action.BaseAction.java

/**
 * get the actual//ww w. j ava 2 s.  co  m
 * 
 * @return the actual phase of the crp
 */
public Phase getActualPhase() {
    try {
        Map<Long, Phase> allPhases = null;
        if (this.getSession() != null) {
            if (!this.getSession().containsKey(APConstants.ALL_PHASES)) {
                List<Phase> phases = phaseManager.findAll().stream()
                        .filter(c -> c.getCrp().getId().longValue() == this.getCrpID().longValue())
                        .collect(Collectors.toList());
                phases.sort((p1, p2) -> p1.getStartDate().compareTo(p2.getStartDate()));
                Map<Long, Phase> allPhasesMap = new HashMap<>();
                for (Phase phase : phases) {
                    allPhasesMap.put(phase.getId(), phase);
                }
                this.getSession().put(APConstants.ALL_PHASES, allPhasesMap);
            }

        }
        /**
         * This throws a null pointer exception if invoked from a struts2 interceptor as the session has not been
         * set on the BaseAction.
         * I've made the RequireUserInterceptor set the session on the baseAction now but this seems a little hacky.
         */
        allPhases = (Map<Long, Phase>) this.getSession().get(APConstants.ALL_PHASES);

        if (this.getPhaseID() != null) {
            long phaseID = this.getPhaseID();
            Phase phase = allPhases.get(new Long(phaseID));
            if (phase == null) {
                phase = phaseManager.getPhaseById(phaseID);
                return phase;
            }
            return phase;
        }

        Map<String, Parameter> parameters = this.getParameters();
        if (parameters != null && parameters.containsKey(APConstants.PHASE_ID)) {
            Phase phase;
            try {
                long phaseID = Long.parseLong(
                        StringUtils.trim(parameters.get(APConstants.PHASE_ID).getMultipleValues()[0]));
                phase = allPhases.get(new Long(phaseID));
                return phase;
            } catch (Exception e) {
                phase = phaseManager.findCycle(this.getCurrentCycleParam(), this.getCurrentCycleYearParam(),
                        this.getCrpID());
            }

            if (phase != null) {
                this.getSession().put(APConstants.CURRENT_PHASE, phase);
                return phase;
            } else {
                return new Phase(null, "", -1);
            }

        }

        if (this.getSession().containsKey(APConstants.CURRENT_PHASE)) {
            Phase phase = (Phase) this.getSession().get(APConstants.CURRENT_PHASE);
            return phase;

        } else {
            Phase phase = phaseManager.findCycle(this.getCurrentCycleParam(), this.getCurrentCycleYearParam(),
                    this.getCrpID());

            if (phase != null) {
                this.getSession().put(APConstants.CURRENT_PHASE, phase);
                return phase;
            } else {
                return new Phase(null, "", -1);
            }

        }

    } catch (

    Exception e) {
        return new Phase(null, "", -1);
    }

}

From source file:org.cgiar.ccafs.marlo.action.BaseAction.java

public List<CrpPpaPartner> getPpaPartners() {
    List<CrpPpaPartner> crpPpaPartners = phaseManager.getPhaseById(this.getActualPhase().getId())
            .getCrpPpaPartner().stream().filter(c -> c.isActive() && c.getCrp().equals(this.getCurrentCrp()))
            .collect(Collectors.toList());
    if (crpPpaPartners != null && !crpPpaPartners.isEmpty()) {
        crpPpaPartners.sort((i1, i2) -> i1.getInstitution().getName().compareTo(i2.getInstitution().getName()));
        return crpPpaPartners;
    } else {/*from  ww  w.  j a va 2 s  .c o  m*/
        return new ArrayList<>();
    }
}

From source file:org.cgiar.ccafs.marlo.action.BaseAction.java

public List<Phase> getPhasesImpact() {
    if (this.getSession().containsKey(APConstants.PHASES_IMPACT)) {
        return (List<Phase>) this.getSession().get(APConstants.PHASES_IMPACT);
    } else {//from w  w  w .ja va 2 s . co m
        List<Phase> phases = phaseManager.findAll().stream()
                .filter(c -> c.getCrp().getId().longValue() == this.getCrpID().longValue()
                        && c.getVisible() != null && c.getVisible()
                        && c.getDescription().equals(APConstants.PLANNING))
                .collect(Collectors.toList());
        phases.sort((p1, p2) -> p1.getStartDate().compareTo(p2.getStartDate()));
        this.getSession().put(APConstants.PHASES_IMPACT, phases);
        return phases;
    }
}