Example usage for java.util Comparator comparing

List of usage examples for java.util Comparator comparing

Introduction

In this page you can find the example usage for java.util Comparator comparing.

Prototype

public static <T, U extends Comparable<? super U>> Comparator<T> comparing(
        Function<? super T, ? extends U> keyExtractor) 

Source Link

Document

Accepts a function that extracts a java.lang.Comparable Comparable sort key from a type T , and returns a Comparator that compares by that sort key.

Usage

From source file:org.eclipse.sw360.portal.portlets.projects.ProjectPortlet.java

private void exportReleasesSpreadsheet(ResourceRequest request, ResourceResponse response) {
    final User user = UserCacheHolder.getUserFromRequest(request);
    try {//  w  w w . j  ava  2  s .  c  om
        String id = request.getParameter(PROJECT_ID);
        Project project = null;
        if (!isNullOrEmpty(id)) {
            project = thriftClients.makeProjectClient().getProjectById(id, user);
        }
        if (project != null) {
            Map<Release, ProjectNamesWithMainlineStatesTuple> releaseStringMap = getProjectsNamesWithMainlineStatesByRelease(
                    project, user);
            List<Release> releases = releaseStringMap.keySet().stream()
                    .sorted(Comparator.comparing(SW360Utils::printFullname)).collect(Collectors.toList());
            ReleaseExporter exporter = new ReleaseExporter(thriftClients.makeComponentClient(), releases,
                    releaseStringMap);

            PortletResponseUtil.sendFile(request, response,
                    String.format("releases-%s-%s-%s.xlsx", project.getName(), project.getVersion(),
                            SW360Utils.getCreatedOn()),
                    exporter.makeExcelExport(releases), CONTENT_TYPE_OPENXML_SPREADSHEET);
        }
    } catch (IOException | TException e) {
        log.error("An error occurred while generating the Excel export", e);
        response.setProperty(ResourceResponse.HTTP_STATUS_CODE,
                Integer.toString(HttpServletResponse.SC_INTERNAL_SERVER_ERROR));
    }
}

From source file:org.cgiar.ccafs.marlo.action.powb.ManagementRiskAction.java

@Override
public void prepare() throws Exception {
    // Get current CRP
    loggedCrp = (GlobalUnit) this.getSession().get(APConstants.SESSION_CRP);
    loggedCrp = crpManager.getGlobalUnitById(loggedCrp.getId());
    Phase phase = this.getActualPhase();

    // If there is a history version being loaded
    if (this.getRequest().getParameter(APConstants.TRANSACTION_ID) != null) {
        transaction = StringUtils.trim(this.getRequest().getParameter(APConstants.TRANSACTION_ID));
        PowbSynthesis history = (PowbSynthesis) auditLogManager.getHistory(transaction);
        if (history != null) {
            powbSynthesis = history;//  w ww. jav  a 2  s. c  om
            powbSynthesisID = powbSynthesis.getId();
        } else {
            this.transaction = null;
            this.setTransaction("-1");
        }
    } else {
        // Get Liaison institution ID Parameter
        try {
            liaisonInstitutionID = Long.parseLong(StringUtils
                    .trim(this.getRequest().getParameter(APConstants.LIAISON_INSTITUTION_REQUEST_ID)));
        } catch (NumberFormatException e) {
            User user = userManager.getUser(this.getCurrentUser().getId());
            if (user.getLiasonsUsers() != null || !user.getLiasonsUsers().isEmpty()) {
                List<LiaisonUser> liaisonUsers = new ArrayList<>(user.getLiasonsUsers().stream()
                        .filter(lu -> lu.isActive() && lu.getLiaisonInstitution().isActive()
                                && lu.getLiaisonInstitution().getCrp().getId() == loggedCrp.getId()
                                && lu.getLiaisonInstitution().getInstitution() == null)
                        .collect(Collectors.toList()));
                if (!liaisonUsers.isEmpty()) {
                    boolean isLeader = false;
                    for (LiaisonUser liaisonUser : liaisonUsers) {
                        LiaisonInstitution institution = liaisonUser.getLiaisonInstitution();
                        if (institution.isActive()) {
                            if (institution.getCrpProgram() != null) {
                                if (institution.getCrpProgram()
                                        .getProgramType() == ProgramType.FLAGSHIP_PROGRAM_TYPE.getValue()) {
                                    liaisonInstitutionID = institution.getId();
                                    isLeader = true;
                                    break;
                                }
                            } else {
                                if (institution.getAcronym().equals("PMU")) {
                                    liaisonInstitutionID = institution.getId();
                                    isLeader = true;
                                    break;
                                }
                            }
                        }
                    }
                    if (!isLeader) {
                        liaisonInstitutionID = this.firstFlagship();
                    }
                } else {
                    liaisonInstitutionID = this.firstFlagship();
                }
            } else {
                liaisonInstitutionID = this.firstFlagship();
            }
        }

        try {
            powbSynthesisID = Long
                    .parseLong(StringUtils.trim(this.getRequest().getParameter(APConstants.POWB_SYNTHESIS_ID)));
            powbSynthesis = powbSynthesisManager.getPowbSynthesisById(powbSynthesisID);

            if (!powbSynthesis.getPhase().equals(phase)) {
                powbSynthesis = powbSynthesisManager.findSynthesis(phase.getId(), liaisonInstitutionID);
                if (powbSynthesis == null) {
                    powbSynthesis = this.createPowbSynthesis(phase.getId(), liaisonInstitutionID);
                }
                powbSynthesisID = powbSynthesis.getId();
            }
        } catch (Exception e) {

            powbSynthesis = powbSynthesisManager.findSynthesis(phase.getId(), liaisonInstitutionID);
            if (powbSynthesis == null) {
                powbSynthesis = this.createPowbSynthesis(phase.getId(), liaisonInstitutionID);
            }
            powbSynthesisID = powbSynthesis.getId();

        }
    }

    if (powbSynthesis != null) {

        PowbSynthesis powbSynthesisDB = powbSynthesisManager.getPowbSynthesisById(powbSynthesisID);
        powbSynthesisID = powbSynthesisDB.getId();
        liaisonInstitutionID = powbSynthesisDB.getLiaisonInstitution().getId();
        liaisonInstitution = liaisonInstitutionManager.getLiaisonInstitutionById(liaisonInstitutionID);

        Path path = this.getAutoSaveFilePath();
        // Verify if there is a Draft file
        if (path.toFile().exists() && this.getCurrentUser().isAutoSave()) {
            BufferedReader reader;
            reader = new BufferedReader(new FileReader(path.toFile()));
            Gson gson = new GsonBuilder().create();
            JsonObject jReader = gson.fromJson(reader, JsonObject.class);
            AutoSaveReader autoSaveReader = new AutoSaveReader();
            powbSynthesis = (PowbSynthesis) autoSaveReader.readFromJson(jReader);
            powbSynthesisID = powbSynthesis.getId();
            this.setDraft(true);
            reader.close();
        } else {
            this.setDraft(false);
            // Check if ToC relation is null -create it
            if (powbSynthesis.getPowbManagementRisk() == null) {
                PowbManagementRisk managementRisk = new PowbManagementRisk();
                // create one to one relation
                powbSynthesis.setPowbManagementRisk(managementRisk);
                managementRisk.setPowbSynthesis(powbSynthesis);
                // save the changes
                powbSynthesis = powbSynthesisManager.savePowbSynthesis(powbSynthesis);
            }
        }
    }
    if (this.isFlagship()) {
        LiaisonInstitution pmuInstitution = loggedCrp.getLiaisonInstitutions().stream()
                .filter(c -> c.getCrpProgram() == null && c.getAcronym().equals("PMU"))
                .collect(Collectors.toList()).get(0);
        PowbSynthesis powbSynthesisDB = powbSynthesisManager.findSynthesis(phase.getId(),
                pmuInstitution.getId());
        if (powbSynthesisDB != null) {
            if (powbSynthesisDB.getPowbManagementRisk() != null) {
                powbSynthesis.setPowbManagementRisk(powbSynthesisDB.getPowbManagementRisk());

            }
        }
    }

    // Get the list of liaison institutions Flagships and PMU.
    liaisonInstitutions = loggedCrp.getLiaisonInstitutions().stream()
            .filter(c -> c.getCrpProgram() != null && c.isActive()
                    && c.getCrpProgram().getProgramType() == ProgramType.FLAGSHIP_PROGRAM_TYPE.getValue())
            .collect(Collectors.toList());
    liaisonInstitutions.sort(Comparator.comparing(LiaisonInstitution::getAcronym));

    // Setup the PUM Management Risk Table
    if (this.isPMU()) {
        this.managementRiskList(phase.getId());
    }
    // ADD PMU as liasion Institutio too
    liaisonInstitutions.addAll(loggedCrp.getLiaisonInstitutions().stream()
            .filter(c -> c.getCrpProgram() == null && c.isActive() && c.getAcronym().equals("PMU"))
            .collect(Collectors.toList()));

    // Base Permission
    String params[] = { loggedCrp.getAcronym(), powbSynthesis.getId() + "" };
    this.setBasePermission(this.getText(Permission.POWB_SYNTHESIS_MANAGEMENT_RISK_BASE_PERMISSION, params));

    if (this.isHttpPost()) {

    }
}

From source file:aiai.ai.launchpad.experiment.ExperimentsController.java

public static void sortSnippetsByType(List<ExperimentSnippet> snippets) {
    snippets.sort(Comparator.comparing(ExperimentSnippet::getType));
}

From source file:de.metas.ui.web.window.descriptor.factory.standard.LayoutFactory.java

public DocumentViewLayout.Builder layoutGridView() {
    final DocumentEntityDescriptor.Builder entityDescriptor = documentEntity();
    logger.trace("Generating grid view layout for {}", entityDescriptor);

    final DocumentViewLayout.Builder layout = DocumentViewLayout.builder()
            .setDetailId(entityDescriptor.getDetailId()).setCaption(entityDescriptor.getCaption())
            .setDescription(entityDescriptor.getDescription())
            .setEmptyResultText(HARDCODED_TAB_EMPTY_RESULT_TEXT)
            .setEmptyResultHint(HARDCODED_TAB_EMPTY_RESULT_HINT)
            .setIdFieldName(entityDescriptor.getIdFieldName());

    ////from www  .  java2 s  .  co  m
    // Create UI elements from AD_UI_Elements which were marked as DisplayedGrid
    {
        streamAD_UI_Elements().filter(adUIElement -> adUIElement.isDisplayedGrid())
                .sorted(Comparator.comparing(I_AD_UI_Element::getSeqNoGrid))
                .map(adUIElement -> layoutElement(adUIElement)).filter(uiElement -> uiElement != null)
                .peek(uiElement -> uiElement.setGridElement()).forEach(layout::addElement);
    }

    //
    // Fallback: when no elements were found: creating the view using the single row layout
    if (!layout.hasElements()) {
        logger.warn("No grid layout was found for {}. Trying to create one based on single row layout elements",
                entityDescriptor);
        streamAD_UI_Elements()
                .filter(adUIElement -> adUIElement.isDisplayed() && !adUIElement.isAdvancedField())
                .map(adUIElement -> layoutElement(adUIElement)).filter(uiElement -> uiElement != null)
                .peek(uiElement -> uiElement.setGridElement()).forEach(layout::addElement);
    }

    //
    // Fallback:
    if (!layout.hasElements()) {
        logger.warn("No grid layout found for {}. Continuing", entityDescriptor);
    }

    //
    // Make sure all added elements have the GridViewField characteristic
    descriptorsFactory.addFieldsCharacteristic(layout.getFieldNames(), Characteristic.GridViewField);

    return layout;
}

From source file:org.cgiar.ccafs.marlo.action.powb.ManagementGovernanceAction.java

@Override
public void prepare() throws Exception {
    // Get current CRP
    loggedCrp = (GlobalUnit) this.getSession().get(APConstants.SESSION_CRP);
    loggedCrp = crpManager.getGlobalUnitById(loggedCrp.getId());
    Phase phase = this.getActualPhase();

    // If there is a history version being loaded
    if (this.getRequest().getParameter(APConstants.TRANSACTION_ID) != null) {
        transaction = StringUtils.trim(this.getRequest().getParameter(APConstants.TRANSACTION_ID));
        PowbSynthesis history = (PowbSynthesis) auditLogManager.getHistory(transaction);
        if (history != null) {
            powbSynthesis = history;//from  ww w.  j  a  v  a  2s .  com
            powbSynthesisID = powbSynthesis.getId();
        } else {
            this.transaction = null;
            this.setTransaction("-1");
        }
    } else {
        // Get Liaison institution ID Parameter
        try {
            liaisonInstitutionID = Long.parseLong(StringUtils
                    .trim(this.getRequest().getParameter(APConstants.LIAISON_INSTITUTION_REQUEST_ID)));
        } catch (NumberFormatException e) {
            User user = userManager.getUser(this.getCurrentUser().getId());
            if (user.getLiasonsUsers() != null || !user.getLiasonsUsers().isEmpty()) {
                List<LiaisonUser> liaisonUsers = new ArrayList<>(user.getLiasonsUsers().stream()
                        .filter(lu -> lu.isActive() && lu.getLiaisonInstitution().isActive()
                                && lu.getLiaisonInstitution().getCrp().getId() == loggedCrp.getId()
                                && lu.getLiaisonInstitution().getInstitution() == null)
                        .collect(Collectors.toList()));
                if (!liaisonUsers.isEmpty()) {
                    boolean isLeader = false;
                    for (LiaisonUser liaisonUser : liaisonUsers) {
                        LiaisonInstitution institution = liaisonUser.getLiaisonInstitution();
                        if (institution.isActive()) {
                            if (institution.getCrpProgram() != null) {
                                if (institution.getCrpProgram()
                                        .getProgramType() == ProgramType.FLAGSHIP_PROGRAM_TYPE.getValue()) {
                                    liaisonInstitutionID = institution.getId();
                                    isLeader = true;
                                    break;
                                }
                            } else {
                                if (institution.getAcronym().equals("PMU")) {
                                    liaisonInstitutionID = institution.getId();
                                    isLeader = true;
                                    break;
                                }
                            }
                        }
                    }
                    if (!isLeader) {
                        liaisonInstitutionID = this.firstFlagship();
                    }
                } else {
                    liaisonInstitutionID = this.firstFlagship();
                }
            } else {
                liaisonInstitutionID = this.firstFlagship();
            }
        }

        try {
            powbSynthesisID = Long
                    .parseLong(StringUtils.trim(this.getRequest().getParameter(APConstants.POWB_SYNTHESIS_ID)));
            powbSynthesis = powbSynthesisManager.getPowbSynthesisById(powbSynthesisID);

            if (!powbSynthesis.getPhase().equals(phase)) {
                powbSynthesis = powbSynthesisManager.findSynthesis(phase.getId(), liaisonInstitutionID);
                if (powbSynthesis == null) {
                    powbSynthesis = this.createPowbSynthesis(phase.getId(), liaisonInstitutionID);
                }
                powbSynthesisID = powbSynthesis.getId();
            }
        } catch (Exception e) {

            powbSynthesis = powbSynthesisManager.findSynthesis(phase.getId(), liaisonInstitutionID);
            if (powbSynthesis == null) {
                powbSynthesis = this.createPowbSynthesis(phase.getId(), liaisonInstitutionID);
            }
            powbSynthesisID = powbSynthesis.getId();

        }
    }

    if (powbSynthesis != null) {

        PowbSynthesis powbSynthesisDB = powbSynthesisManager.getPowbSynthesisById(powbSynthesisID);
        powbSynthesisID = powbSynthesisDB.getId();
        liaisonInstitutionID = powbSynthesisDB.getLiaisonInstitution().getId();
        liaisonInstitution = liaisonInstitutionManager.getLiaisonInstitutionById(liaisonInstitutionID);

        Path path = this.getAutoSaveFilePath();
        // Verify if there is a Draft file
        if (path.toFile().exists() && this.getCurrentUser().isAutoSave()) {
            BufferedReader reader;
            reader = new BufferedReader(new FileReader(path.toFile()));
            Gson gson = new GsonBuilder().create();
            JsonObject jReader = gson.fromJson(reader, JsonObject.class);
            AutoSaveReader autoSaveReader = new AutoSaveReader();
            powbSynthesis = (PowbSynthesis) autoSaveReader.readFromJson(jReader);
            powbSynthesisID = powbSynthesis.getId();
            this.setDraft(true);
            reader.close();
        } else {
            this.setDraft(false);
            // Check if ToC relation is null -create it
            if (powbSynthesis.getPowbManagementGovernance() == null) {
                PowbManagementGovernance managementGovernance = new PowbManagementGovernance();
                // create one to one relation
                powbSynthesis.setPowbManagementGovernance(managementGovernance);
                managementGovernance.setPowbSynthesis(powbSynthesis);
                // save the changes
                powbSynthesis = powbSynthesisManager.savePowbSynthesis(powbSynthesis);
            }
        }
    }
    if (this.isFlagship()) {
        LiaisonInstitution pmuInstitution = loggedCrp.getLiaisonInstitutions().stream()
                .filter(c -> c.getCrpProgram() == null && c.getAcronym().equals("PMU"))
                .collect(Collectors.toList()).get(0);
        PowbSynthesis powbSynthesisDB = powbSynthesisManager.findSynthesis(phase.getId(),
                pmuInstitution.getId());
        if (powbSynthesisDB != null) {
            if (powbSynthesisDB.getPowbManagementGovernance() != null) {
                powbSynthesis.setPowbManagementGovernance(powbSynthesisDB.getPowbManagementGovernance());

            }
        }
    }
    // Get the list of liaison institutions Flagships and PMU.
    liaisonInstitutions = loggedCrp.getLiaisonInstitutions().stream()
            .filter(c -> c.getCrpProgram() != null && c.isActive()
                    && c.getCrpProgram().getProgramType() == ProgramType.FLAGSHIP_PROGRAM_TYPE.getValue())
            .collect(Collectors.toList());
    liaisonInstitutions.sort(Comparator.comparing(LiaisonInstitution::getAcronym));

    // Setup the PUM Management Risk Table
    if (this.isPMU()) {
        this.managementGovernanceList(phase.getId());
    }
    // ADD PMU as liasion Institutio too
    liaisonInstitutions.addAll(loggedCrp.getLiaisonInstitutions().stream()
            .filter(c -> c.getCrpProgram() == null && c.isActive() && c.getAcronym().equals("PMU"))
            .collect(Collectors.toList()));

    // Base Permission
    String params[] = { loggedCrp.getAcronym(), powbSynthesis.getId() + "" };
    this.setBasePermission(
            this.getText(Permission.POWB_SYNTHESIS_MANAGEMENT_GOVERNANCE_BASE_PERMISSION, params));

    if (this.isHttpPost()) {

    }
}

From source file:com.homeadvisor.kafdrop.service.CuratorKafkaMonitor.java

private Stream<ConsumerVO> getConsumerStream(TopicVO topic) {
    return consumerTreeCache.getCurrentChildren(ZkUtils.ConsumersPath()).keySet().stream()
            .map(g -> getConsumerByTopic(g, topic)).filter(Optional::isPresent).map(Optional::get)
            .sorted(Comparator.comparing(ConsumerVO::getGroupId));
}

From source file:org.cgiar.ccafs.marlo.action.powb.CrossCuttingDimensionsAction.java

@Override
public void prepare() throws Exception {

    // Get current CRP
    loggedCrp = (GlobalUnit) this.getSession().get(APConstants.SESSION_CRP);
    loggedCrp = crpManager.getGlobalUnitById(loggedCrp.getId());
    Phase phase = this.getActualPhase();

    // If there is a history version being loaded
    if (this.getRequest().getParameter(APConstants.TRANSACTION_ID) != null) {
        transaction = StringUtils.trim(this.getRequest().getParameter(APConstants.TRANSACTION_ID));
        PowbSynthesis history = (PowbSynthesis) auditLogManager.getHistory(transaction);
        if (history != null) {
            powbSynthesis = history;//from   ww  w .  j a va2s.c  om
            powbSynthesisID = powbSynthesis.getId();
        } else {
            this.transaction = null;
            this.setTransaction("-1");
        }
    } else {
        // Get Liaison institution ID Parameter
        try {
            liaisonInstitutionID = Long.parseLong(StringUtils
                    .trim(this.getRequest().getParameter(APConstants.LIAISON_INSTITUTION_REQUEST_ID)));
        } catch (NumberFormatException e) {
            User user = userManager.getUser(this.getCurrentUser().getId());
            if (user.getLiasonsUsers() != null || !user.getLiasonsUsers().isEmpty()) {
                List<LiaisonUser> liaisonUsers = new ArrayList<>(user.getLiasonsUsers().stream()
                        .filter(lu -> lu.isActive() && lu.getLiaisonInstitution().isActive()
                                && lu.getLiaisonInstitution().getCrp().getId() == loggedCrp.getId()
                                && lu.getLiaisonInstitution().getInstitution() == null)
                        .collect(Collectors.toList()));
                if (!liaisonUsers.isEmpty()) {
                    boolean isLeader = false;
                    for (LiaisonUser liaisonUser : liaisonUsers) {
                        LiaisonInstitution institution = liaisonUser.getLiaisonInstitution();
                        if (institution.isActive()) {
                            if (institution.getCrpProgram() != null) {
                                if (institution.getCrpProgram()
                                        .getProgramType() == ProgramType.FLAGSHIP_PROGRAM_TYPE.getValue()) {
                                    liaisonInstitutionID = institution.getId();
                                    isLeader = true;
                                    break;
                                }
                            } else {
                                if (institution.getAcronym().equals("PMU")) {
                                    liaisonInstitutionID = institution.getId();
                                    isLeader = true;
                                    break;
                                }
                            }
                        }
                    }
                    if (!isLeader) {
                        liaisonInstitutionID = this.firstFlagship();
                    }
                } else {
                    liaisonInstitutionID = this.firstFlagship();
                }
            } else {
                liaisonInstitutionID = this.firstFlagship();
            }
        }

        try {
            powbSynthesisID = Long
                    .parseLong(StringUtils.trim(this.getRequest().getParameter(APConstants.POWB_SYNTHESIS_ID)));
            powbSynthesis = powbSynthesisManager.getPowbSynthesisById(powbSynthesisID);

            if (!powbSynthesis.getPhase().equals(phase)) {
                powbSynthesis = powbSynthesisManager.findSynthesis(phase.getId(), liaisonInstitutionID);
                if (powbSynthesis == null) {
                    powbSynthesis = this.createPowbSynthesis(phase.getId(), liaisonInstitutionID);
                }
                powbSynthesisID = powbSynthesis.getId();
            }
        } catch (Exception e) {

            powbSynthesis = powbSynthesisManager.findSynthesis(phase.getId(), liaisonInstitutionID);
            if (powbSynthesis == null) {
                powbSynthesis = this.createPowbSynthesis(phase.getId(), liaisonInstitutionID);
            }
            powbSynthesisID = powbSynthesis.getId();

        }
    }

    if (powbSynthesis != null) {
        PowbSynthesis powbSynthesisDB = powbSynthesisManager.getPowbSynthesisById(powbSynthesisID);
        powbSynthesisID = powbSynthesisDB.getId();
        liaisonInstitutionID = powbSynthesisDB.getLiaisonInstitution().getId();
        liaisonInstitution = liaisonInstitutionManager.getLiaisonInstitutionById(liaisonInstitutionID);

        Path path = this.getAutoSaveFilePath();

        // Verify if there is a Draft file
        if (path.toFile().exists() && this.getCurrentUser().isAutoSave()) {
            BufferedReader reader;
            reader = new BufferedReader(new FileReader(path.toFile()));
            Gson gson = new GsonBuilder().create();
            JsonObject jReader = gson.fromJson(reader, JsonObject.class);
            AutoSaveReader autoSaveReader = new AutoSaveReader();
            powbSynthesis = (PowbSynthesis) autoSaveReader.readFromJson(jReader);
            powbSynthesisID = powbSynthesis.getId();
            this.setDraft(true);
            reader.close();
        } else {
            this.setDraft(false);
            // Check if CrossCutting relation is null -create it
            if (powbSynthesis.getPowbCrossCuttingDimension() == null && this.isPMU()) {

                PowbCrossCuttingDimension crossCutting = new PowbCrossCuttingDimension();

                // create one to one relation
                powbSynthesis.setPowbCrossCuttingDimension(crossCutting);
                crossCutting.setPowbSynthesis(powbSynthesis);

                // save the changes
                powbSynthesis = powbSynthesisManager.savePowbSynthesis(powbSynthesis);

            }
        }

    }

    if (this.isFlagship()) {
        LiaisonInstitution pmuInstitution = loggedCrp.getLiaisonInstitutions().stream()
                .filter(c -> c.getCrpProgram() == null && c.getAcronym().equals("PMU"))
                .collect(Collectors.toList()).get(0);
        PowbSynthesis powbSynthesisDB = powbSynthesisManager.findSynthesis(phase.getId(),
                pmuInstitution.getId());
        if (powbSynthesisDB != null) {
            if (powbSynthesisDB.getPowbCrossCuttingDimension() != null) {
                powbSynthesis.setFlagshipAssets(powbSynthesisDB.getPowbCrossCuttingDimension().getAssets());
                powbSynthesis
                        .setFlagshipSummarize(powbSynthesisDB.getPowbCrossCuttingDimension().getSummarize());
            }
        }
    }

    // Get the list of liaison institutions Flagships and PMU.
    liaisonInstitutions = loggedCrp.getLiaisonInstitutions().stream()
            .filter(c -> c.getCrpProgram() != null && c.isActive()
                    && c.getCrpProgram().getProgramType() == ProgramType.FLAGSHIP_PROGRAM_TYPE.getValue())
            .collect(Collectors.toList());
    liaisonInstitutions.sort(Comparator.comparing(LiaisonInstitution::getAcronym));
    // ADD PMU as liasion Institutio too
    liaisonInstitutions.addAll(loggedCrp.getLiaisonInstitutions().stream()
            .filter(c -> c.getCrpProgram() == null && c.getAcronym().equals("PMU"))
            .collect(Collectors.toList()));

    // Get the tableC Information
    this.tableCInfo(phase);

    // Base Permission
    String params[] = { loggedCrp.getAcronym(), powbSynthesis.getId() + "" };
    // this.setBasePermission(this.getText(Permission.POWB_SYNTHESIS_TOC_BASE_PERMISSION, params));
    this.setBasePermission(this.getText(Permission.POWB_SYNTHESIS_CROSS_CUTING_BASE_PERMISSION, params));

}

From source file:org.cgiar.ccafs.marlo.action.annualReport.FinancialSummaryAction.java

@Override
public void prepare() throws Exception {
    // Get current CRP
    loggedCrp = (GlobalUnit) this.getSession().get(APConstants.SESSION_CRP);
    loggedCrp = crpManager.getGlobalUnitById(loggedCrp.getId());
    Phase phase = this.getActualPhase();

    // If there is a history version being loaded
    if (this.getRequest().getParameter(APConstants.TRANSACTION_ID) != null) {
        transaction = StringUtils.trim(this.getRequest().getParameter(APConstants.TRANSACTION_ID));
        ReportSynthesis history = (ReportSynthesis) auditLogManager.getHistory(transaction);
        if (history != null) {
            reportSynthesis = history;/*from  w ww .  ja  va 2s.  c o m*/
            synthesisID = reportSynthesis.getId();
        } else {
            this.transaction = null;
            this.setTransaction("-1");
        }
    } else {
        // Get Liaison institution ID Parameter
        try {
            liaisonInstitutionID = Long.parseLong(StringUtils
                    .trim(this.getRequest().getParameter(APConstants.LIAISON_INSTITUTION_REQUEST_ID)));
        } catch (NumberFormatException e) {
            User user = userManager.getUser(this.getCurrentUser().getId());
            if (user.getLiasonsUsers() != null || !user.getLiasonsUsers().isEmpty()) {
                List<LiaisonUser> liaisonUsers = new ArrayList<>(user.getLiasonsUsers().stream()
                        .filter(lu -> lu.isActive() && lu.getLiaisonInstitution().isActive()
                                && lu.getLiaisonInstitution().getCrp().getId() == loggedCrp.getId()
                                && lu.getLiaisonInstitution().getInstitution() == null)
                        .collect(Collectors.toList()));
                if (!liaisonUsers.isEmpty()) {
                    boolean isLeader = false;
                    for (LiaisonUser liaisonUser : liaisonUsers) {
                        LiaisonInstitution institution = liaisonUser.getLiaisonInstitution();
                        if (institution.isActive()) {
                            if (institution.getCrpProgram() != null) {
                                if (institution.getCrpProgram()
                                        .getProgramType() == ProgramType.FLAGSHIP_PROGRAM_TYPE.getValue()) {
                                    liaisonInstitutionID = institution.getId();
                                    isLeader = true;
                                    break;
                                }
                            } else {
                                if (institution.getAcronym().equals("PMU")) {
                                    liaisonInstitutionID = institution.getId();
                                    isLeader = true;
                                    break;
                                }
                            }
                        }
                    }
                    if (!isLeader) {
                        liaisonInstitutionID = this.firstFlagship();
                    }
                } else {
                    liaisonInstitutionID = this.firstFlagship();
                }
            } else {
                liaisonInstitutionID = this.firstFlagship();
            }
        }

        try {
            synthesisID = Long.parseLong(
                    StringUtils.trim(this.getRequest().getParameter(APConstants.REPORT_SYNTHESIS_ID)));
            reportSynthesis = reportSynthesisManager.getReportSynthesisById(synthesisID);

            if (!reportSynthesis.getPhase().equals(phase)) {
                reportSynthesis = reportSynthesisManager.findSynthesis(phase.getId(), liaisonInstitutionID);
                if (reportSynthesis == null) {
                    reportSynthesis = this.createReportSynthesis(phase.getId(), liaisonInstitutionID);
                }
                synthesisID = reportSynthesis.getId();
            }
        } catch (Exception e) {
            reportSynthesis = reportSynthesisManager.findSynthesis(phase.getId(), liaisonInstitutionID);
            if (reportSynthesis == null) {
                reportSynthesis = this.createReportSynthesis(phase.getId(), liaisonInstitutionID);
            }
            synthesisID = reportSynthesis.getId();
        }
    }

    if (reportSynthesis != null) {

        ReportSynthesis reportSynthesisDB = reportSynthesisManager
                .getReportSynthesisById(reportSynthesis.getId());
        synthesisID = reportSynthesisDB.getId();
        liaisonInstitutionID = reportSynthesisDB.getLiaisonInstitution().getId();
        liaisonInstitution = liaisonInstitutionManager.getLiaisonInstitutionById(liaisonInstitutionID);

        Path path = this.getAutoSaveFilePath();
        // Verify if there is a Draft file
        if (path.toFile().exists() && this.getCurrentUser().isAutoSave()) {
            BufferedReader reader;
            reader = new BufferedReader(new FileReader(path.toFile()));
            Gson gson = new GsonBuilder().create();
            JsonObject jReader = gson.fromJson(reader, JsonObject.class);
            reader.close();
            AutoSaveReader autoSaveReader = new AutoSaveReader();
            reportSynthesis = (ReportSynthesis) autoSaveReader.readFromJson(jReader);
            synthesisID = reportSynthesis.getId();
            this.setDraft(true);
        } else {

            this.setDraft(false);
            // Check if relation is null -create it
            if (reportSynthesis.getReportSynthesisFinancialSummary() == null) {
                ReportSynthesisFinancialSummary financialSummary = new ReportSynthesisFinancialSummary();
                // create one to one relation
                reportSynthesis.setReportSynthesisFinancialSummary(financialSummary);
                ;
                financialSummary.setReportSynthesis(reportSynthesis);
                // save the changes
                reportSynthesis = reportSynthesisManager.saveReportSynthesis(reportSynthesis);
            }

            reportSynthesis.getReportSynthesisFinancialSummary().setBudgets(new ArrayList<>());
            if (this.isPMU()) {
                // Flagships Financial Budgets
                if (reportSynthesis.getReportSynthesisFinancialSummary()
                        .getReportSynthesisFinancialSummaryBudgets() != null
                        && !reportSynthesis.getReportSynthesisFinancialSummary()
                                .getReportSynthesisFinancialSummaryBudgets().isEmpty()) {
                    reportSynthesis.getReportSynthesisFinancialSummary()
                            .setBudgets(new ArrayList<>(reportSynthesis.getReportSynthesisFinancialSummary()
                                    .getReportSynthesisFinancialSummaryBudgets().stream()
                                    .filter(t -> t.isActive()).collect(Collectors.toList())));
                } else {

                    List<LiaisonInstitution> flagshipList = loggedCrp.getLiaisonInstitutions().stream()
                            .filter(c -> c.getCrpProgram() != null && c.isActive() && c.getCrpProgram()
                                    .getProgramType() == ProgramType.FLAGSHIP_PROGRAM_TYPE.getValue())
                            .collect(Collectors.toList());
                    flagshipList.sort(Comparator.comparing(LiaisonInstitution::getAcronym));
                    reportSynthesis.getReportSynthesisFinancialSummary().setBudgets(new ArrayList<>());
                    for (LiaisonInstitution liInstitution : flagshipList) {
                        ReportSynthesisFinancialSummaryBudget financialSummaryBudget = new ReportSynthesisFinancialSummaryBudget();
                        financialSummaryBudget.setLiaisonInstitution(liInstitution);
                        reportSynthesis.getReportSynthesisFinancialSummary().getBudgets()
                                .add(financialSummaryBudget);
                    }

                    List<PowbExpenditureAreas> expAreas = new ArrayList<>(powbExpenditureAreasManager.findAll()
                            .stream().filter(x -> x.isActive() && !x.getIsExpenditure())
                            .collect(Collectors.toList()));
                    for (PowbExpenditureAreas powbExpenditureAreas : expAreas) {
                        ReportSynthesisFinancialSummaryBudget financialSummaryBudget = new ReportSynthesisFinancialSummaryBudget();
                        financialSummaryBudget.setExpenditureArea(powbExpenditureAreas);
                        reportSynthesis.getReportSynthesisFinancialSummary().getBudgets()
                                .add(financialSummaryBudget);
                    }

                }
            }
        }
    }

    // Get the list of liaison institutions Flagships and PMU.
    liaisonInstitutions = loggedCrp.getLiaisonInstitutions().stream()
            .filter(c -> c.getCrpProgram() != null && c.isActive()
                    && c.getCrpProgram().getProgramType() == ProgramType.FLAGSHIP_PROGRAM_TYPE.getValue())
            .collect(Collectors.toList());
    liaisonInstitutions.sort(Comparator.comparing(LiaisonInstitution::getAcronym));

    // ADD PMU as liasion Institution too
    liaisonInstitutions.addAll(loggedCrp.getLiaisonInstitutions().stream()
            .filter(c -> c.getCrpProgram() == null && c.isActive() && c.getAcronym().equals("PMU"))
            .collect(Collectors.toList()));

    // Informative table to Flagships
    if (this.isFlagship()) {
        LiaisonInstitution pmuInstitution = loggedCrp.getLiaisonInstitutions().stream()
                .filter(c -> c.getCrpProgram() == null && c.getAcronym().equals("PMU"))
                .collect(Collectors.toList()).get(0);
        ReportSynthesis reportSynthesisDB = reportSynthesisManager.findSynthesis(phase.getId(),
                pmuInstitution.getId());
        if (reportSynthesisDB != null) {
            if (reportSynthesisDB.getReportSynthesisFinancialSummary() != null) {
                if (reportSynthesisDB.getReportSynthesisFinancialSummary()
                        .getReportSynthesisFinancialSummaryBudgets() != null
                        && !reportSynthesisDB.getReportSynthesisFinancialSummary()
                                .getReportSynthesisFinancialSummaryBudgets().isEmpty()) {
                    reportSynthesis.getReportSynthesisFinancialSummary()
                            .setBudgets(new ArrayList<>(reportSynthesisDB.getReportSynthesisFinancialSummary()
                                    .getReportSynthesisFinancialSummaryBudgets().stream()
                                    .filter(t -> t.isActive()).collect(Collectors.toList())));
                }
            }
        }
    }

    // Base Permission
    String params[] = { loggedCrp.getAcronym(), reportSynthesis.getId() + "" };
    this.setBasePermission(this.getText(Permission.REPORT_SYNTHESIS_CRP_PROGRESS_BASE_PERMISSION, params));

    if (this.isHttpPost()) {
        if (reportSynthesis.getReportSynthesisFinancialSummary().getBudgets() != null) {
            reportSynthesis.getReportSynthesisFinancialSummary().getBudgets().clear();
        }
    }
}

From source file:org.eclipse.winery.accountability.AccountabilityManagerImpl.java

private void fillFilesOfModel(ModelProvenanceElement model) {
    // 1. parse element.state as ManifestContent
    RecoveringManifestParser genericParser = new RecoveringManifestParser();
    ManifestContents manifestContents = genericParser.parse(model.getFingerprint());
    // 2. parse the ManifestContent as a TOSCAMetaFile
    TOSCAMetaFileParser parser = new TOSCAMetaFileParser();
    TOSCAMetaFile toscaMetaFile = parser.parse(manifestContents, genericParser.getProblems().size());
    //3. retrieve files from meta file
    Objects.requireNonNull(toscaMetaFile);

    List<FileProvenanceElement> result = toscaMetaFile.getFileBlocks().stream().map(fileSection -> {
        FileProvenanceElement fileElement = new FileProvenanceElement(model);
        fileElement.setFileHash(fileSection.get(TOSCAMetaFileAttributes.HASH));
        fileElement.setAddressInImmutableStorage(fileSection.get(TOSCAMetaFileAttributes.IMMUTABLE_ADDRESS));
        fileElement.setFileName(fileSection.get(TOSCAMetaFileAttributes.NAME));

        return fileElement;
    }).sorted(Comparator.comparing(FileProvenanceElement::getFileName)).collect(Collectors.toList());

    model.setFiles(result);//from   ww  w.j a v  a 2s . c  o m
}

From source file:org.apache.nifi.authorization.FileAccessPolicyProvider.java

@Override
public String getFingerprint() throws AuthorizationAccessException {
    final List<AccessPolicy> policies = new ArrayList<>(getAccessPolicies());
    Collections.sort(policies, Comparator.comparing(AccessPolicy::getIdentifier));

    XMLStreamWriter writer = null;
    final StringWriter out = new StringWriter();
    try {//from ww w.  jav a  2s . c  om
        writer = XML_OUTPUT_FACTORY.createXMLStreamWriter(out);
        writer.writeStartDocument();
        writer.writeStartElement("accessPolicies");

        for (AccessPolicy policy : policies) {
            writePolicy(writer, policy);
        }

        writer.writeEndElement();
        writer.writeEndDocument();
        writer.flush();
    } catch (XMLStreamException e) {
        throw new AuthorizationAccessException("Unable to generate fingerprint", e);
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (XMLStreamException e) {
                // nothing to do here
            }
        }
    }

    return out.toString();
}