← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 21625: Improved deletion handler to remove data elements, data sets, indicators, periods, organisation u...

 

------------------------------------------------------------
revno: 21625
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2016-01-06 16:52:21 +0100
message:
  Improved deletion handler to remove data elements, data sets, indicators, periods, organisation units, cat option groups from favorites (pivot tables, charts, maps) when deleted. Introduced class GenericAnalyticalObjectDeletionHandler to centralize deletion handler code.
added:
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/GenericAnalyticalObjectDeletionHandler.java
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/AnalyticalObject.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/AnalyticalObjectService.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/AnalyticalObjectStore.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/GenericAnalyticalObjectService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/mapping/MapViewDeletionHandler.java
  dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/chart/ChartDeletionHandler.java
  dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/reporttable/ReportTableDeletionHandler.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/common/AnalyticalObject.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/AnalyticalObject.java	2016-01-04 02:27:49 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/AnalyticalObject.java	2016-01-06 15:52:21 +0000
@@ -32,13 +32,16 @@
 import java.util.List;
 import java.util.Map;
 
+import org.hisp.dhis.dataelement.CategoryOptionGroup;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.period.Period;
 import org.hisp.dhis.program.Program;
 
 /**
 * @author Lars Helge Overland
 */
 public interface AnalyticalObject
+    extends IdentifiableObject
 {
     void populateAnalyticalProperties();
     
@@ -54,5 +57,15 @@
     
     OrganisationUnit getRelativeOrganisationUnit();
     
+    List<Period> getPeriods();
+    
+    List<OrganisationUnit> getOrganisationUnits();
+    
+    List<CategoryOptionGroup> getCategoryOptionGroups();
+    
     Program getProgram();
+    
+    boolean addDataDimensionItem( DimensionalItemObject object );
+    
+    boolean removeDataDimensionItem( DimensionalItemObject object );
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/AnalyticalObjectService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/AnalyticalObjectService.java	2016-01-06 14:25:55 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/AnalyticalObjectService.java	2016-01-06 15:52:21 +0000
@@ -41,8 +41,10 @@
 /**
  * @author Lars Helge Overland
  */
-public interface AnalyticalObjectService<T extends BaseAnalyticalObject>
+public interface AnalyticalObjectService<T extends AnalyticalObject>
 {
+    void update( T object );
+    
     List<T> getAnalyticalObjects( Indicator indicator );
     
     List<T> getAnalyticalObjects( DataElement dataElement );

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/AnalyticalObjectStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/AnalyticalObjectStore.java	2016-01-06 14:03:52 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/AnalyticalObjectStore.java	2016-01-06 15:52:21 +0000
@@ -41,7 +41,7 @@
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
-public interface AnalyticalObjectStore<T extends BaseAnalyticalObject>
+public interface AnalyticalObjectStore<T extends AnalyticalObject>
     extends GenericIdentifiableObjectStore<T>
 {
     List<T> getAnalyticalObjects( Indicator indicator );

=== added file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/GenericAnalyticalObjectDeletionHandler.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/GenericAnalyticalObjectDeletionHandler.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/GenericAnalyticalObjectDeletionHandler.java	2016-01-06 15:52:21 +0000
@@ -0,0 +1,94 @@
+package org.hisp.dhis.common;
+
+/*
+ * 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.
+ */
+
+import java.util.List;
+import java.util.function.BiConsumer;
+
+import org.hisp.dhis.dataelement.CategoryOptionGroup;
+import org.hisp.dhis.dataelement.DataElement;
+import org.hisp.dhis.dataset.DataSet;
+import org.hisp.dhis.indicator.Indicator;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.period.Period;
+import org.hisp.dhis.system.deletion.DeletionHandler;
+
+/**
+ * @author Lars Helge Overland
+ */
+public abstract class GenericAnalyticalObjectDeletionHandler<T extends AnalyticalObject>
+    extends DeletionHandler
+{
+    protected abstract AnalyticalObjectService<T> getAnalyticalObjectService();
+    
+    @Override
+    public void deleteIndicator( Indicator indicator )
+    {
+        removeItem( getAnalyticalObjectService().getAnalyticalObjects( indicator ), indicator, ( ao, di ) -> ao.removeDataDimensionItem( di ) );
+    }
+    
+    @Override
+    public void deleteDataElement( DataElement dataElement )
+    {
+        removeItem( getAnalyticalObjectService().getAnalyticalObjects( dataElement ), dataElement, ( ao, di ) -> ao.removeDataDimensionItem( di ) );
+    }
+
+    @Override
+    public void deleteDataSet( DataSet dataSet )
+    {
+        removeItem( getAnalyticalObjectService().getAnalyticalObjects( dataSet ), dataSet, ( ao, di ) -> ao.removeDataDimensionItem( di ) );
+    }
+
+    @Override
+    public void deletePeriod( Period period )
+    {
+        removeItem( getAnalyticalObjectService().getAnalyticalObjects( period ), period, ( ao, di ) -> ao.getPeriods().remove( di ) );
+    }
+    
+    @Override
+    public void deleteOrganisationUnit( OrganisationUnit organisationUnit )
+    {
+        removeItem( getAnalyticalObjectService().getAnalyticalObjects( organisationUnit ), organisationUnit, ( ao, di ) -> ao.getOrganisationUnits().remove( di ) );
+    }
+    
+    @Override
+    public void deleteCategoryOptionGroup( CategoryOptionGroup categoryOptionGroup )
+    {
+        removeItem( getAnalyticalObjectService().getAnalyticalObjects( categoryOptionGroup ), categoryOptionGroup, ( ao, di ) -> ao.getCategoryOptionGroups().remove( di ) );
+    }
+    
+    private void removeItem( List<T> analyticalObjects, DimensionalItemObject itemObject, BiConsumer<AnalyticalObject, DimensionalItemObject> updateOperation )
+    {
+        for ( T analyticalObject : analyticalObjects )
+        {
+            updateOperation.accept( analyticalObject, itemObject );
+            getAnalyticalObjectService().update( analyticalObject );
+        }
+    }
+}

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/GenericAnalyticalObjectService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/GenericAnalyticalObjectService.java	2016-01-06 14:25:55 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/GenericAnalyticalObjectService.java	2016-01-06 15:52:21 +0000
@@ -41,76 +41,95 @@
 /**
  * @author Lars Helge Overland
  */
-public abstract class GenericAnalyticalObjectService<T extends BaseAnalyticalObject>
+public abstract class GenericAnalyticalObjectService<T extends AnalyticalObject>
     implements AnalyticalObjectService<T>
 {
     protected abstract AnalyticalObjectStore<T> getAnalyticalObjectStore();
 
+    public void update( T object )
+    {
+        getAnalyticalObjectStore().update( object );
+    }
+
+    @Override
     public List<T> getAnalyticalObjects( Indicator indicator )
     {
         return getAnalyticalObjectStore().getAnalyticalObjects( indicator );
     }
-    
+
+    @Override
     public List<T> getAnalyticalObjects( DataElement dataElement )
     {
         return getAnalyticalObjectStore().getAnalyticalObjects( dataElement );
     }
-    
+
+    @Override
     public List<T> getAnalyticalObjects( DataSet dataSet )
     {
         return getAnalyticalObjectStore().getAnalyticalObjects( dataSet );
     }
 
+    @Override
     public List<T> getAnalyticalObjects( ProgramIndicator programIndicator )
     {
         return getAnalyticalObjectStore().getAnalyticalObjects( programIndicator );
     }
 
+    @Override
     public List<T> getAnalyticalObjects( Period period )
     {
         return getAnalyticalObjectStore().getAnalyticalObjects( period );
     }
 
+    @Override
     public List<T> getAnalyticalObjects( OrganisationUnit organisationUnit )
     {
         return getAnalyticalObjectStore().getAnalyticalObjects( organisationUnit );
     }
 
+    @Override
     public List<T> getAnalyticalObjects( CategoryOptionGroup categoryOptionGroup )
     {
         return getAnalyticalObjectStore().getAnalyticalObjects( categoryOptionGroup );
     }
-    
+
+    @Override
     public int countAnalyticalObjects( Indicator indicator )
     {
         return getAnalyticalObjectStore().countAnalyticalObjects( indicator );
     }
 
+    @Override
     public int countAnalyticalObjects( DataElement dataElement )
     {
         return getAnalyticalObjectStore().countAnalyticalObjects( dataElement );
     }
 
+    @Override
     public int countAnalyticalObjects( DataSet dataSet )
     {
         return getAnalyticalObjectStore().countAnalyticalObjects( dataSet );
     }
 
+    @Override
     public int countAnalyticalObjects( ProgramIndicator programIndicator )
     {
         return getAnalyticalObjectStore().countAnalyticalObjects( programIndicator );
     }
-    
+
+    @Override
     public int countAnalyticalObjects( Period period )
     {
         return getAnalyticalObjectStore().countAnalyticalObjects( period );
     }
-    
+
+    @Override
     public int countAnalyticalObjects( OrganisationUnit organisationUnit )
     {
         return getAnalyticalObjectStore().countAnalyticalObjects( organisationUnit );
     }
-    
+
+    @Override
     public int countAnalyticalObjects( CategoryOptionGroup categoryOptionGroup )
     {
         return getAnalyticalObjectStore().countAnalyticalObjects( categoryOptionGroup );

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/mapping/MapViewDeletionHandler.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/mapping/MapViewDeletionHandler.java	2016-01-06 14:25:55 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/mapping/MapViewDeletionHandler.java	2016-01-06 15:52:21 +0000
@@ -28,18 +28,14 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import org.hisp.dhis.dataelement.DataElement;
-import org.hisp.dhis.dataset.DataSet;
-import org.hisp.dhis.indicator.Indicator;
-import org.hisp.dhis.organisationunit.OrganisationUnit;
-import org.hisp.dhis.period.Period;
-import org.hisp.dhis.system.deletion.DeletionHandler;
+import org.hisp.dhis.common.AnalyticalObjectService;
+import org.hisp.dhis.common.GenericAnalyticalObjectDeletionHandler;
 
 /**
  * @author Lars Helge Overland
  */
 public class MapViewDeletionHandler
-    extends DeletionHandler
+    extends GenericAnalyticalObjectDeletionHandler<MapView>
 {
     // -------------------------------------------------------------------------
     // Dependencies
@@ -57,6 +53,12 @@
     // -------------------------------------------------------------------------
 
     @Override
+    protected AnalyticalObjectService<MapView> getAnalyticalObjectService()
+    {
+        return mappingService;
+    }
+
+    @Override
     protected String getClassName()
     {
         return MapView.class.getSimpleName();
@@ -67,34 +69,4 @@
     {
         return mappingService.countMapViewMaps( mapView ) == 0 ? null : ERROR;
     }
-
-    @Override
-    public String allowDeleteDataSet( DataSet dataSet )
-    {
-        return mappingService.countAnalyticalObjects( dataSet ) == 0 ? null : ERROR;
-    }
-
-    @Override
-    public String allowDeleteIndicator( Indicator indicator )
-    {
-        return mappingService.countAnalyticalObjects( indicator ) == 0 ? null : ERROR;
-    }
-
-    @Override
-    public String allowDeletePeriod( Period period )
-    {
-        return mappingService.countAnalyticalObjects( period ) == 0 ? null : ERROR;
-    }
-    
-    @Override
-    public String allowDeleteDataElement( DataElement dataElement )
-    {
-        return mappingService.countAnalyticalObjects( dataElement ) == 0 ? null : ERROR;
-    }
-    
-    @Override
-    public String allowDeleteOrganisationUnit( OrganisationUnit organisationUnit )
-    {
-        return mappingService.countAnalyticalObjects( organisationUnit ) == 0 ? null : ERROR;
-    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/chart/ChartDeletionHandler.java'
--- dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/chart/ChartDeletionHandler.java	2016-01-06 14:25:55 +0000
+++ dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/chart/ChartDeletionHandler.java	2016-01-06 15:52:21 +0000
@@ -28,20 +28,14 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import org.hisp.dhis.dataelement.CategoryOptionGroup;
-import org.hisp.dhis.dataelement.DataElement;
-import org.hisp.dhis.dataset.DataSet;
-import org.hisp.dhis.indicator.Indicator;
-import org.hisp.dhis.organisationunit.OrganisationUnit;
-import org.hisp.dhis.period.Period;
-import org.hisp.dhis.system.deletion.DeletionHandler;
+import org.hisp.dhis.common.AnalyticalObjectService;
+import org.hisp.dhis.common.GenericAnalyticalObjectDeletionHandler;
 
 /**
  * @author Lars Helge Overland
- * @version $Id$
  */
 public class ChartDeletionHandler
-    extends DeletionHandler
+    extends GenericAnalyticalObjectDeletionHandler<Chart>
 {
     // -------------------------------------------------------------------------
     // Dependencies
@@ -59,44 +53,14 @@
     // -------------------------------------------------------------------------
 
     @Override
+    protected AnalyticalObjectService<Chart> getAnalyticalObjectService()
+    {
+        return chartService;
+    }
+    
+    @Override
     public String getClassName()
     {
         return Chart.class.getSimpleName();
     }
-
-    @Override
-    public String allowDeleteDataSet( DataSet dataSet )
-    {
-        return chartService.countAnalyticalObjects( dataSet ) == 0 ? null : ERROR;
-    }
-
-    @Override
-    public String allowDeleteIndicator( Indicator indicator )
-    {
-        return chartService.countAnalyticalObjects( indicator ) == 0 ? null : ERROR;
-    }
-
-    @Override
-    public String allowDeleteDataElement( DataElement dataElement )
-    {
-        return chartService.countAnalyticalObjects( dataElement ) == 0 ? null : ERROR;
-    }
-
-    @Override
-    public String allowDeletePeriod( Period period )
-    {
-        return chartService.countAnalyticalObjects( period ) == 0 ? null : ERROR;
-    }
-    
-    @Override
-    public String allowDeleteOrganisationUnit( OrganisationUnit organisationUnit )
-    {
-        return chartService.countAnalyticalObjects( organisationUnit ) == 0 ? null : ERROR;
-    }
-    
-    @Override
-    public String allowDeleteCategoryOptionGroup( CategoryOptionGroup categoryOptionGroup )
-    {
-        return chartService.countAnalyticalObjects( categoryOptionGroup ) == 0 ? null : ERROR;
-    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/reporttable/ReportTableDeletionHandler.java'
--- dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/reporttable/ReportTableDeletionHandler.java	2016-01-06 14:52:32 +0000
+++ dhis-2/dhis-services/dhis-service-reporting/src/main/java/org/hisp/dhis/reporttable/ReportTableDeletionHandler.java	2016-01-06 15:52:21 +0000
@@ -28,19 +28,14 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import org.hisp.dhis.dataelement.CategoryOptionGroup;
-import org.hisp.dhis.dataelement.DataElement;
-import org.hisp.dhis.dataset.DataSet;
-import org.hisp.dhis.indicator.Indicator;
-import org.hisp.dhis.organisationunit.OrganisationUnit;
-import org.hisp.dhis.period.Period;
-import org.hisp.dhis.system.deletion.DeletionHandler;
+import org.hisp.dhis.common.AnalyticalObjectService;
+import org.hisp.dhis.common.GenericAnalyticalObjectDeletionHandler;
 
 /**
  * @author Lars Helge Overland
  */
 public class ReportTableDeletionHandler
-    extends DeletionHandler
+    extends GenericAnalyticalObjectDeletionHandler<ReportTable>
 {
     // -------------------------------------------------------------------------
     // Dependencies
@@ -58,47 +53,14 @@
     // -------------------------------------------------------------------------
 
     @Override
+    protected AnalyticalObjectService<ReportTable> getAnalyticalObjectService()
+    {
+        return reportTableService;
+    }
+    
+    @Override
     public String getClassName()
     {
         return ReportTable.class.getSimpleName();
     }
-    
-    @Override
-    public void deleteIndicator( Indicator indicator )
-    {
-        for ( ReportTable reportTable : reportTableService.getAnalyticalObjects( indicator ) )
-        {
-            reportTableService.deleteReportTable( reportTable );
-        }
-    }
-    
-    @Override
-    public String allowDeleteDataElement( DataElement dataElement )
-    {
-        return reportTableService.countAnalyticalObjects( dataElement ) == 0 ? null : ERROR;
-    }
-
-    @Override
-    public String allowDeleteDataSet( DataSet dataSet )
-    {
-        return reportTableService.countAnalyticalObjects( dataSet ) == 0 ? null : ERROR;
-    }
-    
-    @Override
-    public String allowDeletePeriod( Period period )
-    {
-        return reportTableService.countAnalyticalObjects( period ) == 0 ? null : ERROR;
-    }
-    
-    @Override
-    public String allowDeleteOrganisationUnit( OrganisationUnit organisationUnit )
-    {
-        return reportTableService.countAnalyticalObjects( organisationUnit ) == 0 ? null : ERROR;
-    }
-    
-    @Override
-    public String allowDeleteCategoryOptionGroup( CategoryOptionGroup categoryOptionGroup )
-    {
-        return reportTableService.countAnalyticalObjects( categoryOptionGroup ) == 0 ? null : ERROR;
-    }
 }