⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 reporthandler.java

📁 jsf example about book manager
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
            addMessage("registry_error", e.getMessage());
            return "error";
        }

        if (!rules.canReject(currentUser, isManager, currentReport)) {
            addMessage("report_no_reject_access", null);
            return "error";
        }

        String outcome = "success";
        int currentStatus = currentReport.getStatus();
        currentReport.setStatus(Report.STATUS_REJECTED);
        try {
            saveReport();
        }
        catch (RegistryException e) {
            addMessage("registry_error", e.getMessage());
            currentReport.setStatus(currentStatus);
            outcome = "error";
        }
        return outcome;
    }

    /**
     * Makes the report at the current row in the reports DataModel the
     * current report, or queues an error message if the current user isn't
     * allowed to do that or the registry throws an exception.
     */
    public String select() {
        Report selectedReport = (Report) reportsModel.getRowData();
        if (!rules.canView(currentUser, isManager, selectedReport)) {
            addMessage("report_no_view_access", null);
            return "error";
        }
        setCurrentReport(selectedReport);
        return "success";
    }

    /**
     * Adds an entry to the current report, or queues an error message if
     * the current user isn't allowed to do that or the registry
     * throws an exception.
     */
    public String addEntry(ReportEntry entry) {
        try {
            refreshCache();
        }
        catch (RegistryException e) {
            addMessage("registry_error", e.getMessage());
            return "error";
        }
        
        if (!rules.canEdit(currentUser, isManager, currentReport)) {
            addMessage("report_no_edit_access", null);
            return "error";
        }

        String outcome = "success";
        currentReport.addEntry(entry);
        try {
            saveReport();
        }
        catch (RegistryException e) {
            currentReport.removeEntry(entry.getId());
            addMessage("registry_error", e.getMessage());
            outcome = "error";
        }
        return outcome;
    }

    /**
     * Removes the entry represented by the current row in the entries
     * DataModel from the current report, or queues an error message if
     * the current user isn't allowed to do that or the registry
     * throws an exception.
     */
    public String removeEntry() {
        ReportEntry selectedEntry = 
            (ReportEntry) entriesModel.getRowData();
        int entryId = selectedEntry.getId();
        return removeEntry(entryId);
    }

    /**
     * Removes the specified entry from the current report, or queues
     * an error message if the current user isn't allowed to do that or
     * the registry throws an exception.
     */
    public String removeEntry(int entryId) {
        try {
            refreshCache();
        }
        catch (RegistryException e) {
            addMessage("registry_error", e.getMessage());
            return "error";
        }

        if (!rules.canEdit(currentUser, isManager, currentReport)) {
            addMessage("report_no_edit_access", null);
            return "error";
        }

        String outcome = "success";
        ReportEntry currentEntry = currentReport.getEntry(entryId);
        currentReport.removeEntry(entryId);
        try {
            saveReport();
        }
        catch (RegistryException e) {
            addMessage("registry_error", e.getMessage());
            currentReport.addEntry(currentEntry);
            outcome = "error";
        }
        return outcome;
    }

    /**
     * Uses the entry represented by the current row in the entries
     * DataModel to update the corresponding entry in the current report,
     * or queues an error message if the current user isn't allowed to
     * do that or the registry throws an exception.
     */
    public String updateEntry() {
        ReportEntry selectedEntry = 
            (ReportEntry) entriesModel.getRowData();
        int entryId = selectedEntry.getId();
        return updateEntry(selectedEntry);
    }

    /**
     * Uses the provided entry to update the corresponding entry in the
     * current report, or queues an error message if the current user
     * isn't allowed to do that or the registry throws an exception.
     */
    public String updateEntry(ReportEntry entry) {
        try {
            refreshCache();
        }
        catch (RegistryException e) {
            addMessage("registry_error", e.getMessage());
            return "error";
        }

        if (!rules.canEdit(currentUser, isManager, currentReport)) {
            addMessage("report_no_edit_access", null);
            return "error";
        }

        String outcome = "success";
        ReportEntry currentEntry = currentReport.getEntry(entry.getId());
        currentReport.removeEntry(entry.getId());
        currentReport.addEntry(entry);
        try {
            saveReport();
        }
        catch (RegistryException e) {
            addMessage("registry_error", e.getMessage());
            currentReport.removeEntry(entry.getId());
            currentReport.addEntry(currentEntry);
            outcome = "error";
        }
        return outcome;
    }

    /**
     * Returns "true" if the current report is new (no entries yet).
     */
    public boolean isNewDisabled() {
        return isReportNew();
    }

    /**
     * Returns "true" if the current report is new (no entries yet) or
     * the current user isn't allowed to delete the report.
     */
    public boolean isDeleteDisabled() {
        return isReportNew() || 
            !rules.canDelete(currentUser, isManager, currentReport);
    }

    /**
     * Returns "true" if the current report is new (no entries yet) or
     * the current user isn't allowed to submit the report.
     */
    public boolean isSubmitDisabled() {
        return isReportNew() || 
            !rules.canSubmit(currentUser, isManager, currentReport);
    }

    /**
     * Returns "true" if the current report is new (no entries yet) or
     * the current user isn't allowed to accept the report.
     */
    public boolean isAcceptDisabled() {
        return isReportNew() || 
            !rules.canAccept(currentUser, isManager, currentReport);
    }

    /**
     * Returns "true" if the current user is a manager.
     */
    public boolean isAcceptRendered() {
        return isManager;
    }

    /**
     * Returns "true" if the current report is new (no entries yet) or
     * the current user isn't allowed to reject the report.
     */
    public boolean isRejectDisabled() {
        return isReportNew() || 
            !rules.canReject(currentUser, isManager, currentReport);
    }

    /**
     * Returns "true" if the current user is a manager.
     */
    public boolean isRejectRendered() {
        return isManager;
    }

    /**
     * Returns "true" if the current report isn't new (no entries yet) and
     * the current user isn't allowed to edit the report.
     */
    public boolean isEditDisabled() {
        return !isReportNew() &&
            !rules.canEdit(currentUser, isManager, currentReport);
    }

    /**
     * Returns "true" if the current user is associated with the
     * "manager" role.
     */
    public boolean isManager() {
        FacesContext context = FacesContext.getCurrentInstance();
        ExternalContext ec = context.getExternalContext();
        return ec.isUserInRole("manager");
    }


    /*
     * Methods releated to sorting the reports list.
     */

    /**
     * Sets the sorting column for the reports list to the "title" column,
     * reversing the order if the table is already sorted by this column.
     */
    public String sortByTitle() {
        if (sortBy == SORT_BY_TITLE) {
            ascending = !ascending;
        }
        else {
            sortBy = SORT_BY_TITLE;
            ascending = true;
        }
        return "success";
    }

    /**
     * Sets the sorting column for the reports list to the "owner" column,
     * reversing the order if the table is already sorted by this column.
     */
    public String sortByOwner() {
        if (sortBy == SORT_BY_OWNER) {
            ascending = !ascending;
        }
        else {
            sortBy = SORT_BY_OWNER;
            ascending = true;
        }
        return "success";
    }

    /**
     * Sets the sorting column for the reports list to the "date" column,
     * reversing the order if the table is already sorted by this column.
     */
    public String sortByDate() {
        if (sortBy == SORT_BY_DATE) {
            ascending = !ascending;
        }
        else {
            sortBy = SORT_BY_DATE;
            ascending = false;
        }
        return "success";
    }

    /**
     * Sets the sorting column for the reports list to the "total" column,
     * reversing the order if the table is already sorted by this column.
     */
    public String sortByTotal() {
        if (sortBy == SORT_BY_TOTAL) {
            ascending = !ascending;
        }
        else {
            sortBy = SORT_BY_TOTAL;
            ascending = true;
        }
        return "success";
    }

    /**
     * Sets the sorting column for the reports list to the "status" column,
     * reversing the order if the table is already sorted by this column.
     */
    public String sortByStatus() {
        if (sortBy == SORT_BY_STATUS) {
            ascending = !ascending;
        }
        else {
            sortBy = SORT_BY_STATUS;
            ascending = true;
        }
        return "success";
    }

    /**
     * Sorts the reports according to the current sort column and order.
     */
    private void sortReports(List reports) {
        switch (sortBy) {
            case SORT_BY_TITLE:
                Collections.sort(reports, 
                    ascending ? ASC_TITLE_COMPARATOR : DESC_TITLE_COMPARATOR);
                break;
            case SORT_BY_OWNER:
                Collections.sort(reports, 
                    ascending ? ASC_OWNER_COMPARATOR : DESC_OWNER_COMPARATOR);
                break;
            case SORT_BY_DATE:
                Collections.sort(reports, 
                    ascending ? ASC_DATE_COMPARATOR : DESC_DATE_COMPARATOR);
                break;
            case SORT_BY_TOTAL:
                Collections.sort(reports, 
                    ascending ? ASC_TOTAL_COMPARATOR : DESC_TOTAL_COMPARATOR);
                break;
            case SORT_BY_STATUS:
                Collections.sort(reports, 
                    ascending ? ASC_STATUS_COMPARATOR : DESC_STATUS_COMPARATOR);
                break;
        }
    }

    private static final Comparator ASC_TITLE_COMPARATOR = new Comparator() {
            public int compare(Object o1, Object o2) {
                String s1 = ((Report) o1).getTitle();
                String s2 = ((Report) o2).getTitle();
                return s1.compareTo(s2);
            }
        };

    private static final Comparator DESC_TITLE_COMPARATOR = new Comparator() {
            public int compare(Object o1, Object o2) {
                String s1 = ((Report) o1).getTitle();
                String s2 = ((Report) o2).getTitle();
                return s2.compareTo(s1);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -