001    package hirondelle.web4jtools.metrics.base;
002    
003    import java.util.List;
004    import hirondelle.web4j.action.ActionImpl;
005    import hirondelle.web4j.model.AppException;
006    import hirondelle.web4j.request.RequestParser;
007    import hirondelle.web4j.action.ResponsePage;
008    
009    /**
010    * Abstract Base Class for most Actions related to metrics. 
011    * 
012    * <P>Confirms that a scan of the base directory has been performed. 
013    * If not, then the user is advised to use the Summary page first.
014    */
015    public abstract class MetricsAction extends ActionImpl {
016    
017      /** Full constructor. */
018      public MetricsAction(ResponsePage aResponsePage, RequestParser aRequestParser) {
019        super(aResponsePage, aRequestParser);
020      }
021      
022      /**
023      * <b>Template</b> method. 
024      * 
025      *  If the {@link #fFileList} is present in session scope as expected, then {@link #calculateMetric()} 
026      *  is called; otherwise, an error message is presented to the user. 
027      */
028      @Override public ResponsePage execute() throws AppException {
029        fFileList = (List<FileInfo>)getFromSession(BaseInfoAction.FILE_INFO_LIST_KEY);
030        if ( fFileList == null ) {
031          addError(ERROR_MESSAGE);
032        }
033        else {
034          calculateMetric();
035        }
036        return getResponsePage();
037      }
038      
039      /** Abstract method implemented by subclasses, and called by {@link #execute}. */
040      protected abstract void calculateMetric();
041      
042      /**
043      * If this {@code List<FileInfo>} is not in session scope, then an error message is 
044      * presented to the user. This is is basic data structure used for source code statistics. 
045      * It represents data that is read in when the user 'scans the source code', 
046      * using the Summary page.
047      */
048      protected List<FileInfo> fFileList;
049      
050      private static final String ERROR_MESSAGE = "Please use Summary page to first scan source code.";
051    }