Permalink
Browse files

Cleaned up PatientDao

  • Loading branch information...
1 parent daf4440 commit 93d108121e1a0743f60c6ea1c090537c7dd1ae6d @dieterichlawson committed Oct 12, 2010
@@ -1,6 +1,5 @@
package net.frontlinesms.plugins.patientview.data.repository;
-import java.util.Collection;
import java.util.List;
import net.frontlinesms.plugins.patientview.data.domain.people.CommunityHealthWorker;
@@ -9,47 +8,68 @@
public interface PatientDao {
/**
- * Returns the patients of a CHW
- *
- * @param chw the CHW
- * @return the patients
- */
- public Collection<Patient> getPatientsForCHW(
- CommunityHealthWorker chw);
-
- /**
* Saves a Patient to the data source
*
- * @param p the Patient to save
+ * @param patient
+ * the Patient to save
*/
- public void savePatient(Patient p);
+ public void savePatient(Patient patient);
/**
* Updates a Patient in the data source
*
- * @param p the Patient to update
+ * @param patient
+ * the Patient to update
*/
- public void updatePatient(Patient p);
+ public void updatePatient(Patient patient);
/**
* Deletes a Patient from the data source.
*
- * @param p Patient to save
+ * @param patient
+ * Patient to delete
*/
- public void deletePatient(Patient p);
+ public void deletePatient(Patient patient);
- /** @return all CHWs saved in the data source */
+ /**
+ * @return all Patients saved in the data source
+ **/
public List<Patient> getAllPatients();
+
+ /**
+ * Returns the patients of a CHW
+ *
+ * @param chw
+ * the CHW
+ * @return the patients of the CHW
+ */
+ public List<Patient> getPatientsForCHW(CommunityHealthWorker chw);
- /** get all patients with string s in their name**/
- public List<Patient> getPatientsByName(String s);
-
- /**get limit # of patients with string s in their name**/
- public List<Patient> getPatientsByNameWithLimit(String s, int limit);
-
+ /**
+ * Returns all patients with the given nameFragment somewhere in their name
+ **/
+ public List<Patient> findPatientsByName(String nameFragment);
+
+ /**
+ * Returns (at most) the specified number of patients with the given
+ * nameFragment somewhere in their name
+ **/
+ public List<Patient> findPatientsByName(String nameFragment, int resultLimit);
+
+ /**
+ * Returns the patient with the supplied ID. If there is no such patient,
+ * null is returned.
+ *
+ * @param id
+ * @return
+ */
public Patient getPatientById(Long id);
-
- public Patient getPatient(String name, String birthdate, String id);
+
+ /**
+ * Finds a patient by name, birthdate, or id. Any of these parameters can be
+ * null. If there is no such patient, null is returned.
+ */
+ public Patient findPatient(String name, String birthdate, String id);
public void voidPatient(Patient patient, boolean keepVisible, String reason);
}
@@ -2,8 +2,6 @@
import java.text.ParseException;
-import java.util.ArrayList;
-import java.util.Collection;
import java.util.Date;
import java.util.List;
@@ -16,6 +14,7 @@
import net.frontlinesms.ui.i18n.InternationalisationUtils;
import org.hibernate.criterion.DetachedCriteria;
+import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
public class HibernatePatientDao extends BaseHibernateDao<Patient> implements PatientDao {
@@ -24,66 +23,50 @@ protected HibernatePatientDao() {
super(Patient.class);
}
- //TODO fix this so it works
- public Collection<Patient> getPatientsForCHW(CommunityHealthWorker chw) {
- DetachedCriteria criteria = getBaseCriterion();
- criteria.add(Restrictions.eq("chw", chw));
- return super.getList(criteria);
- }
-
- /** @see PatientDao#savePatient(Patient) */
public void savePatient(Patient p) {
super.saveWithoutDuplicateHandling(p);
}
- /** @see PatientDao#updatePatient(Patient) */
public void updatePatient(Patient p) {
super.updateWithoutDuplicateHandling(p);
}
- /** @see PatientDao#deletePatient(Patient) */
public void deletePatient(Patient p) {
super.delete(p);
}
- /** @see PatientDao#getAllPatients() */
public List<Patient> getAllPatients() {
DetachedCriteria c= getBaseCriterion();
return super.getList(c);
}
+
+ public List<Patient> getPatientsForCHW(CommunityHealthWorker chw) {
+ DetachedCriteria criteria = getBaseCriterion();
+ criteria.add(Restrictions.eq("chw", chw));
+ return super.getList(criteria);
+ }
- public List<Patient> getPatientsByNameWithLimit(String s, int limit) {
+ public List<Patient> findPatientsByName(String nameFragment, int resultsLimit) {
DetachedCriteria c= getBaseCriterion();
- c.add(Restrictions.like("name", "%"+s+"%"));
- if(limit > 0)
- return super.getList(c, 0, limit);
+ c.add(Restrictions.like("name", nameFragment, MatchMode.ANYWHERE));
+ if(resultsLimit > 0)
+ return super.getList(c, 0, resultsLimit);
else{
return super.getList(c);
}
}
- public List<Patient> getPatientsByCHWAndName(String name, CommunityHealthWorker chw){
- DetachedCriteria c= getBaseCriterion();
- c.add(Restrictions.like("name", "%"+name+"%"));
- c.add(Restrictions.eq("chw", chw));
- return super.getList(c);
- }
-
- public List<Patient> getPatientsByName(String s){
- return getPatientsByNameWithLimit(s,-1);
+ public List<Patient> findPatientsByName(String nameFragment){
+ return findPatientsByName(nameFragment,-1);
}
public Patient getPatientById(Long id){
DetachedCriteria c = getBaseCriterion();
c.add(Restrictions.eq("pid", id));
- try{
- return super.getList(c).get(0);
- }catch(Throwable t){
- return null;
- }
+ return super.getUnique(c);
}
- public Patient getPatient(String name, String birthdate, String id){
+ public Patient findPatient(String name, String birthdate, String id){
DetachedCriteria c = getBaseCriterion();
//add the name restriction
if(name !=null && !name.equals("")){
@@ -106,12 +89,11 @@ public Patient getPatient(String name, String birthdate, String id){
long longId = Long.parseLong(id);
c.add(Restrictions.eq("id", longId));
}
- Patient p = super.getUnique(c);
- return p;
+ return super.getUnique(c);
}
private DetachedCriteria getBaseCriterion(){
- DetachedCriteria c= super.getCriterion();
+ DetachedCriteria c= DetachedCriteria.forClass(Patient.class);
c.add(Restrictions.eq("deleted",false));
return c;
}
@@ -166,7 +166,7 @@ public void importFile(String path) {
mfr.addFieldResponse(mffr);
}
try{
- Patient p = patientDao.getPatient(name, birthdate, id);
+ Patient p = patientDao.findPatient(name, birthdate, id);
if(p == null){
exceptions.add(new CsvValidationException(lineNumber,getI18NString("medic.importer.unknown.subject.error")));
}else{
@@ -103,7 +103,7 @@ public void setForm(MedicForm form){
}
}
- Patient p = patientDao.getPatient(name, birthdate, id);
+ Patient p = patientDao.findPatient(name, birthdate, id);
if (p == null) {
exceptions.add(new CsvValidationException(lineNumber,getI18NString("medic.importer.unknown.subject.error")));
}
@@ -56,7 +56,7 @@ private void init(){
public void searchKeyPressed(){
ArrayList<Patient> results = new ArrayList<Patient>();
- results.addAll(patientDao.getPatientsByNameWithLimit(uiController.getText(uiController.find(mainPanel,"searchBox")),15));
+ results.addAll(patientDao.findPatientsByName(uiController.getText(uiController.find(mainPanel,"searchBox")),15));
patientTable.setResults(results);
patientTable.setSelected(0);
}
@@ -163,7 +163,7 @@ public void search(String text){
}
if(!searchingCandidates){
//if we aren't searching for candidates, then we just want a few patients
- tableController.setResults(patientDao.getPatientsByNameWithLimit(text,5));
+ tableController.setResults(patientDao.findPatientsByName(text,5));
}else{
//if we are searching for candidates, filter by name
ArrayList<Candidate> results = new ArrayList<Candidate>();
@@ -34,7 +34,7 @@ public PatientAdministrationPanelController(UiGeneratorController uiController,
@Override
protected List<Patient> getPeopleForString(String s) {
- return new ArrayList<Patient>(patientDao.getPatientsByNameWithLimit(s, 30));
+ return new ArrayList<Patient>(patientDao.findPatientsByName(s, 30));
}
@Override

0 comments on commit 93d1081

Please sign in to comment.