← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 13060: CCEI: work in progress CSV Import functionality

 

------------------------------------------------------------
revno: 13060
committer: Bharath <chbharathk@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2013-11-29 05:29:50 +0000
message:
  CCEI: work in progress CSV Import functionality
modified:
  local/in/dhis-in-api/src/main/java/org/hisp/dhis/coldchain/equipment/EquipmentTypeAttributeService.java
  local/in/dhis-in-api/src/main/java/org/hisp/dhis/coldchain/equipment/EquipmentTypeAttributeStore.java
  local/in/dhis-in-services/dhis-in-service-coldchain/src/main/java/org/hisp/dhis/coldchain/equipment/DefaultEquipmentTypeAttributeService.java
  local/in/dhis-in-services/dhis-in-service-coldchain/src/main/java/org/hisp/dhis/coldchain/equipment/hibernate/HibernateEquipmentTypeAttributeStore.java
  local/in/dhis-web-maintenance-ccem/src/main/java/org/hisp/dhis/ccem/importexport/action/CSVImportAction.java
  local/in/dhis-web-maintenance-ccem/src/main/resources/META-INF/dhis/beans.xml


--
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 'local/in/dhis-in-api/src/main/java/org/hisp/dhis/coldchain/equipment/EquipmentTypeAttributeService.java'
--- local/in/dhis-in-api/src/main/java/org/hisp/dhis/coldchain/equipment/EquipmentTypeAttributeService.java	2013-09-05 11:11:36 +0000
+++ local/in/dhis-in-api/src/main/java/org/hisp/dhis/coldchain/equipment/EquipmentTypeAttributeService.java	2013-11-29 05:29:50 +0000
@@ -18,6 +18,8 @@
     
     EquipmentTypeAttribute getEquipmentTypeAttributeByName( String name );
     
+    EquipmentTypeAttribute getEquipmentTypeAttributeByDescription( String description );
+    
     //  methods
     
     int getEquipmentTypeAttributeCount();

=== modified file 'local/in/dhis-in-api/src/main/java/org/hisp/dhis/coldchain/equipment/EquipmentTypeAttributeStore.java'
--- local/in/dhis-in-api/src/main/java/org/hisp/dhis/coldchain/equipment/EquipmentTypeAttributeStore.java	2013-09-05 11:11:36 +0000
+++ local/in/dhis-in-api/src/main/java/org/hisp/dhis/coldchain/equipment/EquipmentTypeAttributeStore.java	2013-11-29 05:29:50 +0000
@@ -22,6 +22,8 @@
     
     EquipmentTypeAttribute getEquipmentTypeAttributeByName( String name );
     
+    EquipmentTypeAttribute getEquipmentTypeAttributeByDescription( String description );
+    
     //Collection<EquipmentTypeAttribute> getAllEquipmentTypeAttributesForDisplay( EquipmentTypeAttribute equipmentTypeAttribute );
     
     

=== modified file 'local/in/dhis-in-services/dhis-in-service-coldchain/src/main/java/org/hisp/dhis/coldchain/equipment/DefaultEquipmentTypeAttributeService.java'
--- local/in/dhis-in-services/dhis-in-service-coldchain/src/main/java/org/hisp/dhis/coldchain/equipment/DefaultEquipmentTypeAttributeService.java	2013-09-05 11:11:36 +0000
+++ local/in/dhis-in-services/dhis-in-service-coldchain/src/main/java/org/hisp/dhis/coldchain/equipment/DefaultEquipmentTypeAttributeService.java	2013-11-29 05:29:50 +0000
@@ -6,6 +6,7 @@
 
 import java.util.Collection;
 
+import org.hibernate.criterion.Restrictions;
 import org.hisp.dhis.coldchain.equipment.EquipmentTypeAttribute;
 import org.hisp.dhis.coldchain.equipment.EquipmentTypeAttributeService;
 import org.hisp.dhis.coldchain.equipment.EquipmentTypeAttributeStore;
@@ -111,6 +112,11 @@
         return equipmentTypeAttributeStore.getEquipmentTypeAttributeByName( name );
     }
 
+    public EquipmentTypeAttribute getEquipmentTypeAttributeByDescription( String description )
+    {
+        return equipmentTypeAttributeStore.getEquipmentTypeAttributeByDescription( description );
+    }
+
     
     //Methods
     public int getEquipmentTypeAttributeCount()

=== modified file 'local/in/dhis-in-services/dhis-in-service-coldchain/src/main/java/org/hisp/dhis/coldchain/equipment/hibernate/HibernateEquipmentTypeAttributeStore.java'
--- local/in/dhis-in-services/dhis-in-service-coldchain/src/main/java/org/hisp/dhis/coldchain/equipment/hibernate/HibernateEquipmentTypeAttributeStore.java	2013-09-05 11:11:36 +0000
+++ local/in/dhis-in-services/dhis-in-service-coldchain/src/main/java/org/hisp/dhis/coldchain/equipment/hibernate/HibernateEquipmentTypeAttributeStore.java	2013-11-29 05:29:50 +0000
@@ -93,8 +93,13 @@
     public EquipmentTypeAttribute getEquipmentTypeAttributeByName( String name )
     {
         return (EquipmentTypeAttribute) getCriteria( Restrictions.eq( "name", name ) ).uniqueResult();
-        
-    }
+    }
+
+    public EquipmentTypeAttribute getEquipmentTypeAttributeByDescription( String description )
+    {
+        return (EquipmentTypeAttribute) getCriteria( Restrictions.eq( "description", description ) ).uniqueResult();
+    }
+
     /*
     @SuppressWarnings( "unchecked" )
     public Collection<EquipmentTypeAttribute> getAllEquipmentTypeAttributesForDisplay( EquipmentTypeAttribute equipmentTypeAttribute )

=== modified file 'local/in/dhis-web-maintenance-ccem/src/main/java/org/hisp/dhis/ccem/importexport/action/CSVImportAction.java'
--- local/in/dhis-web-maintenance-ccem/src/main/java/org/hisp/dhis/ccem/importexport/action/CSVImportAction.java	2013-11-28 12:35:18 +0000
+++ local/in/dhis-web-maintenance-ccem/src/main/java/org/hisp/dhis/ccem/importexport/action/CSVImportAction.java	2013-11-29 05:29:50 +0000
@@ -15,6 +15,14 @@
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipInputStream;
 
+import org.apache.commons.lang.math.NumberUtils;
+import org.hisp.dhis.coldchain.equipment.Equipment;
+import org.hisp.dhis.coldchain.equipment.EquipmentAttributeValue;
+import org.hisp.dhis.coldchain.equipment.EquipmentService;
+import org.hisp.dhis.coldchain.equipment.EquipmentType;
+import org.hisp.dhis.coldchain.equipment.EquipmentTypeAttribute;
+import org.hisp.dhis.coldchain.equipment.EquipmentTypeAttributeService;
+import org.hisp.dhis.coldchain.equipment.EquipmentTypeService;
 import org.hisp.dhis.coldchain.model.Model;
 import org.hisp.dhis.coldchain.model.ModelAttributeValue;
 import org.hisp.dhis.coldchain.model.ModelService;
@@ -23,6 +31,8 @@
 import org.hisp.dhis.coldchain.model.ModelTypeAttributeService;
 import org.hisp.dhis.coldchain.model.ModelTypeService;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.organisationunit.OrganisationUnitGroup;
+import org.hisp.dhis.organisationunit.OrganisationUnitGroupService;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
 import org.hisp.dhis.user.CurrentUserService;
 import org.springframework.jdbc.core.JdbcTemplate;
@@ -73,7 +83,35 @@
         this.modelTypeService = modelTypeService;
     }
     
-    private JdbcTemplate jdbcTemplate;
+    private EquipmentTypeService equipmentTypeService;
+    
+    public void setEquipmentTypeService( EquipmentTypeService equipmentTypeService )
+    {
+        this.equipmentTypeService = equipmentTypeService;
+    }
+
+    private EquipmentService equipmentService;
+
+    public void setEquipmentService( EquipmentService equipmentService )
+    {
+        this.equipmentService = equipmentService;
+    }
+
+    private EquipmentTypeAttributeService equipmentTypeAttributeService;
+    
+    public void setEquipmentTypeAttributeService( EquipmentTypeAttributeService equipmentTypeAttributeService) 
+    {
+		this.equipmentTypeAttributeService = equipmentTypeAttributeService;
+	}
+
+    private OrganisationUnitGroupService organisationUnitGroupService;
+
+    public void setOrganisationUnitGroupService( OrganisationUnitGroupService organisationUnitGroupService )
+    {
+        this.organisationUnitGroupService = organisationUnitGroupService;
+    }
+
+	private JdbcTemplate jdbcTemplate;
 
     public void setJdbcTemplate( JdbcTemplate jdbcTemplate )
     {
@@ -135,11 +173,15 @@
              */
             ModelType refrigeratorModel = modelTypeService.getModelTypeByName( "Refrigerator Catalog" );
             
-            Map<Integer, OrganisationUnit> orgUnitMap = importAdminHierarchy( uncompressedFolderPath );
-            
-            Map<Integer, OrganisationUnit> faclityMap = importFacility( uncompressedFolderPath, orgUnitMap );
-            
-            //importRefrigeratorCatalogData( uncompressedFolderPath, refrigeratorModel, lookupDataMap );
+            EquipmentType refrigeratorEquipment = equipmentTypeService.getEquipmentTypeByName( "Refrigerators" );
+            
+            Map<String, OrganisationUnit> orgUnitMap = importAdminHierarchy( uncompressedFolderPath );
+            
+            Map<String, OrganisationUnit> faclityMap = importFacility( uncompressedFolderPath, orgUnitMap );
+            
+            Map<String, Model> refrigeratorModelMap = importRefrigeratorCatalogData( uncompressedFolderPath, refrigeratorModel, lookupDataMap );
+            
+            importRefrigetaorDetails( uncompressedFolderPath, refrigeratorEquipment, assetMap, faclityMap, refrigeratorModelMap, lookupDataMap );
             
             /*
             for( String lookupKey : lookupDataMap.keySet() )
@@ -165,10 +207,17 @@
     }
     
     
-    public Map<Integer, OrganisationUnit> importFacility( String facilityCSVFilePath, Map<Integer, OrganisationUnit> orgUnitMap )
+    public Map<String, OrganisationUnit> importFacility( String facilityCSVFilePath, Map<String, OrganisationUnit> orgUnitMap )
     {
         facilityCSVFilePath += File.separator + "Facility.csv";
         
+        List<OrganisationUnitGroup> ouGroups = new ArrayList<OrganisationUnitGroup>( organisationUnitGroupService.getOrganisationUnitGroupByName( EquipmentAttributeValue.HEALTHFACILITY ) ); 
+        OrganisationUnitGroup ouGroup = null;
+        if( ouGroups != null )
+        {
+        	ouGroup = ouGroups.get( 0 );
+        }
+
         try
         {
             CsvReader csvReader = new CsvReader( facilityCSVFilePath, ',', Charset.forName( "UTF-8" ) );
@@ -181,19 +230,21 @@
                 String nodeId = csvReader.get( "FacilityID" );
                 String ouName = csvReader.get( "FacilityName" );
                 //Integer ouLevel = Integer.parseInt( csvReader.get( "Level" ) );                         
-                Integer parentId = Integer.parseInt(  csvReader.get( "AdminRegion" ) );
+                String parentId = csvReader.get( "AdminRegion" );
                 //String ouCode = csvReader.get( "Code" );
                 
                 //if( ouCode != null && ouCode.trim().equals("") )
                 //        ouCode = null;
                 
-                OrganisationUnit organisationUnit = new OrganisationUnit( ouName, ouName, null, new Date(), null, true, parentId+"" );
-                if( parentId != -1 )
+                OrganisationUnit organisationUnit = new OrganisationUnit( ouName, ouName, null, new Date(), null, true, parentId );
+                if( parentId != null && Integer.parseInt( parentId ) != -1 )
                 {
                     OrganisationUnit parentOU = orgUnitMap.get( parentId );
                     organisationUnit.setParent( parentOU );
                     int orgUnitId = organisationUnitService.addOrganisationUnit( organisationUnit );
-                    orgUnitMap.put( Integer.parseInt( nodeId ), organisationUnit );
+                    orgUnitMap.put( nodeId, organisationUnit );
+                    ouGroup.addOrganisationUnit( organisationUnit );
+                    organisationUnitGroupService.updateOrganisationUnitGroup( ouGroup );
                 }
             }
                 
@@ -208,12 +259,12 @@
     }
 
 
-    public Map<Integer, OrganisationUnit> importAdminHierarchy( String adminHierarchyCSVFilePath )
+    public Map<String, OrganisationUnit> importAdminHierarchy( String adminHierarchyCSVFilePath )
     {
     	adminHierarchyCSVFilePath += File.separator + "AdminHierarchy.csv";
     	
     	Map<Integer, List<OrganisationUnit>> levelwiseOrgunits = new HashMap<Integer, List<OrganisationUnit>>();
-    	Map<Integer, OrganisationUnit> orgUnitMap = new HashMap<Integer, OrganisationUnit>();
+    	Map<String, OrganisationUnit> orgUnitMap = new HashMap<String, OrganisationUnit>();
     	
     	List<Integer> ouLevels = new ArrayList<Integer>();
     	
@@ -243,7 +294,7 @@
                 
                 OrganisationUnit organisationUnit = new OrganisationUnit( ouName, ouName, ouCode, new Date(), null, true, parentId );
                 organisationUnit.setDescription( nodeId );
-                orgUnitMap.put( Integer.parseInt(nodeId), organisationUnit );
+                orgUnitMap.put( nodeId, organisationUnit );
                 
                 //tempList.add( nodeId + "#@#" + ouName + "#@#" + parentId + "#@#" + ouCode );
                 //tempList.add( ouName );
@@ -265,22 +316,22 @@
             	List<OrganisationUnit> orgUnits = levelwiseOrgunits.get( ouLevel );
             	for( OrganisationUnit ou : orgUnits )
             	{
-            		Integer parentId = Integer.parseInt( ou.getComment() );
+            		String parentId = ou.getComment();
             		String nodeId = ou.getDescription();
-            		ou.setComment( null );
-            		ou.setAlternativeName( null );
-            		if( parentId == -1 )
+            		//ou.setComment( null );
+            		//ou.setAlternativeName( null );
+            		if( parentId == null || Integer.parseInt( parentId ) == -1 )
             		{
             			parentId = null;
             			int orgUnitId = organisationUnitService.addOrganisationUnit( ou );
-            			orgUnitMap.put( Integer.parseInt( nodeId ), ou );
+            			orgUnitMap.put( nodeId, ou );
             		}
             		else
             		{
             			OrganisationUnit parentOU = orgUnitMap.get( parentId );
             			ou.setParent( parentOU );
             			int orgUnitId = organisationUnitService.addOrganisationUnit( ou );
-            			orgUnitMap.put( Integer.parseInt( nodeId ), ou );
+            			orgUnitMap.put( nodeId, ou );
             		}
             	}
             }
@@ -293,10 +344,114 @@
     	return orgUnitMap;
     }
     
-    public void importRefrigeratorCatalogData( String refrigeratorCatalogDataCSVFilePath, ModelType refrigeratorModel, Map<String, List<String>> lookupDataMap )
+    public void importRefrigetaorDetails( String refrigeratorDetailsCSVFilePath, EquipmentType refrigeratorEquipment, Map<Integer, List<String>> assetMap, Map<String, OrganisationUnit> faclityMap, Map<String, Model> refrigeratorModelMap, Map<String, List<String>> lookupDataMap )
+    {
+    	refrigeratorDetailsCSVFilePath += File.separator + "Refrigerators.csv";
+    	
+        try
+        {
+            CsvReader csvReader = new CsvReader( refrigeratorDetailsCSVFilePath, ',', Charset.forName( "UTF-8" ) );
+            
+            csvReader.readHeaders();
+
+            Map<String, EquipmentTypeAttribute> equipmentTypeAttributeMap = new HashMap<String, EquipmentTypeAttribute>();
+            String headers[] = csvReader.getHeaders();            
+            for( int i = 2; i < headers.length; i++ )
+            {
+            	EquipmentTypeAttribute equipmentTypeAttribute = equipmentTypeAttributeService.getEquipmentTypeAttributeByDescription( headers[i] );
+            	equipmentTypeAttributeMap.put( headers[i], equipmentTypeAttribute );
+            }
+
+            while( csvReader.readRecord() )
+            {
+            	
+            	String uniqueId = csvReader.get( "UniqueID" );
+            	String catalogID = csvReader.get( "ModelID" );
+            	
+            	List<String> tempList = assetMap.get( Integer.parseInt( uniqueId ) );
+            	
+            	if( tempList == null )
+            	{
+            		System.out.println( "tempList is null for : " + uniqueId );
+            		continue;
+            	}
+            	OrganisationUnit orgUnit = faclityMap.get( tempList.get(0) );
+            	
+            	Equipment equipment = new Equipment();
+	            
+	            equipment.setEquipmentType( refrigeratorEquipment );
+	            equipment.setOrganisationUnit( orgUnit );
+	            
+	            Model model = refrigeratorModelMap.get( catalogID );
+	            if( model == null )
+	            {
+	            	System.out.println( "model is null for : " + catalogID );
+	            	continue;
+	            }
+
+	            List<EquipmentAttributeValue> equipmentAttributeValueDetailsList = new ArrayList<EquipmentAttributeValue>();
+
+	            for( int i = 2; i < headers.length; i++ )
+                {
+	            	EquipmentTypeAttribute equipmentTypeAttribute = equipmentTypeAttributeMap.get( headers[i] );
+                    
+                    if ( equipmentTypeAttribute != null )
+                    {
+                    	EquipmentAttributeValue equipmentAttributeValueDetails = new EquipmentAttributeValue();
+                        equipmentAttributeValueDetails.setEquipment( equipment );
+                        equipmentAttributeValueDetails.setEquipmentTypeAttribute( equipmentTypeAttribute );
+                        
+                        if( equipmentTypeAttribute.getOptionSet() != null )
+                        {
+                        	List<String> lookupOptions = lookupDataMap.get( headers[i] );
+                        	if( lookupOptions != null )
+                        	{
+                        		try
+                        		{
+                        			equipmentAttributeValueDetails.setValue( lookupOptions.get( Integer.parseInt( csvReader.get( headers[i] ) ) + 1  ) );
+                        			equipmentAttributeValueDetailsList.add( equipmentAttributeValueDetails );
+                        		}
+                        		catch( Exception e )
+                        		{
+                        			
+                        		}
+                        	}
+                        	else
+                        	{
+                        		
+                        	}
+                        }
+                        else
+                        {
+                            equipmentAttributeValueDetails.setValue( csvReader.get( headers[i] ) );
+                            equipmentAttributeValueDetailsList.add( equipmentAttributeValueDetails );
+                        }
+                    }
+	            	
+                }
+	            
+	            // -----------------------------------------------------------------------------
+	            // Creating EquipmentAttributeValue Instance and saving equipmentAttributeValue data
+	            // -----------------------------------------------------------------------------
+	            Integer id = equipmentService.createEquipment( equipment, equipmentAttributeValueDetailsList );
+
+            }
+            
+            csvReader.close();
+
+        }
+		catch( Exception e )
+		{
+		    e.printStackTrace();
+		}
+
+    }
+    
+    public Map<String, Model> importRefrigeratorCatalogData( String refrigeratorCatalogDataCSVFilePath, ModelType refrigeratorModel, Map<String, List<String>> lookupDataMap )
     {
         refrigeratorCatalogDataCSVFilePath += File.separator + "RefrigeratorCatalog.csv";
         
+        Map<String, Model> refrigeratorModelMap = new HashMap<String, Model>();
         try
         {
             CsvReader csvReader = new CsvReader( refrigeratorCatalogDataCSVFilePath, ',', Charset.forName( "UTF-8" ) );
@@ -330,6 +485,11 @@
                 String freezeNetVolume = csvReader.get( "FreezeNetVolume" );
                 */
                 
+                if( catalogId == null || catalogId.trim().equals("") || modelName == null || modelName.trim().equals("") )
+                {
+                	continue;
+                }
+                
                 Model model = new Model();
                 model.setName( catalogId + " + " + modelName );
                 model.setDescription( catalogId + " + " + modelName );
@@ -379,14 +539,20 @@
                 // Save model
                 // -------------------------------------------------------------------------                    
                 modelService.createModel(  model, modelAttributeValues );
+                
+                refrigeratorModelMap.put( catalogId, model );
 
-            }                                                    
+            }
+            
+            csvReader.close();
 
         }
         catch( Exception e )
         {
             e.printStackTrace();
         }
+        
+        return refrigeratorModelMap;
     }
 
     public Map<Integer, List<String>> getAssetData( String assetListCSVFilePath )

=== modified file 'local/in/dhis-web-maintenance-ccem/src/main/resources/META-INF/dhis/beans.xml'
--- local/in/dhis-web-maintenance-ccem/src/main/resources/META-INF/dhis/beans.xml	2013-11-27 19:15:40 +0000
+++ local/in/dhis-web-maintenance-ccem/src/main/resources/META-INF/dhis/beans.xml	2013-11-29 05:29:50 +0000
@@ -750,6 +750,10 @@
         <property name="modelService" ref="org.hisp.dhis.coldchain.model.ModelService" />
         <property name="modelTypeService" ref="org.hisp.dhis.coldchain.model.ModelTypeService" />
         <property name="organisationUnitService" ref="org.hisp.dhis.organisationunit.OrganisationUnitService" />
+        <property name="organisationUnitGroupService" ref="org.hisp.dhis.organisationunit.OrganisationUnitGroupService" />
+        <property name="equipmentTypeService" ref="org.hisp.dhis.coldchain.equipment.EquipmentTypeService" />
+        <property name="equipmentService" ref="org.hisp.dhis.coldchain.equipment.EquipmentService" />
+        <property name="equipmentTypeAttributeService" ref="org.hisp.dhis.coldchain.equipment.EquipmentTypeAttributeService" />
     </bean>