|
27 | 27 | import org.labkey.snprc_scheduler.domains.TimelineAnimalJunction; |
28 | 28 | import org.labkey.snprc_scheduler.domains.TimelineItem; |
29 | 29 | import org.labkey.snprc_scheduler.domains.TimelineProjectItem; |
| 30 | +import org.labkey.snprc_scheduler.security.QCStateEnum; |
30 | 31 |
|
31 | 32 | import java.util.ArrayList; |
32 | 33 | import java.util.Date; |
33 | 34 | import java.util.List; |
34 | 35 | import java.util.Map; |
35 | 36 |
|
| 37 | +import static org.apache.commons.lang3.StringUtils.isNotBlank; |
| 38 | + |
36 | 39 | /** |
37 | 40 | * Created by thawkins on 10/21/2018 |
38 | 41 | */ |
@@ -91,37 +94,59 @@ public List<JSONObject> getActiveTimelines(Container c, User u, String projectOb |
91 | 94 | } |
92 | 95 |
|
93 | 96 |
|
94 | | - //TODO: need to add scheduleDate criteria |
| 97 | + /** |
| 98 | + * Retrieves timelines for a given species that have procedures scheduled on a specific date. |
| 99 | + * Optionally filters by QC state. |
| 100 | + * |
| 101 | + * @param c = Container object |
| 102 | + * @param u = User object |
| 103 | + * @param species = species identifier to filter timelines |
| 104 | + * @param date = schedule date to match against timeline items |
| 105 | + * @param qcState = optional QC state name to further filter timelines |
| 106 | + * @param errors = exception object for collecting validation errors |
| 107 | + * @return list of timeline JSON objects matching the criteria |
| 108 | + */ |
95 | 109 | @Override |
96 | | - public List<JSONObject> getScheduledTimelinesForSpecies(Container c, User u, String species, Date date, BatchValidationException errors) throws ApiUsageException |
| 110 | + public List<JSONObject> getScheduledTimelinesForSpecies(Container c, User u, String species, Date date, String qcState, BatchValidationException errors) throws ApiUsageException |
97 | 111 | { |
98 | 112 | List<JSONObject> timelinesJson = new ArrayList<>(); |
99 | 113 | try |
100 | 114 | { |
101 | 115 | SNPRC_schedulerUserSchema schema = SNPRC_schedulerManager.getSNPRC_schedulerUserSchema(c, u); |
102 | 116 | TableInfo timelineTable = schema.getTable(SNPRC_schedulerSchema.TABLE_NAME_TIMELINE, schema.getDefaultContainerFilter(), false, false); |
103 | 117 |
|
104 | | - // only return timelines with procedures scheduled on specified date |
| 118 | + // Query for timeline ObjectIds that have items scheduled on the given date |
105 | 119 | SQLFragment sql = new SQLFragment(); |
106 | 120 | sql.append("SELECT DISTINCT t." + Timeline.TIMELINE_OBJECTID); |
107 | 121 | sql.append(" FROM "); |
108 | 122 | sql.append(SNPRC_schedulerSchema.getInstance().getTableInfoTimeline(), "t"); |
109 | 123 | sql.append(" JOIN "); |
110 | 124 | sql.append(SNPRC_schedulerSchema.getInstance().getTableInfoTimelineItem(), "ti"); |
111 | 125 | sql.append(" ON t." + Timeline.TIMELINE_OBJECTID + " = ti." + TimelineItem.TIMELINEITEM_TIMELINE_OBJECT_ID); |
112 | | - sql.append(" WHERE " + "ti." + TimelineItem.TIMELINEITEM_SCHEDULE_DATE + " = ?" ).add(date); |
| 126 | + sql.append(" WHERE ti." + TimelineItem.TIMELINEITEM_SCHEDULE_DATE + " = ?").add(date); |
113 | 127 |
|
114 | 128 | SqlSelector selector = new SqlSelector(SNPRC_schedulerSchema.getInstance().getSchema(), sql); |
115 | 129 |
|
116 | 130 | List<String> objectIds = new ArrayList<>(); |
117 | 131 | selector.forEachMap(row -> objectIds.add( (String) row.get(Timeline.TIMELINE_OBJECTID))); |
118 | 132 |
|
119 | | - //SimpleFilter filter = new SimpleFilter(FieldKey.fromParts(Timeline.TIMELINE_SPECIES, "referenceId", "species"), species, CompareType.EQUAL); |
| 133 | + // Build filter for species, matching ObjectIds, and optional QC state |
120 | 134 | SimpleFilter filter = new SimpleFilter(FieldKey.fromParts("sndProject", "referenceId", "species"), species, CompareType.EQUAL); |
121 | | - filter.addInClause(FieldKey.fromParts(Timeline.TIMELINE_OBJECTID), objectIds ); |
| 135 | + filter.addInClause(FieldKey.fromParts(Timeline.TIMELINE_OBJECTID), objectIds); |
| 136 | + |
| 137 | + if (isNotBlank(qcState)) |
| 138 | + { |
| 139 | + Integer qcStateId = QCStateEnum.getQCStateEnumId(c, u, |
| 140 | + QCStateEnum.getQCStateEnumByName(qcState)); |
| 141 | + if (qcStateId != null) |
| 142 | + { |
| 143 | + filter.addCondition(FieldKey.fromParts(Timeline.TIMELINE_QCSTATE), qcStateId, CompareType.EQUAL); |
| 144 | + } |
| 145 | + } |
122 | 146 |
|
123 | 147 | List<Timeline> timelines = new TableSelector(timelineTable, filter, null).getArrayList(Timeline.class); |
124 | 148 |
|
| 149 | + // Populate nested collections for each timeline and convert to JSON |
125 | 150 | for (Timeline timeline : timelines) |
126 | 151 | { |
127 | 152 | timeline.setTimelineItems(SNPRC_schedulerManager.get().getTimelineItems(c, u, timeline.getObjectId(), date)); |
|
0 commit comments