Author: michiel
Date: 2009-06-05 09:46:40 +0200 (Fri, 05 Jun 2009)
New Revision: 35742

Modified:
   
mmbase/trunk/applications/taglib/src/main/java/org/mmbase/bridge/jsp/taglib/typehandler/AbstractTypeHandler.java
   
mmbase/trunk/applications/taglib/src/main/java/org/mmbase/bridge/jsp/taglib/typehandler/EnumHandler.java
Log:
MMB-1828

Modified: 
mmbase/trunk/applications/taglib/src/main/java/org/mmbase/bridge/jsp/taglib/typehandler/AbstractTypeHandler.java
===================================================================
--- 
mmbase/trunk/applications/taglib/src/main/java/org/mmbase/bridge/jsp/taglib/typehandler/AbstractTypeHandler.java
    2009-06-05 07:10:22 UTC (rev 35741)
+++ 
mmbase/trunk/applications/taglib/src/main/java/org/mmbase/bridge/jsp/taglib/typehandler/AbstractTypeHandler.java
    2009-06-05 07:46:40 UTC (rev 35742)
@@ -203,6 +203,7 @@
         return field.getDataType().cast(value, node, field);
     }
 
+
     /**
      * Returns the field value to be used in the page.
      */
@@ -213,7 +214,7 @@
             log.debug("No value found in context for " + fieldName);
             if (node != null) {
                 value = node.isNull(fieldName) ? null : getValue(node, 
fieldName);
-                log.debug("Value found in node " + value);
+                log.debug("Value for " + fieldName + " found in node " + 
value);
             } else if (useDefault) {
                 value = field.getDataType().getDefaultValue(tag.getLocale(), 
tag.getCloudVar(), field);
                 log.debug("No Node, defaultvalue found in field " + value);
@@ -224,6 +225,14 @@
         return value;
     }
 
+    /**
+     * @since MMBase-1.9.2
+     */
+    protected Object convertToValidate(Object value, Node node, Field field) 
throws JspTagException {
+        return value;
+    }
+
+
     public String checkHtmlInput(Node node, Field field, boolean errors) 
throws JspTagException {
         eh = getEnumHandler(node, field);
         if (eh != null) {
@@ -241,7 +250,7 @@
         if (log.isDebugEnabled()) {
             log.debug("Value for field " + field + ": " + fieldValue + " and 
node " + node);
         }
-        Collection<LocalizedString> col = dt.castAndValidate(fieldValue, node, 
field);
+        Collection<LocalizedString> col = 
dt.castAndValidate(convertToValidate(fieldValue, node, field), node, field);
         if (col.size() == 0) {
             // do actually set the field, because some datatypes need 
cross-field checking
             // also in an mm:form, you can simply commit.
@@ -308,7 +317,7 @@
     protected Object getValue(Node node, String fieldName) {
         Object v = node.getValue(fieldName);
         if (log.isDebugEnabled()) {
-            log.debug("Value for " + fieldName + ": " + v + " of " + 
node.getClass() + " " + node.getNodeManager().getField(fieldName));
+            log.debug("Value for " + node.getNumber() + ":" + fieldName + ": " 
+ v.getClass() + " " + v + " of " + node.getClass());
         }
         return v;
     }

Modified: 
mmbase/trunk/applications/taglib/src/main/java/org/mmbase/bridge/jsp/taglib/typehandler/EnumHandler.java
===================================================================
--- 
mmbase/trunk/applications/taglib/src/main/java/org/mmbase/bridge/jsp/taglib/typehandler/EnumHandler.java
    2009-06-05 07:10:22 UTC (rev 35741)
+++ 
mmbase/trunk/applications/taglib/src/main/java/org/mmbase/bridge/jsp/taglib/typehandler/EnumHandler.java
    2009-06-05 07:46:40 UTC (rev 35742)
@@ -126,21 +126,29 @@
         return field.getDataType().cast(value, node, field);
     }
 
-    @Override protected Object getFieldValue(Node node, Field field, boolean 
useDefault) throws JspTagException {
-        Object value = super.getFieldValue(node, field, useDefault);
-        // if an enum is required ('not null'), and no default value was 
specified, then we simply default to the first
-        // entry, as HTML rendering would do any way.
-        if (value == null && field.getDataType().isRequired()) {
-            Iterator i = getIterator(node, field);
-            if (i!= null && i.hasNext()) {
-                value = ((Map.Entry) i.next()).getKey();
-            }
+
+    @Override
+    protected Object convertToValidate(final Object s, final Node node, final 
Field field) throws JspTagException {
+        DataType dt = field.getDataType();
+        if (s == null && ! dt.isRequired()) return null;
+        Iterator i = getIterator(node, field);
+        Object firstValue = null;
+        if (i!= null && i.hasNext()) {
+            Object value = ((Map.Entry) i.next()).getKey();
+            if (value.equals(s)) return value;
+            firstValue = value;
         }
-        return value;
+        while (i!= null && i.hasNext()) {
+            Object value = ((Map.Entry) i.next()).getKey();
+            if (value.equals(s)) return value;
+        }
+
+        return firstValue;
+
     }
 
 
-    @Override public String htmlInput(Node node, Field field, boolean search) 
throws JspTagException {
+    @Override public String htmlInput(final Node node, final Field field, 
final boolean search) throws JspTagException {
         StringBuilder buffer = new StringBuilder();
         String fieldName = field.getName();
         buffer.append("<select class=\"" + getClasses(node, field) + "\" 
name=\"").append(prefix(fieldName)).append("\" ");
@@ -150,10 +158,13 @@
         }
         addExtraAttributes(buffer);
         buffer.append(">");
-        Object value  = cast(getFieldValue(node, field, true), node, field);
+        Object uncastValue = getFieldValue(node, field, true);
+        Object unvalidValue  = cast(uncastValue, node, field);
+        Object value         = convertToValidate(unvalidValue, node, field);
         if (log.isDebugEnabled()) {
-            log.debug("using value " + (value == null ? "NULL" : 
value.getClass().getName() + " " + value));
+            log.debug("cast " + uncastValue + " -> " + unvalidValue + " -> " + 
value + " -> " + Casting.toString(value));
         }
+
         if (! field.getDataType().isRequired() && ! multiple) {
             buffer.append("<option value=\"\" ");
             if (value == null) buffer.append("selected=\"selected\" ");
@@ -164,6 +175,10 @@
         }
 
         List<String> valueString = multiple ? new ArrayList<String>() : 
Collections.singletonList(Casting.toString(value));
+
+        if (log.isDebugEnabled()) {
+            log.debug("using value for " + field + " " + (value == null ? 
"NULL" : value.getClass().getName() + " " + value) + " asString: " + 
valueString);
+        }
         if (multiple) {
             for (Object v : Casting.toList(value)) {
                 valueString.add(Casting.toString(v));

_______________________________________________
Cvs mailing list
[email protected]
http://lists.mmbase.org/mailman/listinfo/cvs

Reply via email to