← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 14060: Impl CSV import of category options

 

------------------------------------------------------------
revno: 14060
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2014-02-17 19:48:08 +0100
message:
  Impl CSV import of category options
modified:
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/startup/TableAlteror.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/utils/CsvObjectUtils.java
  dhis-2/dhis-web/dhis-web-importexport/src/main/java/org/hisp/dhis/importexport/action/dxf2/FilterExportFormAction.java
  dhis-2/dhis-web/dhis-web-importexport/src/main/java/org/hisp/dhis/importexport/action/dxf2/MetaDataImportAction.java
  dhis-2/dhis-web/dhis-web-importexport/src/main/java/org/hisp/dhis/importexport/action/util/ImportMetaDataCsvTask.java
  dhis-2/dhis-web/dhis-web-importexport/src/main/resources/org/hisp/dhis/importexport/i18n_module.properties
  dhis-2/dhis-web/dhis-web-importexport/src/main/webapp/dhis-web-importexport/dxf2MetaDataImport.vm


--
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-services/dhis-service-core/src/main/java/org/hisp/dhis/startup/TableAlteror.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/startup/TableAlteror.java	2014-01-29 11:02:07 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/startup/TableAlteror.java	2014-02-17 18:48:08 +0000
@@ -628,6 +628,9 @@
         executeSql( "UPDATE dataelementgroupset SET userid=NULL WHERE userid IS NOT NULL" );
         executeSql( "UPDATE dataelementgroupset SET publicaccess=NULL WHERE userid IS NOT NULL" );
 
+        executeSql( "ALTER TABLE dataelementcategory DROP COLUMN conceptid" );
+        executeSql( "ALTER TABLE dataelementcategoryoption DROP COLUMN conceptid" );
+        
         // upgrade system charts/maps to public read-only sharing
         executeSql( "UPDATE chart SET publicaccess='r-------' WHERE user IS NULL AND publicaccess IS NULL;" );
         executeSql( "UPDATE map SET publicaccess='r-------' WHERE user IS NULL AND publicaccess IS NULL;" );

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/utils/CsvObjectUtils.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/utils/CsvObjectUtils.java	2014-02-16 11:29:07 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/utils/CsvObjectUtils.java	2014-02-17 18:48:08 +0000
@@ -35,8 +35,11 @@
 import java.util.List;
 
 import org.apache.commons.lang.StringUtils;
+import org.hisp.dhis.common.BaseIdentifiableObject;
 import org.hisp.dhis.common.CodeGenerator;
+import org.hisp.dhis.dataelement.CategoryOptionGroup;
 import org.hisp.dhis.dataelement.DataElement;
+import org.hisp.dhis.dataelement.DataElementCategoryOption;
 import org.hisp.dhis.dxf2.metadata.MetaData;
 
 import com.csvreader.CsvReader;
@@ -49,45 +52,106 @@
     public static MetaData fromCsv( InputStream input, Class<?> clazz )
         throws IOException
     {
-        CsvReader reader = new CsvReader( input, Charset.forName( "UTF-8" ) );
-        
+        CsvReader reader = new CsvReader( input, Charset.forName( "UTF-8" ) );        
         reader.readRecord(); // Ignore first row
         
-        List<DataElement> dataElements = new ArrayList<DataElement>();
-        
-        while ( reader.readRecord() )
-        {
-            String[] values = reader.getValues();
-
-            if ( values == null || values.length == 0 )
-            {
-                continue;
-            }
-            
-            DataElement element = new DataElement();
-            element.setName( getSafe( values, 0, null ) );
-            element.setUid( getSafe( values, 1, CodeGenerator.generateCode() ) );
-            element.setCode( getSafe( values, 2, null ) );
-            element.setShortName( getSafe( values, 3, StringUtils.substring( element.getName(), 0, 50 ) ) );
-            element.setDescription( getSafe( values, 4, null ) );
-            element.setFormName( getSafe( values, 5, null ) );
-            element.setActive( Boolean.valueOf( getSafe( values, 6, "false" ) ) );
-            element.setDomainType( getSafe( values, 7, DataElement.DOMAIN_TYPE_AGGREGATE ) );
-            element.setType( getSafe( values, 8, DataElement.VALUE_TYPE_INT ) );
-            element.setNumberType( getSafe( values, 9, DataElement.VALUE_TYPE_NUMBER ) );
-            element.setTextType( getSafe( values, 10, null ) );
-            element.setAggregationOperator( getSafe( values, 11, DataElement.AGGREGATION_OPERATOR_SUM ) );
-            element.setUrl( getSafe( values, 12, null ) );
-            element.setZeroIsSignificant( Boolean.valueOf( getSafe( values, 13, "false" ) ) );
-            
-            dataElements.add( element );
-        }
-        
         MetaData metaData = new MetaData();
-        metaData.setDataElements( dataElements );
+        
+        if ( DataElement.class.equals( clazz ) )
+        {
+            metaData.setDataElements( dataElementsFromCsv( reader, input ) );
+        }
+        else if ( DataElementCategoryOption.class.equals( clazz ) )
+        {
+            metaData.setCategoryOptions( categoryOptionsFromCsv( reader, input ) );
+        }
+        else if ( CategoryOptionGroup.class.equals( clazz ) )
+        {
+            metaData.setCategoryOptionGroups( categoryOptionGroupsFromCsv( reader, input ) );
+        }
+        
         return metaData;
     }
     
+    private static List<DataElementCategoryOption> categoryOptionsFromCsv( CsvReader reader, InputStream input )
+        throws IOException
+    {
+        List<DataElementCategoryOption> list = new ArrayList<DataElementCategoryOption>();
+        
+        while ( reader.readRecord() )
+        {
+            String[] values = reader.getValues();
+
+            if ( values != null && values.length > 0 )
+            {
+                DataElementCategoryOption object = new DataElementCategoryOption();
+                setIdentifiableObject( object, values );
+                list.add( object );
+            }
+        }
+        
+        return list;
+    }    
+
+    private static List<CategoryOptionGroup> categoryOptionGroupsFromCsv( CsvReader reader, InputStream input )
+        throws IOException
+    {
+        List<CategoryOptionGroup> list = new ArrayList<CategoryOptionGroup>();
+        
+        while ( reader.readRecord() )
+        {
+            String[] values = reader.getValues();
+
+            if ( values != null && values.length > 0 )
+            {
+                CategoryOptionGroup object = new CategoryOptionGroup();
+                setIdentifiableObject( object, values );
+                list.add( object );
+            }
+        }
+        
+        return list;
+    }    
+    
+    private static List<DataElement> dataElementsFromCsv( CsvReader reader, InputStream input )
+        throws IOException
+    {
+        List<DataElement> list = new ArrayList<DataElement>();
+        
+        while ( reader.readRecord() )
+        {
+            String[] values = reader.getValues();
+
+            if ( values != null && values.length > 0 )
+            {
+                DataElement object = new DataElement();
+                setIdentifiableObject( object, values );
+                object.setShortName( getSafe( values, 3, StringUtils.substring( object.getName(), 0, 50 ) ) );
+                object.setDescription( getSafe( values, 4, null ) );
+                object.setFormName( getSafe( values, 5, null ) );
+                object.setActive( true );
+                object.setDomainType( getSafe( values, 6, DataElement.DOMAIN_TYPE_AGGREGATE ) );
+                object.setType( getSafe( values, 7, DataElement.VALUE_TYPE_INT ) );
+                object.setNumberType( getSafe( values, 8, DataElement.VALUE_TYPE_NUMBER ) );
+                object.setTextType( getSafe( values, 9, null ) );
+                object.setAggregationOperator( getSafe( values, 10, DataElement.AGGREGATION_OPERATOR_SUM ) );
+                object.setUrl( getSafe( values, 11, null ) );
+                object.setZeroIsSignificant( Boolean.valueOf( getSafe( values, 12, "false" ) ) );
+                
+                list.add( object );
+            }
+        }
+        
+        return list;
+    }
+
+    private static void setIdentifiableObject( BaseIdentifiableObject object, String[] values )
+    {
+        object.setName( getSafe( values, 0, null ) );
+        object.setUid( getSafe( values, 1, CodeGenerator.generateCode() ) );
+        object.setCode( getSafe( values, 2, null ) );
+    }
+    
     private static final String getSafe( String[] values, int index, String defaultValue )
     {
         if ( values == null || index < 0 || index >= values.length )

=== modified file 'dhis-2/dhis-web/dhis-web-importexport/src/main/java/org/hisp/dhis/importexport/action/dxf2/FilterExportFormAction.java'
--- dhis-2/dhis-web/dhis-web-importexport/src/main/java/org/hisp/dhis/importexport/action/dxf2/FilterExportFormAction.java	2013-10-31 10:29:22 +0000
+++ dhis-2/dhis-web/dhis-web-importexport/src/main/java/org/hisp/dhis/importexport/action/dxf2/FilterExportFormAction.java	2014-02-17 18:48:08 +0000
@@ -100,7 +100,7 @@
         this.command = command;
     }
 
-// -------------------------------------------------------------------------
+    // -------------------------------------------------------------------------
     // Output
     // -------------------------------------------------------------------------
 

=== modified file 'dhis-2/dhis-web/dhis-web-importexport/src/main/java/org/hisp/dhis/importexport/action/dxf2/MetaDataImportAction.java'
--- dhis-2/dhis-web/dhis-web-importexport/src/main/java/org/hisp/dhis/importexport/action/dxf2/MetaDataImportAction.java	2014-02-15 18:09:14 +0000
+++ dhis-2/dhis-web/dhis-web-importexport/src/main/java/org/hisp/dhis/importexport/action/dxf2/MetaDataImportAction.java	2014-02-17 18:48:08 +0000
@@ -29,6 +29,10 @@
  */
 
 import com.opensymphony.xwork2.Action;
+
+import org.hisp.dhis.dataelement.CategoryOptionGroup;
+import org.hisp.dhis.dataelement.DataElement;
+import org.hisp.dhis.dataelement.DataElementCategoryOption;
 import org.hisp.dhis.dxf2.metadata.ImportOptions;
 import org.hisp.dhis.dxf2.metadata.ImportService;
 import org.hisp.dhis.importexport.ImportStrategy;
@@ -46,6 +50,8 @@
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Map;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
@@ -53,6 +59,12 @@
 public class MetaDataImportAction
     implements Action
 {
+    private static final Map<String, Class<?>> KEY_CLASS_MAP = new HashMap<String, Class<?>>() {{
+       put( "dataelement", DataElement.class );
+       put( "categoryoption", DataElementCategoryOption.class );
+       put( "categoryoptiongroup", CategoryOptionGroup.class ); 
+    }};
+    
     // -------------------------------------------------------------------------
     // Dependencies
     // -------------------------------------------------------------------------
@@ -105,6 +117,13 @@
     {
         this.importFormat = importFormat;
     }
+    
+    private String classKey;
+
+    public void setClassKey( String classKey )
+    {
+        this.classKey = classKey;
+    }
 
     // -------------------------------------------------------------------------
     // Action Implementation
@@ -130,9 +149,9 @@
 
         String userId = user != null ? user.getUid() : null;
         
-        if ( "csv".equals( importFormat ) )
+        if ( "csv".equals( importFormat ) && classKey != null && KEY_CLASS_MAP.get( classKey ) != null )
         {
-            scheduler.executeTask( new ImportMetaDataCsvTask( userId, importService, importOptions, in, taskId ) );
+            scheduler.executeTask( new ImportMetaDataCsvTask( userId, importService, importOptions, in, taskId, KEY_CLASS_MAP.get( classKey ) ) );
         }
         else
         {

=== modified file 'dhis-2/dhis-web/dhis-web-importexport/src/main/java/org/hisp/dhis/importexport/action/util/ImportMetaDataCsvTask.java'
--- dhis-2/dhis-web/dhis-web-importexport/src/main/java/org/hisp/dhis/importexport/action/util/ImportMetaDataCsvTask.java	2014-02-15 18:09:14 +0000
+++ dhis-2/dhis-web/dhis-web-importexport/src/main/java/org/hisp/dhis/importexport/action/util/ImportMetaDataCsvTask.java	2014-02-17 18:48:08 +0000
@@ -33,7 +33,6 @@
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.dxf2.metadata.ImportOptions;
 import org.hisp.dhis.dxf2.metadata.ImportService;
 import org.hisp.dhis.dxf2.metadata.MetaData;
@@ -57,15 +56,18 @@
     private TaskId taskId;
 
     private String userUid;
+    
+    private Class<?> clazz;
 
     public ImportMetaDataCsvTask( String userUid, ImportService importService, ImportOptions importOptions, InputStream inputStream,
-        TaskId taskId )
+        TaskId taskId, Class<?> clazz )
     {
+        this.userUid = userUid;
         this.importService = importService;
         this.importOptions = importOptions;
         this.inputStream = inputStream;
         this.taskId = taskId;
-        this.userUid = userUid;
+        this.clazz = clazz;
     }
 
     @Override
@@ -75,7 +77,7 @@
         
         try
         {
-            metaData = CsvObjectUtils.fromCsv( inputStream, DataElement.class );
+            metaData = CsvObjectUtils.fromCsv( inputStream, clazz );
         }
         catch ( IOException ex )
         {

=== modified file 'dhis-2/dhis-web/dhis-web-importexport/src/main/resources/org/hisp/dhis/importexport/i18n_module.properties'
--- dhis-2/dhis-web/dhis-web-importexport/src/main/resources/org/hisp/dhis/importexport/i18n_module.properties	2014-02-16 11:29:07 +0000
+++ dhis-2/dhis-web/dhis-web-importexport/src/main/resources/org/hisp/dhis/importexport/i18n_module.properties	2014-02-17 18:48:08 +0000
@@ -32,6 +32,7 @@
 compare=Compare
 configuration=Configuration
 source=Source
+object_type=Object type
 del=Del
 up=Up
 specify_file=Please specify a file
@@ -73,6 +74,8 @@
 data_element_group=Data element group
 organisation_unit_group=Organisation unit group
 organisation_unit_group_set=Organisation unit group set
+category_option=Category option
+category_option_group=Category option group
 element=Element
 objects=Objects
 discard_selected=Discard selected
@@ -346,13 +349,10 @@
 value_is_zero_and_not_zero_significant=Value is zero and not zero-significant
 value_not_zero_or_positive_integer=Value is not a valid zero or positive integer
 comment_too_long=Comment is too long
-
 intro_filtered_metadata_export=Export filtered meta-data like data elements and organisation units to the standard DHIS 2 exchange format.
 filtered_metadata_export=Meta-Data Detailed Export
-
 apply=Apply
 sort=Sort
-
 add_new_filter=Add new Filter
 ad_hoc_export=Ad hoc export
 create_new_filter=Create new Filter
@@ -360,20 +360,15 @@
 edit_filter=Edit Filter
 filter_details=Filter Details
 validate_filter=Filter name cannot be empty.
-
 export_with_dependencies=Export with dependencies
-
 attribute_types=Attribute Types
 available_attribute_types=Available Attribute Types
 selected_attribute_types=Selected Attribute Types
-
 categories=Categories
 available_categories=Available Categories
 selected_categories=Selected Categories
-
 available_charts=Available Charts
 selected_charts=Selected Charts
-
 concepts=Concepts
 available_concepts=Available Concepts
 selected_concepts=Selected Concepts

=== modified file 'dhis-2/dhis-web/dhis-web-importexport/src/main/webapp/dhis-web-importexport/dxf2MetaDataImport.vm'
--- dhis-2/dhis-web/dhis-web-importexport/src/main/webapp/dhis-web-importexport/dxf2MetaDataImport.vm	2014-02-15 18:09:14 +0000
+++ dhis-2/dhis-web/dhis-web-importexport/src/main/webapp/dhis-web-importexport/dxf2MetaDataImport.vm	2014-02-17 18:48:08 +0000
@@ -10,6 +10,16 @@
 	<td>$i18n.getString( "file" )</td>
 	<td><input type="file" id="upload" name="upload" style="margin-left:0px"></td>
 </tr>
+#if ( "csv" == $importFormat )
+<tr>
+	<td>$i18n.getString( "object_type" )</td>
+	<td><select id="classKey" name="classKey" style="width:190px">
+		<option value="dataelement">$i18n.getString( "data_element" )</option>
+        <option value="categoryoption">$i18n.getString( "category_option" )</option>
+        <option value="categoryoptiongroup">$i18n.getString( "category_option_group" )</option>
+    </select></td>
+</tr>
+#end
 <tr>
 	<td>$i18n.getString( "dry_run" )</td>
 	<td><select id="dryRun" name="dryRun" style="width:190px">