← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 14595: expose ability to import a single object in metadata importer (including dep scanning etc). Updat...

 

------------------------------------------------------------
revno: 14595
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2014-04-02 12:00:27 +0700
message:
  expose ability to import a single object in metadata importer (including dep scanning etc). Updated crudController to use this. Wip
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserCredentials.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/DefaultImportService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/ImportService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AbstractCrudController.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/user/UserCredentials.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserCredentials.java	2014-03-25 10:14:39 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserCredentials.java	2014-04-02 05:00:27 +0000
@@ -28,6 +28,7 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import com.fasterxml.jackson.annotation.JsonIgnore;
 import com.fasterxml.jackson.annotation.JsonProperty;
 import com.fasterxml.jackson.annotation.JsonView;
 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
@@ -388,12 +389,6 @@
     }
 
     @Override
-    public String toString()
-    {
-        return "[" + username + "]";
-    }
-
-    @Override
     public boolean haveUniqueNames()
     {
         return false;
@@ -403,16 +398,22 @@
     // Getters and setters
     // -------------------------------------------------------------------------
 
+    @JsonIgnore
     public String getPassword()
     {
         return password;
     }
 
+    @JsonProperty
+    @JacksonXmlProperty(namespace = DxfNamespaces.DXF_2_0)
     public void setPassword( String password )
     {
         this.password = password;
     }
 
+    @JsonProperty
+    @JsonView({ DetailedView.class, ExportView.class })
+    @JacksonXmlProperty(namespace = DxfNamespaces.DXF_2_0)
     public Date getPasswordLastUpdated()
     {
         return passwordLastUpdated;
@@ -532,4 +533,22 @@
     {
         this.disabled = disabled;
     }
+
+    @Override
+    public String toString()
+    {
+        return "UserCredentials{" +
+            "username='" + username + '\'' +
+            ", openId='" + openId + '\'' +
+            ", password='" + password + '\'' +
+            ", passwordLastUpdated=" + passwordLastUpdated +
+            ", userAuthorityGroups=" + userAuthorityGroups +
+            ", lastLogin=" + lastLogin +
+            ", restoreToken='" + restoreToken + '\'' +
+            ", restoreCode='" + restoreCode + '\'' +
+            ", restoreExpiry=" + restoreExpiry +
+            ", selfRegistered=" + selfRegistered +
+            ", disabled=" + disabled +
+            '}';
+    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/DefaultImportService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/DefaultImportService.java	2014-04-01 04:34:10 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/DefaultImportService.java	2014-04-02 05:00:27 +0000
@@ -28,19 +28,12 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import static org.springframework.util.Assert.notNull;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Date;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
-
+import com.google.common.collect.Lists;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.hibernate.SessionFactory;
 import org.hisp.dhis.cache.HibernateCacheManager;
+import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.dxf2.timer.SystemNanoTimer;
 import org.hisp.dhis.dxf2.timer.Timer;
 import org.hisp.dhis.scheduling.TaskId;
@@ -53,7 +46,14 @@
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.StringUtils;
 
-import com.google.common.collect.Lists;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Date;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import static org.springframework.util.Assert.notNull;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
@@ -91,6 +91,27 @@
     //-------------------------------------------------------------------------------------------------------
 
     @Override
+    public <T extends IdentifiableObject> ImportTypeSummary importObject( String userUid, T object )
+    {
+        User user = userService.getUser( userUid );
+
+        ImportOptions importOptions = new ImportOptions();
+        importOptions.setDryRun( false );
+        importOptions.setPreheatCache( false );
+
+        objectBridge.setWriteEnabled( !importOptions.isDryRun() );
+        objectBridge.setPreheatCache( importOptions.isPreheatCache() );
+        objectBridge.init();
+
+        ImportTypeSummary importTypeSummary = doImport( user, object, importOptions );
+
+        cacheManager.clearCache();
+        objectBridge.destroy();
+
+        return importTypeSummary;
+    }
+
+    @Override
     public ImportSummary importMetaData( String userUid, MetaData metaData, TaskId taskId )
     {
         return importMetaData( userUid, metaData, ImportOptions.getDefaultImportOptions(), taskId );
@@ -252,4 +273,20 @@
 
         return null;
     }
+
+    private <T extends IdentifiableObject> ImportTypeSummary doImport( User user, T object, ImportOptions importOptions )
+    {
+        Importer<T> importer = findImporterClass( object.getClass() );
+
+        if ( importer != null )
+        {
+            return importer.importObject( user, object, importOptions );
+        }
+        else
+        {
+            log.warn( "Importer for object of type " + object.getClass().getSimpleName() + " not found." );
+        }
+
+        return null;
+    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/ImportService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/ImportService.java	2014-03-18 08:10:10 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/ImportService.java	2014-04-02 05:00:27 +0000
@@ -28,6 +28,7 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.scheduling.TaskId;
 import org.springframework.stereotype.Service;
 
@@ -37,6 +38,8 @@
 @Service
 public interface ImportService
 {
+    <T extends IdentifiableObject> ImportTypeSummary importObject( String userUid, T object );
+
     ImportSummary importMetaData( String userUid, MetaData metaData );
 
     ImportSummary importMetaData( String userUid, MetaData metaData, TaskId taskId );

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.java	2014-03-27 06:07:15 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.java	2014-04-02 05:00:27 +0000
@@ -112,7 +112,7 @@
     @Autowired
     private AclService aclService;
 
-    @Autowired( required = false )
+    @Autowired(required = false)
     private List<ObjectHandler<T>> objectHandlers;
 
     //-------------------------------------------------------------------------------------------------------
@@ -646,6 +646,12 @@
         this.summaryType = new ImportTypeSummary( importerClass.getSimpleName() );
         this.summaryType.setDataValueCount( null );
 
+        if ( object == null )
+        {
+            summaryType.getImportCount().incrementIgnored();
+            return summaryType;
+        }
+
         ObjectHandlerUtils.preObjectHandlers( object, objectHandlers );
         importObjectLocal( user, object );
         ObjectHandlerUtils.postObjectHandlers( object, objectHandlers );

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AbstractCrudController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AbstractCrudController.java	2014-03-31 05:26:06 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/AbstractCrudController.java	2014-04-02 05:00:27 +0000
@@ -41,6 +41,8 @@
 import org.hisp.dhis.common.PagerUtils;
 import org.hisp.dhis.dxf2.filter.FilterService;
 import org.hisp.dhis.dxf2.metadata.ExchangeClasses;
+import org.hisp.dhis.dxf2.metadata.ImportService;
+import org.hisp.dhis.dxf2.metadata.ImportTypeSummary;
 import org.hisp.dhis.dxf2.render.RenderService;
 import org.hisp.dhis.dxf2.utils.JacksonUtils;
 import org.hisp.dhis.hibernate.exception.CreateAccessDeniedException;
@@ -97,6 +99,9 @@
     @Autowired
     protected RenderService renderService;
 
+    @Autowired
+    protected ImportService importService;
+
     //--------------------------------------------------------------------------
     // GET
     //--------------------------------------------------------------------------
@@ -265,7 +270,8 @@
         }
 
         T parsed = renderService.fromXml( request.getInputStream(), getEntityClass() );
-        manager.save( parsed );
+        ImportTypeSummary summary = importService.importObject( currentUserService.getCurrentUser().getUid(), parsed );
+        renderService.toJson( response.getOutputStream(), summary );
     }
 
     @RequestMapping( method = RequestMethod.POST, consumes = "application/json" )
@@ -277,7 +283,8 @@
         }
 
         T parsed = renderService.fromJson( request.getInputStream(), getEntityClass() );
-        manager.save( parsed );
+        ImportTypeSummary summary = importService.importObject( currentUserService.getCurrentUser().getUid(), parsed );
+        renderService.toJson( response.getOutputStream(), summary );
     }
 
     //--------------------------------------------------------------------------
@@ -299,8 +306,8 @@
         T parsed = renderService.fromXml( request.getInputStream(), getEntityClass() );
         ((BaseIdentifiableObject) parsed).setUid( uid );
 
-        object.mergeWith( parsed );
-        manager.update( object );
+        ImportTypeSummary summary = importService.importObject( currentUserService.getCurrentUser().getUid(), parsed );
+        renderService.toJson( response.getOutputStream(), summary );
     }
 
     @RequestMapping( value = "/{uid}", method = RequestMethod.PUT, consumes = "application/json" )
@@ -318,8 +325,8 @@
         T parsed = renderService.fromJson( request.getInputStream(), getEntityClass() );
         ((BaseIdentifiableObject) parsed).setUid( uid );
 
-        object.mergeWith( parsed );
-        manager.update( object );
+        ImportTypeSummary summary = importService.importObject( currentUserService.getCurrentUser().getUid(), parsed );
+        renderService.toJson( response.getOutputStream(), summary );
     }
 
     //--------------------------------------------------------------------------