dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #24561
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 12064: Set status/location header on person-controller
------------------------------------------------------------
revno: 12064
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2013-09-12 14:03:58 +0200
message:
Set status/location header on person-controller
modified:
dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/person/AbstractPersonService.java
dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/person/JacksonPersonService.java
dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/person/PersonService.java
dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/ExchangeClasses.java
dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/PersonController.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-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/person/AbstractPersonService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/person/AbstractPersonService.java 2013-09-12 11:20:41 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/person/AbstractPersonService.java 2013-09-12 12:03:58 +0000
@@ -196,21 +196,29 @@
}
// -------------------------------------------------------------------------
+ // CREATE
+ // -------------------------------------------------------------------------
+
+ @Override
+ public Person savePerson( Person person )
+ {
+ System.err.println( "SAVE: " + person );
+ Patient patient = getPatient( person );
+
+ return person;
+ }
+
+ // -------------------------------------------------------------------------
// UPDATE
// -------------------------------------------------------------------------
@Override
- public void savePerson( Person person )
- {
- System.err.println( "SAVE: " + person );
- Patient patient = getPatient( person );
- }
-
- @Override
- public void updatePerson( Person person )
+ public Person updatePerson( Person person )
{
System.err.println( "UPDATE: " + person );
Patient patient = getPatient( person );
+
+ return person;
}
// -------------------------------------------------------------------------
=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/person/JacksonPersonService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/person/JacksonPersonService.java 2013-09-12 11:20:41 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/person/JacksonPersonService.java 2013-09-12 12:03:58 +0000
@@ -93,9 +93,10 @@
// -------------------------------------------------------------------------
@Override
- public void savePersonXml( InputStream inputStream ) throws IOException
+ public Persons savePersonXml( InputStream inputStream ) throws IOException
{
String input = StreamUtils.copyToString( inputStream, Charset.forName( "UTF-8" ) );
+ Persons savedPersons = new Persons();
try
{
@@ -103,20 +104,23 @@
for ( Person person : persons.getPersons() )
{
- savePerson( person );
+ savedPersons.getPersons().add( savePerson( person ) );
}
}
catch ( Exception ex )
{
Person person = fromXml( input, Person.class );
- savePerson( person );
+ savedPersons.getPersons().add( savePerson( person ) );
}
+
+ return savedPersons;
}
@Override
- public void savePersonJson( InputStream inputStream ) throws IOException
+ public Persons savePersonJson( InputStream inputStream ) throws IOException
{
String input = StreamUtils.copyToString( inputStream, Charset.forName( "UTF-8" ) );
+ Persons savedPersons = new Persons();
try
{
@@ -124,14 +128,16 @@
for ( Person person : persons.getPersons() )
{
- savePerson( person );
+ savedPersons.getPersons().add( savePerson( person ) );
}
}
catch ( Exception ex )
{
Person person = fromJson( input, Person.class );
- savePerson( person );
+ savedPersons.getPersons().add( savePerson( person ) );
}
+
+ return savedPersons;
}
// -------------------------------------------------------------------------
@@ -139,20 +145,24 @@
// -------------------------------------------------------------------------
@Override
- public void updatePersonXml( String id, InputStream inputStream ) throws IOException
+ public Person updatePersonXml( String id, InputStream inputStream ) throws IOException
{
Person person = fromXml( inputStream, Person.class );
person.setPerson( id );
updatePerson( person );
+
+ return person;
}
@Override
- public void updatePersonJson( String id, InputStream inputStream ) throws IOException
+ public Person updatePersonJson( String id, InputStream inputStream ) throws IOException
{
Person person = fromJson( inputStream, Person.class );
person.setPerson( id );
updatePerson( person );
+
+ return person;
}
}
=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/person/PersonService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/person/PersonService.java 2013-09-12 11:20:41 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/person/PersonService.java 2013-09-12 12:03:58 +0000
@@ -71,21 +71,21 @@
// CREATE
// -------------------------------------------------------------------------
- void savePersonXml( InputStream inputStream ) throws IOException;
-
- void savePersonJson( InputStream inputStream ) throws IOException;
-
- void savePerson( Person person );
+ Persons savePersonXml( InputStream inputStream ) throws IOException;
+
+ Persons savePersonJson( InputStream inputStream ) throws IOException;
+
+ Person savePerson( Person person );
// -------------------------------------------------------------------------
// UPDATE
// -------------------------------------------------------------------------
- void updatePersonXml( String id, InputStream inputStream ) throws IOException;
-
- void updatePersonJson( String id, InputStream inputStream ) throws IOException;
-
- void updatePerson( Person person );
+ Person updatePersonXml( String id, InputStream inputStream ) throws IOException;
+
+ Person updatePersonJson( String id, InputStream inputStream ) throws IOException;
+
+ Person updatePerson( Person person );
// -------------------------------------------------------------------------
// DELETE
=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/ExchangeClasses.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/ExchangeClasses.java 2013-08-28 11:29:37 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/ExchangeClasses.java 2013-09-12 12:03:58 +0000
@@ -47,6 +47,8 @@
import org.hisp.dhis.dataset.DataSet;
import org.hisp.dhis.dataset.Section;
import org.hisp.dhis.document.Document;
+import org.hisp.dhis.dxf2.event.Event;
+import org.hisp.dhis.dxf2.event.person.Person;
import org.hisp.dhis.indicator.Indicator;
import org.hisp.dhis.indicator.IndicatorGroup;
import org.hisp.dhis.indicator.IndicatorGroupSet;
@@ -156,7 +158,6 @@
allExportClasses.put( MessageConversation.class, "messageConversations" );
allExportClasses.put( Interpretation.class, "interpretations" );
- allExportClasses.put( Program.class, "programs" );
allExportClasses.put( ProgramStage.class, "programStages" );
allExportClasses.put( BaseDimensionalObject.class, "dimensions" );
=== modified file 'dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/PersonController.java'
--- dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/PersonController.java 2013-09-12 11:20:41 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/PersonController.java 2013-09-12 12:03:58 +0000
@@ -29,11 +29,13 @@
*/
import org.hisp.dhis.api.controller.WebOptions;
+import org.hisp.dhis.api.utils.ContextUtils;
import org.hisp.dhis.common.IdentifiableObjectManager;
import org.hisp.dhis.dxf2.event.person.Gender;
import org.hisp.dhis.dxf2.event.person.Person;
import org.hisp.dhis.dxf2.event.person.PersonService;
import org.hisp.dhis.dxf2.event.person.Persons;
+import org.hisp.dhis.dxf2.utils.JacksonUtils;
import org.hisp.dhis.organisationunit.OrganisationUnit;
import org.hisp.dhis.program.Program;
import org.springframework.beans.factory.annotation.Autowired;
@@ -57,7 +59,7 @@
* @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
*/
@Controller
-@RequestMapping(value = PersonController.RESOURCE_PATH)
+@RequestMapping( value = PersonController.RESOURCE_PATH )
public class PersonController
{
public static final String RESOURCE_PATH = "/persons";
@@ -72,11 +74,11 @@
// READ
// -------------------------------------------------------------------------
- @RequestMapping(value = "", method = RequestMethod.GET)
+ @RequestMapping( value = "", method = RequestMethod.GET )
public String getPersons(
- @RequestParam(value = "orgUnit", required = false) String orgUnitUid,
- @RequestParam(required = false) Gender gender,
- @RequestParam(value = "program", required = false) String programUid,
+ @RequestParam( value = "orgUnit", required = false ) String orgUnitUid,
+ @RequestParam( required = false ) Gender gender,
+ @RequestParam( value = "program", required = false ) String programUid,
@RequestParam Map<String, String> parameters, Model model, HttpServletRequest request ) throws Exception
{
WebOptions options = new WebOptions( parameters );
@@ -155,7 +157,7 @@
return organisationUnit;
}
- @RequestMapping(value = "/{id}", method = RequestMethod.GET)
+ @RequestMapping( value = "/{id}", method = RequestMethod.GET )
public String getPerson( @PathVariable String id, @RequestParam Map<String, String> parameters, Model model )
{
WebOptions options = new WebOptions( parameters );
@@ -172,17 +174,44 @@
// -------------------------------------------------------------------------
@RequestMapping( value = "", method = RequestMethod.POST, consumes = MediaType.APPLICATION_XML_VALUE )
- @ResponseStatus( value = HttpStatus.CREATED )
public void postPersonXml( HttpServletRequest request, HttpServletResponse response ) throws IOException
{
- personService.savePersonXml( request.getInputStream() );
+ Persons persons = personService.savePersonXml( request.getInputStream() );
+
+ if ( persons.getPersons().size() > 1 )
+ {
+ response.setStatus( HttpServletResponse.SC_CREATED );
+ JacksonUtils.toXml( response.getOutputStream(), persons );
+ }
+ else
+ {
+ response.setStatus( HttpServletResponse.SC_CREATED );
+ response.setHeader( "Location", getResourcePath( request, persons.getPersons().get( 0 ) ) );
+ JacksonUtils.toXml( response.getOutputStream(), persons.getPersons().get( 0 ) );
+ }
}
@RequestMapping( value = "", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE )
- @ResponseStatus( value = HttpStatus.CREATED )
public void postPersonJson( HttpServletRequest request, HttpServletResponse response ) throws IOException
{
- personService.savePersonJson( request.getInputStream() );
+ Persons persons = personService.savePersonJson( request.getInputStream() );
+
+ if ( persons.getPersons().size() > 1 )
+ {
+ response.setStatus( HttpServletResponse.SC_CREATED );
+ JacksonUtils.toJson( response.getOutputStream(), persons );
+ }
+ else
+ {
+ response.setStatus( HttpServletResponse.SC_CREATED );
+ response.setHeader( "Location", getResourcePath( request, persons.getPersons().get( 0 ) ) );
+ JacksonUtils.toJson( response.getOutputStream(), persons.getPersons().get( 0 ) );
+ }
+ }
+
+ public String getResourcePath( HttpServletRequest request, Person person )
+ {
+ return ContextUtils.getContextPath( request ) + "/api/" + "persons" + "/" + person.getPerson();
}
// -------------------------------------------------------------------------
@@ -191,16 +220,18 @@
@RequestMapping( value = "/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_XML_VALUE )
@ResponseStatus( value = HttpStatus.NO_CONTENT )
- public void updatePersonXml( @PathVariable String id, HttpServletRequest request ) throws IOException
+ public void updatePersonXml( @PathVariable String id, HttpServletRequest request, HttpServletResponse response ) throws IOException
{
- personService.updatePersonXml( id, request.getInputStream() );
+ Person person = personService.updatePersonXml( id, request.getInputStream() );
+ JacksonUtils.toXml( response.getOutputStream(), person );
}
@RequestMapping( value = "/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE )
@ResponseStatus( value = HttpStatus.NO_CONTENT )
- public void updatePersonJson( @PathVariable String id, HttpServletRequest request ) throws IOException
+ public void updatePersonJson( @PathVariable String id, HttpServletRequest request, HttpServletResponse response ) throws IOException
{
- personService.updatePersonJson( id, request.getInputStream() );
+ Person person = personService.updatePersonJson( id, request.getInputStream() );
+ JacksonUtils.toJson( response.getOutputStream(), person );
}
// -------------------------------------------------------------------------