empire-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From doeb...@apache.org
Subject empire-db git commit: EMPIREDB-235 make using page elments easier by providing constructor defaults
Date Thu, 25 Feb 2016 14:00:20 GMT
Repository: empire-db
Updated Branches:
  refs/heads/master 95a93b71c -> 5b88de4cc


EMPIREDB-235
make using page elments easier by providing constructor defaults

Project: http://git-wip-us.apache.org/repos/asf/empire-db/repo
Commit: http://git-wip-us.apache.org/repos/asf/empire-db/commit/5b88de4c
Tree: http://git-wip-us.apache.org/repos/asf/empire-db/tree/5b88de4c
Diff: http://git-wip-us.apache.org/repos/asf/empire-db/diff/5b88de4c

Branch: refs/heads/master
Commit: 5b88de4cc8d66f3ddb36146a11befc3dd49d75c3
Parents: 95a93b7
Author: Rainer Döbele <doebele@apache.org>
Authored: Thu Feb 25 15:00:15 2016 +0100
Committer: Rainer Döbele <doebele@apache.org>
Committed: Thu Feb 25 15:00:15 2016 +0100

----------------------------------------------------------------------
 .../websample/web/pages/EmployeeDetailPage.java |   3 +-
 .../websample/web/pages/EmployeeListPage.java   |  20 +++-
 .../jsf2/pageelements/BeanListPageElement.java  | 113 +++++++++++++------
 .../jsf2/pageelements/ListPageElement.java      |  18 +++
 .../jsf2/pageelements/RecordPageElement.java    |  20 +++-
 .../apache/empire/jsf2/pages/PageElement.java   |  43 +++++++
 6 files changed, 176 insertions(+), 41 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/empire-db/blob/5b88de4c/empire-db-examples/empire-db-example-jsf2/src/main/java/org/apache/empire/jsf2/websample/web/pages/EmployeeDetailPage.java
----------------------------------------------------------------------
diff --git a/empire-db-examples/empire-db-example-jsf2/src/main/java/org/apache/empire/jsf2/websample/web/pages/EmployeeDetailPage.java
b/empire-db-examples/empire-db-example-jsf2/src/main/java/org/apache/empire/jsf2/websample/web/pages/EmployeeDetailPage.java
index 3a19f89..571ed64 100644
--- a/empire-db-examples/empire-db-example-jsf2/src/main/java/org/apache/empire/jsf2/websample/web/pages/EmployeeDetailPage.java
+++ b/empire-db-examples/empire-db-example-jsf2/src/main/java/org/apache/empire/jsf2/websample/web/pages/EmployeeDetailPage.java
@@ -32,7 +32,6 @@ public class EmployeeDetailPage extends SamplePage
     private static final Logger               log               = LoggerFactory.getLogger(EmployeeDetailPage.class);
     private static final long                 serialVersionUID  = 1L;
 
-    private static final String               EMPLOYEE_PROPERTY = "employee";
     private String                            idParam;
 
     private RecordPageElement<EmployeeRecord> employee;
@@ -45,7 +44,7 @@ public class EmployeeDetailPage extends SamplePage
 
         SampleDB db = getDatabase();
         EmployeeRecord emplRec = new EmployeeRecord(db);
-        employee = new RecordPageElement<EmployeeRecord>(this, emplRec.getTable(),
emplRec, EMPLOYEE_PROPERTY);
+        employee = new RecordPageElement<EmployeeRecord>(this, emplRec.getTable(),
emplRec);
     }
 
     public String getIdParam()

http://git-wip-us.apache.org/repos/asf/empire-db/blob/5b88de4c/empire-db-examples/empire-db-example-jsf2/src/main/java/org/apache/empire/jsf2/websample/web/pages/EmployeeListPage.java
----------------------------------------------------------------------
diff --git a/empire-db-examples/empire-db-example-jsf2/src/main/java/org/apache/empire/jsf2/websample/web/pages/EmployeeListPage.java
b/empire-db-examples/empire-db-example-jsf2/src/main/java/org/apache/empire/jsf2/websample/web/pages/EmployeeListPage.java
index 9cff525..b015c4c 100644
--- a/empire-db-examples/empire-db-example-jsf2/src/main/java/org/apache/empire/jsf2/websample/web/pages/EmployeeListPage.java
+++ b/empire-db-examples/empire-db-example-jsf2/src/main/java/org/apache/empire/jsf2/websample/web/pages/EmployeeListPage.java
@@ -33,6 +33,7 @@ import org.apache.empire.db.DBCommand;
 import org.apache.empire.exceptions.BeanPropertyGetException;
 import org.apache.empire.jsf2.pageelements.BeanListPageElement;
 import org.apache.empire.jsf2.pageelements.ListPageElement;
+import org.apache.empire.jsf2.pageelements.ListPageElement.ParameterizedItem;
 import org.apache.empire.jsf2.websample.db.SampleDB;
 import org.apache.empire.jsf2.websample.db.SampleDB.TDepartments;
 import org.apache.empire.jsf2.websample.db.SampleDB.TEmployees;
@@ -47,11 +48,9 @@ public class EmployeeListPage extends SamplePage
     private static final Logger                    log               = LoggerFactory.getLogger(EmployeeListPage.class);
 
     
-    public static final String                     EMPLOYEES_PROPERTY = "employees";
     private BeanListPageElement<EmployeeListEntry> employees;
-
     
-    public static class EmployeeListEntry extends ListPageElement.SelectableItem
+    public static class EmployeeListEntry extends ListPageElement.SelectableItem implements
ParameterizedItem
     {
         private static final long serialVersionUID = 1L;
 
@@ -63,12 +62,21 @@ public class EmployeeListPage extends SamplePage
         private boolean	 	      retired;
         private String            idParam;
 
+        /**
+         * Implements ParameterizedItem.
+         * Used to uniquely identify this entry for selection and navigation 
+         */
         @Override
         public String getIdParam()
         {
             return this.idParam;
         }
 
+        /**
+         * Implements ParameterizedItem.
+         * This will automatically set the item idParam for navigation 
+         */
+        @Override
         public void setIdParam(String idParam)
         {
             this.idParam = idParam;
@@ -138,9 +146,9 @@ public class EmployeeListPage extends SamplePage
     {
         EmployeeListPage.log.trace("EmployeeListPage created");
         TEmployees EMP = getDatabase().T_EMPLOYEES;
-        
-        DBColumn defSortColumn = EMP.EMPLOYEE_ID;
-        employees = new BeanListPageElement<EmployeeListEntry>(this, EmployeeListEntry.class,
defSortColumn, EmployeeListPage.EMPLOYEES_PROPERTY);
+
+        // create the Employees List page element
+        employees = new BeanListPageElement<EmployeeListEntry>(this, EmployeeListEntry.class,
EMP.EMPLOYEE_ID);
     }
 
     

http://git-wip-us.apache.org/repos/asf/empire-db/blob/5b88de4c/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pageelements/BeanListPageElement.java
----------------------------------------------------------------------
diff --git a/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pageelements/BeanListPageElement.java
b/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pageelements/BeanListPageElement.java
index 81ceb08..0c60ab6 100644
--- a/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pageelements/BeanListPageElement.java
+++ b/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pageelements/BeanListPageElement.java
@@ -58,8 +58,6 @@ public class BeanListPageElement<T> extends ListPageElement<T>
implements ListIt
     private static final long   serialVersionUID     = 1L;
 
     private static final Logger log                  = LoggerFactory.getLogger(BeanListPageElement.class);
-
-    public static final String  IDPARAM_PROPERTY     = "idParam";
     
     public static final String  NO_RESULT_ATTRIBUTE  = "noQueryResult";
 
@@ -95,22 +93,67 @@ public class BeanListPageElement<T> extends ListPageElement<T>
implements ListIt
         }
     }
 
-    public BeanListPageElement(Page page, Class<T> beanClass, DBColumn defaultSortColumn,
String propertyName)
+    /**
+     * Constructor for creating a BeanListPageElement
+     * @param page the page element
+     * @param beanClass the bean class
+     * @param rowset required Table or View
+     * @param defaultSortColumn sort column that must belong to rowset
+     * @param propertyName the property name which is used to get and retrieve session information
+     */
+    public BeanListPageElement(Page page, Class<T> beanClass, DBRowSet rowset, DBColumn
defaultSortColumn, String propertyName)
     {
         super(page, beanClass, propertyName);
         // Check
-        if (defaultSortColumn == null)
-            throw new InvalidArgumentException("defaultSortColumn", defaultSortColumn);
-        // Set Bean Class and more
-        this.rowset = defaultSortColumn.getRowSet();
-        this.defaultSortColumn = defaultSortColumn;
+        if (rowset == null)
+            throw new InvalidArgumentException("rowset", rowset);
         // Default Sort Order
-        if (defaultSortColumn.getDataType() == DataType.DATE || defaultSortColumn.getDataType()
== DataType.DATETIME)
-        { // Date sort order is descending by default
-            defaultSortAscending = false;
+        if (defaultSortColumn!=null)
+        {   // Date sort order is descending by default
+            if (defaultSortColumn.getRowSet()!=rowset)
+                throw new InvalidArgumentException("defaultSortColumn", defaultSortColumn);
+            if (defaultSortColumn.getDataType() == DataType.DATE || defaultSortColumn.getDataType()
== DataType.DATETIME)
+                defaultSortAscending = false;
         }
+        // Set Bean Class and more
+        this.rowset = rowset;
+        this.defaultSortColumn = defaultSortColumn;
+    }
+
+    /**
+     * Overload that requires a default sort order to be provided
+     * @param page the page element
+     * @param beanClass the bean class
+     * @param defaultSortColumn
+     * @param propertyName the property name which is used to get and retrieve session information
+     */
+    public BeanListPageElement(Page page, Class<T> beanClass, DBColumn defaultSortColumn,
String propertyName)
+    {
+        this(page, beanClass, defaultSortColumn.getRowSet(), defaultSortColumn, propertyName);
     }
 
+    /**
+     * Overload that requires a default sort order to be provided
+     * @param page the page element
+     * @param beanClass the bean class
+     * @param defaultSortColumn
+     */
+    public BeanListPageElement(Page page, Class<T> beanClass, DBColumn defaultSortColumn)
+    {
+        this(page, beanClass, defaultSortColumn.getRowSet(), defaultSortColumn, getDefaultPropertyName(defaultSortColumn.getRowSet()));
+    }
+
+    /**
+     * Overload that requires a default sort order to be provided
+     * @param page the page element
+     * @param beanClass the bean class
+     * @param rowset required Table or View
+     */
+    public BeanListPageElement(Page page, Class<T> beanClass, DBRowSet rowSet)
+    {
+        this(page, beanClass, rowSet, null, getDefaultPropertyName(rowSet));
+    }
+    
     @Override
     protected void onInitPage()
     {
@@ -175,7 +218,7 @@ public class BeanListPageElement<T> extends ListPageElement<T>
implements ListIt
         if ((listTableInfo == null) && (listTableInfo = getSessionObject(ListTableInfo.class))
== null)
         { // Create and put on session
             listTableInfo = new BeanListTableInfo();
-            listTableInfo.setSortColumnName(defaultSortColumn.getName());
+            listTableInfo.setSortColumnName((defaultSortColumn!=null ? defaultSortColumn.getName()
: null));
             listTableInfo.setSortAscending(defaultSortAscending);
             setSessionObject(ListTableInfo.class, listTableInfo);
         }
@@ -226,12 +269,20 @@ public class BeanListPageElement<T> extends ListPageElement<T>
implements ListIt
         // log
         int count = getTableInfo().getItemCount();
         if (count==0)
-            FacesUtils.setRequestAttribute(FacesUtils.getContext(), NO_RESULT_ATTRIBUTE,
true);
+            handleNoResult();
         // log
         log.info("ItemList initialized for {} item count is {}.", getPropertyName(), count);
     }
 
     /**
+     * handle the case of an empty query result
+     */
+    protected void handleNoResult()
+    {
+        FacesUtils.setRequestAttribute(FacesUtils.getContext(), NO_RESULT_ATTRIBUTE, true);
+    }
+
+    /**
      * Init list items without pagination
      * 
      * @param queryCmd
@@ -390,7 +441,8 @@ public class BeanListPageElement<T> extends ListPageElement<T>
implements ListIt
         {
             log.error("Invalid Sort Column {}. Using Default!", sortColumnName);
             sortColumn = (DBColumn) getDefaultSortColumn();
-            getTableInfo().setSortColumnName(sortColumn.getName());
+            if (sortColumn!=null)
+                getTableInfo().setSortColumnName(sortColumn.getName());
         }
         // set Order
         setOrderBy(cmd, sortColumn, sortAscending);
@@ -398,14 +450,16 @@ public class BeanListPageElement<T> extends ListPageElement<T>
implements ListIt
 
     protected void setOrderBy(DBCommand cmd, DBColumnExpr sortColumn, boolean sortAscending)
     {
-        // Sort now
-        if (sortAscending)
-        {
-            cmd.orderBy(sortColumn);
-        }
-        else
-        {
-            cmd.orderBy(sortColumn.desc());
+        if (sortColumn!=null)
+        {   // Sort now
+            if (sortAscending)
+            {
+                cmd.orderBy(sortColumn);
+            }
+            else
+            {
+                cmd.orderBy(sortColumn.desc());
+            }
         }
         // Secondary sort
         if (this.secondarySortOrder != null && !secondarySortOrder.getColumn().equals(sortColumn))
@@ -524,21 +578,16 @@ public class BeanListPageElement<T> extends ListPageElement<T>
implements ListIt
     {
         DBColumn[] keyCols = rowset.getKeyColumns();
         if (keyCols == null)
-            throw new NoPrimaryKeyException(rowset);
+            return; // No Primary Key!
         // generate all
         for (Object item : items)
         {
+            if (!(item instanceof ParameterizedItem))
+                continue;
+            // set the idParam
             Object[] key = getItemKey(keyCols, item);
             String idparam = getParameterMap().put(rowset, key);
-            try
-            {
-                BeanUtils.setProperty(item, IDPARAM_PROPERTY, idparam);
-            }
-            catch (Exception e)
-            {
-                log.error("Error setting property idparam on bean.", e);
-                throw new InternalException(e);
-            }
+            ((ParameterizedItem)item).setIdParam(idparam);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/empire-db/blob/5b88de4c/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pageelements/ListPageElement.java
----------------------------------------------------------------------
diff --git a/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pageelements/ListPageElement.java
b/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pageelements/ListPageElement.java
index 9b644c4..a751a87 100644
--- a/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pageelements/ListPageElement.java
+++ b/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pageelements/ListPageElement.java
@@ -117,6 +117,20 @@ public abstract class ListPageElement<T> extends PageElement
         }
     }
 
+    /**
+     * Interface to uniquely identify a list item
+     * This will be used for selection and navigation 
+     */
+    public static interface ParameterizedItem {
+
+        public String getIdParam();
+        
+        public void setIdParam(String idParam);
+    }
+    
+    /**
+     * Abstract superclass to make a list item selectable
+     */
     public static abstract class SelectableItem implements Serializable
     {
         private static final long serialVersionUID = 1L;
@@ -151,6 +165,10 @@ public abstract class ListPageElement<T> extends PageElement
 
     }
 
+    /**
+     * This class holds information about the list view to display.
+     * This will be held on the session in order to maintain position and sorting when navigating
back and forth. 
+     */
     public static class ListTableInfo implements Serializable
     {
         private static final long serialVersionUID = 1L;

http://git-wip-us.apache.org/repos/asf/empire-db/blob/5b88de4c/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pageelements/RecordPageElement.java
----------------------------------------------------------------------
diff --git a/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pageelements/RecordPageElement.java
b/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pageelements/RecordPageElement.java
index e2b519e..e0f0ba3 100644
--- a/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pageelements/RecordPageElement.java
+++ b/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pageelements/RecordPageElement.java
@@ -44,15 +44,33 @@ public class RecordPageElement<T extends DBRecord> extends PageElement
 
     protected T record;
     
+    /**
+     * Creates a record page element for the given Table or View
+     * @param page the page element
+     * @param rowset Table or View
+     * @param record the record object for this page element
+     * @param propertyName the property name which is used to get and retrieve session information
+     */
     public RecordPageElement(Page page, DBRowSet rowset, T record, String propertyName)
     {
         super(page, propertyName);
         // Set Rowset and Record
         this.rowset = rowset;
         this.record = record;
-        
+        // log
         log.debug("RecordPageSupport for {} created.", rowset.getName());
     }
+
+    /**
+     * Creates a record page element for the given Table or View
+     * @param page the page element
+     * @param rowset Table or View
+     * @param record the record object for this page element
+     */
+    public RecordPageElement(Page page, DBRowSet rowset, T record)
+    {
+        this(page, rowset, record, getDefaultPropertyName(rowset));
+    }
     
     public T getRecord()
     {

http://git-wip-us.apache.org/repos/asf/empire-db/blob/5b88de4c/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pages/PageElement.java
----------------------------------------------------------------------
diff --git a/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pages/PageElement.java b/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pages/PageElement.java
index a78bd8c..e5c1147 100644
--- a/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pages/PageElement.java
+++ b/empire-db-jsf2/src/main/java/org/apache/empire/jsf2/pages/PageElement.java
@@ -27,6 +27,7 @@ import javax.faces.context.FacesContext;
 import org.apache.empire.commons.StringUtils;
 import org.apache.empire.db.DBDatabase;
 import org.apache.empire.db.DBObject;
+import org.apache.empire.db.DBRowSet;
 import org.apache.empire.exceptions.InvalidArgumentException;
 
 
@@ -113,6 +114,48 @@ public class PageElement implements Serializable
     }
     
     /**
+     * generates a default property name for the bean list
+     * @param rowset
+     * @return a propertyName
+     */
+    protected static String getDefaultPropertyName(DBRowSet rowset)
+    {
+        String name = rowset.getName();
+        if (name==null)
+            return "unknown"; // no name provided!
+        // compute name
+        name = name.toLowerCase();        
+        String res = "";
+        int beg=0;
+        while (beg<name.length())
+        {
+            int end = name.indexOf('_', beg);
+            if (end<0)
+                end = name.length();
+            // assemble
+            if (end>beg)
+            {
+                if (beg==0)
+                {   // begin with all lower cases
+                    res = name.substring(beg, end);
+                }
+                else
+                {   // add word where first letter is upper case 
+                    res += name.substring(beg, beg+1).toUpperCase();
+                    if (end-beg>1)
+                    {
+                        res += name.substring(beg+1, end);
+                    }
+                }
+            }
+            // next
+            beg = end + 1;
+        }
+        // Result
+        return res;
+    }
+    
+    /**
      * Javascript Call
      * 
      * @param function


Mime
View raw message