← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 6436: Data value import now returns an object serialized to xml containing a summary of the import proc...

 

------------------------------------------------------------
revno: 6436
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2012-03-29 11:59:05 +0200
message:
  Data value import now returns an object serialized to xml containing a summary of the import process, in terms of how many objects were updated/imported and potential conflicts like non-exisisting identifiers
added:
  dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/importsummary/
  dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/importsummary/ImportConflict.java
  dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/importsummary/ImportCount.java
  dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/importsummary/ImportSummary.java
modified:
  dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalue/DataValueService.java
  dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalue/DefaultDataValueService.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DataValueController.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-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalue/DataValueService.java'
--- dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalue/DataValueService.java	2012-03-29 08:52:54 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalue/DataValueService.java	2012-03-29 09:59:05 +0000
@@ -28,9 +28,10 @@
  */
 
 import org.hisp.dhis.common.IdentifiableObject.IdentifiableProperty;
+import org.hisp.dhis.dxf2.importsummary.ImportSummary;
 import org.hisp.dhis.importexport.ImportStrategy;
 
 public interface DataValueService
 {
-    void saveDataValues( DataValues dataValues, IdentifiableProperty idScheme, boolean dryRun, ImportStrategy strategy );
+    ImportSummary saveDataValues( DataValues dataValues, IdentifiableProperty idScheme, boolean dryRun, ImportStrategy strategy );
 }

=== modified file 'dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalue/DefaultDataValueService.java'
--- dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalue/DefaultDataValueService.java	2012-03-29 08:52:54 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalue/DefaultDataValueService.java	2012-03-29 09:59:05 +0000
@@ -40,6 +40,9 @@
 import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
 import org.hisp.dhis.dataelement.DataElementCategoryService;
 import org.hisp.dhis.datavalue.DataValue;
+import org.hisp.dhis.dxf2.importsummary.ImportConflict;
+import org.hisp.dhis.dxf2.importsummary.ImportCount;
+import org.hisp.dhis.dxf2.importsummary.ImportSummary;
 import org.hisp.dhis.importexport.ImportStrategy;
 import org.hisp.dhis.jdbc.batchhandler.DataValueBatchHandler;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
@@ -65,8 +68,10 @@
     private BatchHandlerFactory batchHandlerFactory;
     
     @Transactional
-    public void saveDataValues( DataValues dataValues, IdentifiableProperty idScheme, boolean dryRun, ImportStrategy strategy )
+    public ImportSummary saveDataValues( DataValues dataValues, IdentifiableProperty idScheme, boolean dryRun, ImportStrategy strategy )
     {
+        ImportSummary summary = new ImportSummary();
+        
         Map<String, DataElement> dataElementMap = identifiableObjectManager.getIdMap( DataElement.class, idScheme );
         Map<String, OrganisationUnit> orgUnitMap = identifiableObjectManager.getIdMap( OrganisationUnit.class, idScheme );
         Map<String, DataElementCategoryOptionCombo> categoryOptionComboMap = identifiableObjectManager.getIdMap( DataElementCategoryOptionCombo.class, IdentifiableProperty.UID );
@@ -74,6 +79,9 @@
         DataElementCategoryOptionCombo fallbackCategoryOptionCombo = categoryService.getDefaultDataElementCategoryOptionCombo();
 
         BatchHandler<DataValue> batchHandler = batchHandlerFactory.createBatchHandler( DataValueBatchHandler.class ).init();
+
+        int importCount = 0;
+        int updateCount = 0;                
         
         for ( org.hisp.dhis.dxf2.datavalue.DataValue dataValue : dataValues.getDataValues() )
         {
@@ -86,16 +94,19 @@
             
             if ( dataElement == null )
             {
+                summary.getNoneExistingIdentifiers().add( new ImportConflict( DataElement.class.getSimpleName(), dataValue.getDataElement() ) );
                 continue;
             }
             
             if ( orgUnit == null )
             {
+                summary.getNoneExistingIdentifiers().add( new ImportConflict( OrganisationUnit.class.getSimpleName(), dataValue.getOrgUnit() ) );
                 continue;
             }
 
             if ( period == null )
             {
+                summary.getNoneExistingIdentifiers().add( new ImportConflict( Period.class.getSimpleName(), dataValue.getPeriod() ) );
                 continue;
             }
             
@@ -116,20 +127,34 @@
             
             if ( batchHandler.objectExists( internalValue ) )
             {
-                if ( !dryRun && ( NEW_AND_UPDATES.equals( strategy ) || UPDATES.equals( strategy ) ) )
+                if ( NEW_AND_UPDATES.equals( strategy ) || UPDATES.equals( strategy ) )
                 {
-                    batchHandler.updateObject( internalValue );
+                    if ( !dryRun )
+                    {
+                        batchHandler.updateObject( internalValue );
+                    }
+
+                    updateCount++;
                 }
             }
             else
             {
-                if ( !dryRun && ( NEW_AND_UPDATES.equals( strategy ) || NEW.equals( strategy ) ) )
+                if ( NEW_AND_UPDATES.equals( strategy ) || NEW.equals( strategy ) )
                 {
-                    batchHandler.addObject( internalValue );
+                    if ( !dryRun )
+                    {
+                        batchHandler.addObject( internalValue );
+                    }
+                    
+                    importCount++;
                 }
             }
         }
         
+        summary.getCounts().add( new ImportCount( DataValue.class.getSimpleName(), importCount, updateCount ) );
+        
         batchHandler.flush();
+        
+        return summary;
     }
 }

=== added directory 'dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/importsummary'
=== added file 'dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/importsummary/ImportConflict.java'
--- dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/importsummary/ImportConflict.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/importsummary/ImportConflict.java	2012-03-29 09:59:05 +0000
@@ -0,0 +1,70 @@
+package org.hisp.dhis.dxf2.importsummary;
+
+/*
+ * Copyright (c) 2011, 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 com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
+
+@JacksonXmlRootElement( localName = "conflict" )
+public class ImportConflict
+{
+    private String object;
+    
+    private String value;
+
+    public ImportConflict( String object, String value )
+    {
+        this.object = object;
+        this.value = value;
+    }
+    
+    @JsonProperty
+    @JacksonXmlProperty( isAttribute=true )
+    public String getObject()
+    {
+        return object;
+    }
+
+    public void setObject( String object )
+    {
+        this.object = object;
+    }
+
+    @JsonProperty
+    @JacksonXmlProperty( isAttribute=true )
+    public String getValue()
+    {
+        return value;
+    }
+
+    public void setValue( String value )
+    {
+        this.value = value;
+    }
+}

=== added file 'dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/importsummary/ImportCount.java'
--- dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/importsummary/ImportCount.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/importsummary/ImportCount.java	2012-03-29 09:59:05 +0000
@@ -0,0 +1,85 @@
+package org.hisp.dhis.dxf2.importsummary;
+
+/*
+ * Copyright (c) 2011, 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 com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
+
+@JacksonXmlRootElement( localName = "count" )
+public class ImportCount
+{
+    private String object;
+    
+    private int imports;
+    
+    private int updates;
+
+    public ImportCount( String object, int imports, int updates )
+    {
+        this.object = object;
+        this.imports = imports;
+        this.updates = updates;
+    }
+    
+    @JsonProperty
+    @JacksonXmlProperty( isAttribute=true )
+    public String getObject()
+    {
+        return object;
+    }
+
+    public void setObject( String object )
+    {
+        this.object = object;
+    }
+
+    @JsonProperty
+    @JacksonXmlProperty( isAttribute=true )
+    public int getImports()
+    {
+        return imports;
+    }
+
+    public void setImports( int imports )
+    {
+        this.imports = imports;
+    }
+
+    @JsonProperty
+    @JacksonXmlProperty( isAttribute=true )
+    public int getUpdates()
+    {
+        return updates;
+    }
+
+    public void setUpdates( int updates )
+    {
+        this.updates = updates;
+    }
+}

=== added file 'dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/importsummary/ImportSummary.java'
--- dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/importsummary/ImportSummary.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/importsummary/ImportSummary.java	2012-03-29 09:59:05 +0000
@@ -0,0 +1,74 @@
+package org.hisp.dhis.dxf2.importsummary;
+
+/*
+ * Copyright (c) 2011, 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 java.util.ArrayList;
+import java.util.List;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
+
+@JacksonXmlRootElement( localName = "importSummary" )
+public class ImportSummary
+{
+    private List<ImportCount> counts = new ArrayList<ImportCount>();
+
+    private List<ImportConflict> noneExistingIdentifiers = new ArrayList<ImportConflict>();
+    
+    public void increaseImportCount( Class<?> clazz )
+    {
+    }
+    
+    @JsonProperty
+    @JacksonXmlElementWrapper( localName = "importCounts" )
+    @JacksonXmlProperty( localName = "count" )
+    public List<ImportCount> getCounts()
+    {
+        return counts;
+    }
+
+    public void setCounts( List<ImportCount> counts )
+    {
+        this.counts = counts;
+    }
+
+    @JsonProperty
+    @JacksonXmlElementWrapper( localName = "noneExistingIdentifiers" )
+    @JacksonXmlProperty( localName = "conflict" )
+    public List<ImportConflict> getNoneExistingIdentifiers()
+    {
+        return noneExistingIdentifiers;
+    }
+
+    public void setNoneExistingIdentifiers( List<ImportConflict> noneExistingIdentifiers )
+    {
+        this.noneExistingIdentifiers = noneExistingIdentifiers;
+    }
+}

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DataValueController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DataValueController.java	2012-03-29 08:52:54 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DataValueController.java	2012-03-29 09:59:05 +0000
@@ -32,25 +32,31 @@
 
 import javax.servlet.http.HttpServletResponse;
 
-import org.hisp.dhis.api.utils.ContextUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.hisp.dhis.common.IdentifiableObject.IdentifiableProperty;
 import org.hisp.dhis.dxf2.datavalue.DataValueService;
 import org.hisp.dhis.dxf2.datavalue.DataValues;
+import org.hisp.dhis.dxf2.importsummary.ImportSummary;
 import org.hisp.dhis.dxf2.utils.JacksonUtils;
 import org.hisp.dhis.importexport.ImportStrategy;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
 
-import static org.hisp.dhis.common.IdentifiableObject.*;
+import static org.hisp.dhis.api.utils.ContextUtils.*;
 
 @Controller
 @RequestMapping( value = DataValueController.RESOURCE_PATH )
 public class DataValueController
 {
     public static final String RESOURCE_PATH = "/dataValues";
+    
+    private static final Log log = LogFactory.getLog( DataValueController.class );
 
     @Autowired
     private DataValueService dataValueService;
@@ -61,17 +67,20 @@
                                 @RequestParam(required=false) boolean dryRun,
                                 @RequestParam(required=false, defaultValue="NEW_AND_UPDATES") String strategy,
                                 HttpServletResponse response, 
-                                InputStream input )
+                                InputStream input,
+                                Model model )
         throws IOException
     {
-        IdentifiableProperty _idScheme = IdentifiableProperty.valueOf( idScheme );
-        
+        IdentifiableProperty _idScheme = IdentifiableProperty.valueOf( idScheme );        
         ImportStrategy _strategy = ImportStrategy.valueOf( strategy );
         
         DataValues dataValues = JacksonUtils.fromXml( input, DataValues.class );
         
-        dataValueService.saveDataValues( dataValues, _idScheme, dryRun, _strategy );
-
-        ContextUtils.okResponse( response, "Data values saved using id scheme: " + _idScheme + ", dry run: " + dryRun + ", strategy: " + _strategy );
+        ImportSummary summary = dataValueService.saveDataValues( dataValues, _idScheme, dryRun, _strategy );
+            
+        log.info( "Data values saved using id scheme: " + _idScheme + ", dry run: " + dryRun + ", strategy: " + _strategy );    
+        
+        response.setContentType( CONTENT_TYPE_XML );        
+        JacksonUtils.toXml( response.getOutputStream(), summary );
     }
 }