001    package hirondelle.web4jtools.logview.simpleview;
002    
003    import hirondelle.web4j.model.ModelCtorException;
004    import hirondelle.web4j.model.Check;
005    import static hirondelle.web4j.util.Consts.FAILS;
006    import hirondelle.web4j.model.ModelUtil;
007    import hirondelle.web4j.util.Util;
008    import hirondelle.web4j.security.SafeText;
009    
010    /**
011    * Model Object for the criteria entered by the user for viewing log files as simple text, 
012    * in an unparsed form. 
013    */
014    public final class SimpleCriteria {
015      
016      /**
017      * Full constructor. 
018      * 
019      * @param aLogFor toggle between application logs and server logs (required). See {@link LogFor}.
020      * @param aSection either the first or last section of a log file; required only if <tt>aNumLines</tt> is specified. See {@link Section}.
021      * @param aNumLines number of lines to display; required only if <tt>aSection</tt> is specified; range <tt>1..10,000</tt>.
022      * @param aRefreshRate frequency in seconds in which to refresh the listing; range <tt>1..600</tt>.
023      */
024      public SimpleCriteria(SafeText aLogFor, SafeText aSection, Integer aNumLines, Integer aRefreshRate) throws ModelCtorException {
025        fLogFor = Util.textHasContent(aLogFor) ? LogFor.valueOf(aLogFor.getRawString()) : null;
026        fSection = Util.textHasContent(aSection)? Section.valueOf(aSection.getRawString()) : null;
027        fNumLines = aNumLines;
028        fRefreshRate = aRefreshRate;
029        validateState();
030      }
031      
032      Integer getNumLines(){ return fNumLines; }
033      Integer getRefreshRate(){ return fRefreshRate; }
034      Section getSection(){ return fSection; } 
035      LogFor getLogFor() { return fLogFor; }
036    
037     @Override public String toString(){
038        return ModelUtil.toStringFor(this);
039      }
040      
041      @Override public  boolean equals(Object aThat){
042        Boolean result = ModelUtil.quickEquals(this, aThat);
043        if ( result ==  null ) {
044          SimpleCriteria that = (SimpleCriteria) aThat;
045          result = ModelUtil.equalsFor(this.getSignificantFields(), that.getSignificantFields());
046        }
047        return result;
048      }
049      
050      @Override public int hashCode(){
051         return ModelUtil.hashCodeFor(getSignificantFields());
052      }  
053      
054      // PRIVATE //
055      private Integer fNumLines;
056      private Integer fRefreshRate;
057      private Section fSection;
058      private LogFor fLogFor;
059      
060      private void validateState() throws ModelCtorException {
061        ModelCtorException ex = new ModelCtorException();
062        if ( FAILS == Check.required(fLogFor) ) {
063          ex.add("You must select Application or Server.");
064        }
065        if ( FAILS == Check.optional(fNumLines, Check.range(1,10000)) ) {
066          ex.add("Num Lines is optional, 1..10,000.");
067        }
068        if ( FAILS == Check.optional(fRefreshRate, Check.range(1,600)) ) {
069          ex.add("Refresh Rate is optional, 1..600 seconds.");
070        }
071        if ( fNumLines == null && fSection != null ) {
072          ex.add("Num Lines must be specified.");
073        }
074        if ( fNumLines != null && fSection == null ) {
075          ex.add("Section must be specified.");
076        }
077        if ( ! ex.isEmpty() ) throw ex;    
078      }
079      
080      private Object[] getSignificantFields(){
081        return new Object[]{fLogFor, fNumLines, fRefreshRate, fSection};
082      }
083    }