📄 reporthandler.java
字号:
package com.mycompany.expense;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.servlet.http.HttpServletRequest;
/**
* This class contains properties and methods for the JSF components
* in the report area of the sample expense report application.
*
* @author Hans Bergsten, Gefion Software <hans@gefionsoftware.com>
* @version 1.0
*/
public class ReportHandler {
private static final int SORT_BY_TITLE = 0;
private static final int SORT_BY_OWNER = 1;
private static final int SORT_BY_DATE = 2;
private static final int SORT_BY_TOTAL = 3;
private static final int SORT_BY_STATUS = 4;
private ReportRegistry registry;
private Rules rules;
private Report currentReport;
private String currentUser;
private boolean isManager;
private DataModel reportsModel;
private DataModel entriesModel;
private Date from;
private Date to;
private int[] status;
private boolean ascending = false;
private int sortBy = SORT_BY_DATE;
private int noOfRows = 5;
private int firstRowIndex = 0;
private int noOfPageLinks = 5;
/**
* Creates a new instance, initialized with a new Report and
* information about the current user.
*/
public ReportHandler() {
rules = new Rules();
currentReport = getCurrentReport();
currentUser = getCurrentUser();
isManager = isManager();
}
/**
* Sets the ReportRegistry instance used by this application.
*/
public void setReportRegistry(ReportRegistry registry) {
this.registry = registry;
}
/**
* Returns the current Report instance, or a new instance
* if there's no current instance.
*/
public Report getCurrentReport() {
if (currentReport == null) {
currentReport = createNewReport();
}
return currentReport;
}
/**
* Returns a List with Report instances matching the filtering
* criteria.
*/
public List getReports() {
String user = null;
if (!isManager) {
user = getCurrentUser();
}
List l = null;
try {
l = registry.getReports(user, from, to, status);
}
catch (RegistryException e) {
addMessage("registry_error", e.getMessage());
}
return l;
}
/**
* Returns a DataModel with Report instances matching the
* filtering criteria.
*/
public DataModel getReportsModel() {
if (reportsModel == null) {
reportsModel = new ListDataModel();
}
reportsModel.setWrappedData(getReports());
return reportsModel;
}
/**
* Returns a DataModel with Report instances matching the
* filtering criteria, sorted according to the current
* sort column and order.
*/
public DataModel getSortedReportsModel() {
if (reportsModel == null) {
reportsModel = new ListDataModel();
}
List reports = getReports();
sortReports(reports);
reportsModel.setWrappedData(reports);
return reportsModel;
}
/*
* Methods related to the report filtering.
*/
/**
* Returns the from date, or a Date representing the previous
* month if no from date is set.
*/
public Date getFrom() {
if (from == null) {
from = getPreviousMonth(new Date());
}
return from;
}
/**
* Sets the from date.
*/
public void setFrom(Date from) {
this.from = from;
}
/**
* Returns the to date, or a Date representing today if no
* to date is set.
*/
public Date getTo() {
if (to == null) {
to = new Date();
}
return to;
}
/**
* Sets the to date.
*/
public void setTo(Date to) {
this.to = to;
}
/**
* Returns the status codes for displayed reports, or the
* code for "Submitted" if no status code is set and the
* current user is a manager, or all status codes if no
* status code is set and the current user isn't a manager.
*/
public String[] getStatus() {
if (status == null) {
if (isManager) {
status = new int[1];
status[0] = Report.STATUS_SUBMITTED;
}
else {
status = new int[4];
status[0] = Report.STATUS_OPEN;
status[1] = Report.STATUS_SUBMITTED;
status[2] = Report.STATUS_ACCEPTED;
status[3] = Report.STATUS_REJECTED;
}
}
// Convert the int[] to a String[] to match the SelectItem type
String[] stringStatus = new String[status.length];
for (int i = 0; i < status.length; i++) {
stringStatus[i] = String.valueOf(status[i]);
}
return stringStatus;
}
/**
* Sets the status codes to display.
*/
public void setStatus(String[] stringStatus) {
// Convert the String[], matching the SelectItem type, to
// the int[] used internally
status = null;
if (stringStatus != null) {
status = new int[stringStatus.length];
for (int i = 0; i < stringStatus.length; i++) {
status[i] = Integer.valueOf(stringStatus[i]).intValue();
}
}
}
/*
* Methods related to the report entries for the current report.
*/
/**
* Returns a List with the ReportEntry instances for the current
* Report, sorted on the entry dates.
*/
public List getCurrentReportEntries() {
List currentList = currentReport.getEntries();
Collections.sort(currentList, new Comparator() {
public int compare(Object o1, Object o2) {
Date d1 = ((ReportEntry) o1).getDate();
Date d2 = ((ReportEntry) o2).getDate();
return d1.compareTo(d2);
}
});
return currentList;
}
/**
* Returns a DataModel with the ReportEntry instances for the current
* Report, sorted on the entry dates.
*/
public DataModel getReportEntriesModel() {
if (entriesModel == null) {
entriesModel = new ListDataModel();
entriesModel.setWrappedData(getCurrentReportEntries());
}
return entriesModel;
}
/**
* Creates a new Report and makes it the current report.
*/
public String create() {
currentReport = createNewReport();
return "success";
}
/**
* Deletes 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 delete() {
try {
refreshCache();
}
catch (RegistryException e) {
addMessage("registry_error", e.getMessage());
return "error";
}
if (!rules.canDelete(currentUser, isManager, currentReport)) {
addMessage("report_no_delete_access", null);
return "error";
}
String outcome = "success";
try {
registry.removeReport(currentReport);
currentReport = createNewReport();
}
catch (RegistryException e) {
addMessage("registry_error", e.getMessage());
outcome = "error";
}
return outcome;
}
/**
* Submits 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 submit() {
try {
refreshCache();
}
catch (RegistryException e) {
addMessage("registry_error", e.getMessage());
return "error";
}
if (!rules.canSubmit(currentUser, isManager, currentReport)) {
addMessage("report_no_submit_access", null);
return "error";
}
String outcome = "success";
int currentStatus = currentReport.getStatus();
currentReport.setStatus(Report.STATUS_SUBMITTED);
try {
saveReport();
}
catch (RegistryException e) {
addMessage("registry_error", e.getMessage());
currentReport.setStatus(currentStatus);
outcome = "error";
}
return outcome;
}
/**
* Accepts 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 accept() {
try {
refreshCache();
}
catch (RegistryException e) {
addMessage("registry_error", e.getMessage());
return "error";
}
if (!rules.canAccept(currentUser, isManager, currentReport)) {
addMessage("report_no_accept_access", null);
return "error";
}
String outcome = "success";
int currentStatus = currentReport.getStatus();
currentReport.setStatus(Report.STATUS_ACCEPTED);
try {
saveReport();
}
catch (RegistryException e) {
addMessage("registry_error", e.getMessage());
currentReport.setStatus(currentStatus);
outcome = "error";
}
return outcome;
}
/**
* Rejects 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 reject() {
try {
refreshCache();
}
catch (RegistryException e) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -