← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 18043: support field-filtering in /api/me endpoint

 

------------------------------------------------------------
revno: 18043
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2015-01-19 15:13:30 +0700
message:
  support field-filtering in /api/me endpoint
modified:
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/user/CurrentUserController.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/webapi/controller/user/CurrentUserController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/user/CurrentUserController.java	2015-01-17 07:41:26 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/user/CurrentUserController.java	2015-01-19 08:13:30 +0000
@@ -31,6 +31,7 @@
 import com.google.common.collect.Lists;
 import org.apache.commons.collections.CollectionUtils;
 import org.hisp.dhis.acl.AclService;
+import org.hisp.dhis.common.DxfNamespaces;
 import org.hisp.dhis.common.IdentifiableObjectManager;
 import org.hisp.dhis.common.view.DetailedView;
 import org.hisp.dhis.dashboard.DashboardItem;
@@ -39,6 +40,7 @@
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.dataset.DataSet;
 import org.hisp.dhis.dataset.DataSetService;
+import org.hisp.dhis.dxf2.fieldfilter.FieldFilterService;
 import org.hisp.dhis.dxf2.render.RenderService;
 import org.hisp.dhis.dxf2.utils.JacksonUtils;
 import org.hisp.dhis.i18n.I18nService;
@@ -46,6 +48,8 @@
 import org.hisp.dhis.interpretation.InterpretationService;
 import org.hisp.dhis.message.MessageConversation;
 import org.hisp.dhis.message.MessageService;
+import org.hisp.dhis.node.types.CollectionNode;
+import org.hisp.dhis.node.types.RootNode;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
 import org.hisp.dhis.program.Program;
@@ -58,6 +62,7 @@
 import org.hisp.dhis.user.UserSettingService;
 import org.hisp.dhis.webapi.controller.exception.FilterTooShortException;
 import org.hisp.dhis.webapi.controller.exception.NotAuthenticatedException;
+import org.hisp.dhis.webapi.service.ContextService;
 import org.hisp.dhis.webapi.utils.ContextUtils;
 import org.hisp.dhis.webapi.utils.ContextUtils.CacheStrategy;
 import org.hisp.dhis.webapi.utils.FormUtils;
@@ -76,12 +81,14 @@
 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.ResponseBody;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
@@ -148,9 +155,17 @@
     @Autowired
     private RenderService renderService;
 
-    @RequestMapping( produces = { "application/json", "text/*" } )
-    public void getCurrentUser( HttpServletResponse response ) throws Exception
+    @Autowired
+    protected ContextService contextService;
+
+    @Autowired
+    protected FieldFilterService fieldFilterService;
+
+    @RequestMapping
+    public @ResponseBody RootNode getCurrentUser( HttpServletResponse response ) throws Exception
     {
+        List<String> fields = Lists.newArrayList( contextService.getParameterValues( "fields" ) );
+
         User currentUser = currentUserService.getCurrentUser();
 
         if ( currentUser == null )
@@ -158,8 +173,18 @@
             throw new NotAuthenticatedException();
         }
 
-        response.setContentType( MediaType.APPLICATION_JSON_VALUE );
-        renderService.toJson( response.getOutputStream(), currentUser, DetailedView.class );
+        if ( fields.isEmpty() )
+        {
+            fields.add( ":all" );
+        }
+
+        CollectionNode collectionNode = fieldFilterService.filter( User.class, Arrays.asList( currentUser ), fields );
+
+        RootNode rootNode = new RootNode( collectionNode.getChildren().get( 0 ) );
+        rootNode.setDefaultNamespace( DxfNamespaces.DXF_2_0 );
+        rootNode.setNamespace( DxfNamespaces.DXF_2_0 );
+
+        return rootNode;
     }
 
     @RequestMapping( value = "/dashboards", produces = { "application/json", "text/*" } )