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