← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 21233: audit controller for TEAVA

 

------------------------------------------------------------
revno: 21233
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2015-11-30 10:21:59 +0700
message:
  audit controller for TEAVA
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentityattributevalue/TrackedEntityAttributeValueAuditService.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentityattributevalue/TrackedEntityAttributeValueAuditStore.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentityattributevalue/DefaultTrackedEntityAttributeValueAuditService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentityattributevalue/hibernate/HibernateTrackedEntityAttributeValueAuditStore.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AuditController.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/trackedentityattributevalue/TrackedEntityAttributeValueAuditService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentityattributevalue/TrackedEntityAttributeValueAuditService.java	2015-11-30 02:50:28 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentityattributevalue/TrackedEntityAttributeValueAuditService.java	2015-11-30 03:21:59 +0000
@@ -28,10 +28,19 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import org.hisp.dhis.common.AuditType;
+import org.hisp.dhis.trackedentity.TrackedEntityAttribute;
+import org.hisp.dhis.trackedentity.TrackedEntityInstance;
+
+import java.util.List;
+
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
 public interface TrackedEntityAttributeValueAuditService
 {
     void addTrackedEntityAttributeValueAudit( TrackedEntityAttributeValueAudit trackedEntityAttributeValueAudit );
+
+    List<TrackedEntityAttributeValueAudit> getTrackedEntityAttributeValueAudits( List<TrackedEntityAttribute> trackedEntityAttributes,
+        List<TrackedEntityInstance> trackedEntityInstances, AuditType auditType );
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentityattributevalue/TrackedEntityAttributeValueAuditStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentityattributevalue/TrackedEntityAttributeValueAuditStore.java	2015-11-30 02:50:28 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentityattributevalue/TrackedEntityAttributeValueAuditStore.java	2015-11-30 03:21:59 +0000
@@ -28,10 +28,19 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import org.hisp.dhis.common.AuditType;
+import org.hisp.dhis.trackedentity.TrackedEntityAttribute;
+import org.hisp.dhis.trackedentity.TrackedEntityInstance;
+
+import java.util.List;
+
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
 public interface TrackedEntityAttributeValueAuditStore
 {
     void addTrackedEntityAttributeValueAudit( TrackedEntityAttributeValueAudit trackedEntityAttributeValueAudit );
+
+    List<TrackedEntityAttributeValueAudit> getTrackedEntityAttributeValueAudits( List<TrackedEntityAttribute> trackedEntityAttributes,
+        List<TrackedEntityInstance> trackedEntityInstances, AuditType auditType );
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentityattributevalue/DefaultTrackedEntityAttributeValueAuditService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentityattributevalue/DefaultTrackedEntityAttributeValueAuditService.java	2015-11-30 02:50:28 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentityattributevalue/DefaultTrackedEntityAttributeValueAuditService.java	2015-11-30 03:21:59 +0000
@@ -28,8 +28,13 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import org.hisp.dhis.common.AuditType;
+import org.hisp.dhis.trackedentity.TrackedEntityAttribute;
+import org.hisp.dhis.trackedentity.TrackedEntityInstance;
 import org.springframework.beans.factory.annotation.Autowired;
 
+import java.util.List;
+
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
@@ -44,4 +49,11 @@
     {
         trackedEntityAttributeValueAuditStore.addTrackedEntityAttributeValueAudit( trackedEntityAttributeValueAudit );
     }
+
+    @Override
+    public List<TrackedEntityAttributeValueAudit> getTrackedEntityAttributeValueAudits( List<TrackedEntityAttribute> trackedEntityAttributes,
+        List<TrackedEntityInstance> trackedEntityInstances, AuditType auditType )
+    {
+        return trackedEntityAttributeValueAuditStore.getTrackedEntityAttributeValueAudits( trackedEntityAttributes, trackedEntityInstances, auditType );
+    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentityattributevalue/hibernate/HibernateTrackedEntityAttributeValueAuditStore.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentityattributevalue/hibernate/HibernateTrackedEntityAttributeValueAuditStore.java	2015-11-30 02:50:28 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentityattributevalue/hibernate/HibernateTrackedEntityAttributeValueAuditStore.java	2015-11-30 03:21:59 +0000
@@ -28,10 +28,19 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import org.hibernate.Criteria;
 import org.hibernate.Session;
 import org.hibernate.SessionFactory;
+import org.hibernate.criterion.Order;
+import org.hibernate.criterion.Restrictions;
+import org.hisp.dhis.common.AuditType;
+import org.hisp.dhis.trackedentity.TrackedEntityAttribute;
+import org.hisp.dhis.trackedentity.TrackedEntityInstance;
 import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValueAudit;
 import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValueAuditStore;
+import org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValueAudit;
+
+import java.util.List;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
@@ -60,4 +69,32 @@
         Session session = sessionFactory.getCurrentSession();
         session.save( trackedEntityAttributeValueAudit );
     }
+
+    @Override
+    @SuppressWarnings( "unchecked" )
+    public List<TrackedEntityAttributeValueAudit> getTrackedEntityAttributeValueAudits( List<TrackedEntityAttribute> trackedEntityAttributes,
+        List<TrackedEntityInstance> trackedEntityInstances, AuditType auditType )
+    {
+        Session session = sessionFactory.getCurrentSession();
+        Criteria criteria = session.createCriteria( TrackedEntityAttributeValueAudit.class );
+
+        if ( !trackedEntityAttributes.isEmpty() )
+        {
+            criteria.add( Restrictions.in( "attribute", trackedEntityAttributes ) );
+        }
+
+        if ( !trackedEntityInstances.isEmpty() )
+        {
+            criteria.add( Restrictions.in( "entityInstance", trackedEntityInstances ) );
+        }
+
+        if ( auditType != null )
+        {
+            criteria.add( Restrictions.eq( "auditType", auditType ) );
+        }
+
+        criteria.addOrder( Order.desc( "timestamp" ) );
+
+        return criteria.list();
+    }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AuditController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AuditController.java	2015-11-27 10:16:01 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/AuditController.java	2015-11-30 03:21:59 +0000
@@ -43,6 +43,10 @@
 import org.hisp.dhis.period.PeriodType;
 import org.hisp.dhis.program.ProgramStageInstance;
 import org.hisp.dhis.program.ProgramStageInstanceService;
+import org.hisp.dhis.trackedentity.TrackedEntityAttribute;
+import org.hisp.dhis.trackedentity.TrackedEntityInstance;
+import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValueAudit;
+import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValueAuditService;
 import org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValueAudit;
 import org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValueAuditService;
 import org.hisp.dhis.webapi.utils.WebMessageUtils;
@@ -76,6 +80,9 @@
     private TrackedEntityDataValueAuditService trackedEntityDataValueAuditService;
 
     @Autowired
+    private TrackedEntityAttributeValueAuditService trackedEntityAttributeValueAuditService;
+
+    @Autowired
     private FieldFilterService fieldFilterService;
 
     @RequestMapping( value = "dataValue", method = RequestMethod.GET )
@@ -122,15 +129,102 @@
         return rootNode;
     }
 
+    @RequestMapping( value = "trackedEntityAttributeValue", method = RequestMethod.GET )
+    public @ResponseBody RootNode getTrackedEntityAttributeValueAudit(
+        @RequestParam( required = false, defaultValue = "" ) List<String> tea,
+        @RequestParam( required = false, defaultValue = "" ) List<String> tei,
+        @RequestParam( required = false ) AuditType auditType
+    ) throws WebMessageException
+    {
+        List<TrackedEntityAttribute> trackedEntityAttributes = getTrackedEntityAttributes( tea );
+        List<org.hisp.dhis.trackedentity.TrackedEntityInstance> trackedEntityInstances = getTrackedEntityInstances( tei );
+
+        List<TrackedEntityAttributeValueAudit> attributeValueAudits = trackedEntityAttributeValueAuditService.getTrackedEntityAttributeValueAudits(
+            trackedEntityAttributes, trackedEntityInstances, auditType );
+
+        RootNode rootNode = NodeUtils.createRootNode( "trackedEntityAttributeValueAudits" );
+        rootNode.addChild( fieldFilterService.filter( TrackedEntityAttributeValueAudit.class, attributeValueAudits, new ArrayList<>() ) );
+
+        return rootNode;
+    }
+
     //-----------------------------------------------------------------------------------------------------------------
     // Helpers
     //-----------------------------------------------------------------------------------------------------------------
 
-    private List<ProgramStageInstance> getProgramStageInstances( List<String> psIdentifier ) throws WebMessageException
+    private List<TrackedEntityInstance> getTrackedEntityInstances( List<String> teiIdentifiers ) throws WebMessageException
+    {
+        List<TrackedEntityInstance> trackedEntityInstances = new ArrayList<>();
+
+        for ( String tei : teiIdentifiers )
+        {
+            TrackedEntityInstance trackedEntityInstance = getTrackedEntityInstance( tei );
+
+            if ( trackedEntityInstance != null )
+            {
+                trackedEntityInstances.add( trackedEntityInstance );
+            }
+        }
+
+        return trackedEntityInstances;
+    }
+
+    private TrackedEntityInstance getTrackedEntityInstance( String tei ) throws WebMessageException
+    {
+        if ( tei == null )
+        {
+            return null;
+        }
+
+        TrackedEntityInstance trackedEntityInstance = manager.get( TrackedEntityInstance.class, tei );
+
+        if ( trackedEntityInstance == null )
+        {
+            throw new WebMessageException( WebMessageUtils.conflict( "Illegal trackedEntityInstance identifier: " + tei ) );
+        }
+
+        return trackedEntityInstance;
+    }
+
+    private List<TrackedEntityAttribute> getTrackedEntityAttributes( List<String> teaIdentifiers ) throws WebMessageException
+    {
+        List<TrackedEntityAttribute> trackedEntityAttributes = new ArrayList<>();
+
+        for ( String tea : teaIdentifiers )
+        {
+            TrackedEntityAttribute trackedEntityAttribute = getTrackedEntityAttribute( tea );
+
+            if ( trackedEntityAttribute != null )
+            {
+                trackedEntityAttributes.add( trackedEntityAttribute );
+            }
+        }
+
+        return trackedEntityAttributes;
+    }
+
+    private TrackedEntityAttribute getTrackedEntityAttribute( String tea ) throws WebMessageException
+    {
+        if ( tea == null )
+        {
+            return null;
+        }
+
+        TrackedEntityAttribute trackedEntityAttribute = manager.get( TrackedEntityAttribute.class, tea );
+
+        if ( trackedEntityAttribute == null )
+        {
+            throw new WebMessageException( WebMessageUtils.conflict( "Illegal trackedEntityAttribute identifier: " + tea ) );
+        }
+
+        return trackedEntityAttribute;
+    }
+
+    private List<ProgramStageInstance> getProgramStageInstances( List<String> psIdentifiers ) throws WebMessageException
     {
         List<ProgramStageInstance> programStageInstances = new ArrayList<>();
 
-        for ( String ps : psIdentifier )
+        for ( String ps : psIdentifiers )
         {
             ProgramStageInstance programStageInstance = getProgramStageInstance( ps );