-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWNPRC_ComplianceController.java
More file actions
637 lines (521 loc) · 23.1 KB
/
WNPRC_ComplianceController.java
File metadata and controls
637 lines (521 loc) · 23.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
package org.labkey.wnprc_compliance;
import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.servlet.http.HttpServletResponse;
import org.json.JSONObject;
import org.labkey.api.action.AbstractFileUploadAction;
import org.labkey.api.action.Marshal;
import org.labkey.api.action.Marshaller;
import org.labkey.api.action.MutatingApiAction;
import org.labkey.api.action.ReadOnlyApiAction;
import org.labkey.api.action.SpringActionController;
import org.labkey.api.data.CompareType;
import org.labkey.api.data.DbSchema;
import org.labkey.api.data.DbScope;
import org.labkey.api.data.SimpleFilter;
import org.labkey.api.module.Module;
import org.labkey.api.module.ModuleLoader;
import org.labkey.api.query.FieldKey;
import org.labkey.api.security.ActionNames;
import org.labkey.api.security.CSRF;
import org.labkey.api.security.RequiresPermission;
import org.labkey.api.util.JsonUtil;
import org.labkey.dbutils.api.SimpleQueryFactory;
import org.labkey.dbutils.api.SimpleQueryUpdater;
import org.labkey.dbutils.api.SimplerFilter;
import org.labkey.vfs.FileLike;
import org.labkey.webutils.api.action.SimpleJspPageAction;
import org.labkey.wnprc_compliance.form.NewUserForm;
import org.labkey.wnprc_compliance.form.RequirementForm;
import org.labkey.wnprc_compliance.security.ComplianceAdminPermission;
import org.springframework.validation.BindException;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
public class WNPRC_ComplianceController extends SpringActionController {
private static final DefaultActionResolver _actionResolver = new DefaultActionResolver(WNPRC_ComplianceController.class);
public static final String NAME = "wnprc_compliance";
public WNPRC_ComplianceController() {
setActionResolver(_actionResolver);
}
public abstract class HRJspPageAction extends SimpleJspPageAction {
@Override
public Module getModule() {
return ModuleLoader.getInstance().getModule(WNPRC_ComplianceModule.class);
}
}
@RequiresPermission(ComplianceAdminPermission.class)
public class BeginAction extends HRJspPageAction {
@Override
public String getPathToJsp() {
return "/org/labkey/wnprc_compliance/view/begin.jsp";
}
@Override
public String getTitle() {
return "TB Dashboard";
}
}
@ActionNames("unidentifiedCards")
@RequiresPermission(ComplianceAdminPermission.class)
public class UnidentifiedCardsPage extends HRJspPageAction {
@Override
public String getPathToJsp() {
return "/org/labkey/wnprc_compliance/view/unidentifiedCards.jsp";
}
@Override
public String getTitle() {
return "Unidentified Cards";
}
}
@ActionNames("pendingTBResults")
@RequiresPermission(ComplianceAdminPermission.class)
public class PendingTBResultsPage extends HRJspPageAction {
@Override
public String getPathToJsp() {
return "/org/labkey/wnprc_compliance/view/pendingTBResults.jsp";
}
@Override
public String getTitle() {
return "Pending TB Results";
}
}
@ActionNames("enterTBPage")
@RequiresPermission(ComplianceAdminPermission.class)
public class NewUserPage extends HRJspPageAction {
@Override
public String getPathToJsp() {
return "/org/labkey/wnprc_compliance/view/enterTB.jsp";
}
@Override
public String getTitle() {
return "New User";
}
}
@ActionNames("editTBPage")
@RequiresPermission(ComplianceAdminPermission.class)
public class EditTBPage extends HRJspPageAction {
@Override
public String getPathToJsp() {
return "/org/labkey/wnprc_compliance/view/editTB.jsp";
}
@Override
public String getTitle() {
return "Edit TB";
}
}
@ActionNames("editUserPage")
@RequiresPermission(ComplianceAdminPermission.class)
public class EditUserPage extends HRJspPageAction {
@Override
public String getPathToJsp() {
return "/org/labkey/wnprc_compliance/view/userEdit.jsp";
}
@Override
public String getTitle() {
return "Edit User";
}
}
public static class SearchPersonForm {
public String query;
public void setQuery(String term) {
this.query = term;
}
public String getQuery() {
return this.query;
}
}
public static class SearchPersonFromCardForm {
public String query;
public void setQuery(String term) {
this.query = term;
}
public String getQuery() {
return this.query;
}
}
public static class SearchClearanceFromPersonForm {
public String query;
public String table;
public void setQuery(String term) {
this.query = term;
}
public String getQuery() {
return this.query;
}
public void setTable(String table) {
this.table = table;
}
public String getTable() {
return this.table;
}
}
public static class AddDataToExistingPersonForm {
public String personid;
public boolean hold;
public boolean measles_required;
public RequirementForm tbInfo;
public RequirementForm measlesInfo;
}
@ActionNames("updatePersonClearances")
@RequiresPermission(ComplianceAdminPermission.class)
@Marshal(Marshaller.Jackson)
@CSRF(CSRF.Method.POST)
public class UpdatePersonClearanceAPI extends MutatingApiAction<AddDataToExistingPersonForm> {
@Override
public Object execute(AddDataToExistingPersonForm form, BindException errors) throws Exception {
JSONObject json = new JSONObject();
try (DbScope.Transaction transaction = DbSchema.get(WNPRC_ComplianceSchema.NAME).getScope().beginTransaction()) {
PersonService service = new PersonService(getUser(), getContainer());
String personId = form.personid;
if (form.tbInfo != null) {
if (form.tbInfo.pending) {
service.addClearance(personId, "pending_tb_clearances", "persons_pending_tb_clearances", form.tbInfo);
}
else {
service.addClearance(personId, "tb_clearances", "persons_tb_clearances", form.tbInfo);
}
}
if (form.measlesInfo != null) {
service.addClearance(personId,"measles_clearances", "persons_measles_clearances", form.measlesInfo);
}
json.put("personid", personId);
transaction.commit();
}
return json;
}
}
public static class NewUserFormWithData extends NewUserForm {
public RequirementForm tbInfo;
public RequirementForm measlesInfo;
}
@ActionNames("newUser")
@RequiresPermission(ComplianceAdminPermission.class)
@Marshal(Marshaller.Jackson)
@CSRF(CSRF.Method.POST)
public class NewUserAPI extends MutatingApiAction<NewUserFormWithData> {
@Override
public Object execute(NewUserFormWithData form, BindException errors) throws Exception {
PersonService service = new PersonService(getUser(), getContainer());
JSONObject returnJSON = new JSONObject();
try (DbScope.Transaction transaction = DbSchema.get(WNPRC_ComplianceSchema.NAME).getScope().ensureTransaction()) {
String personId = service.newUser((NewUserForm) form);
//String hold = service.newUser((NewUserForm) form);
if (form.tbInfo != null) {
if (form.tbInfo.pending) {
service.addClearance(personId, "pending_tb_clearances", "persons_pending_tb_clearances", form.tbInfo);
}
else {
service.addClearance(personId, "tb_clearances", "persons_tb_clearances", form.tbInfo);
}
}
if (form.measlesInfo != null) {
service.addClearance(personId,"measles_clearances", "persons_measles_clearances", form.measlesInfo);
}
returnJSON.put("personid", personId);
returnJSON.put("hold", form.hold);
transaction.commit();
}
return returnJSON;
}
}
@ActionNames("uploadAccessReport")
@RequiresPermission(ComplianceAdminPermission.class)
public class UploadAccessReportAPI extends AbstractFileUploadAction {
@Override
protected FileLike handleFile(String filename, InputStream input, Writer writer) throws IOException {
try {
AccessReportService service = new AccessReportService(getUser(), getContainer());
service.importReport(input, filename);
writer.write("Handled file: " + filename);
}
catch (Throwable e) {
getViewContext().getResponse().reset();
getViewContext().getResponse().setStatus(HttpServletResponse.SC_BAD_REQUEST);
getViewContext().getResponse().setContentType("text/plain");
writer.write(e.getMessage());
writer.flush();
}
return null;
}
@Override
protected FileLike getTargetFile(String filename) throws IOException {
// We'll be intercepting the stream, and we override handleFile(), so we don't need
// to implement this. If that ever changes, look at using /dev/null ("NUL" for Windows)
// to truly give a data sink.
return null;
}
@Override
public String getResponse(FileUploadForm form, Map files) throws UploadException
{
return "";
}
}
@ActionNames("uploadAccessReportPage")
@RequiresPermission(ComplianceAdminPermission.class)
public class UploadAccessReportPage extends HRJspPageAction {
@Override
public String getPathToJsp() {
return "/org/labkey/wnprc_compliance/view/uploadAccessReport.jsp";
}
@Override
public String getTitle() {
return "Upload Access Report";
}
}
@ActionNames("personSearch")
@RequiresPermission(ComplianceAdminPermission.class)
public class SearchUserAPI extends ReadOnlyApiAction<SearchPersonForm> {
@Override
public Object execute(SearchPersonForm form, BindException errors)
{
JSONObject json = new JSONObject();
Map<String, List<JSONObject>> results = new HashMap<>();
// Construct an "OR" clause based on space-delimited queries.
SimpleFilter.OrClause orClause = new SimpleFilter.OrClause();
for (String queryTerm : form.getQuery().split(" +")) {
if (!queryTerm.equals("")) {
orClause.addClause(new SimplerFilter("displayLcase", CompareType.CONTAINS, queryTerm).getClauses().get(0));
}
}
SimpleFilter filter = new SimpleFilter();
filter.addClause(orClause);
SimpleQueryFactory factory = new SimpleQueryFactory(getUser(), getContainer());
for (JSONObject row : JsonUtil.toJSONObjectList(factory.selectRows(WNPRC_ComplianceSchema.NAME, "searchResults", filter))) {
String type = row.getString("type");
List<JSONObject> rows = results.get(type);
if (rows == null) {
rows = new ArrayList<>();
results.put(type, rows);
}
rows.add(row);
}
json.put("results", results);
return json;
}
}
public static class CardExemptForm {
public Integer cardId;
public String reason;
public String personId;
}
public static class CardLinkForm {
public Integer cardId;
public String personId;
}
@ActionNames("markCardsExempt")
@RequiresPermission(ComplianceAdminPermission.class)
@Marshal(Marshaller.Jackson)
@CSRF(CSRF.Method.POST)
public class MarkCardExemptAPI extends MutatingApiAction<CardExemptForm> {
@Override
public Object execute(CardExemptForm form, BindException errors) throws Exception {
JSONObject json = new JSONObject();
try (DbScope.Transaction transaction = DbSchema.get(WNPRC_ComplianceSchema.NAME).getScope().ensureTransaction()) {
SimpleQueryUpdater cardUpdater = new SimpleQueryUpdater(getUser(), getContainer(), WNPRC_ComplianceSchema.NAME, "cards");
JSONObject card = new JSONObject();
card.put("card_id", form.cardId.toString());
card.put("exempt_reason", form.reason);
card.put("container", getContainer().getId());
card.put("exempt", true);
cardUpdater.upsert(card);
json.put("success", true);
transaction.commit();
}
return json;
}
}
@ActionNames("getPersons")
@RequiresPermission(ComplianceAdminPermission.class)
@Marshal(Marshaller.Jackson)
public class GetPersonFromCardAPI extends ReadOnlyApiAction<SearchPersonFromCardForm> {
public Object execute(SearchPersonFromCardForm form, BindException errors) throws Exception {
JSONObject json = new JSONObject();
Map<String, List<JSONObject>> results = new HashMap<>();
// Construct an "OR" clause based on space-delimited queries.
SimpleFilter.OrClause orClause = new SimpleFilter.OrClause();
if (!form.getQuery().equals("")) {
orClause.addClause(new SimplerFilter("displayLcase", CompareType.CONTAINS, form.getQuery()).getClauses().get(0));
}
SimpleFilter filter = new SimpleFilter();
filter.addClause(orClause);
SimpleQueryFactory factory = new SimpleQueryFactory(getUser(), getContainer());
for (JSONObject row : JsonUtil.toJSONObjectList(factory.selectRows(WNPRC_ComplianceSchema.NAME, "searchResults", filter)))
{
String type = row.getString("type");
List<JSONObject> rows = results.get(type);
if (rows == null) {
rows = new ArrayList<>();
results.put(type, rows);
}
rows.add(row);
}
json.put("results", results);
return json;
}
}
@ActionNames("getClearances")
@RequiresPermission(ComplianceAdminPermission.class)
@Marshal(Marshaller.Jackson)
@CSRF(CSRF.Method.NONE)
public class GetClearancesFromPerson extends ReadOnlyApiAction<SearchClearanceFromPersonForm> {
public Object execute(SearchClearanceFromPersonForm form, BindException errors)
{
int resultLimit = 3;
JSONObject json = new JSONObject();
SimpleFilter filter = new SimpleFilter();
filter.addCondition(FieldKey.fromString("personid"), form.getQuery(), CompareType.EQUAL);
SimpleQueryFactory factory = new SimpleQueryFactory(getUser(), getContainer());
List<JSONObject> rows = new ArrayList<>();
int resultCount = 0;
for (JSONObject row : JsonUtil.toJSONObjectList(factory.selectRows(WNPRC_ComplianceSchema.NAME, form.getTable(), filter))) {
if (resultCount < resultLimit)
{
rows.add(row);
}
resultCount++;
}
json.put("results", rows);
return json;
}
}
public static class ClearanceForm {
public String notes;
public String first_name;
public String last_name;
public String middle_name;
public String personid;
public boolean measles_required;
public boolean hold;
@JsonFormat(pattern="yyyy-MM-dd'T'HH:mm:ss")
public Date date;
public String id;
public String _row;
public String table_name;
public boolean mutated;
}
public static class Clearances {
public ClearanceForm[] clearances;
public String table_name;
}
@ActionNames("updateClearance")
@RequiresPermission(ComplianceAdminPermission.class)
@Marshal(Marshaller.Jackson)
@CSRF(CSRF.Method.POST)
public class UpdateClearanceAPI extends MutatingApiAction<Clearances> {
@Override
public Object execute(Clearances form, BindException errors) throws Exception {
JSONObject json = new JSONObject();
try (DbScope.Transaction transaction = DbSchema.get(WNPRC_ComplianceSchema.NAME).getScope().ensureTransaction()) {
SimpleQueryUpdater tbClearanceUpdater = new SimpleQueryUpdater(getUser(), getContainer(), WNPRC_ComplianceSchema.NAME, form.table_name);
List<Map<String, Object>> clearancesToUpdate = new ArrayList<>();
for (ClearanceForm tbform : form.clearances)
{
Map<String, Object> tbClearance = new HashMap<>();
tbClearance.put("id", tbform.id);
tbClearance.put("date", tbform.date);
tbClearance.put("container", getContainer().getId());
clearancesToUpdate.add(tbClearance);
}
tbClearanceUpdater.upsert(clearancesToUpdate);
json.put("success", true);
transaction.commit();
}
return json;
}
}
@ActionNames("getMeaslesClearances")
@RequiresPermission(ComplianceAdminPermission.class)
@Marshal(Marshaller.Jackson)
@CSRF(CSRF.Method.NONE)
public class GetMeaslesClearanceFromPerson extends ReadOnlyApiAction<SearchPersonFromCardForm> {
public Object execute(SearchPersonFromCardForm form, BindException errors)
{
JSONObject json = new JSONObject();
SimpleFilter filter = new SimpleFilter();
filter.addCondition(FieldKey.fromString("personid"), form.getQuery(), CompareType.EQUAL);
SimpleQueryFactory factory = new SimpleQueryFactory(getUser(), getContainer());
List<JSONObject> rows = new ArrayList<>(JsonUtil.toJSONObjectList(factory.selectRows(WNPRC_ComplianceSchema.NAME, "mapMeaslesClearances", filter)));
json.put("results", rows);
return json;
}
}
@ActionNames("linkCards")
@RequiresPermission(ComplianceAdminPermission.class)
@Marshal(Marshaller.Jackson)
@CSRF(CSRF.Method.POST)
public class LinkCardAPI extends MutatingApiAction<CardLinkForm> {
@Override
public Object execute(CardLinkForm form, BindException errors) throws Exception {
JSONObject json = new JSONObject();
try (DbScope.Transaction transaction = DbSchema.get(WNPRC_ComplianceSchema.NAME).getScope().ensureTransaction()) {
SimpleQueryUpdater cardUpdater = new SimpleQueryUpdater(getUser(), getContainer(), WNPRC_ComplianceSchema.NAME, "persons_to_cards");
JSONObject personToCard = new JSONObject();
personToCard.put("cardid", form.cardId.toString());
personToCard.put("personid", form.personId);
personToCard.put("container", getContainer().getId());
cardUpdater.upsert(personToCard);
json.put("success", true);
transaction.commit();
}
return json;
}
}
public static class ResolvePendingTBResultsForm {
public String[] pendingTBIds;
public String notes;
@JsonFormat(pattern="yyyy-MM-dd'T'HH:mm:ssXXX")
public Date date;
}
@ActionNames("resolvePendingTBResults")
@RequiresPermission(ComplianceAdminPermission.class)
@CSRF(CSRF.Method.POST)
@Marshal(Marshaller.Jackson)
public class ResolvePendingTBResultsAPI extends MutatingApiAction<ResolvePendingTBResultsForm> {
@Override
public Object execute(ResolvePendingTBResultsForm form, BindException errors) throws Exception {
JSONObject json = new JSONObject();
SimpleQueryFactory queryFactory = new SimpleQueryFactory(getUser(), getContainer());
try (DbScope.Transaction transaction = DbSchema.get(WNPRC_ComplianceSchema.NAME).getScope().ensureTransaction()) {
SimpleQueryUpdater pendingTbUpdater = new SimpleQueryUpdater(getUser(), getContainer(), WNPRC_ComplianceSchema.NAME, "pending_tb_clearances");
SimpleQueryUpdater tbUpdater = new SimpleQueryUpdater(getUser(), getContainer(), WNPRC_ComplianceSchema.NAME, "tb_clearances");
SimpleQueryUpdater tbMapUpdater = new SimpleQueryUpdater(getUser(), getContainer(), WNPRC_ComplianceSchema.NAME, "persons_tb_clearances");
List<Map<String, Object>> pendingTBsToUpdate = new ArrayList<>();
List<Map<String, Object>> tBsToUpdate = new ArrayList<>();
List<Map<String, Object>> tbMapsToUpdate = new ArrayList<>();
for (String pendingTBId : form.pendingTBIds) {
String tbClearanceId = UUID.randomUUID().toString().toUpperCase();
// Insert the new clearance
Map<String, Object> tbClearance = new HashMap<>();
tbClearance.put("id", tbClearanceId);
tbClearance.put("date", form.date);
tbClearance.put("comment", form.notes);
tbClearance.put("container", getContainer().getId());
tBsToUpdate.add(tbClearance);
// Update the pending clearance to point to the completed one.
Map<String, Object> pendingTbClearance = new HashMap<>();
pendingTbClearance.put("id", pendingTBId);
pendingTbClearance.put("tbclearance_id", tbClearanceId);
pendingTbClearance.put("container", getContainer().getId());
pendingTBsToUpdate.add(pendingTbClearance);
List<JSONObject> rows = JsonUtil.toJSONObjectList(queryFactory.selectRows(WNPRC_ComplianceSchema.NAME, "persons_pending_tb_clearances", new SimplerFilter("clearance_id", CompareType.EQUAL, pendingTBId)));
Map<String, Object> tbMap = new HashMap<>();
tbMap.put("person_id", rows.get(0).getString("person_id"));
tbMap.put("clearance_id", tbClearanceId);
tbMap.put("container", getContainer().getId());
tbMapsToUpdate.add(tbMap);
}
tbUpdater.upsert(tBsToUpdate);
pendingTbUpdater.upsert(pendingTBsToUpdate);
tbMapUpdater.upsert(tbMapsToUpdate);
json.put("success", true);
transaction.commit();
}
return json;
}
}
}