=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/Patient.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/Patient.java 2012-07-06 04:04:41 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/Patient.java 2012-07-06 09:59:36 +0000 @@ -49,20 +49,21 @@ private static final long serialVersionUID = 884114994005945275L; public static final String MALE = "M"; - public static final String FEMALE = "F"; public static final char DOB_TYPE_VERIFIED = 'V'; - public static final char DOB_TYPE_DECLARED = 'D'; - public static final char DOB_TYPE_APPROXIATED = 'A'; public static final char AGE_TYPE_YEAR = 'Y'; - public static final char AGE_TYPE_MONTH = 'M'; - public static final char AGE_TYPE_DAY = 'D'; + + public static String PREFIX_IDENTIFIER_TYPE = "iden"; + public static String PREFIX_FIXED_ATTRIBUTE = "fixedAttr"; + public static String PREFIX_PATIENT_ATTRIBUTE = "attr"; + public static String PREFIX_PROGRAM = "prg"; + private Integer id; === modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientService.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientService.java 2012-06-12 07:46:25 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientService.java 2012-07-06 09:59:36 +0000 @@ -203,4 +203,7 @@ void removeErollmentPrograms( Program program ); + Collection searchPatients( List searchKeys, OrganisationUnit orgunit, Integer min, Integer max ); + + int countSearchPatients( List searchKeys, OrganisationUnit orgunit ); } === modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientStore.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientStore.java 2012-06-12 07:46:25 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientStore.java 2012-07-06 09:59:36 +0000 @@ -29,6 +29,7 @@ import java.util.Collection; import java.util.Date; +import java.util.List; import org.hisp.dhis.common.GenericStore; import org.hisp.dhis.organisationunit.OrganisationUnit; @@ -64,4 +65,8 @@ int countGetPatientsByOrgUnitProgram( OrganisationUnit organisationUnit, Program program ); void removeErollmentPrograms( Program program ); + + Collection search( List searchKeys, OrganisationUnit orgunit, Integer min, Integer max ); + + int countSearch( List searchKeys, OrganisationUnit orgunit ); } === modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/patientattributevalue/PatientAttributeValueService.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/patientattributevalue/PatientAttributeValueService.java 2012-05-24 13:53:25 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/patientattributevalue/PatientAttributeValueService.java 2012-07-06 09:59:36 +0000 @@ -27,10 +27,8 @@ package org.hisp.dhis.patientattributevalue; import java.util.Collection; -import java.util.List; import java.util.Map; -import org.hisp.dhis.organisationunit.OrganisationUnit; import org.hisp.dhis.patient.Patient; import org.hisp.dhis.patient.PatientAttribute; import org.hisp.dhis.patient.PatientAttributeOption; @@ -76,20 +74,6 @@ Collection getPatient( PatientAttribute attribute, String value ); - Collection searchPatients( PatientAttribute patientAttribute, String searchText, int min, int max); - - Collection searchPatients( PatientAttribute patientAttribute, String searchText ); - - Collection searchPatients( List patientAttributeIds, List searchTexts, int min, int max); - - Collection searchPatients( List patientAttributeIds, List searchTexts, OrganisationUnit orgunit, int min, int max); - - int countSearchPatientAttributeValue( PatientAttribute patientAttribute, String searchText ); - - int countSearchPatients( List patientAttributeIds, List searchTexts ); - - int countSearchPatients( List patientAttributeIds, List searchTexts, OrganisationUnit orgunit ); - void updatePatientAttributeValues( PatientAttributeOption patientAttributeOption); Collection getPatientAttributeValues( Patient patient, Program program ); === modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/patientattributevalue/PatientAttributeValueStore.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/patientattributevalue/PatientAttributeValueStore.java 2012-05-24 13:53:25 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/patientattributevalue/PatientAttributeValueStore.java 2012-07-06 09:59:36 +0000 @@ -27,10 +27,8 @@ package org.hisp.dhis.patientattributevalue; import java.util.Collection; -import java.util.List; import org.hisp.dhis.common.GenericStore; -import org.hisp.dhis.organisationunit.OrganisationUnit; import org.hisp.dhis.patient.Patient; import org.hisp.dhis.patient.PatientAttribute; import org.hisp.dhis.patient.PatientAttributeOption; @@ -65,20 +63,6 @@ Collection getPatient( PatientAttribute attribute, String value ); - Collection searchPatients( PatientAttribute patientAttribute, String searchText, int min, int max); - - Collection searchPatients( PatientAttribute patientAttribute, String searchText ); - - Collection searchPatients( List patientAttributeIds, List searchTexts, int min, int max); - - Collection searchPatients( List patientAttributeIds, List searchTexts, OrganisationUnit orgunit, int min, int max); - - int countSearchPatientAttributeValue( PatientAttribute patientAttribute, String searchText ); - - int countSearchPatients( List patientAttributeIds, List searchTexts ); - - int countSearchPatients( List patientAttributeIds, List searchTexts, OrganisationUnit orgunit ); - void updatePatientAttributeValues( PatientAttributeOption patientAttributeOption); Collection get( Patient patient, Program program ); === modified file 'dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/DefaultPatientService.java' --- dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/DefaultPatientService.java 2012-06-12 07:46:25 +0000 +++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/DefaultPatientService.java 2012-07-06 09:59:36 +0000 @@ -522,4 +522,14 @@ { patientStore.removeErollmentPrograms( program ); } + + public Collection searchPatients( List searchKeys, OrganisationUnit orgunit, Integer min, Integer max ) + { + return patientStore.search( searchKeys, orgunit, min, max ); + } + + public int countSearchPatients( List searchKeys, OrganisationUnit orgunit ) + { + return patientStore.countSearch( searchKeys, orgunit ); + } } === modified file 'dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/hibernate/HibernatePatientStore.java' --- dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/hibernate/HibernatePatientStore.java 2012-06-12 07:46:25 +0000 +++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/hibernate/HibernatePatientStore.java 2012-07-06 09:59:36 +0000 @@ -32,6 +32,7 @@ import java.util.Collection; import java.util.Date; import java.util.HashSet; +import java.util.List; import java.util.Set; import org.amplecode.quick.StatementHolder; @@ -89,7 +90,7 @@ // ------------------------------------------------------------------------- // Implementation methods // ------------------------------------------------------------------------- - + @SuppressWarnings( "unchecked" ) @Override public Collection getByGender( String gender ) @@ -139,8 +140,7 @@ @SuppressWarnings( "unchecked" ) @Override - public Collection get( String firstName, String middleName, String lastName, Date birthdate, - String gender ) + public Collection get( String firstName, String middleName, String lastName, Date birthdate, String gender ) { Criteria crit = getCriteria(); Conjunction con = Restrictions.conjunction(); @@ -182,18 +182,19 @@ @SuppressWarnings( "unchecked" ) @Override - public Collection getByOrgUnitProgram( OrganisationUnit organisationUnit, Program program, Integer min, Integer max ) + public Collection getByOrgUnitProgram( OrganisationUnit organisationUnit, Program program, Integer min, + Integer max ) { - Criteria criteria = getCriteria( Restrictions.eq( "organisationUnit", organisationUnit ) ).createAlias( "programs", - "program" ).add( Restrictions.eq( "program.id", program.getId() ) ); - - criteria.addOrder( Order.desc( "id" ) ); - - if( min != null && max != null ) - { - criteria.setFirstResult( min ).setMaxResults( max ); - } - return criteria.list(); + Criteria criteria = getCriteria( Restrictions.eq( "organisationUnit", organisationUnit ) ).createAlias( + "programs", "program" ).add( Restrictions.eq( "program.id", program.getId() ) ); + + criteria.addOrder( Order.desc( "id" ) ); + + if ( min != null && max != null ) + { + criteria.setFirstResult( min ).setMaxResults( max ); + } + return criteria.list(); } @Override @@ -240,9 +241,9 @@ @Override public int countGetPatientsByOrgUnitProgram( OrganisationUnit organisationUnit, Program program ) { - Number rs = (Number) getCriteria( Restrictions.eq( "organisationUnit", organisationUnit ) ).createAlias( - "programs", "program" ).add( Restrictions.eq( "program.id", program.getId() ) ).setProjection( - Projections.rowCount() ).uniqueResult(); + Number rs = (Number) getCriteria( Restrictions.eq( "organisationUnit", organisationUnit ) ) + .createAlias( "programs", "program" ).add( Restrictions.eq( "program.id", program.getId() ) ) + .setProjection( Projections.rowCount() ).uniqueResult(); return rs != null ? rs.intValue() : 0; } @@ -263,4 +264,136 @@ jdbcTemplate.execute( sql ); } + + @Override + public Collection search( List searchKeys, OrganisationUnit orgunit, Integer min, Integer max ) + { + String sql = searchPatientSql( false, searchKeys, orgunit, min, max ); + Collection patients = new HashSet(); + StatementHolder holder = statementManager.getHolder(); + try + { + Statement statement = holder.getStatement(); + + ResultSet resultSet = statement.executeQuery( sql ); + + if ( resultSet.next() ) + { + int patientId = resultSet.getInt( 1 ); + patients.add( get( patientId ) ); + } + } + catch ( Exception ex ) + { + ex.printStackTrace(); + } + finally + { + holder.close(); + } + + return patients; + } + + public int countSearch( List searchKeys, OrganisationUnit orgunit ) + { + String sql = searchPatientSql( true, searchKeys, orgunit, null, null ); + + return jdbcTemplate.queryForInt( sql ); + } + + // ------------------------------------------------------------------------- + // Supportive methods + // ------------------------------------------------------------------------- + + private String searchPatientSql( boolean count, List searchKeys, OrganisationUnit orgunit, Integer min, Integer max ) + { + String selector = count ? "count(*) " : "* "; + + String sql = "select " + selector + " from ( select distinct p.patientid, p.firstname, p.middlename, p.lastname, p.gender, p.phonenumber, p.birthdate, p.deathdate,"; + String patientWhere = ""; + String patientOperator = " where "; + String otherWhere = ""; + String operator = " where "; + boolean hasIdentifier = false; + + for ( String searchKey : searchKeys ) + { + String[] keys = searchKey.split( "_" ); + String id = keys[1]; + String value = ""; + if ( keys.length == 3 ) + { + value = keys[2]; + } + + if ( keys[0].equals( Patient.PREFIX_FIXED_ATTRIBUTE ) ) + { + patientWhere += patientOperator + " lower(p." + id + ")='" + value + "'"; + patientOperator = " and "; + } + else if ( keys[0].equals( Patient.PREFIX_IDENTIFIER_TYPE ) ) + { + int startIndex = id.indexOf( ' ' ); + int endIndex = id.lastIndexOf( ' ' ); + String firstName = id.substring( 0, startIndex ); + String middleName = ""; + String lastName = ""; + + if ( startIndex == endIndex ) + { + middleName = ""; + lastName = id.substring( startIndex + 1, id.length() ); + } + else + { + middleName = id.substring( startIndex + 1, endIndex ); + lastName = id.substring( endIndex + 1, id.length() ); + } + + patientWhere = operator + "( ( lower(p.firstname)='" + firstName + "' and lower(p.middlename)='" + middleName + "' and lower(p.lastname)='" + lastName + "' ) or lower(pi.identifier)='" + id + "') "; + patientOperator = " and "; + hasIdentifier = true; + } + else if ( keys[0].equals( Patient.PREFIX_PATIENT_ATTRIBUTE ) ) + { + sql += "(select value from patientattributevalue where patientid=p.patientid and patientattributeid=" + + id + " ) as " + Patient.PREFIX_PATIENT_ATTRIBUTE + "_" + id + ","; + otherWhere = operator + "lower(" + Patient.PREFIX_PATIENT_ATTRIBUTE + "_" + id + ")='" + value + "'"; + operator = " and "; + } + else if ( keys[0].equals( Patient.PREFIX_PROGRAM ) ) + { + sql += "(select programid from patient_programs where patientid=p.patientid and programid=" + keys[1] + + " ) as " + Patient.PREFIX_PROGRAM + "_" + id + ","; + otherWhere = operator + Patient.PREFIX_PROGRAM + "_" + id + "=" + id; + operator = " and "; + } + } + + sql = sql.substring( 0, sql.length() - 1 ) + " "; // Removing last comma + + sql += " from patient p "; + if ( hasIdentifier ) + { + sql += " left join patientidentifier pi on p.patientid=pi.patientid "; + } + + if( orgunit != null ) + { + patientWhere += " and p.organisationunitid = " + orgunit.getId(); + } + + sql += patientWhere + " order by p.patientid desc "; + sql += " ) as searchresult"; + sql += otherWhere; + + if ( min != null && max != null ) + { + sql += statementBuilder.limitRecord( min, max ); + } + + return sql; + } + } === modified file 'dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patientattributevalue/DefaultPatientAttributeValueService.java' --- dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patientattributevalue/DefaultPatientAttributeValueService.java 2012-05-24 13:53:25 +0000 +++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patientattributevalue/DefaultPatientAttributeValueService.java 2012-07-06 09:59:36 +0000 @@ -29,14 +29,12 @@ import java.util.Collection; import java.util.HashMap; import java.util.HashSet; -import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.Set; import java.util.SortedMap; import java.util.TreeMap; -import java.util.Map.Entry; -import org.hisp.dhis.organisationunit.OrganisationUnit; import org.hisp.dhis.patient.Patient; import org.hisp.dhis.patient.PatientAttribute; import org.hisp.dhis.patient.PatientAttributeOption; @@ -216,33 +214,7 @@ { return patientAttributeValueStore.getPatient( attribute, value ); } - - public int countSearchPatientAttributeValue( PatientAttribute patientAttribute, String searchText ) - { - return patientAttributeValueStore.countSearchPatientAttributeValue( patientAttribute, searchText ); - } - - public Collection searchPatients( PatientAttribute patientAttribute, String searchText, int min, int max ) - { - return patientAttributeValueStore.searchPatients( patientAttribute, searchText, min, max ); - } - - public Collection searchPatients( PatientAttribute patientAttribute, String searchText ) - { - return patientAttributeValueStore.searchPatients( patientAttribute, searchText ); - } - - public Collection searchPatients( List patientAttributeIds, List searchTexts, int min, - int max ) - { - return patientAttributeValueStore.searchPatients( patientAttributeIds, searchTexts, min, max ); - } - - public int countSearchPatients( List patientAttributeIds, List searchTexts ) - { - return patientAttributeValueStore.countSearchPatients( patientAttributeIds, searchTexts ); - } - + public void updatePatientAttributeValues( PatientAttributeOption patientAttributeOption ) { patientAttributeValueStore.updatePatientAttributeValues( patientAttributeOption ); @@ -257,18 +229,4 @@ { return patientAttributeValueStore.getWithoutProgram( patient ); } - - @Override - public int countSearchPatients( List patientAttributeIds, List searchTexts, - OrganisationUnit orgunit ) - { - return patientAttributeValueStore.countSearchPatients( patientAttributeIds, searchTexts, orgunit ); - } - - @Override - public Collection searchPatients( List patientAttributeIds, List searchTexts, - OrganisationUnit orgunit, int min, int max ) - { - return patientAttributeValueStore.searchPatients( patientAttributeIds, searchTexts, orgunit, min, max ); - } } === modified file 'dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patientattributevalue/hibernate/HibernatePatientAttributeValueStore.java' --- dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patientattributevalue/hibernate/HibernatePatientAttributeValueStore.java 2012-07-06 04:04:41 +0000 +++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patientattributevalue/hibernate/HibernatePatientAttributeValueStore.java 2012-07-06 09:59:36 +0000 @@ -27,13 +27,11 @@ package org.hisp.dhis.patientattributevalue.hibernate; import java.util.Collection; -import java.util.List; import org.hibernate.Query; import org.hibernate.criterion.Projections; import org.hibernate.criterion.Restrictions; import org.hisp.dhis.hibernate.HibernateGenericStore; -import org.hisp.dhis.organisationunit.OrganisationUnit; import org.hisp.dhis.patient.Patient; import org.hisp.dhis.patient.PatientAttribute; import org.hisp.dhis.patient.PatientAttributeOption; @@ -131,83 +129,6 @@ .setProjection( Projections.property( "patient" ) ).list(); } - public int countSearchPatientAttributeValue( PatientAttribute patientAttribute, String searchText ) - { - Number rs = (Number) getCriteria( Restrictions.eq( "patientAttribute", patientAttribute ), - Restrictions.ilike( "value", "%" + searchText + "%" ) ).setProjection( Projections.rowCount() ) - .uniqueResult(); - - return rs != null ? rs.intValue() : 0; - } - - @SuppressWarnings( "unchecked" ) - public Collection searchPatients( PatientAttribute patientAttribute, String searchText, int min, int max ) - { - return getCriteria( Restrictions.eq( "patientAttribute", patientAttribute ), - Restrictions.ilike( "value", "%" + searchText + "%" ) ).setProjection( - Projections.distinct( Projections.property( "patient" ) ) ).setFirstResult( min ).setMaxResults( max ) - .list(); - - } - - @SuppressWarnings( "unchecked" ) - public Collection searchPatients( PatientAttribute patientAttribute, String searchText ) - { - String hql = "select pav.patient from PatientAttributeValue pav where pav.patientAttribute = :patientAttribute and pav.value like '%" - + searchText + "%'"; - - Query query = getQuery( hql ); - query.setEntity( "patientAttribute", patientAttribute ); - - return query.list(); - } - - @SuppressWarnings( "unchecked" ) - public Collection searchPatients( List patientAttributeIds, List searchTexts, int min, - int max ) - { - String hql = "SELECT DISTINCT p FROM Patient as p WHERE p in "; - String end = ""; - - int index = 0; - for ( Integer patientAttributeId : patientAttributeIds ) - { - end += ")"; - - hql += createHQL( patientAttributeId, searchTexts.get( index ), index, patientAttributeIds.size() ); - - index++; - } - - hql += " ORDER BY p.id ASC"; - - Query query = getQuery( hql + end ).setFirstResult( min ).setMaxResults( max ); - - return query.list(); - } - - public int countSearchPatients( List patientAttributeIds, List searchTexts ) - { - String hql = "SELECT COUNT( DISTINCT p ) FROM Patient as p WHERE p in "; - String end = ""; - - int index = 0; - for ( Integer patientAttributeId : patientAttributeIds ) - { - end += ")"; - - hql += createHQL( patientAttributeId, searchTexts.get( index ), index, patientAttributeIds.size() ); - - index++; - } - - Query query = getQuery( hql + end ); - - Number rs = (Number) query.uniqueResult(); - - return (rs != null) ? rs.intValue() : 0; - } - public void updatePatientAttributeValues( PatientAttributeOption patientAttributeOption ) { String sql = "UPDATE patientattributevalue SET value='" + patientAttributeOption.getName() @@ -236,173 +157,4 @@ return query.list(); } - - // ------------------------------------------------------------------------- - // Supportive methods - // ------------------------------------------------------------------------- - - private String createHQL( Integer patientAttributeId, String searchText, int index, int noCondition ) - { - String hql = ""; - searchText = searchText.trim(); - boolean isSearchByAttribute = true; - - // --------------------------------------------------------------------- - // search patients by name or identifier - // --------------------------------------------------------------------- - if ( patientAttributeId == null ) - { - int startIndex = searchText.indexOf( ' ' ); - int endIndex = searchText.lastIndexOf( ' ' ); - - String firstName = searchText.toString(); - String middleName = ""; - String lastName = ""; - - // --------------------------------------------------------------------- - // search patients by full-name or identifier - // --------------------------------------------------------------------- - - if ( searchText.indexOf( ' ' ) != -1 ) - { - firstName = searchText.substring( 0, startIndex ); - if ( startIndex == endIndex ) - { - middleName = ""; - lastName = searchText.substring( startIndex + 1, searchText.length() ); - } - else - { - middleName = searchText.substring( startIndex + 1, endIndex ); - lastName = searchText.substring( endIndex + 1, searchText.length() ); - } - } - - hql += " ( SELECT p" + index + " FROM Patient as p" + index + " JOIN p" + index - + ".identifiers as identifier" + index + " " + "WHERE lower(identifier" + index - + ".identifier)=lower('" + searchText + "') " + "OR (lower(p" + index + ".firstName) LIKE lower('%" - + firstName + "%') " + "AND lower(p" + index + ".middleName) = lower('" + middleName + "') " - + "AND lower(p" + index + ".lastName) LIKE lower('%" + lastName + "%')) "; - - isSearchByAttribute = false; - } - - // --------------------------------------------------------------------- - // search patients by birth-date - // --------------------------------------------------------------------- - - else if ( patientAttributeId == -1 ) - { - hql += " ( SELECT p" + index + " FROM Patient AS p" + index - + " WHERE p" + index + ".birthDate " + searchText; - isSearchByAttribute = false; - } - // gender - else if ( patientAttributeId == -2 ) - { - hql += " ( SELECT p" + index + " FROM Patient AS p" + index - + " WHERE p" + index + ".gender='" + searchText + "'"; - isSearchByAttribute = false; - } - // age - else if ( patientAttributeId == -3 ) - { - hql += " ( SELECT p" + index + " FROM Patient AS p" + index - + " WHERE p" + index + ".integerValueOfAge='" + searchText + "'"; - isSearchByAttribute = false; - } - // phone number - else if ( patientAttributeId == -4 ) - { - hql += " ( SELECT p" + index + " FROM Patient AS p" + index - + " WHERE p" + index + ".phoneNumber='" + searchText + "'"; - isSearchByAttribute = false; - } - - - // --------------------------------------------------------------------- - // search patients by program - // --------------------------------------------------------------------- - else if ( patientAttributeId == 0 ) - { - hql += " ( SELECT p" + index + " FROM Patient AS p" + index + " " + " JOIN p" + index - + ".programs AS program" + index + " WHERE program" + index + ".id=" + searchText; - - isSearchByAttribute = false; - } - // ----------------------------------------------------------------- - // search patients by attribute - // ----------------------------------------------------------------- - else - { - hql += " ( SELECT pav" + index + ".patient FROM PatientAttributeValue as pav" + index + " " + "WHERE pav" - + index + ".patientAttribute.id=" + patientAttributeId + " AND lower(pav" + index - + ".value) LIKE lower('%" + searchText + "%') "; - } - - if ( index < noCondition - 1 ) - { - - if ( isSearchByAttribute ) - { - hql += " AND pav" + index + ".patient in "; - } - else - { - hql += " AND p" + index + " in "; - } - } - - return hql; - - } - - @Override - public int countSearchPatients( List patientAttributeIds, List searchTexts, - OrganisationUnit orgunit ) - { - String hql = "SELECT COUNT( DISTINCT p ) FROM Patient as p WHERE p in "; - String end = ""; - - int index = 0; - for ( Integer patientAttributeId : patientAttributeIds ) - { - end += ")"; - - hql += createHQL( patientAttributeId, searchTexts.get( index ), index, patientAttributeIds.size() ); - - index++; - } - - Query query = getQuery( hql + end + " AND p.organisationUnit.id=" + orgunit.getId() ); - - Number rs = (Number) query.uniqueResult(); - - return (rs != null) ? rs.intValue() : 0; - } - - @SuppressWarnings("unchecked") - @Override - public Collection searchPatients( List patientAttributeIds, List searchTexts, - OrganisationUnit orgunit, int min, int max ) - { - String hql = "SELECT DISTINCT p FROM Patient as p WHERE p in "; - String end = ""; - - int index = 0; - for ( Integer patientAttributeId : patientAttributeIds ) - { - end += ")"; - - hql += createHQL( patientAttributeId, searchTexts.get( index ), index, patientAttributeIds.size() ); - - index++; - } - - hql += " ORDER BY p.id ASC"; - - Query query = getQuery( hql + end + " AND p.organisationUnit.id=" + orgunit.getId() ).setFirstResult( min ).setMaxResults( max ); - - return query.list(); - } } === removed file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/SearchPatientAction.java' --- dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/SearchPatientAction.java 2012-06-12 07:46:25 +0000 +++ dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/caseentry/SearchPatientAction.java 1970-01-01 00:00:00 +0000 @@ -1,220 +0,0 @@ -/* - * Copyright (c) 2004-2009, 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. - */ - -package org.hisp.dhis.caseentry.action.caseentry; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; - -import org.hisp.dhis.organisationunit.OrganisationUnit; -import org.hisp.dhis.ouwt.manager.OrganisationUnitSelectionManager; -import org.hisp.dhis.paging.ActionPagingSupport; -import org.hisp.dhis.patient.Patient; -import org.hisp.dhis.patient.PatientAttribute; -import org.hisp.dhis.patient.PatientAttributeService; -import org.hisp.dhis.patient.PatientService; -import org.hisp.dhis.patientattributevalue.PatientAttributeValueService; - -/** - * @author Abyot Asalefew Gizaw - * @version $Id$ - */ -public class SearchPatientAction - extends ActionPagingSupport -{ - // ------------------------------------------------------------------------- - // Dependencies - // ------------------------------------------------------------------------- - - private OrganisationUnitSelectionManager selectionManager; - - private PatientService patientService; - - private PatientAttributeService patientAttributeService; - - private PatientAttributeValueService patientAttributeValueService; - - // ------------------------------------------------------------------------- - // Input/output - // ------------------------------------------------------------------------- - - private String searchText; - - private boolean listAll; - - private Integer searchingAttributeId; - - private Collection patients = new ArrayList(); - - private Map mapPatientOrgunit = new HashMap(); - - // ------------------------------------------------------------------------- - // Getters && Setters - // ------------------------------------------------------------------------- - - public void setSelectionManager( OrganisationUnitSelectionManager selectionManager ) - { - this.selectionManager = selectionManager; - } - - public void setPatientService( PatientService patientService ) - { - this.patientService = patientService; - } - - public void setPatientAttributeService( PatientAttributeService patientAttributeService ) - { - this.patientAttributeService = patientAttributeService; - } - - public void setPatientAttributeValueService( PatientAttributeValueService patientAttributeValueService ) - { - this.patientAttributeValueService = patientAttributeValueService; - } - - public Map getMapPatientOrgunit() - { - return mapPatientOrgunit; - } - - public void setSearchText( String searchText ) - { - this.searchText = searchText; - } - - public String getSearchText() - { - return searchText; - } - - public void setListAll( boolean listAll ) - { - this.listAll = listAll; - } - - public Collection getPatients() - { - return patients; - } - - public void setSearchingAttributeId( Integer searchingAttributeId ) - { - this.searchingAttributeId = searchingAttributeId; - } - - private Integer total; - - public Integer getTotal() - { - return total; - } - - // ------------------------------------------------------------------------- - // Implementation Action - // ------------------------------------------------------------------------- - - public String execute() - throws Exception - { - OrganisationUnit organisationUnit = selectionManager.getSelectedOrganisationUnit(); - - // List all patients - if ( listAll ) - { - searchText = "list_all_patients"; - - total = patientService.countGetPatientsByOrgUnit( organisationUnit ); - this.paging = createPaging( total ); - - patients = new ArrayList( patientService.getPatients( organisationUnit, paging.getStartPos(), - paging.getPageSize() ) ); - - return SUCCESS; - } - - if ( searchText != null ) - { - int index = searchText.indexOf( ' ' ); - - if ( index != -1 && index == searchText.lastIndexOf( ' ' ) ) - { - String[] keys = searchText.split( " " ); - searchText = keys[0] + " " + keys[1]; - } - } - - // Search patients by attribute - if ( searchingAttributeId != null && searchText != null ) - { - PatientAttribute searchingPatientAttribute = patientAttributeService - .getPatientAttribute( searchingAttributeId ); - - total = patientAttributeValueService.countSearchPatientAttributeValue( searchingPatientAttribute, - searchText ); - this.paging = createPaging( total ); - - patients = patientAttributeValueService.searchPatients( searchingPatientAttribute, searchText, paging - .getStartPos(), paging.getPageSize() ); - } - - // Search patients by full-name / identifier - else - { - total = patientService.countGetPatients( searchText ); - this.paging = createPaging( total ); - - patients = patientService.getPatients( searchText, paging.getStartPos(), paging.getPageSize() ); - } - - for ( Patient patient : patients ) - { - mapPatientOrgunit.put( patient.getId(), getHierarchyOrgunit( patient.getOrganisationUnit() ) ); - } - - return SUCCESS; - } - - // ------------------------------------------------------------------------- - // Supportive method - // ------------------------------------------------------------------------- - - private String getHierarchyOrgunit( OrganisationUnit orgunit ) - { - String hierarchyOrgunit = orgunit.getName(); - - while ( orgunit.getParent() != null ) - { - hierarchyOrgunit = orgunit.getParent().getName() + " / " + hierarchyOrgunit; - - orgunit = orgunit.getParent(); - } - - return hierarchyOrgunit; - } -} === modified file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/patient/SearchPatientAction.java' --- dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/patient/SearchPatientAction.java 2012-06-25 02:11:55 +0000 +++ dhis-2/dhis-web/dhis-web-caseentry/src/main/java/org/hisp/dhis/caseentry/action/patient/SearchPatientAction.java 2012-07-06 09:59:36 +0000 @@ -30,18 +30,15 @@ import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import org.hisp.dhis.organisationunit.OrganisationUnit; +import org.hisp.dhis.ouwt.manager.OrganisationUnitSelectionManager; import org.hisp.dhis.paging.ActionPagingSupport; import org.hisp.dhis.patient.Patient; -import org.hisp.dhis.patient.PatientAttribute; -import org.hisp.dhis.patient.PatientAttributeService; import org.hisp.dhis.patient.PatientService; -import org.hisp.dhis.caseentry.state.SelectedStateManager; -import org.hisp.dhis.patientattributevalue.PatientAttributeValue; -import org.hisp.dhis.patientattributevalue.PatientAttributeValueService; /** * @author Abyot Asalefew Gizaw @@ -54,52 +51,34 @@ // Dependencies // ------------------------------------------------------------------------- - private SelectedStateManager selectedStateManager; + private OrganisationUnitSelectionManager selectionManager; private PatientService patientService; - private PatientAttributeValueService patientAttributeValueService; - - private PatientAttributeService patientAttributeService; - - // ------------------------------------------------------------------------- - // Input - // ------------------------------------------------------------------------- - - private List searchText = new ArrayList(); - - private Boolean listAll; - - private List searchObjectId = new ArrayList(); + // ------------------------------------------------------------------------- + // Input/output + // ------------------------------------------------------------------------- + + private List searchTexts = new ArrayList(); private Boolean searchBySelectedOrgunit; - // ------------------------------------------------------------------------- - // Output - // ------------------------------------------------------------------------- - - private Integer total; + private boolean listAll; private Collection patients = new ArrayList(); - private Map mapPatientPatientAttr = new HashMap(); - - private Map mapPatientOrgunit = new HashMap(); - - private List patientAttributes = new ArrayList(); - - // ------------------------------------------------------------------------- - // Getters/Setters - // ------------------------------------------------------------------------- - - public void setSelectedStateManager( SelectedStateManager selectedStateManager ) + // ------------------------------------------------------------------------- + // Getters && Setters + // ------------------------------------------------------------------------- + + public void setSelectionManager( OrganisationUnitSelectionManager selectionManager ) { - this.selectedStateManager = selectedStateManager; + this.selectionManager = selectionManager; } - public void setPatientAttributeService( PatientAttributeService patientAttributeService ) + public void setSearchBySelectedOrgunit( Boolean searchBySelectedOrgunit ) { - this.patientAttributeService = patientAttributeService; + this.searchBySelectedOrgunit = searchBySelectedOrgunit; } public void setPatientService( PatientService patientService ) @@ -107,59 +86,33 @@ this.patientService = patientService; } - public List getPatientAttributes() - { - return patientAttributes; - } - - public Map getMapPatientOrgunit() - { - return mapPatientOrgunit; - } - - public void setPatientAttributeValueService( PatientAttributeValueService patientAttributeValueService ) - { - this.patientAttributeValueService = patientAttributeValueService; - } - - public void setSearchBySelectedOrgunit( Boolean searchBySelectedOrgunit ) - { - this.searchBySelectedOrgunit = searchBySelectedOrgunit; - } - - public void setSearchText( List searchText ) - { - this.searchText = searchText; - } - - public void setListAll( Boolean listAll ) + public void setSearchTexts( List searchTexts ) + { + this.searchTexts = searchTexts; + } + + public void setListAll( boolean listAll ) { this.listAll = listAll; } - public Boolean getListAll() - { - return listAll; - } - - public void setSearchObjectId( List searchObjectId ) - { - this.searchObjectId = searchObjectId; - } - public Collection getPatients() { return patients; } + private Integer total; + public Integer getTotal() { return total; } - public Map getMapPatientPatientAttr() + private Map mapPatientOrgunit = new HashMap(); + + public Map getMapPatientOrgunit() { - return mapPatientPatientAttr; + return mapPatientOrgunit; } // ------------------------------------------------------------------------- @@ -169,85 +122,41 @@ public String execute() throws Exception { - OrganisationUnit selectOrgunit = selectedStateManager.getSelectedOrganisationUnit(); - - // --------------------------------------------------------------------- - // Get all of patients into the selected organisation unit - // --------------------------------------------------------------------- - - if ( listAll != null && listAll ) + OrganisationUnit organisationUnit = selectionManager.getSelectedOrganisationUnit(); + + // List all patients + if ( listAll ) { - listAllPatient( selectOrgunit ); - - return SUCCESS; + total = patientService.countGetPatientsByOrgUnit( organisationUnit ); + this.paging = createPaging( total ); + + patients = new ArrayList( patientService.getPatients( organisationUnit, paging.getStartPos(), + paging.getPageSize() ) ); + } - - // --------------------------------------------------------------------- - // Search patients by attributes - // --------------------------------------------------------------------- - - for ( Integer attributeId : searchObjectId ) + // search patients + else if ( searchTexts.size() > 0 ) { - if ( attributeId != null && attributeId > 0 ) + organisationUnit = (searchBySelectedOrgunit) ? organisationUnit : null; + + total = patientService.countSearchPatients( searchTexts, organisationUnit ); + this.paging = createPaging( total ); + patients = patientService.searchPatients( searchTexts, organisationUnit, paging.getStartPos(), + paging.getPageSize() ); + + for ( Patient patient : patients ) { - patientAttributes.add( patientAttributeService.getPatientAttribute( attributeId ) ); + mapPatientOrgunit.put( patient.getId(), getHierarchyOrgunit( patient.getOrganisationUnit() ) ); } } - searchBySelectedOrgunit = (searchBySelectedOrgunit == null) ? false : searchBySelectedOrgunit; - searchPatientByAttributes( searchObjectId, searchText, selectOrgunit, searchBySelectedOrgunit ); return SUCCESS; } // ------------------------------------------------------------------------- - // Supporting methods + // Supportive method // ------------------------------------------------------------------------- - private void listAllPatient( OrganisationUnit organisationUnit ) - { - total = patientService.countGetPatientsByOrgUnit( organisationUnit ); - this.paging = createPaging( total ); - - patients = new ArrayList( patientService.getPatients( organisationUnit, paging.getStartPos(), paging - .getPageSize() ) ); - } - - private void searchPatientByAttributes( List searchingAttributeId, List searchText, - OrganisationUnit orgunit, Boolean searchBySelectedOrgunit ) - { - if ( searchBySelectedOrgunit ) - { - total = patientAttributeValueService.countSearchPatients( searchingAttributeId, searchText, orgunit ); - this.paging = createPaging( total ); - patients = patientAttributeValueService.searchPatients( searchingAttributeId, searchText, orgunit, paging - .getStartPos(), paging.getPageSize() ); - } - else - { - total = patientAttributeValueService.countSearchPatients( searchingAttributeId, searchText ); - this.paging = createPaging( total ); - patients = patientAttributeValueService.searchPatients( searchingAttributeId, searchText, paging - .getStartPos(), paging.getPageSize() ); - } - - Collection attributeValues = patientAttributeValueService - .getPatientAttributeValues( patients ); - - for ( Patient patient : patients ) - { - if ( !searchBySelectedOrgunit ) - { - mapPatientOrgunit.put( patient.getId(), getHierarchyOrgunit( patient.getOrganisationUnit() ) ); - } - - for ( PatientAttributeValue attributeValue : attributeValues ) - { - mapPatientPatientAttr.put( patient.getId() + "-" + attributeValue.getPatientAttribute().getId(), - attributeValue.getValue() ); - } - } - } - private String getHierarchyOrgunit( OrganisationUnit orgunit ) { String hierarchyOrgunit = orgunit.getName(); === modified file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/META-INF/dhis/beans.xml' --- dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/META-INF/dhis/beans.xml 2012-07-05 09:07:21 +0000 +++ dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/META-INF/dhis/beans.xml 2012-07-06 09:59:36 +0000 @@ -25,18 +25,14 @@ ref="org.hisp.dhis.ouwt.manager.OrganisationUnitSelectionManager" /> - - - - + - - - - - - - === modified file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/struts.xml' --- dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/struts.xml 2012-07-05 09:07:21 +0000 +++ dhis-2/dhis-web/dhis-web-caseentry/src/main/resources/struts.xml 2012-07-06 09:59:36 +0000 @@ -52,7 +52,7 @@ + class="org.hisp.dhis.caseentry.action.patient.SearchPatientAction"> /content.vm true /dhis-web-caseentry/listPatient.vm === modified file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/javascript/commons.js' --- dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/javascript/commons.js 2012-07-05 20:16:19 +0000 +++ dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/javascript/commons.js 2012-07-06 09:59:36 +0000 @@ -189,36 +189,19 @@ }); if(flag){ - jQuery("#searchDiv :input").each( function( i, item ) - { - var elementId = $(this).attr('id'); - var elementName = $(this).attr('name'); - if( elementId =='dateOperator' ) - { - dateOperator = jQuery(this).val(); - } - else - { - var value = ''; - if( jQuery(this).attr('type')=='checkbox' ){ - value = byId(this.id).checked; - } - else if( jQuery(this).val()!='' ){ - value = htmlEncode(jQuery(this).val()); - } - if( dateOperator != '' ) - { - value = dateOperator + "'" + value + "'"; - dateOperator = ""; - } - if( elementName=='searchText') - params += "searchText="; - else - params += elementId + "="; - - params += value + "&"; - } + jQuery( '#advancedSearchTB tbody tr' ).each( function( i, row ){ + jQuery( this ).find(':input').each( function( idx, item ){ + if( idx == 0){ + params += "searchTexts=" + item.value; + } + else if( idx == 1){ + params += "_" + htmlEncode( item.value.toLowerCase() ); + } + }) }); + params += '&listAll=false'; + params += '&searchBySelectedOrgunit=' + byId('searchBySelectedOrgunit').checked; + contentDiv = 'listPatientDiv'; jQuery( "#loaderDiv" ).show(); advancedSearch( params ); === modified file 'dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/searchPatientCriteria.vm' --- dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/searchPatientCriteria.vm 2012-07-05 09:52:36 +0000 +++ dhis-2/dhis-web/dhis-web-caseentry/src/main/webapp/dhis-web-caseentry/searchPatientCriteria.vm 2012-07-06 09:59:36 +0000 @@ -31,14 +31,14 @@ *