-
Notifications
You must be signed in to change notification settings - Fork 15
/
meetingGrid.ur
1292 lines (1160 loc) · 72.3 KB
/
meetingGrid.ur
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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
style table_fixedheader
style meeting_default
style meeting_selected
style meeting_conflict
style meeting_movable
open Bootstrap
open Tooltip
functor Make(M : sig
con homeKey1 :: Name
type homeKeyT
con homeKeyR :: {Type}
constraint [homeKey1] ~ homeKeyR
con homeKey = [homeKey1 = homeKeyT] ++ homeKeyR
con homeOffice :: {Type}
con homeHardConst :: {Type}
con homeSoftConst :: {Type}
con homeRest :: {Type}
constraint homeKey ~ homeRest
constraint (homeKey ++ homeRest) ~ homeOffice
constraint (homeKey ++ homeRest ++ homeOffice) ~ homeSoftConst
constraint (homeKey ++ homeRest ++ homeOffice ++ homeSoftConst) ~ homeHardConst
con homeKeyName :: Name
con homeOtherConstraints :: {{Unit}}
constraint [homeKeyName] ~ homeOtherConstraints
val home : sql_table (homeKey ++ homeOffice ++ homeSoftConst ++ homeHardConst ++ homeRest) ([homeKeyName = map (fn _ => ()) homeKey] ++ homeOtherConstraints)
val homeInj : $(map sql_injectable_prim homeKey)
val homeKeyFl : folder homeKey
val homeKeyShow : show $homeKey
val homeKeyRead : read $homeKey
val homeKeyEq : $(map eq homeKey)
val homeKeyOrd : $(map ord homeKey)
val officeFl : folder homeOffice
val officeRender : $homeKey -> $homeOffice -> xbody
val homeSoftConstFl : folder homeSoftConst
val homeSoftConstInj : $(map sql_injectable homeSoftConst)
val homeSoftConst : $homeSoftConst
val homeHardConstFl : folder homeHardConst
val homeHardConstInj : $(map sql_injectable homeHardConst)
val homeHardConst : $homeHardConst
con awayKey1 :: Name
type awayKeyT
con awayKeyR :: {Type}
constraint [awayKey1] ~ awayKeyR
con awayKey = [awayKey1 = awayKeyT] ++ awayKeyR
con awayConst :: {Type}
con awayFilter :: {Type}
con awayRest :: {Type}
constraint awayKey ~ awayRest
constraint (awayKey ++ awayRest) ~ awayConst
constraint (awayKey ++ awayRest ++ awayConst) ~ awayFilter
con awayKeyName :: Name
con awayOtherConstraints :: {{Unit}}
constraint [awayKeyName] ~ awayOtherConstraints
val away : sql_table (awayKey ++ awayConst ++ awayFilter ++ awayRest) ([awayKeyName = map (fn _ => ()) awayKey] ++ awayOtherConstraints)
val awayInj : $(map sql_injectable_prim awayKey)
val awayKeyFl : folder awayKey
val awayKeyShow : show $awayKey
val awayKeyRead : read $awayKey
val awayKeyEq : $(map eq awayKey)
val awayKeyOrd : $(map ord awayKey)
val awayConstFl : folder awayConst
val awayConstInj : $(map sql_injectable awayConst)
val awayConst : $awayConst
val awayFilterFl : folder awayFilter
con timeKey1 :: Name
type timeKeyT
con timeKeyR :: {Type}
constraint [timeKey1] ~ timeKeyR
con timeKey = [timeKey1 = timeKeyT] ++ timeKeyR
con timeRest :: {Type}
constraint timeKey ~ timeRest
con timeKeyName :: Name
con timeOtherConstraints :: {{Unit}}
constraint [timeKeyName] ~ timeOtherConstraints
val time : sql_table (timeKey ++ timeRest) ([timeKeyName = map (fn _ => ()) timeKey] ++ timeOtherConstraints)
val timeInj : $(map sql_injectable_prim timeKey)
val timeKeyFl : folder timeKey
val timeKeyShow : show $timeKey
val timeKeyRead : read $timeKey
val timeKeyEq : $(map eq timeKey)
constraint homeKey ~ awayKey
constraint (homeKey ++ awayKey) ~ timeKey
constraint homeOffice ~ timeKey
constraint (homeKey ++ awayKey) ~ [ByHome, Channel]
val amHome : transaction (option $homeKey)
val amAway : transaction (option $awayKey)
val fixed : transaction (list {When : $timeKey, Descr : string})
val timeOrd : ord $timeKey
val isNowReadOnly : transaction bool
end) = struct
open M
val homeKeyEq : eq $homeKey = @@Record.eq [homeKey] homeKeyEq homeKeyFl
val awayKeyEq : eq $awayKey = @@Record.eq [awayKey] awayKeyEq awayKeyFl
val timeKeyEq : eq $timeKey = @@Record.eq [timeKey] timeKeyEq timeKeyFl
val homeKeyOrd : ord $homeKey = @@Record.ord [homeKey] homeKeyOrd homeKeyFl
val awayKeyOrd : ord $awayKey = @@Record.ord [awayKey] awayKeyOrd awayKeyFl
table meeting : (homeKey ++ timeKey ++ awayKey)
PRIMARY KEY {{@primary_key [homeKey1] [homeKeyR ++ timeKey ++ awayKey] ! !
(homeInj ++ timeInj ++ awayInj)}},
{{one_constraint [#Home] (@Sql.easy_foreign ! ! ! ! ! ! homeKeyFl home)}},
{{one_constraint [#Away] (@Sql.easy_foreign ! ! ! ! ! ! awayKeyFl away)}},
{{one_constraint [#Time] (@Sql.easy_foreign ! ! ! ! ! ! timeKeyFl time)}}
con all = homeKey ++ awayKey ++ timeKey
val allFl = @Folder.concat ! homeKeyFl (@Folder.concat ! timeKeyFl awayKeyFl)
view meetings = {@Sql.viewOf allFl meeting}
datatype operation = Add | Del
type action = { Operation : operation, Home : $homeKey, Away : $awayKey, Time : $timeKey }
table globalListeners : { Channel : channel action }
type home_action = { Operation : operation, Away : $awayKey, Time : $timeKey, Place : option unit }
table homeListeners : ([Channel = channel home_action] ++ homeKey)
{{one_constraint [#Home] (@Sql.easy_foreign ! ! ! ! ! ! homeKeyFl home)}}
type away_action = { Operation : operation, Home : $homeKey, Time : $timeKey, Place : option $homeOffice }
table awayListeners : ([Channel = channel away_action] ++ awayKey)
{{one_constraint [#Away] (@Sql.easy_foreign ! ! ! ! ! ! awayKeyFl away)}}
table preference : ([ByHome = bool] ++ homeKey ++ awayKey)
PRIMARY KEY {{@primary_key [#ByHome] [homeKey ++ awayKey] ! !
({ByHome = _} ++ homeInj ++ awayInj)}},
{{one_constraint [#Home] (@Sql.easy_foreign ! ! ! ! ! ! homeKeyFl home)}},
{{one_constraint [#Away] (@Sql.easy_foreign ! ! ! ! ! ! awayKeyFl away)}}
table homeUnavailable : (homeKey ++ timeKey)
PRIMARY KEY {{@primary_key [homeKey1] [homeKeyR ++ timeKey] ! !
(homeInj ++ timeInj)}},
{{one_constraint [#Home] (@Sql.easy_foreign ! ! ! ! ! ! homeKeyFl home)}},
{{one_constraint [#Time] (@Sql.easy_foreign ! ! ! ! ! ! timeKeyFl time)}}
table awayUnavailable : (awayKey ++ timeKey)
PRIMARY KEY {{@primary_key [awayKey1] [awayKeyR ++ timeKey] ! !
(awayInj ++ timeInj)}},
{{one_constraint [#Away] (@Sql.easy_foreign ! ! ! ! ! ! awayKeyFl away)}},
{{one_constraint [#Time] (@Sql.easy_foreign ! ! ! ! ! ! timeKeyFl time)}}
val allInj = @mp [sql_injectable_prim] [sql_injectable] @@sql_prim allFl (homeInj ++ awayInj ++ timeInj)
val homeInj' = @mp [sql_injectable_prim] [sql_injectable] @@sql_prim homeKeyFl homeInj
val awayInj' = @mp [sql_injectable_prim] [sql_injectable] @@sql_prim awayKeyFl awayInj
val timeInj' = @mp [sql_injectable_prim] [sql_injectable] @@sql_prim timeKeyFl timeInj
fun addMeeting r =
office <- oneRow1 (SELECT home.{{homeOffice}}
FROM home
WHERE {@@Sql.easy_where [#Home] [homeKey] [_] [_] [_] [_]
! ! homeInj' homeKeyFl (r --- awayKey --- timeKey)});
@@Sql.easy_insert [all] [_] allInj allFl meeting r;
queryI1 (SELECT * FROM globalListeners)
(fn i => send i.Channel {Operation = Add,
Home = r --- awayKey --- timeKey,
Away = r --- homeKey --- timeKey,
Time = r --- awayKey --- homeKey});
queryI1 (SELECT * FROM homeListeners
WHERE {@@Sql.easy_where [#HomeListeners] [homeKey] [_] [_] [_] [_]
! ! homeInj' homeKeyFl (r --- awayKey --- timeKey)})
(fn i => send i.Channel {Operation = Add,
Away = r --- homeKey --- timeKey,
Time = r --- awayKey --- homeKey,
Place = Some ()});
queryI1 (SELECT * FROM awayListeners
WHERE {@@Sql.easy_where [#AwayListeners] [awayKey] [_] [_] [_] [_]
! ! awayInj' awayKeyFl (r --- homeKey --- timeKey)})
(fn i => send i.Channel {Operation = Add,
Home = r --- awayKey --- timeKey,
Time = r --- awayKey --- homeKey,
Place = Some office})
fun delMeeting r =
dml (DELETE FROM meeting
WHERE {@@Sql.easy_where [#T] [all] [_] [_] [_] [_]
! ! allInj allFl r});
queryI1 (SELECT * FROM globalListeners)
(fn i => send i.Channel {Operation = Del,
Home = r --- awayKey --- timeKey,
Away = r --- homeKey --- timeKey,
Time = r --- awayKey --- homeKey});
queryI1 (SELECT * FROM homeListeners
WHERE {@@Sql.easy_where [#HomeListeners] [homeKey] [_] [_] [_] [_]
! ! homeInj' homeKeyFl (r --- awayKey --- timeKey)})
(fn i => send i.Channel {Operation = Del,
Away = r --- homeKey --- timeKey,
Time = r --- awayKey --- homeKey,
Place = None});
queryI1 (SELECT * FROM awayListeners
WHERE {@@Sql.easy_where [#AwayListeners] [awayKey] [_] [_] [_] [_]
! ! awayInj' awayKeyFl (r --- homeKey --- timeKey)})
(fn i => send i.Channel {Operation = Del,
Home = r --- awayKey --- timeKey,
Time = r --- awayKey --- homeKey,
Place = None})
(* This functor helps us abstract over the two directions.
* We want symmetric functionality for each. *)
functor Side(N : sig
con usKey :: {Type}
con usOffice :: {Type}
con usSoftConst :: {Type}
con usHardConst :: {Type}
con usOther :: {Type}
con themKey :: {Type}
con themOffice :: {Type}
con themSoftConst :: {Type}
con themHardConst :: {Type}
con themOther :: {Type}
(* Note: the "office" fields here do double-duty as "filter" fields in the eventual invocation! *)
constraint usKey ~ usOther
constraint (usKey ++ usOther) ~ usOffice
constraint (usKey ++ usOther ++ usOffice) ~ usSoftConst
constraint (usKey ++ usOther ++ usOffice ++ usSoftConst) ~ usHardConst
constraint themKey ~ themOther
constraint (themKey ++ themOther) ~ themOffice
constraint (themKey ++ themOther ++ themOffice) ~ themSoftConst
constraint (themKey ++ themOther ++ themOffice ++ themSoftConst) ~ themHardConst
constraint usKey ~ themKey
constraint (usKey ++ themKey) ~ timeKey
constraint themOffice ~ timeKey
constraint (usKey ++ themKey) ~ [ByHome]
val localized : { Home : $homeKey, Away : $awayKey }
-> { Us : $usKey, Them : $themKey }
val canonical : { Us : $usKey, Them : $themKey }
-> { Home : $homeKey, Away : $awayKey }
table us : (usKey ++ usOffice ++ usSoftConst ++ usHardConst ++ usOther)
table them : (themKey ++ themOffice ++ themSoftConst ++ themHardConst ++ themOther)
table preference : ([ByHome = bool] ++ usKey ++ themKey)
table unavailable : (usKey ++ timeKey)
table unavailableThem : (themKey ++ timeKey)
table meeting : (usKey ++ themKey ++ timeKey)
constraint usKey ~ [Channel]
type usChannel
val usChannel : usChannel
-> { Operation : operation,
Them : $themKey,
Time : $timeKey,
Place : option $themOffice }
table usListeners : ([Channel = channel usChannel] ++ usKey)
val usFl : folder usKey
val themFl : folder themKey
val usInj : $(map sql_injectable_prim usKey)
val themInj : $(map sql_injectable_prim themKey)
val usShow : show $usKey
val themShow : show $themKey
val themRead : read $themKey
val usEq : eq $usKey
val usOrd : ord $usKey
val themEq : eq $themKey
val usOfficeFl : folder usOffice
val usOfficeRender : $usKey -> $usOffice -> xbody
val themOfficeRender : $themKey -> $themOffice -> xbody
val usSoftConstFl : folder usSoftConst
val usSoftConstInj : $(map sql_injectable usSoftConst)
val usSoftConst : $usSoftConst
val usHardConstFl : folder usHardConst
val usHardConstInj : $(map sql_injectable usHardConst)
val usHardConst : $usHardConst
val themSoftConstFl : folder themSoftConst
val themSoftConstInj : $(map sql_injectable themSoftConst)
val themSoftConst : $themSoftConst
val themHardConstFl : folder themHardConst
val themHardConstInj : $(map sql_injectable themHardConst)
val themHardConst : $themHardConst
val byHome : bool
val amUs : transaction (option $usKey)
end) : sig
(* Display a full, editable grid of all meetings (rows: aways, columns: times).
* The signal function can be used to control which rows are displayed. *)
structure FullGrid : Ui.S where type input = $(N.usKey ++ N.usOffice) -> signal bool
(* Display a read-only record of all records for an away. *)
structure One : Ui.S where type input = $N.usKey
(* Inputing meeting preferences for an away *)
structure Prefs : Ui.S where type input = $N.usKey
(* Inputing schedule constraints for an away *)
structure Unavail : Ui.S where type input = $N.usKey
(* Delete all of this person's meetings. *)
val deleteFor : $N.usKey -> transaction unit
(* Display who asked to meet with this person. *)
structure WhoAskedFor : Ui.S where type input = $N.usKey
end = struct
open N
con all = usKey ++ themKey ++ timeKey
val allFl = @Folder.concat ! usFl (@Folder.concat ! timeKeyFl themFl)
val allInj = @mp [sql_injectable_prim] [sql_injectable] @@sql_prim allFl (usInj ++ themInj ++ timeInj)
val usInj' = @mp [sql_injectable_prim] [sql_injectable] @@sql_prim usFl usInj
val themInj' = @mp [sql_injectable_prim] [sql_injectable] @@sql_prim themFl themInj
fun canonicalized r =
let
val r' = canonical {Us = r --- timeKey --- themKey,
Them = r --- timeKey --- usKey}
in
r --- usKey --- themKey ++ r'.Home ++ r'.Away
end
val addMeeting r = addMeeting (canonicalized r)
val delMeeting r = delMeeting (canonicalized r)
structure FullGrid = struct
type themSet = list ($themKey * int (* number of meetings this them has at this time *))
type timeMap = list ($timeKey * bool (* available? *) * themSet)
type usMap = list ($usKey * $usOffice * timeMap * list $themKey (* requested but not scheduled yet *))
type themMap = list ($themKey * list $timeKey (* unavailable times *))
type input = _
type a = _
val create =
allTimes <- queryL1 (SELECT time.{{timeKey}}
FROM time
ORDER BY {{{@Sql.order_by timeKeyFl
(@Sql.some_fields [#Time] [timeKey] ! ! timeKeyFl)
sql_asc}}});
let
(* A bit of a little dance to initialize the meeting states,
* including blank entries for unused us/time pairs *)
fun initMap (acc : usMap)
(rows : list {Meeting : $all, ThisTime : option int})
(uses : list $(usKey ++ usOffice))
(times : list $timeKey)
(unavails : list $(usKey ++ timeKey))
(themsDone : themSet)
(timesDone : timeMap)
: usMap =
case uses of
[] =>
(* Finished with all uses. Done! *)
List.rev acc
| us :: uses' =>
case times of
[] =>
(* Finished with one us. Move to next. *)
initMap ((us --- usOffice, us --- usKey, List.rev timesDone, []) :: acc)
rows
uses'
allTimes
unavails
[]
[]
| time :: times' =>
(* Check if the next row is about this time. *)
case rows of
[] =>
(* Nope. *)
let
val (available, unavails') =
case unavails of
[] => (True, [])
| r :: unavails' =>
if r --- timeKey = us --- usOffice
&& @eq timeKeyEq (r --- usKey) time then
(False, unavails')
else
(True, unavails)
in
initMap acc
[]
uses
times'
unavails'
[]
((time, available, List.rev themsDone) :: timesDone)
end
| {Meeting = row, ThisTime = tt} :: rows' =>
if row --- themKey --- timeKey < us --- usOffice then
(* We've encountered a meeting for an invalid us. Skip it! *)
initMap acc rows' uses times unavails themsDone timesDone
else if row --- themKey --- timeKey = us --- usOffice
&& @eq timeKeyEq (row --- usKey --- themKey) time then
(* Aha, a match! Record this meeting. *)
initMap acc
rows'
uses
times
unavails
((row --- usKey --- timeKey, Option.get 1 tt) :: themsDone)
timesDone
else
(* No match. On to next time. *)
let
val (available, unavails') =
case unavails of
[] => (True, [])
| r :: unavails' =>
if r --- timeKey = us --- usOffice && @eq timeKeyEq (r --- usKey) time then
(False, unavails')
else
(True, unavails)
in
initMap acc
rows
uses
times'
unavails'
[]
((time, available, List.rev themsDone) :: timesDone)
end
fun mergeThemsWithUnavails (thems : list $themKey) (unavails : list $(themKey ++ timeKey)) (times : list $timeKey) (acc : themMap) : themMap =
case thems of
[] => acc
| them :: thems' =>
case unavails of
[] => List.revAppend (List.mp (fn them' => (them', [])) thems')
((them, times) :: acc)
| r :: unavails' =>
let
val them' = r --- timeKey
val tm = r --- themKey
in
if them' = them then
mergeThemsWithUnavails thems unavails' (tm :: times) acc
else
mergeThemsWithUnavails thems' unavails [] ((them, times) :: acc)
end
in
uses <- queryL1 (SELECT us.{{usKey}}, us.{{usOffice}}
FROM us
WHERE {@@Sql.easy_where [#Us] [usHardConst ++ usSoftConst]
[usKey ++ usOffice ++ usOther] [_] [_] [_]
! ! (usHardConstInj ++ usSoftConstInj)
(@Folder.concat ! usHardConstFl usSoftConstFl)
(usHardConst ++ usSoftConst)}
ORDER BY {{{@Sql.order_by usFl
(@Sql.some_fields [#Us] [usKey] ! ! usFl)
sql_asc}}});
thems <- queryL1 (SELECT them.{{themKey}}
FROM them
WHERE {@@Sql.easy_where [#Them] [themHardConst ++ themSoftConst]
[themKey ++ themOffice ++ themOther] [_] [_] [_]
! ! (themHardConstInj ++ themSoftConstInj)
(@Folder.concat ! themHardConstFl themSoftConstFl)
(themHardConst ++ themSoftConst)}
ORDER BY {{{@Sql.order_by themFl
(@Sql.some_fields [#Them] [themKey] ! ! themFl)
sql_desc}}});
unavailsThem <- queryL1 (SELECT unavailableThem.{{themKey}}, unavailableThem.{{timeKey}}
FROM unavailableThem
ORDER BY {{{@Sql.order_by (@Folder.concat ! themFl timeKeyFl)
(@Sql.some_fields [#UnavailableThem] [themKey ++ timeKey] ! !
(@Folder.concat ! themFl timeKeyFl))
sql_desc}}});
unavails <- queryL1 (SELECT unavailable.{{usKey}}, unavailable.{{timeKey}}
FROM unavailable JOIN us
ON {@@Sql.easy_join [#Unavailable] [#Us]
[usKey] [timeKey] [usOffice ++ usSoftConst ++ usHardConst ++ usOther]
[_] [_] [_] ! ! ! ! usFl}
WHERE {@@Sql.easy_where [#Us] [usHardConst ++ usSoftConst]
[usKey ++ usOffice ++ usOther] [_] [_] [_]
! ! (usHardConstInj ++ usSoftConstInj)
(@Folder.concat ! usHardConstFl usSoftConstFl)
(usHardConst ++ usSoftConst)}
ORDER BY {{{@Sql.order_by (@Folder.concat ! usFl timeKeyFl)
(@Sql.some_fields [#Unavailable] [usKey ++ timeKey] ! !
(@Folder.concat ! usFl timeKeyFl))
sql_asc}}});
meetings <- queryL (SELECT meeting.{{usKey}}, meeting.{{timeKey}}, meeting.{{themKey}},
(SELECT COUNT( * )
FROM meeting AS ByThem
WHERE {@@Sql.easy_join [#ByThem] [#Meeting]
[themKey] [usKey ++ timeKey] [usKey ++ timeKey]
[_] [_] [_] ! ! ! ! themFl}
AND {@@Sql.easy_join [#ByThem] [#Meeting]
[timeKey] [usKey ++ themKey] [usKey ++ themKey]
[_] [_] [_] ! ! ! ! timeKeyFl}) AS ThisTime
FROM meeting
ORDER BY {{{@Sql.order_by allFl
(@Sql.some_fields [#Meeting] [all] ! ! allFl)
sql_asc}}});
meetings <- List.mapM (fn (us, off, tms, _) =>
tms' <- List.mapM (fn (tm, avail, ths) =>
ths <- source ths;
return (tm, avail, ths)) tms;
requested <- queryL1 (SELECT preference.{{themKey}}
FROM preference
WHERE {@@Sql.easy_where [#Preference] [usKey] [_] [_] [_] [_]
! ! usInj' usFl us}
AND preference.ByHome = {[byHome]}
ORDER BY {{{@Sql.order_by themFl
(@Sql.some_fields [#Preference] [themKey] ! ! themFl)
sql_asc}}});
return (us, off, tms', requested))
(initMap []
meetings
uses
allTimes
unavails
[]
[]);
thems <- return (mergeThemsWithUnavails thems unavailsThem [] []);
chan <- channel;
dml (INSERT INTO globalListeners (Channel) VALUES ({[chan]}));
mf <- source None;
mt <- source None;
ro <- isNowReadOnly;
return {Thems = thems, Times = allTimes, Meetings = meetings,
Channel = chan, MovingFrom = mf, MovingTo = mt,
ReadOnly = ro}
end
val ensureUser =
user <- amUs;
case user of
None => error <xml>Must be authenticated to access this page</xml>
| Some us' => return ()
val ensureWritable =
ro <- isNowReadOnly;
if ro then
error <xml>Changes to the schedule have been disabled</xml>
else
return ()
fun schedule r =
ensureUser;
ensureWritable;
alreadyScheduled <- oneRowE1 (SELECT COUNT( * ) > 0
FROM meeting
WHERE {@@Sql.easy_where [#Meeting] [all] [_] [_] [_] [_]
! ! allInj allFl r});
if alreadyScheduled then
return ()
else
addMeeting r
fun unschedule r =
ensureUser;
ensureWritable;
alreadyScheduled <- oneRowE1 (SELECT COUNT( * ) > 0
FROM meeting
WHERE {@@Sql.easy_where [#Meeting] [all] [_] [_] [_] [_]
! ! allInj allFl r});
if not alreadyScheduled then
return ()
else
delMeeting r
fun reschedule (r : {Them : $themKey, OldUs : $usKey, OldTime : $timeKey, NewUs : $usKey, NewTime : $timeKey}) =
ensureUser;
ensureWritable;
alreadyScheduled <- oneRowE1 (SELECT COUNT( * ) > 0
FROM meeting
WHERE {@@Sql.easy_where [#Meeting] [all] [_] [_] [_] [_]
! ! allInj allFl (r.Them ++ r.OldUs ++ r.OldTime)});
spotUsed <- oneRowE1 (SELECT COUNT( * ) > 0
FROM meeting
WHERE {@@Sql.easy_where [#Meeting] [all] [_] [_] [_] [_]
! ! allInj allFl (r.Them ++ r.NewUs ++ r.NewTime)});
if not alreadyScheduled || spotUsed then
return ()
else
delMeeting (r.Them ++ r.OldUs ++ r.OldTime);
addMeeting (r.Them ++ r.NewUs ++ r.NewTime)
fun render filt ctx t =
let
val colwidth = show (100 / (1 + List.length t.Times)) ^ "%"
val colwidth = oneProperty noStyle (value (property "width") (atom colwidth))
in
<xml>
<div class="table-fixedheader"><table class="bs-table table-fixedheader">
<thead>
<tr>
<th style={colwidth}> </th>
(* Header: one column per time *)
{List.mapX (fn tm => <xml><th style={colwidth}>{[tm]}</th></xml>) t.Times}
</tr>
</thead>
<tbody style="height: 500px">
(* One row per us *)
{List.mapX (fn (us, off, tms, requested) => <xml>
<tr dynStyle={b <- filt (us ++ off); return (if b then STYLE "" else STYLE "display: none")}>
<td style={colwidth}><strong>{usOfficeRender us off}</strong>
<dyn signal={reqs <- List.filterM (fn th => b <- List.existsM (fn (_, _, ths) =>
thsV <- signal ths;
return (List.exists (fn (th', _) => th' = th) thsV)) tms;
return (not b)) requested;
return (case reqs of
[] => <xml></xml>
| req1 :: reqs => <xml><span class="tooltip glyphicon glyphicon-question-circle"><span class="tooltiptext"><i>Requested but unscheduled:</i> {[req1]}{List.mapX (fn v => <xml>, {[v]}</xml>) reqs}</span></span></xml>)}/>
</td>
(* One column per time *)
{List.mapX (fn (tm, avail, ths) => <xml>
<td dynClass={mf <- signal t.MovingFrom;
cls <- (case mf of
None => return (CLASS "")
| Some _ =>
mt <- signal t.MovingTo;
return (case mt of
None => CLASS ""
| Some mt =>
if mt.Us = us && mt.Time = tm then
CLASS "bs-active"
else
CLASS ""));
return (if avail then cls else classes cls bg_danger)}
onmouseover={fn _ =>
if t.ReadOnly then
return ()
else
set t.MovingTo (Some {Us = us, Time = tm})}
onclick={fn _ =>
if t.ReadOnly then
return ()
else
mf <- get t.MovingFrom;
case mf of
None => return ()
| Some mf =>
mt <- get t.MovingTo;
case mt of
None => return ()
| Some mt =>
set t.MovingFrom None;
rpc (reschedule {Them = mf.Them,
OldUs = mf.Us,
OldTime = mf.Time,
NewUs = mt.Us,
NewTime = mt.Time})}
style={colwidth}>
<active code={selected <- source "";
deleting <- source False;
return <xml>
<dyn signal={thsv <- signal ths;
(* One button per meeting *)
return <xml>
{List.mapX (fn (th, tt) => <xml>
<div dynClass={case List.find (fn (th', _) => th' = th) t.Thems of
None => return (if t.ReadOnly then meeting_default else classes meeting_default meeting_movable)
| Some (_, unavails) =>
mf <- signal t.MovingFrom;
default <- return (if avail && not (List.mem tm unavails) then
case thsv of
_ :: _ :: _ => if t.ReadOnly then meeting_conflict else classes meeting_conflict meeting_movable
| _ => if tt > 1 then meeting_conflict else if t.ReadOnly then meeting_default else classes meeting_default meeting_movable
else
if t.ReadOnly then meeting_conflict else classes meeting_conflict meeting_movable);
return (case mf of
None => default
| Some mf =>
if mf.Us = us
&& mf.Them = th
&& mf.Time = tm then
classes meeting_selected tooltip
else
default)}
onclick={fn _ =>
if t.ReadOnly then
return ()
else
del <- get deleting;
if del then
set deleting False
else
stopPropagation;
set t.MovingFrom
(Some {Us = us,
Them = th,
Time = tm})}>
{case List.find (fn (th', _) => th' = th) t.Thems of
None => <xml></xml>
| Some (_, unavails) =>
if not avail then
<xml><span class="tooltip glyphicon glyphicon-question-circle"><span class="tooltiptext"><i>Conflict:</i> {[us]} marked this time as <b>unavailable.</b></span></span></xml>
else if List.mem tm unavails then
<xml><span class="tooltip glyphicon glyphicon-question-circle"><span class="tooltiptext"><i>Conflict:</i> {[th]} marked this time as <b>unavailable.</b></span></span></xml>
else
case thsv of
_ :: _ :: _ => <xml><span class="tooltip glyphicon glyphicon-question-circle"><span class="tooltiptext"><i>Conflict:</i> there are <b>multiple</b> meetings in this cell, indicating a double booking.</span></span></xml>
| _ => if tt > 1 then <xml><span class="tooltip glyphicon glyphicon-question-circle"><span class="tooltiptext"><i>Conflict:</i> {[th]} has <b>another meeting</b> scheduled at this time.</span></span></xml> else <xml></xml>}
{[th]}
{if t.ReadOnly then
<xml></xml>
else
Ui.modalButton ctx (CLASS "close")
<xml>×</xml>
(set deleting True;
return (Ui.modal
(rpc (unschedule (th ++ us ++ tm)))
<xml>Are you sure you want to delete the {[tm]} meeting between {[us]} and {[th]}?</xml>
<xml/>
<xml>Yes!</xml>))}
</div>
</xml>) thsv}
{if t.ReadOnly then
<xml></xml>
else
Ui.modalButton ctx (CLASS "btn btn-default btn-xs")
<xml>+</xml>
(return (Ui.modal
(sel <- get selected;
th <- return (readError sel : $themKey);
rpc (schedule (th ++ us ++ tm)))
<xml>Adding meeting for {[us]} at {[tm]}</xml>
<xml>
<cselect class="form-control"
source={selected}>
{List.mapX (fn (th, _) =>
if List.exists (fn (th', _) => th' = th) thsv then
<xml/>
else
<xml><coption>{[th]}</coption></xml>) t.Thems}
</cselect>
</xml>
<xml>Add Meeting</xml>))}
</xml>}/>
</xml>}/>
</td>
</xml>) tms}
</tr>
</xml>) t.Meetings}
</tbody>
</table></div>
</xml>
end
fun tweakMeeting (f : themSet -> themSet) (g : int -> int) (us : $usKey) (th : $themKey) (tm : $timeKey) =
List.app (fn (us', _, tms, _) =>
(if us' = us then
let
fun addTimes tms =
case tms of
[] => return ()
| (tm', _, ths) :: tms' =>
if tm' = tm then
v <- get ths;
set ths (f v)
else
addTimes tms'
in
addTimes tms
end
else
return ());
List.app (fn (tm', _, ths) =>
if tm = tm' then
thsv <- get ths;
set ths (List.mp (fn (th', count) =>
(th', if th' = th then
g count
else
count)) thsv)
else
return ()) tms)
fun onload t =
let
fun loop () =
r <- recv t.Channel;
let
val r' = localized (r -- #Operation -- #Time)
fun findExistingMeeting uses =
case uses of
[] => return 0
| (_, _, tms, _) :: uses' =>
let
fun fem tms =
case tms of
[] => findExistingMeeting uses'
| (tm, _, ths) :: tms' =>
if tm <> r.Time then
fem tms'
else
thsv <- get ths;
case List.find (fn (th, _) => th = r'.Them) thsv of
None => fem tms'
| Some (_, count) => return count
in
fem tms
end
in
case r.Operation of
Add =>
count <- findExistingMeeting t.Meetings;
tweakMeeting
(fn ls => List.sort (fn (x, _) (y, _) => show x > show y)
((r'.Them, count) :: ls))
(fn n => n + 1)
r'.Us r'.Them r.Time t.Meetings
| Del => tweakMeeting
(List.filter (fn (th, _) => th <> r'.Them))
(fn n => n - 1)
r'.Us r'.Them r.Time t.Meetings
end;
loop ()
in
spawn (loop ())
end
fun notification _ _ = <xml></xml>
fun buttons _ _ = <xml></xml>
fun ui filt = {
Create = create,
Onload = onload,
Render = render filt,
Notification = notification,
Buttons = buttons
}
end
structure One = struct
type themSet = list ($themKey * $themOffice)
type timeMap = list ($timeKey * themSet)
type input = $usKey
datatype timeslot =
Meeting of source themSet
| Fixed of string
type a = {Us : $usKey,
Meetings : list ($timeKey * timeslot),
Channel : channel usChannel}
fun create us =
let
fun doTimes (times : list $timeKey)
(rows : list {Meeting : $(timeKey ++ themKey), Them : $themOffice})
(thisTime : themSet)
(acc : timeMap)
: timeMap =
case times of
[] => List.rev acc
| tm :: times' =>
case rows of
[] => doTimes times' [] [] ((tm, List.rev thisTime) :: acc)
| row :: rows' =>
if row.Meeting --- themKey = tm then
doTimes times rows' ((row.Meeting --- timeKey, row.Them) :: thisTime) acc
else
doTimes times' rows [] ((tm, List.rev thisTime) :: acc)
fun merge (ls1 : list ($timeKey * source themSet)) (ls2 : list {When : $timeKey, Descr : string})
: list ($timeKey * timeslot) =
case (ls1, ls2) of
((tm1, s) :: ls1', {When = tm2, Descr = d} :: ls2') =>
if tm1 < tm2 then
(tm1, Meeting s) :: merge ls1' ls2
else
(tm2, Fixed d) :: merge ls1 ls2'
| ([], _) => List.mp (fn {When = tm, Descr = d} => (tm, Fixed d)) ls2
| (_, []) => List.mp (fn (tm, s) => (tm, Meeting s)) ls1
in
allTimes <- queryL1 (SELECT time.{{timeKey}}
FROM time
ORDER BY {{{@Sql.order_by timeKeyFl
(@Sql.some_fields [#Time] [timeKey] ! ! timeKeyFl)
sql_asc}}});
meetings <- queryL (SELECT meeting.{{timeKey}}, meeting.{{themKey}}, them.{{themOffice}}
FROM meeting
JOIN them ON {@@Sql.easy_join [#Meeting] [#Them] [themKey]
[usKey ++ timeKey]
[themOffice ++ themHardConst ++ themSoftConst ++ themOther]
[_] [_] [_]
! ! ! ! themFl}
WHERE {@@Sql.easy_where [#Meeting] [usKey] [_] [_] [_] [_]
! ! usInj' usFl us}
AND {@@Sql.easy_where [#Them] [themHardConst ++ themSoftConst]
[themKey ++ themOffice ++ themOther] [_] [_] [_]
! ! (themHardConstInj ++ themSoftConstInj)
(@Folder.concat ! themHardConstFl themSoftConstFl)
(themHardConst ++ themSoftConst)}
ORDER BY {{{@Sql.order_by (@Folder.concat ! timeKeyFl themFl)
(@Sql.some_fields [#Meeting] [timeKey ++ themKey] ! !
(@Folder.concat ! timeKeyFl themFl))
sql_asc}}});
meetings <- List.mapM (fn (tm, ths) =>
ths <- source ths;
return (tm, ths))
(doTimes allTimes meetings [] []);
ch <- channel;
@@Sql.easy_insert [[Channel = _] ++ usKey] [_]
({Channel = _} ++ usInj')
(@Folder.cons [#Channel] [_] ! usFl)
usListeners
({Channel = ch} ++ us);
fxd <- fixed;
return {Us = us, Meetings = merge meetings (List.sort (fn a b => a.When > b.When) fxd), Channel = ch}
end
(* Note: with a new improvement to Ur/Web type inference, this annotation becomes necessary.
* Should probably look further into why, some day. *)
fun render (t : a) = <xml>
<table class="bs-table">
<thead><tr>
<th>Time</th>
<th>Meeting</th>
</tr></thead>
<tbody>
{List.mapX (fn (tm, slot) => <xml>
<tr>
<td>{[tm]}</td>
<td>
<dyn signal={case slot of
Fixed d => return <xml><i>{[d]}</i></xml>
| Meeting ths =>
ths <- signal ths;
return (case ths of
[] => <xml>—</xml>
| (th, off) :: ths => <xml>{themOfficeRender th off}{List.mapX (fn (th, off) => <xml>, {themOfficeRender th off}</xml>) ths}</xml>)}/>
</td>
</tr>
</xml>) t.Meetings}
</tbody>
</table>
</xml>
fun tweakMeeting (f : themSet -> themSet) (tm : $timeKey) =
let
fun addTimes tms =
case tms of
[] => return ()
| (_, Fixed _) :: tms' => addTimes tms'
| (tm', Meeting ths) :: tms' =>
if tm' = tm then
v <- get ths;
set ths (f v)
else
addTimes tms'
in
addTimes
end
fun onload t =
let
fun loop () =
r <- recv t.Channel;
let
val r = usChannel r
in
case r.Operation of
Add =>
(case r.Place of
None => error <xml>One.onload: missing place</xml>
| Some off =>
tweakMeeting
(fn ls => List.sort (fn (x, _) (y, _) => show x > show y) ((r.Them, off) :: ls))
r.Time t.Meetings)
| Del => tweakMeeting
(List.filter (fn (th, _) => th <> r.Them))
r.Time t.Meetings
end;
loop ()
in
spawn (loop ())
end
fun notification _ _ = <xml></xml>
fun buttons _ _ = <xml></xml>
fun ui x = {
Create = create x,
Onload = onload,
Render = fn _ => render,
Notification = notification,
Buttons = buttons
}