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 | 974 | 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 | 974 | } |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
private AdminUtils(String contextPath, String servlet, |
87 | 974 | String staticURL, String logicalDatabase) { |
88 | 974 | this.contextPath = contextPath; |
89 | 974 | this.servletURL = contextPath + servlet; |
90 | 974 | this.staticURL = staticURL; |
91 | 974 | this.logicalDatabase = logicalDatabase; |
92 | |
|
93 | |
|
94 | 974 | if (contextPath == "") { |
95 | 0 | this.contextPath = servlet.substring(0, servlet.lastIndexOf("/")); |
96 | |
} |
97 | 974 | } |
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
public static String getPrimaryDisplayTable(Melati melati) { |
103 | 456 | if (Admin.getPrimaryDisplayTable() == null) |
104 | 2 | Admin.setPrimaryDisplayTable(melati.getDatabase(). |
105 | |
getSettingTable().get(Admin.class.getName() + ".PrimaryDisplayTable")); |
106 | 456 | if (Admin.getPrimaryDisplayTable() == null) |
107 | 2 | Admin.setPrimaryDisplayTable("columninfo"); |
108 | 456 | return Admin.getPrimaryDisplayTable(); |
109 | |
} |
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
public String getScreenStylesheetURL(Melati melati) { |
116 | 598 | if (Admin.getScreenStylesheetURL() == null) |
117 | 2 | Admin.setScreenStylesheetURL(melati.getDatabase(). |
118 | |
getSettingTable().get(Admin.class.getName() + ".ScreenStylesheetURL")); |
119 | 598 | if (Admin.getScreenStylesheetURL() == null) |
120 | 2 | Admin.setScreenStylesheetURL("/admin.css"); |
121 | 598 | return staticURL + Admin.getScreenStylesheetURL(); |
122 | |
} |
123 | |
|
124 | |
|
125 | |
|
126 | |
public String getSetupURL() { |
127 | 32 | return servletURL + "/" + logicalDatabase + |
128 | |
"/setting/setup"; |
129 | |
} |
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
public String getHomepageURL(Melati melati) { |
140 | 116 | if (Admin.getHomepageURL() == null) |
141 | 2 | Admin.setHomepageURL(melati.getDatabase(). |
142 | |
getSettingTable().get(Admin.class.getName() + ".HomepageURL")); |
143 | 116 | if (Admin.getHomepageURL() == null) |
144 | 2 | Admin.setHomepageURL("http://www.melati.org/"); |
145 | 116 | return Admin.getHomepageURL(); |
146 | |
} |
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
public String getURL(Melati melati, String name) { |
154 | 432 | String url = servletURL + "/" + logicalDatabase; |
155 | 432 | if (melati.getTable() != null) |
156 | 374 | url += "/" + melati.getTable().getName(); |
157 | 432 | if (melati.getObject() != null) |
158 | 82 | url += "/" + melati.getObject().getTroid(); |
159 | 432 | return url + "/" + name; |
160 | |
} |
161 | |
|
162 | |
|
163 | |
|
164 | |
public String getURL(Table table, String name) { |
165 | 74 | String url = servletURL + "/" + logicalDatabase; |
166 | 74 | url += "/" + table.getName(); |
167 | 74 | return url + "/" + name; |
168 | |
} |
169 | |
|
170 | |
|
171 | |
|
172 | |
public String MainURL(String ld) { |
173 | 348 | String url = servletURL + "/" + ld; |
174 | 348 | return url + "/Main"; |
175 | |
} |
176 | |
|
177 | |
public String MainURL(Melati melati) { |
178 | 58 | return getURL(melati, "Main"); |
179 | |
} |
180 | |
|
181 | |
public String MainURL(Table table) { |
182 | 4 | return getURL(table, "Main"); |
183 | |
} |
184 | |
|
185 | |
public String MainURL(Table table,Persistent p) { |
186 | 0 | String url = servletURL + "/" + logicalDatabase; |
187 | 0 | url += "/" + table.getName(); |
188 | 0 | url += "/" + p.troid(); |
189 | 0 | return url + "/" + "Main"; |
190 | |
} |
191 | |
|
192 | |
|
193 | |
public String TopURL(Melati melati) { |
194 | 52 | 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 | 110 | String url = servletURL + "/" + logicalDatabase + "/"; |
217 | 110 | if (melati.getTable() != null) |
218 | 52 | url += melati.getTable().getName(); |
219 | |
else |
220 | 58 | url += getPrimaryDisplayTable(melati); |
221 | 110 | if (melati.getObject() != null) |
222 | 18 | url += "/" + melati.getObject().getTroid(); |
223 | 110 | url += "/Bottom"; |
224 | 110 | return url; |
225 | |
} |
226 | |
|
227 | |
|
228 | |
|
229 | |
public String TableURL(Table table) { |
230 | 70 | return getURL(table, "Table"); |
231 | |
} |
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
public String RecordURL(Persistent object) throws AccessPoemException { |
237 | 8 | 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 | 4762 | 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 | 70 | return getURL(melati, "Record"); |
256 | |
} |
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
public String PrimarySelectURL(Melati melati) { |
262 | 72 | return getURL(melati, "PrimarySelect"); |
263 | |
} |
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
public String SelectionURL(Table table) { |
269 | 312 | return SelectionURL(table,"admin_record"); |
270 | |
} |
271 | |
|
272 | |
|
273 | |
|
274 | |
public String SelectionURL(Table table, String returnTarget) { |
275 | 368 | return SelectionURL(table, "admin_record", returnTarget); |
276 | |
} |
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
public String SelectionURL(Table table, String targetPane, String returnTarget) { |
284 | 368 | 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 | 408 | String url = melati.sameURLWith(field,value); |
296 | 408 | String toggleField = "&" + field + "-toggle=true"; |
297 | 408 | if (url.endsWith(toggleField)) |
298 | 48 | return url.substring(0,url.length() - toggleField.length()); |
299 | |
else |
300 | 360 | return url + "&" + field + "-toggle=true"; |
301 | |
} |
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
|
307 | |
public String SelectionURL(Melati melati) { |
308 | 72 | return SelectionURL(melati,"admin_record"); |
309 | |
} |
310 | |
|
311 | |
|
312 | |
|
313 | |
|
314 | |
public String SelectionURL(Melati melati, String returnTarget) { |
315 | 72 | 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 | 12 | return servletURL + "/" + logicalDatabase + "/" + table.getName() |
331 | |
+ "/SelectionRight"; |
332 | |
} |
333 | |
|
334 | |
|
335 | |
|
336 | |
|
337 | |
public String NavigationURL(Table table) { |
338 | 72 | return servletURL + "/" + logicalDatabase + "/" + table.getName() |
339 | |
+ "/Navigation"; |
340 | |
} |
341 | |
|
342 | |
|
343 | |
|
344 | |
|
345 | |
public String EditHeaderURL(Melati melati) throws AccessPoemException { |
346 | 80 | if (melati.getObject() == null) |
347 | 62 | return getURL(melati, "blank"); |
348 | |
else |
349 | 18 | return getURL(melati, "EditHeader"); |
350 | |
} |
351 | |
|
352 | |
|
353 | |
|
354 | |
|
355 | |
public String EditURL(Melati melati) throws AccessPoemException { |
356 | 100 | if (melati.getObject() == null) |
357 | 62 | return getURL(melati, "blank"); |
358 | |
else |
359 | 38 | return getURL(melati, "Edit"); |
360 | |
} |
361 | |
|
362 | |
|
363 | |
|
364 | |
public String EditURL(Persistent object) throws AccessPoemException { |
365 | 2 | return servletURL + "/" + logicalDatabase + "/" + object.getTable().getName() |
366 | |
+ "/" + object.troid() + "/Edit"; |
367 | |
} |
368 | |
|
369 | |
|
370 | |
|
371 | |
|
372 | |
|
373 | |
public String EditFrameName(Melati melati) { |
374 | 132 | String name = "admin_edit"; |
375 | 132 | name += "_" + melati.getTable().getName(); |
376 | 132 | if (melati.getObject() != null) |
377 | 70 | name += "_" + melati.getObject().troid(); |
378 | 132 | return name; |
379 | |
} |
380 | |
|
381 | |
|
382 | |
|
383 | |
public String TreeURL(Persistent object) throws AccessPoemException { |
384 | 20 | return servletURL + "/" + logicalDatabase + "/" + object.getTable().getName() |
385 | |
+ "/" + object.troid() + "/Tree"; |
386 | |
} |
387 | |
|
388 | |
|
389 | |
|
390 | |
|
391 | |
public String TreeURL(Table table) throws AccessPoemException { |
392 | 94 | return servletURL + "/" + logicalDatabase + "/" + table.getName() |
393 | |
+ "/Tree"; |
394 | |
} |
395 | |
|
396 | |
|
397 | |
|
398 | |
|
399 | |
|
400 | |
public String AddURL(Table table) throws AccessPoemException { |
401 | 128 | return servletURL |
402 | |
+ "/" |
403 | |
+ logicalDatabase |
404 | |
+ "/" |
405 | |
+ table.getName() |
406 | |
+ "/" |
407 | |
+ "Add"; |
408 | |
} |
409 | |
|
410 | |
|
411 | |
|
412 | |
|
413 | |
public String PopUpURL(Table table) { |
414 | 144 | return servletURL + "/" + logicalDatabase + "/" + table.getName() + "/PopUp"; |
415 | |
} |
416 | |
|
417 | |
|
418 | |
|
419 | |
|
420 | |
public String SelectionWindowURL(Table table) { |
421 | 40 | return servletURL + "/" + logicalDatabase + "/" + table.getName() |
422 | |
+ "/SelectionWindow?target="; |
423 | |
} |
424 | |
|
425 | |
|
426 | |
|
427 | |
|
428 | |
public String SelectionWindowPrimarySelectURL(Table table) { |
429 | 2 | return servletURL + "/" + logicalDatabase + "/" + table.getName() |
430 | |
+ "/SelectionWindowPrimarySelect"; |
431 | |
} |
432 | |
|
433 | |
|
434 | |
|
435 | |
|
436 | |
public String SelectionWindowSelectionURL(Table table) { |
437 | 6 | return servletURL + "/" + logicalDatabase + "/" + table.getName() |
438 | |
+ "/SelectionWindowSelection"; |
439 | |
} |
440 | |
|
441 | |
|
442 | |
|
443 | |
|
444 | |
public String StatusURL() { |
445 | 32 | return contextPath + "/org.melati.admin.Status/" + logicalDatabase; |
446 | |
} |
447 | |
|
448 | |
|
449 | |
|
450 | |
|
451 | |
public String SessionURL() { |
452 | 32 | return contextPath + "/org.melati.test.SessionAnalysisServlet"; |
453 | |
} |
454 | |
|
455 | |
|
456 | |
|
457 | |
|
458 | |
public String DsdURL() { |
459 | 32 | 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 | 16 | 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 | 4 | return upload(table, object) + "/UploadDone?field=" + field; |
487 | |
} |
488 | |
private String upload(Table table, Persistent object) { |
489 | 20 | String url = servletURL + "/" + logicalDatabase + "/" + table.getName(); |
490 | 20 | if (object != null) |
491 | 4 | url += "/" + object.troid(); |
492 | 20 | 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 | 40 | if (object instanceof AdminSpecialised) |
508 | 2 | melati.getTemplateEngine().expandTemplate(melati.getWriter(), |
509 | |
((AdminSpecialised) object).adminSpecialFacilities(melati, ml), |
510 | |
melati.getTemplateContext()); |
511 | 40 | return ""; |
512 | |
|
513 | |
|
514 | |
|
515 | |
|
516 | |
|
517 | |
|
518 | |
|
519 | |
|
520 | |
|
521 | |
} |
522 | |
|
523 | |
|
524 | |
|
525 | |
|
526 | |
public String getStaticURL() { |
527 | 1472 | return staticURL; |
528 | |
} |
529 | |
|
530 | |
|
531 | |
|
532 | |
|
533 | |
|
534 | |
|
535 | |
public JSStaticTree createTree(Treeable node) { |
536 | 4 | return new JSStaticTree(new Tree(node), getStaticURL()); |
537 | |
} |
538 | |
|
539 | |
|
540 | |
|
541 | |
|
542 | |
|
543 | |
|
544 | |
public JSStaticTree createForest(Table table) { |
545 | 2 | Object[] all = ArrayUtils.arrayOf(table.selection()); |
546 | 2 | Hashtable<Treeable, Boolean> hasParent = new Hashtable<Treeable, Boolean>(); |
547 | 6 | for (int i = 0; i < all.length; i++) { |
548 | 4 | if (hasParent.get(all[i]) == null) { |
549 | 4 | Treeable[] kids = ((Treeable)all[i]).getChildren(); |
550 | 6 | for (int j = 0; j < kids.length; j++) |
551 | 2 | hasParent.put(kids[j], Boolean.TRUE); |
552 | |
} |
553 | |
} |
554 | 2 | int count = 0; |
555 | 6 | for (int i = 0; i < all.length; i++) { |
556 | 4 | if (hasParent.get(all[i]) == null){ |
557 | 4 | count++; |
558 | |
} |
559 | |
} |
560 | 2 | Treeable[] roots = new Treeable[count]; |
561 | 2 | int j = 0; |
562 | 6 | for (int i = 0; i < all.length; i++) { |
563 | 4 | if (hasParent.get(all[i]) == null) { |
564 | 4 | roots[j] = (Treeable)all[i]; |
565 | 4 | j++; |
566 | |
} |
567 | |
} |
568 | 2 | return new JSStaticTree(roots, getStaticURL()); |
569 | |
} |
570 | |
|
571 | |
|
572 | |
|
573 | |
|
574 | |
|
575 | |
public static String simpleName(String qualifiedName) { |
576 | 26 | 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 | 4552 | StringBuffer b = new StringBuffer(); |
589 | 4552 | StringUtils.appendEscaped(b, in, '"', '"'); |
590 | 4552 | return b.toString(); |
591 | |
} |
592 | |
} |