001    package hirondelle.web4jtools.metrics.files;
002    
003    import java.util.*;
004    import hirondelle.web4j.request.RequestParameter;
005    import hirondelle.web4j.request.RequestParser;
006    import hirondelle.web4j.util.Util;
007    import hirondelle.web4j.action.ResponsePage;
008    import hirondelle.web4jtools.util.TemplatedPage;
009    import hirondelle.web4jtools.metrics.base.FileInfo;
010    import hirondelle.web4jtools.metrics.base.MetricsAction;
011    import hirondelle.web4jtools.metrics.base.BaseInfoAction;
012    
013    /**
014    * Show a sortable file listing. 
015    * 
016    * <P>The file listing is not hierarchical. It is simply a list of all files in your project.
017    * The default sorting is by extension.
018    */
019    public final class FilesAction extends MetricsAction {
020    
021      public FilesAction(RequestParser aRequestParser){
022        super(FORWARD, aRequestParser);
023      }
024    
025      /** Sort the files in the desired way, and present to the user.  */
026      protected void calculateMetric() {
027        sort(fFileList);
028        //retain the sort by replacing the session scope object
029        addToSession(BaseInfoAction.FILE_INFO_LIST_KEY, fFileList);
030      }
031      
032      /** The column to sort by. See the <tt>sortLinks.tag</tt> file. */
033      public static final RequestParameter SORT_ON = RequestParameter.withRegexCheck("SortOn", "(Extension|FullName|Lines|Modified|Bytes)");
034      /** Ascending or descending order. See the <tt>sortLinks.tag</tt> file. */
035      public static final RequestParameter ORDER = RequestParameter.withRegexCheck("Order", "(ASC|DESC)");
036      
037      // PRIVATE //
038      private static final ResponsePage FORWARD = new ResponsePage("File Listing" , "view.jsp", FilesAction.class);
039      
040      private void sort(List<FileInfo> aFileInfos){
041        String sortOn = getParamUnsafe(SORT_ON);
042        String order = getParamUnsafe(ORDER);
043        Collections.sort(aFileInfos, getComparator(sortOn));
044        if( "DESC".equals(order) ) {
045          Collections.reverse(aFileInfos);
046        }
047      }
048      
049      private Comparator<FileInfo> getComparator(String aSortOn){
050        Comparator<FileInfo> result = null;
051        //default sorting by extension
052        if( "Extension".equals(aSortOn) || ! Util.textHasContent(aSortOn) ) {
053          result = new Comparator<FileInfo>() {
054            public int compare(FileInfo aThis, FileInfo aThat) {
055              return aThis.getExtension().compareTo(aThat.getExtension());
056            };
057          };
058        }
059        else if ( "FullName".equals(aSortOn) ) {
060          result = new Comparator<FileInfo>() {
061            public int compare(FileInfo aThis, FileInfo aThat) {
062              return aThis.getName().compareTo(aThat.getName());
063            };
064          };
065        }
066        else if ( "Lines".equals(aSortOn) ) {
067          result = new Comparator<FileInfo>() {
068            public int compare(FileInfo aThis, FileInfo aThat) {
069              return aThis.getNumLines().compareTo(aThat.getNumLines());
070            };
071          };
072        }
073        else if ( "Modified".equals(aSortOn) ) {
074          result = new Comparator<FileInfo>() {
075            public int compare(FileInfo aThis, FileInfo aThat) {
076              return aThis.getLastModified().compareTo(aThat.getLastModified());
077            };
078          };
079        }
080        else if ( "Bytes".equals(aSortOn) ) {
081          result = new Comparator<FileInfo>() {
082            public int compare(FileInfo aThis, FileInfo aThat) {
083              return aThis.getSize().compareTo(aThat.getSize());
084            };
085          };
086        }
087        else {
088          throw new AssertionError("Unknown sort column : " + Util.quote(aSortOn));
089        }
090        return result;
091      }
092    }