dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #20310
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 9211: FRED-API: moved ou => facility conversion logic out of the controller
------------------------------------------------------------
revno: 9211
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2012-12-07 22:21:53 +0300
message:
FRED-API: moved ou => facility conversion logic out of the controller
added:
dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/OrganisationUnitToFacilityConverter.java
modified:
dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityController.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-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityController.java'
--- dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityController.java 2012-12-07 16:00:59 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityController.java 2012-12-07 19:21:53 +0000
@@ -29,15 +29,15 @@
import org.hisp.dhis.common.DeleteNotAllowedException;
import org.hisp.dhis.common.comparator.IdentifiableObjectNameComparator;
-import org.hisp.dhis.dataset.DataSet;
import org.hisp.dhis.hierarchy.HierarchyViolationException;
import org.hisp.dhis.organisationunit.OrganisationUnit;
import org.hisp.dhis.organisationunit.OrganisationUnitService;
import org.hisp.dhis.web.webapi.v1.domain.Facilities;
import org.hisp.dhis.web.webapi.v1.domain.Facility;
-import org.hisp.dhis.web.webapi.v1.utils.GeoUtils;
+import org.hisp.dhis.web.webapi.v1.utils.OrganisationUnitToFacilityConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.core.convert.converter.Converter;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
@@ -47,28 +47,32 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
/**
* @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
*/
-@Controller(value = "facility-controller-" + FredController.PREFIX)
-@RequestMapping(FacilityController.RESOURCE_PATH)
+@Controller( value = "facility-controller-" + FredController.PREFIX )
+@RequestMapping( FacilityController.RESOURCE_PATH )
public class FacilityController
{
public static final String RESOURCE_PATH = "/" + FredController.PREFIX + "/facilities";
@Autowired
- @Qualifier("org.hisp.dhis.organisationunit.OrganisationUnitService")
+ @Qualifier( "org.hisp.dhis.organisationunit.OrganisationUnitService" )
private OrganisationUnitService organisationUnitService;
+ private Converter<OrganisationUnit, Facility> convertToFacility = new OrganisationUnitToFacilityConverter();
+
//--------------------------------------------------------------------------
// GET HTML
//--------------------------------------------------------------------------
- @RequestMapping(value = "", method = RequestMethod.GET)
+ @RequestMapping( value = "", method = RequestMethod.GET )
public String readFacilities( Model model )
{
Facilities facilities = new Facilities();
@@ -78,7 +82,7 @@
for ( OrganisationUnit organisationUnit : allOrganisationUnits )
{
- Facility facility = convertToFacility( organisationUnit );
+ Facility facility = convertToFacility.convert( organisationUnit );
facilities.getFacilities().add( facility );
}
@@ -90,11 +94,11 @@
return FredController.PREFIX + "/layout";
}
- @RequestMapping(value = "/{id}", method = RequestMethod.GET)
+ @RequestMapping( value = "/{id}", method = RequestMethod.GET )
public String readFacility( Model model, @PathVariable String id )
{
OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit( id );
- Facility facility = convertToFacility( organisationUnit );
+ Facility facility = convertToFacility.convert( organisationUnit );
model.addAttribute( "entity", facility );
model.addAttribute( "baseUrl", linkTo( FredController.class ).toString() );
@@ -108,7 +112,7 @@
// POST JSON
//--------------------------------------------------------------------------
- @RequestMapping(value = "/{id}", method = RequestMethod.POST)
+ @RequestMapping( value = "/{id}", method = RequestMethod.POST )
public ResponseEntity<Void> createFacility()
{
return new ResponseEntity<Void>( HttpStatus.OK );
@@ -118,7 +122,7 @@
// PUT JSON
//--------------------------------------------------------------------------
- @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
+ @RequestMapping( value = "/{id}", method = RequestMethod.PUT )
public ResponseEntity<Void> updateFacility()
{
return new ResponseEntity<Void>( HttpStatus.OK );
@@ -128,7 +132,7 @@
// DELETE JSON
//--------------------------------------------------------------------------
- @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
+ @RequestMapping( value = "/{id}", method = RequestMethod.DELETE )
public ResponseEntity<Void> deleteFacility( @PathVariable String id ) throws HierarchyViolationException
{
OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit( id );
@@ -144,58 +148,6 @@
}
//--------------------------------------------------------------------------
- // HELPERS
- //--------------------------------------------------------------------------
-
- private Facility convertToFacility( OrganisationUnit organisationUnit )
- {
- Facility facility = new Facility();
- facility.setId( organisationUnit.getUid() );
- facility.setName( organisationUnit.getDisplayName() );
- facility.setActive( organisationUnit.isActive() );
- facility.setCreatedAt( organisationUnit.getLastUpdated() );
- facility.setUpdatedAt( organisationUnit.getLastUpdated() );
- facility.setUrl( linkTo( FacilityController.class ).slash( facility.getId() ).toString() );
-
- if ( organisationUnit.getFeatureType() != null && organisationUnit.getFeatureType().equalsIgnoreCase( "POINT" )
- && organisationUnit.getCoordinates() != null )
- {
- GeoUtils.Coordinates coordinates = GeoUtils.parseCoordinates( organisationUnit.getCoordinates() );
- facility.getCoordinates().add( coordinates.lat );
- facility.getCoordinates().add( coordinates.lng );
- }
-
- if ( organisationUnit.getParent() != null )
- {
- facility.getProperties().put( "parent", organisationUnit.getParent().getUid() );
- }
-
- if ( organisationUnit.getCode() != null )
- {
- Map<String, String> codeMap = new HashMap<String, String>();
- codeMap.put( "agency", "DHIS2" );
- codeMap.put( "context", "DHIS2_CODE" );
- codeMap.put( "id", organisationUnit.getCode() );
-
- facility.getIdentifiers().add( codeMap );
- }
-
- if ( !organisationUnit.getDataSets().isEmpty() )
- {
- List<String> dataSets = new ArrayList<String>();
-
- for ( DataSet dataSet : organisationUnit.getDataSets() )
- {
- dataSets.add( dataSet.getUid() );
- }
-
- facility.getProperties().put( "dataSets", dataSets );
- }
-
- return facility;
- }
-
- //--------------------------------------------------------------------------
// EXCEPTION HANDLERS
//--------------------------------------------------------------------------
=== added file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/OrganisationUnitToFacilityConverter.java'
--- dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/OrganisationUnitToFacilityConverter.java 1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/OrganisationUnitToFacilityConverter.java 2012-12-07 19:21:53 +0000
@@ -0,0 +1,96 @@
+package org.hisp.dhis.web.webapi.v1.utils;
+
+/*
+ * Copyright (c) 2004-2012, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * * Neither the name of the HISP project nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import org.hisp.dhis.dataset.DataSet;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.web.webapi.v1.controller.FacilityController;
+import org.hisp.dhis.web.webapi.v1.domain.Facility;
+import org.springframework.core.convert.converter.Converter;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+public class OrganisationUnitToFacilityConverter implements Converter<OrganisationUnit, Facility>
+{
+ @Override
+ public Facility convert( OrganisationUnit organisationUnit )
+ {
+ Facility facility = new Facility();
+ facility.setId( organisationUnit.getUid() );
+ facility.setName( organisationUnit.getDisplayName() );
+ facility.setActive( organisationUnit.isActive() );
+ facility.setCreatedAt( organisationUnit.getLastUpdated() );
+ facility.setUpdatedAt( organisationUnit.getLastUpdated() );
+ facility.setUrl( linkTo( FacilityController.class ).slash( facility.getId() ).toString() );
+
+ if ( organisationUnit.getFeatureType() != null && organisationUnit.getFeatureType().equalsIgnoreCase( "POINT" )
+ && organisationUnit.getCoordinates() != null )
+ {
+ GeoUtils.Coordinates coordinates = GeoUtils.parseCoordinates( organisationUnit.getCoordinates() );
+ facility.getCoordinates().add( coordinates.lat );
+ facility.getCoordinates().add( coordinates.lng );
+ }
+
+ if ( organisationUnit.getParent() != null )
+ {
+ facility.getProperties().put( "parent", organisationUnit.getParent().getUid() );
+ }
+
+ if ( organisationUnit.getCode() != null )
+ {
+ Map<String, String> codeMap = new HashMap<String, String>();
+ codeMap.put( "agency", "DHIS2" );
+ codeMap.put( "context", "DHIS2_CODE" );
+ codeMap.put( "id", organisationUnit.getCode() );
+
+ facility.getIdentifiers().add( codeMap );
+ }
+
+ if ( !organisationUnit.getDataSets().isEmpty() )
+ {
+ List<String> dataSets = new ArrayList<String>();
+
+ for ( DataSet dataSet : organisationUnit.getDataSets() )
+ {
+ dataSets.add( dataSet.getUid() );
+ }
+
+ facility.getProperties().put( "dataSets", dataSets );
+ }
+
+ return facility;
+ }
+}