~registry/dhis2-academy/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package org.hisp.dhis.api.mobile.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.hisp.dhis.api.mobile.NotAllowedException;
import org.hisp.dhis.api.mobile.model.DataStreamSerializable;
import org.hisp.dhis.api.mobile.model.MobileOrgUnitLinks;
import org.hisp.dhis.api.mobile.model.OrgUnits;
import org.hisp.dhis.organisationunit.OrganisationUnit;
import org.hisp.dhis.user.CurrentUserService;
import org.hisp.dhis.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.web.util.UrlUtils;
import org.springframework.stereotype.Controller;
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.ResponseBody;

@Controller
@RequestMapping( value = "/mobile" )
public class MobileClientController
    extends AbstractMobileController
{
    @Autowired
    private CurrentUserService currentUserService;

    @RequestMapping( method = RequestMethod.GET )
    @ResponseBody
    public OrgUnits getOrgUnitsForUser2_8( HttpServletRequest request )
        throws NotAllowedException
    {
        User user = currentUserService.getCurrentUser();

        if ( user == null )
        {
            throw NotAllowedException.NO_USER;
        }

        Collection<OrganisationUnit> units = user.getOrganisationUnits();

        List<MobileOrgUnitLinks> unitList = new ArrayList<MobileOrgUnitLinks>();
        for ( OrganisationUnit unit : units )
        {
            unitList.add( getOrgUnit( unit, request ) );
        }
        OrgUnits orgUnits = new OrgUnits( unitList );
        orgUnits.setClientVersion( DataStreamSerializable.TWO_POINT_EIGHT );
        return orgUnits;
    }
    
    @RequestMapping( method = RequestMethod.GET, value = "/{version}" )
    @ResponseBody
    public OrgUnits getOrgUnitsForUser( HttpServletRequest request, @PathVariable String version )
        throws NotAllowedException
    {
        User user = currentUserService.getCurrentUser();

        if ( user == null )
        {
            throw NotAllowedException.NO_USER;
        }

        Collection<OrganisationUnit> units = user.getOrganisationUnits();

        List<MobileOrgUnitLinks> unitList = new ArrayList<MobileOrgUnitLinks>();
        for ( OrganisationUnit unit : units )
        {
            unitList.add( getOrgUnit( unit, request ) );
        }
        OrgUnits orgUnits = new OrgUnits( unitList );
        orgUnits.setClientVersion( DataStreamSerializable.TWO_POINT_NINE );
        return orgUnits;
    }

    private MobileOrgUnitLinks getOrgUnit( OrganisationUnit unit, HttpServletRequest request )
    {
        MobileOrgUnitLinks orgUnit = new MobileOrgUnitLinks();

        orgUnit.setId( unit.getId() );
        orgUnit.setName( unit.getShortName() );

        orgUnit.setDownloadAllUrl( getUrl( request, unit.getId(), "all" ) );
        orgUnit.setUpdateActivityPlanUrl( getUrl( request, unit.getId(), "activitiyplan" ) );
        orgUnit.setUploadFacilityReportUrl( getUrl( request, unit.getId(), "dataSets" ) );
        orgUnit.setUploadActivityReportUrl( getUrl( request, unit.getId(), "activities" ) );
        orgUnit.setUpdateDataSetUrl( getUrl( request, unit.getId(), "updateDataSets" ) );
        orgUnit.setChangeUpdateDataSetLangUrl( getUrl( request, unit.getId(), "changeLanguageDataSet" ) );
        orgUnit.setSearchUrl( getUrl( request, unit.getId(), "search" ) );

        // generate URL for download new version
        String full = UrlUtils.buildFullRequestUrl( request );
        String root = full.substring( 0, full.length() - UrlUtils.buildRequestUrl( request ).length() );
        String updateNewVersionUrl = root + "/dhis-web-api-mobile/updateClient.action";
        orgUnit.setUpdateNewVersionUrl( updateNewVersionUrl );
        return orgUnit;
    }

    private static String getUrl( HttpServletRequest request, int id, String path )
    {
        String url = UrlUtils.buildFullRequestUrl( request );
        url = url + "/orgUnits/" + id + "/" + path;
        return url;
    }
}