~dhis2-academy/dhis2-academy/android

« back to all changes in this revision

Viewing changes to dhis-mobile/dhis-web-webservice/src/main/java/org/hisp/dhis/web/webservice/resources/MobileOrgUnitResource.java

  • Committer: Kristian Haga Karstensen
  • Date: 2010-11-23 22:21:12 UTC
  • Revision ID: kristhk@ifi.uio.no-20101123222112-ulzyu7kpg9kmfv6l
Added some resources for getting datasets and organisation units. Not finished, but it's working a bit - data exchange format and more functionality is needed (and something needs to be changed). Also reverted DataSet in dhis-api, since we're not supposed to change things here (also breaks build of dhis-2)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.hisp.dhis.web.webservice.resources;
 
2
 
 
3
import java.util.ArrayList;
 
4
import java.util.Collection;
 
5
import java.util.List;
 
6
 
 
7
import javax.ws.rs.GET;
 
8
import javax.ws.rs.Path;
 
9
import javax.ws.rs.PathParam;
 
10
import javax.ws.rs.Produces;
 
11
 
 
12
import org.hisp.dhis.organisationunit.OrganisationUnit;
 
13
import org.hisp.dhis.web.webservice.model.OrgUnitIdentifier;
 
14
import org.hisp.dhis.web.webservice.service.DataService;
 
15
import org.springframework.beans.factory.annotation.Autowired;
 
16
/***
 
17
 * Resource for providing the Organisation units (Name, Id) to a mobile client.
 
18
 * @author Kristian Haga Karstensen <kristhk@ifi.uio.no>
 
19
 *
 
20
 */
 
21
@Path("/orgunits/")
 
22
public class MobileOrgUnitResource {
 
23
 
 
24
        @Autowired
 
25
        DataService ds;
 
26
        
 
27
 
 
28
        /**
 
29
         * Returns a mobile-friendly list of the root org units (OrgUnitIdentifiers)
 
30
         */
 
31
        @Produces("text/html")
 
32
        @GET
 
33
        public String getMobileOrgUnitRoot() {
 
34
                Collection<OrganisationUnit> orgUnits = ds.getRootOrgUnits();
 
35
                List<OrgUnitIdentifier> mobileOrgUnits = new ArrayList<OrgUnitIdentifier>();
 
36
                for (OrganisationUnit ou : orgUnits) {
 
37
                        mobileOrgUnits.add(new OrgUnitIdentifier(ou.getId(), ou.getName()));
 
38
                }
 
39
                
 
40
                /* Return the org units as some sort of list */
 
41
                String result = "";
 
42
                for (OrgUnitIdentifier ouId : mobileOrgUnits) {
 
43
                        result +=ouId.getId()+", "+ouId.getName()+" \n";
 
44
                }
 
45
                
 
46
                return result;
 
47
        }
 
48
        @Produces("text/html")
 
49
        @GET
 
50
        @Path("{id}")
 
51
        public String getMobileOrgUnitChildren(@PathParam("id") String id) {
 
52
                Collection<OrganisationUnit> orgUnits = ds.getChildrenFromOrgUnit(Integer.parseInt(id));
 
53
                List<OrgUnitIdentifier> mobileOrgUnits = new ArrayList<OrgUnitIdentifier>();
 
54
                
 
55
                for(OrganisationUnit ou : orgUnits) {
 
56
                        mobileOrgUnits.add(new OrgUnitIdentifier(ou.getId(),ou.getName()));
 
57
                }
 
58
                
 
59
                String result = "";
 
60
                for (OrgUnitIdentifier ouId : mobileOrgUnits) {
 
61
                        result += ouId.getId()+", "+ouId.getName()+" \n";
 
62
                }
 
63
                
 
64
                return result;
 
65
        }
 
66
}