=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientIdentifierService.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientIdentifierService.java 2013-09-11 04:12:50 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientIdentifierService.java 2013-09-13 12:36:44 +0000 @@ -62,6 +62,8 @@ PatientIdentifier get( PatientIdentifierType type, String identifier ); + Collection getAll( PatientIdentifierType type, String identifier ); + Patient getPatient( PatientIdentifierType idenType, String value ); Collection getPatientsByIdentifier( String identifier, int min, int max ); === modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientIdentifierStore.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientIdentifierStore.java 2013-09-13 07:40:23 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientIdentifierStore.java 2013-09-13 12:36:44 +0000 @@ -48,6 +48,9 @@ PatientIdentifier get( PatientIdentifierType type, String identifier ); + /* We need this since we have allowed identifiers with duplicate values in the past. This returns a list instead. */ + Collection getAll( PatientIdentifierType type, String identifier ); + Collection getByIdentifier( String identifier ); Collection getByType( PatientIdentifierType identifierType ); === 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 10:42:46 +0000 +++ 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 @@ -48,8 +48,12 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; import java.util.List; +import java.util.Map; +import java.util.Set; import static org.hisp.dhis.system.util.TextUtils.nullIfEmpty; @@ -306,8 +310,12 @@ @Override public Person savePerson( Person person ) { - checkForRequiredIdentifiers( person ); - checkForRequiredAttributes( person ); + List importConflicts = new ArrayList(); + importConflicts.addAll( checkForRequiredIdentifiers( person ) ); + importConflicts.addAll( checkForRequiredAttributes( person ) ); + + System.err.println( importConflicts ); + addSystemIdentifier( person ); Patient patient = getPatient( person ); @@ -319,14 +327,71 @@ return getPerson( patient ); } - private ImportConflict checkForRequiredIdentifiers( Person person ) + private List checkForRequiredIdentifiers( Person person ) { - return null; + List importConflicts = new ArrayList(); + Collection patientIdentifierTypes = manager.getAll( PatientIdentifierType.class ); + Map cacheMap = new HashMap(); + + for ( Identifier identifier : person.getIdentifiers() ) + { + if ( identifier.getValue() != null ) + { + cacheMap.put( identifier.getType(), identifier.getValue() ); + } + } + + for ( PatientIdentifierType patientIdentifierType : patientIdentifierTypes ) + { + if ( patientIdentifierType.isMandatory() ) + { + if ( !cacheMap.keySet().contains( patientIdentifierType.getUid() ) ) + { + importConflicts.add( + new ImportConflict( "Identifier.type", "Missing required identifier type " + patientIdentifierType.getUid() ) ); + } + } + + Collection patientIdentifiers = patientIdentifierService.getAll( + patientIdentifierType, cacheMap.get( patientIdentifierType.getUid() ) ); + + if ( !patientIdentifiers.isEmpty() ) + { + importConflicts.add( + new ImportConflict( "Identifier.value", "Value already exists for identifier type " + patientIdentifierType.getUid() ) ); + } + } + + return importConflicts; } - private ImportConflict checkForRequiredAttributes( Person person ) + private List checkForRequiredAttributes( Person person ) { - return null; + List importConflicts = new ArrayList(); + Collection patientAttributes = manager.getAll( PatientAttribute.class ); + Set cache = new HashSet(); + + for ( Identifier identifier : person.getIdentifiers() ) + { + if ( identifier.getValue() != null ) + { + cache.add( identifier.getType() ); + } + } + + for ( PatientAttribute patientAttribute : patientAttributes ) + { + if ( patientAttribute.isMandatory() ) + { + if ( !cache.contains( patientAttribute.getUid() ) ) + { + importConflicts.add( + new ImportConflict( "Identifier.type", "Missing required attribute type " + patientAttribute.getUid() ) ); + } + } + } + + return importConflicts; } private void addAttributes( Patient patient, Person person ) === modified file 'dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/DefaultPatientIdentifierService.java' --- dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/DefaultPatientIdentifierService.java 2013-09-11 04:12:50 +0000 +++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/DefaultPatientIdentifierService.java 2013-09-13 12:36:44 +0000 @@ -28,10 +28,10 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +import org.springframework.transaction.annotation.Transactional; + import java.util.Collection; -import org.springframework.transaction.annotation.Transactional; - /** * @author Abyot Asalefew Gizaw * @version $Id$ @@ -115,6 +115,11 @@ return patientIdentifierStore.get( type, identifier ); } + public Collection getAll( PatientIdentifierType type, String identifier ) + { + return patientIdentifierStore.getAll( type, identifier ); + } + public Patient getPatient( PatientIdentifierType idenType, String value ) { return patientIdentifierStore.getPatient( idenType, value ); @@ -135,7 +140,7 @@ { return patientIdentifierStore.get( identifierTypes, patient ); } - + @Override public boolean checkDuplicateIdentifier( String identifier ) { === modified file 'dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/hibernate/HibernatePatientIdentifierStore.java' --- dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/hibernate/HibernatePatientIdentifierStore.java 2013-09-11 16:02:06 +0000 +++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/hibernate/HibernatePatientIdentifierStore.java 2013-09-13 12:36:44 +0000 @@ -29,6 +29,7 @@ */ import java.util.Collection; +import java.util.Set; import org.hibernate.criterion.Projections; import org.hibernate.criterion.Restrictions; @@ -65,6 +66,13 @@ } @SuppressWarnings( "unchecked" ) + public Collection getAll( PatientIdentifierType type, String identifier ) + { + return getCriteria( Restrictions.eq( "identifierType", type ), + Restrictions.eq( "identifier", identifier ) ).list(); + } + + @SuppressWarnings( "unchecked" ) public Collection getByIdentifier( String identifier ) { return getCriteria( Restrictions.ilike( "identifier", "%" + identifier + "%" ) ).list();