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