knguyen     2005/04/28 16:06:25 CEST

  Modified files:
    core/src/java/org/jahia/data/containers 
                                            ContainerFilterBean.java 
                                            ContainerFilterByCategories.java 
                                            ContainerSorterBean.java 
                                            
ContainerSorterByContainerDefinition.java 
                                            FilterClause.java 
  Added files:
    core/src/java/org/jahia/data/containers NumberFormats.java 
  Log:
  - update container filter and sorter to support all number format ( float, 
double, ...) while the default format used if no indication is given is Long.
  
  Revision  Changes    Path
  1.11      +58 -67    
jahia/core/src/java/org/jahia/data/containers/ContainerFilterBean.java
http://jahia.mine.nu:8080/cgi-bin/cvsweb.cgi/jahia/core/src/java/org/jahia/data/containers/ContainerFilterBean.java.diff?r1=1.10&r2=1.11&f=h
  1.4       +18 -3     
jahia/core/src/java/org/jahia/data/containers/ContainerFilterByCategories.java
http://jahia.mine.nu:8080/cgi-bin/cvsweb.cgi/jahia/core/src/java/org/jahia/data/containers/ContainerFilterByCategories.java.diff?r1=1.3&r2=1.4&f=h
  1.10      +112 -90   
jahia/core/src/java/org/jahia/data/containers/ContainerSorterBean.java
http://jahia.mine.nu:8080/cgi-bin/cvsweb.cgi/jahia/core/src/java/org/jahia/data/containers/ContainerSorterBean.java.diff?r1=1.9&r2=1.10&f=h
  1.8       +89 -23    
jahia/core/src/java/org/jahia/data/containers/ContainerSorterByContainerDefinition.java
http://jahia.mine.nu:8080/cgi-bin/cvsweb.cgi/jahia/core/src/java/org/jahia/data/containers/ContainerSorterByContainerDefinition.java.diff?r1=1.7&r2=1.8&f=h
  1.4       +30 -17    
jahia/core/src/java/org/jahia/data/containers/FilterClause.java
http://jahia.mine.nu:8080/cgi-bin/cvsweb.cgi/jahia/core/src/java/org/jahia/data/containers/FilterClause.java.diff?r1=1.3&r2=1.4&f=h
  1.1       +211 -0    
jahia/core/src/java/org/jahia/data/containers/NumberFormats.java (new)
http://jahia.mine.nu:8080/cgi-bin/cvsweb.cgi/jahia/core/src/java/org/jahia/data/containers/NumberFormats.java?rev=1.1&content-type=text/plain
  
  
  
  Index: NumberFormats.java
  ====================================================================
  package org.jahia.data.containers;
  
  import java.util.List;
  import java.util.ArrayList;
  import java.util.Arrays;
  
  /**
   * Created by IntelliJ IDEA.
   * User: hollis
   * Date: 28 avr. 2005
   * Time: 11:56:54
   * To change this template use File | Settings | File Templates.
   */
  public class NumberFormats {
  
      public static final String BYTE_FORMAT = "Byte";
      public static final String SHORT_FORMAT = "Short";
      public static final String INTEGER_FORMAT = "Integer";
      public static final String LONG_FORMAT = "Long";
      public static final String FLOAT_FORMAT = "Float";
      public static final String DOUBLE_FORMAT = "Double";
  
      private static final String[] formats = 
{BYTE_FORMAT,SHORT_FORMAT,INTEGER_FORMAT,
                                              
LONG_FORMAT,FLOAT_FORMAT,DOUBLE_FORMAT};
  
      private static List formatsList;
  
      static {
          formatsList = Arrays.asList(formats);
      }
  
      /**
       * Return true if the format is the one of the above
       *
       * @param format
       * @return
       */
      public static boolean isValidFormat(String format){
          if ( format == null ){
              return false;
          }
          return formatsList.contains(format);
      }
  
      /**
       *
       * @param v1
       * @param v2
       * @param format
       * @return
       */
      public static int compareNumber(String v1,
                                      String v2,
                                      String format){
         int result = 0;
         if ( BYTE_FORMAT.equals(format) ){
             return compareByte(v1,v2);
         } else if ( SHORT_FORMAT.equals(format) ){
             return compareShort(v1,v2);
         } else if ( INTEGER_FORMAT.equals(format) ){
             return compareInteger(v1,v2);
         } else if ( LONG_FORMAT.equals(format) ){
             return compareLong(v1,v2);
         } else if ( FLOAT_FORMAT.equals(format) ){
             return compareFloat(v1,v2);
         } else if ( DOUBLE_FORMAT.equals(format) ){
             return compareDouble(v1,v2);
         }
         return result;
      }
  
  
     public static int compareByte(String v1, String v2){
          int result = 0;
          byte n1 = 0;
          byte n2 = 0;
  
          try {
              n1 = Byte.parseByte(v1);
          } catch ( Throwable t){
              n1 = Byte.MIN_VALUE;
          }
          try {
              n2 = Byte.parseByte(v2);
          } catch ( Throwable t){
              n2 = Byte.MIN_VALUE;
          }
          if ( n1 == n2 ){
              return 0;
          } else if (  n1 > n2 ){
              return 1;
          }
          return -1;
      }
  
      public static int compareShort(String v1, String v2){
          int result = 0;
          short n1 = 0;
          short n2 = 0;
  
          try {
              n1 = Short.parseShort(v1);
          } catch ( Throwable t){
              n1 = Short.MIN_VALUE;
          }
          try {
              n2 = Short.parseShort(v2);
          } catch ( Throwable t){
              n2 = Short.MIN_VALUE;
          }
          if ( n1 == n2 ){
              return 0;
          } else if (  n1 > n2 ){
              return 1;
          }
          return -1;
      }
  
      public static int compareInteger(String v1, String v2){
          int result = 0;
          int n1 = 0;
          int n2 = 0;
  
          try {
              n1 = Integer.parseInt(v1);
          } catch ( Throwable t){
              n1 = Integer.MIN_VALUE;
          }
          try {
              n2 = Integer.parseInt(v2);
          } catch ( Throwable t){
              n2 = Integer.MIN_VALUE;
          }
          if ( n1 == n2 ){
              return 0;
          } else if (  n1 > n2 ){
              return 1;
          }
          return -1;
      }
  
      public static int compareLong(String v1, String v2){
          int result = 0;
          long n1 = 0;
          long n2 = 0;
  
          try {
              n1 = Long.parseLong(v1);
          } catch ( Throwable t){
              n1 = Long.MIN_VALUE;
          }
          try {
              n2 = Long.parseLong(v2);
          } catch ( Throwable t){
              n2 = Long.MIN_VALUE;
          }
          if ( n1 == n2 ){
              return 0;
          } else if (  n1 > n2 ){
              return 1;
          }
          return -1;
      }
  
      public static int compareFloat(String v1, String v2){
          int result = 0;
          float n1 = 0;
          float n2 = 0;
  
          try {
              n1 = Float.parseFloat(v1);
          } catch ( Throwable t){
              n1 = Float.MIN_VALUE;
          }
          try {
              n2 = Float.parseFloat(v2);
          } catch ( Throwable t){
              n2 = Float.MIN_VALUE;
          }
          if ( n1 == n2 ){
              return 0;
          } else if (  n1 > n2 ){
              return 1;
          }
          return -1;
      }
  
      public static int compareDouble(String v1, String v2){
          int result = 0;
          double n1 = 0;
          double n2 = 0;
  
          try {
              n1 = Double.parseDouble(v1);
          } catch ( Throwable t){
              n1 = Double.MIN_VALUE;
          }
          try {
              n2 = Double.parseDouble(v2);
          } catch ( Throwable t){
              n2 = Double.MIN_VALUE;
          }
          if ( n1 == n2 ){
              return 0;
          } else if (  n1 > n2 ){
              return 1;
          }
          return -1;
      }
  
  }
  
  
  
  Index: ContainerFilterBean.java
  ===================================================================
  RCS file: 
/home/cvs/repository/jahia/core/src/java/org/jahia/data/containers/ContainerFilterBean.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- ContainerFilterBean.java  3 Mar 2005 12:16:06 -0000       1.10
  +++ ContainerFilterBean.java  28 Apr 2005 14:06:24 -0000      1.11
  @@ -93,18 +93,20 @@
       public static final String FIELD_WORKFLOW_STATE = "b.workflow_state";
       public static final String FIELD_LANGUAGE_CODE = "b.language_code";
   
  -    private String fieldName;
  +    protected String fieldName;
   
  -    private boolean numberFiltering = false;
  +    protected boolean numberFiltering = false;
   
  -    private boolean multipleFieldValue = false;
  +    protected String numberFormat = NumberFormats.LONG_FORMAT;
  +    
  +    protected boolean multipleFieldValue = false;
   
       /** The list of FilterClause bean **/
  -    private Vector clauses = new Vector();
  +    protected Vector clauses = new Vector();
   
  -    private EntryLoadRequest entryLoadRequest = EntryLoadRequest.CURRENT;
  +    protected EntryLoadRequest entryLoadRequest = EntryLoadRequest.CURRENT;
   
  -    private ContainerFilters containerFilters = null;
  +    protected ContainerFilters containerFilters = null;
   
       
//--------------------------------------------------------------------------
       /**
  @@ -114,8 +116,7 @@
        * @deprecated use ContainerFilterBean(String fieldName, 
EntryLoadRequest entryLoadRequest)
        */
       public ContainerFilterBean(String fieldName){
  -        this.fieldName = fieldName;
  -        logger.debug("Created with field name : " + fieldName);
  +        this(fieldName,null);
       }
   
       
//--------------------------------------------------------------------------
  @@ -126,11 +127,7 @@
        * @param entryLoadRequest
        */
       public ContainerFilterBean(String fieldName, EntryLoadRequest 
entryLoadRequest){
  -        this.fieldName = fieldName;
  -        if ( entryLoadRequest != null ){
  -            this.entryLoadRequest = entryLoadRequest;
  -        }
  -        logger.debug("Created with field name : " + fieldName);
  +        this(fieldName,false,entryLoadRequest);
       }
   
       
//--------------------------------------------------------------------------
  @@ -142,9 +139,7 @@
        * @deprecated use ContainerFilterBean(String fieldName, 
EntryLoadRequest entryLoadRequest)
        */
       public ContainerFilterBean(String fieldName, boolean numberFiltering){
  -        this.fieldName = fieldName;
  -        this.numberFiltering = numberFiltering;
  -        logger.debug("Created with field name : " + fieldName);
  +        this(fieldName,numberFiltering,null);
       }
   
       
//--------------------------------------------------------------------------
  @@ -156,12 +151,7 @@
        * @param entryLoadRequest
        */
       public ContainerFilterBean(String fieldName, boolean numberFiltering, 
EntryLoadRequest entryLoadRequest){
  -        this.fieldName = fieldName;
  -        this.numberFiltering = numberFiltering;
  -        if ( entryLoadRequest != null ){
  -            this.entryLoadRequest = entryLoadRequest;
  -        }
  -        logger.debug("Created with field name : " + fieldName);
  +        this(fieldName,numberFiltering,false,entryLoadRequest);
       }
   
       
//--------------------------------------------------------------------------
  @@ -176,9 +166,27 @@
       public ContainerFilterBean(String fieldName, boolean numberFiltering,
                                  boolean multipleFieldValue,
                                  EntryLoadRequest entryLoadRequest){
  +        
this(fieldName,numberFiltering,null,multipleFieldValue,entryLoadRequest);
  +    }
  +
  +    /**
  +     *
  +     * @param fieldName
  +     * @param boolean numberFiltering, if true force to convert filed value 
to number representation
  +     * @param numberFormat, only used if numberSort is true. If null, the 
format used is NumberFormat.LONG_FORMAT
  +     * @param multipleFieldValue
  +     * @param entryLoadRequest
  +     */
  +    public ContainerFilterBean(String fieldName, boolean numberFiltering,
  +                               String numberFormat,
  +                               boolean multipleFieldValue,
  +                               EntryLoadRequest entryLoadRequest){
           this.fieldName = fieldName;
           this.numberFiltering = numberFiltering;
           this.multipleFieldValue = multipleFieldValue;
  +        if ( NumberFormats.isValidFormat(numberFormat) ){
  +            this.numberFormat = numberFormat;
  +        }
           if ( entryLoadRequest != null ){
               this.entryLoadRequest = entryLoadRequest;
           }
  @@ -199,7 +207,7 @@
        *
        * </pre>
        *
  -     * @param String comparator, the comparator used to compare the field 
value.
  +     * @param String comparator, the comparator used to compareNumber the 
field value.
        * @param String value, a single value
        */
       public void addClause(String comparator, String value){
  @@ -230,7 +238,7 @@
        *
        * </pre>
        *
  -     * @param String comparator, the comparator used to compare the field 
value with each value of the values array.
  +     * @param String comparator, the comparator used to compareNumber the 
field value with each value of the values array.
        * @param String[] values, an array of values as String
        */
       public void addClause(String comparator, String[] values){
  @@ -783,35 +791,26 @@
           Enumeration keys = datas.keys();
           Integer I = null;
           String S = null;
  -        Long L = null;
   
           while ( keys.hasMoreElements() )
           {
               I = (Integer)keys.nextElement();
               S = (String)datas.get(I);
  -            L = null;
  -            try {
  -                L = Long.valueOf(S);
  -            } catch ( Throwable t ){
  -            }
  -            if ( L != null )
  +            boolean match = false;
  +            int size = this.clauses.size();
  +            int i = 0;
  +            while ( (i<size) && !match )
               {
  -                boolean match = false;
  -                int size = this.clauses.size();
  -                int i = 0;
  -                while ( (i<size) && !match )
  -                {
  -                    fClause = (FilterClause)this.clauses.get(i);
  -                    if ( fClause != null && fClause.isValid() )
  -                    {
  -                        match = fClause.compare(L.longValue());
  -                    }
  -                    i++;
  -                }
  -                if ( match )
  +                fClause = (FilterClause)this.clauses.get(i);
  +                if ( fClause != null && fClause.isValid() )
                   {
  -                    result.set(I.intValue());
  +                    match = fClause.compareNumber(S,this.numberFormat);
                   }
  +                i++;
  +            }
  +            if ( match )
  +            {
  +                result.set(I.intValue());
               }
           }
   
  @@ -1508,39 +1507,31 @@
           Enumeration keys = datas.keys();
           Integer I = null;
           String S = null;
  -        Long L = null;
  -
  +        String v = null;
           while ( keys.hasMoreElements() )
           {
               I = (Integer)keys.nextElement();
               S = (String)datas.get(I);
  -            L = null;
               boolean match = false;
               String[] vals = 
JahiaTools.getTokens(S,JahiaField.MULTIPLE_VALUES_SEP);
               int nbVals = vals.length;
               int i = 0;
               while ( (i<nbVals) && !match ){
  -                try {
  -                    L = Long.valueOf((String)vals[i]);
  -                } catch ( Throwable t ){
  -                }
  -                if ( L != null )
  +                v = (String)vals[i];
  +                int size = this.clauses.size();
  +                int j = 0;
  +                while ( (j<size) && !match )
                   {
  -                    int size = this.clauses.size();
  -                    int j = 0;
  -                    while ( (j<size) && !match )
  -                    {
  -                        fClause = (FilterClause)this.clauses.get(j);
  -                        if ( fClause != null && fClause.isValid() )
  -                        {
  -                            match = fClause.compare(L.longValue());
  -                        }
  -                        j++;
  -                    }
  -                    if ( match )
  +                    fClause = (FilterClause)this.clauses.get(j);
  +                    if ( fClause != null && fClause.isValid() )
                       {
  -                        result.set(I.intValue());
  +                        match = fClause.compareNumber(v,this.numberFormat);
                       }
  +                    j++;
  +                }
  +                if ( match )
  +                {
  +                    result.set(I.intValue());
                   }
                   i++;
               }
  @@ -1870,7 +1861,7 @@
       /**
        * Check if the comparator is valid
        *
  -     * @param String comparator, the comparator
  +     * @param comparator the comparator
        */
       private boolean checkComparator(String comparator){
           if ( comparator == null )
  
  
  
  Index: ContainerFilterByCategories.java
  ===================================================================
  RCS file: 
/home/cvs/repository/jahia/core/src/java/org/jahia/data/containers/ContainerFilterByCategories.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ContainerFilterByCategories.java  12 Aug 2004 13:15:59 -0000      1.3
  +++ ContainerFilterByCategories.java  28 Apr 2005 14:06:24 -0000      1.4
  @@ -27,6 +27,7 @@
   import java.util.Vector;
   
   import org.jahia.content.ObjectKey;
  +import org.jahia.content.ContentDefinition;
   import org.jahia.exceptions.JahiaException;
   import org.jahia.registries.ServicesRegistry;
   import org.jahia.services.categories.Category;
  @@ -229,7 +230,7 @@
               for ( int i=0 ; i<size; i++ ){
                   ContentContainer contentContainer =
                           (ContentContainer)containers.get(i);
  -                // @todo : filter out here
  +
                   result.set(contentContainer.getID());
               }
           }catch ( Throwable t ){
  @@ -247,7 +248,7 @@
           }
           ContainerFilterByLoadRequest cfblr =
                   new ContainerFilterByLoadRequest(this.entryLoadRequest);
  -        BitSet result2 = cfblr.doFilterBySite(-1,null,-1);
  +        BitSet result2 = 
cfblr.doFilterBySite(siteId,containerDefinitionName,listId);
           result.and(result2);
           return result;
       }
  @@ -293,7 +294,21 @@
                           ContentContainer contentContainer =
                                   
(ContentContainer)ContentContainer.getInstance(curObjectKey);
                           if ( contentContainer != null ){
  -                          val.add(contentContainer);
  +                            if ( containerDefinitionName != null ) {
  +                                try {
  +                                    ContentDefinition definition = 
ContentDefinition
  +                                            
.getContentDefinitionInstance(contentContainer.getDefinitionKey(null));
  +                                    if ( definition != null &&
  +                                            
containerDefinitionName.equalsIgnoreCase(definition.getName()) ){
  +                                        val.add(contentContainer);
  +                                    }
  +                                } catch ( Throwable t) {
  +                                    logger.debug("Error retrieving container 
definition for container "
  +                                            + contentContainer.getID(),t);
  +                                }
  +                            } else {
  +                                val.add(contentContainer);
  +                            }
                           }
                       } catch (Throwable t){
                           logger.debug("Error loading contentContainer " + 
curObjectKey.toString());
  
  
  
  Index: ContainerSorterBean.java
  ===================================================================
  RCS file: 
/home/cvs/repository/jahia/core/src/java/org/jahia/data/containers/ContainerSorterBean.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- ContainerSorterBean.java  17 Nov 2004 15:01:03 -0000      1.9
  +++ ContainerSorterBean.java  28 Apr 2005 14:06:24 -0000      1.10
  @@ -35,6 +35,7 @@
   import java.util.Locale;
   import java.util.TimeZone;
   import java.util.Vector;
  +import java.text.Collator;
   
   import org.jahia.bin.Jahia;
   import org.jahia.data.fields.ExpressionMarker;
  @@ -60,26 +61,30 @@
       private static org.apache.log4j.Logger logger =
           org.apache.log4j.Logger.getLogger(ContainerSorterBean.class);
   
  +
  +
       private static final String CLASS_NAME = 
ContainerSorterBean.class.getName();
   
  -    private int ctnListID = -1;
  +    protected int ctnListID = -1;
   
  -    private String fieldName;
  +    protected String fieldName;
   
  -    private boolean updated = false;
  +    protected boolean updated = false;
   
  -    private long lastSortingTime = -1;
  +    protected long lastSortingTime = -1;
   
  -    private boolean numberSort = false;
  +    protected boolean numberSort = false;
   
  -    private boolean isValid = false;
  +    protected String numberFormat = NumberFormats.LONG_FORMAT;
   
  -    private boolean ASC_Ordering = true; // by default ASCENDANT ORDER.
  +    protected boolean isValid = false;
  +
  +    protected boolean ASC_Ordering = true; // by default ASCENDANT ORDER.
   
       //** sorted ctnids **/
  -    private Vector result;
  +    protected Vector result;
   
  -    private EntryLoadRequest entryLoadRequest = EntryLoadRequest.CURRENT;
  +    protected EntryLoadRequest entryLoadRequest = EntryLoadRequest.CURRENT;
   
       protected ContainerSorterBean(){}
   
  @@ -94,12 +99,7 @@
       public ContainerSorterBean(int ctnListID, String fieldName)
       throws JahiaException
       {
  -        if ( ctnListID >0 && fieldName != null && 
!fieldName.trim().equals("") )
  -        {
  -            this.ctnListID = ctnListID;
  -            this.fieldName = fieldName;
  -            this.isValid = true;
  -        }
  +        this(ctnListID,fieldName,null);
       }
   
       
//--------------------------------------------------------------------------
  @@ -115,15 +115,7 @@
                                  EntryLoadRequest entryLoadRequest)
       throws JahiaException
       {
  -        if ( ctnListID >0 && fieldName != null && 
!fieldName.trim().equals("") )
  -        {
  -            this.ctnListID = ctnListID;
  -            this.fieldName = fieldName;
  -            this.isValid = true;
  -        }
  -        if (entryLoadRequest != null){
  -            this.entryLoadRequest = entryLoadRequest;
  -        }
  +        this(ctnListID,fieldName,false,entryLoadRequest);
       }
   
       
//--------------------------------------------------------------------------
  @@ -140,11 +132,32 @@
                                   EntryLoadRequest entryLoadRequest)
       throws JahiaException
       {
  +        this(ctnListID, fieldName, numberSort, null, entryLoadRequest);
  +    }
  +
  +    
//--------------------------------------------------------------------------
  +    /**
  +     * Constructor
  +     *
  +     * @param int ctnListID, the container list id.
  +     * @param String the field name, the field on which to sort.
  +     * @param boolean , force field values to be converted to number 
representation before sorting ( if true ).
  +     * @param numberFormat, only used if numberSort is true. If null, the 
format used is NumberFormat.LONG_FORMAT
  +     * @param entryLoadRequest
  +     * @throws JahiaException
  +     */
  +    public ContainerSorterBean(int ctnListID, String fieldName, boolean 
numberSort,
  +                               String numberFormat, EntryLoadRequest 
entryLoadRequest)
  +    throws JahiaException
  +    {
           if ( ctnListID >0 && fieldName != null && 
!fieldName.trim().equals("") )
           {
               this.ctnListID = ctnListID;
               this.fieldName = fieldName;
               this.numberSort = numberSort;
  +            if ( NumberFormats.isValidFormat(numberFormat) ){
  +                this.numberFormat = numberFormat;
  +            }
               this.isValid = true;
           }
           if (entryLoadRequest != null){
  @@ -159,22 +172,12 @@
        * @param String containerListName, the container list name.
        * @param ParamBean, the param bean.
        * @param String the field name, the field on which to sort.
  +     * @deprecated
        */
       public ContainerSorterBean(String containerListName, ParamBean params, 
String fieldName)
       throws JahiaException
       {
  -        logger.debug("Created container sort for : " + containerListName);
  -
  -        if ( containerListName != null ){
  -            int clistID = 
ServicesRegistry.getInstance().getJahiaContainersService().
  -               getContainerListID( containerListName, 
params.getPage().getID() );
  -            if ( clistID >0 && fieldName != null && 
!fieldName.trim().equals("") )
  -            {
  -                this.ctnListID = clistID;
  -                this.fieldName = fieldName;
  -                this.isValid = true;
  -            }
  -        }
  +        
this(containerListName,params,fieldName,params.getEntryLoadRequest());
       }
   
       
//--------------------------------------------------------------------------
  @@ -191,21 +194,7 @@
                                  EntryLoadRequest entryLoadRequest)
       throws JahiaException
       {
  -        logger.debug("Created container sort for : " + containerListName);
  -
  -        if ( containerListName != null ){
  -            int clistID = 
ServicesRegistry.getInstance().getJahiaContainersService().
  -               getContainerListID( containerListName, 
params.getPage().getID() );
  -            if ( clistID >0 && fieldName != null && 
!fieldName.trim().equals("") )
  -            {
  -                this.ctnListID = clistID;
  -                this.fieldName = fieldName;
  -                this.isValid = true;
  -            }
  -        }
  -        if (entryLoadRequest != null){
  -            this.entryLoadRequest = entryLoadRequest;
  -        }
  +        this(containerListName,params,fieldName,false,entryLoadRequest);
       }
   
       
//--------------------------------------------------------------------------
  @@ -216,25 +205,12 @@
        * @param ParamBean, the param bean.
        * @param String the field name, the field on which to sort.
        * @param boolean , force field values to be converted to long 
representation before sorting ( if true ).
  +     * @deprecated
        */
       public ContainerSorterBean(String containerListName, ParamBean params, 
String fieldName, boolean numberSort)
       throws JahiaException
       {
  -        logger.debug("Created container sort for clist: " + 
containerListName + " on the field " + fieldName);
  -
  -        if ( containerListName != null ){
  -            int clistID = 
ServicesRegistry.getInstance().getJahiaContainersService().
  -               getContainerListID( containerListName, 
params.getPage().getID() );
  -            if ( clistID >0 && fieldName != null && 
!fieldName.trim().equals("") )
  -            {
  -                this.ctnListID = clistID;
  -                this.fieldName = fieldName;
  -                this.numberSort = numberSort;
  -                this.isValid = true;
  -
  -                //JahiaConsole.println("ContainerSorterBean","Constructor 
sorter is valid ");
  -            }
  -        }
  +        
this(containerListName,params,fieldName,numberSort,params.getEntryLoadRequest());
       }
   
       
//--------------------------------------------------------------------------
  @@ -252,6 +228,26 @@
                                  boolean numberSort, EntryLoadRequest 
entryLoadRequest)
       throws JahiaException
       {
  +        
this(containerListName,params,fieldName,numberSort,null,entryLoadRequest);
  +    }
  +
  +    
//--------------------------------------------------------------------------
  +    /**
  +     * Constructor
  +     *
  +     * @param String containerListName, the container list name.
  +     * @param ParamBean, the param bean.
  +     * @param String the field name, the field on which to sort.
  +     * @param boolean , force field values to be converted to number 
representation before sorting ( if true ).
  +     * @param numberFormat, only used if numberSort is true. If null, the 
format used is NumberFormat.LONG_FORMAT
  +     * @param entryLoadRequest
  +     * @throws JahiaException
  +     */
  +    public ContainerSorterBean(String containerListName, ParamBean params, 
String fieldName,
  +                               boolean numberSort, String numberFormat,
  +                               EntryLoadRequest entryLoadRequest)
  +    throws JahiaException
  +    {
           logger.debug("Created container sort for clist: " + 
containerListName + " on the field " + fieldName);
   
           if ( containerListName != null ){
  @@ -263,15 +259,20 @@
                   this.fieldName = fieldName;
                   this.numberSort = numberSort;
                   this.isValid = true;
  -
  +                if ( NumberFormats.isValidFormat(numberFormat) ){
  +                    this.numberFormat = numberFormat;
  +                }
                   //JahiaConsole.println("ContainerSorterBean","Constructor 
sorter is valid ");
               }
           }
           if (entryLoadRequest != null){
               this.entryLoadRequest = entryLoadRequest;
  +        } else if ( params.getEntryLoadRequest() != null ){
  +            this.entryLoadRequest = params.getEntryLoadRequest();
           }
       }
   
  +
       
//--------------------------------------------------------------------------
       /**
        * Do the sort. Optionally, you can provide a BitSet where each bit set 
correspond the a container id you want in the result.
  @@ -503,6 +504,8 @@
               locale = Locale.ENGLISH;
           }
   
  +        Collator collator = this.getCollator();
  +
           Vector datas = new Vector();
           HashMap maps = new HashMap();
           try
  @@ -588,15 +591,9 @@
                   }
                   Object obj = null;
                   if (convertValueAsLong) {
  -                    try {
  -                        obj = new DataBean(aField.ctnID,
  -                                           Long.parseLong(aField.value));
  -                    } catch (Throwable t) {
  -                        // we want to display the ctn anyway
  -                        obj = new DataBean(aField.ctnID, -1);
  -                    }
  +                    obj = new DataBean(aField.ctnID,aField.value);
                   } else {
  -                    obj = new StrDataBean(aField.ctnID, aField.value);
  +                    obj = new StrDataBean(aField.ctnID, aField.value, 
collator);
                   }
                   if (this.entryLoadRequest.isCurrent()) {
                       datas.add(obj);
  @@ -634,10 +631,12 @@
   
           Vector datas = 
this.getFieldValues(this.ctnListID,this.fieldName,this.isNumberOrdering(),bits);
   
  +        Collator collator = this.getCollator();
  +        
           // sort the datas
           if ( datas.size()>1 ){
               // a dummy dataBean
  -            StrDataBean dummyDataBean = new StrDataBean(ASC_Ordering);
  +            StrDataBean dummyDataBean = new 
StrDataBean(ASC_Ordering,collator);
               Collections.sort(datas,dummyDataBean);
           }
           // retrieve sorted ids
  @@ -699,15 +698,14 @@
           }
       }
   
  -
       
//--------------------------------------------------------------------------
       protected class DataBean implements Comparator
       {
           int ctnID = 0;
  -        long value = -1;
  +        String value = null;
           boolean ASC_Ordering = true;
   
  -        public DataBean (int ctnID, long value)
  +        public DataBean (int ctnID, String value)
           {
               this.ctnID = ctnID;
               this.value = value;
  @@ -722,14 +720,11 @@
   
               DataBean dataBean1 = (DataBean)obj1;
               DataBean dataBean2 = (DataBean)obj2;
  -            if ( dataBean1.value == dataBean2.value ){
  -                return 0;
  -            } else if ( ASC_Ordering && (dataBean1.value > dataBean2.value) 
){
  -                return 1;
  -            } else if ( !ASC_Ordering && (dataBean1.value < dataBean2.value) 
){
  -                return 1;
  +            if ( ASC_Ordering ){
  +                return 
NumberFormats.compareNumber(dataBean1.value,dataBean2.value, numberFormat);
  +            } else {
  +                return 
NumberFormats.compareNumber(dataBean2.value,dataBean1.value, numberFormat);
               }
  -            return -1;
           }
       }
   
  @@ -739,16 +734,26 @@
           int ctnID = 0;
           String value = "";
           boolean ASC_Ordering = true;
  +        Collator collator = null;
   
  -        public StrDataBean (int ctnID, String value)
  +        public StrDataBean (int ctnID, String value, Collator collator)
           {
               this.ctnID = ctnID;
               this.value = value;
  +            this.collator = collator;
  +
  +            if ( this.collator == null ){
  +                this.collator = Collator.getInstance();
  +            }
           }
   
  -        public StrDataBean (boolean ASC_Ordering)
  +        public StrDataBean (boolean ASC_Ordering, Collator collator)
           {
               this.ASC_Ordering = ASC_Ordering;
  +            this.collator = collator;
  +            if ( this.collator == null ){
  +                this.collator = Collator.getInstance();
  +            }
           }
   
           public int compare(Object obj1, Object obj2) throws 
ClassCastException {
  @@ -756,9 +761,9 @@
               StrDataBean dataBean1 = (StrDataBean)obj1;
               StrDataBean dataBean2 = (StrDataBean)obj2;
               if ( this.ASC_Ordering ){
  -                return 
dataBean1.value.compareToIgnoreCase(dataBean2.value.toLowerCase());
  +                return collator.compare(dataBean1.value,dataBean2.value);
               } else {
  -                return 
dataBean2.value.compareToIgnoreCase(dataBean1.value.toLowerCase());
  +                return collator.compare(dataBean2.value,dataBean1.value);
               }
           }
       }
  @@ -828,6 +833,23 @@
           return datas;
       }
   
  +    /**
  +     *  Return the collator instantiated with the first locale from the 
internal EntryLoadRequest.
  +     *  If the entryLoadRequest is null, the localtor is instantiated with 
the default locale of the system
  +     * @return
  +     */
  +    private Collator getCollator(){
  +        Collator collator = Collator.getInstance();
  +        if ( this.getEntryLoadRequest() != null ){
  +            Locale locale = null;
  +            locale = this.getEntryLoadRequest().getFirstLocale(true);
  +            if ( locale != null ){
  +                collator = Collator.getInstance(locale);
  +            }
  +        }
  +        return collator;
  +    }
  +
       public String toString() {
           StringBuffer buf = new StringBuffer();
           if (result != null) {
  
  
  
  Index: ContainerSorterByContainerDefinition.java
  ===================================================================
  RCS file: 
/home/cvs/repository/jahia/core/src/java/org/jahia/data/containers/ContainerSorterByContainerDefinition.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- ContainerSorterByContainerDefinition.java 17 Nov 2004 15:01:03 -0000      
1.7
  +++ ContainerSorterByContainerDefinition.java 28 Apr 2005 14:06:24 -0000      
1.8
  @@ -35,6 +35,7 @@
   import java.util.Locale;
   import java.util.TimeZone;
   import java.util.Vector;
  +import java.text.Collator;
   
   import org.jahia.bin.Jahia;
   import org.jahia.data.fields.ExpressionMarker;
  @@ -72,6 +73,8 @@
   
       protected boolean numberSort = false;
   
  +    private String numberFormat = NumberFormats.LONG_FORMAT;
  +
       protected boolean isValid = false;
   
       protected boolean ASC_Ordering = true; // by default ASCENDANT ORDER.
  @@ -89,12 +92,52 @@
        * @param fieldName
        * @param containerDefinitionName
        * @throws JahiaException
  +     * @deprecated, inverted numbersort and entryLoadRequest parameter
  +     *
        */
       public ContainerSorterByContainerDefinition(int siteId, String fieldName,
               String containerDefinitionName, EntryLoadRequest 
entryLoadRequest,
               boolean numberSort)
       throws JahiaException
       {
  +        
this(siteId,fieldName,containerDefinitionName,numberSort,entryLoadRequest);
  +    }
  +
  +    
//--------------------------------------------------------------------------
  +    /**
  +     *
  +     * @param siteId
  +     * @param fieldName
  +     * @param containerDefinitionName
  +     * @param numberSort
  +     * @param entryLoadRequest
  +     * @throws JahiaException
  +     */
  +    public ContainerSorterByContainerDefinition(int siteId, String fieldName,
  +            String containerDefinitionName, boolean numberSort,
  +            EntryLoadRequest entryLoadRequest)
  +    throws JahiaException
  +    {
  +        
this(siteId,fieldName,containerDefinitionName,numberSort,null,entryLoadRequest);
  +    }
  +
  +    /**
  +     *
  +     * @param siteId
  +     * @param fieldName
  +     * @param containerDefinitionName
  +     * @param boolean , force field values to be converted to number 
representation before sorting ( if true ).
  +     * @param numberFormat, only used if numberSort is true. If null, the 
format used is NumberFormat.LONG_FORMAT
  +     * @param entryLoadRequest
  +     * @throws JahiaException
  +     */
  +    public ContainerSorterByContainerDefinition(int siteId, String fieldName,
  +            String containerDefinitionName,
  +            boolean numberSort,
  +            String numberFormat,
  +            EntryLoadRequest entryLoadRequest )
  +    throws JahiaException
  +    {
           if ( fieldName != null && !fieldName.trim().equals("") )
           {
               this.siteId = siteId;
  @@ -102,6 +145,9 @@
               this.containerDefinitionName = containerDefinitionName;
               this.isValid = true;
               this.numberSort = numberSort;
  +            if ( NumberFormats.isValidFormat(numberFormat) ){
  +                this.numberFormat = numberFormat;
  +            }
           }
           if (entryLoadRequest != null){
               this.entryLoadRequest = entryLoadRequest;
  @@ -355,6 +401,7 @@
           if ( locale == null ){
               locale = Locale.ENGLISH;
           }
  +        Collator collator = this.getCollator();
   
           Vector datas = new Vector();
           HashMap maps = new HashMap();
  @@ -441,15 +488,9 @@
                   }
                   Object obj = null;
                   if (this.numberSort) {
  -                    try {
  -                        obj = new DataBean(aField.ctnID,
  -                                           Long.parseLong(aField.value));
  -                    } catch (Throwable t) {
  -                        // we want to display the ctn anyway
  -                        obj = new DataBean(aField.ctnID, -1);
  -                    }
  +                    obj = new DataBean(aField.ctnID,aField.value);
                   } else {
  -                    obj = new StrDataBean(aField.ctnID, aField.value);
  +                    obj = new StrDataBean(aField.ctnID, aField.value, 
collator);
                   }
                   if (this.entryLoadRequest.isCurrent()) {
                       datas.add(obj);
  @@ -487,10 +528,12 @@
   
           Vector datas = this.getFieldValues(bits);
   
  +        Collator collator = this.getCollator();
  +
           // sort the datas
           if ( datas.size()>1 ){
               // a dummy dataBean
  -            StrDataBean dummyDataBean = new StrDataBean(ASC_Ordering);
  +            StrDataBean dummyDataBean = new StrDataBean(ASC_Ordering, 
collator);
               Collections.sort(datas,dummyDataBean);
           }
           // retrieve sorted ids
  @@ -553,14 +596,13 @@
       }
   
   
  -    
//--------------------------------------------------------------------------
       protected class DataBean implements Comparator
       {
           int ctnID = 0;
  -        long value = -1;
  +        String value = null;
           boolean ASC_Ordering = true;
   
  -        public DataBean (int ctnID, long value)
  +        public DataBean (int ctnID, String value)
           {
               this.ctnID = ctnID;
               this.value = value;
  @@ -575,14 +617,11 @@
   
               DataBean dataBean1 = (DataBean)obj1;
               DataBean dataBean2 = (DataBean)obj2;
  -            if ( dataBean1.value == dataBean2.value ){
  -                return 0;
  -            } else if ( ASC_Ordering && (dataBean1.value > dataBean2.value) 
){
  -                return 1;
  -            } else if ( !ASC_Ordering && (dataBean1.value < dataBean2.value) 
){
  -                return 1;
  +            if ( ASC_Ordering ){
  +                return 
NumberFormats.compareNumber(dataBean1.value,dataBean2.value, numberFormat);
  +            } else {
  +                return 
NumberFormats.compareNumber(dataBean2.value,dataBean1.value, numberFormat);
               }
  -            return -1;
           }
       }
   
  @@ -592,16 +631,26 @@
           int ctnID = 0;
           String value = "";
           boolean ASC_Ordering = true;
  +        Collator collator = null;
   
  -        public StrDataBean (int ctnID, String value)
  +        public StrDataBean (int ctnID, String value, Collator collator)
           {
               this.ctnID = ctnID;
               this.value = value;
  +            this.collator = collator;
  +
  +            if ( this.collator == null ){
  +                this.collator = Collator.getInstance();
  +            }
           }
   
  -        public StrDataBean (boolean ASC_Ordering)
  +        public StrDataBean (boolean ASC_Ordering, Collator collator)
           {
               this.ASC_Ordering = ASC_Ordering;
  +            this.collator = collator;
  +            if ( this.collator == null ){
  +                this.collator = Collator.getInstance();
  +            }
           }
   
           public int compare(Object obj1, Object obj2) throws 
ClassCastException {
  @@ -627,9 +676,9 @@
               }
   
               if ( this.ASC_Ordering ){
  -                return 
valueBean1.compareToIgnoreCase(valueBean2.toLowerCase());
  +                return collator.compare(valueBean1,valueBean2);
               } else {
  -                return 
valueBean2.compareToIgnoreCase(valueBean1.toLowerCase());
  +                return collator.compare(valueBean2,valueBean1);
               }
           }
       }
  @@ -709,4 +758,21 @@
           return datas;
       }
   
  +    /**
  +     *  Return the collator instantiated with the first locale from the 
internal EntryLoadRequest.
  +     *  If the entryLoadRequest is null, the localtor is instantiated with 
the default locale of the system
  +     * @return
  +     */
  +    private Collator getCollator(){
  +        Collator collator = Collator.getInstance();
  +        if ( this.getEntryLoadRequest() != null ){
  +            Locale locale = null;
  +            locale = this.getEntryLoadRequest().getFirstLocale(true);
  +            if ( locale != null ){
  +                collator = Collator.getInstance(locale);
  +            }
  +        }
  +        return collator;
  +    }
  +
   }
  
  
  
  Index: FilterClause.java
  ===================================================================
  RCS file: 
/home/cvs/repository/jahia/core/src/java/org/jahia/data/containers/FilterClause.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- FilterClause.java 3 Mar 2005 12:16:06 -0000       1.3
  +++ FilterClause.java 28 Apr 2005 14:06:24 -0000      1.4
  @@ -174,10 +174,11 @@
         * Return true if the given Long value match this clause
         * Here, the clause's values are converted to long and a number 
comparison is performed
         *
  -      * @param long value
  +      * @param value
  +     * @param numberFormat , @see NumberFormats
         * @return boolean , the comparison result.
         */
  -     public boolean compare(long value){
  +     public boolean compareNumber(String value, String numberFormat){
                if ( !isValid ){
                        return false;
                }
  @@ -186,20 +187,21 @@
   
                try {
                        String comp = getComp();
  +            String v = null;
               if ( isRangeClause() ){
  -                Long L = Long.valueOf(getValue());
  -                result = compare(value,L.longValue(),comp);
  +                v = getValue();
  +                result = compare(value,v,comp,numberFormat);
                   if ( result )
                   {
  -                    L = Long.valueOf(getUpperValue());
  -                    result = compare(value,L.longValue(),getUpperComp());
  +                    v = getUpperValue();
  +                    result = compare(value,v,getUpperComp(),numberFormat);
                   }
               } else {
                   String[] vals = this.getValues();
                   int size = vals.length;
                   for ( int i=0; i<size; i++ ){
  -                    Long L = Long.valueOf((String)vals[i]);
  -                    result = compare(value,L.longValue(),comp);
  +                    v = (String)vals[i];
  +                    result = compare(value,v,comp,numberFormat);
                       if ( result ){
                           return true;
                       }
  @@ -214,7 +216,7 @@
       
//--------------------------------------------------------------------------
       /**
        *
  -     * @param long value
  +     * @param value
        * @return boolean , the comparison result.
        */
       public boolean compare(String value){
  @@ -258,22 +260,33 @@
         * @param comp the comparator
         * @return boolean the comparison result.
         */
  -     private boolean compare(long valueA, long valueB, String comp){
  +     private boolean compare(String valueA, String valueB, String comp, 
String format){
                boolean result = false;
  +             int compResult = 0;
                if ( comp.equals(ContainerFilterBean.COMP_EQUAL) ){
  -                     result = ( valueA == valueB );
  +                     result = ( NumberFormats.compareNumber(valueA,valueB, 
format) == 0 );
                } else if ( comp.equals(ContainerFilterBean.COMP_SMALLER) ){
  -                     result = ( valueA < valueB );
  +                     result = ( NumberFormats.compareNumber(valueA,valueB, 
format) == -1 );
                } else if ( 
comp.equals(ContainerFilterBean.COMP_SMALLER_OR_EQUAL) ){
  -                     result = ( valueA <= valueB );
  +            compResult = NumberFormats.compareNumber(valueA,valueB, format);
  +                     result = ( compResult == 0 || compResult == -1 );
                } else if ( 
comp.equals(ContainerFilterBean.COMP_BIGGER_OR_EQUAL) ){
  -                     result = ( valueA >= valueB );
  +            compResult = NumberFormats.compareNumber(valueA,valueB, format);
  +            result = ( compResult == 0 || compResult == 1 );
                } else if ( comp.equals(ContainerFilterBean.COMP_BIGGER) ){
  -                     result = ( valueA > valueB );
  +            compResult = NumberFormats.compareNumber(valueA,valueB, format);
  +            result = ( compResult == 1 );
                   } else if ( comp.equals(ContainerFilterBean.COMP_NOT_EQUAL) 
){
  -                     result = valueA != valueB ;
  +            if ( valueA == null && valueB == null ){
  +                return true;
  +            } else {
  +                try {
  +                             result = valueA.equals(valueB) ;
  +                } catch ( Throwable t ){
  +                }
  +            }
                }
  -             //JahiaConsole.println(CLASS_NAME+".compare","valueA["+ valueA 
+"] " + comp + " valueB[" + valueB + "] result=" + result);
  +        //JahiaConsole.println(CLASS_NAME+".compareNumber","valueA["+ valueA 
+"] " + comp + " valueB[" + valueB + "] result=" + result);
   
                return result;
        }
  

Reply via email to