| 23 | | import org.openmrs.reporting.PatientSet; |
|---|
| 24 | | |
|---|
| | 26 | import org.openmrs.api.ReportService; |
|---|
| | 27 | import org.openmrs.cohort.CohortDefinition; |
|---|
| | 28 | import org.openmrs.report.EvaluationContext; |
|---|
| | 29 | import org.simpleframework.xml.Attribute; |
|---|
| | 30 | import org.simpleframework.xml.Element; |
|---|
| | 31 | import org.simpleframework.xml.ElementList; |
|---|
| | 32 | import org.simpleframework.xml.Root; |
|---|
| | 33 | |
|---|
| | 34 | /** |
|---|
| | 35 | * This class represents a list of patientIds. |
|---|
| | 36 | * If it is generated from a CohortDefinition via {@link ReportService#evaluate(org.openmrs.report.ReportSchema, Cohort, EvaluationContext)} |
|---|
| | 37 | * then it will contain a link back to the CohortDefinition it came from and the |
|---|
| | 38 | * EvalutionContext that definition was evaluated in. |
|---|
| | 39 | * @see org.openmrs.cohort.CohortDefinition |
|---|
| | 40 | */ |
|---|
| | 41 | @Root(strict=false) |
|---|
| 45 | | public PatientSet toPatientSet() { |
|---|
| 46 | | PatientSet ret = new PatientSet(); |
|---|
| 47 | | ret.copyPatientIds(getMemberIds()); |
|---|
| 48 | | return ret; |
|---|
| 49 | | } |
|---|
| 50 | | |
|---|
| | 64 | /** |
|---|
| | 65 | * Convenience constructor to create a Cohort object that |
|---|
| | 66 | * has an primarykey/internal identifier of <code>cohortId</code> |
|---|
| | 67 | * |
|---|
| | 68 | * @param cohortId the internal identifier for this cohort |
|---|
| | 69 | */ |
|---|
| | 70 | public Cohort(Integer cohortId) { |
|---|
| | 71 | memberIds = new HashSet<Integer>(); |
|---|
| | 72 | this.cohortId = cohortId; |
|---|
| | 73 | } |
|---|
| | 74 | |
|---|
| | 75 | public Cohort(Set<Integer> memberIds) { |
|---|
| | 76 | this.memberIds = memberIds; |
|---|
| | 77 | } |
|---|
| | 78 | |
|---|
| | 79 | public Cohort(Collection<Integer> memberIds) { |
|---|
| | 80 | this.memberIds = new HashSet<Integer>(); |
|---|
| | 81 | this.memberIds.addAll(memberIds); |
|---|
| | 82 | } |
|---|
| | 83 | |
|---|
| | 84 | public Cohort(String commaSeparatedIds) { |
|---|
| | 85 | memberIds = new HashSet<Integer>(); |
|---|
| | 86 | for (StringTokenizer st = new StringTokenizer(commaSeparatedIds, ","); st.hasMoreTokens(); ) { |
|---|
| | 87 | String id = st.nextToken(); |
|---|
| | 88 | memberIds.add(new Integer(id.trim())); |
|---|
| | 89 | } |
|---|
| | 90 | } |
|---|
| | 91 | |
|---|
| | 92 | public String getCommaSeparatedPatientIds() { |
|---|
| | 93 | StringBuilder sb = new StringBuilder(); |
|---|
| | 94 | for (Iterator<Integer> i = getMemberIds().iterator(); i.hasNext(); ) { |
|---|
| | 95 | sb.append(i.next()); |
|---|
| | 96 | if (i.hasNext()) { |
|---|
| | 97 | sb.append(","); |
|---|
| | 98 | } |
|---|
| | 99 | } |
|---|
| | 100 | return sb.toString(); |
|---|
| | 101 | } |
|---|
| | 102 | |
|---|
| 52 | | StringBuilder sb = new StringBuilder(); |
|---|
| 53 | | sb.append("Cohort{").append("\n"); |
|---|
| 54 | | sb.append(" name=" + getName()).append("\n"); |
|---|
| 55 | | sb.append(" description=" + getDescription()).append("\n"); |
|---|
| 56 | | sb.append(" creator=" + getCreator()).append("\n"); |
|---|
| 57 | | sb.append(" dateCreated=" + getDateCreated()).append("\n"); |
|---|
| 58 | | sb.append(" voided=" + getVoided()).append("\n"); |
|---|
| 59 | | sb.append(" voidedBy=" + getVoidedBy()).append("\n"); |
|---|
| 60 | | sb.append(" dateVoided=" + getDateVoided()).append("\n"); |
|---|
| 61 | | sb.append(" voidReason=" + getVoidReason()).append("\n"); |
|---|
| 62 | | sb.append(" memberIds=" + getMemberIds()).append("\n"); |
|---|
| 63 | | if (getMemberIds() != null) |
|---|
| 64 | | sb.append(" memberIds.class=" + getMemberIds().getClass()).append("\n"); |
|---|
| 65 | | sb.append(" }"); |
|---|
| 66 | | return sb.toString(); |
|---|
| | 104 | return getMemberIds() == null ? "Cohort with null members" : (getMemberIds().size() + " patients"); |
|---|
| | 124 | public void addMember(Integer memberId) { |
|---|
| | 125 | getMemberIds().add(memberId); |
|---|
| | 126 | } |
|---|
| | 127 | |
|---|
| | 128 | public void removeMember(Integer memberId) { |
|---|
| | 129 | getMemberIds().remove(memberId); |
|---|
| | 130 | } |
|---|
| | 131 | |
|---|
| | 132 | public int size() { |
|---|
| | 133 | return getMemberIds() == null ? 0 : getMemberIds().size(); |
|---|
| | 134 | } |
|---|
| | 135 | |
|---|
| | 136 | public int getSize() { |
|---|
| | 137 | return size(); |
|---|
| | 138 | } |
|---|
| | 139 | |
|---|
| | 140 | // static utility methods |
|---|
| | 141 | |
|---|
| | 142 | public static Cohort union(Cohort a, Cohort b) { |
|---|
| | 143 | Cohort ret = new Cohort(); |
|---|
| | 144 | ret.setName("(" + a.getName() + " + " + b.getName() + ")"); |
|---|
| | 145 | if (a != null) |
|---|
| | 146 | ret.getMemberIds().addAll(a.getMemberIds()); |
|---|
| | 147 | if (b != null) |
|---|
| | 148 | ret.getMemberIds().addAll(b.getMemberIds()); |
|---|
| | 149 | return ret; |
|---|
| | 150 | } |
|---|
| | 151 | |
|---|
| | 152 | public static Cohort intersect(Cohort a, Cohort b) { |
|---|
| | 153 | Cohort ret = new Cohort(); |
|---|
| | 154 | ret.setName("(" + a.getName() + " * " + b.getName() + ")"); |
|---|
| | 155 | if (a != null && b != null) { |
|---|
| | 156 | ret.getMemberIds().addAll(a.getMemberIds()); |
|---|
| | 157 | ret.getMemberIds().retainAll(b.getMemberIds()); |
|---|
| | 158 | } |
|---|
| | 159 | return ret; |
|---|
| | 160 | } |
|---|
| | 161 | |
|---|
| | 162 | public static Cohort subtract(Cohort a, Cohort b) { |
|---|
| | 163 | Cohort ret = new Cohort(); |
|---|
| | 164 | ret.setName("(" + a.getName() + " - " + b.getName() + ")"); |
|---|
| | 165 | if (a != null) { |
|---|
| | 166 | ret.getMemberIds().addAll(a.getMemberIds()); |
|---|
| | 167 | if (b != null) |
|---|
| | 168 | ret.getMemberIds().removeAll(b.getMemberIds()); |
|---|
| | 169 | } |
|---|
| | 170 | return ret; |
|---|
| | 171 | } |
|---|
| | 172 | |
|---|
| 167 | | |
|---|
| | 287 | |
|---|
| | 288 | /** |
|---|
| | 289 | * @return the cohortDefinition |
|---|
| | 290 | */ |
|---|
| | 291 | @Element(required=false) |
|---|
| | 292 | public CohortDefinition getCohortDefinition() { |
|---|
| | 293 | return cohortDefinition; |
|---|
| | 294 | } |
|---|
| | 295 | |
|---|
| | 296 | /** |
|---|
| | 297 | * @param cohortDefinition the cohortDefinition to set |
|---|
| | 298 | */ |
|---|
| | 299 | @Element(required=false) |
|---|
| | 300 | public void setCohortDefinition(CohortDefinition cohortDefinition) { |
|---|
| | 301 | this.cohortDefinition = cohortDefinition; |
|---|
| | 302 | } |
|---|
| | 303 | |
|---|
| | 304 | /** |
|---|
| | 305 | * @return the evaluationContext |
|---|
| | 306 | */ |
|---|
| | 307 | @Element(required=false) |
|---|
| | 308 | public EvaluationContext getEvaluationContext() { |
|---|
| | 309 | return evaluationContext; |
|---|
| | 310 | } |
|---|
| | 311 | |
|---|
| | 312 | /** |
|---|
| | 313 | * @param evaluationContext the evaluationContext to set |
|---|
| | 314 | */ |
|---|
| | 315 | @Element(required=false) |
|---|
| | 316 | public void setEvaluationContext(EvaluationContext evaluationContext) { |
|---|
| | 317 | this.evaluationContext = evaluationContext; |
|---|
| | 318 | } |
|---|
| | 319 | |
|---|