← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 12083: return proper importSummary from creation of new Persons

 

------------------------------------------------------------
revno: 12083
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2013-09-13 15:06:54 +0200
message:
  return proper importSummary from creation of new Persons
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-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-13 12:36:44 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/person/AbstractPersonService.java	2013-09-13 13:06:54 +0000
@@ -30,6 +30,8 @@
 
 import org.hisp.dhis.common.IdentifiableObjectManager;
 import org.hisp.dhis.dxf2.importsummary.ImportConflict;
+import org.hisp.dhis.dxf2.importsummary.ImportStatus;
+import org.hisp.dhis.dxf2.importsummary.ImportSummary;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.patient.Patient;
 import org.hisp.dhis.patient.PatientAttribute;
@@ -308,13 +310,21 @@
     // -------------------------------------------------------------------------
 
     @Override
-    public Person savePerson( Person person )
+    public ImportSummary savePerson( Person person )
     {
+        ImportSummary importSummary = new ImportSummary();
+
         List<ImportConflict> importConflicts = new ArrayList<ImportConflict>();
         importConflicts.addAll( checkForRequiredIdentifiers( person ) );
         importConflicts.addAll( checkForRequiredAttributes( person ) );
 
-        System.err.println( importConflicts );
+        importSummary.setConflicts( importConflicts );
+
+        if ( !importConflicts.isEmpty() )
+        {
+            importSummary.getDataValueCount().incrementIgnored();
+            return importSummary;
+        }
 
         addSystemIdentifier( person );
 
@@ -324,7 +334,11 @@
         addAttributes( patient, person );
         patientService.updatePatient( patient );
 
-        return getPerson( patient );
+        importSummary.setStatus( ImportStatus.SUCCESS );
+        importSummary.setReference( patient.getUid() );
+        importSummary.getDataValueCount().incrementImported();
+
+        return importSummary;
     }
 
     private List<ImportConflict> checkForRequiredIdentifiers( Person person )
@@ -459,12 +473,14 @@
     // -------------------------------------------------------------------------
 
     @Override
-    public Person updatePerson( Person person )
+    public ImportSummary updatePerson( Person person )
     {
+        ImportSummary importSummary = new ImportSummary();
+
         System.err.println( "UPDATE: " + person );
         Patient patient = getPatient( person );
 
-        return getPerson( patient );
+        return importSummary;
     }
 
     // -------------------------------------------------------------------------

=== 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 12:03:58 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/person/JacksonPersonService.java	2013-09-13 13:06:54 +0000
@@ -31,6 +31,8 @@
 import com.fasterxml.jackson.databind.DeserializationFeature;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.dataformat.xml.XmlMapper;
+import org.hisp.dhis.dxf2.importsummary.ImportSummaries;
+import org.hisp.dhis.dxf2.importsummary.ImportSummary;
 import org.hisp.dhis.system.notification.Notifier;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.util.StreamUtils;
@@ -54,25 +56,25 @@
     private static ObjectMapper xmlMapper = new XmlMapper();
     private static ObjectMapper jsonMapper = new ObjectMapper();
 
-    @SuppressWarnings("unchecked")
+    @SuppressWarnings( "unchecked" )
     private static <T> T fromXml( InputStream inputStream, Class<?> clazz ) throws IOException
     {
         return (T) xmlMapper.readValue( inputStream, clazz );
     }
 
-    @SuppressWarnings("unchecked")
+    @SuppressWarnings( "unchecked" )
     private static <T> T fromXml( String input, Class<?> clazz ) throws IOException
     {
         return (T) xmlMapper.readValue( input, clazz );
     }
 
-    @SuppressWarnings("unchecked")
+    @SuppressWarnings( "unchecked" )
     private static <T> T fromJson( InputStream inputStream, Class<?> clazz ) throws IOException
     {
         return (T) jsonMapper.readValue( inputStream, clazz );
     }
 
-    @SuppressWarnings("unchecked")
+    @SuppressWarnings( "unchecked" )
     private static <T> T fromJson( String input, Class<?> clazz ) throws IOException
     {
         return (T) jsonMapper.readValue( input, clazz );
@@ -93,10 +95,10 @@
     // -------------------------------------------------------------------------
 
     @Override
-    public Persons savePersonXml( InputStream inputStream ) throws IOException
+    public ImportSummaries savePersonXml( InputStream inputStream ) throws IOException
     {
+        ImportSummaries importSummaries = new ImportSummaries();
         String input = StreamUtils.copyToString( inputStream, Charset.forName( "UTF-8" ) );
-        Persons savedPersons = new Persons();
 
         try
         {
@@ -104,23 +106,23 @@
 
             for ( Person person : persons.getPersons() )
             {
-                savedPersons.getPersons().add( savePerson( person ) );
+                importSummaries.addImportSummary( savePerson( person ) );
             }
         }
         catch ( Exception ex )
         {
             Person person = fromXml( input, Person.class );
-            savedPersons.getPersons().add( savePerson( person ) );
+            importSummaries.addImportSummary( savePerson( person ) );
         }
 
-        return savedPersons;
+        return importSummaries;
     }
 
     @Override
-    public Persons savePersonJson( InputStream inputStream ) throws IOException
+    public ImportSummaries savePersonJson( InputStream inputStream ) throws IOException
     {
+        ImportSummaries importSummaries = new ImportSummaries();
         String input = StreamUtils.copyToString( inputStream, Charset.forName( "UTF-8" ) );
-        Persons savedPersons = new Persons();
 
         try
         {
@@ -128,16 +130,16 @@
 
             for ( Person person : persons.getPersons() )
             {
-                savedPersons.getPersons().add( savePerson( person ) );
+                importSummaries.addImportSummary( savePerson( person ) );
             }
         }
         catch ( Exception ex )
         {
             Person person = fromJson( input, Person.class );
-            savedPersons.getPersons().add( savePerson( person ) );
+            importSummaries.addImportSummary( savePerson( person ) );
         }
 
-        return savedPersons;
+        return importSummaries;
     }
 
     // -------------------------------------------------------------------------
@@ -145,24 +147,27 @@
     // -------------------------------------------------------------------------
 
     @Override
-    public Person updatePersonXml( String id, InputStream inputStream ) throws IOException
+    public ImportSummary updatePersonXml( String id, InputStream inputStream ) throws IOException
     {
+        ImportSummary importSummary = new ImportSummary();
         Person person = fromXml( inputStream, Person.class );
         person.setPerson( id );
 
         updatePerson( person );
 
-        return person;
+        return importSummary;
     }
 
     @Override
-    public Person updatePersonJson( String id, InputStream inputStream ) throws IOException
+    public ImportSummary updatePersonJson( String id, InputStream inputStream ) throws IOException
     {
+        ImportSummary importSummary = new ImportSummary();
+
         Person person = fromJson( inputStream, Person.class );
         person.setPerson( id );
 
         updatePerson( person );
 
-        return person;
+        return importSummary;
     }
 }

=== 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 12:03:58 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/event/person/PersonService.java	2013-09-13 13:06:54 +0000
@@ -28,6 +28,8 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import org.hisp.dhis.dxf2.importsummary.ImportSummaries;
+import org.hisp.dhis.dxf2.importsummary.ImportSummary;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.patient.Patient;
 import org.hisp.dhis.program.Program;
@@ -71,21 +73,21 @@
     // CREATE
     // -------------------------------------------------------------------------
 
-    Persons savePersonXml( InputStream inputStream ) throws IOException;
-
-    Persons savePersonJson( InputStream inputStream ) throws IOException;
-
-    Person savePerson( Person person );
+    ImportSummaries savePersonXml( InputStream inputStream ) throws IOException;
+
+    ImportSummaries savePersonJson( InputStream inputStream ) throws IOException;
+
+    ImportSummary savePerson( Person person );
 
     // -------------------------------------------------------------------------
     // UPDATE
     // -------------------------------------------------------------------------
 
-    Person updatePersonXml( String id, InputStream inputStream ) throws IOException;
-
-    Person updatePersonJson( String id, InputStream inputStream ) throws IOException;
-
-    Person updatePerson( Person person );
+    ImportSummary updatePersonXml( String id, InputStream inputStream ) throws IOException;
+
+    ImportSummary updatePersonJson( String id, InputStream inputStream ) throws IOException;
+
+    ImportSummary updatePerson( Person person );
 
     // -------------------------------------------------------------------------
     // DELETE

=== 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 12:03:58 +0000
+++ dhis-2/dhis-web/dhis-web-api/src/main/java/org/hisp/dhis/api/controller/event/PersonController.java	2013-09-13 13:06:54 +0000
@@ -35,6 +35,8 @@
 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.importsummary.ImportSummaries;
+import org.hisp.dhis.dxf2.importsummary.ImportSummary;
 import org.hisp.dhis.dxf2.utils.JacksonUtils;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.program.Program;
@@ -176,42 +178,44 @@
     @RequestMapping( value = "", method = RequestMethod.POST, consumes = MediaType.APPLICATION_XML_VALUE )
     public void postPersonXml( HttpServletRequest request, HttpServletResponse response ) throws IOException
     {
-        Persons persons = personService.savePersonXml( request.getInputStream() );
+        ImportSummaries importSummaries = personService.savePersonXml( request.getInputStream() );
 
-        if ( persons.getPersons().size() > 1 )
+        if ( importSummaries.getImportSummaries().size() > 1 )
         {
             response.setStatus( HttpServletResponse.SC_CREATED );
-            JacksonUtils.toXml( response.getOutputStream(), persons );
+            JacksonUtils.toXml( response.getOutputStream(), importSummaries );
         }
         else
         {
             response.setStatus( HttpServletResponse.SC_CREATED );
-            response.setHeader( "Location", getResourcePath( request, persons.getPersons().get( 0 ) ) );
-            JacksonUtils.toXml( response.getOutputStream(), persons.getPersons().get( 0 ) );
+            ImportSummary importSummary = importSummaries.getImportSummaries().get( 0 );
+            response.setHeader( "Location", getResourcePath( request, importSummary ) );
+            JacksonUtils.toXml( response.getOutputStream(), importSummary );
         }
     }
 
     @RequestMapping( value = "", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE )
     public void postPersonJson( HttpServletRequest request, HttpServletResponse response ) throws IOException
     {
-        Persons persons = personService.savePersonJson( request.getInputStream() );
+        ImportSummaries importSummaries = personService.savePersonJson( request.getInputStream() );
 
-        if ( persons.getPersons().size() > 1 )
+        if ( importSummaries.getImportSummaries().size() > 1 )
         {
             response.setStatus( HttpServletResponse.SC_CREATED );
-            JacksonUtils.toJson( response.getOutputStream(), persons );
+            JacksonUtils.toJson( response.getOutputStream(), importSummaries );
         }
         else
         {
             response.setStatus( HttpServletResponse.SC_CREATED );
-            response.setHeader( "Location", getResourcePath( request, persons.getPersons().get( 0 ) ) );
-            JacksonUtils.toJson( response.getOutputStream(), persons.getPersons().get( 0 ) );
+            ImportSummary importSummary = importSummaries.getImportSummaries().get( 0 );
+            response.setHeader( "Location", getResourcePath( request, importSummary ) );
+            JacksonUtils.toJson( response.getOutputStream(), importSummary );
         }
     }
 
-    public String getResourcePath( HttpServletRequest request, Person person )
+    public String getResourcePath( HttpServletRequest request, ImportSummary importSummary )
     {
-        return ContextUtils.getContextPath( request ) + "/api/" + "persons" + "/" + person.getPerson();
+        return ContextUtils.getContextPath( request ) + "/api/" + "persons" + "/" + importSummary.getReference();
     }
 
     // -------------------------------------------------------------------------
@@ -222,16 +226,16 @@
     @ResponseStatus( value = HttpStatus.NO_CONTENT )
     public void updatePersonXml( @PathVariable String id, HttpServletRequest request, HttpServletResponse response ) throws IOException
     {
-        Person person = personService.updatePersonXml( id, request.getInputStream() );
-        JacksonUtils.toXml( response.getOutputStream(), person );
+        ImportSummary importSummary = personService.updatePersonXml( id, request.getInputStream() );
+        JacksonUtils.toXml( response.getOutputStream(), importSummary );
     }
 
     @RequestMapping( value = "/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE )
     @ResponseStatus( value = HttpStatus.NO_CONTENT )
     public void updatePersonJson( @PathVariable String id, HttpServletRequest request, HttpServletResponse response ) throws IOException
     {
-        Person person = personService.updatePersonJson( id, request.getInputStream() );
-        JacksonUtils.toJson( response.getOutputStream(), person );
+        ImportSummary importSummary = personService.updatePersonJson( id, request.getInputStream() );
+        JacksonUtils.toJson( response.getOutputStream(), importSummary );
     }
 
     // -------------------------------------------------------------------------