Author: ekoneil
Date: Thu Jan  6 14:26:10 2005
New Revision: 124459

URL: http://svn.apache.org/viewcvs?view=rev&rev=124459
Log:
Work on sort support in the grid.

- add a SortModel for storing sort state.  The grid tags talk exclusively to 
this class for sort information.
- fixup the API of the SortService.  This is now aware only of the URL and is 
wrapped by the SortModel.
- switch the ISort constant ints to an enum.  We'll see how this works out.
- fix the PagerService to work around an Eclipse compiler bug with autoboxing

BB: self
DRT: Beehive pass


Added:
   
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/model/sort/SortModel.java
   (contents, props changed)
Modified:
   
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/model/sort/DefaultSort.java
   
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/model/sort/ISort.java
   
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/rendering/cell/SortedCellDecorator.java
   
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/rendering/pager/FirstPreviousNextLastPagerRenderer.java
   
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/rendering/table/TableRenderer.java
   
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/services/DataGridStateService.java
   
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/services/pager/PagerService.java
   
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/services/sort/SortFactory.java
   
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/services/sort/SortService.java
   
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/tags/databinding/datagrid/DataGrid.java
   
incubator/beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/datagrid/DefaultSortTest.java
   
incubator/beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/datagrid/SortServiceTest.java

Modified: 
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/model/sort/DefaultSort.java
Url: 
http://svn.apache.org/viewcvs/incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/model/sort/DefaultSort.java?view=diff&rev=124459&p1=incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/model/sort/DefaultSort.java&r1=124458&p2=incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/model/sort/DefaultSort.java&r2=124459
==============================================================================
--- 
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/model/sort/DefaultSort.java
        (original)
+++ 
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/model/sort/DefaultSort.java
        Thu Jan  6 14:26:10 2005
@@ -17,47 +17,46 @@
  */
 package org.apache.beehive.netui.databinding.datagrid.model.sort;
 
-/*
- * Default sort syntax: netui_sort=<gridNamespace>~[+|-]<expr>
- */
+/* Default sort syntax: netui_sort=<gridNamespace>~[+|-]<expr> */
 /**
  *
  */
 public class DefaultSort
     implements ISort {
 
-    private String _expr;
-    private int _dir;
+    private String _sortExpression;
+    private Direction _sortDirection;
 
     public DefaultSort() {
         super();
     }
 
-    public DefaultSort(String expression, int direction) {
+    public DefaultSort(String sortExpression, Direction sortDirection) {
         this();
-        _expr = expression;
-        _dir = direction;
+        _sortExpression = sortExpression;
+        _sortDirection = sortDirection;
     }
 
     public String getSortExpression() {
-        return _expr;
+        return _sortExpression;
     }
 
     public void setSortExpression(String expression) {
         assert expression != null;
-        assert !expression.equals("");
-        
-        _expr = expression;
+
+        _sortExpression = expression;
     }
 
-    public int getDirection() {
-        return _dir;
+    public Direction getDirection() {
+        return _sortDirection;
     }
 
-    public void setDirection(int direction) {
-        assert direction == ISort.ASCENDING || direction == ISort.DESCENDING 
|| direction == ISort.NONE;
+    public void setDirection(Direction sortDirection) {
+        _sortDirection = sortDirection;
+    }
 
-        _dir = direction;
+    public void changeSortDirection() {
+        _sortDirection = _sortDirection.flipDirection();
     }
 
     public String write(String namespace) {
@@ -68,49 +67,34 @@
         return writeSortParam(namespace, flipDirection);
     }
 
-    public void changeSortDirection() {
-        _dir = flipSortDirection(_dir);
-    }
-
     public String toString() {
-        StringBuilder buf = new StringBuilder();
+        StringBuilder buf = new StringBuilder(32);
         buf.append("\n");
         buf.append("expr: ");
-        buf.append(_expr);
+        buf.append(_sortExpression);
         buf.append("\n");
         buf.append("dir: ");
-        buf.append(_dir);
+        buf.append(_sortDirection);
         buf.append("\n");
         return buf.toString();
     }
 
-    private static final int flipSortDirection(int direction) {
-        switch (direction) {
-            case ISort.ASCENDING:
-                return ISort.DESCENDING;
-            case ISort.DESCENDING:
-                return ISort.ASCENDING;
-            default:
-                return ISort.ASCENDING;
-        }
-    }
-
     private String writeSortParam(String namespace, boolean flipDirection) {
 
-        int direction = _dir;
+        Direction direction = _sortDirection;
         if (flipDirection)
             /* todo: break this dependency.  switch ISort to AbstractSort with 
utility methods */
-            direction = flipSortDirection(direction);
+            direction = direction.flipDirection();
 
-        return writeSortParam(namespace, _expr, direction);
+        return writeSortParam(namespace, _sortExpression, direction);
     }
 
-    public static final String writeSortParam(String namespace, String 
sortExpression, int direction) {
+    public static final String writeSortParam(String namespace, String 
sortExpression, Direction sortDirection) {
         StringBuilder builder = new StringBuilder();
 
         builder.append(namespace);
         builder.append("~");
-        if (direction == ISort.DESCENDING)
+        if(sortDirection == Direction.DESCENDING)
             builder.append("-");
         builder.append(sortExpression);
         return builder.toString();

Modified: 
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/model/sort/ISort.java
Url: 
http://svn.apache.org/viewcvs/incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/model/sort/ISort.java?view=diff&rev=124459&p1=incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/model/sort/ISort.java&r1=124458&p2=incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/model/sort/ISort.java&r2=124459
==============================================================================
--- 
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/model/sort/ISort.java
      (original)
+++ 
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/model/sort/ISort.java
      Thu Jan  6 14:26:10 2005
@@ -24,17 +24,25 @@
 {
     String SORT_PARAM_KEY = "netui_sort";
 
-    int NONE = 0;
-    int ASCENDING = 1;
-    int DESCENDING = 2;
+    enum Direction{
+        NONE, ASCENDING, DESCENDING;
+
+        Direction flipDirection() {
+            if(this == Direction.ASCENDING)
+                return Direction.DESCENDING;
+            else if(this == Direction.DESCENDING)
+                return Direction.ASCENDING;
+            else return Direction.ASCENDING;
+        }
+    };
 
     String getSortExpression();
 
     void setSortExpression(String sortExpression);
 
-    int getDirection();
+    Direction getDirection();
     
-    void setDirection(int direction);
+    void setDirection(Direction sortDirection);
 
     String write(String namespace);
 

Added: 
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/model/sort/SortModel.java
Url: 
http://svn.apache.org/viewcvs/incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/model/sort/SortModel.java?view=auto&rev=124459
==============================================================================
--- (empty file)
+++ 
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/model/sort/SortModel.java
  Thu Jan  6 14:26:10 2005
@@ -0,0 +1,114 @@
+/*
+ * Copyright 2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * $Header:$
+ */
+package org.apache.beehive.netui.databinding.datagrid.model.sort;
+
+import java.util.List;
+import javax.servlet.ServletRequest;
+
+import 
org.apache.beehive.netui.databinding.datagrid.model.sort.ISort.Direction;
+import org.apache.beehive.netui.databinding.datagrid.services.sort.SortService;
+
+/**
+ *
+ */
+public class SortModel {
+
+    private String _name = null;
+    private String _sortParamKey = null;
+    private List<ISort> _sorts = null;
+
+    public SortModel(String name) {
+        _name = name;
+    }
+
+    public void handleRequest(ServletRequest request) {
+        SortService sortService = SortService.getInstance(request, _name);
+
+        _sorts = sortService.getSorts();
+    }
+
+    public List<ISort> getSorts() {
+        return _sorts;
+    }
+
+    public String getSortParamKey() {
+        if(_sortParamKey != null)
+            return _sortParamKey;
+        else return ISort.SORT_PARAM_KEY;
+    }
+
+    public boolean isPrimarySort(String sortExpression) {
+        /* optimize for the case where the sortExpression *is* the primary 
sort */
+        if (_sorts != null &&
+            _sorts.size() > 0 &&
+            _sorts.get(0).getSortExpression().equals(sortExpression)) {
+            return true;
+        }
+        else return false;
+    }
+
+    public boolean isSorted(String sortExpression) {
+        ISort term = findTerm(sortExpression);
+        if (term == null || term.getDirection() == Direction.NONE)
+            return false;
+        else
+            return true;
+    }
+
+    public Direction getSortDirection(String sortExpression) {
+        ISort term = findTerm(sortExpression);
+        return term == null ? Direction.NONE : term.getDirection();
+    }
+
+    public boolean removeSort(String sortExpression) {
+        ISort term = findTerm(sortExpression);
+        if(term != null) {
+            _sorts.remove(term);
+            return true;
+        }
+        else return false;
+    }
+
+    public void addSort(String sortExpression, Direction sortDirection) {
+        DefaultSort sort = new DefaultSort(sortExpression, sortDirection);
+        _sorts.add(sort);
+    }
+
+    public String toString() {
+        StringBuilder sb = new StringBuilder();
+        sb.append("SortModel:\n");
+        for(ISort sort : _sorts) {
+            sb.append("\t");
+            sb.append(sort.toString());
+            sb.append("\n");
+        }
+        return sb.toString();
+    }
+
+    private ISort findTerm(String sortExpression) {
+        if (_sorts == null)
+            return null;
+
+        for (ISort sort : _sorts) {
+            if (sort.getSortExpression().equals(sortExpression))
+                return sort;
+        }
+
+        return null;
+    }
+}

Modified: 
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/rendering/cell/SortedCellDecorator.java
Url: 
http://svn.apache.org/viewcvs/incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/rendering/cell/SortedCellDecorator.java?view=diff&rev=124459&p1=incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/rendering/cell/SortedCellDecorator.java&r1=124458&p2=incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/rendering/cell/SortedCellDecorator.java&r2=124459
==============================================================================
--- 
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/rendering/cell/SortedCellDecorator.java
    (original)
+++ 
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/rendering/cell/SortedCellDecorator.java
    Thu Jan  6 14:26:10 2005
@@ -32,9 +32,10 @@
 import org.apache.beehive.netui.databinding.datagrid.model.CellModel;
 import 
org.apache.beehive.netui.databinding.datagrid.model.cell.HeaderCellModel;
 import org.apache.beehive.netui.databinding.datagrid.model.DataGridModel;
+import 
org.apache.beehive.netui.databinding.datagrid.model.sort.ISort.Direction;
+import org.apache.beehive.netui.databinding.datagrid.model.sort.SortModel;
 import org.apache.beehive.netui.databinding.datagrid.util.JspUtil;
 import 
org.apache.beehive.netui.databinding.datagrid.services.DataGridStateService;
-import org.apache.beehive.netui.databinding.datagrid.services.sort.SortService;
 import org.apache.beehive.netui.util.logging.Logger;
 
 /**
@@ -75,7 +76,7 @@
         assert dgm != null;
 
         DataGridStateService dgss = dgm.getDataGridStateService();
-        SortService ss = dgss.getSortService();
+        SortModel sortModel = dgss.getSortModel();
 
         StringBuilder builder = new StringBuilder();
         AbstractRenderAppender appender = new 
StringBuilderRenderAppender(builder);
@@ -86,10 +87,10 @@
         AnchorTag.State aTag = new AnchorTag.State();
         ImageTag.State imgTag = new ImageTag.State();
 
-        int sortDirection = ss.getSortDirection(cellModel.getSortExpression());
+        Direction sortDirection = 
sortModel.getSortDirection(cellModel.getSortExpression());
 
         /* build icon for existing sort */
-        if(ss.isSorted(cellModel.getSortExpression()))
+        if(sortModel.isSorted(cellModel.getSortExpression()))
             imgTag.src = dgss.getSortImagePath(request.getContextPath(), 
sortDirection);
         else imgTag.src = 
dgss.getDefaultSortImagePath(request.getContextPath());
 

Modified: 
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/rendering/pager/FirstPreviousNextLastPagerRenderer.java
Url: 
http://svn.apache.org/viewcvs/incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/rendering/pager/FirstPreviousNextLastPagerRenderer.java?view=diff&rev=124459&p1=incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/rendering/pager/FirstPreviousNextLastPagerRenderer.java&r1=124458&p2=incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/rendering/pager/FirstPreviousNextLastPagerRenderer.java&r2=124459
==============================================================================
--- 
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/rendering/pager/FirstPreviousNextLastPagerRenderer.java
    (original)
+++ 
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/rendering/pager/FirstPreviousNextLastPagerRenderer.java
    Thu Jan  6 14:26:10 2005
@@ -20,6 +20,9 @@
 import org.apache.beehive.netui.databinding.datagrid.model.PagerModel;
 import org.apache.beehive.netui.databinding.datagrid.model.DataGridModel;
 
+/**
+ * 
+ */
 public class FirstPreviousNextLastPagerRenderer
     extends AbstractPagerRenderer
 {

Modified: 
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/rendering/table/TableRenderer.java
Url: 
http://svn.apache.org/viewcvs/incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/rendering/table/TableRenderer.java?view=diff&rev=124459&p1=incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/rendering/table/TableRenderer.java&r1=124458&p2=incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/rendering/table/TableRenderer.java&r2=124459
==============================================================================
--- 
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/rendering/table/TableRenderer.java
 (original)
+++ 
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/rendering/table/TableRenderer.java
 Thu Jan  6 14:26:10 2005
@@ -29,6 +29,9 @@
 import org.apache.beehive.netui.tags.rendering.AbstractRenderAppender;
 import org.apache.beehive.netui.databinding.datagrid.model.style.StylePolicy;
 
+/**
+ * 
+ */
 public class TableRenderer
 {
     private static final CaptionTag.State CAPTION_STATE = new 
CaptionTag.State();

Modified: 
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/services/DataGridStateService.java
Url: 
http://svn.apache.org/viewcvs/incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/services/DataGridStateService.java?view=diff&rev=124459&p1=incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/services/DataGridStateService.java&r1=124458&p2=incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/services/DataGridStateService.java&r2=124459
==============================================================================
--- 
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/services/DataGridStateService.java
 (original)
+++ 
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/services/DataGridStateService.java
 Thu Jan  6 14:26:10 2005
@@ -21,13 +21,13 @@
 import java.util.HashMap;
 import java.util.Collections;
 import java.util.ArrayList;
+import java.util.List;
 import javax.servlet.ServletRequest;
 
 import org.apache.beehive.netui.databinding.datagrid.model.sort.ISort;
 import org.apache.beehive.netui.databinding.datagrid.model.sort.DefaultSort;
-import org.apache.beehive.netui.databinding.datagrid.model.PagerModel;
-import org.apache.beehive.netui.databinding.datagrid.services.sort.SortService;
-import 
org.apache.beehive.netui.databinding.datagrid.services.pager.PagerService;
+import org.apache.beehive.netui.databinding.datagrid.model.sort.SortModel;
+import 
org.apache.beehive.netui.databinding.datagrid.model.sort.ISort.Direction;
 
 /**
  * todo: need to support registering URL prefixes that have state service 
handlers
@@ -37,7 +37,7 @@
     implements java.io.Serializable {
 
     private String _namespace = null;
-    private SortService _sortService = null;
+    private SortModel _sortModel = null;
 
     public static final DataGridStateService createInstance(ServletRequest 
request, String namespace) {
         assert request != null;
@@ -49,7 +49,6 @@
         return dgss;
     }
 
-    /* todo: need a factory for creating these state services */
     /**
      * Default constructor that builds a DataGridStateService with the default
      * configuration including support for a [EMAIL PROTECTED] 
org.apache.beehive.netui.databinding.datagrid.services.sort.SortService} and a
@@ -70,74 +69,75 @@
      * @param request The current servlet request.
      */
     public void handleRequest(ServletRequest request) {
-        _sortService = ensureSortService(request, _namespace);
-    }
-
-    public SortService getSortService() {
-        return _sortService;
+        if(_sortModel == null)
+            _sortModel = new SortModel(_namespace);
+        _sortModel.handleRequest(request);
     }
 
     public String getNamespace() {
         return _namespace;
     }
 
+    /* todo: could add lookups here based on base-class as a generic way to 
get to a service implementation */
+    public SortModel getSortModel() {
+        return _sortModel;
+    }
+
     public Map<String, String[]> buildSortQueryParamsMap() {
-        ISort[] sorts = _sortService.getSorts();
+        List<ISort> sorts = _sortModel.getSorts();
 
         if(sorts == null)
             return Collections.EMPTY_MAP;
 
         ArrayList valueList = new ArrayList(5);
-        for(int i = 0; i < sorts.length; i++)
-            valueList.add(sorts[i].write(_namespace));
+        for(ISort sort : sorts)
+            valueList.add(sort.write(_namespace));
 
         String[] values = (String[])valueList.toArray(new String[0]);
         HashMap<String, String[]> map = new HashMap<String, 
String[]>(values.length);
-        map.put(_sortService.getKey(), values);
+        map.put(_sortModel.getSortParamKey(), values);
 
         return map;
     }
 
-    public Map<String, String[]> buildSortQueryParamsMap(String 
sortExpression, int direction) {
+    public Map<String, String[]> buildSortQueryParamsMap(String 
sortExpression, Direction sortDirection) {
+
         /* todo: consolidate the query param map construction methods */
         assert sortExpression != null;
-        assert direction == ISort.ASCENDING || direction == ISort.DESCENDING 
|| direction == ISort.NONE;
 
-        ISort[] sorts = _sortService.getSorts();
+        List<ISort> sorts = _sortModel.getSorts();
 
         ArrayList valueList = new ArrayList(5);
-        if(direction != ISort.NONE && !_sortService.isSorted(sortExpression))
-            valueList.add(DefaultSort.writeSortParam(_namespace, 
sortExpression, direction));
+        if(sortDirection != Direction.NONE && 
!_sortModel.isSorted(sortExpression))
+            valueList.add(DefaultSort.writeSortParam(_namespace, 
sortExpression, sortDirection));
 
         if(sorts != null) {
             /* todo: deal with additional query params here */
-            for(int i = 0; i < sorts.length; i++) {
-                if(sorts[i].getSortExpression().equals(sortExpression)) {
-                    if(direction != ISort.NONE)
+            for(ISort sort : sorts) {
+                if(sort.getSortExpression().equals(sortExpression)) {
+                    if(sortDirection != Direction.NONE)
                         /* todo: broke encapsulation here.  bad bad bad. */
                         /* @todo: need to move this parameter handling into 
some sort of URLParams class that can be *fast* */
-                       valueList.add(DefaultSort.writeSortParam(_namespace, 
sorts[i].getSortExpression(), direction));
+                       valueList.add(DefaultSort.writeSortParam(_namespace, 
sort.getSortExpression(), sortDirection));
                 }
-                else valueList.add(sorts[i].write(_namespace));
+                else valueList.add(sort.write(_namespace));
             }
         }
 
         HashMap<String, String[]> map = new HashMap<String, 
String[]>(valueList.size());
-        map.put(_sortService.getKey(), (String[])valueList.toArray(new 
String[0]));
+        map.put(_sortModel.getSortParamKey(), (String[])valueList.toArray(new 
String[0]));
         return map;
     }
 
-    public String getSortImagePath(String contextPath, int direction) {
-        switch (direction)
-        {
-            case ISort.ASCENDING:
-                return contextPath + "/resources/images/sortdown.gif";
-            case ISort.DESCENDING:
-                return contextPath + "/resources/images/sortup.gif";
-            default:
-                  assert direction == ISort.NONE : "Encountered an invalid 
sort direction.";
-                    /* todo: need to flag on whether this is sort up or down 
and set the image accordingly */
-                    return getDefaultSortImagePath(contextPath);
+    public String getSortImagePath(String contextPath, Direction direction) {
+        if(direction == Direction.ASCENDING)
+            return contextPath + "/resources/images/sortdown.gif";
+        else if(direction == Direction.DESCENDING)
+            return contextPath + "/resources/images/sortup.gif";
+        else {
+            assert direction == Direction.NONE : "Encountered an invalid sort 
direction.";
+            /* todo: need to flag on whether this is sort up or down and set 
the image accordingly */
+            return getDefaultSortImagePath(contextPath);
         }
     }
 
@@ -147,21 +147,14 @@
     }
 
     public Map<String, String[]> getSortQueryParamsMap(String sortExpression) {
-        int sortDirection = _sortService.getSortDirection(sortExpression);
+        Direction sortDirection = _sortModel.getSortDirection(sortExpression);
 
-        if(sortDirection == ISort.NONE) {
-            return buildSortQueryParamsMap(sortExpression, ISort.ASCENDING);
-        }
-        else if(sortDirection == ISort.ASCENDING) {
-            return buildSortQueryParamsMap(sortExpression, ISort.DESCENDING);
-        }
-        else if(sortDirection == ISort.DESCENDING) {
-            return buildSortQueryParamsMap(sortExpression, ISort.NONE);
-        }
+        if(sortDirection == Direction.NONE)
+            return buildSortQueryParamsMap(sortExpression, 
Direction.ASCENDING);
+        else if(sortDirection == Direction.ASCENDING)
+            return buildSortQueryParamsMap(sortExpression, 
Direction.DESCENDING);
+        else if(sortDirection == Direction.DESCENDING)
+            return buildSortQueryParamsMap(sortExpression, Direction.NONE);
         else throw new IllegalStateException("Received invalid sort direction 
\"" + sortDirection + "\"");
-    }
-
-    private static final SortService ensureSortService(ServletRequest request, 
String namespace) {
-        return SortService.getInstance(request, namespace);
     }
 }

Modified: 
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/services/pager/PagerService.java
Url: 
http://svn.apache.org/viewcvs/incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/services/pager/PagerService.java?view=diff&rev=124459&p1=incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/services/pager/PagerService.java&r1=124458&p2=incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/services/pager/PagerService.java&r2=124459
==============================================================================
--- 
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/services/pager/PagerService.java
   (original)
+++ 
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/services/pager/PagerService.java
   Thu Jan  6 14:26:10 2005
@@ -66,7 +66,7 @@
 
     public Integer getCurrentPage() {
         if(getCurrentRow() != null && getPageSize() != null)
-            _currentPage = (int)Math.ceil((double)getCurrentRow() / 
(double)getPageSize());
+            _currentPage = (int)Math.ceil(getCurrentRow().doubleValue() / 
getPageSize().doubleValue());
         
         return _currentPage;
     }

Modified: 
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/services/sort/SortFactory.java
Url: 
http://svn.apache.org/viewcvs/incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/services/sort/SortFactory.java?view=diff&rev=124459&p1=incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/services/sort/SortFactory.java&r1=124458&p2=incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/services/sort/SortFactory.java&r2=124459
==============================================================================
--- 
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/services/sort/SortFactory.java
     (original)
+++ 
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/services/sort/SortFactory.java
     Thu Jan  6 14:26:10 2005
@@ -18,6 +18,7 @@
 package org.apache.beehive.netui.databinding.datagrid.services.sort;
 
 import org.apache.beehive.netui.databinding.datagrid.model.sort.ISort;
+import 
org.apache.beehive.netui.databinding.datagrid.model.sort.ISort.Direction;
 import org.apache.beehive.netui.databinding.datagrid.model.sort.DefaultSort;
 
 /**
@@ -25,14 +26,14 @@
  */
 public class SortFactory {
 
-    public static final ISort getInstance(String expression, int direction) {
-        return new DefaultSort(expression, direction);
+    /* do not construct */
+    private SortFactory() {}
+
+    public static final ISort getInstance(String sortExpression, Direction 
sortDirection) {
+        return new DefaultSort(sortExpression, sortDirection);
     }
 
     public static final ISort getInstance() {
         return new DefaultSort();
     }
-
-    /* do not construct */
-    private SortFactory() {}
 }

Modified: 
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/services/sort/SortService.java
Url: 
http://svn.apache.org/viewcvs/incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/services/sort/SortService.java?view=diff&rev=124459&p1=incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/services/sort/SortService.java&r1=124458&p2=incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/services/sort/SortService.java&r2=124459
==============================================================================
--- 
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/services/sort/SortService.java
     (original)
+++ 
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/databinding/datagrid/services/sort/SortService.java
     Thu Jan  6 14:26:10 2005
@@ -22,8 +22,8 @@
 import javax.servlet.ServletRequest;
 
 import org.apache.beehive.netui.databinding.datagrid.model.sort.ISort;
+import 
org.apache.beehive.netui.databinding.datagrid.model.sort.ISort.Direction;
 import org.apache.beehive.netui.databinding.datagrid.model.sort.DefaultSort;
-import org.apache.beehive.netui.util.logging.Logger;
 
 /**
  */
@@ -37,87 +37,26 @@
         assert request != null;
         assert namespace != null && !namespace.equals("");
         
-        SortService svc = new SortService(namespace);
-        svc.handleRequest(request);
-        return svc;
+        return new SortService(request, namespace);
     }
 
     private String _namespace = null;
-    private ISort[] _sorts = null;
+    private ArrayList<ISort> _sorts = null;
+    private ServletRequest _request = null;
 
-    private SortService(String namespace) {
+    private SortService(ServletRequest request, String namespace) {
         super();
+        _request = request;
         _namespace = namespace;
-    }
-
-    public ISort[] getSorts() {
-        return _sorts;
-    }
-
-    public String getKey() {
-        return ISort.SORT_PARAM_KEY;
-    }
-
-    public void handleRequest(ServletRequest request) {
         _sorts = parseTerms(request, _namespace);
     }
 
-    public boolean isPrimarySort(String sortExpression) {
-        /* optimize for the case where the sortExpression *is* the primary 
sort */
-        if (_sorts != null &&
-                _sorts.length > 0 &&
-                _sorts[0].getSortExpression().equals(sortExpression))
-            return true;
-        else
-            return false;
-    }
-
-    public boolean isSorted(String sortExpression) {
-        ISort term = findTerm(sortExpression);
-        if (term == null || term.getDirection() == ISort.NONE)
-            return false;
-        else
-            return true;
-    }
-
-    public int getSortDirection(String sortExpression) {
-        ISort term = findTerm(sortExpression);
-        return term == null ? ISort.NONE : term.getDirection();
-    }
-
-    public boolean removeSort(String sortExpression) {
-        throw new UnsupportedOperationException("NYI");
-    }
-
-    public boolean addSort(String sortExpression, int direction) {
-        assert direction == ISort.NONE || direction == ISort.ASCENDING || 
direction == ISort.DESCENDING;
-        //ISort sort = new DefaultSort(sortExpression, direction);
-        throw new UnsupportedOperationException("NYI");
-    }
-
-    public boolean replaceSort(String sortExpression, int direction) {
-        assert direction == ISort.NONE || direction == ISort.ASCENDING || 
direction == ISort.DESCENDING;
-        throw new UnsupportedOperationException("NYI");
-    }
-
-    public String toString() {
-        throw new UnsupportedOperationException("NYI");
-    }
-
-    private ISort findTerm(String sortExpression) {
-        if (_sorts == null) return null;
-
-        for (int i = 0; i < _sorts.length; i++) {
-            ISort s = _sorts[i];
-            if (s.getSortExpression().equals(sortExpression))
-                return s;
-        }
-
-        return null;
+    public List<ISort> getSorts() {
+        return _sorts;
     }
 
-    /* todo: handle this incrementally */
-    private final ISort[] parseTerms(ServletRequest request, String namespace) 
{
+    /* todo: handle this incrementally in a base class */
+    private ArrayList<ISort> parseTerms(ServletRequest request, String 
namespace) {
         String[] params = request.getParameterValues(ISort.SORT_PARAM_KEY);
 
         if (params == null)
@@ -125,7 +64,7 @@
 
         String namespacePrefix = namespace + SORT_DELIM;
 
-        List sorts = new ArrayList();
+        ArrayList<ISort> sorts = new ArrayList<ISort>();
         for (int i = 0; i < params.length; i++) {
             String param = params[i];
             if (param.startsWith(namespacePrefix)) {
@@ -136,14 +75,17 @@
 
                 for (int j = 0; j < cols.length; j++) {
                     String t = cols[j];
-                    int dir = (cols[j].startsWith("-") == true ? 
ISort.DESCENDING : ISort.ASCENDING);
-                    String expr = (dir == ISort.DESCENDING ? 
cols[j].substring(1) : cols[j]);
-                    ISort sort = new DefaultSort(expr, dir);
+                    Direction sortDirection = Direction.NONE;
+                    if(cols[j].startsWith("-"))
+                        sortDirection = Direction.DESCENDING;
+                    else sortDirection = Direction.ASCENDING;
+                    String sortExpression = (sortDirection == 
Direction.DESCENDING ? cols[j].substring(1) : cols[j]);
+                    ISort sort = new DefaultSort(sortExpression, 
sortDirection);
                     sorts.add(sort);
                 }
             }
         }
 
-        return (ISort[])sorts.toArray(new ISort[0]);
+        return sorts;
     }
 }

Modified: 
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/tags/databinding/datagrid/DataGrid.java
Url: 
http://svn.apache.org/viewcvs/incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/tags/databinding/datagrid/DataGrid.java?view=diff&rev=124459&p1=incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/tags/databinding/datagrid/DataGrid.java&r1=124458&p2=incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/tags/databinding/datagrid/DataGrid.java&r2=124459
==============================================================================
--- 
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/tags/databinding/datagrid/DataGrid.java
 (original)
+++ 
incubator/beehive/trunk/netui/src/tags-databinding/org/apache/beehive/netui/tags/databinding/datagrid/DataGrid.java
 Thu Jan  6 14:26:10 2005
@@ -33,7 +33,6 @@
 import 
org.apache.beehive.netui.databinding.datagrid.model.style.EmptyStylePolicy;
 import org.apache.beehive.netui.databinding.datagrid.util.PagedDataSet;
 import 
org.apache.beehive.netui.databinding.datagrid.rendering.table.TableRenderer;
-import 
org.apache.beehive.netui.databinding.datagrid.rendering.table.TableRenderer;
 import 
org.apache.beehive.netui.databinding.datagrid.services.DataGridStateService;
 import org.apache.beehive.netui.script.common.IDataAccessProvider;
 import org.apache.beehive.netui.script.common.DataAccessProviderStack;
@@ -167,7 +166,6 @@
             /*
               now that the model objects have been initialized, it's time to 
start rendering
              */
-            //_gridModel.beforeRender();
             _gridModel.setRenderState(DataGridModel.START_RENDER_STATE);
 
             if (!_disableDefaultPager)

Modified: 
incubator/beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/datagrid/DefaultSortTest.java
Url: 
http://svn.apache.org/viewcvs/incubator/beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/datagrid/DefaultSortTest.java?view=diff&rev=124459&p1=incubator/beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/datagrid/DefaultSortTest.java&r1=124458&p2=incubator/beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/datagrid/DefaultSortTest.java&r2=124459
==============================================================================
--- 
incubator/beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/datagrid/DefaultSortTest.java
       (original)
+++ 
incubator/beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/datagrid/DefaultSortTest.java
       Thu Jan  6 14:26:10 2005
@@ -19,6 +19,7 @@
 
 import org.apache.beehive.netui.databinding.datagrid.model.sort.ISort;
 import org.apache.beehive.netui.databinding.datagrid.model.sort.DefaultSort;
+import 
org.apache.beehive.netui.databinding.datagrid.model.sort.ISort.Direction;
 import org.apache.beehive.netui.databinding.datagrid.services.sort.SortFactory;
 
 import junit.framework.Test;
@@ -41,11 +42,11 @@
         ISort sort = createSort();
         sort.setSortExpression("customername");
 
-        sort.setDirection(ISort.ASCENDING);
+        sort.setDirection(Direction.ASCENDING);
         String str = sort.write(gridName);
         assertTrue("testgrid~customername".equals(str));
 
-        sort.setDirection(ISort.DESCENDING);
+        sort.setDirection(Direction.DESCENDING);
         str = sort.write(gridName);
         assertTrue("testgrid~-customername".equals(str));
     }
@@ -53,45 +54,15 @@
     public void testChangeSortDirection() {
         ISort sort = createSort();
 
-        sort.setDirection(ISort.ASCENDING);
+        sort.setDirection(Direction.ASCENDING);
         sort.setSortExpression("customername");
-        assertTrue(sort.getDirection() == ISort.ASCENDING);
+        assertTrue(sort.getDirection() == Direction.ASCENDING);
 
         ((DefaultSort)sort).changeSortDirection();
-        assertTrue(sort.getDirection() == ISort.DESCENDING);
+        assertTrue(sort.getDirection() == Direction.DESCENDING);
 
         ((DefaultSort)sort).changeSortDirection();
-        assertTrue(sort.getDirection() == ISort.ASCENDING);
-    }
-
-    public void testInvalidSortDirection() {
-        try
-        {
-            ISort sort = createSort();
-            sort.setSortExpression("customername");
-            sort.setDirection(5);
-        }
-        catch(AssertionError ae) {
-            /* ignore */
-            return;
-        }
-
-        assertTrue("Didn't catch assertion as expected", false);
-    }
-
-    public void testInvalidSortExpression() {
-        try
-        {
-            ISort sort = createSort();
-            sort.setSortExpression("customername");
-            sort.setDirection(5);
-        }
-        catch(AssertionError ae) {
-            /* ignore */
-            return;
-        }
-
-        assertTrue("Didn't catch assertion as expected", false);
+        assertTrue(sort.getDirection() == Direction.ASCENDING);
     }
 
     protected void setUp() {

Modified: 
incubator/beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/datagrid/SortServiceTest.java
Url: 
http://svn.apache.org/viewcvs/incubator/beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/datagrid/SortServiceTest.java?view=diff&rev=124459&p1=incubator/beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/datagrid/SortServiceTest.java&r1=124458&p2=incubator/beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/datagrid/SortServiceTest.java&r2=124459
==============================================================================
--- 
incubator/beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/datagrid/SortServiceTest.java
       (original)
+++ 
incubator/beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/datagrid/SortServiceTest.java
       Thu Jan  6 14:26:10 2005
@@ -18,11 +18,13 @@
 package org.apache.beehive.netui.test.datagrid;
 
 import java.lang.reflect.Proxy;
+import java.util.List;
 import javax.servlet.ServletRequest;
 import javax.servlet.http.HttpServletRequest;
 
 import org.apache.beehive.netui.databinding.datagrid.services.sort.SortService;
 import org.apache.beehive.netui.databinding.datagrid.model.sort.ISort;
+import 
org.apache.beehive.netui.databinding.datagrid.model.sort.ISort.Direction;
 import org.apache.beehive.netui.test.servlet.ServletFactory;
 import org.apache.beehive.netui.test.servlet.HttpServletRequestHandler;
 
@@ -43,57 +45,52 @@
         return _request;
     }
 
-    public void testSingleAscendingSort() {
+    public void testEmptySortService() {
         SortService svc = SortService.getInstance(getRequest(), _namespace);
         assert svc.getSorts() == null;
-
+    }
+    public void testSingleAscendingSort() {
         DataGridTestUtil.initQueryString(_request, "netui_sort=" + _namespace 
+ "~CUSTOMERNAME");
-        svc.handleRequest(getRequest());
-        ISort[] sorts = svc.getSorts();
+        SortService svc = SortService.getInstance(getRequest(), _namespace);
+        List<ISort> sorts = svc.getSorts();
 
         assert svc.getSorts() != null;
-        assert sorts.length == 1;
-        assert sorts[0].getDirection() == ISort.ASCENDING;
-        assert sorts[0].getSortExpression().equals("CUSTOMERNAME");
+        assert sorts.size()== 1;
+        assert sorts.get(0).getDirection() == Direction.ASCENDING;
+        assert sorts.get(0).getSortExpression().equals("CUSTOMERNAME");
     }
 
     public void testMultipleSort() {
-        SortService svc = SortService.getInstance(getRequest(), _namespace);
-        assert svc.getSorts() == null;
-
         DataGridTestUtil.initQueryString(_request, "?netui_sort=" + _namespace 
+ "~CUSTOMERNAME" +
                                                    "&netui_sort=" + _namespace 
+ "~-UNITPRICE&" +
                                                    
"&netui_sort=productGrid~QUANTITY");
-        svc.handleRequest(getRequest());
-        ISort[] sorts = svc.getSorts();
+        SortService svc = SortService.getInstance(getRequest(), _namespace);
+        List<ISort> sorts = svc.getSorts();
 
         assert svc.getSorts() != null;
-        assert sorts.length == 2;
-        assert sorts[0].getDirection() == ISort.ASCENDING;
-        assert sorts[0].getSortExpression().equals("CUSTOMERNAME");
-        assert sorts[1].getDirection() == ISort.DESCENDING;
-        assert sorts[1].getSortExpression().equals("UNITPRICE");
+        assert sorts.size() == 2;
+        assert sorts.get(0).getDirection() == Direction.ASCENDING;
+        assert sorts.get(0).getSortExpression().equals("CUSTOMERNAME");
+        assert sorts.get(1).getDirection() == Direction.DESCENDING;
+        assert sorts.get(1).getSortExpression().equals("UNITPRICE");
 
         SortService productSvc = SortService.getInstance(getRequest(), 
"productGrid");
         assert productSvc.getSorts() != null;
-        assert productSvc.getSorts().length == 1;
+        assert productSvc.getSorts().size() == 1;
         sorts = productSvc.getSorts();
-        assert sorts[0].getDirection() == ISort.ASCENDING;
-        assert sorts[0].getSortExpression().equals("QUANTITY");
+        assert sorts.get(0).getDirection() == Direction.ASCENDING;
+        assert sorts.get(0).getSortExpression().equals("QUANTITY");
     }
 
     public void testSingleDescendingSort() {
-        SortService svc = SortService.getInstance(getRequest(), _namespace);
-        assert svc.getSorts() == null;
-
         DataGridTestUtil.initQueryString(_request, "netui_sort=" + _namespace 
+ "~-CUSTOMERNAME");
-        svc.handleRequest(getRequest());
-        ISort[] sorts = svc.getSorts();
+        SortService svc = SortService.getInstance(getRequest(), _namespace);
+        List<ISort> sorts = svc.getSorts();
 
         assert svc.getSorts() != null;
-        assert sorts.length == 1;
-        assert sorts[0].getDirection() == ISort.DESCENDING;
-        assert sorts[0].getSortExpression().equals("CUSTOMERNAME");
+        assert sorts.size() == 1;
+        assert sorts.get(0).getDirection() == Direction.DESCENDING;
+        assert sorts.get(0).getSortExpression().equals("CUSTOMERNAME");
     }
 
     protected void setUp() {

Reply via email to