adamc@1004
|
1 open Meta
|
adamc@1001
|
2
|
adamc@1001
|
3 functor Make(M : sig
|
adamc@1003
|
4 con paper :: {(Type * Type)}
|
adamc@1010
|
5 constraint [Id, Document, Authors] ~ paper
|
adamc@1003
|
6 val paper : $(map meta paper)
|
adamc@1007
|
7 val paperFolder : folder paper
|
adamc@1003
|
8
|
adamc@1001
|
9 con review :: {(Type * Type)}
|
adamc@1003
|
10 constraint [Paper, User] ~ review
|
adamc@1003
|
11 val review : $(map meta review)
|
adamc@1011
|
12 val reviewFolder : folder review
|
adamc@1006
|
13
|
adamc@1006
|
14 val submissionDeadline : time
|
adamc@1009
|
15 val summarizePaper : $(map fst paper) -> xbody
|
adamc@1001
|
16 end) = struct
|
adamc@1001
|
17
|
adamc@1003
|
18 table user : {Id : int, Nam : string, Password : string, Chair : bool, OnPc : bool}
|
adamc@1003
|
19 PRIMARY KEY Id,
|
adamc@1003
|
20 CONSTRAINT Nam UNIQUE Nam
|
adamc@1003
|
21 sequence userId
|
adamc@1003
|
22
|
adamc@1008
|
23 con paper = [Id = int, Document = blob] ++ map fst M.paper
|
adamc@1003
|
24 table paper : paper
|
adamc@1003
|
25 PRIMARY KEY Id
|
adamc@1003
|
26 sequence paperId
|
adamc@1003
|
27
|
adamc@1010
|
28 table authorship : {Paper : int, User : int}
|
adamc@1010
|
29 PRIMARY KEY (Paper, User),
|
adamc@1011
|
30 CONSTRAINT Paper FOREIGN KEY Paper REFERENCES paper(Id) ON DELETE CASCADE,
|
adamc@1010
|
31 CONSTRAINT User FOREIGN KEY User REFERENCES user(Id)
|
adamc@1010
|
32
|
adamc@1003
|
33 con review = [Paper = int, User = int] ++ map fst M.review
|
adamc@1003
|
34 table review : review
|
adamc@1003
|
35 PRIMARY KEY (Paper, User),
|
adamc@1003
|
36 CONSTRAINT Paper FOREIGN KEY Paper REFERENCES paper(Id),
|
adamc@1003
|
37 CONSTRAINT User FOREIGN KEY User REFERENCES user(Id)
|
adamc@1003
|
38 sequence reviewId
|
adamc@1003
|
39
|
adamc@1003
|
40 cookie login : {Id : int, Password : string}
|
adamc@1003
|
41
|
adamc@1004
|
42 val checkLogin =
|
adamc@1003
|
43 r <- getCookie login;
|
adamc@1003
|
44 case r of
|
adamc@1003
|
45 None => return None
|
adamc@1003
|
46 | Some r =>
|
adamc@1003
|
47 oneOrNoRows1 (SELECT user.Id, user.Nam, user.Chair, user.OnPc
|
adamc@1003
|
48 FROM user
|
adamc@1003
|
49 WHERE user.Id = {[r.Id]}
|
adamc@1003
|
50 AND user.Password = {[r.Password]})
|
adamc@1003
|
51
|
adamc@1010
|
52 val getLogin =
|
adamc@1010
|
53 ro <- checkLogin;
|
adamc@1010
|
54 case ro of
|
adamc@1010
|
55 None => error <xml>You must be logged in to do that.</xml>
|
adamc@1010
|
56 | Some r => return r
|
adamc@1010
|
57
|
adamc@1009
|
58 fun checkPaper id =
|
adamc@1010
|
59 r <- getLogin;
|
adamc@1010
|
60 if r.OnPc then
|
adamc@1009
|
61 return ()
|
adamc@1009
|
62 else
|
adamc@1010
|
63 error <xml>You aren't authorized to see that paper.</xml>
|
adamc@1009
|
64
|
adamc@1004
|
65 structure Users = BulkEdit.Make(struct
|
adamc@1004
|
66 con keyName = #Id
|
adamc@1004
|
67 val visible = {Nam = string "Name",
|
adamc@1004
|
68 Chair = bool "Chair?",
|
adamc@1004
|
69 OnPc = bool "On PC?"}
|
adamc@1004
|
70
|
adamc@1004
|
71 val title = "Users"
|
adamc@1004
|
72 val isAllowed =
|
adamc@1004
|
73 me <- checkLogin;
|
adamc@1004
|
74 return (Option.isSome me)
|
adamc@1004
|
75
|
adamc@1004
|
76 val t = user
|
adamc@1004
|
77 end)
|
adamc@1004
|
78
|
adamc@1010
|
79 datatype dnat = O | S of source dnat
|
adamc@1010
|
80 type dnatS = source dnat
|
adamc@1010
|
81
|
adamc@1010
|
82 fun inc n =
|
adamc@1010
|
83 v <- get n;
|
adamc@1010
|
84 case v of
|
adamc@1010
|
85 O =>
|
adamc@1010
|
86 n' <- source O;
|
adamc@1010
|
87 set n (S n')
|
adamc@1010
|
88 | S n => inc n
|
adamc@1010
|
89
|
adamc@1010
|
90 fun dec n =
|
adamc@1010
|
91 let
|
adamc@1010
|
92 fun dec' last n =
|
adamc@1010
|
93 v <- get n;
|
adamc@1010
|
94 case v of
|
adamc@1010
|
95 O => (case last of
|
adamc@1010
|
96 None => return ()
|
adamc@1010
|
97 | Some n' => set n' O)
|
adamc@1010
|
98 | S n' => dec' (Some n) n'
|
adamc@1010
|
99 in
|
adamc@1010
|
100 dec' None n
|
adamc@1010
|
101 end
|
adamc@1004
|
102
|
adamc@1003
|
103 fun doRegister r =
|
adamc@1003
|
104 n <- oneRowE1 (SELECT COUNT( * ) AS N
|
adamc@1003
|
105 FROM user
|
adamc@1003
|
106 WHERE user.Nam = {[r.Nam]});
|
adamc@1003
|
107 if n > 0 then
|
adamc@1003
|
108 register (Some "Sorry; that username is taken.")
|
adamc@1003
|
109 else
|
adamc@1003
|
110 id <- nextval userId;
|
adamc@1003
|
111 dml (INSERT INTO user (Id, Nam, Password, Chair, OnPc)
|
adamc@1003
|
112 VALUES ({[id]}, {[r.Nam]}, {[r.Password]}, FALSE, FALSE));
|
adamc@1003
|
113 setCookie login {Id = id, Password = r.Password};
|
adamc@1003
|
114 main ()
|
adamc@1003
|
115
|
adamc@1003
|
116 and register msg = return <xml><body>
|
adamc@1003
|
117 <h1>Registering a New Account</h1>
|
adamc@1003
|
118
|
adamc@1003
|
119 {case msg of
|
adamc@1003
|
120 None => <xml/>
|
adamc@1003
|
121 | Some msg => <xml><div>{[msg]}</div></xml>}
|
adamc@1003
|
122
|
adamc@1003
|
123 <form><table>
|
adamc@1003
|
124 <tr> <th>Username:</th> <td><textbox{#Nam}/></td> </tr>
|
adamc@1003
|
125 <tr> <th>Password:</th> <td><password{#Password}/></td> </tr>
|
adamc@1003
|
126 <tr> <th><submit action={doRegister}/></th> </tr>
|
adamc@1003
|
127 </table></form>
|
adamc@1003
|
128 </body></xml>
|
adamc@1003
|
129
|
adamc@1006
|
130 and signin r =
|
adamc@1006
|
131 ro <- oneOrNoRowsE1 (SELECT user.Id AS N
|
adamc@1006
|
132 FROM user
|
adamc@1006
|
133 WHERE user.Nam = {[r.Nam]}
|
adamc@1006
|
134 AND user.Password = {[r.Password]});
|
adamc@1006
|
135 (case ro of
|
adamc@1006
|
136 None => return ()
|
adamc@1006
|
137 | Some id => setCookie login {Id = id, Password = r.Password});
|
adamc@1006
|
138 m <- main' ();
|
adamc@1006
|
139 return <xml><body>
|
adamc@1006
|
140 {case ro of
|
adamc@1006
|
141 None => <xml><div>Invalid username or password.</div></xml>
|
adamc@1006
|
142 | _ => <xml/>}
|
adamc@1006
|
143
|
adamc@1006
|
144 {m}
|
adamc@1006
|
145 </body></xml>
|
adamc@1006
|
146
|
adamc@1006
|
147 and main' () =
|
adamc@1004
|
148 me <- checkLogin;
|
adamc@1006
|
149 now <- now;
|
adamc@1006
|
150 return <xml><ul>
|
adamc@1003
|
151 {case me of
|
adamc@1006
|
152 None => <xml>
|
adamc@1006
|
153 <li><a link={register None}>Register for access</a></li>
|
adamc@1006
|
154 <li><b>Log in:</b> <form><table>
|
adamc@1006
|
155 <tr> <th>Username:</th> <td><textbox{#Nam}/></td> </tr>
|
adamc@1006
|
156 <tr> <th>Password:</th> <td><password{#Password}/></td> </tr>
|
adamc@1006
|
157 <tr> <th><submit value="Log in" action={signin}/></th> </tr>
|
adamc@1006
|
158 </table></form></li>
|
adamc@1006
|
159 </xml>
|
adamc@1004
|
160 | Some me => <xml>
|
adamc@1004
|
161 <div>Welcome, {[me.Nam]}!</div>
|
adamc@1004
|
162
|
adamc@1004
|
163 {if me.Chair then
|
adamc@1004
|
164 <xml><li><a link={Users.main ()}>Manage users</a></li></xml>
|
adamc@1004
|
165 else
|
adamc@1004
|
166 <xml/>}
|
adamc@1006
|
167
|
adamc@1009
|
168 {if me.OnPc then
|
adamc@1009
|
169 <xml><li><a link={all ()}>All papers</a></li></xml>
|
adamc@1009
|
170 else
|
adamc@1009
|
171 <xml/>}
|
adamc@1009
|
172
|
adamc@1006
|
173 {if now < M.submissionDeadline then
|
adamc@1007
|
174 <xml><li><a link={submit ()}>Submit</a></li></xml>
|
adamc@1006
|
175 else
|
adamc@1006
|
176 <xml/>}
|
adamc@1004
|
177 </xml>}
|
adamc@1006
|
178 </ul></xml>
|
adamc@1006
|
179
|
adamc@1006
|
180 and main () =
|
adamc@1006
|
181 m <- main' ();
|
adamc@1006
|
182 return <xml><body>{m}</body></xml>
|
adamc@1001
|
183
|
adamc@1008
|
184 and submit () =
|
adamc@1008
|
185 let
|
adamc@1009
|
186 fun doSubmit r =
|
adamc@1010
|
187 me <- getLogin;
|
adamc@1010
|
188 coauthors <- List.mapM (fn name => oneOrNoRowsE1 (SELECT user.Id AS N
|
adamc@1010
|
189 FROM user
|
adamc@1010
|
190 WHERE user.Nam = {[name.Nam]})) r.Authors;
|
adamc@1010
|
191 if List.exists Option.isNone coauthors then
|
adamc@1010
|
192 error <xml>At least one of those coauthor usernames isn't registered.</xml>
|
adamc@1010
|
193 else
|
adamc@1010
|
194 id <- nextval paperId;
|
adamc@1010
|
195 dml (insert paper ({Id = sql_inject id, Document = sql_inject (fileData r.Document)}
|
adamc@1010
|
196 ++ ensql M.paper (r -- #Authors -- #Document) M.paperFolder));
|
adamc@1010
|
197 List.app (fn uid =>
|
adamc@1010
|
198 case uid of
|
adamc@1010
|
199 None => error <xml>Impossible empty uid!</xml>
|
adamc@1010
|
200 | Some uid => dml (INSERT INTO authorship (Paper, User)
|
adamc@1010
|
201 VALUES ({[id]}, {[uid]})))
|
adamc@1010
|
202 (Some me.Id :: coauthors);
|
adamc@1010
|
203 return <xml><body>
|
adamc@1010
|
204 Thanks for submitting!
|
adamc@1010
|
205 </body></xml>
|
adamc@1010
|
206
|
adamc@1010
|
207 fun authorBlanks n =
|
adamc@1010
|
208 case n of
|
adamc@1010
|
209 O => <xml/>
|
adamc@1010
|
210 | S n => <xml>
|
adamc@1010
|
211 <entry><b>Author:</b> <textbox{#Nam}/><br/></entry>
|
adamc@1010
|
212 <dyn signal={authorBlanksS n}/>
|
adamc@1010
|
213 </xml>
|
adamc@1010
|
214
|
adamc@1010
|
215 and authorBlanksS n =
|
adamc@1010
|
216 n <- signal n;
|
adamc@1010
|
217 return (authorBlanks n)
|
adamc@1008
|
218 in
|
adamc@1010
|
219 me <- getLogin;
|
adamc@1010
|
220 numAuthors <- source O;
|
adamc@1010
|
221
|
adamc@1008
|
222 return <xml><body>
|
adamc@1008
|
223 <h1>Submit a Paper</h1>
|
adamc@1008
|
224
|
adamc@1008
|
225 <form>
|
adamc@1010
|
226 <b>Author:</b> {[me.Nam]}<br/>
|
adamc@1010
|
227 <subforms{#Authors}>
|
adamc@1010
|
228 <dyn signal={authorBlanksS numAuthors}/>
|
adamc@1010
|
229 </subforms>
|
adamc@1010
|
230 <button value="Add author" onclick={inc numAuthors}/><br/>
|
adamc@1010
|
231 <button value="Remove author" onclick={dec numAuthors}/><br/>
|
adamc@1010
|
232 <br/>
|
adamc@1010
|
233
|
adamc@1010
|
234 {useMore (allWidgets M.paper M.paperFolder)}
|
adamc@1008
|
235 <b>Paper:</b> <upload{#Document}/><br/>
|
adamc@1008
|
236 <submit value="Submit" action={doSubmit}/>
|
adamc@1008
|
237 </form>
|
adamc@1008
|
238 </body></xml>
|
adamc@1008
|
239 end
|
adamc@1007
|
240
|
adamc@1009
|
241 and all () =
|
adamc@1009
|
242 ps <- queryX (SELECT paper.Id, paper.{{map fst M.paper}} FROM paper)
|
adamc@1009
|
243 (fn r => <xml><li><a link={one r.Paper.Id}>{M.summarizePaper (r.Paper -- #Id)}</a></li></xml>);
|
adamc@1009
|
244 return <xml><body>
|
adamc@1009
|
245 <h1>All Papers</h1>
|
adamc@1009
|
246
|
adamc@1009
|
247 <ul>
|
adamc@1009
|
248 {ps}
|
adamc@1009
|
249 </ul>
|
adamc@1009
|
250 </body></xml>
|
adamc@1009
|
251
|
adamc@1009
|
252 and one id =
|
adamc@1011
|
253 me <- getLogin;
|
adamc@1009
|
254 checkPaper id;
|
adamc@1009
|
255 ro <- oneOrNoRows (SELECT paper.{{map fst M.paper}}, octet_length(paper.Document) AS N
|
adamc@1009
|
256 FROM paper
|
adamc@1009
|
257 WHERE paper.Id = {[id]});
|
adamc@1010
|
258 authors <- queryX (SELECT user.Nam
|
adamc@1010
|
259 FROM authorship
|
adamc@1010
|
260 JOIN user ON authorship.User = user.Id
|
adamc@1010
|
261 WHERE authorship.Paper = {[id]})
|
adamc@1010
|
262 (fn r => <xml><li>{[r.User.Nam]}</li></xml>);
|
adamc@1011
|
263 myReview <- oneOrNoRows1 (SELECT review.{{map fst M.review}}
|
adamc@1011
|
264 FROM review
|
adamc@1011
|
265 WHERE review.User = {[me.Id]}
|
adamc@1011
|
266 AND review.Paper = {[id]});
|
adamc@1009
|
267 case ro of
|
adamc@1009
|
268 None => error <xml>Paper not found!</xml>
|
adamc@1009
|
269 | Some r => return <xml><body>
|
adamc@1009
|
270 <h1>Paper #{[id]}</h1>
|
adamc@1009
|
271
|
adamc@1010
|
272 <h3>Authors:</h3>
|
adamc@1010
|
273 <ul>
|
adamc@1010
|
274 {authors}
|
adamc@1010
|
275 </ul>
|
adamc@1010
|
276
|
adamc@1009
|
277 {allContent M.paper r.Paper M.paperFolder}<br/>
|
adamc@1009
|
278
|
adamc@1009
|
279 {if r.N = 0 then
|
adamc@1009
|
280 <xml><div>No paper uploaded yet.</div></xml>
|
adamc@1009
|
281 else
|
adamc@1009
|
282 <xml><a link={download id}>Download paper</a> ({[r.N]} bytes)</xml>}
|
adamc@1011
|
283
|
adamc@1011
|
284 <hr/>
|
adamc@1011
|
285
|
adamc@1011
|
286 {case myReview of
|
adamc@1011
|
287 None => <xml>
|
adamc@1011
|
288 <h2>Add Your Review</h2>
|
adamc@1011
|
289
|
adamc@1011
|
290 <form>
|
adamc@1011
|
291 {allWidgets M.review M.reviewFolder}
|
adamc@1011
|
292 </form>
|
adamc@1011
|
293 </xml>
|
adamc@1011
|
294 | Some myReview => <xml>
|
adamc@1011
|
295 <h2>Edit Your Review</h2>
|
adamc@1011
|
296
|
adamc@1011
|
297 <form>
|
adamc@1011
|
298 {allPopulated M.review myReview M.reviewFolder}
|
adamc@1011
|
299 </form>
|
adamc@1011
|
300 </xml>}
|
adamc@1009
|
301 </body></xml>
|
adamc@1009
|
302
|
adamc@1009
|
303 and download id =
|
adamc@1009
|
304 checkPaper id;
|
adamc@1009
|
305 ro <- oneOrNoRows (SELECT paper.Document
|
adamc@1009
|
306 FROM paper
|
adamc@1009
|
307 WHERE paper.Id = {[id]});
|
adamc@1009
|
308 case ro of
|
adamc@1009
|
309 None => error <xml>Paper not found!</xml>
|
adamc@1009
|
310 | Some r => returnBlob r.Paper.Document (blessMime "application/pdf")
|
adamc@1009
|
311
|
adamc@1001
|
312 end
|