1
package org.hisp.dhis.api.mobile.controller;
3
import java.util.ArrayList;
4
import java.util.Collection;
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;
23
@RequestMapping( value = "/mobile" )
24
public class MobileClientController
25
extends AbstractMobileController
28
private CurrentUserService currentUserService;
30
@RequestMapping( method = RequestMethod.GET )
32
public OrgUnits getOrgUnitsForUser2_8( HttpServletRequest request )
33
throws NotAllowedException
35
User user = currentUserService.getCurrentUser();
39
throw NotAllowedException.NO_USER;
42
Collection<OrganisationUnit> units = user.getOrganisationUnits();
44
List<MobileOrgUnitLinks> unitList = new ArrayList<MobileOrgUnitLinks>();
45
for ( OrganisationUnit unit : units )
47
unitList.add( getOrgUnit( unit, request ) );
49
OrgUnits orgUnits = new OrgUnits( unitList );
50
orgUnits.setClientVersion( DataStreamSerializable.TWO_POINT_EIGHT );
54
@RequestMapping( method = RequestMethod.GET, value = "/{version}" )
56
public OrgUnits getOrgUnitsForUser( HttpServletRequest request, @PathVariable String version )
57
throws NotAllowedException
59
User user = currentUserService.getCurrentUser();
63
throw NotAllowedException.NO_USER;
66
Collection<OrganisationUnit> units = user.getOrganisationUnits();
68
List<MobileOrgUnitLinks> unitList = new ArrayList<MobileOrgUnitLinks>();
69
for ( OrganisationUnit unit : units )
71
unitList.add( getOrgUnit( unit, request ) );
73
OrgUnits orgUnits = new OrgUnits( unitList );
74
orgUnits.setClientVersion( DataStreamSerializable.TWO_POINT_NINE );
78
private MobileOrgUnitLinks getOrgUnit( OrganisationUnit unit, HttpServletRequest request )
80
MobileOrgUnitLinks orgUnit = new MobileOrgUnitLinks();
82
orgUnit.setId( unit.getId() );
83
orgUnit.setName( unit.getShortName() );
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" ) );
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 );
101
private static String getUrl( HttpServletRequest request, int id, String path )
103
String url = UrlUtils.buildFullRequestUrl( request );
104
url = url + "/orgUnits/" + id + "/" + path;