← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 17132: add new controller, ProgramInstanceController, can be used to query PIs, required parameters are ...

 

------------------------------------------------------------
revno: 17132
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2014-10-16 12:27:09 +0700
message:
  add new controller, ProgramInstanceController, can be used to query PIs, required parameters are program, orgUnit (at least one), startDate, endDate.
added:
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/ProgramInstanceController.java
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramInstance.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentitycomment/TrackedEntityComment.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/program/ProgramInstance.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramInstance.java	2014-08-15 07:40:20 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramInstance.java	2014-10-16 05:27:09 +0000
@@ -29,15 +29,12 @@
  */
 
 import com.fasterxml.jackson.annotation.JsonProperty;
-import com.fasterxml.jackson.annotation.JsonView;
 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
 import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
 import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
-
 import org.hisp.dhis.common.BaseIdentifiableObject;
 import org.hisp.dhis.common.DxfNamespaces;
-import org.hisp.dhis.common.view.DetailedView;
-import org.hisp.dhis.common.view.ExportView;
 import org.hisp.dhis.event.EventStatus;
 import org.hisp.dhis.message.MessageConversation;
 import org.hisp.dhis.sms.outbound.OutboundSms;
@@ -71,8 +68,6 @@
 
     public static int STATUS_CANCELLED = 2;
 
-    private int id;
-
     private Date dateOfIncident; // TODO rename to incidenceDate
 
     private Date enrollmentDate;
@@ -101,8 +96,7 @@
     {
     }
 
-    public ProgramInstance( Date enrollmentDate, Date dateOfIncident, TrackedEntityInstance entityInstance,
-        Program program )
+    public ProgramInstance( Date enrollmentDate, Date dateOfIncident, TrackedEntityInstance entityInstance, Program program )
     {
         this.enrollmentDate = enrollmentDate;
         this.dateOfIncident = dateOfIncident;
@@ -117,9 +111,9 @@
     /**
      * Updated the bi-directional associations between this program instance and
      * the given entity instance and program.
-     * 
+     *
      * @param entityInstance the entity instance to enroll.
-     * @param program the program to enroll the entity instance to.
+     * @param program        the program to enroll the entity instance to.
      */
     public void enrollTrackedEntityInstance( TrackedEntityInstance entityInstance, Program program )
     {
@@ -266,18 +260,7 @@
     // Getters and setters
     // -------------------------------------------------------------------------
 
-    public int getId()
-    {
-        return id;
-    }
-
-    public void setId( int id )
-    {
-        this.id = id;
-    }
-
     @JsonProperty
-    @JsonView( { DetailedView.class, ExportView.class } )
     @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
     public Date getDateOfIncident()
     {
@@ -290,7 +273,6 @@
     }
 
     @JsonProperty
-    @JsonView( { DetailedView.class, ExportView.class } )
     @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
     public Date getEnrollmentDate()
     {
@@ -303,7 +285,6 @@
     }
 
     @JsonProperty
-    @JsonView( { DetailedView.class, ExportView.class } )
     @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
     public Date getEndDate()
     {
@@ -316,7 +297,6 @@
     }
 
     @JsonProperty
-    @JsonView( { DetailedView.class, ExportView.class } )
     @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
     public int getStatus()
     {
@@ -330,7 +310,6 @@
 
     @JsonProperty( "trackedEntityInstance" )
     @JsonSerialize( as = BaseIdentifiableObject.class )
-    @JsonView( { DetailedView.class, ExportView.class } )
     @JacksonXmlProperty( localName = "trackedEntityInstance", namespace = DxfNamespaces.DXF_2_0 )
     public TrackedEntityInstance getEntityInstance()
     {
@@ -344,7 +323,6 @@
 
     @JsonProperty
     @JsonSerialize( as = BaseIdentifiableObject.class )
-    @JsonView( { DetailedView.class, ExportView.class } )
     @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
     public Program getProgram()
     {
@@ -356,6 +334,9 @@
         this.program = program;
     }
 
+    @JsonProperty
+    @JacksonXmlElementWrapper( localName = "programStageInstances", namespace = DxfNamespaces.DXF_2_0 )
+    @JacksonXmlProperty( localName = "programStageInstance", namespace = DxfNamespaces.DXF_2_0 )
     public Set<ProgramStageInstance> getProgramStageInstances()
     {
         return programStageInstances;
@@ -377,7 +358,6 @@
     }
 
     @JsonProperty
-    @JsonView( { DetailedView.class, ExportView.class } )
     @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
     public Boolean getFollowup()
     {
@@ -390,8 +370,8 @@
     }
 
     @JsonProperty
-    @JsonView( { DetailedView.class, ExportView.class } )
-    @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
+    @JacksonXmlElementWrapper( localName = "messageConversations", namespace = DxfNamespaces.DXF_2_0 )
+    @JacksonXmlProperty( localName = "messageConversation", namespace = DxfNamespaces.DXF_2_0 )
     public List<MessageConversation> getMessageConversations()
     {
         return messageConversations;
@@ -402,9 +382,9 @@
         this.messageConversations = messageConversations;
     }
 
-    @JsonProperty
-    @JsonView( { DetailedView.class, ExportView.class } )
-    @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
+    @JsonProperty( "trackedEntityComments" )
+    @JacksonXmlElementWrapper( localName = "trackedEntityComments", namespace = DxfNamespaces.DXF_2_0 )
+    @JacksonXmlProperty( localName = "trackedEntityComment", namespace = DxfNamespaces.DXF_2_0 )
     public List<TrackedEntityComment> getComments()
     {
         return comments;

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentitycomment/TrackedEntityComment.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentitycomment/TrackedEntityComment.java	2014-05-23 14:58:07 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/trackedentitycomment/TrackedEntityComment.java	2014-10-16 05:27:09 +0000
@@ -29,19 +29,16 @@
  */
 
 import com.fasterxml.jackson.annotation.JsonProperty;
-import com.fasterxml.jackson.annotation.JsonView;
 import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
 import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
 import org.hisp.dhis.common.DxfNamespaces;
-import org.hisp.dhis.common.view.DetailedView;
-import org.hisp.dhis.common.view.ExportView;
 
 import java.util.Date;
 
 /**
  * @author Chau Thu Tran
  */
-@JacksonXmlRootElement( localName = "trackedEntityComment", namespace = DxfNamespaces.DXF_2_0 )
+@JacksonXmlRootElement(localName = "trackedEntityComment", namespace = DxfNamespaces.DXF_2_0)
 public class TrackedEntityComment
 {
     private int id;
@@ -84,8 +81,7 @@
     }
 
     @JsonProperty
-    @JsonView( { DetailedView.class, ExportView.class } )
-    @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
+    @JacksonXmlProperty(namespace = DxfNamespaces.DXF_2_0)
     public String getCommentText()
     {
         return commentText;
@@ -97,8 +93,7 @@
     }
 
     @JsonProperty
-    @JsonView( { DetailedView.class, ExportView.class } )
-    @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
+    @JacksonXmlProperty(namespace = DxfNamespaces.DXF_2_0)
     public Date getCreatedDate()
     {
         return createdDate;
@@ -110,8 +105,7 @@
     }
 
     @JsonProperty
-    @JsonView( { DetailedView.class, ExportView.class } )
-    @JacksonXmlProperty( namespace = DxfNamespaces.DXF_2_0 )
+    @JacksonXmlProperty(namespace = DxfNamespaces.DXF_2_0)
     public String getCreator()
     {
         return creator;

=== added file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/ProgramInstanceController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/ProgramInstanceController.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/event/ProgramInstanceController.java	2014-10-16 05:27:09 +0000
@@ -0,0 +1,150 @@
+package org.hisp.dhis.webapi.controller.event;
+
+/*
+ * Copyright (c) 2004-2014, 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 com.google.common.collect.Lists;
+import org.hisp.dhis.common.DxfNamespaces;
+import org.hisp.dhis.common.IdentifiableObjectManager;
+import org.hisp.dhis.common.IdentifiableObjectUtils;
+import org.hisp.dhis.common.Pager;
+import org.hisp.dhis.dxf2.fieldfilter.FieldFilterService;
+import org.hisp.dhis.node.types.ComplexNode;
+import org.hisp.dhis.node.types.RootNode;
+import org.hisp.dhis.node.types.SimpleNode;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.program.Program;
+import org.hisp.dhis.program.ProgramInstance;
+import org.hisp.dhis.program.ProgramInstanceService;
+import org.hisp.dhis.webapi.service.ContextService;
+import org.hisp.dhis.webapi.utils.ContextUtils;
+import org.hisp.dhis.webapi.webdomain.WebMetaData;
+import org.hisp.dhis.webapi.webdomain.WebOptions;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.format.annotation.DateTimeFormat;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+
+import javax.servlet.http.HttpServletResponse;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+@RestController
+@RequestMapping( value = "/programInstances" )
+public class ProgramInstanceController
+{
+    @Autowired
+    private ProgramInstanceService programInstanceService;
+
+    @Autowired
+    private IdentifiableObjectManager manager;
+
+    @Autowired
+    protected ContextService contextService;
+
+    @Autowired
+    protected FieldFilterService fieldFilterService;
+
+    @RequestMapping( method = RequestMethod.GET )
+    public RootNode getProgramInstances(
+        @RequestParam( value = "program" ) String programId,
+        @RequestParam( value = "orgUnit" ) List<String> orgUnits,
+        @RequestParam @DateTimeFormat( pattern = "yyyy-MM-dd" ) Date startDate,
+        @RequestParam @DateTimeFormat( pattern = "yyyy-MM-dd" ) Date endDate,
+        @RequestParam Map<String, String> parameters, HttpServletResponse response
+    )
+    {
+        List<String> fields = Lists.newArrayList( contextService.getParameterValues( "fields" ) );
+
+        if ( fields.isEmpty() )
+        {
+            fields.add( ":identifiable" );
+        }
+
+        WebOptions options = new WebOptions( parameters );
+        WebMetaData metaData = new WebMetaData();
+
+        Program program = manager.get( Program.class, programId );
+
+        if ( program == null )
+        {
+            ContextUtils.conflictResponse( response, "program parameter is required." );
+        }
+
+        if ( orgUnits.isEmpty() )
+        {
+            ContextUtils.conflictResponse( response, "At least one orgUnit is required." );
+        }
+
+        List<OrganisationUnit> organisationUnits = new ArrayList<>( manager.getByUid( OrganisationUnit.class, orgUnits ) );
+        List<Integer> identifiers = IdentifiableObjectUtils.getIdentifiers( organisationUnits );
+
+        List<ProgramInstance> programInstances;
+
+        if ( options.hasPaging() )
+        {
+            int count = programInstanceService.countProgramInstances( program, identifiers, startDate, endDate );
+
+            Pager pager = new Pager( options.getPage(), count, options.getPageSize() );
+            metaData.setPager( pager );
+
+            programInstances = new ArrayList<>( programInstanceService.getProgramInstances( program, identifiers,
+                startDate, endDate, pager.getOffset(), pager.getPageSize() ) );
+        }
+        else
+        {
+            programInstances = new ArrayList<>( programInstanceService.getProgramInstances( program, identifiers,
+                startDate, endDate, 0, Integer.MAX_VALUE ) );
+        }
+
+        RootNode rootNode = new RootNode( "metadata" );
+        rootNode.setDefaultNamespace( DxfNamespaces.DXF_2_0 );
+        rootNode.setNamespace( DxfNamespaces.DXF_2_0 );
+
+        if ( options.hasPaging() )
+        {
+            ComplexNode pagerNode = rootNode.addChild( new ComplexNode( "pager" ) );
+            pagerNode.addChild( new SimpleNode( "page", metaData.getPager().getPage() ) );
+            pagerNode.addChild( new SimpleNode( "pageCount", metaData.getPager().getPageCount() ) );
+            pagerNode.addChild( new SimpleNode( "total", metaData.getPager().getTotal() ) );
+            pagerNode.addChild( new SimpleNode( "nextPage", metaData.getPager().getNextPage() ) );
+            pagerNode.addChild( new SimpleNode( "prevPage", metaData.getPager().getPrevPage() ) );
+        }
+
+        rootNode.addChild( fieldFilterService.filter( ProgramInstance.class, programInstances, fields ) );
+
+        return rootNode;
+    }
+}