click-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sa...@apache.org
Subject svn commit: r1079936 - in /click/trunk/click: documentation/docs/roadmap-changes.html extras/src/org/apache/click/extras/cayenne/CayenneForm.java extras/src/org/apache/click/extras/cayenne/CayenneUtils.java
Date Wed, 09 Mar 2011 19:02:35 GMT
Author: sabob
Date: Wed Mar  9 19:02:34 2011
New Revision: 1079936

URL: http://svn.apache.org/viewvc?rev=1079936&view=rev
Log:
Moved CayenneForm validation logic into CayenneUtils for easy reuse. CLK-756

Modified:
    click/trunk/click/documentation/docs/roadmap-changes.html
    click/trunk/click/extras/src/org/apache/click/extras/cayenne/CayenneForm.java
    click/trunk/click/extras/src/org/apache/click/extras/cayenne/CayenneUtils.java

Modified: click/trunk/click/documentation/docs/roadmap-changes.html
URL: http://svn.apache.org/viewvc/click/trunk/click/documentation/docs/roadmap-changes.html?rev=1079936&r1=1079935&r2=1079936&view=diff
==============================================================================
--- click/trunk/click/documentation/docs/roadmap-changes.html (original)
+++ click/trunk/click/documentation/docs/roadmap-changes.html Wed Mar  9 19:02:34 2011
@@ -199,6 +199,12 @@ Action support and light-weight stateful
       </div>
       <ul style="padding: 0em; margin-left:0em;margin-bottom: 2em">
           <li class="change">
+              Added new CayenneUtils method, <a href="extras-api/org/apache/click/extras/cayenne/CayenneUtils.html#applyMetaData(org.apache.click.control.Form,
java.lang.Class)">applyMetaData(Form, Class)</a>,
+              for adding validation metadata such as required and maxlength
+              to form fields for a given DataObject type
+          [<a target="_blank" href="https://issues.apache.org/jira/browse/CLK-756">CLK-756</a>].
+          </li>
+          <li class="change">
               Added support for multiple TabbedPanels on a page. To programmatically
               activate a specific TabbedPanel use the <tt>tabPanelIndex-&lt;panel-name&gt;</tt>
               request parameter, for example <tt>tabPanelIndex-myTabbedPanel</tt>

Modified: click/trunk/click/extras/src/org/apache/click/extras/cayenne/CayenneForm.java
URL: http://svn.apache.org/viewvc/click/trunk/click/extras/src/org/apache/click/extras/cayenne/CayenneForm.java?rev=1079936&r1=1079935&r2=1079936&view=diff
==============================================================================
--- click/trunk/click/extras/src/org/apache/click/extras/cayenne/CayenneForm.java (original)
+++ click/trunk/click/extras/src/org/apache/click/extras/cayenne/CayenneForm.java Wed Mar
 9 19:02:34 2011
@@ -23,12 +23,9 @@ import java.util.List;
 import java.util.Map;
 
 import org.apache.cayenne.BaseContext;
-import org.apache.click.control.Checkbox;
 import org.apache.click.control.Field;
 import org.apache.click.control.Form;
 import org.apache.click.control.HiddenField;
-import org.apache.click.control.TextArea;
-import org.apache.click.control.TextField;
 import org.apache.click.util.ClickUtils;
 import org.apache.click.util.ContainerUtils;
 import org.apache.click.util.HtmlStringBuffer;
@@ -37,8 +34,6 @@ import org.apache.cayenne.DataObject;
 import org.apache.cayenne.DataObjectUtils;
 import org.apache.cayenne.PersistenceState;
 import org.apache.cayenne.access.DataContext;
-import org.apache.cayenne.map.DbAttribute;
-import org.apache.cayenne.map.ObjAttribute;
 import org.apache.cayenne.map.ObjEntity;
 import org.apache.cayenne.map.ObjRelationship;
 import org.apache.cayenne.validation.ValidationException;
@@ -615,21 +610,9 @@ public class CayenneForm extends Form {
 
         try {
             Class dataClass = ClickUtils.classForName(classField.getValue());
+            CayenneUtils.applyMetaData(this, dataClass);
 
-            ObjEntity objEntity =
-                getDataContext().getEntityResolver().lookupObjEntity(dataClass);
-
-            setObjEntityFieldConstrains(null, objEntity);
-
-            for (ObjRelationship objRelationship : objEntity.getRelationships()) {
-                String relName = objRelationship.getName();
-                ObjEntity relObjEntity =
-                        (ObjEntity) objRelationship.getTargetEntity();
-
-                setObjEntityFieldConstrains(relName, relObjEntity);
-            }
-
-        } catch (Exception e) {
+} catch (ClassNotFoundException e) {
             throw new RuntimeException(e);
         }
 
@@ -646,50 +629,4 @@ public class CayenneForm extends Form {
         return dataObject.getPersistenceState() != PersistenceState.TRANSIENT
                 && dataObject.getPersistenceState() != PersistenceState.NEW;
     }
-
-    /**
-     * Set the <tt>ObjEntity</tt> meta data constraints on the form fields.
-     *
-     * @param relationshipName the object relationship name, null if the
-     *      ObjEntity of the CayenneForm
-     * @param objEntity the ObjEntity to lookup meta data from
-     */
-    protected void setObjEntityFieldConstrains(String relationshipName,
-            ObjEntity objEntity) {
-
-        for (ObjAttribute objAttribute : objEntity.getAttributes()) {
-            DbAttribute dbAttribute = objAttribute.getDbAttribute();
-
-            String fieldName = objAttribute.getName();
-            if (relationshipName != null) {
-                fieldName = relationshipName + "." + fieldName;
-            }
-
-            Field field = getField(fieldName);
-
-            if (field != null) {
-                if (!field.isRequired() && dbAttribute.isMandatory()) {
-                    if (!(field instanceof Checkbox)) {
-                        field.setRequired(true);
-                    }
-                }
-
-                int maxlength = dbAttribute.getMaxLength();
-                if (maxlength != -1) {
-                    if (field instanceof TextField) {
-                        TextField textField = (TextField) field;
-                        if (textField.getMaxLength() == 0) {
-                            textField.setMaxLength(maxlength);
-                        }
-                    } else if (field instanceof TextArea) {
-                        TextArea textArea = (TextArea) field;
-                        if (textArea.getMaxLength() == 0) {
-                            textArea.setMaxLength(maxlength);
-                        }
-                    }
-                }
-            }
-        }
-    }
-
 }

Modified: click/trunk/click/extras/src/org/apache/click/extras/cayenne/CayenneUtils.java
URL: http://svn.apache.org/viewvc/click/trunk/click/extras/src/org/apache/click/extras/cayenne/CayenneUtils.java?rev=1079936&r1=1079935&r2=1079936&view=diff
==============================================================================
--- click/trunk/click/extras/src/org/apache/click/extras/cayenne/CayenneUtils.java (original)
+++ click/trunk/click/extras/src/org/apache/click/extras/cayenne/CayenneUtils.java Wed Mar
 9 19:02:34 2011
@@ -20,6 +20,7 @@ package org.apache.click.extras.cayenne;
 
 import java.util.Collection;
 import java.util.List;
+import org.apache.cayenne.BaseContext;
 
 import org.apache.click.util.ClickUtils;
 
@@ -30,9 +31,16 @@ import org.apache.cayenne.ObjectId;
 import org.apache.cayenne.dba.TypesMapping;
 import org.apache.cayenne.map.DbAttribute;
 import org.apache.cayenne.map.DbEntity;
+import org.apache.cayenne.map.ObjAttribute;
 import org.apache.cayenne.map.ObjEntity;
+import org.apache.cayenne.map.ObjRelationship;
 import org.apache.cayenne.query.ObjectIdQuery;
 import org.apache.cayenne.query.Query;
+import org.apache.click.control.Checkbox;
+import org.apache.click.control.Field;
+import org.apache.click.control.Form;
+import org.apache.click.control.TextArea;
+import org.apache.click.control.TextField;
 import org.apache.commons.lang.Validate;
 
 /**
@@ -43,6 +51,43 @@ import org.apache.commons.lang.Validate;
  */
 public final class CayenneUtils {
 
+
+    /**
+     * Applies the <tt>DataObject class</tt> validation database meta data to
the
+     * form fields.
+     * <p/>
+     * The field validation attributes include:
+     * <ul>
+     * <li>required - is a mandatory field and cannot be null</li>
+     * <li>maxLength - the maximum length of the field</li>
+     * </ul>
+     *
+     * @param form the form which fields to apply metadata to
+     * @param dataObjectClass the dataObject class which metadata to apply
+     */
+    public static void applyMetaData(Form form, Class<?> dataObjectClass) {
+        Validate.notNull(dataObjectClass, "Null dataObjectClass parameter");
+
+        try {
+            ObjectContext oc = BaseContext.getThreadObjectContext();
+            ObjEntity objEntity =
+                oc.getEntityResolver().lookupObjEntity(dataObjectClass);
+
+            setObjEntityFieldConstraints(form, null, objEntity);
+
+            for (ObjRelationship objRelationship : objEntity.getRelationships()) {
+                String relName = objRelationship.getName();
+                ObjEntity relObjEntity =
+                        (ObjEntity) objRelationship.getTargetEntity();
+
+                setObjEntityFieldConstraints(form, relName, relObjEntity);
+            }
+
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
     /**
      * Create a new ObjectId for the given ObjectContext, data object class and
      * primary key.
@@ -107,7 +152,7 @@ public final class CayenneUtils {
 
         List objects = objectContext.performQuery(query);
 
-        if (objects.size() == 0) {
+        if (objects.isEmpty()) {
             return null;
 
         } else if (objects.size() > 1) {
@@ -203,4 +248,48 @@ public final class CayenneUtils {
         return attr.getName();
     }
 
+    /**
+     * Set the <tt>ObjEntity</tt> meta data constraints on the form fields.
+     *
+     * @param form the form on which to set the field constraints
+     * @param relationshipName the object relationship name, null if the
+     *      ObjEntity of the CayenneForm
+     * @param objEntity the ObjEntity to lookup meta data from
+     */
+    private static void setObjEntityFieldConstraints(Form form, String relationshipName,
ObjEntity objEntity) {
+
+        for (ObjAttribute objAttribute : objEntity.getAttributes()) {
+            DbAttribute dbAttribute = objAttribute.getDbAttribute();
+
+            String fieldName = objAttribute.getName();
+            if (relationshipName != null) {
+                fieldName = relationshipName + "." + fieldName;
+            }
+
+            Field field = form.getField(fieldName);
+
+            if (field != null) {
+                if (!field.isRequired() && dbAttribute.isMandatory()) {
+                    if (!(field instanceof Checkbox)) {
+                        field.setRequired(true);
+                    }
+                }
+
+                int maxlength = dbAttribute.getMaxLength();
+                if (maxlength != -1) {
+                    if (field instanceof TextField) {
+                        TextField textField = (TextField) field;
+                        if (textField.getMaxLength() == 0) {
+                            textField.setMaxLength(maxlength);
+                        }
+                    } else if (field instanceof TextArea) {
+                        TextArea textArea = (TextArea) field;
+                        if (textArea.getMaxLength() == 0) {
+                            textArea.setMaxLength(maxlength);
+                        }
+                    }
+                }
+            }
+        }
+    }
 }



Mime
View raw message