1 10 package org.mmbase.applications.editwizard; 11 12 import java.util.*; 13 import java.net.*; 14 import org.mmbase.util.xml.URIResolver; 15 import javax.servlet.jsp.PageContext ; 16 import javax.servlet.http.HttpServletRequest ; 17 import javax.servlet.http.HttpServletResponse ; 18 19 import org.mmbase.util.ResourceLoader; 20 21 import org.mmbase.bridge.*; 22 import org.mmbase.util.logging.*; 23 import org.mmbase.util.xml.UtilReader; 24 import org.mmbase.util.Encode; 25 26 35 36 public class Config { 37 38 private static final long serialVersionUID = 1L; 40 43 public final static int DEFAULT_MAX_UPLOAD_SIZE = 4 * 1024 * 1024; 44 45 private static final Logger log = Logging.getLoggerInstance(Config.class); 46 47 private final static String PROTOCOL = "http://"; 49 50 52 public static final String CONFIG_FILE = "editwizard.xml"; 53 54 public static String wizardStyleSheet = "xsl/wizard.xsl"; 55 public static String listStyleSheet = "xsl/list.xsl"; 56 public static String searchlistStyleSheet = "xsl/searchlist.xsl"; 57 public static int maxUploadSize = DEFAULT_MAX_UPLOAD_SIZE; 58 59 62 private static final UtilReader reader = new UtilReader(CONFIG_FILE, 63 new Runnable () { 64 public void run() { 65 readConfiguration(reader.getProperties()); 66 } 67 }); 68 static { 69 readConfiguration(reader.getProperties()); 70 } 71 72 synchronized static void readConfiguration(Map configuration) { 73 String tmp = (String ) configuration.get("wizardStyleSheet"); 74 if (tmp != null && !tmp.equals("")) { 75 wizardStyleSheet = tmp; 76 log.service("Editwizard default wizard style sheet " + wizardStyleSheet); 77 } 78 tmp = (String ) configuration.get("listStyleSheet"); 79 if (tmp != null && !tmp.equals("")) { 80 listStyleSheet = tmp; 81 log.service("Editwizard default list style sheet " + listStyleSheet); 82 } 83 tmp = (String ) configuration.get("searchlistStyleSheet"); 84 if (tmp != null && !tmp.equals("")) { 85 searchlistStyleSheet = tmp; 86 log.service("Editwizard default searchlist style sheet " + searchlistStyleSheet); 87 } 88 tmp = (String ) configuration.get("maxUploadSize"); 89 if (tmp != null && !tmp.equals("")) { 90 try { 91 maxUploadSize = Integer.parseInt(tmp); 92 } catch (Exception e) {} 93 log.service("Editwizard default max upload size " + maxUploadSize); 94 } 95 96 } 97 98 public String sessionKey = null; 99 public URIResolver uriResolver = null; 100 public int maxupload = Config.maxUploadSize; 101 public Stack subObjects = new Stack(); public String sessionId; public String backPage; 104 public String templates; 105 public String language; 106 public String timezone; 107 108 114 protected Map attributes; 115 116 117 119 static public class SubConfig implements java.io.Serializable { 120 public boolean debug = false; 121 public String wizard; 122 public String page; 123 public HashMap popups = new HashMap(); 125 public HashMap attributes = new HashMap(); 126 127 142 public void configure(Config.Configurator configurator) throws WizardException { 143 wizard = configurator.getParam("wizard", wizard); 144 if (wizard != null && wizard.startsWith("/")) { 145 try { 146 wizard = configurator.getResource(wizard).toString(); 147 } catch(MalformedURLException mfue) { 148 throw new WizardException(mfue); 149 } 150 } 151 configurator.fillAttributes(attributes); 152 153 debug = configurator.getParam("debug", debug); 158 } 159 160 168 171 public Map getAttributes() { 172 Map attributeMap = new HashMap(attributes); 173 return attributeMap; 174 } 175 176 } 177 178 static public class WizardConfig extends SubConfig { 179 public Wizard wiz; 180 public String objectNumber; 181 public String parentFid; 182 public String parentDid; 183 public String popupId; 184 185 198 public void configure(Config.Configurator configurator) throws WizardException { 199 super.configure(configurator); 200 popupId = configurator.getParam("popupid", ""); 201 objectNumber = configurator.getParam("objectnumber"); 202 } 203 204 207 216 } 217 218 static public class ListConfig extends SubConfig { 219 220 public static final int SEARCH_NO = 0; 222 223 public static final int SEARCH_AUTO = 5; 225 public static final int SEARCH_YES = 10; 226 public static final int SEARCH_FORCE = 11; 228 229 public String title; 230 public URL template; 231 public String fields; 232 public String startNodes; 233 public String nodePath; 234 public String constraints; 235 public String orderBy; 236 public String directions; 237 public String searchDir; 238 239 public String searchFields; 240 public String realSearchField; 241 public String searchValue=""; 242 public String searchType="like"; 243 public String baseConstraints; 244 public int search = SEARCH_AUTO; 245 246 public int age = -1; 247 public int start = 0; 248 public boolean distinct = false; 249 public int pagelength = 50; 250 public int maxpagecount = 10; 251 252 public boolean multilevel = false; 253 public String mainObjectName = null; 254 public List fieldList = null; 255 256 protected Cloud cloud; 257 258 ListConfig(Cloud cloud) { 259 this.cloud = cloud; 260 } 261 262 265 ListConfig() { this.cloud = null; 267 } 268 269 private boolean parsed = false; 270 271 protected static String removeDigits(String complete) { 272 int end = complete.length() - 1; 273 while (Character.isDigit(complete.charAt(end))) { 274 --end; 275 } 276 return complete.substring(0, end + 1); 277 } 278 279 306 public void configure(Config.Configurator configurator) throws WizardException { 307 super.configure(configurator); 308 title = configurator.getParam("title", title); 309 pagelength = configurator.getParam("pagelength", new Integer (pagelength)).intValue(); 310 maxpagecount = configurator.getParam("maxpagecount", new Integer (maxpagecount)).intValue(); 311 startNodes = configurator.getParam("startnodes", startNodes); 312 313 String parameter = configurator.getParam("nodepath"); 318 if (parameter != null) { 319 nodePath = parameter; 320 parsed = false; 321 } 322 if (nodePath == null) { 323 throw new WizardException("The parameter 'nodepath' is required but not given."); 324 } 325 326 parameter = configurator.getParam("fields"); 331 if (parameter != null) { 332 fields = parameter; 333 parsed = false; 334 } 335 if (fields == null) { 336 log.debug("The parameter 'fields' is not given, going to take the first field"); 338 340 } 341 342 age = configurator.getParam("age", new Integer (age)).intValue(); 343 if (age >= 99999) age=-1; 344 345 start = configurator.getParam("start", new Integer (start)).intValue(); 346 searchType = configurator.getParam("searchtype", searchType); 347 searchFields = configurator.getParam("searchfields", searchFields); 348 searchValue = configurator.getParam("searchvalue", searchValue); 349 searchDir = configurator.getParam("searchdir", searchDir); 350 searchDir = configurator.getParam("searchdirs", searchDir); 351 baseConstraints = configurator.getParam("constraints", baseConstraints); 352 String searchString = configurator.getParam("search", (String ) null); 353 if (searchString != null) { 354 searchString = searchString.toLowerCase(); 355 if (searchString.equals("auto")) { 356 search = SEARCH_AUTO; 357 } else if (searchString.equals("no")) { 358 search = SEARCH_NO; 359 } else if (searchString.equals("yes")) { 360 search = SEARCH_YES; 361 } else if (searchString.equals("force")) { 362 search = SEARCH_FORCE; 363 } else { 364 throw new WizardException("Unknown value for search parameter '" + searchString + "'"); 365 } 366 } else { 367 log.debug("Search is null?"); 368 } 369 370 realSearchField = configurator.getParam("realsearchfield", realSearchField); 372 373 if (searchFields == null) { 374 constraints = baseConstraints; 375 } else { 376 StringBuffer constraintsBuffer; 377 String sType = searchType; 379 String sFields = realSearchField; 383 if (sFields == null) sFields = searchFields; 384 if (sFields.equals("owner") || sFields.endsWith(".owner")) { 385 sType = "like"; 386 } else if (sFields.equals("number") || sFields.endsWith(".number")) { 387 sType = "equals"; 388 } 389 String where = Encode.encode("ESCAPE_SINGLE_QUOTE",searchValue); 390 constraintsBuffer = null; 391 if (sType.equals("like")) { 392 if (! "".equals(where)) { 393 where = " LIKE '%" + where.toLowerCase() + "%'"; 394 } 395 } else if (sType.equals("string")) { 396 if (! "".equals(where)) { 397 where = " = '" + where + "'"; 398 } 399 } else { 400 if (where.equals("")) { 401 where = "0"; 402 } 403 if (sType.equals("greaterthan")) { 404 where = " > " + where; 405 } else if (sType.equals("lessthan")) { 406 where = " < " + where; 407 } else if (sType.equals("notgreaterthan")) { 408 where = " <= " + where; 409 } else if (sType.equals("notlessthan")) { 410 where = " >= " + where; 411 } else if (sType.equals("notequals")) { 412 where = " != " + where; 413 } else { where = " = " + where; 415 } 416 } 417 if (! "".equals(where)) { 418 StringTokenizer searchTokens= new StringTokenizer(sFields, ","); 419 while (searchTokens.hasMoreTokens()) { 420 String tok = searchTokens.nextToken(); 421 if (constraintsBuffer != null) { 422 constraintsBuffer.append(" OR "); 423 } else { 424 constraintsBuffer = new StringBuffer (); 425 } 426 if (sType.equals("like")) { 427 constraintsBuffer.append("lower([").append(tok).append("])").append(where); 428 } else { 429 constraintsBuffer.append('[').append(tok).append(']').append(where); 430 } 431 } 432 } 433 if (baseConstraints != null) { 434 if (constraintsBuffer != null) { 435 constraints = "(" + baseConstraints + ") and (" + constraintsBuffer.toString() + ")"; 436 } else { 437 constraints = baseConstraints; 438 } 439 } else { 440 if (constraintsBuffer != null) { 441 constraints = constraintsBuffer.toString() ; 442 } else { 443 constraints = null; 444 } 445 } 446 } 447 searchDir = configurator.getParam("searchdir", searchDir); 448 directions = configurator.getParam("directions", directions); 449 orderBy = configurator.getParam("orderby", orderBy); 450 distinct = configurator.getParam("distinct", distinct); 451 452 if (!parsed) { 454 String defaultTemplate = Config.listStyleSheet; 455 if ("search".equals(configurator.getParam("listtype"))) { 456 defaultTemplate =Config.searchlistStyleSheet; 457 } 458 String templatePath = configurator.getParam("template", defaultTemplate); 459 try { 460 template = configurator.resolveToURL(templatePath); 461 } catch (Exception e) { 462 throw new WizardException(e); 463 } 464 465 mainObjectName = configurator.getParam("main", (String ) null); 468 boolean mainPresent = mainObjectName != null; 469 470 StringTokenizer stok = new StringTokenizer(nodePath, ","); 472 int nodecount = stok.countTokens(); 473 if (nodecount == 0) { 474 throw new WizardException("The parameter 'nodepath' should be passed with a comma-separated list of nodemanagers."); 475 } 476 multilevel = nodecount > 1; 477 if (mainObjectName == null) { 478 while (stok.hasMoreTokens()) { 480 mainObjectName = stok.nextToken(); 481 } 482 } 483 485 if (fields == null) { 487 if (cloud != null) { 488 StringBuffer fieldsBuffer = new StringBuffer (); 489 FieldIterator i = cloud.getNodeManager(removeDigits(mainObjectName)).getFields(org.mmbase.bridge.NodeManager.ORDER_LIST).fieldIterator(); 490 while (i.hasNext()) { 491 Field field = i.nextField(); 492 if (multilevel && field.isVirtual()) { 493 continue; 497 } 498 fieldsBuffer.append(multilevel ? mainObjectName + "." : "" ).append(field.getName()); 499 if (i.hasNext()) fieldsBuffer.append(','); 500 } 501 fields = fieldsBuffer.toString(); 502 } else { 503 throw new WizardException("The parameter 'fields' is required but not given (or make sure there is a cloud)"); 505 } 506 } 507 508 stok = new StringTokenizer(fields, ","); 510 if (stok.countTokens() == 0) { 511 throw new WizardException("The parameter 'fields' should be passed with a comma-separated list of fieldnames."); 512 } 513 514 fieldList = new ArrayList(); 515 while (stok.hasMoreTokens()) { 516 String token = stok.nextToken(); 517 fieldList.add(token); 518 521 526 528 if (! mainPresent && token.endsWith(".number")) { 529 mainObjectName = token.substring(0, token.length() - 7); 530 mainPresent = true; 531 } 533 } 534 535 if (search >= SEARCH_YES && searchFields == null) { 536 if (cloud != null) { 537 StringBuffer searchFieldsBuffer = new StringBuffer (); 538 FieldIterator i = cloud.getNodeManager(removeDigits(mainObjectName)). 539 getFields(org.mmbase.bridge.NodeManager.ORDER_LIST).fieldIterator(); 540 while (i.hasNext()) { 541 Field f = i.nextField(); 542 if (f.getType() == Field.TYPE_STRING && ! f.getName().equals("owner")) { 543 if (searchFieldsBuffer.length() > 0) searchFieldsBuffer.append(','); 544 searchFieldsBuffer.append(multilevel ? mainObjectName + "." : "" ).append(f.getName()); 545 } 546 } 547 searchFields = searchFieldsBuffer.toString(); 548 549 } else { 550 throw new WizardException("Cannot auto-determin search-fields without a cloud (use a newer list.jsp"); 552 } 553 } 554 555 if (search == SEARCH_NO && searchFields != null) { 556 log.debug("Using searchfields and explicitiy no search"); 557 searchFields = null; 558 } 559 560 String numberField = "number"; 563 if (multilevel) { 564 numberField = mainObjectName + ".number"; 565 } 566 if (fieldList.indexOf(numberField) == -1) { 567 fields = numberField + "," + fields; 568 } 569 parsed = true; 570 } 571 572 } 573 574 577 public Map getAttributes() { 578 Map attributeMap = super.getAttributes(); 579 attributeMap.put("nodepath", nodePath); 581 attributeMap.put("fields", fields); 582 if (title != null) attributeMap.put("title", title); 584 attributeMap.put("age", age+""); 585 if (multilevel) attributeMap.put("objecttype",mainObjectName); 586 if (startNodes!=null) attributeMap.put("startnodes", startNodes); 587 if (orderBy!=null) attributeMap.put("orderby", orderBy); 588 if (directions!=null) attributeMap.put("directions", directions); 589 attributeMap.put("distinct", distinct+""); 590 if (searchDir!=null) attributeMap.put("searchdir", searchDir); 591 if (baseConstraints!=null) attributeMap.put("constraints", baseConstraints); 592 if (searchType!=null) attributeMap.put("searchtype", searchType); 594 if (searchFields!=null) attributeMap.put("searchfields", searchFields); 595 if (realSearchField!=null) attributeMap.put("realsearchfield", realSearchField); 596 if (searchValue!=null) attributeMap.put("searchvalue", searchValue); 597 598 return attributeMap; 599 } 600 } 601 602 607 public static class Configurator { 608 private static final Logger log = Logging.getLoggerInstance(Config.class); 609 610 protected PageContext page; 611 protected HttpServletRequest request; 612 protected HttpServletResponse response; 613 private Config config; 614 615 public Configurator(PageContext pageContext, Config c) throws WizardException, java.net.MalformedURLException { 616 page = pageContext; 617 request = (HttpServletRequest )page.getRequest(); 618 response = (HttpServletResponse )page.getResponse(); 619 config = c; 620 621 config.sessionId = response.encodeURL("test.jsp").substring(8); 622 if (log.isDebugEnabled()) { 623 log.debug("Sessionid : " + config.sessionId); 624 } 625 626 if (config.language == null) { 627 config.language = getParam("language", org.mmbase.bridge.ContextProvider.getDefaultCloudContext().getDefaultLocale().getLanguage()); 628 } 629 630 if (config.timezone == null) { 631 config.timezone = getParam("timezone", ""); 632 } 633 634 640 if (config.attributes == null) { 641 config.attributes = new HashMap(); 642 fillAttributes(config.attributes); 643 } 644 if (config.backPage == null) { 648 log.debug("No backpage. Getting from parameters"); 649 config.backPage = org.mmbase.util.Encode.decode("ESCAPE_URL_PARAM", getParam("referrer", "")).replace('\\', '/'); 651 if (config.backPage.equals("")) { 652 log.debug("No backpage getting from header"); 653 config.backPage = request.getHeader("Referer"); 654 } 655 if (config.backPage == null) { 656 log.debug("No backpage setting to ''"); 657 config.backPage = ""; 658 } 659 } 660 661 if (config.uriResolver == null) { 664 if (log.isDebugEnabled()) { 665 log.trace("creating uriresolver (backpage = " + config.backPage + ")"); 666 } 667 URIResolver.EntryList extraDirs = new URIResolver.EntryList(); 668 669 672 URL ref; 673 int protocolPos= config.backPage.indexOf(PROTOCOL); 675 676 677 678 679 if (protocolPos >=0 ) { String path = new URL(config.backPage).getPath(); 681 ref = new URL(getResource(path.substring(request.getContextPath().length())), "."); 682 } else { 684 String bp = config.backPage; 686 int questionPos = bp.indexOf('?'); 687 if (questionPos != -1) { 688 bp = bp.substring(0, questionPos); 689 } 690 URL path = getResource(bp); 691 692 if (path != null) { 693 ref = new URL(path, "."); 694 } else { 695 ref = null; 696 } 697 } 698 if (ref != null) { 699 if (! config.language.equals("")) { 700 URL refi18n = new URL(ref, "i18n/" + config.language); 701 if (getResource(refi18n.getPath()) != null) { 702 extraDirs.add("refi18n:", refi18n); 703 } 704 } 705 extraDirs.add("ref:", ref); 706 } else { 707 log.warn("" + ref + " does not exist"); 708 } 709 710 713 config.templates = request.getParameter("templates"); 714 715 if (config.templates != null) { 716 URL templatesDir = getResource(config.templates); 717 if (templatesDir == null) { 718 throw new WizardException("" + config.templates + " does not exist"); 719 } 720 extraDirs.add("templates:", templatesDir); 721 } 722 723 727 728 URL jspFileDir = new URL(getResource(request.getServletPath()), "."); URL basedir = new URL(jspFileDir, "../data/"); 731 if (! config.language.equals("")) { 732 URL i18n = new URL(basedir, "i18n/" + config.language + "/"); 733 if (i18n == null) { 734 if (! "en".equals(config.language)) { log.warn("Tried to internationalize the editwizard for language " + config.language + " for which support is lacking (" + i18n + " is not an existing directory)"); 736 } 737 } else { 738 extraDirs.add("i18n:", i18n); 739 } 740 } 741 742 extraDirs.add("ew:", basedir); 743 config.uriResolver = new URIResolver(jspFileDir, extraDirs); 744 config.maxupload = getParam("maxsize", config.maxupload); 745 } 746 } 747 748 753 public URL getResource(String path) throws MalformedURLException { 754 return ResourceLoader.getWebRoot().getResource(path); 755 } 757 758 759 public URL resolveToURL(String templatePath) throws javax.xml.transform.TransformerException { 760 return config.uriResolver.resolveToURL(templatePath, null); 761 } 762 763 764 public PageContext getPage() { 765 return page; 766 } 767 768 protected String getParam(String paramName) { 769 return request.getParameter(paramName); 770 } 771 772 protected String getParam(String paramName, String defaultValue) { 773 String value = getParam(paramName); 774 if (value == null) value = defaultValue; 775 return value; 776 } 777 778 protected int getParam(String paramName, int def) { 779 String i = getParam(paramName); 780 if (i == null || i.equals("")) return def; 781 return new Integer (i).intValue(); 782 } 783 784 protected Integer getParam(String paramName, Integer def) { 785 String i = getParam(paramName); 786 if (i == null || i.equals("")) return def; 787 return new Integer (i); 788 } 789 790 protected boolean getParam(String paramName, boolean def) { 791 String b = getParam(paramName); 792 if (b == null) return def; 793 return Boolean.valueOf(b).booleanValue(); 794 } 795 796 protected Boolean getParam(String paramName, Boolean def) { 797 String b = getParam(paramName); 798 if (b == null) return def; 799 return Boolean.valueOf(b); 800 } 801 802 807 protected void fillAttributes(Map map) { 808 map.putAll(config.attributes); 810 Enumeration e = request.getParameterNames(); 811 while (e.hasMoreElements()) { 812 String param = (String ) e.nextElement(); 813 map.put(param, request.getParameter(param)); 814 } 815 816 } 819 820 public String getBackPage(){ 821 if(config.subObjects.size() == 0) { 822 return config.backPage; 823 } else { 824 return ((SubConfig) config.subObjects.peek()).page; 825 } 826 } 827 828 829 public ListConfig createList(Cloud cloud) { 830 ListConfig l = new ListConfig(cloud); 831 l.page = response.encodeURL(request.getServletPath() + "?proceed=yes"); 832 return l; 833 } 834 835 838 public ListConfig createList() { 839 return createList(null); 840 } 841 842 public Config.WizardConfig createWizard(Cloud cloud) throws WizardException { 843 WizardConfig wizard = new WizardConfig(); 844 wizard.page = response.encodeURL(request.getServletPath() + "?proceed=yes"); 845 config(wizard); if (wizard.wizard == null) { 848 throw new WizardException("Wizardname may not be null, configurated by class with name: " + this.getClass().getName()); 849 } 850 wizard.wiz = new Wizard(request.getContextPath(), config.uriResolver, wizard, cloud); 851 wizard.wiz.setSessionId(config.sessionId); 852 wizard.wiz.setSessionKey(config.sessionKey); 853 wizard.wiz.setReferrer(config.backPage); 854 wizard.wiz.setTemplatesDir(config.templates); 855 wizard.wiz.setTimezone(config.timezone); 856 return wizard; 857 } 858 859 865 public void config(Config.SubConfig config) throws WizardException { 866 config.configure(this); 867 } 868 869 } 870 871 } 872 | Popular Tags |