← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 2100: Moved method convertDataEntryForm to the DataEntryForm object, makes it a bit more object-oriented

 

------------------------------------------------------------
revno: 2100
committer: Lars <larshelg@larshelg-laptop>
branch nick: trunk
timestamp: Mon 2010-09-13 13:06:01 +0200
message:
  Moved method convertDataEntryForm to the DataEntryForm object, makes it a bit more object-oriented
added:
  dhis-2/dhis-api/src/test/java/org/hisp/dhis/dataentryform/
  dhis-2/dhis-api/src/test/java/org/hisp/dhis/dataentryform/DataEntryFormTest.java
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DeleteNotAllowedException.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataentryform/DataEntryForm.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataentryform/DataEntryFormService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataentryform/DefaultDataEntryFormService.java
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataentryform/DataEntryFormServiceTest.java
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ExceptionUtils.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DeleteNotAllowedException.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DeleteNotAllowedException.java	2010-04-12 21:23:33 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/DeleteNotAllowedException.java	2010-09-13 11:06:01 +0000
@@ -29,7 +29,6 @@
 
 /**
  * @author Lars Helge Overland
- * @version $Id$
  */
 public class DeleteNotAllowedException
     extends RuntimeException

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataentryform/DataEntryForm.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataentryform/DataEntryForm.java	2010-04-12 21:23:33 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataentryform/DataEntryForm.java	2010-09-13 11:06:01 +0000
@@ -28,10 +28,19 @@
  */
 
 import java.io.Serializable;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
+/**
+ * @author Bharath Kumar
+ */
 public class DataEntryForm
     implements Serializable
 {
+    public static final Pattern INPUT_PATTERN = Pattern.compile( "value\\[\\d+\\]\\.value:value\\[\\d+\\]\\.value" );
+    public static final Pattern OPERAND_PATTERN = Pattern.compile( "\\d+" );
+    
     /**
      * The unique identifier for this DataEntryForm
      */
@@ -67,6 +76,59 @@
     }
 
     // -------------------------------------------------------------------------
+    // Logic
+    // -------------------------------------------------------------------------
+
+    /**
+     * Map the identifiers inside the HTML code according to the provided data 
+     * element identifier and category option combo identifier mappings.
+     * 
+     * @param htmlCode the HTML code.
+     * @param dataElementMap the mapping between data element identifiers to be 
+     *        converted.
+     * @param categoryOptionComboMap the mapping between category option combo
+     *        identifiers to be converted.
+     * @return the converted HTML code.
+     */
+    public void convertDataEntryForm( Map<Object, Integer> dataElementMap, Map<Object, Integer> categoryOptionComboMap )
+    {
+        Matcher inputMatcher = INPUT_PATTERN.matcher( htmlCode );        
+        StringBuffer buffer = new StringBuffer();
+        
+        while ( inputMatcher.find() )
+        {
+            String input = inputMatcher.group();
+            Matcher operandMatcher = OPERAND_PATTERN.matcher( input );
+            
+            operandMatcher.find();            
+            String d = operandMatcher.group();
+            throwException( d == null, "Could not find data element identifier in form" );
+            Integer dataElement = dataElementMap.get( Integer.parseInt( d ) );
+            throwException( dataElement == null, "Data element identifier does not exist: " + d );
+            
+            operandMatcher.find();
+            String c = operandMatcher.group();
+            throwException( c == null, "Could not find category option combo identifier in form" );
+            Integer categoryOptionCombo = categoryOptionComboMap.get( Integer.parseInt( c ) );
+            throwException( categoryOptionCombo == null, "Category option combo identifier does not exist: " + c );
+            
+            inputMatcher.appendReplacement( buffer, "value[" + dataElement + "].value:value[" + categoryOptionCombo + "].value" );
+        }
+        
+        inputMatcher.appendTail( buffer );
+        
+        this.htmlCode = buffer.toString();
+    }
+
+    private static void throwException( boolean condition, String message )
+    {
+        if ( condition )
+        {
+            throw new IllegalArgumentException( message );
+        }
+    }
+    
+    // -------------------------------------------------------------------------
     // hashCode and equals
     // -------------------------------------------------------------------------
 
@@ -132,6 +194,4 @@
     {
         this.name = name;
     }
-
-
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataentryform/DataEntryFormService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataentryform/DataEntryFormService.java	2010-09-13 10:22:43 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataentryform/DataEntryFormService.java	2010-09-13 11:06:01 +0000
@@ -29,8 +29,10 @@
 
 import java.util.Collection;
 import java.util.List;
-import java.util.Map;
 
+/**
+ * @author Bharath Kumar
+ */
 public interface DataEntryFormService
 {
     String ID = DataEntryFormService.class.getName();
@@ -91,21 +93,7 @@
      */
     String prepareDataEntryFormCode( String preparedCode );
     
-    /**
-     * Map the identifiers inside the HTML code according to the provided data 
-     * element identifier and category option combo identifier mappings.
-     * 
-     * @param htmlCode the HTML code.
-     * @param dataElementMap the mapping between data element identifiers to be 
-     *        converted.
-     * @param categoryOptionComboMap the mapping between category option combo
-     *        identifiers to be converted.
-     * @return the converted HTML code.
-     */
-    String convertDataEntryForm( String htmlCode, Map<Object, Integer> dataElementMap, Map<Object, Integer> categoryOptionComboMap );
-    
     Collection<DataEntryForm> listDisctinctDataEntryFormByProgramStageIds( List<Integer> programStageIds );
     
-    Collection<DataEntryForm> listDisctinctDataEntryFormByDataSetIds( List<Integer> dataSetIds );
-    
+    Collection<DataEntryForm> listDisctinctDataEntryFormByDataSetIds( List<Integer> dataSetIds );    
 }

=== added directory 'dhis-2/dhis-api/src/test/java/org/hisp/dhis/dataentryform'
=== added file 'dhis-2/dhis-api/src/test/java/org/hisp/dhis/dataentryform/DataEntryFormTest.java'
--- dhis-2/dhis-api/src/test/java/org/hisp/dhis/dataentryform/DataEntryFormTest.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-api/src/test/java/org/hisp/dhis/dataentryform/DataEntryFormTest.java	2010-09-13 11:06:01 +0000
@@ -0,0 +1,85 @@
+package org.hisp.dhis.dataentryform;
+
+/*
+ * Copyright (c) 2004-2010, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright notice, this
+ *   list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ *   this list of conditions and the following disclaimer in the documentation
+ *   and/or other materials provided with the distribution.
+ * * Neither the name of the HISP project nor the names of its contributors may
+ *   be used to endorse or promote products derived from this software without
+ *   specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.fail;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.junit.Test;
+
+/**
+ * @author Lars Helge Overland
+ */
+public class DataEntryFormTest
+{
+    private String htmlCodeA = 
+        "<p><table><tr valign=\"top\">" +
+        "<td align=\"center\" width=\"8%\"><p><input title=\"\" view=\"@@deshortname@@\" value=\"\" name=\"entryfield\" id=\"value[1000].value:value[3].value\" style=\"width: 4em; text-align: center;\" /></p></td>" +
+        "<td align=\"center\" width=\"4%\"><p><input title=\"\" view=\"@@deshortname@@\" value=\"\" name=\"entryfield\" id=\"value[2000].value:value[4].value\" style=\"width: 4em; text-align: center;\" /></p></td>" +
+        "</tr></table></p>";
+
+    private String htmlCodeB = 
+        "<p><table><tr valign=\"top\">" +
+        "<td align=\"center\" width=\"8%\"><p><input title=\"\" view=\"@@deshortname@@\" value=\"\" name=\"entryfield\" id=\"value[1100].value:value[13].value\" style=\"width: 4em; text-align: center;\" /></p></td>" +
+        "<td align=\"center\" width=\"4%\"><p><input title=\"\" view=\"@@deshortname@@\" value=\"\" name=\"entryfield\" id=\"value[2100].value:value[14].value\" style=\"width: 4em; text-align: center;\" /></p></td>" +
+        "</tr></table></p>";
+
+    @Test
+    public void testConvertDataEntryForm()
+    {
+        Map<Object, Integer> dataElementMap = new HashMap<Object, Integer>();
+        Map<Object, Integer> categoryOptionComboMap = new HashMap<Object, Integer>();
+        
+        dataElementMap.put( 1000, 1100 );
+        dataElementMap.put( 2000, 2100 );
+        categoryOptionComboMap.put( 3, 13 );
+        categoryOptionComboMap.put( 4, 14 );
+        
+        DataEntryForm form = new DataEntryForm( "DataEntryFormA", htmlCodeA );
+        
+        form.convertDataEntryForm( dataElementMap, categoryOptionComboMap );
+        
+        assertEquals( htmlCodeB, form.getHtmlCode() );
+    }
+
+    @Test(expected=RuntimeException.class)
+    public void testConvertDataEntryFormInvalid()
+    {
+        Map<Object, Integer> dataElementMap = new HashMap<Object, Integer>();
+        Map<Object, Integer> categoryOptionComboMap = new HashMap<Object, Integer>();
+
+        DataEntryForm form = new DataEntryForm( "DataEntryFormA", htmlCodeA );
+        
+        form.convertDataEntryForm( dataElementMap, categoryOptionComboMap );
+        
+        fail();
+    }
+}

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataentryform/DefaultDataEntryFormService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataentryform/DefaultDataEntryFormService.java	2010-09-13 10:22:43 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataentryform/DefaultDataEntryFormService.java	2010-09-13 11:06:01 +0000
@@ -27,11 +27,8 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import static org.hisp.dhis.system.util.ExceptionUtils.throwException;
-
 import java.util.Collection;
 import java.util.List;
-import java.util.Map;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -45,9 +42,6 @@
 public class DefaultDataEntryFormService
     implements DataEntryFormService
 {
-    private static final Pattern INPUT_PATTERN = Pattern.compile( "value\\[\\d+\\]\\.value:value\\[\\d+\\]\\.value" );
-    private static final Pattern OPERAND_PATTERN = Pattern.compile( "\\d+" );
-    
     // ------------------------------------------------------------------------
     // Dependencies
     // ------------------------------------------------------------------------
@@ -168,36 +162,6 @@
         return sb.toString();
     }
     
-    public String convertDataEntryForm( String htmlCode, Map<Object, Integer> dataElementMap, Map<Object, Integer> categoryOptionComboMap )
-    {
-        Matcher inputMatcher = INPUT_PATTERN.matcher( htmlCode );        
-        StringBuffer buffer = new StringBuffer();
-        
-        while ( inputMatcher.find() )
-        {
-            String input = inputMatcher.group();
-            Matcher operandMatcher = OPERAND_PATTERN.matcher( input );
-            
-            operandMatcher.find();            
-            String d = operandMatcher.group();
-            throwException( d == null, "Could not find data element identifier in form" );
-            Integer dataElement = dataElementMap.get( Integer.parseInt( d ) );
-            throwException( dataElement == null, "Data element identifier does not exist: " + d );
-            
-            operandMatcher.find();
-            String c = operandMatcher.group();
-            throwException( c == null, "Could not find category option combo identifier in form" );
-            Integer categoryOptionCombo = categoryOptionComboMap.get( Integer.parseInt( c ) );
-            throwException( categoryOptionCombo == null, "Category option combo identifier does not exist: " + c );
-            
-            inputMatcher.appendReplacement( buffer, "value[" + dataElement + "].value:value[" + categoryOptionCombo + "].value" );
-        }
-        
-        inputMatcher.appendTail( buffer );
-        
-        return buffer.toString();
-    }
-
     public Collection<DataEntryForm> listDisctinctDataEntryFormByProgramStageIds( List<Integer> programStageIds )
     {
         if ( programStageIds == null || programStageIds.size() == 0 )

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataentryform/DataEntryFormServiceTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataentryform/DataEntryFormServiceTest.java	2010-09-13 10:22:43 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataentryform/DataEntryFormServiceTest.java	2010-09-13 11:06:01 +0000
@@ -31,16 +31,11 @@
 import static junit.framework.Assert.assertNotNull;
 import static junit.framework.Assert.assertNull;
 import static junit.framework.Assert.assertTrue;
-import static junit.framework.Assert.fail;
 
 import java.util.Collection;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
 
 import org.hisp.dhis.DhisSpringTest;
-import org.hisp.dhis.dataentryform.DataEntryForm;
-import org.hisp.dhis.dataentryform.DataEntryFormService;
 import org.hisp.dhis.dataset.DataSet;
 import org.hisp.dhis.dataset.DataSetService;
 import org.hisp.dhis.period.PeriodStore;
@@ -54,18 +49,6 @@
 public class DataEntryFormServiceTest
     extends DhisSpringTest
 {
-    private String htmlCodeA = 
-        "<p><table><tr valign=\"top\">" +
-        "<td align=\"center\" width=\"8%\"><p><input title=\"\" view=\"@@deshortname@@\" value=\"\" name=\"entryfield\" id=\"value[1000].value:value[3].value\" style=\"width: 4em; text-align: center;\" /></p></td>" +
-        "<td align=\"center\" width=\"4%\"><p><input title=\"\" view=\"@@deshortname@@\" value=\"\" name=\"entryfield\" id=\"value[2000].value:value[4].value\" style=\"width: 4em; text-align: center;\" /></p></td>" +
-        "</tr></table></p>";
-
-    private String htmlCodeB = 
-        "<p><table><tr valign=\"top\">" +
-        "<td align=\"center\" width=\"8%\"><p><input title=\"\" view=\"@@deshortname@@\" value=\"\" name=\"entryfield\" id=\"value[1100].value:value[13].value\" style=\"width: 4em; text-align: center;\" /></p></td>" +
-        "<td align=\"center\" width=\"4%\"><p><input title=\"\" view=\"@@deshortname@@\" value=\"\" name=\"entryfield\" id=\"value[2100].value:value[14].value\" style=\"width: 4em; text-align: center;\" /></p></td>" +
-        "</tr></table></p>";
-    
     private PeriodStore periodStore;
 
     private DataSetService dataSetService;
@@ -96,33 +79,6 @@
     // -------------------------------------------------------------------------
 
     @Test
-    public void testConvertDataEntryForm()
-    {
-        Map<Object, Integer> dataElementMap = new HashMap<Object, Integer>();
-        Map<Object, Integer> categoryOptionComboMap = new HashMap<Object, Integer>();
-        
-        dataElementMap.put( 1000, 1100 );
-        dataElementMap.put( 2000, 2100 );
-        categoryOptionComboMap.put( 3, 13 );
-        categoryOptionComboMap.put( 4, 14 );
-        
-        String code = dataEntryFormService.convertDataEntryForm( htmlCodeA, dataElementMap, categoryOptionComboMap );
-        
-        assertEquals( htmlCodeB, code );
-    }
-    
-    @Test(expected=RuntimeException.class)
-    public void testConvertDataEntryFormInvalid()
-    {
-        Map<Object, Integer> dataElementMap = new HashMap<Object, Integer>();
-        Map<Object, Integer> categoryOptionComboMap = new HashMap<Object, Integer>();
-        
-        dataEntryFormService.convertDataEntryForm( htmlCodeA, dataElementMap, categoryOptionComboMap );
-        
-        fail();
-    }
-    
-    @Test
     public void testAddDataEntryForm()
     {
         DataSet dataSetA = new DataSet( "DataSet-A", periodType );

=== modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ExceptionUtils.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ExceptionUtils.java	2010-09-10 09:54:49 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ExceptionUtils.java	2010-09-13 11:06:01 +0000
@@ -1,8 +1,38 @@
 package org.hisp.dhis.system.util;
 
+/*
+ * Copyright (c) 2004-2010, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright notice, this
+ *   list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ *   this list of conditions and the following disclaimer in the documentation
+ *   and/or other materials provided with the distribution.
+ * * Neither the name of the HISP project nor the names of its contributors may
+ *   be used to endorse or promote products derived from this software without
+ *   specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
+/**
+ * @author Lars Helge Overland
+ */
 public class ExceptionUtils
 {
     private static final Log log = LogFactory.getLog( ExceptionUtils.class );