com.mycompany.expense
Class ReportHandler

java.lang.Object
  extended bycom.mycompany.expense.ReportHandler

public class ReportHandler
extends java.lang.Object

This class contains properties and methods for the JSF components in the report area of the sample expense report application.

Version:
1.0
Author:
Hans Bergsten, Gefion Software

Nested Class Summary
static class ReportHandler.Page
          This class represents a page in the page navigation bar model.
 
Field Summary
private static java.util.Comparator ASC_DATE_COMPARATOR
           
private static java.util.Comparator ASC_OWNER_COMPARATOR
           
private static java.util.Comparator ASC_STATUS_COMPARATOR
           
private static java.util.Comparator ASC_TITLE_COMPARATOR
           
private static java.util.Comparator ASC_TOTAL_COMPARATOR
           
private  boolean ascending
           
private  Report currentReport
           
private  java.lang.String currentUser
           
private static java.util.Comparator DESC_DATE_COMPARATOR
           
private static java.util.Comparator DESC_OWNER_COMPARATOR
           
private static java.util.Comparator DESC_STATUS_COMPARATOR
           
private static java.util.Comparator DESC_TITLE_COMPARATOR
           
private static java.util.Comparator DESC_TOTAL_COMPARATOR
           
private  javax.faces.model.DataModel entriesModel
           
private  int firstRowIndex
           
private  java.util.Date from
           
private  boolean isManager
           
private  int noOfPageLinks
           
private  int noOfRows
           
private  ReportRegistry registry
           
private  javax.faces.model.DataModel reportsModel
           
private  Rules rules
           
private static int SORT_BY_DATE
           
private static int SORT_BY_OWNER
           
private static int SORT_BY_STATUS
           
private static int SORT_BY_TITLE
           
private static int SORT_BY_TOTAL
           
private  int sortBy
           
private  int[] status
           
private  java.util.Date to
           
 
Constructor Summary
ReportHandler()
          Creates a new instance, initialized with a new Report and information about the current user.
 
Method Summary
 java.lang.String accept()
          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.
 java.lang.String addEntry(ReportEntry entry)
          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.
private  void addMessage(java.lang.String messageKey, java.lang.Object param)
          Adds the message matching the key in the application's resource bundle, formatted with the parameters (if any), the the JSF message queue as a global message.
 java.lang.String create()
          Creates a new Report and makes it the current report.
private  Report createNewReport()
          Creates a new Report instance initialized with the current user as the owner, and resets the cached entries DataModel.
 java.lang.String delete()
          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.
 int getCurrentPage()
          Returns the index for the page matching the currently rendered set of reports in the reports list.
 Report getCurrentReport()
          Returns the current Report instance, or a new instance if there's no current instance.
 java.util.List getCurrentReportEntries()
          Returns a List with the ReportEntry instances for the current Report, sorted on the entry dates.
private  java.lang.String getCurrentUser()
          Returns the username of the current, authenticated user.
 int getFirstPageIndex()
          Returns the index for the first page link to render.
 int getFirstRowIndex()
          Returns the index for the first row in the reports table.
 java.util.Date getFrom()
          Returns the from date, or a Date representing the previous month if no from date is set.
 int getNoOfPageLinks()
          Returns the number of links to render in the navigation bar.
 int getNoOfRows()
          Returns the number of rows to show in the reports table.
 java.util.List getPages()
          Returns a List with Page instances for each page of reports.
private  java.util.Date getPreviousMonth(java.util.Date current)
          Returns a Date one month prior to the provided Date.
 javax.faces.model.DataModel getReportEntriesModel()
          Returns a DataModel with the ReportEntry instances for the current Report, sorted on the entry dates.
 java.util.List getReports()
          Returns a List with Report instances matching the filtering criteria.
 javax.faces.model.DataModel getReportsModel()
          Returns a DataModel with Report instances matching the filtering criteria.
 javax.faces.model.DataModel getSortedReportsModel()
          Returns a DataModel with Report instances matching the filtering criteria, sorted according to the current sort column and order.
 java.lang.String[] getStatus()
          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.
 java.util.Date getTo()
          Returns the to date, or a Date representing today if no to date is set.
 boolean isAcceptDisabled()
          Returns "true" if the current report is new (no entries yet) or the current user isn't allowed to accept the report.
 boolean isAcceptRendered()
          Returns "true" if the current user is a manager.
 boolean isDeleteDisabled()
          Returns "true" if the current report is new (no entries yet) or the current user isn't allowed to delete the report.
 boolean isEditDisabled()
          Returns "true" if the current report isn't new (no entries yet) and the current user isn't allowed to edit the report.
 boolean isManager()
          Returns "true" if the current user is associated with the "manager" role.
 boolean isNewDisabled()
          Returns "true" if the current report is new (no entries yet).
 boolean isRejectDisabled()
          Returns "true" if the current report is new (no entries yet) or the current user isn't allowed to reject the report.
 boolean isRejectRendered()
          Returns "true" if the current user is a manager.
private  boolean isReportNew()
          Returns "true" if the current report has status "new".
 boolean isScrollFirstDisabled()
          Returns "true" if the first page of reports is displayed.
 boolean isScrollLastDisabled()
          Returns "true" if the last page of reports is displayed.
 boolean isScrollNextDisabled()
          Returns "true" if there aren't enough rows to scroll forward one page.
 boolean isScrollPreviousDisabled()
          Returns "true" if the first page is displayed.
 boolean isSubmitDisabled()
          Returns "true" if the current report is new (no entries yet) or the current user isn't allowed to submit the report.
private  void refreshCache()
          If the current report isn't new (i.e., not yet stored), refreshes the current report with a copy from the registry to ensure the local copy is the latest version.
 java.lang.String reject()
          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.
 java.lang.String removeEntry()
          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.
 java.lang.String removeEntry(int 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.
private  void saveReport()
          If the current report is new, changes its status to "open" and add it to the registry; otherwise, updates the report in the registry.
 java.lang.String scrollFirst()
          Sets the index for the first row to display in the reports list to zero.
 java.lang.String scrollLast()
          Sets the index for the first row to display in the reports list to the index of the top row for the last page.
 java.lang.String scrollNext()
          Sets the index for the first row to display in the reports table to the index of the top row for the next page, or to zero if there is no more page.
 java.lang.String scrollPrevious()
          Sets the index for the first row to display in the reports table to the index of the top row for the previou page, or to zero if there is no more page.
 java.lang.String select()
          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.
private  void setCurrentPage(int currentPage)
          Sets the index for the first row in the reports table to match the specified page number.
private  void setCurrentReport(Report report)
          Makes the provided Report the current report, and resets the cached entries DataModel.
 void setFrom(java.util.Date from)
          Sets the from date.
 void setNoOfRows(int noOfRows)
          Sets the number of rows to show in the reports table.
 void setReportRegistry(ReportRegistry registry)
          Sets the ReportRegistry instance used by this application.
 void setStatus(java.lang.String[] stringStatus)
          Sets the status codes to display.
 void setTo(java.util.Date to)
          Sets the to date.
 java.lang.String sortByDate()
          Sets the sorting column for the reports list to the "date" column, reversing the order if the table is already sorted by this column.
 java.lang.String sortByOwner()
          Sets the sorting column for the reports list to the "owner" column, reversing the order if the table is already sorted by this column.
 java.lang.String sortByStatus()
          Sets the sorting column for the reports list to the "status" column, reversing the order if the table is already sorted by this column.
 java.lang.String sortByTitle()
          Sets the sorting column for the reports list to the "title" column, reversing the order if the table is already sorted by this column.
 java.lang.String sortByTotal()
          Sets the sorting column for the reports list to the "total" column, reversing the order if the table is already sorted by this column.
private  void sortReports(java.util.List reports)
          Sorts the reports according to the current sort column and order.
 java.lang.String submit()
          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.
 java.lang.String updateEntry()
          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.
 java.lang.String updateEntry(ReportEntry entry)
          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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SORT_BY_TITLE

private static final int SORT_BY_TITLE
See Also:
Constant Field Values

SORT_BY_OWNER

private static final int SORT_BY_OWNER
See Also:
Constant Field Values

SORT_BY_DATE

private static final int SORT_BY_DATE
See Also:
Constant Field Values

SORT_BY_TOTAL

private static final int SORT_BY_TOTAL
See Also:
Constant Field Values

SORT_BY_STATUS

private static final int SORT_BY_STATUS
See Also:
Constant Field Values

registry

private ReportRegistry registry

rules

private Rules rules

currentReport

private Report currentReport

currentUser

private java.lang.String currentUser

isManager

private boolean isManager

reportsModel

private javax.faces.model.DataModel reportsModel

entriesModel

private javax.faces.model.DataModel entriesModel

from

private java.util.Date from

to

private java.util.Date to

status

private int[] status

ascending

private boolean ascending

sortBy

private int sortBy

noOfRows

private int noOfRows

firstRowIndex

private int firstRowIndex

noOfPageLinks

private int noOfPageLinks

ASC_TITLE_COMPARATOR

private static final java.util.Comparator ASC_TITLE_COMPARATOR

DESC_TITLE_COMPARATOR

private static final java.util.Comparator DESC_TITLE_COMPARATOR

ASC_OWNER_COMPARATOR

private static final java.util.Comparator ASC_OWNER_COMPARATOR

DESC_OWNER_COMPARATOR

private static final java.util.Comparator DESC_OWNER_COMPARATOR

ASC_DATE_COMPARATOR

private static final java.util.Comparator ASC_DATE_COMPARATOR

DESC_DATE_COMPARATOR

private static final java.util.Comparator DESC_DATE_COMPARATOR

ASC_TOTAL_COMPARATOR

private static final java.util.Comparator ASC_TOTAL_COMPARATOR

DESC_TOTAL_COMPARATOR

private static final java.util.Comparator DESC_TOTAL_COMPARATOR

ASC_STATUS_COMPARATOR

private static final java.util.Comparator ASC_STATUS_COMPARATOR

DESC_STATUS_COMPARATOR

private static final java.util.Comparator DESC_STATUS_COMPARATOR
Constructor Detail

ReportHandler

public ReportHandler()
Creates a new instance, initialized with a new Report and information about the current user.

Method Detail

setReportRegistry

public void setReportRegistry(ReportRegistry registry)
Sets the ReportRegistry instance used by this application.


getCurrentReport

public Report getCurrentReport()
Returns the current Report instance, or a new instance if there's no current instance.


getReports

public java.util.List getReports()
Returns a List with Report instances matching the filtering criteria.


getReportsModel

public javax.faces.model.DataModel getReportsModel()
Returns a DataModel with Report instances matching the filtering criteria.


getSortedReportsModel

public javax.faces.model.DataModel getSortedReportsModel()
Returns a DataModel with Report instances matching the filtering criteria, sorted according to the current sort column and order.


getFrom

public java.util.Date getFrom()
Returns the from date, or a Date representing the previous month if no from date is set.


setFrom

public void setFrom(java.util.Date from)
Sets the from date.


getTo

public java.util.Date getTo()
Returns the to date, or a Date representing today if no to date is set.


setTo

public void setTo(java.util.Date to)
Sets the to date.


getStatus

public java.lang.String[] getStatus()
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.


setStatus

public void setStatus(java.lang.String[] stringStatus)
Sets the status codes to display.


getCurrentReportEntries

public java.util.List getCurrentReportEntries()
Returns a List with the ReportEntry instances for the current Report, sorted on the entry dates.


getReportEntriesModel

public javax.faces.model.DataModel getReportEntriesModel()
Returns a DataModel with the ReportEntry instances for the current Report, sorted on the entry dates.


create

public java.lang.String create()
Creates a new Report and makes it the current report.


delete

public java.lang.String delete()
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.


submit

public java.lang.String submit()
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.


accept

public java.lang.String accept()
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.


reject

public java.lang.String reject()
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.


select

public java.lang.String select()
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.


addEntry

public java.lang.String addEntry(ReportEntry entry)
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.


removeEntry

public java.lang.String removeEntry()
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.


removeEntry

public java.lang.String removeEntry(int 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.


updateEntry

public java.lang.String updateEntry()
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.


updateEntry

public java.lang.String updateEntry(ReportEntry entry)
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.


isNewDisabled

public boolean isNewDisabled()
Returns "true" if the current report is new (no entries yet).


isDeleteDisabled

public boolean isDeleteDisabled()
Returns "true" if the current report is new (no entries yet) or the current user isn't allowed to delete the report.


isSubmitDisabled

public boolean isSubmitDisabled()
Returns "true" if the current report is new (no entries yet) or the current user isn't allowed to submit the report.


isAcceptDisabled

public boolean isAcceptDisabled()
Returns "true" if the current report is new (no entries yet) or the current user isn't allowed to accept the report.


isAcceptRendered

public boolean isAcceptRendered()
Returns "true" if the current user is a manager.


isRejectDisabled

public boolean isRejectDisabled()
Returns "true" if the current report is new (no entries yet) or the current user isn't allowed to reject the report.


isRejectRendered

public boolean isRejectRendered()
Returns "true" if the current user is a manager.


isEditDisabled

public boolean isEditDisabled()
Returns "true" if the current report isn't new (no entries yet) and the current user isn't allowed to edit the report.


isManager

public boolean isManager()
Returns "true" if the current user is associated with the "manager" role.


sortByTitle

public java.lang.String sortByTitle()
Sets the sorting column for the reports list to the "title" column, reversing the order if the table is already sorted by this column.


sortByOwner

public java.lang.String sortByOwner()
Sets the sorting column for the reports list to the "owner" column, reversing the order if the table is already sorted by this column.


sortByDate

public java.lang.String sortByDate()
Sets the sorting column for the reports list to the "date" column, reversing the order if the table is already sorted by this column.


sortByTotal

public java.lang.String sortByTotal()
Sets the sorting column for the reports list to the "total" column, reversing the order if the table is already sorted by this column.


sortByStatus

public java.lang.String sortByStatus()
Sets the sorting column for the reports list to the "status" column, reversing the order if the table is already sorted by this column.


sortReports

private void sortReports(java.util.List reports)
Sorts the reports according to the current sort column and order.


getNoOfRows

public int getNoOfRows()
Returns the number of rows to show in the reports table.


setNoOfRows

public void setNoOfRows(int noOfRows)
Sets the number of rows to show in the reports table.


getFirstRowIndex

public int getFirstRowIndex()
Returns the index for the first row in the reports table.


isScrollFirstDisabled

public boolean isScrollFirstDisabled()
Returns "true" if the first page of reports is displayed.


isScrollLastDisabled

public boolean isScrollLastDisabled()
Returns "true" if the last page of reports is displayed.


isScrollNextDisabled

public boolean isScrollNextDisabled()
Returns "true" if there aren't enough rows to scroll forward one page.


isScrollPreviousDisabled

public boolean isScrollPreviousDisabled()
Returns "true" if the first page is displayed.


scrollFirst

public java.lang.String scrollFirst()
Sets the index for the first row to display in the reports list to zero.


scrollLast

public java.lang.String scrollLast()
Sets the index for the first row to display in the reports list to the index of the top row for the last page.


scrollNext

public java.lang.String scrollNext()
Sets the index for the first row to display in the reports table to the index of the top row for the next page, or to zero if there is no more page.


scrollPrevious

public java.lang.String scrollPrevious()
Sets the index for the first row to display in the reports table to the index of the top row for the previou page, or to zero if there is no more page.


getPages

public java.util.List getPages()
Returns a List with Page instances for each page of reports.


getNoOfPageLinks

public int getNoOfPageLinks()
Returns the number of links to render in the navigation bar.


getFirstPageIndex

public int getFirstPageIndex()
Returns the index for the first page link to render.


getCurrentPage

public int getCurrentPage()
Returns the index for the page matching the currently rendered set of reports in the reports list.


setCurrentPage

private void setCurrentPage(int currentPage)
Sets the index for the first row in the reports table to match the specified page number.


saveReport

private void saveReport()
                 throws RegistryException
If the current report is new, changes its status to "open" and add it to the registry; otherwise, updates the report in the registry. Resets the cached entries DataModel.

Throws:
RegistryException

refreshCache

private void refreshCache()
                   throws RegistryException
If the current report isn't new (i.e., not yet stored), refreshes the current report with a copy from the registry to ensure the local copy is the latest version.

Throws:
RegistryException

createNewReport

private Report createNewReport()
Creates a new Report instance initialized with the current user as the owner, and resets the cached entries DataModel.


setCurrentReport

private void setCurrentReport(Report report)
Makes the provided Report the current report, and resets the cached entries DataModel.


getPreviousMonth

private java.util.Date getPreviousMonth(java.util.Date current)
Returns a Date one month prior to the provided Date.


getCurrentUser

private java.lang.String getCurrentUser()
Returns the username of the current, authenticated user.


addMessage

private void addMessage(java.lang.String messageKey,
                        java.lang.Object param)
Adds the message matching the key in the application's resource bundle, formatted with the parameters (if any), the the JSF message queue as a global message.


isReportNew

private boolean isReportNew()
Returns "true" if the current report has status "new".