~registry/dhis2-academy/trunk

« back to all changes in this revision

Viewing changes to dhis-2/dhis-web/dhis-web-api-mobile/src/main/java/org/hisp/dhis/api/mobile/controller/MobileClientController.java

  • Committer: Abyot Asalefew Gizaw
  • Date: 2012-10-12 00:08:27 UTC
  • Revision ID: abyota@gmail.com-20121012000827-qqmmfllx0s5vtkqi
Updating the acadmy trunk with the main DHIS2 trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.hisp.dhis.api.mobile.controller;
 
2
 
 
3
import java.util.ArrayList;
 
4
import java.util.Collection;
 
5
import java.util.List;
 
6
import javax.servlet.http.HttpServletRequest;
 
7
import org.hisp.dhis.api.mobile.NotAllowedException;
 
8
import org.hisp.dhis.api.mobile.model.DataStreamSerializable;
 
9
import org.hisp.dhis.api.mobile.model.MobileOrgUnitLinks;
 
10
import org.hisp.dhis.api.mobile.model.OrgUnits;
 
11
import org.hisp.dhis.organisationunit.OrganisationUnit;
 
12
import org.hisp.dhis.user.CurrentUserService;
 
13
import org.hisp.dhis.user.User;
 
14
import org.springframework.beans.factory.annotation.Autowired;
 
15
import org.springframework.security.web.util.UrlUtils;
 
16
import org.springframework.stereotype.Controller;
 
17
import org.springframework.web.bind.annotation.PathVariable;
 
18
import org.springframework.web.bind.annotation.RequestMapping;
 
19
import org.springframework.web.bind.annotation.RequestMethod;
 
20
import org.springframework.web.bind.annotation.ResponseBody;
 
21
 
 
22
@Controller
 
23
@RequestMapping( value = "/mobile" )
 
24
public class MobileClientController
 
25
    extends AbstractMobileController
 
26
{
 
27
    @Autowired
 
28
    private CurrentUserService currentUserService;
 
29
 
 
30
    @RequestMapping( method = RequestMethod.GET )
 
31
    @ResponseBody
 
32
    public OrgUnits getOrgUnitsForUser2_8( HttpServletRequest request )
 
33
        throws NotAllowedException
 
34
    {
 
35
        User user = currentUserService.getCurrentUser();
 
36
 
 
37
        if ( user == null )
 
38
        {
 
39
            throw NotAllowedException.NO_USER;
 
40
        }
 
41
 
 
42
        Collection<OrganisationUnit> units = user.getOrganisationUnits();
 
43
 
 
44
        List<MobileOrgUnitLinks> unitList = new ArrayList<MobileOrgUnitLinks>();
 
45
        for ( OrganisationUnit unit : units )
 
46
        {
 
47
            unitList.add( getOrgUnit( unit, request ) );
 
48
        }
 
49
        OrgUnits orgUnits = new OrgUnits( unitList );
 
50
        orgUnits.setClientVersion( DataStreamSerializable.TWO_POINT_EIGHT );
 
51
        return orgUnits;
 
52
    }
 
53
    
 
54
    @RequestMapping( method = RequestMethod.GET, value = "/{version}" )
 
55
    @ResponseBody
 
56
    public OrgUnits getOrgUnitsForUser( HttpServletRequest request, @PathVariable String version )
 
57
        throws NotAllowedException
 
58
    {
 
59
        User user = currentUserService.getCurrentUser();
 
60
 
 
61
        if ( user == null )
 
62
        {
 
63
            throw NotAllowedException.NO_USER;
 
64
        }
 
65
 
 
66
        Collection<OrganisationUnit> units = user.getOrganisationUnits();
 
67
 
 
68
        List<MobileOrgUnitLinks> unitList = new ArrayList<MobileOrgUnitLinks>();
 
69
        for ( OrganisationUnit unit : units )
 
70
        {
 
71
            unitList.add( getOrgUnit( unit, request ) );
 
72
        }
 
73
        OrgUnits orgUnits = new OrgUnits( unitList );
 
74
        orgUnits.setClientVersion( DataStreamSerializable.TWO_POINT_NINE );
 
75
        return orgUnits;
 
76
    }
 
77
 
 
78
    private MobileOrgUnitLinks getOrgUnit( OrganisationUnit unit, HttpServletRequest request )
 
79
    {
 
80
        MobileOrgUnitLinks orgUnit = new MobileOrgUnitLinks();
 
81
 
 
82
        orgUnit.setId( unit.getId() );
 
83
        orgUnit.setName( unit.getShortName() );
 
84
 
 
85
        orgUnit.setDownloadAllUrl( getUrl( request, unit.getId(), "all" ) );
 
86
        orgUnit.setUpdateActivityPlanUrl( getUrl( request, unit.getId(), "activitiyplan" ) );
 
87
        orgUnit.setUploadFacilityReportUrl( getUrl( request, unit.getId(), "dataSets" ) );
 
88
        orgUnit.setUploadActivityReportUrl( getUrl( request, unit.getId(), "activities" ) );
 
89
        orgUnit.setUpdateDataSetUrl( getUrl( request, unit.getId(), "updateDataSets" ) );
 
90
        orgUnit.setChangeUpdateDataSetLangUrl( getUrl( request, unit.getId(), "changeLanguageDataSet" ) );
 
91
        orgUnit.setSearchUrl( getUrl( request, unit.getId(), "search" ) );
 
92
 
 
93
        // generate URL for download new version
 
94
        String full = UrlUtils.buildFullRequestUrl( request );
 
95
        String root = full.substring( 0, full.length() - UrlUtils.buildRequestUrl( request ).length() );
 
96
        String updateNewVersionUrl = root + "/dhis-web-api-mobile/updateClient.action";
 
97
        orgUnit.setUpdateNewVersionUrl( updateNewVersionUrl );
 
98
        return orgUnit;
 
99
    }
 
100
 
 
101
    private static String getUrl( HttpServletRequest request, int id, String path )
 
102
    {
 
103
        String url = UrlUtils.buildFullRequestUrl( request );
 
104
        url = url + "/orgUnits/" + id + "/" + path;
 
105
        return url;
 
106
    }
 
107
}