| 143 | | @SuppressWarnings("unchecked") |
|---|
| 144 | | public Set<Patient> getPatientsByIdentifier(String identifier, boolean includeVoided) throws DAOException { |
|---|
| 145 | | Query query; |
|---|
| 146 | | |
|---|
| 147 | | String sql = "select patient from Patient patient, PersonName name join patient.identifiers ids where ids.identifier = :id and patient.patientId = name.person.personId"; |
|---|
| 148 | | String order = " order by name.givenName asc, name.middleName asc, name.familyName asc"; |
|---|
| 149 | | |
|---|
| 150 | | if (includeVoided) { |
|---|
| 151 | | query = sessionFactory.getCurrentSession().createQuery(sql + order); |
|---|
| 152 | | query.setString("id", identifier); |
|---|
| 153 | | } |
|---|
| 154 | | else { |
|---|
| 155 | | query = sessionFactory.getCurrentSession().createQuery(sql + " and patient.voided = :void" + order); |
|---|
| 156 | | query.setString("id", identifier); |
|---|
| 157 | | query.setBoolean("void", includeVoided); |
|---|
| 158 | | } |
|---|
| 159 | | |
|---|
| 160 | | Set<Patient> returnSet = new LinkedHashSet<Patient>(); |
|---|
| 161 | | returnSet.addAll(query.list()); |
|---|
| 162 | | |
|---|
| 163 | | return returnSet; |
|---|
| 164 | | } |
|---|
| 165 | | |
|---|
| 166 | | |
|---|
| 167 | | |
|---|
| 168 | | /** |
|---|
| 169 | | * @see org.openmrs.api.db.PatientDAO#getPatientsByIdentifierPattern(java.lang.String, boolean) |
|---|
| 170 | | */ |
|---|
| 171 | | @SuppressWarnings("unchecked") |
|---|
| 172 | | public Collection<Patient> getPatientsByIdentifierPattern(String identifier, boolean includeVoided) throws DAOException { |
|---|
| 173 | | |
|---|
| 174 | | Criteria criteria = sessionFactory.getCurrentSession().createCriteria(PatientIdentifier.class); |
|---|
| 175 | | criteria.setProjection(Projections.property("patient")); |
|---|
| 176 | | |
|---|
| 177 | | AdministrationService adminService = Context.getAdministrationService(); |
|---|
| 178 | | String regex = adminService.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_IDENTIFIER_REGEX, ""); |
|---|
| 179 | | |
|---|
| 180 | | // if the regex is empty, default to a simple "like" search |
|---|
| 181 | | if (regex.equals("")) { |
|---|
| 182 | | String prefix = adminService.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_IDENTIFIER_PREFIX, ""); |
|---|
| 183 | | String suffix = adminService.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_IDENTIFIER_SUFFIX, ""); |
|---|
| 184 | | StringBuffer likeString = new StringBuffer(prefix).append(identifier).append(suffix); |
|---|
| 185 | | criteria.add(Expression.like("identifier", likeString.toString())); |
|---|
| 186 | | } |
|---|
| 187 | | // if the regex is present, search on that |
|---|
| 188 | | else { |
|---|
| 189 | | regex = regex.replace("@SEARCH@", identifier); |
|---|
| 190 | | criteria.add(Restrictions.sqlRestriction("identifier regexp '" + regex + "'")); |
|---|
| 191 | | } |
|---|
| 192 | | |
|---|
| 193 | | if (includeVoided == false) { |
|---|
| 194 | | criteria.createAlias("patient", "pat"); |
|---|
| 195 | | criteria.add(Restrictions.eq("pat.voided", false)); |
|---|
| 196 | | } |
|---|
| 197 | | |
|---|
| 198 | | criteria.setFirstResult(0); |
|---|
| 199 | | criteria.setMaxResults(getMaximumSearchResults()); |
|---|
| 200 | | |
|---|
| 201 | | return criteria.list(); |
|---|
| 202 | | } |
|---|
| 203 | | |
|---|
| 204 | | @SuppressWarnings("unchecked") |
|---|
| 205 | | public Collection<Patient> getPatientsByName(String name, boolean includeVoided) throws DAOException { |
|---|
| 206 | | //TODO simple name search to start testing, will need to make "real" name search |
|---|
| 207 | | // i.e. split on whitespace, guess at first/last name, etc |
|---|
| 208 | | // TODO return the matched name instead of the primary name |
|---|
| 209 | | // possible solution: "select new" org.openmrs.PatientListItem and return a list of those |
|---|
| 210 | | |
|---|
| 211 | | name = name.replaceAll(" ", " "); |
|---|
| 212 | | name = name.replace(", ", " "); |
|---|
| 213 | | String[] names = name.split(" "); |
|---|
| 214 | | |
|---|
| 215 | | if (log.isDebugEnabled()) |
|---|
| 216 | | log.debug("name: " + name); |
|---|
| 217 | | |
|---|
| 218 | | Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Patient.class).createAlias("names", "name"); |
|---|
| 219 | | for (String n : names) { |
|---|
| 220 | | if (n != null && n.length() > 0) { |
|---|
| 221 | | criteria.add(Expression.or( |
|---|
| 222 | | Expression.like("name.familyName", n, MatchMode.START), |
|---|
| 223 | | Expression.or( |
|---|
| 224 | | Expression.like("name.middleName", n, MatchMode.START), |
|---|
| 225 | | Expression.like("name.givenName", n, MatchMode.START) |
|---|
| 226 | | ) |
|---|
| 227 | | ) |
|---|
| 228 | | ); |
|---|
| 229 | | } |
|---|
| 230 | | } |
|---|
| 231 | | |
|---|
| 232 | | if (includeVoided == false) |
|---|
| 233 | | criteria.add(Expression.eq("voided", new Boolean(false))); |
|---|
| 234 | | |
|---|
| | 141 | /** |
|---|
| | 142 | * @see org.openmrs.api.db.PatientDAO#getPatients(java.lang.String, java.lang.String, java.util.List) |
|---|
| | 143 | */ |
|---|
| | 144 | @SuppressWarnings("unchecked") |
|---|
| | 145 | public List<Patient> getPatients(String name, String identifier, |
|---|
| | 146 | List<PatientIdentifierType> identifierTypes) throws DAOException { |
|---|
| | 147 | |
|---|
| | 148 | Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Patient.class); |
|---|
| | 149 | |
|---|
| | 150 | criteria.createAlias("names", "name"); |
|---|
| | 151 | criteria.add(Expression.eq("name.voided", false)); |
|---|
| 238 | | |
|---|
| | 155 | |
|---|
| | 156 | if (name != null) { |
|---|
| | 157 | // TODO simple name search to start testing, will need to make "real" |
|---|
| | 158 | // name search |
|---|
| | 159 | // i.e. split on whitespace, guess at first/last name, etc |
|---|
| | 160 | // TODO return the matched name instead of the primary name |
|---|
| | 161 | // possible solution: "select new" org.openmrs.PatientListItem and |
|---|
| | 162 | // return a list of those |
|---|
| | 163 | |
|---|
| | 164 | name = name.replaceAll(" ", " "); |
|---|
| | 165 | name = name.replace(", ", " "); |
|---|
| | 166 | String[] names = name.split(" "); |
|---|
| | 167 | |
|---|
| | 168 | // TODO add junit test for searching on voided patient names |
|---|
| | 169 | |
|---|
| | 170 | String nameSoFar = names[0]; |
|---|
| | 171 | for (int i=0 ; i < names.length ; i++) { |
|---|
| | 172 | String n = names[i]; |
|---|
| | 173 | if (n != null && n.length() > 0) { |
|---|
| | 174 | LogicalExpression oneNameSearch = getNameSearch(n); |
|---|
| | 175 | LogicalExpression searchExpression = oneNameSearch; |
|---|
| | 176 | if(i>0){ |
|---|
| | 177 | nameSoFar += " " + n; |
|---|
| | 178 | LogicalExpression fullNameSearch = getNameSearch(nameSoFar); |
|---|
| | 179 | searchExpression = Expression.or(oneNameSearch, fullNameSearch); |
|---|
| | 180 | } |
|---|
| | 181 | criteria.add(searchExpression); |
|---|
| | 182 | } |
|---|
| | 183 | } |
|---|
| | 184 | |
|---|
| | 185 | } |
|---|
| | 186 | |
|---|
| | 187 | // do the restriction on either identifier string or types |
|---|
| | 188 | if (identifier != null || identifierTypes.size() > 0) { |
|---|
| | 189 | |
|---|
| | 190 | // TODO add junit test for searching on voided identifiers |
|---|
| | 191 | |
|---|
| | 192 | // add the join on the identifiers table |
|---|
| | 193 | criteria.createAlias("identifiers", "ids"); |
|---|
| | 194 | criteria.add(Expression.eq("ids.voided", false)); |
|---|
| | 195 | |
|---|
| | 196 | // do the identifier restriction |
|---|
| | 197 | if (identifier != null) { |
|---|
| | 198 | AdministrationService adminService = Context.getAdministrationService(); |
|---|
| | 199 | String regex = adminService.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_IDENTIFIER_REGEX, ""); |
|---|
| | 200 | |
|---|
| | 201 | // if the regex is empty, default to a simple "like" search or if |
|---|
| | 202 | // we're in hsql world, also only do the simple like search (because |
|---|
| | 203 | // hsql doesn't know how to deal with 'regexp' |
|---|
| | 204 | if (regex.equals("") || HibernateUtil.isHSQLDialect(sessionFactory)) { |
|---|
| | 205 | String prefix = adminService.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_IDENTIFIER_PREFIX, ""); |
|---|
| | 206 | String suffix = adminService.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_IDENTIFIER_SUFFIX, ""); |
|---|
| | 207 | StringBuffer likeString = new StringBuffer(prefix).append(identifier) |
|---|
| | 208 | .append(suffix); |
|---|
| | 209 | criteria.add(Expression.like("ids.identifier", likeString.toString())); |
|---|
| | 210 | } |
|---|
| | 211 | // if the regex is present, search on that |
|---|
| | 212 | else { |
|---|
| | 213 | regex = regex.replace("@SEARCH@", identifier); |
|---|
| | 214 | criteria.add(Restrictions.sqlRestriction("identifier regexp ?", regex, Hibernate.STRING)); |
|---|
| | 215 | } |
|---|
| | 216 | } |
|---|
| | 217 | |
|---|
| | 218 | // TODO add a junit test for patientIdentifierType restrictions |
|---|
| | 219 | |
|---|
| | 220 | // do the type restriction |
|---|
| | 221 | if (identifierTypes.size() > 0) { |
|---|
| | 222 | criteria.add(Expression.in("ids.identifierType", identifierTypes)); |
|---|
| | 223 | } |
|---|
| | 224 | } |
|---|
| | 225 | |
|---|
| | 226 | // TODO add junit test for searching on voided patients |
|---|
| | 227 | |
|---|
| | 228 | // make sure the patient object isn't voided |
|---|
| | 229 | criteria.add(Expression.eq("voided", false)); |
|---|
| | 230 | |
|---|
| | 231 | // restricting the search to the max search results value |
|---|
| 246 | | * @see org.openmrs.api.db.PatientService#deletePatient(org.openmrs.Patient) |
|---|
| | 239 | * Auto generated method comment |
|---|
| | 240 | * |
|---|
| | 241 | * @param name |
|---|
| | 242 | * @return |
|---|
| | 243 | */ |
|---|
| | 244 | private LogicalExpression getNameSearch(String name){ |
|---|
| | 245 | // this criteria is essentially: |
|---|
| | 246 | // where voided = false && name in [familyName, middleName, givenName] |
|---|
| | 247 | return Expression.and(Expression.eq("name.voided", false), |
|---|
| | 248 | Expression.or(Expression.like("name.familyName", name, MatchMode.START), |
|---|
| | 249 | Expression.or(Expression.like("name.middleName", name, MatchMode.START), |
|---|
| | 250 | Expression.like("name.givenName", name, MatchMode.START)))); |
|---|
| | 251 | } |
|---|
| | 252 | |
|---|
| | 253 | /** |
|---|
| | 254 | * @see org.openmrs.api.db.PatientDAO#getAllPatients(boolean) |
|---|
| | 255 | */ |
|---|
| | 256 | @SuppressWarnings("unchecked") |
|---|
| | 257 | public List<Patient> getAllPatients(boolean includeVoided) |
|---|
| | 258 | throws DAOException { |
|---|
| | 259 | Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Patient.class); |
|---|
| | 260 | |
|---|
| | 261 | if (includeVoided == false) |
|---|
| | 262 | criteria.add(Expression.eq("voided", false)); |
|---|
| | 263 | |
|---|
| | 264 | return criteria.list(); |
|---|
| | 265 | } |
|---|
| | 266 | |
|---|
| | 267 | /** |
|---|
| | 268 | * @see org.openmrs.api.PatientService#purgePatientIdentifierType(org.openmrs.PatientIdentifierType) |
|---|
| | 269 | * @see org.openmrs.api.db.PatientDAO#deletePatientIdentifierType(org.openmrs.PatientIdentifierType) |
|---|
| | 270 | */ |
|---|
| | 271 | public void deletePatientIdentifierType( |
|---|
| | 272 | PatientIdentifierType patientIdentifierType) throws DAOException { |
|---|
| | 273 | sessionFactory.getCurrentSession().delete(patientIdentifierType); |
|---|
| | 274 | } |
|---|
| | 275 | |
|---|
| | 276 | /** |
|---|
| | 277 | * @see org.openmrs.api.db.PatientDAO#getPatientIdentifiers(java.lang.String, java.util.List, java.util.List, java.util.List, java.lang.Boolean) |
|---|
| | 278 | * @see org.openmrs.api.PatientService#getPatientIdentifiers(java.lang.String, java.util.List, java.util.List, java.util.List, java.lang.Boolean) |
|---|
| | 279 | */ |
|---|
| | 280 | @SuppressWarnings("unchecked") |
|---|
| | 281 | public List<PatientIdentifier> getPatientIdentifiers(String identifier, |
|---|
| | 282 | List<PatientIdentifierType> patientIdentifierTypes, |
|---|
| | 283 | List<Location> locations, List<Patient> patients, |
|---|
| | 284 | Boolean isPreferred) |
|---|
| | 285 | throws DAOException { |
|---|
| | 286 | Criteria criteria = sessionFactory.getCurrentSession().createCriteria(PatientIdentifier.class); |
|---|
| | 287 | |
|---|
| | 288 | // TODO add junit test for not getting voided |
|---|
| | 289 | // make sure the patient object isn't voided |
|---|
| | 290 | criteria.add(Expression.eq("voided", false)); |
|---|
| | 291 | |
|---|
| | 292 | // TODO add junit test for getting by identifier (and for not getting by partial here) |
|---|
| | 293 | if (identifier != null) |
|---|
| | 294 | criteria.add(Expression.eq("identifier", identifier)); |
|---|
| | 295 | |
|---|
| | 296 | // TODO add junit test for getting by identifier type |
|---|
| | 297 | if (patientIdentifierTypes.size() > 0) |
|---|
| | 298 | criteria.add(Expression.in("identifierType", patientIdentifierTypes)); |
|---|
| | 299 | |
|---|
| | 300 | // TODO add junit test for getting by patients |
|---|
| | 301 | if (patients.size() > 0) |
|---|
| | 302 | criteria.add(Expression.in("patient", patients)); |
|---|
| | 303 | |
|---|
| | 304 | // TODO add junit test for getting by null/true/false isPreferred |
|---|
| | 305 | if (isPreferred != null) |
|---|
| | 306 | criteria.add(Expression.eq("preferred", isPreferred)); |
|---|
| | 307 | |
|---|
| | 308 | return criteria.list(); |
|---|
| | 309 | } |
|---|
| | 310 | |
|---|
| | 311 | /** |
|---|
| | 312 | * @see org.openmrs.api.db.PatientDAO#savePatientIdentifierType(org.openmrs.PatientIdentifierType) |
|---|
| | 313 | */ |
|---|
| | 314 | public PatientIdentifierType savePatientIdentifierType( |
|---|
| | 315 | PatientIdentifierType patientIdentifierType) throws DAOException { |
|---|
| | 316 | sessionFactory.getCurrentSession().saveOrUpdate(patientIdentifierType); |
|---|
| | 317 | return patientIdentifierType; |
|---|
| | 318 | } |
|---|
| | 319 | |
|---|
| | 320 | /** |
|---|
| | 321 | * @see org.openmrs.api.PatientService#deletePatient(org.openmrs.Patient) |
|---|
| 251 | | |
|---|
| 252 | | /** |
|---|
| 253 | | * @see org.openmrs.api.db.PatientService#getPatientIdentifiers(org.openmrs.PatientIdentifierType) |
|---|
| 254 | | */ |
|---|
| 255 | | @SuppressWarnings("unchecked") |
|---|
| 256 | | public List<PatientIdentifier> getPatientIdentifiers(PatientIdentifierType pit) throws DAOException { |
|---|
| 257 | | List<PatientIdentifier> patientIdentifiers = sessionFactory.getCurrentSession().createQuery("from PatientIdentifier p where p.identifierType = :pit and p.voided = false") |
|---|
| 258 | | .setParameter("pit", pit) |
|---|
| 259 | | .list(); |
|---|
| 260 | | |
|---|
| 261 | | return patientIdentifiers; |
|---|
| 262 | | } |
|---|
| 263 | | |
|---|
| 264 | | /** |
|---|
| 265 | | * @see org.openmrs.api.db.PatientService#getPatientIdentifiers(java.lang.String,org.openmrs.PatientIdentifierType) |
|---|
| 266 | | */ |
|---|
| 267 | | @SuppressWarnings("unchecked") |
|---|
| 268 | | public List<PatientIdentifier> getPatientIdentifiers(String identifier, PatientIdentifierType pit) throws DAOException { |
|---|
| 269 | | List<PatientIdentifier> ids; |
|---|
| 270 | | ids = sessionFactory.getCurrentSession().createQuery("from PatientIdentifier p where p.identifierType = :pit and p.identifier = :id and p.voided = false") |
|---|
| 271 | | .setParameter("pit", pit) |
|---|
| 272 | | .setString("id", identifier) |
|---|
| 273 | | .list(); |
|---|
| 274 | | |
|---|
| 275 | | return ids; |
|---|
| 276 | | } |
|---|
| 277 | | |
|---|
| 278 | | /** |
|---|
| 279 | | * Only updates the identifier type at the moment |
|---|
| 280 | | * |
|---|
| 281 | | * |
|---|
| 282 | | * @see org.openmrs.api.db.PatientService#updatePatientIdentifier(org.openmrs.PatientIdentifier) |
|---|
| 283 | | */ |
|---|
| 284 | | public void updatePatientIdentifier(PatientIdentifier pi) throws DAOException { |
|---|
| 285 | | log.debug("type: " + pi.getIdentifierType().getName()); |
|---|
| 286 | | sessionFactory.getCurrentSession().createQuery("update PatientIdentifier p set p.identifierType = :pit where p.patient = :pat and p.identifier = :id and p.location = :loc") |
|---|
| 287 | | .setParameter("pit", pi.getIdentifierType()) |
|---|
| 288 | | .setParameter("pat", pi.getPatient()) |
|---|
| 289 | | .setParameter("id", pi.getIdentifier()) |
|---|
| 290 | | .setParameter("loc", pi.getLocation()) |
|---|
| 291 | | .executeUpdate(); |
|---|
| 292 | | } |
|---|
| 293 | | |
|---|
| 294 | | /** |
|---|
| 295 | | * @see org.openmrs.api.db.PatientService#getPatientIdentifierType(java.lang.Integer) |
|---|
| 296 | | */ |
|---|
| 297 | | public PatientIdentifierType getPatientIdentifierType(Integer patientIdentifierTypeId) throws DAOException { |
|---|
| 298 | | PatientIdentifierType patientIdentifierType = (PatientIdentifierType) sessionFactory.getCurrentSession().get(PatientIdentifierType.class, patientIdentifierTypeId); |
|---|
| 299 | | |
|---|
| 300 | | return patientIdentifierType; |
|---|
| 301 | | } |
|---|
| 302 | | |
|---|
| 303 | | /** |
|---|
| 304 | | * @see org.openmrs.api.db.PatientService#getPatientIdentifierType(java.lang.String) |
|---|
| 305 | | */ |
|---|
| 306 | | public PatientIdentifierType getPatientIdentifierType(String name) throws DAOException { |
|---|
| 307 | | PatientIdentifierType ret = (PatientIdentifierType) sessionFactory.getCurrentSession().createQuery("from PatientIdentifierType t where t.name = :name") |
|---|
| 308 | | .setString("name", name) |
|---|
| 309 | | .uniqueResult(); |
|---|
| 310 | | |
|---|
| 311 | | return ret; |
|---|
| | 326 | |
|---|
| | 327 | /** |
|---|
| | 328 | * @see org.openmrs.api.PatientService#getPatientIdentifierType(java.lang.Integer) |
|---|
| | 329 | */ |
|---|
| | 330 | public PatientIdentifierType getPatientIdentifierType( |
|---|
| | 331 | Integer patientIdentifierTypeId) throws DAOException { |
|---|
| | 332 | return (PatientIdentifierType) sessionFactory.getCurrentSession() |
|---|
| | 333 | .get(PatientIdentifierType.class, |
|---|
| | 334 | patientIdentifierTypeId); |
|---|
| | 335 | } |
|---|
| | 336 | |
|---|
| | 337 | /** |
|---|
| | 338 | * @see org.openmrs.api.db.PatientDAO#getAllPatientIdentifierTypes(boolean) |
|---|
| | 339 | */ |
|---|
| | 340 | @SuppressWarnings("unchecked") |
|---|
| | 341 | public List<PatientIdentifierType> getAllPatientIdentifierTypes(boolean includeRetired) |
|---|
| | 342 | throws DAOException { |
|---|
| | 343 | |
|---|
| | 344 | // TODO test this method |
|---|
| | 345 | |
|---|
| | 346 | Criteria criteria = sessionFactory.getCurrentSession().createCriteria(PatientIdentifierType.class); |
|---|
| | 347 | criteria.addOrder(Order.asc("name")); |
|---|
| | 348 | |
|---|
| | 349 | if (includeRetired == false) |
|---|
| | 350 | criteria.add(Expression.eq("retired", false)); |
|---|
| | 351 | |
|---|
| | 352 | return criteria.list(); |
|---|
| 315 | | * @see org.openmrs.api.db.PatientService#getPatientIdentifierTypes() |
|---|
| 316 | | */ |
|---|
| 317 | | @SuppressWarnings("unchecked") |
|---|
| 318 | | public List<PatientIdentifierType> getPatientIdentifierTypes() throws DAOException { |
|---|
| 319 | | List<PatientIdentifierType> patientIdentifierTypes = sessionFactory.getCurrentSession().createQuery("from PatientIdentifierType p order by p.name").list(); |
|---|
| 320 | | |
|---|
| 321 | | return patientIdentifierTypes; |
|---|
| 322 | | } |
|---|
| 323 | | |
|---|
| 324 | | /** |
|---|
| 325 | | * @see org.openmrs.api.db.PatientService#getTribe() |
|---|
| | 356 | * @see org.openmrs.api.db.PatientDAO#getPatientIdentifierTypes(java.lang.String, java.lang.String, java.lang.Boolean, java.lang.Boolean) |
|---|
| | 357 | */ |
|---|
| | 358 | @SuppressWarnings("unchecked") |
|---|
| | 359 | public List<PatientIdentifierType> getPatientIdentifierTypes(String name, |
|---|
| | 360 | String format, Boolean required, Boolean hasCheckDigit) |
|---|
| | 361 | throws DAOException { |
|---|
| | 362 | // TODO test this method |
|---|
| | 363 | |
|---|
| | 364 | Criteria criteria = sessionFactory.getCurrentSession().createCriteria(PatientIdentifierType.class); |
|---|
| | 365 | criteria.addOrder(Order.asc("name")); |
|---|
| | 366 | |
|---|
| | 367 | if (name != null) |
|---|
| | 368 | criteria.add(Expression.eq("name", name)); |
|---|
| | 369 | |
|---|
| | 370 | if (format != null) |
|---|
| | 371 | criteria.add(Expression.eq("format", format)); |
|---|
| | 372 | |
|---|
| | 373 | if (required != null) |
|---|
| | 374 | criteria.add(Expression.eq("required", required)); |
|---|
| | 375 | |
|---|
| | 376 | if (hasCheckDigit!= null) |
|---|
| | 377 | criteria.add(Expression.eq("checkDigit", hasCheckDigit)); |
|---|
| | 378 | |
|---|
| | 379 | criteria.add(Expression.eq("retired", false)); |
|---|
| | 380 | |
|---|
| | 381 | return criteria.list(); |
|---|
| | 382 | } |
|---|
| | 383 | |
|---|
| | 384 | /** |
|---|
| | 385 | * @see org.openmrs.api.PatientService#getTribe() |
|---|
| | 386 | * @deprecated tribe will be moved to patient attribute |
|---|
| 440 | | if (attributes.size() > 0) { |
|---|
| 441 | | String select = "select p from Patient p"; |
|---|
| 442 | | String where = " where 1=1 "; |
|---|
| 443 | | String groupBy= " group by "; |
|---|
| 444 | | String having = " having count(p.patientId) > 1"; |
|---|
| 445 | | |
|---|
| 446 | | Class patient = Patient.class; |
|---|
| 447 | | Set<String> patientFieldNames = new HashSet<String>(patient.getDeclaredFields().length); |
|---|
| 448 | | for (Field f : patient.getDeclaredFields()){ |
|---|
| 449 | | patientFieldNames.add(f.getName()); |
|---|
| 450 | | log.debug(f.getName()); |
|---|
| 451 | | } |
|---|
| 452 | | |
|---|
| 453 | | Class patientName = PersonName.class; |
|---|
| 454 | | Set<String> patientNameFieldNames = new HashSet<String>(patientName.getDeclaredFields().length); |
|---|
| 455 | | for (Field f : patientName.getDeclaredFields()){ |
|---|
| 456 | | patientNameFieldNames.add(f.getName()); |
|---|
| 457 | | log.debug(f.getName()); |
|---|
| 458 | | } |
|---|
| 459 | | |
|---|
| 460 | | Class identifier = PatientIdentifier.class; |
|---|
| 461 | | Set<String> identifierFieldNames = new HashSet<String>(identifier.getDeclaredFields().length); |
|---|
| 462 | | for (Field f : identifier.getDeclaredFields()){ |
|---|
| 463 | | identifierFieldNames.add(f.getName()); |
|---|
| 464 | | log.debug(f.getName()); |
|---|
| 465 | | } |
|---|
| 466 | | |
|---|
| 467 | | for (String s : attributes) { |
|---|
| 468 | | if (patientFieldNames.contains(s)) { |
|---|
| 469 | | groupBy += "p." + s + ", "; |
|---|
| 470 | | } |
|---|
| 471 | | else if (patientNameFieldNames.contains(s)) { |
|---|
| 472 | | if (!select.contains("PersonName")) { |
|---|
| 473 | | select += ", PersonName pn"; |
|---|
| 474 | | where += "and p = pn.patient "; |
|---|
| 475 | | } |
|---|
| 476 | | groupBy += "pn." + s + ", "; |
|---|
| 477 | | } |
|---|
| 478 | | else if (identifierFieldNames.contains(s)) { |
|---|
| 479 | | if (!select.contains("PatientIdentifier")) { |
|---|
| 480 | | select += ", PatientIdentifier pi"; |
|---|
| 481 | | where += "and p = pi.patient "; |
|---|
| 482 | | } |
|---|
| 483 | | groupBy += "pi." + s + ", "; |
|---|
| 484 | | } |
|---|
| 485 | | else |
|---|
| 486 | | log.warn("Unidentified attribute: " + s); |
|---|
| 487 | | } |
|---|
| 488 | | |
|---|
| 489 | | int index = groupBy.lastIndexOf(", "); |
|---|
| 490 | | groupBy = groupBy.substring(0, index); |
|---|
| 491 | | |
|---|
| 492 | | select = select + where + groupBy + having; |
|---|
| 493 | | |
|---|
| 494 | | Query query = session.createQuery(select); |
|---|
| 495 | | |
|---|
| 496 | | patients = query.list(); |
|---|
| 497 | | } |
|---|
| | 506 | * if (attributes.size() > 0) { String select = "select p from Patient |
|---|
| | 507 | * p"; String where = " where 1=1 "; String groupBy= " group by "; |
|---|
| | 508 | * String having = " having count(p.patientId) > 1"; |
|---|
| | 509 | * |
|---|
| | 510 | * Class patient = Patient.class; Set<String> patientFieldNames = new |
|---|
| | 511 | * HashSet<String>(patient.getDeclaredFields().length); for (Field f : |
|---|
| | 512 | * patient.getDeclaredFields()){ patientFieldNames.add(f.getName()); |
|---|
| | 513 | * log.debug(f.getName()); } |
|---|
| | 514 | * |
|---|
| | 515 | * Class patientName = PersonName.class; Set<String> |
|---|
| | 516 | * patientNameFieldNames = new HashSet<String>(patientName.getDeclaredFields().length); |
|---|
| | 517 | * for (Field f : patientName.getDeclaredFields()){ |
|---|
| | 518 | * patientNameFieldNames.add(f.getName()); log.debug(f.getName()); } |
|---|
| | 519 | * |
|---|
| | 520 | * Class identifier = PatientIdentifier.class; Set<String> |
|---|
| | 521 | * identifierFieldNames = new HashSet<String>(identifier.getDeclaredFields().length); |
|---|
| | 522 | * for (Field f : identifier.getDeclaredFields()){ |
|---|
| | 523 | * identifierFieldNames.add(f.getName()); log.debug(f.getName()); } |
|---|
| | 524 | * |
|---|
| | 525 | * for (String s : attributes) { if (patientFieldNames.contains(s)) { |
|---|
| | 526 | * groupBy += "p." + s + ", "; } else if |
|---|
| | 527 | * (patientNameFieldNames.contains(s)) { if |
|---|
| | 528 | * (!select.contains("PersonName")) { select += ", PersonName pn"; where += |
|---|
| | 529 | * "and p = pn.patient "; } groupBy += "pn." + s + ", "; } else if |
|---|
| | 530 | * (identifierFieldNames.contains(s)) { if |
|---|
| | 531 | * (!select.contains("PatientIdentifier")) { select += ", |
|---|
| | 532 | * PatientIdentifier pi"; where += "and p = pi.patient "; } groupBy += |
|---|
| | 533 | * "pi." + s + ", "; } else log.warn("Unidentified attribute: " + s); } |
|---|
| | 534 | * |
|---|
| | 535 | * int index = groupBy.lastIndexOf(", "); groupBy = groupBy.substring(0, |
|---|
| | 536 | * index); |
|---|
| | 537 | * |
|---|
| | 538 | * select = select + where + groupBy + having; |
|---|
| | 539 | * |
|---|
| | 540 | * Query query = session.createQuery(select); |
|---|
| | 541 | * |
|---|
| | 542 | * patients = query.list(); } |
|---|