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 package org.melati.admin;
46
47
48 import java.util.Enumeration;
49 import java.util.Hashtable;
50
51 import org.melati.Melati;
52 import org.melati.poem.AccessPoemException;
53 import org.melati.poem.Field;
54 import org.melati.poem.Persistent;
55 import org.melati.poem.Table;
56 import org.melati.poem.Treeable;
57 import org.melati.poem.util.ArrayUtils;
58 import org.melati.poem.util.StringUtils;
59 import org.melati.template.MarkupLanguage;
60 import org.melati.util.JSStaticTree;
61 import org.melati.util.Tree;
62
63
64
65
66
67 public class AdminUtils {
68
69 private String contextPath;
70 private String servletURL;
71 private String staticURL;
72 private String logicalDatabase;
73
74
75
76
77 public AdminUtils(Melati melati) {
78 this(melati.getRequest() == null ? null : melati.getRequest().getContextPath(),
79 melati.getRequest() == null ? null : melati.getRequest().getServletPath(),
80 melati.getConfig().getStaticURL() ,
81 melati.getPoemContext().getLogicalDatabase());
82 }
83
84
85
86
87 private AdminUtils(String contextPath, String servlet,
88 String staticURL, String logicalDatabase) {
89 this.contextPath = contextPath;
90 this.servletURL = contextPath + servlet;
91 this.staticURL = staticURL;
92 this.logicalDatabase = logicalDatabase;
93
94
95 if (contextPath == "") {
96 this.contextPath = servlet.substring(0, servlet.lastIndexOf("/"));
97 }
98 }
99
100
101
102
103 public static String getPrimaryDisplayTable(Melati melati) {
104 if (Admin.getPrimaryDisplayTable() == null)
105 Admin.setPrimaryDisplayTable(melati.getDatabase().
106 getSettingTable().get(Admin.class.getName() + ".PrimaryDisplayTable"));
107 if (Admin.getPrimaryDisplayTable() == null)
108 Admin.setPrimaryDisplayTable("columninfo");
109 return Admin.getPrimaryDisplayTable();
110 }
111
112
113
114
115
116 public String getScreenStylesheetURL(Melati melati) {
117 if (Admin.getScreenStylesheetURL() == null)
118 Admin.setScreenStylesheetURL(melati.getDatabase().
119 getSettingTable().get(Admin.class.getName() + ".ScreenStylesheetURL"));
120 if (Admin.getScreenStylesheetURL() == null)
121 Admin.setScreenStylesheetURL("/admin.css");
122 return staticURL + Admin.getScreenStylesheetURL();
123 }
124
125
126
127 public String getSetupURL() {
128 return servletURL + "/" + logicalDatabase +
129 "/setting/setup";
130 }
131
132
133
134 public String getSummaryURL() {
135 return servletURL + "/" + logicalDatabase +
136 "/Summary";
137 }
138
139
140
141
142
143
144
145
146
147 public String getHomepageURL(Melati melati) {
148 if (Admin.getHomepageURL() == null)
149 Admin.setHomepageURL(melati.getDatabase().
150 getSettingTable().get(Admin.class.getName() + ".HomepageURL"));
151 if (Admin.getHomepageURL() == null)
152 Admin.setHomepageURL("http://www.melati.org/");
153 return Admin.getHomepageURL();
154 }
155
156
157
158
159
160
161 public String getURL(Melati melati, String name) {
162 String url = servletURL + "/" + logicalDatabase;
163 if (melati.getTable() != null)
164 url += "/" + melati.getTable().getName();
165 if (melati.getObject() != null)
166 url += "/" + melati.getObject().getTroid();
167 return url + "/" + name;
168 }
169
170
171
172 public String getURL(Table<?> table, String name) {
173 String url = servletURL + "/" + logicalDatabase;
174 url += "/" + table.getName();
175 return url + "/" + name;
176 }
177
178
179
180 public String getMainURL(String ld) {
181 String url = servletURL + "/" + ld;
182 return url + "/Main";
183 }
184
185 public String getMainURL(Melati melati) {
186 return getURL(melati, "Main");
187 }
188
189
190 public String getTopURL(Melati melati) {
191 return getURL(melati, "Top");
192 }
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212 public String getBottomURL(Melati melati) {
213 String url = servletURL + "/" + logicalDatabase + "/";
214 if (melati.getTable() != null)
215 url += melati.getTable().getName();
216 else
217 url += getPrimaryDisplayTable(melati);
218 if (melati.getObject() != null)
219 url += "/" + melati.getObject().getTroid();
220 url += "/Bottom";
221 return url;
222 }
223
224
225
226
227 public String getTableURL(Table<?> table) {
228 return getURL(table, "Table");
229 }
230
231
232
233
234 public String getRecordURL(Persistent object) throws AccessPoemException {
235 return servletURL + "/" + logicalDatabase + "/" + object.getTable().getName()
236 + "/" + object.troid() + "/Record";
237 }
238
239
240
241
242 public String getRecordURL(Persistent object, String returnTarget, String returnURL) throws AccessPoemException {
243 return servletURL + "/" + logicalDatabase + "/" + object.getTable().getName()
244 + "/" + object.troid() + "/Record" +
245 "?returnTarget=" + returnTarget +
246 "&returnURL=" + returnURL;
247 }
248
249
250
251
252 public String getRecordURL(Melati melati) throws AccessPoemException {
253 return getURL(melati, "Record");
254 }
255
256
257
258
259 public String getPrimarySelectURL(Melati melati) {
260 return getURL(melati, "PrimarySelect");
261 }
262
263
264
265
266 public String getSelectionURL(Table<?> table) {
267 return getSelectionURL(table,"admin_record");
268 }
269
270
271
272 public String getSelectionURL(Table<?> table, String returnTarget) {
273 return getSelectionURL(table, "admin_record", returnTarget);
274 }
275
276
277
278
279
280
281 public String getSelectionURL(Table<?> table, String targetPane, String returnTarget) {
282 return servletURL + "/" + logicalDatabase + "/" + table.getName()
283 + "/Selection?" +
284 "target=" + targetPane +
285 "&returnTarget=" + returnTarget;
286 }
287
288
289
290
291
292 public String getToggledOrderSelectionURL(Melati melati, String field, String value) {
293 String url = melati.sameURLWith(field,value);
294 String toggleField = "&" + field + "-toggle=true";
295 if (url.endsWith(toggleField))
296 return url.substring(0,url.length() - toggleField.length());
297 else
298 return url + "&" + field + "-toggle=true";
299 }
300
301
302
303
304
305 public String getSelectionURL(Melati melati) {
306 return getSelectionURL(melati,"admin_record");
307 }
308
309
310
311
312 public String getSelectionURL(Melati melati, String returnTarget) {
313 return servletURL + "/" +
314 logicalDatabase + "/" +
315 melati.getTable().getName()
316 + "/Selection?" +
317 "target=admin_record" +
318 "&returnTarget=" + (returnTarget == null ? "" : returnTarget) +
319 (melati.getObject() == null ?
320 "" :
321 "&field_id=" + melati.getObject().troid());
322 }
323
324
325
326
327 public String getSelectionRightURL(Table<?> table) {
328 return servletURL + "/" + logicalDatabase + "/" + table.getName()
329 + "/SelectionRight";
330 }
331
332
333
334
335 public String getNavigationURL(Table<?> table) {
336 return servletURL + "/" + logicalDatabase + "/" + table.getName()
337 + "/Navigation";
338 }
339
340
341
342
343 public String getEditHeaderURL(Melati melati) throws AccessPoemException {
344 if (melati.getObject() == null)
345 return getURL(melati, "blank");
346 else
347 return getURL(melati, "EditHeader");
348 }
349
350
351
352
353 public String getEditURL(Melati melati) throws AccessPoemException {
354 if (melati.getObject() == null)
355 return getURL(melati, "blank");
356 else
357 return getURL(melati, "Edit");
358 }
359
360
361
362 public String getEditURL(Persistent object) throws AccessPoemException {
363 return servletURL + "/" + logicalDatabase + "/" + object.getTable().getName()
364 + "/" + object.troid() + "/Edit";
365 }
366
367
368
369
370
371 public String getEditFrameName(Melati melati) {
372 String name = "admin_edit";
373 name += "_" + melati.getTable().getName();
374 if (melati.getObject() != null)
375 name += "_" + melati.getObject().troid();
376 return name;
377 }
378
379
380
381 public String getTreeURL(Persistent object) throws AccessPoemException {
382 return servletURL + "/" + logicalDatabase + "/" + object.getTable().getName()
383 + "/" + object.troid() + "/Tree";
384 }
385
386
387
388
389 public String getTreeURL(Table<?> table) throws AccessPoemException {
390 return servletURL + "/" + logicalDatabase + "/" + table.getName()
391 + "/Tree";
392 }
393
394
395
396
397
398 public String getAddURL(Table<?> table) throws AccessPoemException {
399 return servletURL
400 + "/"
401 + logicalDatabase
402 + "/"
403 + table.getName()
404 + "/"
405 + "Add";
406 }
407
408
409
410
411 public String getPopUpURL(Table<?> table) {
412 return servletURL + "/" + logicalDatabase + "/" + table.getName() + "/PopUp";
413 }
414
415
416
417
418 public String getSelectionWindowURL(Table<?> table) {
419 return servletURL + "/" + logicalDatabase + "/" + table.getName()
420 + "/SelectionWindow?target=";
421 }
422
423
424
425
426 public String getSelectionWindowPrimarySelectURL(Table<?> table) {
427 return servletURL + "/" + logicalDatabase + "/" + table.getName()
428 + "/SelectionWindowPrimarySelect";
429 }
430
431
432
433
434 public String getSelectionWindowSelectionURL(Table<?> table) {
435 return servletURL + "/" + logicalDatabase + "/" + table.getName()
436 + "/SelectionWindowSelection";
437 }
438
439
440
441
442 public String getStatusURL() {
443 return contextPath + "/org.melati.admin.Status/" + logicalDatabase;
444 }
445
446
447
448
449 public String getSessionURL() {
450 return contextPath + "/org.melati.test.SessionAnalysisServlet";
451 }
452
453
454
455
456 public String getDsdURL() {
457 return servletURL + "/" + logicalDatabase + "/DSD";
458 }
459
460
461
462
463
464
465
466
467
468
469
470
471 public String getUploadURL(Table<?> table, Persistent object, Field<?> field) {
472 return upload(table, object) + "/Upload?field=" + field.getName();
473 }
474
475
476
477
478
479
480
481
482
483 public String getUploadHandlerURL(Table<?> table, Persistent object, String field) {
484 return upload(table, object) + "/UploadDone?field=" + field;
485 }
486 private String upload(Table<?> table, Persistent object) {
487 String url = servletURL + "/" + logicalDatabase + "/" + table.getName();
488 if (object != null)
489 url += "/" + object.troid();
490 return url;
491 }
492
493
494
495
496
497
498
499
500
501
502
503 public String specialFacilities(Melati melati, MarkupLanguage ml,
504 Persistent object) throws Exception {
505 if (object instanceof AdminSpecialised)
506 melati.getTemplateEngine().expandTemplate(melati.getWriter(),
507 ((AdminSpecialised) object).adminSpecialFacilities(melati, ml),
508 melati.getTemplateContext());
509 return "";
510
511
512
513
514
515
516
517
518
519 }
520
521
522
523
524 public String getStaticURL() {
525 return staticURL;
526 }
527
528
529
530
531
532
533 public JSStaticTree createTree(Treeable node) {
534 return new JSStaticTree(new Tree(node), getStaticURL());
535 }
536
537
538
539
540
541
542 @SuppressWarnings({ "unchecked", "rawtypes" })
543 public JSStaticTree createForest(Table<?> table) {
544 Object[] all = ArrayUtils.arrayOf((Enumeration) table.selection());
545 Hashtable<Treeable, Boolean> hasParent = new Hashtable<Treeable, Boolean>();
546 for (int i = 0; i < all.length; i++) {
547 if (hasParent.get(all[i]) == null) {
548 Treeable[] kids = ((Treeable)all[i]).getChildren();
549 for (int j = 0; j < kids.length; j++)
550 hasParent.put(kids[j], Boolean.TRUE);
551 }
552 }
553 int count = 0;
554 for (int i = 0; i < all.length; i++) {
555 if (hasParent.get(all[i]) == null){
556 count++;
557 }
558 }
559 Treeable[] roots = new Treeable[count];
560 int j = 0;
561 for (int i = 0; i < all.length; i++) {
562 if (hasParent.get(all[i]) == null) {
563 roots[j] = (Treeable)all[i];
564 j++;
565 }
566 }
567 return new JSStaticTree(roots, getStaticURL());
568 }
569
570
571
572
573
574 public static String simpleName(String qualifiedName) {
575 return qualifiedName.substring(
576 qualifiedName.lastIndexOf('.') != -1 ?
577 qualifiedName.lastIndexOf('.') + 1 :
578 0,
579 qualifiedName.length());
580 }
581
582
583
584
585
586 public static String csvEscaped(String in) {
587 StringBuffer b = new StringBuffer();
588 StringUtils.appendEscaped(b, in, '"', '"');
589 return b.toString();
590 }
591 }