← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 6927: Added method to web api for searching org units, first for uid, then code, then name, at organisa...

 

------------------------------------------------------------
revno: 6927
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Sun 2012-05-13 18:59:37 +0200
message:
  Added method to web api for searching org units, first for uid, then code, then name, at organisationUnits/search/{query} . If OK we can impl consistently.
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectManager.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/DefaultIdentifiableObjectManager.java
  dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/organisationunit/OrganisationUnitController.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/common/IdentifiableObjectManager.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectManager.java	2012-04-13 14:11:00 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/IdentifiableObjectManager.java	2012-05-13 16:59:37 +0000
@@ -47,6 +47,8 @@
     <T extends IdentifiableObject> T getByCode( Class<T> clazz, String code );
 
     <T extends IdentifiableObject> T getByName( Class<T> clazz, String name );
+    
+    <T extends IdentifiableObject> T search( Class<T> clazz, String query );
 
     <T extends IdentifiableObject> Collection<T> getAll( Class<T> clazz );
 

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/DefaultIdentifiableObjectManager.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/DefaultIdentifiableObjectManager.java	2012-04-23 11:13:41 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/common/DefaultIdentifiableObjectManager.java	2012-05-13 16:59:37 +0000
@@ -168,6 +168,24 @@
     }
 
     @Override
+    public <T extends IdentifiableObject> T search( Class<T> clazz, String query )
+    {
+        T object = get( clazz, query );
+        
+        if ( object == null )
+        {
+            object = getByCode( clazz, query );
+        }
+        
+        if ( object == null )
+        {
+            object = getByName( clazz, query );
+        }
+        
+        return object;
+    }
+    
+    @Override
     @SuppressWarnings( "unchecked" )
     public <T extends IdentifiableObject> Collection<T> getAll( Class<T> clazz )
     {

=== 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	2012-03-27 18:07:36 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/organisationunit/OrganisationUnitController.java	2012-05-13 16:59:37 +0000
@@ -4,6 +4,7 @@
 import org.hisp.dhis.api.utils.IdentifiableObjectParams;
 import org.hisp.dhis.api.utils.ObjectPersister;
 import org.hisp.dhis.api.utils.WebLinkPopulator;
+import org.hisp.dhis.common.IdentifiableObjectManager;
 import org.hisp.dhis.common.Pager;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
@@ -17,6 +18,7 @@
 import org.springframework.web.bind.annotation.PathVariable;
 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.ResponseStatus;
 
 import javax.servlet.http.HttpServletRequest;
@@ -39,6 +41,9 @@
 
     @Autowired
     private ObjectPersister objectPersister;
+    
+    @Autowired
+    private IdentifiableObjectManager identifiableObjectManager;
 
     //-------------------------------------------------------------------------------------------------------
     // GET
@@ -93,6 +98,22 @@
 
         return "organisationUnit";
     }
+    
+    @RequestMapping( value = "/search/{query}", method = RequestMethod.GET )
+    public String searchOrganisationUnit( @PathVariable String query, IdentifiableObjectParams params, Model model, HttpServletRequest request )
+    {
+        OrganisationUnit organisationUnit = identifiableObjectManager.search( OrganisationUnit.class, query );
+        
+        if ( params.hasLinks() )
+        {
+            new WebLinkPopulator( request ).addLinks( organisationUnit );
+        }
+
+        model.addAttribute( "model", organisationUnit );
+        model.addAttribute( "view", "detailed" );
+
+        return "organisationUnit";
+    }
 
     //-------------------------------------------------------------------------------------------------------
     // POST