← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 21347: Data prune. Impl data pruning maintenance function at /api/dataPruning/organisationUnits/{uid}. I...

 

------------------------------------------------------------
revno: 21347
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Sat 2015-12-05 12:02:20 +0100
message:
  Data prune. Impl data pruning maintenance function at /api/dataPruning/organisationUnits/{uid}. Includes data set registrations, data approvals, data value audits
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataapproval/DataApprovalService.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataapproval/DataApprovalStore.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/maintenance/MaintenanceService.java
  dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/maintenance/DefaultMaintenanceService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataapproval/DefaultDataApprovalService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataapproval/hibernate/HibernateDataApprovalStore.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/MaintenanceController.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/dataapproval/DataApprovalService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataapproval/DataApprovalService.java	2015-12-01 17:19:15 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataapproval/DataApprovalService.java	2015-12-05 11:02:20 +0000
@@ -112,4 +112,11 @@
      */
     List<DataApprovalStatus> getUserDataApprovalsAndPermissions( DataApprovalWorkflow workflow,
         Period period, OrganisationUnit orgUnit, DataElementCategoryCombo attributeCombo );
+    
+    /**
+     * Deletes DataApprovals for the given organisation unit.
+     * 
+     * @param organisationUnit the organisation unit.
+     */
+    void deleteDataApprovals( OrganisationUnit organisationUnit );
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataapproval/DataApprovalStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataapproval/DataApprovalStore.java	2015-12-01 17:19:15 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataapproval/DataApprovalStore.java	2015-12-05 11:02:20 +0000
@@ -72,6 +72,13 @@
     void deleteDataApproval( DataApproval dataApproval );
 
     /**
+     * Deletes DataApprovals for the given organisation unit.
+     * 
+     * @param organisationUnit the organisation unit.
+     */
+    void deleteDataApprovals( OrganisationUnit organisationUnit );
+
+    /**
      * Returns the DataApproval object (if any) matching the properties
      * of a (non-Hibernate) DataApproval object.
      *

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/maintenance/MaintenanceService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/maintenance/MaintenanceService.java	2015-09-16 14:49:50 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/maintenance/MaintenanceService.java	2015-12-05 11:02:20 +0000
@@ -28,9 +28,10 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+
 /**
  * @author Lars Helge Overland
- * @version $Id$
  */
 public interface MaintenanceService
 {
@@ -50,6 +51,15 @@
     void prunePeriods();
     
     /**
+     * Prunes complete data set registrations, data approvals, data value audits
+     * and data values for the given organisation unit.
+     * 
+     * @param organisationUnit the organisation unit.
+     * @return true if the data pruning took place, false if not permitted.
+     */
+    boolean pruneData( OrganisationUnit organisationUnit );
+    
+    /**
      * Deletes user accounts representing expired account invitations.
      */
     void removeExpiredInvitations();

=== modified file 'dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/maintenance/DefaultMaintenanceService.java'
--- dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/maintenance/DefaultMaintenanceService.java	2015-06-15 13:44:20 +0000
+++ dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/maintenance/DefaultMaintenanceService.java	2015-12-05 11:02:20 +0000
@@ -9,10 +9,18 @@
 import org.hisp.dhis.period.Period;
 import org.hisp.dhis.period.PeriodService;
 import org.hisp.dhis.commons.util.PageRange;
+import org.hisp.dhis.dataapproval.DataApprovalService;
+import org.hisp.dhis.dataset.CompleteDataSetRegistrationService;
+import org.hisp.dhis.datavalue.DataValueAuditService;
+import org.hisp.dhis.datavalue.DataValueService;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.user.CurrentUserService;
 import org.hisp.dhis.user.User;
 import org.hisp.dhis.user.UserInvitationStatus;
 import org.hisp.dhis.user.UserQueryParams;
 import org.hisp.dhis.user.UserService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.transaction.annotation.Transactional;
 
 /*
  * Copyright (c) 2004-2015, University of Oslo
@@ -74,6 +82,21 @@
     {
         this.userService = userService;
     }
+    
+    @Autowired
+    private CurrentUserService currentUserService;
+    
+    @Autowired
+    private DataValueService dataValueService;
+    
+    @Autowired
+    private DataValueAuditService dataValueAuditService;
+    
+    @Autowired
+    private CompleteDataSetRegistrationService completeRegistrationService;
+    
+    @Autowired
+    private DataApprovalService dataApprovalService;
 
     // -------------------------------------------------------------------------
     // MaintenanceService implementation
@@ -106,6 +129,25 @@
     }
 
     @Override
+    @Transactional
+    public boolean pruneData( OrganisationUnit organisationUnit )
+    {
+        User user = currentUserService.getCurrentUser();
+        
+        if ( user == null || !user.isSuper() )
+        {
+            return false;
+        }
+        
+        dataApprovalService.deleteDataApprovals( organisationUnit );
+        completeRegistrationService.deleteCompleteDataSetRegistrations( organisationUnit );
+        dataValueAuditService.deleteDataValueAudits( organisationUnit );
+        dataValueService.deleteDataValues( organisationUnit );
+        
+        return true;
+    }
+
+    @Override
     public void removeExpiredInvitations()
     {
         UserQueryParams params = new UserQueryParams();

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataapproval/DefaultDataApprovalService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataapproval/DefaultDataApprovalService.java	2015-12-01 18:17:25 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataapproval/DefaultDataApprovalService.java	2015-12-05 11:02:20 +0000
@@ -409,6 +409,12 @@
 
         return statusList;
     }
+    
+    @Override
+    public void deleteDataApprovals( OrganisationUnit organisationUnit )
+    {
+        dataApprovalStore.deleteDataApprovals( organisationUnit );
+    }
 
     // -------------------------------------------------------------------------
     // Supportive methods

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataapproval/hibernate/HibernateDataApprovalStore.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataapproval/hibernate/HibernateDataApprovalStore.java	2015-12-01 18:17:25 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataapproval/hibernate/HibernateDataApprovalStore.java	2015-12-05 11:02:20 +0000
@@ -154,6 +154,15 @@
         dataApproval.setPeriod( periodService.reloadPeriod( dataApproval.getPeriod() ) );
 
         delete( dataApproval );
+    }    
+
+    @Override
+    public void deleteDataApprovals( OrganisationUnit organisationUnit )
+    {
+        String hql = "delete from DataApproval d where d.organisationUnit = :unit";
+        
+        sessionFactory.getCurrentSession().createQuery( hql ).
+            setEntity( "unit", organisationUnit ).executeUpdate();
     }
 
     @Override

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/MaintenanceController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/MaintenanceController.java	2015-11-24 07:06:45 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/MaintenanceController.java	2015-12-05 11:02:20 +0000
@@ -37,8 +37,12 @@
 import org.hisp.dhis.dxf2.metadata.MetaData;
 import org.hisp.dhis.dxf2.render.RenderService;
 import org.hisp.dhis.dxf2.schema.SchemaValidator;
+import org.hisp.dhis.dxf2.webmessage.WebMessage;
 import org.hisp.dhis.validation.ValidationViolation;
+import org.hisp.dhis.webapi.service.WebMessageService;
+import org.hisp.dhis.webapi.utils.WebMessageUtils;
 import org.hisp.dhis.maintenance.MaintenanceService;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
 import org.hisp.dhis.resourcetable.ResourceTableService;
 import org.hisp.dhis.schema.Property;
@@ -48,6 +52,7 @@
 import org.springframework.http.MediaType;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 
@@ -69,6 +74,9 @@
     public static final String RESOURCE_PATH = "/maintenance";
 
     @Autowired
+    private WebMessageService webMessageService;
+    
+    @Autowired
     private MaintenanceService maintenanceService;
 
     @Autowired
@@ -111,6 +119,26 @@
     {
         maintenanceService.prunePeriods();
     }
+    
+    @RequestMapping( value = "/dataPruning/organisationUnits/{uid}", method = { RequestMethod.PUT, RequestMethod.POST } )
+    @PreAuthorize( "hasRole('ALL')" )
+    public void pruneDataByOrganisationUnit( @PathVariable String uid, HttpServletResponse response )
+        throws Exception
+    {
+        OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit( uid );
+        
+        if ( organisationUnit == null )
+        {
+            webMessageService.sendJson( WebMessageUtils.conflict( "Organisation unit does not exist: " + uid ), response );
+            return;
+        }
+        
+        boolean result = maintenanceService.pruneData( organisationUnit );
+        
+        WebMessage message = result ? WebMessageUtils.ok( "Data was pruned successfully" ) : WebMessageUtils.conflict( "Data could not be pruned" );
+        
+        webMessageService.sendJson( message, response );
+    }    
 
     @RequestMapping( value = "/zeroDataValueRemoval", method = { RequestMethod.PUT, RequestMethod.POST } )
     @PreAuthorize( "hasRole('ALL') or hasRole('F_PERFORM_MAINTENANCE')" )