← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 6547: Merged the solutions for import options

 

------------------------------------------------------------
revno: 6547
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2012-04-11 18:18:25 +0200
message:
  Merged the solutions for import options
removed:
  dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/ImportStrategy.java
modified:
  dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DataValueSetService.java
  dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DefaultDataValueSetService.java
  dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/ImportOptions.java
  dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/AbstractImporter.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DataValueSetController.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/datavalueset/DataValueSetService.java'
--- dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DataValueSetService.java	2012-04-01 12:17:55 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DataValueSetService.java	2012-04-11 16:18:25 +0000
@@ -30,9 +30,8 @@
 import java.io.InputStream;
 import java.io.OutputStream;
 
-import org.hisp.dhis.common.IdentifiableObject.IdentifiableProperty;
 import org.hisp.dhis.dxf2.importsummary.ImportSummary;
-import org.hisp.dhis.importexport.ImportStrategy;
+import org.hisp.dhis.dxf2.metadata.ImportOptions;
 
 public interface DataValueSetService
 {
@@ -40,5 +39,5 @@
     
     ImportSummary saveDataValueSet( InputStream in );
     
-    ImportSummary saveDataValueSet( InputStream in, IdentifiableProperty dataElementIdScheme, IdentifiableProperty orgUnitIdScheme, boolean dryRun, ImportStrategy strategy );
+    ImportSummary saveDataValueSet( InputStream in, ImportOptions importOptions );
 }

=== modified file 'dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DefaultDataValueSetService.java'
--- dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DefaultDataValueSetService.java	2012-04-11 11:33:43 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DefaultDataValueSetService.java	2012-04-11 16:18:25 +0000
@@ -56,6 +56,7 @@
 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.dxf2.metadata.ImportOptions;
 import org.hisp.dhis.importexport.ImportStrategy;
 import org.hisp.dhis.jdbc.batchhandler.DataValueBatchHandler;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
@@ -69,6 +70,9 @@
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.transaction.annotation.Transactional;
 
+/**
+ * @author Lars Helge Overland
+ */
 @Transactional
 public class DefaultDataValueSetService
     implements DataValueSetService
@@ -144,10 +148,10 @@
     
     public ImportSummary saveDataValueSet( InputStream in )
     {
-        return saveDataValueSet( in, IdentifiableProperty.UID, IdentifiableProperty.UID, false, ImportStrategy.NEW_AND_UPDATES );
+        return saveDataValueSet( in, ImportOptions.getDefaultImportOptions() );
     }
     
-    public ImportSummary saveDataValueSet( InputStream in, IdentifiableProperty dataElementIdScheme, IdentifiableProperty orgUnitIdScheme, boolean dryRun, ImportStrategy strategy )
+    public ImportSummary saveDataValueSet( InputStream in, ImportOptions importOptions )
     {
         notifier.clear( DATAVALUE_IMPORT ).notify( DATAVALUE_IMPORT, "Process started" );
         
@@ -155,8 +159,9 @@
         
         DataValueSet dataValueSet = new StreamingDataValueSet( XMLFactory.getXMLReader( in ) );
         
-        dataElementIdScheme = dataValueSet.getDataElementIdScheme() != null ? IdentifiableProperty.valueOf( dataValueSet.getDataElementIdScheme().toUpperCase() ) : dataElementIdScheme;
-        orgUnitIdScheme = dataValueSet.getOrgUnitIdScheme() != null ? IdentifiableProperty.valueOf( dataValueSet.getOrgUnitIdScheme().toUpperCase() ) : orgUnitIdScheme;
+        IdentifiableProperty dataElementIdScheme = dataValueSet.getDataElementIdScheme() != null ? IdentifiableProperty.valueOf( dataValueSet.getDataElementIdScheme().toUpperCase() ) : importOptions.getDataElementIdScheme();
+        IdentifiableProperty orgUnitIdScheme = dataValueSet.getOrgUnitIdScheme() != null ? IdentifiableProperty.valueOf( dataValueSet.getOrgUnitIdScheme().toUpperCase() ) : importOptions.getOrgUnitIdScheme();
+        ImportStrategy strategy = importOptions.getImportStrategy();
         
         Map<String, DataElement> dataElementMap = identifiableObjectManager.getIdMap( DataElement.class, dataElementIdScheme );
         Map<String, OrganisationUnit> orgUnitMap = identifiableObjectManager.getIdMap( OrganisationUnit.class, orgUnitIdScheme );
@@ -242,7 +247,7 @@
             {
                 if ( NEW_AND_UPDATES.equals( strategy ) || UPDATES.equals( strategy ) )
                 {
-                    if ( !dryRun )
+                    if ( !importOptions.isDryRun() )
                     {
                         batchHandler.updateObject( internalValue );
                     }
@@ -254,7 +259,7 @@
             {
                 if ( NEW_AND_UPDATES.equals( strategy ) || NEW.equals( strategy ) )
                 {
-                    if ( !dryRun )
+                    if ( !importOptions.isDryRun() )
                     {
                         batchHandler.addObject( internalValue );
                     }

=== modified file 'dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/ImportOptions.java'
--- dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/ImportOptions.java	2012-04-11 15:11:09 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/ImportOptions.java	2012-04-11 16:18:25 +0000
@@ -27,50 +27,98 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import org.hisp.dhis.importexport.ImportStrategy;
+import org.hisp.dhis.common.IdentifiableObject.IdentifiableProperty;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
 public class ImportOptions
 {
+    private IdentifiableProperty dataElementIdScheme;
+    
+    private IdentifiableProperty orgUnitIdScheme;
+    
     private boolean dryRun = false;
 
     private ImportStrategy importStrategy;
 
-    private static ImportOptions defaultImportOptions = new ImportOptions( ImportStrategy.getDefaultImportStrategy() );
+    private static ImportOptions DEFAULT_OPTIONS = new ImportOptions( IdentifiableProperty.UID, IdentifiableProperty.UID, false, ImportStrategy.NEW_AND_UPDATES );
 
     public static ImportOptions getDefaultImportOptions()
     {
-        return defaultImportOptions;
+        return DEFAULT_OPTIONS;
     }
 
     public ImportOptions()
     {
-        this.importStrategy = ImportStrategy.getDefaultImportStrategy();
     }
-
+    
     public ImportOptions( ImportStrategy importStrategy )
     {
         this.importStrategy = importStrategy;
     }
 
+    public ImportOptions( IdentifiableProperty dataElementIdScheme, IdentifiableProperty orgUnitIdScheme, boolean dryRun, ImportStrategy importStrategy )
+    {
+        this.dataElementIdScheme = dataElementIdScheme;
+        this.orgUnitIdScheme = orgUnitIdScheme;
+        this.dryRun = dryRun;
+        this.importStrategy = importStrategy;
+    }
+
+    //--------------------------------------------------------------------------
+    // Get methods
+    //--------------------------------------------------------------------------
+
+    public IdentifiableProperty getDataElementIdScheme()
+    {
+        return dataElementIdScheme != null ? dataElementIdScheme : IdentifiableProperty.UID;
+    }
+
+    public IdentifiableProperty getOrgUnitIdScheme()
+    {
+        return orgUnitIdScheme != null ? orgUnitIdScheme : IdentifiableProperty.UID;
+    }
+
     public boolean isDryRun()
     {
         return dryRun;
     }
 
-    public void setDryRun( boolean dryRun )
-    {
-        this.dryRun = dryRun;
-    }
-
     public ImportStrategy getImportStrategy()
     {
-        return importStrategy;
-    }
-
-    public void setImportStrategy( ImportStrategy importStrategy )
-    {
-        this.importStrategy = importStrategy;
+        return importStrategy != null ? importStrategy : ImportStrategy.NEW_AND_UPDATES;
+    }
+
+    //--------------------------------------------------------------------------
+    // Set methods
+    //--------------------------------------------------------------------------
+
+    public void setDataElementIdScheme( String scheme )
+    {
+        this.dataElementIdScheme = scheme != null ? IdentifiableProperty.valueOf( scheme.toUpperCase() ) : null;
+    }
+
+    public void setOrgUnitIdScheme( String scheme )
+    {
+        this.orgUnitIdScheme = scheme != null ? IdentifiableProperty.valueOf( scheme.toUpperCase() ) : null;
+    }
+
+    public void setDryRun( Boolean dryRun )
+    {
+        this.dryRun = dryRun != null ? dryRun : false;
+    }
+
+    public void setImportStrategy( String strategy )
+    {
+        this.importStrategy = strategy != null ? ImportStrategy.valueOf( strategy.toUpperCase() ) : null;
+    }
+    
+    @Override
+    public String toString()
+    {
+        return "[data element id scheme: " + dataElementIdScheme + ", org unit id scheme: " + 
+            orgUnitIdScheme + ", dry run: " + dryRun + ", strategy: " + importStrategy + "]";
     }
 }

=== removed file 'dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/ImportStrategy.java'
--- dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/ImportStrategy.java	2012-04-06 14:20:16 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/ImportStrategy.java	1970-01-01 00:00:00 +0000
@@ -1,88 +0,0 @@
-package org.hisp.dhis.dxf2.metadata;
-
-/*
- * Copyright (c) 2012, 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.
- */
-
-/**
- * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
- */
-public class ImportStrategy
-{
-    public static final String NEW_AND_UPDATES_STRATEGY = "newAndUpdates";
-    public static final String UPDATES_STRATEGY = "updates";
-    public static final String NEW_STRATEGY = "new";
-
-    private String strategy;
-
-    private boolean newAndUpdatesStrategy;
-
-    private boolean updatesStrategy;
-
-    private boolean newStrategy;
-
-    public static ImportStrategy getDefaultImportStrategy()
-    {
-        return new ImportStrategy( ImportStrategy.NEW_AND_UPDATES_STRATEGY );
-    }
-
-    public ImportStrategy( String strategy )
-    {
-        setStrategy( strategy );
-    }
-
-    public String getStrategy()
-    {
-        return strategy;
-    }
-
-    public void setStrategy( String strategy )
-    {
-        this.strategy = strategy;
-
-        if ( strategy != null )
-        {
-            newAndUpdatesStrategy = strategy.equals( ImportStrategy.NEW_AND_UPDATES_STRATEGY );
-            updatesStrategy = strategy.equals( ImportStrategy.UPDATES_STRATEGY );
-            newStrategy = strategy.equals( ImportStrategy.NEW_STRATEGY );
-        }
-    }
-
-    public boolean isNewAndUpdatesStrategy()
-    {
-        return newAndUpdatesStrategy;
-    }
-
-    public boolean isUpdatesStrategy()
-    {
-        return updatesStrategy;
-    }
-
-    public boolean isNewStrategy()
-    {
-        return newStrategy;
-    }
-}

=== modified file 'dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/AbstractImporter.java'
--- dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/AbstractImporter.java	2012-04-11 15:16:17 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/AbstractImporter.java	2012-04-11 16:18:25 +0000
@@ -34,6 +34,7 @@
 import org.hisp.dhis.dxf2.importsummary.ImportCount;
 import org.hisp.dhis.dxf2.metadata.ImportOptions;
 import org.hisp.dhis.dxf2.metadata.Importer;
+import org.hisp.dhis.importexport.ImportStrategy;
 import org.hisp.dhis.system.util.ReflectionUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 
@@ -267,7 +268,7 @@
         T oldObject = getObject( object );
         ImportConflict conflict;
 
-        if ( options.getImportStrategy().isNewStrategy() )
+        if ( ImportStrategy.NEW.equals( options.getImportStrategy() ) )
         {
             prepareIdentifiableObject( object );
             conflict = newObject( object, options );
@@ -279,7 +280,7 @@
 
             imports++;
         }
-        else if ( options.getImportStrategy().isUpdatesStrategy() )
+        else if ( ImportStrategy.UPDATES.equals( options.getImportStrategy() ) )
         {
             conflict = updatedObject( object, oldObject, options );
 
@@ -290,7 +291,7 @@
 
             updates++;
         }
-        else if ( options.getImportStrategy().isNewAndUpdatesStrategy() )
+        else if ( ImportStrategy.NEW_AND_UPDATES.equals( options.getImportStrategy() ) )
         {
             if ( oldObject != null )
             {
@@ -339,15 +340,15 @@
 
         ImportConflict conflict = null;
 
-        if ( options.getImportStrategy().isNewStrategy() )
+        if ( ImportStrategy.NEW.equals( options.getImportStrategy() ) )
         {
             conflict = validateForNewStrategy( object, options );
         }
-        else if ( options.getImportStrategy().isUpdatesStrategy() )
+        else if ( ImportStrategy.UPDATES.equals( options.getImportStrategy() ) )
         {
             conflict = validateForUpdatesStrategy( object, options );
         }
-        else if ( options.getImportStrategy().isNewAndUpdatesStrategy() )
+        else if ( ImportStrategy.NEW_AND_UPDATES.equals( options.getImportStrategy() ) )
         {
             // if we have a match on at least one of the objects, then assume update
             if ( uidObject != null || codeObject != null || nameObject != null || shortNameObject != null || alternativeNameObject != null )

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DataValueSetController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DataValueSetController.java	2012-04-01 12:17:55 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/DataValueSetController.java	2012-04-11 16:18:25 +0000
@@ -37,17 +37,15 @@
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.hisp.dhis.api.webdomain.DataValueSets;
-import org.hisp.dhis.common.IdentifiableObject.IdentifiableProperty;
 import org.hisp.dhis.dxf2.datavalueset.DataValueSet;
 import org.hisp.dhis.dxf2.datavalueset.DataValueSetService;
 import org.hisp.dhis.dxf2.importsummary.ImportSummary;
+import org.hisp.dhis.dxf2.metadata.ImportOptions;
 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.ExceptionHandler;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
@@ -88,30 +86,16 @@
     
     @RequestMapping( method = RequestMethod.POST, headers = {"Content-Type=application/xml"} )
     @PreAuthorize( "hasRole('ALL') or hasRole('F_DATAVALUE_ADD')" )
-    public void postDataValueSet( @RequestParam(required=false, defaultValue="UID") String dataElementIdScheme,
-                                  @RequestParam(required=false, defaultValue="UID") String orgUnitIdScheme,
-                                  @RequestParam(required=false) boolean dryRun,
-                                  @RequestParam(required=false, defaultValue="NEW_AND_UPDATES") String strategy,
+    public void postDataValueSet( ImportOptions importOptions,
                                   HttpServletResponse response, 
                                   InputStream in,
                                   Model model ) throws IOException
     {
-        IdentifiableProperty _dataElementidScheme = IdentifiableProperty.valueOf( dataElementIdScheme.toUpperCase() );
-        IdentifiableProperty _orgUnitIdScheme = IdentifiableProperty.valueOf( orgUnitIdScheme.toUpperCase() );
-        ImportStrategy _strategy = ImportStrategy.valueOf( strategy.toUpperCase() );
-        
-        ImportSummary summary = dataValueSetService.saveDataValueSet( in, _dataElementidScheme, _orgUnitIdScheme, dryRun, _strategy );
+        ImportSummary summary = dataValueSetService.saveDataValueSet( in, importOptions );
 
-        log.info( "Data values set saved, data element id scheme: " + _dataElementidScheme + ", org unit id scheme: " + _orgUnitIdScheme + ",  dry run: " + dryRun + ", strategy: " + _strategy );    
+        log.info( "Data values set saved " + importOptions );    
 
         response.setContentType( CONTENT_TYPE_XML );        
         JacksonUtils.toXml( response.getOutputStream(), summary );
     }
-
-    @ExceptionHandler( IllegalArgumentException.class )
-    public void handleException( HttpServletResponse response, IllegalArgumentException ex )
-        throws IOException
-    {
-        response.sendError( HttpServletResponse.SC_CONFLICT, ex.getMessage() );
-    }
 }