1 package org.jahia.clipbuilder.html.bean; 2 3 import java.net.*; 4 import java.util.*; 5 6 import org.jahia.clipbuilder.html.util.*; 7 import org.jahia.clipbuilder.html.web.Constant.*; 8 import org.jahia.clipbuilder.html.web.html.*; 9 10 15 public class UrlBean extends Bean { 16 private ClipperBean clipperBean; 17 private String relativeUrl; 18 private URL baseURL; 19 private URL redirectUrl; 20 21 private HTMLDocument document; 22 private int position; 23 private Hashtable formHasthable = new Hashtable(); 24 private List queryParamBeanList = new ArrayList(); 25 private String from = ""; 26 private String hash = ""; 27 private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(UrlBean.class); 28 29 30 40 public UrlBean(ClipperBean cBean, URL baseURL, String relativeUrl, Map queryParamMap, String from, String hash) { 41 setBaseURL(baseURL); 42 setRelativeUrl(relativeUrl); 43 setRedirectUrl(baseURL); 44 setClipperBean(cBean); 45 setQueryParamListFromMap(queryParamMap); 46 setFrom(from); 47 setHash(hash); 48 } 49 50 51 61 public UrlBean(ClipperBean cBean, String baseURL, String relativeUrl, Map queryParamMap, String from, String hash) { 62 this(cBean, URLUtilities.getURL(baseURL), relativeUrl, queryParamMap, from, hash); 63 64 } 65 66 67 68 73 public void setPosition(int position) { 74 this.position = position; 75 } 76 77 78 83 public void setDocument(HTMLDocument document) { 84 this.document = document; 85 } 86 87 88 93 public void setQueryParamBeanList(List queryParamBeanList) { 94 this.queryParamBeanList = queryParamBeanList; 95 } 96 97 98 103 public void setClipperBean(ClipperBean clipperBean) { 104 this.clipperBean = clipperBean; 105 } 106 107 108 113 public void setRedirectUrl(URL redirectUrl) { 114 this.redirectUrl = redirectUrl; 115 } 116 117 118 123 public void setFrom(String from) { 124 this.from = from; 125 } 126 127 128 133 public void setHash(String hash) { 134 this.hash = hash; 135 } 136 137 138 143 public void setRelativeUrl(String relativeUrl) { 144 this.relativeUrl = relativeUrl; 145 } 146 147 148 153 public void setBaseURL(URL url) { 154 this.baseURL = url; 155 } 156 157 158 168 public void setMappingFormParam(String formParentName, String formParentId, int formParentPos, String name, String mapping, int paramPosition) { 169 getFormParamBeanByNameAndFormParentNameAndFormParentId(formParentName, formParentId, formParentPos, name, paramPosition).setMapping(mapping); 170 } 171 172 173 178 public void setQueryParamListFromMap(Map paramDefaultValue) { 179 Iterator i = paramDefaultValue.keySet().iterator(); 180 181 184 while (i.hasNext()) { 185 String name = (String ) i.next(); 186 String defaultValueNameTab[] = ((String []) paramDefaultValue.get(name)); 187 for (int queryParamPos = 0; queryParamPos < defaultValueNameTab.length; queryParamPos++) { 188 String defaultValue = ((String []) paramDefaultValue.get(name))[queryParamPos]; 189 addQueryParameter(name, defaultValue, queryParamPos); 190 logger.debug("[Parameter whith name=" + name + " setted whith default value " + defaultValue + " ]"); 191 } 192 } 193 } 194 195 196 201 public void setBaseUrl(String url) { 202 this.setBaseURL(URLUtilities.getURL(url)); 203 } 204 205 206 207 220 public FormParamBean setFormParameterValue(String formParentName, String formParentId, int formParentPos, String param, String possibleValue, String type, String visibility, int paramPosition) { 221 FormParamBean bean = getFormParamBeanByNameAndFormParentNameAndFormParentId(formParentName, formParentId, formParentPos, param, paramPosition); 222 if (bean != null) { 223 logger.debug("[ set used value for " + param + ": " + possibleValue + "]"); 224 bean.setUsedValue(possibleValue); 225 } 226 else { 227 logger.warn("[ FormBean not found " + param + "," + formParentName + "," + type + " ]"); 228 logger.warn("[ If it's the test mode, it's an ERROR !!!!!]"); 229 } 230 return bean; 231 } 232 233 234 239 public String getBaseUrlValue() { 240 return getBaseURL().toExternalForm(); 241 } 242 243 244 249 public URL getBaseURL() { 250 return this.baseURL; 251 } 252 253 254 259 public Map getQueryParamsMap() { 260 Map queryParams = new TreeMap(); 261 for (int j = 0; j < this.getQueryParamBeanList().size(); j++) { 262 QueryParamBean qBean = (QueryParamBean) getQueryParamBeanList().get(j); 263 String name = qBean.getName(); 264 String [] value = {qBean.getDefaultValue()}; 265 queryParams.put(name, value); 266 } 267 return queryParams; 268 } 269 270 271 276 public String getAbsoluteUrlValue() { 277 return getAbsoluteURL().toExternalForm(); 278 } 279 280 281 282 288 public List getFormParamBeanListByFormParentHash(String formParentHash) { 289 List l = (List) formHasthable.get(formParentHash); 290 if (l == null) { 291 Iterator key = formHasthable.keySet().iterator(); 292 while (key.hasNext()) { 293 String value = key.next().toString(); 294 logger.debug("Found form hash: " + value); 295 } 296 297 l = new ArrayList(); 298 formHasthable.put(formParentHash, l); 299 } 300 return l; 301 } 302 303 304 305 310 public List getFormParamBeanList() { 311 Iterator it = formHasthable.values().iterator(); 312 ArrayList allFormParam = new ArrayList(); 313 while (it.hasNext()) { 314 List l = (List) it.next(); 315 for (int j = 0; j < l.size(); j++) { 316 allFormParam.add(l.get(j)); 317 } 318 319 } 320 return allFormParam; 321 } 322 323 324 325 330 public List getQueryParamBeanList() { 331 return queryParamBeanList; 332 } 333 334 335 340 public int getPosition() { 341 return position; 342 } 343 344 345 350 public HTMLDocument getDocument() { 351 return document; 352 } 353 354 355 360 public ClipperBean getClipperBean() { 361 return clipperBean; 362 } 363 364 365 370 public URL getRedirectUrl() { 371 return redirectUrl; 372 } 373 374 375 380 public String getFrom() { 381 return from; 382 } 383 384 385 390 public String getLabelForStrut() { 391 return getAbsoluteUrlValue(); 393 } 394 395 396 397 402 public String getHash() { 403 return hash; 404 } 405 406 407 412 public String getRelativeUrlValue() { 413 return relativeUrl; 414 } 415 416 417 422 public URL getAbsoluteURL() { 423 URL u = null; 424 String relativeUrl = getRelativeUrlValue(); 425 if(relativeUrl == null){ 426 logger.error("Relative url is null !!"); 427 } 428 try { 429 if (URLUtilities.isRelatif(getRelativeUrlValue())) { 430 u = URLUtilities.getAbsolutURL(getRedirectUrl(),getRelativeUrlValue()); 431 } 432 else { 433 u = URLUtilities.getURL(getRelativeUrlValue()); 434 } 435 } 436 catch (MalformedURLException ex) { 437 ex.printStackTrace(); 438 logger.error("Erro: Malformed URL"); 439 } 440 finally { 441 return u; 442 } 443 } 444 445 446 447 457 public ArrayList getParamFormAllowedValueBeanList(String formParentName, String formParentId, int formParentPos, String paramName, int paramPosition) { 458 return this.getFormParamBeanByNameAndFormParentNameAndFormParentId(formParentName, formParentId, formParentPos, paramName, paramPosition).getListPossibleBeanValue(); 459 } 460 461 462 472 public ArrayList getParamFormAllowedValueStringList(String formParentName, String formParentId, int formParentPos, String paramName, int paramPosition) { 473 return this.getFormParamBeanByNameAndFormParentNameAndFormParentId(formParentName, formParentId, formParentPos, paramName, paramPosition).getListPossibleStringValue(); 474 } 475 476 477 487 public String getFormParameterMapping(String formParentName, String formParentId, int formParentPos, String paramName, int paramPosition) { 488 FormParamBean bean = getFormParamBeanByNameAndFormParentNameAndFormParentId(formParentName, formParentId, formParentPos, paramName, paramPosition); 489 return bean.getMapping(); 490 } 491 492 493 499 public String getQueryParamDefaultValue(String param) { 500 logger.debug("[url " + getAbsoluteUrlValue() + ", queryParam: " + param + " ]"); 501 QueryParamBean bean = getQueryParamBean(param); 502 return bean.getDefaultValue(); 503 } 504 505 506 516 public String getFormParamType(String formParentName, String formParentId, int formParentPos, String paramName, int paramPosition) { 517 FormParamBean bean = getFormParamBeanByNameAndFormParentNameAndFormParentId(formParentName, formParentId, formParentPos, paramName, paramPosition); 518 return bean.getType(); 519 } 520 521 522 527 public String getFirtParamName() { 528 String formName = (String ) formHasthable.keySet().iterator().next(); 529 if (getFormParamBeanListByFormParentHash(formName).isEmpty()) { 530 return "No param"; 531 } 532 else { 533 return ((FormParamBean) getFormParamBeanListByFormParentHash(formName).get(0)).getName(); 534 } 535 } 536 537 538 539 546 public FormParamBean getFormParamBeanByMapping(String formParentName, String mapping) { 547 List a = getFormParamBeanListByFormParentHash(formParentName); 549 FormParamBean paramAndValue = null; 550 for (int i = 0; i < a.size(); i++) { 551 paramAndValue = (FormParamBean) a.get(i); 552 if (paramAndValue.getMapping().equalsIgnoreCase(mapping)) { 553 return paramAndValue; 554 } 555 } 556 logger.debug("[Form param Bean " + mapping + " not found]"); 557 return null; 558 } 559 560 561 571 public FormParamBean getFormParamBeanByNameAndFormParentNameAndFormParentId(String formParentName, String formParentId, int formParentPos, String name, int paramPosition) { 572 String formHash = HashUtilities.buildFormHash(formParentName, formParentId, formParentPos); 574 return getFormParamByNameAndFormParentHash(name, paramPosition, formHash); 575 } 576 577 578 586 public FormParamBean getFormParamByNameAndFormParentHash(String name, int paramPosition, String formHash) { 587 List a = getFormParamBeanListByFormParentHash(formHash); 588 if (a == null) { 589 logger.error("Form param List not found for form parent: " + formHash); 590 } 591 FormParamBean currentFormParam = null; 592 for (int i = 0; i < a.size(); i++) { 593 currentFormParam = (FormParamBean) a.get(i); 594 if (currentFormParam.getPosition() == paramPosition) { 595 if (currentFormParam.getName().equalsIgnoreCase(name)) { 596 return currentFormParam; 597 } 598 } 599 } 600 logger.warn("[Form param Bean " + name + " not found: name=" + name + "," + "formHash=" + formHash + "]"); 601 return null; 602 } 603 604 605 611 public FormParamBean getSubmittedParamBean(String formParentHash) { 612 List allFormParamBean = getFormParamBeanListByFormParentHash(formParentHash); 613 for (int i = 0; i < allFormParamBean.size(); i++) { 614 FormParamBean fBean = (FormParamBean) allFormParamBean.get(i); 615 String type = fBean.getType(); 616 QueryParamBean qBean = fBean.getQueryParamBean(); 618 if (qBean != null) { 619 if (type.equalsIgnoreCase(WebConstants.TYPE_SUBMIT) || type.equalsIgnoreCase(WebConstants.TYPE_IMAGE) || type.equalsIgnoreCase(WebConstants.TYPE_RESET) || type.equalsIgnoreCase(WebConstants.TYPE_BUTTON)) { 620 return fBean; 621 } 622 } 623 } 624 return null; 625 } 626 627 628 634 public String getNameInputSubmit(String fromHash) { 635 FormParamBean fBean = getSubmittedParamBean(fromHash); 636 if (fBean != null) { 637 logger.debug("Submit input found: " + fBean.getName()); 638 return fBean.getName(); 639 } 640 else { 641 logger.warn("Submit input nor found"); 642 return null; 643 } 644 } 645 646 647 650 public void clearParam() { 651 logger.debug("[ Clear param list ]"); 652 656 } 657 658 659 682 public FormParamBean addFormParameter(String formParentName, String formParentId, int formParentPosition, String paramName, String possibleValue, String type, String visibility, boolean editable, int position) { 683 String formParentHah = HashUtilities.buildFormHash(formParentName, formParentId, formParentPosition); 684 685 int pos = 0; 687 FormParamBean fBean = getFormParamBeanByNameAndFormParentNameAndFormParentId(formParentName, formParentId, formParentPosition, paramName, pos); 688 if (fBean != null && (type.equalsIgnoreCase(WebConstants.TYPE_RADIO) || type.equalsIgnoreCase(WebConstants.TYPE_SELECT))) { 689 logger.debug("[ Add possible value for " + paramName + " ]"); 690 if (!editable) { 691 logger.warn("[ Already recorded ]"); 692 } 693 fBean.addPossibleValue(possibleValue); 694 return fBean; 695 } 696 697 while (fBean != null) { 699 pos++; 700 fBean = getFormParamBeanByNameAndFormParentNameAndFormParentId(formParentName, formParentId, formParentPosition, paramName, pos); 701 } 702 fBean = new FormParamBean(this, formParentName, formParentId, formParentPosition, paramName, type, possibleValue, visibility, pos); 704 getFormParamBeanListByFormParentHash(formParentHah).add(fBean); 705 706 return fBean; 707 } 708 709 710 711 732 public FormParamBean addFormParameter(String formParentName, String formParentId, int formParentPosition, String param, String possibleValue, String type, String visibility, int paramPosition) { 733 return addFormParameter(formParentName, formParentId, formParentPosition, param, possibleValue, type, visibility, true, paramPosition); 734 } 735 736 737 748 public QueryParamBean addQueryParameter(String param, String defaultValue, int position) { 749 QueryParamBean bean = new QueryParamBean(this, param, defaultValue, position); 750 getQueryParamBeanList().add(bean); 751 return bean; 752 } 753 754 755 765 public boolean formParamExist(String formParentName, String formParentId, int formParentPos, String paramName, int paramPosition) { 766 return (getFormParamBeanByNameAndFormParentNameAndFormParentId(formParentName, formParentId, formParentPos, paramName, paramPosition) != null); 767 } 768 769 770 776 private QueryParamBean getQueryParamBean(String param) { 777 List a = this.getQueryParamBeanList(); 779 QueryParamBean paramAndValue = null; 780 for (int i = 0; i < a.size(); i++) { 781 paramAndValue = (QueryParamBean) a.get(i); 782 if (paramAndValue.getName().equalsIgnoreCase(param)) { 783 return paramAndValue; 784 } 785 } 786 logger.debug("[Query param Bean " + param + " not found]"); 787 return null; 788 } 789 790 } 791 | Popular Tags |