← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 11715: wip, add new entrypoint /api/currentUser/assignedPrograms (also renamed /forms => /assignedDataSe...

 

------------------------------------------------------------
revno: 11715
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2013-08-20 13:01:56 +0200
message:
  wip, add new entrypoint /api/currentUser/assignedPrograms (also renamed /forms => /assignedDataSets, forms is still available but deprecated).
added:
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/webdomain/FormProgram.java
modified:
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/organisationunit/OrganisationUnitController.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/user/CurrentUserController.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/webdomain/FormDataSet.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/webdomain/FormOrganisationUnit.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-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/organisationunit/OrganisationUnitController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/organisationunit/OrganisationUnitController.java	2013-07-10 09:50:45 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/organisationunit/OrganisationUnitController.java	2013-08-20 11:01:56 +0000
@@ -30,6 +30,7 @@
 import org.hisp.dhis.api.controller.AbstractCrudController;
 import org.hisp.dhis.api.controller.WebMetaData;
 import org.hisp.dhis.api.controller.WebOptions;
+import org.hisp.dhis.api.controller.exception.NotFoundException;
 import org.hisp.dhis.api.utils.ContextUtils;
 import org.hisp.dhis.api.utils.WebUtils;
 import org.hisp.dhis.common.Pager;
@@ -128,8 +129,7 @@
 
         if ( entity == null )
         {
-            ContextUtils.notFoundResponse( response, "Object not found for uid: " + uid );
-            return null;
+            throw new NotFoundException( uid );
         }
 
         if ( options.getOptions().containsKey( "level" ) )

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/user/CurrentUserController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/user/CurrentUserController.java	2013-08-20 08:04:18 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/user/CurrentUserController.java	2013-08-20 11:01:56 +0000
@@ -34,6 +34,7 @@
 import org.hisp.dhis.api.utils.FormUtils;
 import org.hisp.dhis.api.webdomain.FormDataSet;
 import org.hisp.dhis.api.webdomain.FormOrganisationUnit;
+import org.hisp.dhis.api.webdomain.FormProgram;
 import org.hisp.dhis.api.webdomain.Forms;
 import org.hisp.dhis.api.webdomain.user.Dashboard;
 import org.hisp.dhis.api.webdomain.user.Inbox;
@@ -48,7 +49,10 @@
 import org.hisp.dhis.message.MessageConversation;
 import org.hisp.dhis.message.MessageService;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.organisationunit.OrganisationUnitGroup;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
+import org.hisp.dhis.program.Program;
+import org.hisp.dhis.program.ProgramService;
 import org.hisp.dhis.user.CurrentUserService;
 import org.hisp.dhis.user.User;
 import org.hisp.dhis.user.UserGroup;
@@ -65,7 +69,10 @@
 import java.io.IOException;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
 import java.util.Set;
 
 /**
@@ -103,6 +110,9 @@
     private DataSetService dataSetService;
 
     @Autowired
+    private ProgramService programService;
+
+    @Autowired
     private ContextUtils contextUtils;
 
     @Autowired
@@ -155,7 +165,7 @@
         JacksonUtils.toJson( response.getOutputStream(), dashboard );
     }
 
-    @RequestMapping(value = "/user-account", produces = { "application/json", "text/*" })
+    @RequestMapping( value = "/user-account", produces = { "application/json", "text/*" } )
     public void getUserAccount( HttpServletResponse response ) throws Exception
     {
         User currentUser = currentUserService.getCurrentUser();
@@ -192,7 +202,7 @@
         JacksonUtils.toJson( response.getOutputStream(), userAccount );
     }
 
-    @RequestMapping(value = "/user-account", method = RequestMethod.POST, consumes = "application/json")
+    @RequestMapping( value = "/user-account", method = RequestMethod.POST, consumes = "application/json" )
     public void postUserAccountJson( HttpServletResponse response, HttpServletRequest request ) throws Exception
     {
         UserAccount userAccount = JacksonUtils.fromJson( request.getInputStream(), UserAccount.class );
@@ -228,9 +238,9 @@
         userService.updateUser( currentUser );
     }
 
-    @RequestMapping(value = "/recipients", produces = { "application/json", "text/*" })
+    @RequestMapping( value = "/recipients", produces = { "application/json", "text/*" } )
     public void recipientsJson( HttpServletResponse response,
-        @RequestParam(value = "filter") String filter ) throws IOException, NotAuthenticatedException
+        @RequestParam( value = "filter" ) String filter ) throws IOException, NotAuthenticatedException
     {
         User currentUser = currentUserService.getCurrentUser();
 
@@ -250,7 +260,7 @@
         JacksonUtils.toJson( response.getOutputStream(), recipients );
     }
 
-    @RequestMapping(value = "/assignedOrganisationUnits", produces = { "application/json", "text/*" })
+    @RequestMapping( value = "/assignedOrganisationUnits", produces = { "application/json", "text/*" } )
     public void getAssignedOrganisationUnits( HttpServletResponse response ) throws IOException, NotAuthenticatedException
     {
         User currentUser = currentUserService.getCurrentUser();
@@ -263,9 +273,90 @@
         JacksonUtils.toJson( response.getOutputStream(), currentUser.getOrganisationUnits() );
     }
 
-    @SuppressWarnings("unchecked")
-    @RequestMapping(value = "/forms", produces = { "application/json", "text/*" })
-    public void getForms( HttpServletResponse response ) throws IOException, NotAuthenticatedException
+    @SuppressWarnings( "unchecked" )
+    @RequestMapping( value = { "/assignedPrograms" }, produces = { "application/json", "text/*" } )
+    public void getPrograms( HttpServletResponse response ) throws IOException, NotAuthenticatedException
+    {
+        User currentUser = currentUserService.getCurrentUser();
+
+        if ( currentUser == null )
+        {
+            throw new NotAuthenticatedException();
+        }
+
+        Forms forms = new Forms();
+
+        List<Program> programsByCurrentUser = new ArrayList<Program>( programService.getProgramsByCurrentUser() );
+        Set<OrganisationUnit> organisationUnits = new HashSet<OrganisationUnit>();
+        Map<String, Set<Program>> mappedPrograms = new HashMap<String, Set<Program>>();
+
+        for ( Program program : programsByCurrentUser )
+        {
+            for ( OrganisationUnit organisationUnit : program.getOrganisationUnits() )
+            {
+                organisationUnits.add( organisationUnit );
+
+                if ( mappedPrograms.get( organisationUnit.getUid() ) == null )
+                {
+                    Set<Program> programs = new HashSet<Program>();
+                    programs.add( program );
+                    mappedPrograms.put( organisationUnit.getUid(), programs );
+
+                }
+                else
+                {
+                    mappedPrograms.get( organisationUnit.getUid() ).add( program );
+                }
+            }
+
+            for ( OrganisationUnitGroup organisationUnitGroup : program.getOrganisationUnitGroups() )
+            {
+                for ( OrganisationUnit organisationUnit : organisationUnitGroup.getMembers() )
+                {
+                    organisationUnits.add( organisationUnit );
+
+                    if ( mappedPrograms.get( organisationUnit.getUid() ) == null )
+                    {
+                        Set<Program> programs = new HashSet<Program>();
+                        programs.add( program );
+                        mappedPrograms.put( organisationUnit.getUid(), programs );
+
+                    }
+                    else
+                    {
+                        mappedPrograms.get( organisationUnit.getUid() ).add( program );
+                    }
+                }
+            }
+        }
+
+        for ( OrganisationUnit organisationUnit : organisationUnits )
+        {
+            FormOrganisationUnit formOrganisationUnit = new FormOrganisationUnit();
+            formOrganisationUnit.setId( organisationUnit.getUid() );
+            formOrganisationUnit.setLabel( organisationUnit.getDisplayName() );
+            formOrganisationUnit.setLevel( organisationUnit.getOrganisationUnitLevel() );
+
+            Set<Program> programs = mappedPrograms.get( organisationUnit.getUid() );
+
+            for ( Program program : programs )
+            {
+                FormProgram formProgram = new FormProgram();
+                formProgram.setLabel( program.getDisplayName() );
+                formProgram.setId( program.getUid() );
+
+                formOrganisationUnit.getPrograms().add( formProgram );
+            }
+
+            forms.getOrganisationUnits().put( formOrganisationUnit.getId(), formOrganisationUnit );
+        }
+
+        JacksonUtils.toJson( response.getOutputStream(), forms );
+    }
+
+    @SuppressWarnings( "unchecked" )
+    @RequestMapping( value = { "/forms", "/assignedDataSets" }, produces = { "application/json", "text/*" } )
+    public void getDataSets( HttpServletResponse response ) throws IOException, NotAuthenticatedException
     {
         User currentUser = currentUserService.getCurrentUser();
 
@@ -318,9 +409,10 @@
 
         for ( OrganisationUnit organisationUnit : organisationUnits )
         {
-            FormOrganisationUnit ou = new FormOrganisationUnit();
-            ou.setId( organisationUnit.getUid() );
-            ou.setLabel( organisationUnit.getDisplayName() );
+            FormOrganisationUnit formOrganisationUnit = new FormOrganisationUnit();
+            formOrganisationUnit.setId( organisationUnit.getUid() );
+            formOrganisationUnit.setLabel( organisationUnit.getDisplayName() );
+            formOrganisationUnit.setLevel( organisationUnit.getOrganisationUnitLevel() );
 
             Set<DataSet> dataSets = new HashSet<DataSet>( CollectionUtils.intersection( organisationUnit.getDataSets(), userDataSets ) );
             i18nService.internationalise( dataSets );
@@ -332,15 +424,15 @@
 
                 String uid = dataSet.getUid();
 
-                FormDataSet ds = new FormDataSet();
-                ds.setId( uid );
-                ds.setLabel( dataSet.getDisplayName() );
+                FormDataSet formDataSet = new FormDataSet();
+                formDataSet.setId( uid );
+                formDataSet.setLabel( dataSet.getDisplayName() );
 
                 forms.getForms().put( uid, FormUtils.fromDataSet( dataSet ) );
-                ou.getDataSets().add( ds );
+                formOrganisationUnit.getDataSets().add( formDataSet );
             }
 
-            forms.getOrganisationUnits().put( ou.getId(), ou );
+            forms.getOrganisationUnits().put( formOrganisationUnit.getId(), formOrganisationUnit );
         }
 
         JacksonUtils.toJson( response.getOutputStream(), forms );

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/webdomain/FormDataSet.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/webdomain/FormDataSet.java	2012-10-15 10:20:17 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/webdomain/FormDataSet.java	2013-08-20 11:01:56 +0000
@@ -1,7 +1,7 @@
 package org.hisp.dhis.api.webdomain;
 
-/**
- * Copyright (c) 2004-2012, University of Oslo
+/*
+ * Copyright (c) 2004-2013, University of Oslo
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without

=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/webdomain/FormOrganisationUnit.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/webdomain/FormOrganisationUnit.java	2012-10-15 10:20:17 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/webdomain/FormOrganisationUnit.java	2013-08-20 11:01:56 +0000
@@ -44,8 +44,12 @@
 
     private String label;
 
+    private Integer level;
+
     private Set<FormDataSet> dataSets = new HashSet<FormDataSet>();
 
+    private Set<FormProgram> programs = new HashSet<FormProgram>();
+
     public FormOrganisationUnit()
     {
     }
@@ -73,6 +77,17 @@
     }
 
     @JsonProperty
+    public Integer getLevel()
+    {
+        return level;
+    }
+
+    public void setLevel( Integer level )
+    {
+        this.level = level;
+    }
+
+    @JsonProperty
     public Set<FormDataSet> getDataSets()
     {
         return dataSets;
@@ -82,4 +97,15 @@
     {
         this.dataSets = dataSets;
     }
+
+    @JsonProperty
+    public Set<FormProgram> getPrograms()
+    {
+        return programs;
+    }
+
+    public void setPrograms( Set<FormProgram> programs )
+    {
+        this.programs = programs;
+    }
 }

=== added file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/webdomain/FormProgram.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/webdomain/FormProgram.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/webdomain/FormProgram.java	2013-08-20 11:01:56 +0000
@@ -0,0 +1,66 @@
+package org.hisp.dhis.api.webdomain;
+
+/*
+ * Copyright (c) 2004-2013, 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.fasterxml.jackson.annotation.JsonProperty;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+public class FormProgram
+{
+    private String id;
+
+    private String label;
+
+    public FormProgram()
+    {
+    }
+
+    @JsonProperty
+    public String getId()
+    {
+        return id;
+    }
+
+    public void setId( String id )
+    {
+        this.id = id;
+    }
+
+    @JsonProperty
+    public String getLabel()
+    {
+        return label;
+    }
+
+    public void setLabel( String label )
+    {
+        this.label = label;
+    }
+}