• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

openmrs / openmrs-core / 44617254895

19 Nov 2026 09:17PM UTC coverage: 69.102% (-0.2%) from 69.299%
44617254895

Pull #6841

github

web-flow
Merge 613c9f9ce into 57615988d
Pull Request #6841: TRUNK-6868: Allow the session timeout to be configured (2.8.x)

24 of 28 new or added lines in 2 files covered. (85.71%)

88 existing lines in 14 files now uncovered.

26833 of 38831 relevant lines covered (69.1%)

0.69 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

77.98
/api/src/main/java/org/openmrs/Cohort.java
1
/**
2
 * This Source Code Form is subject to the terms of the Mozilla Public License,
3
 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
4
 * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
5
 * the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
6
 *
7
 * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
8
 * graphic logo is a trademark of OpenMRS Inc.
9
 */
10
package org.openmrs;
11

12
import org.apache.commons.lang3.StringUtils;
13
import org.hibernate.envers.Audited;
14
import org.openmrs.util.CollectionUtil;
15

16
import java.util.Arrays;
17
import java.util.Collection;
18
import java.util.Date;
19
import java.util.Set;
20
import java.util.TreeSet;
21
import java.util.stream.Collectors;
22

23
/**
24
 * This class represents a list of patientIds.
25
 */
26
@Audited
27
public class Cohort extends BaseChangeableOpenmrsData {
28
        
29
        public static final long serialVersionUID = 0L;
30
        
31
        private Integer cohortId;
32
        
33
        private String name;
34
        
35
        private String description;
36
        
37
        private Collection<CohortMembership> memberships;
38
        
39
        public Cohort() {
1 ✔
40
                memberships = new TreeSet<>();
1 ✔
41
        }
1 ✔
42
        
43
        /**
44
         * Convenience constructor to create a Cohort object that has an primarykey/internal identifier
45
         * of <code>cohortId</code>
46
         *
47
         * @param cohortId the internal identifier for this cohort
48
         */
49
        public Cohort(Integer cohortId) {
50
                this();
1 ✔
51
                this.cohortId = cohortId;
1 ✔
52
        }
1 ✔
53
        
54
        /**
55
         * This constructor does not check whether the database contains patients with the given ids,
56
         * but {@link org.openmrs.api.CohortService#saveCohort(Cohort)} will.
57
         * 
58
         * @param name
59
         * @param description optional description
60
         * @param ids option array of Integer ids
61
         */
62
        public Cohort(String name, String description, Integer[] ids) {
63
                this();
1 ✔
64
                this.name = name;
1 ✔
65
                this.description = description;
1 ✔
66
                if (ids != null) {
1 ✔
67
                        Arrays.stream(ids).forEach(this::addMember);
1 ✔
68
                }
69
        }
1 ✔
70
        
71
        /**
72
         * This constructor does not check whether the database contains patients with the given ids,
73
         * but {@link org.openmrs.api.CohortService#saveCohort(Cohort)} will.
74
         * 
75
         * @param name
76
         * @param description optional description
77
         * @param patients optional array of patients
78
         */
79
        public Cohort(String name, String description, Patient[] patients) {
80
                this(name, description, (Integer[]) null);
×
81
                if (patients != null) {
×
82
                        Arrays.stream(patients).forEach(p -> addMembership(new CohortMembership(p.getPatientId())));
×
83
                }
84
        }
×
85
        
86
        /**
87
         * This constructor does not check whether the database contains patients with the given ids,
88
         * but {@link org.openmrs.api.CohortService#saveCohort(Cohort)} will.
89
         * 
90
         * @param patientsOrIds optional collection which may contain Patients, or patientIds which may
91
         *            be Integers, Strings, or anything whose toString() can be parsed to an Integer.
92
         */
93
        public Cohort(Collection<?> patientsOrIds) {
94
                this(null, null, patientsOrIds);
×
95
        }
×
96
        
97
        /**
98
         * This constructor does not check whether the database contains patients with the given ids,
99
         * but {@link org.openmrs.api.CohortService#saveCohort(Cohort)} will.
100
         * 
101
         * @param name
102
         * @param description optional description
103
         * @param patientsOrIds optional collection which may contain Patients, or patientIds which may
104
         *            be Integers, Strings, or anything whose toString() can be parsed to an Integer.
105
         */
106
        public Cohort(String name, String description, Collection<?> patientsOrIds) {
107
                this(name, description, (Integer[]) null);
1 ✔
108
                if (patientsOrIds != null) {
1 ✔
109
                        for (Object o : patientsOrIds) {
1 ✔
110
                                if (o instanceof Patient) {
1 ✔
111
                                        addMembership(new CohortMembership(((Patient) o).getPatientId()));
1 ✔
112
                                } else if (o instanceof Integer) {
×
113
                                        addMembership(new CohortMembership((Integer) o));
×
114
                                }
115
                        }
1 ✔
116
                }
117
        }
1 ✔
118
        
119
        /**
120
         * Convenience constructor taking in a string that is a list of comma separated patient ids This
121
         * constructor does not check whether the database contains patients with the given ids, but
122
         * {@link org.openmrs.api.CohortService#saveCohort(Cohort)} will.
123
         * 
124
         * @param commaSeparatedIds
125
         */
126
        public Cohort(String commaSeparatedIds) {
127
                this();
1 ✔
128
                String[] ids = StringUtils.split(commaSeparatedIds, ',');
1 ✔
129
                Arrays.stream(ids).forEach(id -> addMembership(new CohortMembership(Integer.valueOf(id.trim()))));
1 ✔
130
        }
1 ✔
131
        
132
        /**
133
         * @deprecated since 2.1.0 cohorts are more complex than just a set of patient ids, so there is no one-line replacement
134
         * @return Returns a comma-separated list of patient ids in the cohort.
135
         */
136
        @Deprecated
137
        public String getCommaSeparatedPatientIds() {
138
                return StringUtils.join(getMemberIds(), ',');
1 ✔
139
        }
140
        
141
        public boolean contains(Integer patientId) {
142
                return getMemberships() != null
1 ✔
143
                        && getMemberships().stream().anyMatch(m -> m.getPatientId().equals(patientId) && !m.getVoided());
1 ✔
144
        }
145
        
146
        @Override
147
        public String toString() {
UNCOV
148
                StringBuilder sb = new StringBuilder("Cohort id=" + getCohortId());
×
UNCOV
149
                if (getName() != null) {
×
UNCOV
150
                        sb.append(" name=").append(getName());
×
151
                }
UNCOV
152
                if (getMemberships() != null) {
×
UNCOV
153
                        sb.append(" size=").append(getMemberships().size());
×
154
                }
UNCOV
155
                return sb.toString();
×
156
        }
157
        
158
        public void addMember(Integer memberId) {
159
                this.addMembership(new CohortMembership(memberId));
1 ✔
160
        }
1 ✔
161
        
162
        /**
163
         * @since 2.1.0
164
         */
165
        public boolean addMembership(CohortMembership cohortMembership) {
166
                if (cohortMembership != null) {
1 ✔
167
                        cohortMembership.setCohort(this);
1 ✔
168
                        return getMemberships().add(cohortMembership);
1 ✔
169
                }
170
                return false;
×
171
        }
172
        
173
        /**
174
         * @since 2.1.0
175
         */
176
        public boolean removeMembership(CohortMembership cohortMembership) {
177
                return getMemberships().remove(cohortMembership);
1 ✔
178
        }
179
        
180
        /**
181
         * @since 2.1.0
182
         * @param includeVoided boolean true/false to include/exclude voided memberships
183
         * @return Collection of cohort memberships
184
         */
185
        public Collection<CohortMembership> getMemberships(boolean includeVoided) {
186
                if (includeVoided) {
1 ✔
187
                        return getMemberships();
1 ✔
188
                }
189
                return getMemberships().stream().filter(m -> m.getVoided() == includeVoided).collect(Collectors.toList());
1 ✔
190
        }
191
        
192
        /**
193
         * @since 2.1.0
194
         */
195
        public Collection<CohortMembership> getMemberships() {
196
                if (memberships == null) {
1 ✔
197
                        memberships = new TreeSet<>();
×
198
                }
199
                return memberships;
1 ✔
200
        }
201
        
202
        /**
203
         * @since 2.1.0
204
         * @param asOfDate date used to return active memberships
205
         * @return Collection of cohort memberships
206
         */
207
        public Collection<CohortMembership> getActiveMemberships(Date asOfDate) {
208
                return getMemberships().stream().filter(m -> m.isActive(asOfDate)).collect(Collectors.toList());
1 ✔
209
        }
210
        
211
        public Collection<CohortMembership> getActiveMemberships() {
212
                return getActiveMemberships(new Date());
1 ✔
213
        }
214
        
215
        /**
216
         * @since 2.1.0
217
         */
218
        public CohortMembership getActiveMembership(Patient patient) {
219
                return getMemberships().stream().filter(m -> m.isActive() && m.getPatientId().equals(patient.getPatientId())).findFirst().get();
×
220
        }
221
        
222
        public int size() {
223
                return getMemberships().stream().filter(m -> !m.getVoided()).collect(Collectors.toList())
1 ✔
224
                        .size();
1 ✔
225
        }
226
        
227
        /**
228
         * @deprecated use {@link #size()}
229
         */
230
        @Deprecated
231
        public int getSize() {
232
                return size();
×
233
        }
234
        
235
        public boolean isEmpty() {
236
                return size() == 0;
×
237
        }
238
        
239
        // static utility methods
240
        
241
        /**
242
         * Returns the union of two cohorts
243
         *
244
         * @param a The first Cohort
245
         * @param b The second Cohort
246
         * @return Cohort
247
         */
248
        public static Cohort union(Cohort a, Cohort b) {
249
                Cohort ret = new Cohort();
1 ✔
250
                if (a != null) {
1 ✔
251
                        ret.getMemberships().addAll(a.getMemberships());
1 ✔
252
                }
253
                if (b != null) {
1 ✔
254
                        ret.getMemberships().addAll(b.getMemberships());
1 ✔
255
                }
256
                if (a != null && b != null) {
1 ✔
257
                        ret.setName("(" + a.getName() + " + " + b.getName() + ")");
1 ✔
258
                }
259
                return ret;
1 ✔
260
        }
261
        
262
        /**
263
         * Returns the intersection of two cohorts, treating null as an empty cohort
264
         *
265
         * @param a The first Cohort
266
         * @param b The second Cohort
267
         * @return Cohort
268
         */
269
        public static Cohort intersect(Cohort a, Cohort b) {
270
                Cohort ret = new Cohort();
1 ✔
271
                ret.setName("(" + (a == null ? "NULL" : a.getName()) + " * " + (b == null ? "NULL" : b.getName()) + ")");
1 ✔
272
                if (a != null && b != null) {
1 ✔
273
                        ret.getMemberships().addAll(a.getMemberships());
1 ✔
274
                        ret.getMemberships().retainAll(b.getMemberships());
1 ✔
275
                }
276
                return ret;
1 ✔
277
        }
278
        
279
        /**
280
         * Subtracts a cohort from a cohort
281
         *
282
         * @param a the original Cohort
283
         * @param b the Cohort to subtract
284
         * @return Cohort
285
         */
286
        public static Cohort subtract(Cohort a, Cohort b) {
287
                Cohort ret = new Cohort();
1 ✔
288
                if (a != null) {
1 ✔
289
                        ret.getMemberships().addAll(a.getMemberships());
1 ✔
290
                        if (b != null) {
1 ✔
291
                                ret.getMemberships().removeAll(b.getMemberships());
1 ✔
292
                                ret.setName("(" + a.getName() + " - " + b.getName() + ")");
1 ✔
293
                        }
294
                }
295
                return ret;
1 ✔
296
        }
297
        
298
        // getters and setters
299
        
300
        public Integer getCohortId() {
301
                return cohortId;
1 ✔
302
        }
303
        
304
        public void setCohortId(Integer cohortId) {
305
                this.cohortId = cohortId;
1 ✔
306
        }
1 ✔
307
        
308
        public String getDescription() {
309
                return description;
1 ✔
310
        }
311
        
312
        public void setDescription(String description) {
313
                this.description = description;
1 ✔
314
        }
1 ✔
315
        
316
        public String getName() {
317
                return name;
1 ✔
318
        }
319
        
320
        public void setName(String name) {
321
                this.name = name;
1 ✔
322
        }
1 ✔
323
        
324
        /**
325
         * @deprecated since 2.1.0 cohorts are more complex than just a set of patient ids, so there is no one-line replacement
326
         */
327
        @Deprecated
328
        public Set<Integer> getMemberIds() {
329
                Set<Integer> memberIds = new TreeSet<>();
1 ✔
330
                for (CohortMembership member : getMemberships()) {
1 ✔
331
                        memberIds.add(member.getPatientId());
1 ✔
332
                }
1 ✔
333
                return memberIds;
1 ✔
334
        }
335
        
336
        /**
337
         * @deprecated since 2.1.0 cohorts are more complex than just a set of patient ids, so there is no one-line replacement
338
         * @param memberIds
339
         */
340
        @Deprecated
341
        public void setMemberIds(Set<Integer> memberIds) {
342
                if (getMemberships().isEmpty()) {
1 ✔
343
                        for (Integer id : memberIds) {
1 ✔
344
                                addMembership(new CohortMembership(id));
1 ✔
345
                        }
1 ✔
346
                }
347
                else {
348
                        throw new IllegalArgumentException("since 2.1.0 cohorts are more complex than just a set of patient ids");
×
349
                }
350
        }
1 ✔
351
        
352
        public void setMemberships(Collection<CohortMembership> members) {
353
                this.memberships = CollectionUtil.setCollection(this.memberships, members);
×
354
        }
×
355
        
356
        /**
357
         * @since 1.5
358
         * @see org.openmrs.OpenmrsObject#getId()
359
         */
360
        @Override
361
        public Integer getId() {
362
                
363
                return getCohortId();
1 ✔
364
        }
365
        
366
        /**
367
         * @since 1.5
368
         * @see org.openmrs.OpenmrsObject#setId(java.lang.Integer)
369
         */
370
        @Override
371
        public void setId(Integer id) {
372
                setCohortId(id);
×
373
                
374
        }
×
375
        
376
        /**
377
         * @since 2.3
378
         * 
379
         * This function checks if there exists any active CohortMembership for a given patientId
380
         * 
381
         * @param patientId is the patientid that should be checked for activity in cohort
382
         * @return true if cohort has active membership for the requested patient             
383
         */
384
        public boolean hasActiveMembership(int patientId) {
385
                return getMemberships().stream().anyMatch(m  -> m.getPatientId() == patientId && m.isActive());
1 ✔
386
        }
387
        
388
        /**
389
         * 
390
         * @since  2.3
391
         * This method returns the number of active members in the cohort
392
         * 
393
         * @return  number of active memberships in the cohort
394
         */
395
        public int activeMembershipSize() {
396
                return getActiveMemberships().size();
1 ✔
397
        }
398
        
399
        /**
400
         *
401
         * @since  2.3
402
         * This method returns true if cohort has no active memberships
403
         *
404
         * @return true if no active cohort exists
405
         **/
406
        public boolean hasNoActiveMemberships() {
407
                return getActiveMemberships().isEmpty();
1 ✔
408
        }
409
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc