- Timestamp:
- 05/24/08 14:37:02 (8 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
openmrs/trunk/src/api/org/openmrs/api/impl/CohortServiceImpl.java
r4158 r4358 15 15 16 16 import java.util.ArrayList; 17 import java.util.Date; 17 18 import java.util.Iterator; 18 19 import java.util.LinkedHashMap; … … 33 34 import org.openmrs.reporting.PatientCharacteristicFilter; 34 35 import org.openmrs.reporting.PatientSearch; 36 import org.openmrs.util.OpenmrsConstants; 37 import org.springframework.util.StringUtils; 35 38 36 39 /** 37 * 40 * API functions related to Cohorts 38 41 */ 39 public class CohortServiceImpl implements CohortService {40 41 pr otectedLog log = LogFactory.getLog(this.getClass());42 public class CohortServiceImpl extends BaseOpenmrsService implements CohortService { 43 44 private Log log = LogFactory.getLog(this.getClass()); 42 45 private CohortDAO dao; 43 46 44 47 private static Map<Class<? extends CohortDefinition>, CohortDefinitionProvider> cohortDefinitionProviders = null; 45 48 46 public CohortServiceImpl() { 47 } 48 49 private CohortDAO getCohortDAO() { 50 return dao; 51 } 52 49 /** 50 * @see org.openmrs.api.CohortService#setCohortDAO(org.openmrs.api.db.CohortDAO) 51 */ 53 52 public void setCohortDAO(CohortDAO dao) { 54 53 this.dao = dao; 55 54 } 56 55 57 public void createCohort(Cohort cohort) { 58 if (cohort.getCreator() == null) 56 /** 57 * @see org.openmrs.api.CohortService#saveCohort(org.openmrs.Cohort) 58 */ 59 public Cohort saveCohort(Cohort cohort) throws APIException { 60 if (cohort.getCohortId() == null) { 61 Context.requirePrivilege(OpenmrsConstants.PRIV_ADD_COHORTS); 62 } else { 63 Context.requirePrivilege(OpenmrsConstants.PRIV_EDIT_COHORTS); 64 } 65 if (cohort.getName() == null) { 66 throw new APIException("Cohort name is required"); 67 } 68 Date now = new Date(); 69 if (cohort.getDateCreated() == null) { 70 cohort.setDateCreated(now); 71 } 72 if (cohort.getCreator() == null) { 59 73 cohort.setCreator(Context.getAuthenticatedUser()); 60 if (cohort.getDateCreated() == null) 61 cohort.setDateCreated(new java.util.Date()); 62 if (cohort.getName() == null) 63 throw new IllegalArgumentException("Missing Name"); 64 getCohortDAO().createCohort(cohort); 65 } 66 74 } 75 if (cohort.getCohortId() != null) { 76 cohort.setChangedBy(Context.getAuthenticatedUser()); 77 cohort.setDateChanged(now); 78 } 79 if (log.isInfoEnabled()) 80 log.info("Saving cohort " + cohort); 81 82 return dao.saveCohort(cohort); 83 } 84 85 /** 86 * @see org.openmrs.api.CohortService#createCohort(org.openmrs.Cohort) 87 */ 88 public Cohort createCohort(Cohort cohort) { 89 return saveCohort(cohort); 90 } 91 92 /** 93 * @see org.openmrs.api.CohortService#getCohort(java.lang.Integer) 94 */ 67 95 public Cohort getCohort(Integer id) { 68 return getCohortDAO().getCohort(id); 69 } 70 96 return dao.getCohort(id); 97 } 98 99 /** 100 * @see org.openmrs.api.CohortService#getCohorts() 101 */ 71 102 public List<Cohort> getCohorts() { 72 return getCohortDAO().getCohorts(); 73 } 74 75 public void voidCohort(Cohort cohort, String reason) { 103 return getAllCohorts(); 104 } 105 106 /** 107 * @see org.openmrs.api.CohortService#voidCohort(org.openmrs.Cohort, java.lang.String) 108 */ 109 public Cohort voidCohort(Cohort cohort, String reason) { 110 if (cohort.isVoided()) { 111 return cohort; 112 } else { 113 if (!StringUtils.hasText(reason)) 114 throw new APIException("Reason is required"); 76 115 cohort.setVoided(true); 77 116 cohort.setVoidedBy(Context.getAuthenticatedUser()); 78 117 cohort.setVoidReason(reason); 79 updateCohort(cohort); 118 return saveCohort(cohort); 119 } 80 120 } 81 121 … … 83 123 * @see org.openmrs.api.CohortService#addPatientToCohort(org.openmrs.Cohort, org.openmrs.Patient) 84 124 */ 85 public void addPatientToCohort(Cohort cohort, Patient patient) { 125 public Cohort addPatientToCohort(Cohort cohort, Patient patient) { 126 if (!cohort.contains(patient)) { 86 127 cohort.getMemberIds().add(patient.getPatientId()); 87 updateCohort(cohort); 128 saveCohort(cohort); 129 } 130 return cohort; 88 131 } 89 132 … … 91 134 * @see org.openmrs.api.CohortService#removePatientFromCohort(org.openmrs.Cohort, org.openmrs.Patient) 92 135 */ 93 public void removePatientFromCohort(Cohort cohort, Patient patient) { 136 public Cohort removePatientFromCohort(Cohort cohort, Patient patient) { 137 if (cohort.contains(patient)) { 94 138 cohort.getMemberIds().remove(patient.getPatientId()); 95 updateCohort(cohort); 139 saveCohort(cohort); 140 } 141 return cohort; 96 142 } 97 143 … … 99 145 * @see org.openmrs.api.CohortService#updateCohort(org.openmrs.Cohort) 100 146 */ 101 public void updateCohort(Cohort cohort) { 102 if (cohort.getCreator() == null) 103 cohort.setCreator(Context.getAuthenticatedUser()); 104 if (cohort.getDateCreated() == null) 105 cohort.setDateCreated(new java.util.Date()); 106 if (cohort.getName() == null) 107 throw new IllegalArgumentException("Missing Name"); 108 // TODO: Add modifiedBy and dateModified to Cohort 109 //cohort.setDateModified(new java.util.Date()); 110 //cohort.setModifiedBy(Context.getAuthenticatedUser()); 111 getCohortDAO().updateCohort(cohort); 147 public Cohort updateCohort(Cohort cohort) { 148 return saveCohort(cohort); 112 149 } 113 150 … … 116 153 */ 117 154 public List<Cohort> getCohortsContainingPatient(Patient patient) { 118 return getCohortDAO().getCohortsContainingPatientId(patient.getPatientId());155 return dao.getCohortsContainingPatientId(patient.getPatientId()); 119 156 } 120 157 121 158 public List<Cohort> getCohortsContainingPatientId(Integer patientId) { 122 return getCohortDAO().getCohortsContainingPatientId(patientId); 123 } 124 125 /** 159 return dao.getCohortsContainingPatientId(patientId); 160 } 161 162 /** 163 * @see org.openmrs.api.CohortService#getCohorts(java.lang.String) 164 */ 165 public List<Cohort> getCohorts(String nameFragment) throws APIException { 166 return dao.getCohorts(nameFragment); 167 } 168 169 /** 170 * @see org.openmrs.api.CohortService#getAllCohorts() 171 */ 172 public List<Cohort> getAllCohorts() throws APIException { 173 return getAllCohorts(false); 174 } 175 176 /** 177 * @see org.openmrs.api.CohortService#getAllCohorts(boolean) 178 */ 179 public List<Cohort> getAllCohorts(boolean includeVoided) throws APIException { 180 return dao.getAllCohorts(includeVoided); 181 } 182 183 /** 184 * @see org.openmrs.api.CohortService#getCohort(java.lang.String) 185 */ 186 public Cohort getCohort(String name) throws APIException { 187 return dao.getCohort(name); 188 } 189 190 /** 191 * @see org.openmrs.api.CohortService#purgeCohort(org.openmrs.Cohort) 192 */ 193 public Cohort purgeCohort(Cohort cohort) throws APIException { 194 return dao.deleteCohort(cohort); 195 } 196 197 /** 126 198 * Auto generated method comment 127 199 *