← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 21982: Implemented basic deletion for object bundle commits

 

------------------------------------------------------------
revno: 21982
committer: Morten Olav Hansen <morten@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2016-02-17 12:33:53 +0700
message:
  Implemented basic deletion for object bundle commits
added:
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleStatus.java
  dhis-2/dhis-services/dhis-service-dxf2/src/test/resources/dxf2/de_deletes1.json
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/Preheat.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/DefaultObjectBundleService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundle.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleParams.java
  dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleServiceTest.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/preheat/Preheat.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/Preheat.java	2016-02-08 08:21:50 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/preheat/Preheat.java	2016-02-17 05:33:53 +0000
@@ -30,8 +30,10 @@
 
 import org.hisp.dhis.common.IdentifiableObject;
 
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 /**
@@ -56,6 +58,24 @@
         return (T) map.get( identifier ).get( klass ).get( key );
     }
 
+    @SuppressWarnings( "unchecked" )
+    public <T extends IdentifiableObject> List<T> getAll( PreheatIdentifier identifier, List<T> keys )
+    {
+        List<T> objects = new ArrayList<>();
+
+        for ( T key : keys )
+        {
+            IdentifiableObject identifiableObject = get( identifier, key );
+
+            if ( identifiableObject != null )
+            {
+                objects.add( (T) identifiableObject );
+            }
+        }
+
+        return objects;
+    }
+
     public <T extends IdentifiableObject> T get( PreheatIdentifier identifier, T object )
     {
         if ( object == null )

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/DefaultObjectBundleService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/DefaultObjectBundleService.java	2016-02-16 04:53:44 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/DefaultObjectBundleService.java	2016-02-17 05:33:53 +0000
@@ -28,13 +28,19 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.hibernate.Session;
+import org.hibernate.SessionFactory;
 import org.hisp.dhis.common.IdentifiableObject;
+import org.hisp.dhis.common.IdentifiableObjectUtils;
 import org.hisp.dhis.preheat.InvalidObject;
 import org.hisp.dhis.preheat.PreheatMode;
 import org.hisp.dhis.preheat.PreheatParams;
 import org.hisp.dhis.preheat.PreheatService;
 import org.hisp.dhis.schema.validation.SchemaValidator;
 import org.hisp.dhis.schema.validation.ValidationViolation;
+import org.hisp.dhis.user.CurrentUserService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
@@ -48,18 +54,31 @@
 @Component
 public class DefaultObjectBundleService implements ObjectBundleService
 {
+    private static final Log log = LogFactory.getLog( DefaultObjectBundleService.class );
+
+    @Autowired
+    private CurrentUserService currentUserService;
+
     @Autowired
     private PreheatService preheatService;
 
     @Autowired
     private SchemaValidator schemaValidator;
 
+    @Autowired
+    private SessionFactory sessionFactory;
+
     @Override
     public ObjectBundle create( ObjectBundleParams params )
     {
         ObjectBundle bundle = new ObjectBundle( params );
         bundle.putObjects( params.getObjects() );
 
+        if ( params.getUser() == null )
+        {
+            params.setUser( currentUserService.getCurrentUser() );
+        }
+
         PreheatParams preheatParams = params.getPreheatParams();
 
         if ( PreheatMode.REFERENCE == preheatParams.getPreheatMode() )
@@ -114,6 +133,8 @@
             objectBundleValidation.addValidationViolation( klass, validationViolations );
         }
 
+        bundle.setObjectBundleStatus( ObjectBundleStatus.VALIDATED );
+
         return objectBundleValidation;
     }
 
@@ -124,5 +145,78 @@
         {
             return; // skip if validate only
         }
+
+        Session session = sessionFactory.getCurrentSession();
+
+        for ( Class<? extends IdentifiableObject> klass : bundle.getObjects().keySet() )
+        {
+            List<IdentifiableObject> objects = bundle.getObjects().get( klass );
+
+            if ( objects.isEmpty() )
+            {
+                continue;
+            }
+
+            switch ( bundle.getImportMode() )
+            {
+                case CREATE_AND_UPDATE:
+                case NEW_AND_UPDATES:
+                {
+                    handleCreatesAndUpdates( session, objects, bundle );
+                    break;
+                }
+                case CREATE:
+                case NEW:
+                {
+                    handleCreates( session, objects, bundle );
+                    break;
+                }
+                case UPDATE:
+                case UPDATES:
+                {
+                    handleUpdates( session, objects, bundle );
+                    break;
+                }
+                case DELETE:
+                case DELETES:
+                {
+                    handleDeletes( session, objects, bundle );
+                    break;
+                }
+            }
+        }
+
+        bundle.setObjectBundleStatus( ObjectBundleStatus.COMMITTED );
+    }
+
+    private void handleCreatesAndUpdates( Session session, List<IdentifiableObject> objects, ObjectBundle bundle )
+    {
+
+    }
+
+    private void handleCreates( Session session, List<IdentifiableObject> objects, ObjectBundle bundle )
+    {
+
+    }
+
+    private void handleUpdates( Session klass, List<IdentifiableObject> objects, ObjectBundle bundle )
+    {
+
+    }
+
+    private void handleDeletes( Session session, List<IdentifiableObject> objects, ObjectBundle bundle )
+    {
+        List<IdentifiableObject> persistedObjects = bundle.getPreheat().getAll( bundle.getPreheatIdentifier(), objects );
+
+        for ( IdentifiableObject object : persistedObjects )
+        {
+            session.delete( object );
+
+            if ( log.isDebugEnabled() )
+            {
+                String msg = "Deleted object '" + IdentifiableObjectUtils.getDisplayName( object ) + "'";
+                log.debug( msg );
+            }
+        }
     }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundle.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundle.java	2016-02-15 05:57:10 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundle.java	2016-02-17 05:33:53 +0000
@@ -34,6 +34,7 @@
 import org.hisp.dhis.preheat.Preheat;
 import org.hisp.dhis.preheat.PreheatIdentifier;
 import org.hisp.dhis.preheat.PreheatMode;
+import org.hisp.dhis.user.User;
 
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -45,6 +46,8 @@
  */
 public class ObjectBundle
 {
+    private final User user;
+
     private final ObjectBundleMode objectBundleMode;
 
     private final PreheatIdentifier preheatIdentifier;
@@ -55,12 +58,15 @@
 
     private final MergeMode mergeMode;
 
+    private ObjectBundleStatus objectBundleStatus = ObjectBundleStatus.CREATED;
+
     private Preheat preheat = new Preheat();
 
     private Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> objects = new HashMap<>();
 
     public ObjectBundle( ObjectBundleParams params )
     {
+        this.user = params.getUser();
         this.objectBundleMode = params.getObjectBundleMode();
         this.preheatIdentifier = params.getPreheatIdentifier();
         this.importMode = params.getImportMode();
@@ -68,6 +74,11 @@
         this.mergeMode = params.getMergeMode();
     }
 
+    public User getUser()
+    {
+        return user;
+    }
+
     public ObjectBundleMode getObjectBundleMode()
     {
         return objectBundleMode;
@@ -93,6 +104,16 @@
         return mergeMode;
     }
 
+    public ObjectBundleStatus getObjectBundleStatus()
+    {
+        return objectBundleStatus;
+    }
+
+    public void setObjectBundleStatus( ObjectBundleStatus objectBundleStatus )
+    {
+        this.objectBundleStatus = objectBundleStatus;
+    }
+
     public Preheat getPreheat()
     {
         return preheat;

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleParams.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleParams.java	2016-02-15 05:57:10 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleParams.java	2016-02-17 05:33:53 +0000
@@ -34,6 +34,7 @@
 import org.hisp.dhis.preheat.PreheatIdentifier;
 import org.hisp.dhis.preheat.PreheatMode;
 import org.hisp.dhis.preheat.PreheatParams;
+import org.hisp.dhis.user.User;
 
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -45,6 +46,8 @@
  */
 public class ObjectBundleParams
 {
+    private User user;
+
     private ObjectBundleMode objectBundleMode = ObjectBundleMode.VALIDATE;
 
     private PreheatIdentifier preheatIdentifier = PreheatIdentifier.UID;
@@ -59,6 +62,17 @@
 
     public ObjectBundleParams()
     {
+
+    }
+
+    public User getUser()
+    {
+        return user;
+    }
+
+    public void setUser( User user )
+    {
+        this.user = user;
     }
 
     public ObjectBundleMode getObjectBundleMode()

=== added file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleStatus.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleStatus.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleStatus.java	2016-02-17 05:33:53 +0000
@@ -0,0 +1,39 @@
+package org.hisp.dhis.dxf2.metadata2.objectbundle;
+
+/*
+ * Copyright (c) 2004-2016, 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 enum ObjectBundleStatus
+{
+    CREATED,
+    VALIDATED,
+    COMMITTED
+}

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleServiceTest.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleServiceTest.java	2016-02-16 06:00:52 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata2/objectbundle/ObjectBundleServiceTest.java	2016-02-17 05:33:53 +0000
@@ -378,6 +378,27 @@
         ObjectBundleValidation validate = objectBundleService.validate( bundle );
     }
 
+    @Test
+    public void testDeletes1() throws IOException
+    {
+        Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(
+            new ClassPathResource( "dxf2/de_deletes1.json" ).getInputStream(), RenderFormat.JSON );
+        defaultSetup();
+
+        ObjectBundleParams params = new ObjectBundleParams();
+        params.setObjectBundleMode( ObjectBundleMode.COMMIT );
+        params.setImportMode( ImportStrategy.DELETE );
+        params.setObjects( metadata );
+
+        ObjectBundle bundle = objectBundleService.create( params );
+        objectBundleService.validate( bundle );
+        objectBundleService.commit( bundle );
+
+        List<DataElement> dataElements = manager.getAll( DataElement.class );
+        assertEquals( 1, dataElements.size() );
+        assertEquals( "deabcdefghB", dataElements.get( 0 ).getUid() );
+    }
+
     private void defaultSetup()
     {
         DataElement de1 = createDataElement( 'A' );

=== added file 'dhis-2/dhis-services/dhis-service-dxf2/src/test/resources/dxf2/de_deletes1.json'
--- dhis-2/dhis-services/dhis-service-dxf2/src/test/resources/dxf2/de_deletes1.json	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/test/resources/dxf2/de_deletes1.json	2016-02-17 05:33:53 +0000
@@ -0,0 +1,13 @@
+{
+  "dataElements": [
+    {
+      "id": "deabcdefghA"
+    },
+    {
+      "id": "deabcdefghC"
+    },
+    {
+      "id": "deabcdefghD"
+    }
+  ]
+}
\ No newline at end of file