=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/DataSet.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/DataSet.java 2012-12-04 15:27:28 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataset/DataSet.java 2012-12-08 16:07:13 +0000 @@ -389,7 +389,9 @@ @Override public String toString() { - return "[" + name + "]"; + return "DataSet{" + + "name=" + name + + '}'; } // ------------------------------------------------------------------------- === 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-08 08:31:02 +0000 +++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityController.java 2012-12-08 16:07:13 +0000 @@ -34,21 +34,25 @@ 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.ValidationUtils; +import org.hisp.dhis.web.webapi.v1.validation.group.UpdateSequence; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.core.convert.ConversionService; import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.ExceptionHandler; -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.*; +import javax.validation.ConstraintViolation; +import javax.validation.Validator; +import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Set; import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo; @@ -69,6 +73,9 @@ @Qualifier( "conversionService" ) private ConversionService conversionService; + @Autowired + private Validator validator; + //-------------------------------------------------------------------------- // GET HTML //-------------------------------------------------------------------------- @@ -115,20 +122,65 @@ // POST JSON //-------------------------------------------------------------------------- - @RequestMapping( value = "/{id}", method = RequestMethod.POST ) - public ResponseEntity createFacility() + @RequestMapping( value = "", method = RequestMethod.POST ) + public ResponseEntity createFacility( @RequestBody Facility facility ) throws IOException { - return new ResponseEntity( HttpStatus.OK ); + OrganisationUnit organisationUnit = conversionService.convert( facility, OrganisationUnit.class ); + + return new ResponseEntity( "ok", HttpStatus.OK ); + + /* + Set> constraintViolations = validator.validate( facility, CreateSequence.class ); + + String json = ValidationUtils.constraintViolationsToJson( constraintViolations ); + + if ( constraintViolations.isEmpty() ) + { + return new ResponseEntity( json, HttpStatus.OK ); + } + else + { + return new ResponseEntity( json, HttpStatus.UNPROCESSABLE_ENTITY ); + } + */ } //-------------------------------------------------------------------------- // PUT JSON //-------------------------------------------------------------------------- - @RequestMapping( value = "/{id}", method = RequestMethod.PUT ) - public ResponseEntity updateFacility() + @RequestMapping( value = "/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE ) + public ResponseEntity updateFacility( @PathVariable String id, @RequestBody Facility facility ) throws IOException { - return new ResponseEntity( HttpStatus.OK ); + facility.setId( id ); + OrganisationUnit organisationUnit = conversionService.convert( facility, OrganisationUnit.class ); + + Set> constraintViolations = validator.validate( facility, UpdateSequence.class ); + + String json = ValidationUtils.constraintViolationsToJson( constraintViolations ); + + if ( constraintViolations.isEmpty() ) + { + OrganisationUnit ou = organisationUnitService.getOrganisationUnit( facility.getId() ); + + ou.setName( organisationUnit.getName() ); + ou.setShortName( organisationUnit.getShortName() ); + + ou.setFeatureType( organisationUnit.getFeatureType() ); + ou.setCoordinates( organisationUnit.getCoordinates() ); + ou.setDataSets( organisationUnit.getDataSets() ); + ou.setParent( organisationUnit.getParent() ); + + ou.setActive( organisationUnit.isActive() ); + + organisationUnitService.updateOrganisationUnit( ou ); + + return new ResponseEntity( json, HttpStatus.OK ); + } + else + { + return new ResponseEntity( json, HttpStatus.UNPROCESSABLE_ENTITY ); + } } //-------------------------------------------------------------------------- === modified file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/GeoUtils.java' --- dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/GeoUtils.java 2012-12-08 11:49:49 +0000 +++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/GeoUtils.java 2012-12-08 16:07:13 +0000 @@ -36,23 +36,68 @@ */ final public class GeoUtils { + public static enum CoordinateOrder + { + COORDINATE_LATLNG, + COORDINATE_LNGLAT + } + public static class Coordinates { public Double lat = 0.0d; public Double lng = 0.0d; + + @Override + public String toString() + { + return "Coordinates{" + + "lat=" + lat + + ", lng=" + lng + + '}'; + } } + // helper for most common case, our internal lnglat to latlng public static Coordinates parseCoordinates( String coordinatesString ) { + return parseCoordinates( coordinatesString, CoordinateOrder.COORDINATE_LNGLAT, CoordinateOrder.COORDINATE_LATLNG ); + } + + public static Coordinates parseCoordinates( String coordinatesString, CoordinateOrder from, CoordinateOrder to ) + { Coordinates coordinates = new Coordinates(); try { List list = new ObjectMapper().readValue( coordinatesString, List.class ); - coordinates.lat = convertToDouble( list.get( 1 ) ); - coordinates.lng = convertToDouble( list.get( 0 ) ); + if ( from == CoordinateOrder.COORDINATE_LATLNG ) + { + if ( to == CoordinateOrder.COORDINATE_LATLNG ) + { + coordinates.lat = convertToDouble( list.get( 0 ) ); + coordinates.lng = convertToDouble( list.get( 1 ) ); + } + else if ( to == CoordinateOrder.COORDINATE_LNGLAT ) + { + coordinates.lat = convertToDouble( list.get( 1 ) ); + coordinates.lng = convertToDouble( list.get( 0 ) ); + } + } + else if ( from == CoordinateOrder.COORDINATE_LNGLAT ) + { + if ( to == CoordinateOrder.COORDINATE_LATLNG ) + { + coordinates.lat = convertToDouble( list.get( 0 ) ); + coordinates.lng = convertToDouble( list.get( 1 ) ); + } + else if ( to == CoordinateOrder.COORDINATE_LNGLAT ) + { + coordinates.lat = convertToDouble( list.get( 0 ) ); + coordinates.lng = convertToDouble( list.get( 1 ) ); + } + } } catch ( Exception ignored ) { === added file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/ToOrganisationUnitConverter.java' --- dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/ToOrganisationUnitConverter.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/ToOrganisationUnitConverter.java 2012-12-08 16:07:13 +0000 @@ -0,0 +1,93 @@ +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.dataset.DataSetService; +import org.hisp.dhis.organisationunit.OrganisationUnit; +import org.hisp.dhis.organisationunit.OrganisationUnitService; +import org.hisp.dhis.web.webapi.v1.domain.Facility; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.core.convert.converter.Converter; +import org.springframework.stereotype.Component; + +import java.util.Collection; + +/** + * @author Morten Olav Hansen + */ +@Component +public class ToOrganisationUnitConverter implements Converter +{ + @Autowired + @Qualifier( "org.hisp.dhis.organisationunit.OrganisationUnitService" ) + private OrganisationUnitService organisationUnitService; + + @Autowired + @Qualifier( "org.hisp.dhis.dataset.DataSetService" ) + private DataSetService dataSetService; + + @Override + public OrganisationUnit convert( Facility facility ) + { + OrganisationUnit organisationUnit = new OrganisationUnit(); + + organisationUnit.setUid( facility.getId() ); + organisationUnit.setName( facility.getName() ); + + if ( facility.getName().length() > 49 ) + { + organisationUnit.setShortName( facility.getName().substring( 0, 49 ) ); + } + else + { + organisationUnit.setShortName( facility.getName() ); + } + + organisationUnit.setActive( facility.getActive() ); + organisationUnit.setParent( organisationUnitService.getOrganisationUnit( (String) facility.getProperties().get( "parent" ) ) ); + + Collection dataSets = (Collection) facility.getProperties().get( "dataSets" ); + + for ( String uid : dataSets ) + { + DataSet dataSet = dataSetService.getDataSet( uid ); + organisationUnit.getDataSets().add( dataSet ); + } + + organisationUnit.setFeatureType( OrganisationUnit.FEATURETYPE_POINT ); + + GeoUtils.Coordinates coordinates = GeoUtils.parseCoordinates( + facility.getCoordinates().toString(), GeoUtils.CoordinateOrder.COORDINATE_LATLNG, GeoUtils.CoordinateOrder.COORDINATE_LNGLAT ); + + organisationUnit.setCoordinates( String.format( "[%f, %f]", coordinates.lng, coordinates.lat ) ); + + return organisationUnit; + } +} === modified file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/resources/META-INF/dhis/webapi-fred.xml' --- dhis-2/dhis-web/dhis-web-api-fred/src/main/resources/META-INF/dhis/webapi-fred.xml 2012-12-08 08:31:02 +0000 +++ dhis-2/dhis-web/dhis-web-api-fred/src/main/resources/META-INF/dhis/webapi-fred.xml 2012-12-08 16:07:13 +0000 @@ -15,6 +15,7 @@ + === modified file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/webapp/WEB-INF/api-fred-velocity/v1/facilities.vm' --- dhis-2/dhis-web/dhis-web-api-fred/src/main/webapp/WEB-INF/api-fred-velocity/v1/facilities.vm 2012-12-07 14:16:37 +0000 +++ dhis-2/dhis-web/dhis-web-api-fred/src/main/webapp/WEB-INF/api-fred-velocity/v1/facilities.vm 2012-12-08 16:07:13 +0000 @@ -53,9 +53,7 @@ - - @@ -63,26 +61,20 @@ #foreach( $facility in $entity.facilities ) - -
ID NameActive Actions
$facility.id $facility.name - #if( $facility.active ) - - #else - - #end -
- - + #else + + #end +
=== modified file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/webapp/WEB-INF/api-fred-velocity/v1/facility.vm' --- dhis-2/dhis-web/dhis-web-api-fred/src/main/webapp/WEB-INF/api-fred-velocity/v1/facility.vm 2012-12-08 11:50:26 +0000 +++ dhis-2/dhis-web/dhis-web-api-fred/src/main/webapp/WEB-INF/api-fred-velocity/v1/facility.vm 2012-12-08 16:07:13 +0000 @@ -1,6 +1,24 @@ - +
-
+
-
+
#set( $inputSize = "span12") + #set( $canEdit = true )
Facility @@ -43,24 +82,30 @@ - + - - + - +
- +
+ +
+ +
=== added directory 'dhis-2/dhis-web/dhis-web-api-fred/src/test/java' === added directory 'dhis-2/dhis-web/dhis-web-api-fred/src/test/java/org' === added directory 'dhis-2/dhis-web/dhis-web-api-fred/src/test/java/org/hisp' === added directory 'dhis-2/dhis-web/dhis-web-api-fred/src/test/java/org/hisp/dhis' === added directory 'dhis-2/dhis-web/dhis-web-api-fred/src/test/java/org/hisp/dhis/web' === added directory 'dhis-2/dhis-web/dhis-web-api-fred/src/test/java/org/hisp/dhis/web/webapi' === added directory 'dhis-2/dhis-web/dhis-web-api-fred/src/test/java/org/hisp/dhis/web/webapi/v1' === added directory 'dhis-2/dhis-web/dhis-web-api-fred/src/test/java/org/hisp/dhis/web/webapi/v1/utils' === added file 'dhis-2/dhis-web/dhis-web-api-fred/src/test/java/org/hisp/dhis/web/webapi/v1/utils/GeoUtilsTest.java' --- dhis-2/dhis-web/dhis-web-api-fred/src/test/java/org/hisp/dhis/web/webapi/v1/utils/GeoUtilsTest.java 1970-01-01 00:00:00 +0000 +++ dhis-2/dhis-web/dhis-web-api-fred/src/test/java/org/hisp/dhis/web/webapi/v1/utils/GeoUtilsTest.java 2012-12-08 16:07:13 +0000 @@ -0,0 +1,97 @@ +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.junit.Assert; +import org.junit.Test; + +/** + * @author Morten Olav Hansen + */ +public class GeoUtilsTest +{ + @Test + public void latToLat() + { + Double lat = 1.0d; + Double lng = 2.0d; + + String coordinatesString = String.format( "[%f, %f]", lat, lng ); + + GeoUtils.Coordinates coordinates = GeoUtils.parseCoordinates( coordinatesString, GeoUtils.CoordinateOrder.COORDINATE_LATLNG, + GeoUtils.CoordinateOrder.COORDINATE_LATLNG ); + + Assert.assertEquals( lat, coordinates.lat ); + Assert.assertEquals( lng, coordinates.lng ); + } + + @Test + public void lngToLng() + { + Double lat = 1.0d; + Double lng = 2.0d; + + String coordinatesString = String.format( "[%f, %f]", lat, lng ); + + GeoUtils.Coordinates coordinates = GeoUtils.parseCoordinates( coordinatesString, GeoUtils.CoordinateOrder.COORDINATE_LNGLAT, + GeoUtils.CoordinateOrder.COORDINATE_LNGLAT ); + + Assert.assertEquals( lng, coordinates.lng ); + Assert.assertEquals( lat, coordinates.lat ); + } + + @Test + public void latToLng() + { + Double lat = 1.0d; + Double lng = 2.0d; + + String coordinatesString = String.format( "[%f, %f]", lat, lng ); + + GeoUtils.Coordinates coordinates = GeoUtils.parseCoordinates( coordinatesString, GeoUtils.CoordinateOrder.COORDINATE_LATLNG, + GeoUtils.CoordinateOrder.COORDINATE_LNGLAT ); + + Assert.assertEquals( lat, coordinates.lng ); + Assert.assertEquals( lng, coordinates.lat ); + } + + @Test + public void lngToLat() + { + Double lat = 1.0d; + Double lng = 2.0d; + + String coordinatesString = String.format( "[%f, %f]", lng, lat ); + + GeoUtils.Coordinates coordinates = GeoUtils.parseCoordinates( coordinatesString, GeoUtils.CoordinateOrder.COORDINATE_LNGLAT, + GeoUtils.CoordinateOrder.COORDINATE_LATLNG ); + + Assert.assertEquals( lng, coordinates.lat ); + Assert.assertEquals( lat, coordinates.lng ); + } +}