Downloads Documentation Community Contribute Demo






Show Sidebar
Login | Register
Show
Ignore:
Timestamp:
05/09/08 10:53:35 (2 months ago)
Author:
bwolfe
Message:

Merging report-api-refactoring to trunk [2696]:[4157]

Files:

Legend:

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

    r4095 r4158  
    1515 
    1616import java.io.Serializable; 
     17import java.util.Collection; 
    1718import java.util.Date; 
    1819import java.util.HashSet; 
     20import java.util.Iterator; 
    1921import java.util.Set; 
     22import java.util.StringTokenizer; 
    2023 
    2124import org.apache.commons.logging.Log; 
    2225import org.apache.commons.logging.LogFactory; 
    23 import org.openmrs.reporting.PatientSet; 
    24  
     26import org.openmrs.api.ReportService; 
     27import org.openmrs.cohort.CohortDefinition; 
     28import org.openmrs.report.EvaluationContext; 
     29import org.simpleframework.xml.Attribute; 
     30import org.simpleframework.xml.Element; 
     31import org.simpleframework.xml.ElementList; 
     32import 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) 
    2542public class Cohort implements Serializable { 
    2643 
     
    3855        private String voidReason; 
    3956        private Set<Integer> memberIds; 
     57        private CohortDefinition cohortDefinition; 
     58        private EvaluationContext evaluationContext; 
    4059 
    4160        public Cohort() { 
     
    4362        } 
    4463         
    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                 
    51103        public String toString() { 
    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"); 
    67105        } 
    68106         
     
    84122        } 
    85123         
     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         
    86173        // getters and setters 
    87174         
     175        @Attribute(required=false) 
    88176        public Integer getCohortId() { 
    89177                return cohortId; 
    90178        } 
    91179         
     180        @Attribute(required=false) 
    92181        public void setCohortId(Integer cohortId) { 
    93182                this.cohortId = cohortId; 
    94183        } 
    95184         
     185        @Attribute(required=false) 
    96186        public User getCreator() { 
    97187                return creator; 
    98188        } 
    99189         
     190        @Attribute(required=false) 
    100191        public void setCreator(User creator) { 
    101192                this.creator = creator; 
    102193        } 
    103194         
     195        @Attribute(required=false) 
    104196        public Date getDateCreated() { 
    105197                return dateCreated; 
    106198        } 
    107199         
     200        @Attribute(required=false) 
    108201        public void setDateCreated(Date dateCreated) { 
    109202                this.dateCreated = dateCreated; 
    110203        } 
    111204         
     205        @Attribute(required=false) 
    112206        public Date getDateVoided() { 
    113207                return dateVoided; 
    114208        } 
    115209         
     210        @Attribute(required=false) 
    116211        public void setDateVoided(Date dateVoided) { 
    117212                this.dateVoided = dateVoided; 
    118213        } 
    119214         
     215        @Element(required=false) 
    120216        public String getDescription() { 
    121217                return description; 
    122218        } 
    123219         
     220        @Element(required=false) 
    124221        public void setDescription(String description) { 
    125222                this.description = description; 
    126223        } 
    127224         
     225        @Element(required=false) 
    128226        public String getName() { 
    129227                return name; 
    130228        } 
    131229         
     230        @Element(required=false) 
    132231        public void setName(String name) { 
    133232                this.name = name; 
    134233        } 
    135234         
     235        @Attribute(required=false) 
    136236        public Boolean getVoided() { 
    137237                return voided; 
    138238        } 
    139239         
     240        @Attribute(required=false) 
    140241        public void setVoided(Boolean voided) { 
    141242                this.voided = voided; 
    142243        } 
    143244         
     245        @Attribute(required=false) 
    144246        public User getVoidedBy() { 
    145247                return voidedBy; 
    146248        } 
    147249         
     250        @Attribute(required=false) 
    148251        public void setVoidedBy(User voidedBy) { 
    149252                this.voidedBy = voidedBy; 
    150253        } 
    151254         
     255        @Attribute(required=false) 
    152256        public String getVoidReason() { 
    153257                return voidReason; 
    154258        } 
    155259         
     260        @Attribute(required=false) 
    156261        public void setVoidReason(String voidReason) { 
    157262                this.voidReason = voidReason; 
    158263        } 
    159264 
     265        @ElementList(required=true) 
    160266        public Set<Integer> getMemberIds() { 
    161267                return memberIds; 
    162268        } 
    163  
     269         
     270        /** 
     271         * This method is only here for some backwards compatibility  
     272         * with the PatientSet object that this Cohort object  
     273         * replaced.  Do not use this method. 
     274         *  
     275         * @deprecated use #getMemberIds() 
     276         * @return the memberIds 
     277         */ 
     278        public Set<Integer> getPatientIds() { 
     279                return getMemberIds(); 
     280        } 
     281         
     282 
     283        @ElementList(required=true) 
    164284        public void setMemberIds(Set<Integer> memberIds) { 
    165285                this.memberIds = memberIds; 
    166286        } 
    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         
    168320}