Youngho Cho created TRB-93:
------------------------------

             Summary: Intake add option to skip field.setProperty method when 
field value does not set
                 Key: TRB-93
                 URL: https://issues.apache.org/jira/browse/TRB-93
             Project: Turbine
          Issue Type: Improvement
          Components: Fulcrum
            Reporter: Youngho Cho


During migration from old intake to current trunk version,
I found that Field.setProperty method behavior changed from when it does not 
execute when the value does not set to force execute using safe empty default 
value.

I hope to give old behavior as option by adding useSafeEmpty property.

Here is a patch.


Thanks,

Youngho

----------------------------------------------

Index: src/java/org/apache/fulcrum/intake/model/Field.java
===================================================================
--- src/java/org/apache/fulcrum/intake/model/Field.java (revision 1637028)
+++ src/java/org/apache/fulcrum/intake/model/Field.java (working copy)
@@ -42,7 +42,7 @@
  * Base class for Intake generated input processing classes.
  *
  * @author <a href="mailto:jmcna...@collab.net";>John McNally</a>
- * @author <a href="mailto:d...@finemaltcoding.com>Daniel Rall</a>
+ * @author <a href="mailto:d...@finemaltcoding.com";>Daniel Rall</a>
  * @author <a href="mailto:h...@intermeta.de";>Henning P. Schmiedehausen</a>
  * @author <a href="mailto:quint...@bellsouth.net";>Quinton McCombs</a>
  * @author <a href="mailto:j...@byteaction.de";>J&uuml;rgen Hoffmann</a>
@@ -133,6 +133,9 @@
     /** Has the field has been set from the parser? */
     protected boolean initialized;
 
+    /** useSafeEmptyValue or not when field unset */
+    protected boolean useSafeEmpty = true;
+
     /** Error message, is any, resulting from validation */
     protected String message;
 
@@ -468,6 +471,15 @@
     }
 
     /**
+     *
+     * @param v value to assign to useSafeEmpty
+     */
+    protected void setUseSafeEmpty(boolean v)
+    {
+        useSafeEmpty = v;
+    }
+    
+    /**
      * Removes references to this group and its fields from the
      * query parameters
      */
@@ -490,6 +502,7 @@
         validFlag = false;
         validated = false;
         required = false;
+        useSafeEmpty = true;
         message = null;
         retrievable = null;
 
@@ -904,7 +917,7 @@
                 log.debug(name + ": Property is set, value is " + valArray[0]);
             }
         }
-        else
+        else if(useSafeEmpty)
         {
             valArray[0] = getSafeEmptyValue();
             if (isDebugEnabled)
@@ -912,6 +925,17 @@
                 log.debug(name + ": Property is not set, using emptyValue " + 
valArray[0]);
             }
         }
+        else
+        {
+            if (isDebugEnabled)
+            {
+                log.debug("Skip unset " + name + ".setProperty(" + 
obj.getClass().getName() + ")");
+            }
+            /*
+             * Do not invoke unset field if useSafeEmpty doesn't accept
+             */
+            return;
+        }
 
         try
         {
Index: src/java/org/apache/fulcrum/intake/xmlmodel/AppData.java
===================================================================
--- src/java/org/apache/fulcrum/intake/xmlmodel/AppData.java    (revision 
1629357)
+++ src/java/org/apache/fulcrum/intake/xmlmodel/AppData.java    (working copy)
@@ -29,7 +29,7 @@
 /**
  * A class for holding application data structures.
  *
- * @author <a href="mailto:jmcna...@collab.net>John McNally</a>
+ * @author <a href="mailto:jmcna...@collab.net";>John McNally</a>
  * @author <a href="mailto:h...@intermeta.de";>Henning P. Schmiedehausen</a>
  * @author <a href="mailto:t...@apache.org";>Thomas Vandahl</a>
  * @version $Id$
Index: src/java/org/apache/fulcrum/intake/xmlmodel/XmlGroup.java
===================================================================
--- src/java/org/apache/fulcrum/intake/xmlmodel/XmlGroup.java   (revision 
1629357)
+++ src/java/org/apache/fulcrum/intake/xmlmodel/XmlGroup.java   (working copy)
@@ -29,7 +29,7 @@
 /**
  * A Class for holding data about a grouping of inputs used in an Application.
  *
- * @author <a href="mailto:jmcna...@collab.net>John McNally</a>
+ * @author <a href="mailto:jmcna...@collab.net";>John McNally</a>
  * @author <a href="mailto:t...@apache.org";>Thomas Vandahl</a>
  * @version $Id$
  */
Index: src/test/intake1.xml
===================================================================
--- src/test/intake1.xml        (revision 1629357)
+++ src/test/intake1.xml        (working copy)
@@ -53,7 +53,19 @@
         <field name="EmptyDoubleTestField" key="edtf" type="double"/>
         <field name="EmptyBigDecimalTestField" key="ebdtf" type="BigDecimal"/>
         <field name="NumberTestField" key="ntf" type="int">
-            <rule name="invalidNumber">Not a number</rule>
+            <rule name="invalidNumber" value="">Not a number</rule>
         </field>
     </group>
+
+<group name="AttributeValue" key="attv" mapToObject="AttributeValue">
+        <field name="Id" key="id" type="long" mapToProperty="ValueId">
+            <rule name="invalidNumber" value="">intakeBadIdMessage</rule>
+        </field>
+        <field name="AttributeId" key="attid" type="int">
+        </field>
+        <field name="UserId" key="visid" type="custom"
+                       
fieldClass="org.apache.fulcrum.intake.test.MyIntegerField">
+        </field>
+</group>
+
 </input-data>
Index: src/test/org/apache/fulcrum/intake/IntakeTest.java
===================================================================
--- src/test/org/apache/fulcrum/intake/IntakeTest.java  (revision 1629357)
+++ src/test/org/apache/fulcrum/intake/IntakeTest.java  (working copy)
@@ -23,6 +23,7 @@
 
 import org.apache.fulcrum.intake.model.Field;
 import org.apache.fulcrum.intake.model.Group;
+import org.apache.fulcrum.intake.test.AttributeValue;
 import org.apache.fulcrum.intake.test.LoginForm;
 import org.apache.fulcrum.intake.validator.BooleanValidator;
 import org.apache.fulcrum.intake.validator.IntegerValidator;
@@ -255,4 +256,40 @@
             assertEquals("Invalid number message is wrong.", "Not a number", 
ve.getMessage());
         }
     }
+    
+    public void testEmptyNumberField() throws Exception
+    {
+        IntakeService is = (IntakeService) this.resolve( 
IntakeService.class.getName() );
+        Group group = is.getGroup("AttributeValue");
+        assertNotNull(group);
+
+        ParserService ps = (ParserService) this.resolve( 
ParserService.class.getName() );
+        ValueParser pp = ps.getParser(DefaultParameterParser.class);
+
+        pp.add("attv_0attid", "1");
+        group.init(pp);
+        
+        Field<?> attributeField = group.get("AttributeId");
+        assertNotNull(attributeField);
+        assertEquals(1, attributeField.getValue());
+        assertTrue(attributeField.isSet());
+        
+        Field<?> valueField = group.get("Id");
+        assertNull(valueField.getValue());
+        assertFalse(valueField.isSet());
+        
+        Field<?> userField = group.get("UserId");
+        assertNull(userField.getValue());
+        assertFalse(userField.isSet());
+        
+        //
+        assertTrue(group.isAllValid());
+        //
+        AttributeValue value = new AttributeValue();
+        group.setProperties(value);
+        
+        assertEquals(value.toString(), Long.valueOf(0), value.getValueId());
+        assertEquals(value.toString(), Integer.valueOf(1), 
value.getAttributeId());
+        assertNull(value.toString(), value.getUserId());
+    }
 }
Index: src/test/org/apache/fulcrum/intake/test/AttributeValue.java
===================================================================
--- src/test/org/apache/fulcrum/intake/test/AttributeValue.java (revision 0)
+++ src/test/org/apache/fulcrum/intake/test/AttributeValue.java (working copy)
@@ -0,0 +1,61 @@
+package org.apache.fulcrum.intake.test;
+
+import org.apache.commons.lang.ArrayUtils;
+
+public class AttributeValue {
+
+    private Long valueId;
+    
+    private Integer attributeId;
+    
+    private Integer userId;
+    
+  /**
+   * @return
+   */
+  public Long getValueId() {
+    return valueId;
+  }
+
+  /**
+   * @param userId
+   */
+  public void setValueId(Long valueId) {
+    this.valueId = valueId;
+  }
+  
+  /**
+   * @return
+   */
+  public Integer getAttributeId() {
+    return attributeId;
+  }
+
+  /**
+   * @param attributeId
+   */
+  public void setAttributeId(Integer attributeId) {
+    this.attributeId = attributeId;
+  }
+
+  public Integer getUserId() {
+      return userId;
+  }
+  /**
+   * @param userId
+   */
+  public void setUserId(Integer userId) {
+      this.userId = userId;
+  }
+
+  public String toString() {
+      final StringBuilder sb = new StringBuilder();
+      sb.append("ValueId ");
+      sb.append(this.valueId);
+      sb.append(", AttributeId ");
+      sb.append(this.attributeId);
+      sb.append(", UserId ");
+      sb.append(this.userId);
+      return sb.toString();
+  }
+}
Index: src/test/org/apache/fulcrum/intake/test/MyIntegerField.java
===================================================================
--- src/test/org/apache/fulcrum/intake/test/MyIntegerField.java (revision 0)
+++ src/test/org/apache/fulcrum/intake/test/MyIntegerField.java (working copy)
@@ -0,0 +1,36 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+package org.apache.fulcrum.intake.test;
+
+import org.apache.fulcrum.intake.IntakeException;
+import org.apache.fulcrum.intake.model.Group;
+import org.apache.fulcrum.intake.model.IntegerField;
+import org.apache.fulcrum.intake.xmlmodel.XmlField;
+
+public class MyIntegerField extends IntegerField
+{
+
+    public MyIntegerField(XmlField field, Group group) throws IntakeException
+    {
+        super(field, group);
+        setUseSafeEmpty(false);
+        log.info("Instance of MyIntegerField created.");
+    }
+
+}




--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to