| 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 | 924 | this(melati.getRequest() == null ? null : melati.getRequest().getContextPath(), |
| 79 | 462 | melati.getRequest() == null ? null : melati.getRequest().getServletPath(), |
| 80 | 462 | melati.getConfig().getStaticURL() , |
| 81 | 462 | melati.getPoemContext().getLogicalDatabase()); |
| 82 | 462 | } |
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
private AdminUtils(String contextPath, String servlet, |
| 88 | 462 | String staticURL, String logicalDatabase) { |
| 89 | 462 | this.contextPath = contextPath; |
| 90 | 462 | this.servletURL = contextPath + servlet; |
| 91 | 462 | this.staticURL = staticURL; |
| 92 | 462 | this.logicalDatabase = logicalDatabase; |
| 93 | |
|
| 94 | |
|
| 95 | 462 | if (contextPath == "") { |
| 96 | 0 | this.contextPath = servlet.substring(0, servlet.lastIndexOf("/")); |
| 97 | |
} |
| 98 | 462 | } |
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
public static String getPrimaryDisplayTable(Melati melati) { |
| 104 | 240 | if (Admin.getPrimaryDisplayTable() == null) |
| 105 | 2 | Admin.setPrimaryDisplayTable(melati.getDatabase(). |
| 106 | 1 | getSettingTable().get(Admin.class.getName() + ".PrimaryDisplayTable")); |
| 107 | 240 | if (Admin.getPrimaryDisplayTable() == null) |
| 108 | 1 | Admin.setPrimaryDisplayTable("columninfo"); |
| 109 | 240 | return Admin.getPrimaryDisplayTable(); |
| 110 | |
} |
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
public String getScreenStylesheetURL(Melati melati) { |
| 117 | 283 | if (Admin.getScreenStylesheetURL() == null) |
| 118 | 2 | Admin.setScreenStylesheetURL(melati.getDatabase(). |
| 119 | 1 | getSettingTable().get(Admin.class.getName() + ".ScreenStylesheetURL")); |
| 120 | 283 | if (Admin.getScreenStylesheetURL() == null) |
| 121 | 1 | Admin.setScreenStylesheetURL("/admin.css"); |
| 122 | 283 | return staticURL + Admin.getScreenStylesheetURL(); |
| 123 | |
} |
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
public String getSetupURL() { |
| 128 | 17 | return servletURL + "/" + logicalDatabase + |
| 129 | |
"/setting/setup"; |
| 130 | |
} |
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
public String getSummaryURL() { |
| 135 | 30 | return servletURL + "/" + logicalDatabase + |
| 136 | |
"/Summary"; |
| 137 | |
} |
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
public String getHomepageURL(Melati melati) { |
| 148 | 60 | if (Admin.getHomepageURL() == null) |
| 149 | 2 | Admin.setHomepageURL(melati.getDatabase(). |
| 150 | 1 | getSettingTable().get(Admin.class.getName() + ".HomepageURL")); |
| 151 | 60 | if (Admin.getHomepageURL() == null) |
| 152 | 1 | Admin.setHomepageURL("http://www.melati.org/"); |
| 153 | 60 | return Admin.getHomepageURL(); |
| 154 | |
} |
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
public String getURL(Melati melati, String name) { |
| 162 | 207 | String url = servletURL + "/" + logicalDatabase; |
| 163 | 207 | if (melati.getTable() != null) |
| 164 | 182 | url += "/" + melati.getTable().getName(); |
| 165 | 207 | if (melati.getObject() != null) |
| 166 | 49 | url += "/" + melati.getObject().getTroid(); |
| 167 | 207 | return url + "/" + name; |
| 168 | |
} |
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
public String getURL(Table<?> table, String name) { |
| 173 | 32 | String url = servletURL + "/" + logicalDatabase; |
| 174 | 32 | url += "/" + table.getName(); |
| 175 | 32 | return url + "/" + name; |
| 176 | |
} |
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
public String getMainURL(String ld) { |
| 181 | 108 | String url = servletURL + "/" + ld; |
| 182 | 108 | return url + "/Main"; |
| 183 | |
} |
| 184 | |
|
| 185 | |
public String getMainURL(Melati melati) { |
| 186 | 34 | return getURL(melati, "Main"); |
| 187 | |
} |
| 188 | |
|
| 189 | |
|
| 190 | |
public String getTopURL(Melati melati) { |
| 191 | 23 | 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 | 53 | String url = servletURL + "/" + logicalDatabase + "/"; |
| 214 | 53 | if (melati.getTable() != null) |
| 215 | 28 | url += melati.getTable().getName(); |
| 216 | |
else |
| 217 | 25 | url += getPrimaryDisplayTable(melati); |
| 218 | 53 | if (melati.getObject() != null) |
| 219 | 11 | url += "/" + melati.getObject().getTroid(); |
| 220 | 53 | url += "/Bottom"; |
| 221 | 53 | return url; |
| 222 | |
} |
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | |
public String getTableURL(Table<?> table) { |
| 228 | 32 | return getURL(table, "Table"); |
| 229 | |
} |
| 230 | |
|
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
public String getRecordURL(Persistent object) throws AccessPoemException { |
| 235 | 4 | return servletURL + "/" + logicalDatabase + "/" + object.getTable().getName() |
| 236 | 4 | + "/" + object.troid() + "/Record"; |
| 237 | |
} |
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
public String getRecordURL(Persistent object, String returnTarget, String returnURL) throws AccessPoemException { |
| 243 | 2231 | return servletURL + "/" + logicalDatabase + "/" + object.getTable().getName() |
| 244 | 2231 | + "/" + object.troid() + "/Record" + |
| 245 | |
"?returnTarget=" + returnTarget + |
| 246 | |
"&returnURL=" + returnURL; |
| 247 | |
} |
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
public String getRecordURL(Melati melati) throws AccessPoemException { |
| 253 | 32 | return getURL(melati, "Record"); |
| 254 | |
} |
| 255 | |
|
| 256 | |
|
| 257 | |
|
| 258 | |
|
| 259 | |
public String getPrimarySelectURL(Melati melati) { |
| 260 | 33 | return getURL(melati, "PrimarySelect"); |
| 261 | |
} |
| 262 | |
|
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
public String getSelectionURL(Table<?> table) { |
| 267 | 144 | return getSelectionURL(table,"admin_record"); |
| 268 | |
} |
| 269 | |
|
| 270 | |
|
| 271 | |
|
| 272 | |
public String getSelectionURL(Table<?> table, String returnTarget) { |
| 273 | 172 | return getSelectionURL(table, "admin_record", returnTarget); |
| 274 | |
} |
| 275 | |
|
| 276 | |
|
| 277 | |
|
| 278 | |
|
| 279 | |
|
| 280 | |
|
| 281 | |
public String getSelectionURL(Table<?> table, String targetPane, String returnTarget) { |
| 282 | 172 | 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 | 192 | String url = melati.sameURLWith(field,value); |
| 294 | 192 | String toggleField = "&" + field + "-toggle=true"; |
| 295 | 192 | if (url.endsWith(toggleField)) |
| 296 | 24 | return url.substring(0,url.length() - toggleField.length()); |
| 297 | |
else |
| 298 | 168 | return url + "&" + field + "-toggle=true"; |
| 299 | |
} |
| 300 | |
|
| 301 | |
|
| 302 | |
|
| 303 | |
|
| 304 | |
|
| 305 | |
public String getSelectionURL(Melati melati) { |
| 306 | 33 | return getSelectionURL(melati,"admin_record"); |
| 307 | |
} |
| 308 | |
|
| 309 | |
|
| 310 | |
|
| 311 | |
|
| 312 | |
public String getSelectionURL(Melati melati, String returnTarget) { |
| 313 | 33 | return servletURL + "/" + |
| 314 | |
logicalDatabase + "/" + |
| 315 | 33 | melati.getTable().getName() |
| 316 | |
+ "/Selection?" + |
| 317 | |
"target=admin_record" + |
| 318 | |
"&returnTarget=" + (returnTarget == null ? "" : returnTarget) + |
| 319 | 33 | (melati.getObject() == null ? |
| 320 | |
"" : |
| 321 | 0 | "&field_id=" + melati.getObject().troid()); |
| 322 | |
} |
| 323 | |
|
| 324 | |
|
| 325 | |
|
| 326 | |
|
| 327 | |
public String getSelectionRightURL(Table<?> table) { |
| 328 | 6 | return servletURL + "/" + logicalDatabase + "/" + table.getName() |
| 329 | |
+ "/SelectionRight"; |
| 330 | |
} |
| 331 | |
|
| 332 | |
|
| 333 | |
|
| 334 | |
|
| 335 | |
public String getNavigationURL(Table<?> table) { |
| 336 | 33 | return servletURL + "/" + logicalDatabase + "/" + table.getName() |
| 337 | |
+ "/Navigation"; |
| 338 | |
} |
| 339 | |
|
| 340 | |
|
| 341 | |
|
| 342 | |
|
| 343 | |
public String getEditHeaderURL(Melati melati) throws AccessPoemException { |
| 344 | 37 | if (melati.getObject() == null) |
| 345 | 27 | return getURL(melati, "blank"); |
| 346 | |
else |
| 347 | 10 | return getURL(melati, "EditHeader"); |
| 348 | |
} |
| 349 | |
|
| 350 | |
|
| 351 | |
|
| 352 | |
|
| 353 | |
public String getEditURL(Melati melati) throws AccessPoemException { |
| 354 | 48 | if (melati.getObject() == null) |
| 355 | 27 | return getURL(melati, "blank"); |
| 356 | |
else |
| 357 | 21 | return getURL(melati, "Edit"); |
| 358 | |
} |
| 359 | |
|
| 360 | |
|
| 361 | |
|
| 362 | |
public String getEditURL(Persistent object) throws AccessPoemException { |
| 363 | 0 | return servletURL + "/" + logicalDatabase + "/" + object.getTable().getName() |
| 364 | 0 | + "/" + object.troid() + "/Edit"; |
| 365 | |
} |
| 366 | |
|
| 367 | |
|
| 368 | |
|
| 369 | |
|
| 370 | |
|
| 371 | |
public String getEditFrameName(Melati melati) { |
| 372 | 71 | String name = "admin_edit"; |
| 373 | 71 | name += "_" + melati.getTable().getName(); |
| 374 | 71 | if (melati.getObject() != null) |
| 375 | 44 | name += "_" + melati.getObject().troid(); |
| 376 | 71 | return name; |
| 377 | |
} |
| 378 | |
|
| 379 | |
|
| 380 | |
|
| 381 | |
public String getTreeURL(Persistent object) throws AccessPoemException { |
| 382 | 11 | return servletURL + "/" + logicalDatabase + "/" + object.getTable().getName() |
| 383 | 11 | + "/" + object.troid() + "/Tree"; |
| 384 | |
} |
| 385 | |
|
| 386 | |
|
| 387 | |
|
| 388 | |
|
| 389 | |
public String getTreeURL(Table<?> table) throws AccessPoemException { |
| 390 | 44 | return servletURL + "/" + logicalDatabase + "/" + table.getName() |
| 391 | |
+ "/Tree"; |
| 392 | |
} |
| 393 | |
|
| 394 | |
|
| 395 | |
|
| 396 | |
|
| 397 | |
|
| 398 | |
public String getAddURL(Table<?> table) throws AccessPoemException { |
| 399 | 60 | return servletURL |
| 400 | |
+ "/" |
| 401 | |
+ logicalDatabase |
| 402 | |
+ "/" |
| 403 | 60 | + table.getName() |
| 404 | |
+ "/" |
| 405 | |
+ "Add"; |
| 406 | |
} |
| 407 | |
|
| 408 | |
|
| 409 | |
|
| 410 | |
|
| 411 | |
public String getPopUpURL(Table<?> table) { |
| 412 | 66 | return servletURL + "/" + logicalDatabase + "/" + table.getName() + "/PopUp"; |
| 413 | |
} |
| 414 | |
|
| 415 | |
|
| 416 | |
|
| 417 | |
|
| 418 | |
public String getSelectionWindowURL(Table<?> table) { |
| 419 | 19 | return servletURL + "/" + logicalDatabase + "/" + table.getName() |
| 420 | |
+ "/SelectionWindow?target="; |
| 421 | |
} |
| 422 | |
|
| 423 | |
|
| 424 | |
|
| 425 | |
|
| 426 | |
public String getSelectionWindowPrimarySelectURL(Table<?> table) { |
| 427 | 1 | return servletURL + "/" + logicalDatabase + "/" + table.getName() |
| 428 | |
+ "/SelectionWindowPrimarySelect"; |
| 429 | |
} |
| 430 | |
|
| 431 | |
|
| 432 | |
|
| 433 | |
|
| 434 | |
public String getSelectionWindowSelectionURL(Table<?> table) { |
| 435 | 3 | return servletURL + "/" + logicalDatabase + "/" + table.getName() |
| 436 | |
+ "/SelectionWindowSelection"; |
| 437 | |
} |
| 438 | |
|
| 439 | |
|
| 440 | |
|
| 441 | |
|
| 442 | |
public String getStatusURL() { |
| 443 | 17 | return contextPath + "/org.melati.admin.Status/" + logicalDatabase; |
| 444 | |
} |
| 445 | |
|
| 446 | |
|
| 447 | |
|
| 448 | |
|
| 449 | |
public String getSessionURL() { |
| 450 | 17 | return contextPath + "/org.melati.test.SessionAnalysisServlet"; |
| 451 | |
} |
| 452 | |
|
| 453 | |
|
| 454 | |
|
| 455 | |
|
| 456 | |
public String getDsdURL() { |
| 457 | 17 | 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 | 8 | 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 | 2 | return upload(table, object) + "/UploadDone?field=" + field; |
| 485 | |
} |
| 486 | |
private String upload(Table<?> table, Persistent object) { |
| 487 | 10 | String url = servletURL + "/" + logicalDatabase + "/" + table.getName(); |
| 488 | 10 | if (object != null) |
| 489 | 2 | url += "/" + object.troid(); |
| 490 | 10 | 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 | 20 | if (object instanceof AdminSpecialised) |
| 506 | 2 | melati.getTemplateEngine().expandTemplate(melati.getWriter(), |
| 507 | 1 | ((AdminSpecialised) object).adminSpecialFacilities(melati, ml), |
| 508 | 1 | melati.getTemplateContext()); |
| 509 | 20 | return ""; |
| 510 | |
|
| 511 | |
|
| 512 | |
|
| 513 | |
|
| 514 | |
|
| 515 | |
|
| 516 | |
|
| 517 | |
|
| 518 | |
|
| 519 | |
} |
| 520 | |
|
| 521 | |
|
| 522 | |
|
| 523 | |
|
| 524 | |
public String getStaticURL() { |
| 525 | 692 | return staticURL; |
| 526 | |
} |
| 527 | |
|
| 528 | |
|
| 529 | |
|
| 530 | |
|
| 531 | |
|
| 532 | |
|
| 533 | |
public JSStaticTree createTree(Treeable node) { |
| 534 | 2 | 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 | 1 | Object[] all = ArrayUtils.arrayOf((Enumeration) table.selection()); |
| 545 | 1 | Hashtable<Treeable, Boolean> hasParent = new Hashtable<Treeable, Boolean>(); |
| 546 | 3 | for (int i = 0; i < all.length; i++) { |
| 547 | 2 | if (hasParent.get(all[i]) == null) { |
| 548 | 2 | Treeable[] kids = ((Treeable)all[i]).getChildren(); |
| 549 | 3 | for (int j = 0; j < kids.length; j++) |
| 550 | 1 | hasParent.put(kids[j], Boolean.TRUE); |
| 551 | |
} |
| 552 | |
} |
| 553 | 1 | int count = 0; |
| 554 | 3 | for (int i = 0; i < all.length; i++) { |
| 555 | 2 | if (hasParent.get(all[i]) == null){ |
| 556 | 2 | count++; |
| 557 | |
} |
| 558 | |
} |
| 559 | 1 | Treeable[] roots = new Treeable[count]; |
| 560 | 1 | int j = 0; |
| 561 | 3 | for (int i = 0; i < all.length; i++) { |
| 562 | 2 | if (hasParent.get(all[i]) == null) { |
| 563 | 2 | roots[j] = (Treeable)all[i]; |
| 564 | 2 | j++; |
| 565 | |
} |
| 566 | |
} |
| 567 | 1 | return new JSStaticTree(roots, getStaticURL()); |
| 568 | |
} |
| 569 | |
|
| 570 | |
|
| 571 | |
|
| 572 | |
|
| 573 | |
|
| 574 | |
public static String simpleName(String qualifiedName) { |
| 575 | 26 | return qualifiedName.substring( |
| 576 | 13 | qualifiedName.lastIndexOf('.') != -1 ? |
| 577 | 10 | qualifiedName.lastIndexOf('.') + 1 : |
| 578 | |
0, |
| 579 | 13 | qualifiedName.length()); |
| 580 | |
} |
| 581 | |
|
| 582 | |
|
| 583 | |
|
| 584 | |
|
| 585 | |
|
| 586 | |
public static String csvEscaped(String in) { |
| 587 | 2276 | StringBuffer b = new StringBuffer(); |
| 588 | 2276 | StringUtils.appendEscaped(b, in, '"', '"'); |
| 589 | 2276 | return b.toString(); |
| 590 | |
} |
| 591 | |
} |