← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 20857: support bulk deletion of events

 

------------------------------------------------------------
revno: 20857
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2015-10-23 11:36:05 +0700
message:
  support bulk deletion of events
modified:
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/AbstractEventService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/JacksonEventService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/importsummary/ImportSummaries.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/importsummary/ImportSummary.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/EventController.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-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/AbstractEventService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/AbstractEventService.java	2015-10-23 03:41:44 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/AbstractEventService.java	2015-10-23 04:36:05 +0000
@@ -110,8 +110,6 @@
 {
     private static final Log log = LogFactory.getLog( AbstractEventService.class );
 
-    public static final String OPTIONS_SEP = ";";
-
     // -------------------------------------------------------------------------
     // Dependencies
     // -------------------------------------------------------------------------
@@ -257,13 +255,13 @@
 
         if ( program == null )
         {
-            return new ImportSummary( ImportStatus.ERROR, "Event.program does not point to a valid program" );
+            return new ImportSummary( ImportStatus.ERROR, "Event.program does not point to a valid program" ).incrementIgnored();
         }
 
         if ( programStage == null && program.isRegistration() )
         {
             return new ImportSummary( ImportStatus.ERROR,
-                "Event.programStage does not point to a valid programStage, and program is multi stage" );
+                "Event.programStage does not point to a valid programStage, and program is multi stage" ).incrementIgnored();
         }
         else if ( programStage == null )
         {
@@ -276,7 +274,7 @@
         if ( verifyProgramAccess( program, user ) )
         {
             return new ImportSummary( ImportStatus.ERROR,
-                "Current user does not have permission to access this program" );
+                "Current user does not have permission to access this program" ).incrementIgnored();
         }
 
         if ( program.isRegistration() )
@@ -284,7 +282,7 @@
             if ( event.getTrackedEntityInstance() == null )
             {
                 return new ImportSummary( ImportStatus.ERROR,
-                    "No Event.trackedEntityInstance was provided for registration based program" );
+                    "No Event.trackedEntityInstance was provided for registration based program" ).incrementIgnored();
             }
 
             org.hisp.dhis.trackedentity.TrackedEntityInstance entityInstance = entityInstanceService
@@ -293,7 +291,7 @@
             if ( entityInstance == null )
             {
                 return new ImportSummary( ImportStatus.ERROR,
-                    "Event.trackedEntityInstance does not point to a valid tracked entity instance" );
+                    "Event.trackedEntityInstance does not point to a valid tracked entity instance" ).incrementIgnored();
             }
 
             List<ProgramInstance> programInstances = new ArrayList<>( programInstanceService.getProgramInstances(
@@ -302,12 +300,12 @@
             if ( programInstances.isEmpty() )
             {
                 return new ImportSummary( ImportStatus.ERROR, "TrackedEntityInstance " + entityInstance.getUid()
-                    + " is not enrolled in program " + program.getUid() );
+                    + " is not enrolled in program " + program.getUid() ).incrementIgnored();
             }
             else if ( programInstances.size() > 1 )
             {
                 return new ImportSummary( ImportStatus.ERROR, "TrackedEntityInstance " + entityInstance.getUid()
-                    + " have multiple active enrollments in program " + program.getUid() );
+                    + " have multiple active enrollments in program " + program.getUid() ).incrementIgnored();
             }
 
             programInstance = programInstances.get( 0 );
@@ -320,12 +318,12 @@
                 if ( programStageInstances.isEmpty() )
                 {
                     return new ImportSummary( ImportStatus.ERROR, "TrackedEntityInstance " + entityInstance.getUid()
-                        + " is not enrolled in program stage " + programStage.getUid() );
+                        + " is not enrolled in program stage " + programStage.getUid() ).incrementIgnored();
                 }
                 else if ( programStageInstances.size() > 1 )
                 {
                     return new ImportSummary( ImportStatus.ERROR, "TrackedEntityInstance " + entityInstance.getUid()
-                        + " have multiple active enrollments in program stage " + programStage.getUid() );
+                        + " have multiple active enrollments in program stage " + programStage.getUid() ).incrementIgnored();
                 }
 
                 programStageInstance = programStageInstances.get( 0 );
@@ -347,7 +345,7 @@
                         {
                             if ( !CodeGenerator.isValidCode( event.getEvent() ) )
                             {
-                                return new ImportSummary( ImportStatus.ERROR, "Event.event did not point to a valid event" );
+                                return new ImportSummary( ImportStatus.ERROR, "Event.event did not point to a valid event" ).incrementIgnored();
                             }
                         }
                     }
@@ -375,7 +373,7 @@
             else if ( programInstances.size() > 1 )
             {
                 return new ImportSummary( ImportStatus.ERROR,
-                    "Multiple active program instances exists for program " + program.getUid() );
+                    "Multiple active program instances exists for program " + program.getUid() ).incrementIgnored();
             }
 
             programInstance = programInstances.get( 0 );
@@ -388,7 +386,7 @@
                 {
                     if ( !CodeGenerator.isValidCode( event.getEvent() ) )
                     {
-                        return new ImportSummary( ImportStatus.ERROR, "Event.event did not point to a valid event" );
+                        return new ImportSummary( ImportStatus.ERROR, "Event.event did not point to a valid event" ).incrementIgnored();
                     }
                 }
             }
@@ -398,12 +396,12 @@
 
         if ( organisationUnit == null )
         {
-            return new ImportSummary( ImportStatus.ERROR, "Event.orgUnit does not point to a valid organisation unit" );
+            return new ImportSummary( ImportStatus.ERROR, "Event.orgUnit does not point to a valid organisation unit" ).incrementIgnored();
         }
 
         if ( verifyProgramOrganisationUnitAssociation( program, organisationUnit ) )
         {
-            return new ImportSummary( ImportStatus.ERROR, "Program is not assigned to this organisation unit" );
+            return new ImportSummary( ImportStatus.ERROR, "Program is not assigned to this organisation unit" ).incrementIgnored();
         }
 
         return saveEvent( program, programInstance, programStage, programStageInstance, organisationUnit, event,
@@ -630,7 +628,7 @@
         if ( programStageInstance == null )
         {
             importSummary.getConflicts().add( new ImportConflict( "Invalid Event ID.", event.getEvent() ) );
-            return importSummary;
+            return importSummary.incrementIgnored();
         }
 
         if ( importOptions == null )
@@ -812,10 +810,10 @@
         if ( programStageInstance != null )
         {
             programStageInstanceService.deleteProgramStageInstance( programStageInstance );
-            return new ImportSummary( ImportStatus.SUCCESS, "Deletion of event " + uid + " was successful." );
+            return new ImportSummary( ImportStatus.SUCCESS, "Deletion of event " + uid + " was successful." ).incrementDeleted();
         }
 
-        return new ImportSummary( ImportStatus.ERROR, "ID " + uid + " does not point to a valid event" );
+        return new ImportSummary( ImportStatus.ERROR, "ID " + uid + " does not point to a valid event" ).incrementIgnored();
     }
 
     @Override

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/JacksonEventService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/JacksonEventService.java	2015-10-20 06:38:20 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/JacksonEventService.java	2015-10-23 04:36:05 +0000
@@ -48,6 +48,7 @@
 import java.nio.charset.Charset;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.stream.Collectors;
 
 /**
  * Implementation of EventService that uses Jackson for serialization and deserialization.
@@ -143,13 +144,14 @@
 
     private ImportSummaries addEvents( List<Event> events, TaskId taskId, ImportOptions importOptions )
     {
-        ImportSummaries importSummaries;
+        ImportSummaries importSummaries = new ImportSummaries();
 
         notifier.clear( taskId ).notify( taskId, "Importing events" );
         Timer timer = new SystemTimer().start();
 
         List<Event> create = new ArrayList<>();
         List<Event> update = new ArrayList<>();
+        List<String> delete = new ArrayList<>();
 
         if ( importOptions.getImportStrategy().isCreate() )
         {
@@ -176,9 +178,14 @@
                 }
             }
         }
+        else if ( importOptions.getImportStrategy().isDelete() )
+        {
+            delete.addAll( events.stream().map( Event::getEvent ).collect( Collectors.toList() ) );
+        }
 
-        importSummaries = addEvents( create, importOptions );
-        updateEvents( update, false );
+        importSummaries.addImportSummaries( addEvents( create, importOptions ) );
+        importSummaries.addImportSummaries( updateEvents( update, false ) );
+        importSummaries.addImportSummaries( deleteEvents( delete ) );
 
         if ( taskId != null )
         {

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/importsummary/ImportSummaries.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/importsummary/ImportSummaries.java	2015-09-30 04:29:57 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/importsummary/ImportSummaries.java	2015-10-23 04:36:05 +0000
@@ -48,6 +48,8 @@
 
     private int updated;
 
+    private int deleted;
+
     private int ignored;
 
     private List<ImportSummary> importSummaries = new ArrayList<>();
@@ -68,6 +70,7 @@
         {
             imported += importSummary.getImportCount().getImported();
             updated += importSummary.getImportCount().getUpdated();
+            deleted += importSummary.getImportCount().getDeleted();
             ignored += importSummary.getImportCount().getIgnored();
         }
 
@@ -105,6 +108,13 @@
 
     @JsonProperty
     @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
+    public int getDeleted()
+    {
+        return deleted;
+    }
+
+    @JsonProperty
+    @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
     public int getIgnored()
     {
         return ignored;

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/importsummary/ImportSummary.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/importsummary/ImportSummary.java	2015-07-08 04:24:08 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/importsummary/ImportSummary.java	2015-10-23 04:36:05 +0000
@@ -171,6 +171,30 @@
         this.href = href;
     }
 
+    public ImportSummary incrementImported()
+    {
+        importCount.incrementImported();
+        return this;
+    }
+
+    public ImportSummary incrementUpdated()
+    {
+        importCount.incrementUpdated();
+        return this;
+    }
+
+    public ImportSummary incrementIgnored()
+    {
+        importCount.incrementIgnored();
+        return this;
+    }
+
+    public ImportSummary incrementDeleted()
+    {
+        importCount.incrementDeleted();
+        return this;
+    }
+
     @Override
     public String toString()
     {

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/EventController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/EventController.java	2015-10-22 03:42:30 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/EventController.java	2015-10-23 04:36:05 +0000
@@ -347,7 +347,7 @@
             ImportSummaries importSummaries = eventService.addEventsXml( inputStream, importOptions );
 
             importSummaries.getImportSummaries().stream()
-                .filter( importSummary -> !importOptions.isDryRun() && !importSummary.getStatus().equals( ImportStatus.ERROR ) )
+                .filter( importSummary -> !importOptions.isDryRun() && !importSummary.getStatus().equals( ImportStatus.ERROR ) && !importOptions.getImportStrategy().isDelete() )
                 .forEach( importSummary -> importSummary.setHref( ContextUtils.getRootPath( request ) + RESOURCE_PATH + "/" + importSummary.getReference() ) );
 
             if ( importSummaries.getImportSummaries().size() == 1 )
@@ -386,7 +386,7 @@
             ImportSummaries importSummaries = eventService.addEventsJson( inputStream, importOptions );
 
             importSummaries.getImportSummaries().stream()
-                .filter( importSummary -> !importOptions.isDryRun() && !importSummary.getStatus().equals( ImportStatus.ERROR ) )
+                .filter( importSummary -> !importOptions.isDryRun() && !importSummary.getStatus().equals( ImportStatus.ERROR ) && !importOptions.getImportStrategy().isDelete() )
                 .forEach( importSummary -> importSummary.setHref( ContextUtils.getRootPath( request ) + RESOURCE_PATH + "/" + importSummary.getReference() ) );
 
             if ( importSummaries.getImportSummaries().size() == 1 )