Downloads Documentation Community Contribute Demo






Show Sidebar
Login | Register
Show
Ignore:
Timestamp:
05/24/08 14:37:02 (8 months ago)
Author:
bwolfe
Message:

Merging api-refactoring to trunk [3595]:[4355]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • openmrs/trunk/src/api/org/openmrs/api/impl/CohortServiceImpl.java

    r4158 r4358  
    1515 
    1616import java.util.ArrayList; 
     17import java.util.Date; 
    1718import java.util.Iterator; 
    1819import java.util.LinkedHashMap; 
     
    3334import org.openmrs.reporting.PatientCharacteristicFilter; 
    3435import org.openmrs.reporting.PatientSearch; 
     36import org.openmrs.util.OpenmrsConstants; 
     37import org.springframework.util.StringUtils; 
    3538 
    3639/** 
    37  * 
     40 * API functions related to Cohorts 
    3841 */ 
    39 public class CohortServiceImpl implements CohortService { 
    40  
    41         protected Log log = LogFactory.getLog(this.getClass()); 
     42public class CohortServiceImpl extends BaseOpenmrsService implements CohortService { 
     43 
     44        private Log log = LogFactory.getLog(this.getClass()); 
    4245        private CohortDAO dao; 
    4346         
    4447        private static Map<Class<? extends CohortDefinition>, CohortDefinitionProvider> cohortDefinitionProviders = null; 
    4548         
    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         */ 
    5352        public void setCohortDAO(CohortDAO dao) { 
    5453                this.dao = dao; 
    5554        } 
    5655         
    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) { 
    5973                        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         */ 
    6795        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         */ 
    71102        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"); 
    76115                cohort.setVoided(true); 
    77116                cohort.setVoidedBy(Context.getAuthenticatedUser()); 
    78117                cohort.setVoidReason(reason); 
    79                 updateCohort(cohort); 
     118                        return saveCohort(cohort); 
     119                } 
    80120        } 
    81121 
     
    83123     * @see org.openmrs.api.CohortService#addPatientToCohort(org.openmrs.Cohort, org.openmrs.Patient) 
    84124     */ 
    85     public void addPatientToCohort(Cohort cohort, Patient patient) { 
     125    public Cohort addPatientToCohort(Cohort cohort, Patient patient) { 
     126        if (!cohort.contains(patient)) { 
    86127            cohort.getMemberIds().add(patient.getPatientId()); 
    87             updateCohort(cohort); 
     128                saveCohort(cohort); 
     129        } 
     130        return cohort; 
    88131    } 
    89132 
     
    91134     * @see org.openmrs.api.CohortService#removePatientFromCohort(org.openmrs.Cohort, org.openmrs.Patient) 
    92135     */ 
    93     public void removePatientFromCohort(Cohort cohort, Patient patient) { 
     136    public Cohort removePatientFromCohort(Cohort cohort, Patient patient) { 
     137        if (cohort.contains(patient)) { 
    94138            cohort.getMemberIds().remove(patient.getPatientId()); 
    95             updateCohort(cohort); 
     139                saveCohort(cohort); 
     140        } 
     141        return cohort; 
    96142    } 
    97143 
     
    99145     * @see org.openmrs.api.CohortService#updateCohort(org.openmrs.Cohort) 
    100146     */ 
    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); 
    112149        } 
    113150 
     
    116153     */ 
    117154    public List<Cohort> getCohortsContainingPatient(Patient patient) { 
    118             return getCohortDAO().getCohortsContainingPatientId(patient.getPatientId()); 
     155            return dao.getCohortsContainingPatientId(patient.getPatientId()); 
    119156    } 
    120157     
    121158    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        /** 
    126198     * Auto generated method comment 
    127199     *