------------------------------------------------------------ revno: 16206 committer: Lars Helge Overland <larshe...@gmail.com> branch nick: dhis2 timestamp: Mon 2014-07-21 16:01:10 +0200 message: DataSetController, forms, centralized code, better feedback on wrong org unit uid modified: dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DataSetController.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-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DataSetController.java' --- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DataSetController.java 2014-07-07 15:02:50 +0000 +++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/DataSetController.java 2014-07-21 14:01:10 +0000 @@ -145,37 +145,6 @@ JacksonUtils.toJson( response.getOutputStream(), versionMap ); } - @RequestMapping( value = "/{uid}/form", method = RequestMethod.GET, produces = "application/json" ) - public void getFormJson( @PathVariable( "uid" ) String uid, @RequestParam( value = "ou", required = false ) String orgUnit, - @RequestParam( value = "pe", required = false ) String period, HttpServletResponse response ) throws IOException - { - List<DataSet> dataSets = getEntity( uid ); - - if ( dataSets.isEmpty() ) - { - ContextUtils.notFoundResponse( response, "Object not found for uid: " + uid ); - return; - } - - i18nService.internationalise( dataSets.get( 0 ) ); - i18nService.internationalise( dataSets.get( 0 ).getDataElements() ); - i18nService.internationalise( dataSets.get( 0 ).getSections() ); - - Form form = FormUtils.fromDataSet( dataSets.get( 0 ) ); - - if ( orgUnit != null && !orgUnit.isEmpty() && period != null && !period.isEmpty() ) - { - OrganisationUnit ou = manager.get( OrganisationUnit.class, orgUnit ); - Period p = PeriodType.getPeriodFromIsoString( period ); - - Collection<DataValue> dataValues = dataValueService.getDataValues( ou, p, dataSets.get( 0 ).getDataElements() ); - - FormUtils.fillWithDataValues( form, dataValues ); - } - - JacksonUtils.toJson( response.getOutputStream(), form ); - } - @RequestMapping( value = "/{uid}/dataValueSet", method = RequestMethod.GET ) public @ResponseBody RootNode getDvs( @PathVariable( "uid" ) String uid, @RequestParam( value = "orgUnitIdScheme", defaultValue = "ID", required = false ) String orgUnitIdScheme, @@ -197,9 +166,42 @@ return dataValueSetService.getDataValueSetTemplate( dataSets.get( 0 ), pe, orgUnits, comment, orgUnitIdScheme, dataElementIdScheme ); } + @RequestMapping( value = "/{uid}/form", method = RequestMethod.GET, produces = "application/json" ) + public void getFormJson( + @PathVariable( "uid" ) String uid, + @RequestParam( value = "ou", required = false ) String orgUnit, + @RequestParam( value = "pe", required = false ) String period, + HttpServletResponse response ) throws IOException + { + List<DataSet> dataSets = getEntity( uid ); + + if ( dataSets.isEmpty() ) + { + ContextUtils.notFoundResponse( response, "Data set does not exist: " + uid ); + return; + } + + OrganisationUnit ou = null; + + if ( orgUnit != null && ( ou = manager.get( OrganisationUnit.class, orgUnit ) ) == null ) + { + ContextUtils.notFoundResponse( response, "Organisation unit does not exist: " + orgUnit ); + return; + } + + Period pe = PeriodType.getPeriodFromIsoString( period ); + + Form form = getForm( dataSets, ou, pe ); + + JacksonUtils.toJson( response.getOutputStream(), form ); + } + @RequestMapping( value = "/{uid}/form", method = RequestMethod.GET, produces = { "application/xml", "text/xml" } ) - public void getFormXml( @PathVariable( "uid" ) String uid, @RequestParam( value = "ou", required = false ) String orgUnit, - @RequestParam( value = "pe", required = false ) String period, HttpServletResponse response ) throws IOException + public void getFormXml( + @PathVariable( "uid" ) String uid, + @RequestParam( value = "ou", required = false ) String orgUnit, + @RequestParam( value = "pe", required = false ) String period, + HttpServletResponse response ) throws IOException { List<DataSet> dataSets = getEntity( uid ); @@ -209,25 +211,39 @@ return; } + OrganisationUnit ou = null; + + if ( orgUnit != null && ( ou = manager.get( OrganisationUnit.class, orgUnit ) ) == null ) + { + ContextUtils.notFoundResponse( response, "Organisation unit does not exist: " + orgUnit ); + return; + } + + Period pe = PeriodType.getPeriodFromIsoString( period ); + + Form form = getForm( dataSets, ou, pe ); + + JacksonUtils.toXml( response.getOutputStream(), form ); + } + + private Form getForm( List<DataSet> dataSets, OrganisationUnit ou, Period pe ) + { i18nService.internationalise( dataSets.get( 0 ) ); i18nService.internationalise( dataSets.get( 0 ).getDataElements() ); i18nService.internationalise( dataSets.get( 0 ).getSections() ); Form form = FormUtils.fromDataSet( dataSets.get( 0 ) ); - if ( orgUnit != null && !orgUnit.isEmpty() && period != null && !period.isEmpty() ) + if ( ou != null && pe != null ) { - OrganisationUnit ou = manager.get( OrganisationUnit.class, orgUnit ); i18nService.internationalise( ou ); - Period p = PeriodType.getPeriodFromIsoString( period ); - - Collection<DataValue> dataValues = dataValueService.getDataValues( ou, p, dataSets.get( 0 ).getDataElements() ); + Collection<DataValue> dataValues = dataValueService.getDataValues( ou, pe, dataSets.get( 0 ).getDataElements() ); FormUtils.fillWithDataValues( form, dataValues ); } - - JacksonUtils.toXml( response.getOutputStream(), form ); + + return form; } @RequestMapping( value = "/{uid}/customDataEntryForm", method = { RequestMethod.PUT, RequestMethod.POST }, consumes = "text/html" )
_______________________________________________ Mailing list: https://launchpad.net/~dhis2-devs Post to : dhis2-devs@lists.launchpad.net Unsubscribe : https://launchpad.net/~dhis2-devs More help : https://help.launchpad.net/ListHelp