| 63 | | public void createOrUpdateProgram(Program program) throws DAOException { |
|---|
| 64 | | log.debug("Creating or updating program " + program); |
|---|
| 65 | | if (program.getWorkflows() != null) |
|---|
| 66 | | log.debug("\twith " + program.getWorkflows().size() + " workflows: " + program.getWorkflows()); |
|---|
| 67 | | |
|---|
| 68 | | if (program.getProgramId() == null) |
|---|
| 69 | | sessionFactory.getCurrentSession().save(program); |
|---|
| 70 | | else |
|---|
| 71 | | sessionFactory.getCurrentSession().merge(program); |
|---|
| 72 | | |
|---|
| 73 | | } |
|---|
| 74 | | |
|---|
| 75 | | public Program getProgram(Integer id) throws DAOException { |
|---|
| 76 | | return (Program) sessionFactory.getCurrentSession().get(Program.class, id); |
|---|
| 77 | | } |
|---|
| 78 | | |
|---|
| | 63 | // ************************** |
|---|
| | 64 | // PROGRAM |
|---|
| | 65 | // ************************** |
|---|
| | 66 | |
|---|
| | 67 | /** |
|---|
| | 68 | * @see org.openmrs.api.db.ProgramWorkflowDAO#saveProgram(org.openmrs.Program) |
|---|
| | 69 | */ |
|---|
| | 70 | public Program saveProgram(Program program) throws DAOException { |
|---|
| | 71 | sessionFactory.getCurrentSession().saveOrUpdate(program); |
|---|
| | 72 | return program; |
|---|
| | 73 | } |
|---|
| | 74 | |
|---|
| | 75 | /** |
|---|
| | 76 | * @see org.openmrs.api.db.ProgramWorkflowDAO#getProgram(java.lang.Integer) |
|---|
| | 77 | */ |
|---|
| | 78 | public Program getProgram(Integer programId) throws DAOException { |
|---|
| | 79 | return (Program) sessionFactory.getCurrentSession().get(Program.class, programId); |
|---|
| | 80 | } |
|---|
| | 81 | |
|---|
| | 82 | /** |
|---|
| | 83 | * @see org.openmrs.api.db.ProgramWorkflowDAO#getAllPrograms(boolean) |
|---|
| | 84 | */ |
|---|
| | 85 | @SuppressWarnings("unchecked") |
|---|
| | 86 | public List<Program> getAllPrograms(boolean includeRetired) throws DAOException { |
|---|
| | 87 | Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Program.class); |
|---|
| | 88 | if (includeRetired == false) { |
|---|
| | 89 | criteria.add(Expression.eq("retired", false)); |
|---|
| | 90 | } |
|---|
| | 91 | return criteria.list(); |
|---|
| | 92 | } |
|---|
| | 93 | |
|---|
| | 94 | /** |
|---|
| | 95 | * @see org.openmrs.api.db.ProgramWorkflowDAO#findPrograms(java.lang.String) |
|---|
| | 96 | */ |
|---|
| 80 | | public List<Program> getPrograms() throws DAOException { |
|---|
| 81 | | List<Program> programs = new ArrayList<Program>(); |
|---|
| 82 | | programs.addAll(sessionFactory.getCurrentSession().createQuery("from Program").list()); |
|---|
| 83 | | return programs; |
|---|
| 84 | | } |
|---|
| 85 | | |
|---|
| 86 | | public ProgramWorkflow findWorkflowByProgramAndConcept(Integer programId, Integer conceptId) throws DAOException { |
|---|
| 87 | | ProgramWorkflow w = null; |
|---|
| 88 | | w = (ProgramWorkflow) sessionFactory.getCurrentSession().createQuery("from ProgramWorkflow w where w.program.programid = :programId and w.concept.conceptId = :conceptId") |
|---|
| 89 | | .setInteger("programId", programId) |
|---|
| 90 | | .setInteger("conceptId", conceptId) |
|---|
| 91 | | .uniqueResult(); |
|---|
| 92 | | |
|---|
| 93 | | return w; |
|---|
| 94 | | } |
|---|
| 95 | | |
|---|
| 96 | | public void createPatientProgram(PatientProgram p) throws DAOException { |
|---|
| 97 | | if (p.getPatient() == null || p.getPatient().getPatientId() == null |
|---|
| 98 | | || p.getProgram() == null || p.getProgram().getProgramId() == null) { |
|---|
| 99 | | throw new DAOException("can't create a PatientProgram without already-persisted patient and program"); |
|---|
| 100 | | } |
|---|
| 101 | | sessionFactory.getCurrentSession().save(p); |
|---|
| 102 | | |
|---|
| 103 | | } |
|---|
| 104 | | |
|---|
| 105 | | public void updatePatientProgram(PatientProgram p) throws DAOException { |
|---|
| 106 | | sessionFactory.getCurrentSession().update(p); |
|---|
| 107 | | } |
|---|
| 108 | | |
|---|
| 109 | | public PatientProgram getPatientProgram(Integer id) throws DAOException { |
|---|
| 110 | | return (PatientProgram) sessionFactory.getCurrentSession().get(PatientProgram.class, id); |
|---|
| 111 | | } |
|---|
| 112 | | |
|---|
| 113 | | |
|---|
| 114 | | public PatientState getPatientState(Integer id) throws DAOException { |
|---|
| 115 | | return (PatientState)sessionFactory.getCurrentSession().get(PatientState.class, id); |
|---|
| 116 | | } |
|---|
| 117 | | |
|---|
| | 98 | public List<Program> findPrograms(String nameFragment) throws DAOException { |
|---|
| | 99 | Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Program.class, "program"); |
|---|
| | 100 | criteria.add(Expression.ilike("name", nameFragment, MatchMode.ANYWHERE)); |
|---|
| | 101 | criteria.addOrder(Order.asc("name")); |
|---|
| | 102 | return criteria.list(); |
|---|
| | 103 | } |
|---|
| | 104 | |
|---|
| | 105 | /** |
|---|
| | 106 | * @see org.openmrs.api.db.ProgramWorkflowDAO#deleteProgram(org.openmrs.Program) |
|---|
| | 107 | */ |
|---|
| | 108 | public void deleteProgram(Program program) throws DAOException { |
|---|
| | 109 | sessionFactory.getCurrentSession().delete(program); |
|---|
| | 110 | } |
|---|
| | 111 | |
|---|
| | 112 | // ************************** |
|---|
| | 113 | // PATIENT PROGRAM |
|---|
| | 114 | // ************************** |
|---|
| | 115 | |
|---|
| | 116 | /** |
|---|
| | 117 | * @see org.openmrs.api.db.ProgramWorkflowDAO#savePatientProgram(org.openmrs.PatientProgram) |
|---|
| | 118 | */ |
|---|
| | 119 | public PatientProgram savePatientProgram(PatientProgram patientProgram) throws DAOException { |
|---|
| | 120 | if (patientProgram.getPatientProgramId() == null) { |
|---|
| | 121 | sessionFactory.getCurrentSession().save(patientProgram); |
|---|
| | 122 | } |
|---|
| | 123 | else { |
|---|
| | 124 | sessionFactory.getCurrentSession().merge(patientProgram); |
|---|
| | 125 | } |
|---|
| | 126 | return patientProgram; |
|---|
| | 127 | } |
|---|
| | 128 | |
|---|
| | 129 | /** |
|---|
| | 130 | * @see org.openmrs.api.db.ProgramWorkflowDAO#getPatientProgram(java.lang.Integer) |
|---|
| | 131 | */ |
|---|
| | 132 | public PatientProgram getPatientProgram(Integer patientProgramId) throws DAOException { |
|---|
| | 133 | return (PatientProgram) sessionFactory.getCurrentSession().get(PatientProgram.class, patientProgramId); |
|---|
| | 134 | } |
|---|
| | 135 | |
|---|
| | 136 | /** |
|---|
| | 137 | * @see org.openmrs.api.db.ProgramWorkflowDAO#getPatientPrograms(org.openmrs.Patient, org.openmrs.Program, java.util.Date, java.util.Date, java.util.Date, java.util.Date) |
|---|
| | 138 | */ |
|---|
| 119 | | public Collection<PatientProgram> getPatientPrograms(Patient patient) { |
|---|
| 120 | | List<PatientProgram> patientPrograms = new ArrayList<PatientProgram>(); |
|---|
| 121 | | |
|---|
| 122 | | Criteria criteria = sessionFactory.getCurrentSession().createCriteria(PatientProgram.class); |
|---|
| 123 | | criteria.add(Restrictions.eq("patient", patient)); |
|---|
| 124 | | criteria.addOrder(org.hibernate.criterion.Order.desc("dateEnrolled")); |
|---|
| 125 | | criteria.addOrder(org.hibernate.criterion.Order.desc("dateCompleted")); |
|---|
| 126 | | patientPrograms.addAll(criteria.list()); |
|---|
| 127 | | |
|---|
| 128 | | return patientPrograms; |
|---|
| 129 | | } |
|---|
| 130 | | |
|---|
| | 140 | public List<PatientProgram> getPatientPrograms(Patient patient, Program program, Date minEnrollmentDate, Date maxEnrollmentDate, Date minCompletionDate, Date maxCompletionDate, boolean includeVoided) throws DAOException { |
|---|
| | 141 | Criteria crit = sessionFactory.getCurrentSession().createCriteria(PatientProgram.class); |
|---|
| | 142 | if (patient != null) { |
|---|
| | 143 | crit.add(Expression.eq("patient", patient)); |
|---|
| | 144 | } |
|---|
| | 145 | if (program != null) { |
|---|
| | 146 | crit.add(Expression.eq("program", program)); |
|---|
| | 147 | } |
|---|
| | 148 | if (minEnrollmentDate != null) { |
|---|
| | 149 | crit.add(Expression.ge("dateEnrolled", minEnrollmentDate)); |
|---|
| | 150 | } |
|---|
| | 151 | if (maxEnrollmentDate != null) { |
|---|
| | 152 | crit.add(Expression.le("dateEnrolled", maxEnrollmentDate)); |
|---|
| | 153 | } |
|---|
| | 154 | if (minCompletionDate != null) { |
|---|
| | 155 | crit.add(Expression.or(Expression.isNull("dateCompleted"), Expression.ge("dateCompleted", minCompletionDate))); |
|---|
| | 156 | } |
|---|
| | 157 | if (maxCompletionDate != null) { |
|---|
| | 158 | crit.add(Expression.le("dateCompleted", maxCompletionDate)); |
|---|
| | 159 | } |
|---|
| | 160 | if (!includeVoided) { |
|---|
| | 161 | crit.add(Expression.eq("voided", false)); |
|---|
| | 162 | } |
|---|
| | 163 | return crit.list(); |
|---|
| | 164 | } |
|---|
| | 165 | |
|---|
| | 166 | /** |
|---|
| | 167 | * TODO: refactor this |
|---|
| | 168 | * @see org.openmrs.api.db.ProgramWorkflowDAO#getPatientPrograms(org.openmrs.Cohort, java.util.Collection) |
|---|
| | 169 | */ |
|---|
| 149 | | } |
|---|
| 150 | | |
|---|
| 151 | | public ProgramWorkflow getWorkflow(Integer id) { |
|---|
| 152 | | return (ProgramWorkflow) sessionFactory.getCurrentSession().get(ProgramWorkflow.class, id); |
|---|
| 153 | | } |
|---|
| 154 | | |
|---|
| 155 | | public void createWorkflow(ProgramWorkflow w) { |
|---|
| 156 | | sessionFactory.getCurrentSession().save(w); |
|---|
| 157 | | } |
|---|
| 158 | | |
|---|
| 159 | | public void updateWorkflow(ProgramWorkflow w) { |
|---|
| 160 | | sessionFactory.getCurrentSession().update(w); |
|---|
| 161 | | } |
|---|
| 162 | | |
|---|
| 163 | | public List<ProgramWorkflowState> getStates(boolean includeVoided) { |
|---|
| 164 | | Criteria crit = sessionFactory.getCurrentSession().createCriteria(ProgramWorkflowState.class); |
|---|
| 165 | | if (!includeVoided) |
|---|
| 166 | | crit.add(Expression.eq("voided", false)); |
|---|
| 167 | | crit.addOrder(Order.asc("programWorkflow.programWorkflowId")); |
|---|
| 168 | | crit.addOrder(Order.asc("programWorkflowStateId")); |
|---|
| 169 | | return crit.list(); |
|---|
| 170 | | } |
|---|
| 171 | | |
|---|
| 172 | | public ProgramWorkflowState getState(Integer id) { |
|---|
| 173 | | return (ProgramWorkflowState) sessionFactory.getCurrentSession().get(ProgramWorkflowState.class, id); |
|---|
| 174 | | } |
|---|
| 175 | | |
|---|
| 176 | | @SuppressWarnings("unchecked") |
|---|
| 177 | | public Collection<Integer> patientsInProgram(Program program, Date fromDate, Date toDate) { |
|---|
| 178 | | String sql = "select patient_id " + |
|---|
| 179 | | "from patient_program " + |
|---|
| 180 | | "where voided = false " + |
|---|
| 181 | | " and program_id = :programId "; |
|---|
| 182 | | if (toDate != null) { |
|---|
| 183 | | sql += "and (date_enrolled is null or date_enrolled <= :toDate) "; |
|---|
| 184 | | } |
|---|
| 185 | | if (fromDate != null) { |
|---|
| 186 | | sql += "and (date_completed is null or date_completed >= :fromDate) "; |
|---|
| 187 | | } |
|---|
| 188 | | Query q = sessionFactory.getCurrentSession().createSQLQuery(sql); |
|---|
| 189 | | q.setInteger("programId", program.getProgramId()); |
|---|
| 190 | | if (toDate != null) |
|---|
| 191 | | q.setDate("toDate", toDate); |
|---|
| 192 | | if (fromDate != null) |
|---|
| 193 | | q.setDate("fromDate", fromDate); |
|---|
| 194 | | Set<Integer> ret = new HashSet<Integer>(); |
|---|
| 195 | | for (Integer ptId : (List<Integer>) q.list()) { |
|---|
| 196 | | ret.add(ptId); |
|---|
| 197 | | } |
|---|
| 198 | | return ret; |
|---|
| 199 | | } |
|---|
| 200 | | |
|---|
| 201 | | public void createConceptStateConversion(ConceptStateConversion csc) { |
|---|
| 202 | | sessionFactory.getCurrentSession().save(csc); |
|---|
| 203 | | } |
|---|
| 204 | | |
|---|
| 205 | | public void updateConceptStateConversion(ConceptStateConversion csc) { |
|---|
| 206 | | sessionFactory.getCurrentSession().update(csc); |
|---|
| 207 | | } |
|---|
| 208 | | |
|---|
| | 188 | } |
|---|
| | 189 | |
|---|
| | 190 | /** |
|---|
| | 191 | * @see org.openmrs.api.db.ProgramWorkflowDAO#deletePatientProgram(org.openmrs.PatientProgram) |
|---|
| | 192 | */ |
|---|
| | 193 | public void deletePatientProgram(PatientProgram patientProgram) throws DAOException { |
|---|
| | 194 | sessionFactory.getCurrentSession().delete(patientProgram); |
|---|
| | 195 | } |
|---|
| | 196 | |
|---|
| | 197 | /** |
|---|
| | 198 | * @see org.openmrs.api.db.ProgramWorkflowDAO#saveConceptStateConversion(org.openmrs.ConceptStateConversion) |
|---|
| | 199 | */ |
|---|
| | 200 | public ConceptStateConversion saveConceptStateConversion(ConceptStateConversion csc) throws DAOException { |
|---|
| | 201 | if (csc.getConceptStateConversionId() == null) { |
|---|
| | 202 | sessionFactory.getCurrentSession().save(csc); |
|---|
| | 203 | } |
|---|
| | 204 | else { |
|---|
| | 205 | sessionFactory.getCurrentSession().merge(csc); |
|---|
| | 206 | } |
|---|
| | 207 | return csc; |
|---|
| | 208 | } |
|---|
| | 209 | |
|---|
| | 210 | /** |
|---|
| | 211 | * @see org.openmrs.api.db.ProgramWorkflowDAO#getAllConceptStateConversions(boolean) |
|---|
| | 212 | */ |
|---|
| | 213 | @SuppressWarnings("unchecked") |
|---|
| | 214 | public List<ConceptStateConversion> getAllConceptStateConversions() throws DAOException { |
|---|
| | 215 | return sessionFactory.getCurrentSession().createCriteria(ConceptStateConversion.class).list(); |
|---|
| | 216 | } |
|---|
| | 217 | |
|---|
| | 218 | /** |
|---|
| | 219 | * @see org.openmrs.api.db.ProgramWorkflowDAO#getConceptStateConversion(java.lang.Integer) |
|---|
| | 220 | */ |
|---|
| | 221 | public ConceptStateConversion getConceptStateConversion(Integer conceptStateConversionId) { |
|---|
| | 222 | return (ConceptStateConversion) sessionFactory.getCurrentSession().get(ConceptStateConversion.class, conceptStateConversionId); |
|---|
| | 223 | } |
|---|
| | 224 | |
|---|
| | 225 | /** |
|---|
| | 226 | * @see org.openmrs.api.db.ProgramWorkflowDAO#deleteConceptStateConversion(org.openmrs.ConceptStateConversion) |
|---|
| | 227 | */ |
|---|
| 213 | | public ConceptStateConversion getConceptStateConversion(Integer id) { |
|---|
| 214 | | log.debug("In getCsc with id of " + id.toString()); |
|---|
| 215 | | ConceptStateConversion csc = (ConceptStateConversion) sessionFactory.getCurrentSession().get(ConceptStateConversion.class, id); |
|---|
| 216 | | if ( csc != null ) log.debug("Csc is " + csc); |
|---|
| 217 | | else log.debug("csc is null back from hibernate"); |
|---|
| 218 | | return csc; |
|---|
| 219 | | } |
|---|
| 220 | | |
|---|
| 221 | | public List<ConceptStateConversion> getAllConversions() throws DAOException { |
|---|
| 222 | | log.debug("In getAllconversions"); |
|---|
| 223 | | |
|---|
| 224 | | List<ConceptStateConversion> conversions = new ArrayList<ConceptStateConversion>(); |
|---|
| 225 | | conversions.addAll(sessionFactory.getCurrentSession().createCriteria(ConceptStateConversion.class).list()); |
|---|
| 226 | | |
|---|
| 227 | | if ( conversions == null ) log.debug("Conversions are null"); |
|---|
| 228 | | else log.debug("conversions is size " + conversions.size()); |
|---|
| 229 | | |
|---|
| 230 | | return conversions; |
|---|
| 231 | | } |
|---|
| 232 | | |
|---|
| | 232 | /** |
|---|
| | 233 | * @see org.openmrs.api.db.ProgramWorkflowDAO#getConceptStateConversion(org.openmrs.ProgramWorkflow, org.openmrs.Concept) |
|---|
| | 234 | */ |
|---|