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