001 /** 002 * Copyright 2005-2014 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.rice.kns.lookup; 017 018 import org.apache.commons.lang.StringUtils; 019 import org.kuali.rice.core.api.config.property.ConfigContext; 020 import org.kuali.rice.core.web.format.DateFormatter; 021 import org.kuali.rice.kns.document.authorization.BusinessObjectRestrictions; 022 import org.kuali.rice.kns.document.authorization.FieldRestriction; 023 import org.kuali.rice.kns.service.BusinessObjectAuthorizationService; 024 import org.kuali.rice.kns.service.KNSServiceLocator; 025 import org.kuali.rice.kns.util.KNSConstants; 026 import org.kuali.rice.krad.service.KRADServiceLocatorWeb; 027 import org.kuali.rice.krad.util.KRADConstants; 028 import org.kuali.rice.krad.util.ObjectUtils; 029 030 import java.io.Serializable; 031 import java.sql.Date; 032 import java.util.HashMap; 033 import java.util.Iterator; 034 import java.util.List; 035 import java.util.Map; 036 037 /** 038 * This class holds details of html data for an action url. 039 * 040 * @author Kuali Rice Team (rice.collab@kuali.org) 041 * 042 */ 043 @Deprecated 044 public abstract class HtmlData implements Serializable { 045 046 protected static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(HtmlData.class); 047 048 public static final String ANCHOR_HTML_DATA_TYPE = AnchorHtmlData.class.getName(); 049 public static final String INPUT_HTML_DATA_TYPE = InputHtmlData.class.getName(); 050 051 protected String name = ""; 052 protected String title = ""; 053 protected String methodToCall = ""; 054 protected String displayText = ""; 055 protected String prependDisplayText = ""; 056 protected String appendDisplayText = ""; 057 protected List<HtmlData> childUrlDataList; 058 protected String maxLength; 059 060 /** 061 * 062 * This method constructs the complete html tag based on the class attribute 063 * values. 064 * 065 * @return 066 */ 067 public abstract String constructCompleteHtmlTag(); 068 069 /** 070 * @return the appendDisplayText 071 */ 072 public String getAppendDisplayText() { 073 return this.appendDisplayText; 074 } 075 076 /** 077 * @param appendDisplayText the appendDisplayText to set 078 */ 079 public void setAppendDisplayText(String appendDisplayText) { 080 this.appendDisplayText = appendDisplayText; 081 } 082 083 /** 084 * @return the childUrlDataList 085 */ 086 public List<HtmlData> getChildUrlDataList() { 087 return this.childUrlDataList; 088 } 089 090 /** 091 * @param childUrlDataList the childUrlDataList to set 092 */ 093 public void setChildUrlDataList(List<HtmlData> childUrlDataList) { 094 this.childUrlDataList = childUrlDataList; 095 } 096 097 /** 098 * @return the prependDisplayText 099 */ 100 public String getPrependDisplayText() { 101 return this.prependDisplayText; 102 } 103 104 /** 105 * @param prependDisplayText the prependDisplayText to set 106 */ 107 public void setPrependDisplayText(String prependDisplayText) { 108 this.prependDisplayText = prependDisplayText; 109 } 110 111 /** 112 * @return the title 113 */ 114 public String getTitle() { 115 return this.title; 116 } 117 118 /** 119 * @param title the title to set 120 */ 121 public void setTitle(String title) { 122 this.title = title; 123 } 124 125 /** 126 * @return the name 127 */ 128 public String getName() { 129 return this.name; 130 } 131 132 /** 133 * @param name the name to set 134 */ 135 public void setName(String name) { 136 this.name = name; 137 } 138 139 /** 140 * @return the displayText 141 */ 142 public String getDisplayText() { 143 return this.displayText; 144 } 145 146 /** 147 * @param displayText the displayText to set 148 */ 149 public void setDisplayText(String displayText) { 150 this.displayText = displayText; 151 } 152 153 /** 154 * @return the methodToCall 155 */ 156 public String getMethodToCall() { 157 return this.methodToCall; 158 } 159 160 /** 161 * @param methodToCall the methodToCall to set 162 */ 163 public void setMethodToCall(String methodToCall) { 164 this.methodToCall = methodToCall; 165 } 166 167 public String getTitle(String prependText, Class bo, List keys) { 168 return KRADConstants.EMPTY_STRING; 169 } 170 171 /** 172 * KFSMI-658 This method gets the title text for a link/control 173 * 174 * @param prependText 175 * @param dataObject 176 * @param fieldConversions 177 * @param returnKeys 178 * @return title text 179 */ 180 public static String getTitleText(String prependText, Object dataObject, List<String> keys, BusinessObjectRestrictions businessObjectRestrictions) { 181 if (dataObject == null) 182 return KRADConstants.EMPTY_STRING; 183 184 Map<String, String> keyValueMap = new HashMap<String, String>(); 185 Iterator keysIt = keys.iterator(); 186 while (keysIt.hasNext()) { 187 String fieldNm = (String) keysIt.next(); 188 Object fieldVal = ObjectUtils.getPropertyValue(dataObject, fieldNm); 189 190 FieldRestriction fieldRestriction = null; 191 if (businessObjectRestrictions != null) { 192 fieldRestriction = businessObjectRestrictions.getFieldRestriction(fieldNm); 193 } 194 195 if (KNSServiceLocator.getDataDictionaryService().getAttributeDefinition(dataObject.getClass().getName(), fieldNm) == null) { 196 fieldVal = KRADConstants.EMPTY_STRING; 197 } else if (fieldRestriction != null && (fieldRestriction.isMasked() || fieldRestriction.isPartiallyMasked())) { 198 fieldVal = fieldRestriction.getMaskFormatter().maskValue(fieldVal); 199 } else if (fieldVal == null) { 200 fieldVal = KRADConstants.EMPTY_STRING; 201 } else if (fieldVal instanceof Date) { 202 // need to format date in url 203 DateFormatter dateFormatter = new DateFormatter(); 204 fieldVal = dateFormatter.format(fieldVal); 205 } 206 keyValueMap.put(fieldNm, fieldVal.toString()); 207 } 208 return getTitleText(prependText, dataObject.getClass(), keyValueMap); 209 } 210 211 private static BusinessObjectAuthorizationService businessObjectAuthorizationService; 212 private static BusinessObjectAuthorizationService getBusinessObjectAuthorizationService() { 213 if (businessObjectAuthorizationService == null) { 214 businessObjectAuthorizationService = KNSServiceLocator.getBusinessObjectAuthorizationService(); 215 } 216 return businessObjectAuthorizationService; 217 } 218 219 public static String getTitleText(String prependText, Class<?> dataObjectClass, Map<String, String> keyValueMap) { 220 StringBuffer titleText = new StringBuffer(prependText); 221 for (String key : keyValueMap.keySet()) { 222 String fieldVal = keyValueMap.get(key).toString(); 223 224 if (StringUtils.isNotBlank(fieldVal)) { 225 titleText.append(KRADServiceLocatorWeb.getDataDictionaryService() 226 .getAttributeLabel(dataObjectClass, key) 227 + "=" + fieldVal.toString() + " "); 228 } 229 } 230 return titleText.toString(); 231 } 232 233 /** 234 * 235 * This class is an extension of HtmlData. It represents an anchor tag. 236 * 237 * @author Kuali Rice Team (rice.collab@kuali.org) 238 * 239 */ 240 public static class AnchorHtmlData extends HtmlData { 241 public static final String TARGET_BLANK = "_blank"; 242 protected String href = ""; 243 protected String target = ""; 244 protected String style = ""; 245 protected String styleClass =""; 246 protected String onclick =""; 247 248 /** 249 * Needed by inquiry framework 250 */ 251 public AnchorHtmlData() { 252 } 253 254 public AnchorHtmlData(String href, String title) { 255 this.href = href; 256 this.title = title; 257 } 258 259 public AnchorHtmlData(String href, String methodToCall, 260 String displayText) { 261 this.href = href; 262 this.methodToCall = methodToCall; 263 this.displayText = displayText; 264 } 265 266 /** 267 * @param href the href to set 268 */ 269 public void setHref(String href) { 270 this.href = href; 271 } 272 273 /** 274 * 275 * This method generates anchor tag. 276 * 277 * @see HtmlData#constructCompleteHtmlTag() 278 */ 279 public String constructCompleteHtmlTag() { 280 String completeHtmlTag; 281 if (StringUtils.isEmpty(getHref())) 282 completeHtmlTag = getDisplayText(); 283 else 284 completeHtmlTag = getPrependDisplayText() 285 + "<a title=\"" 286 + title 287 + "\"" 288 + " href=\"" 289 + getHref() 290 + "\"" 291 + getStyle() 292 + " " 293 + getStyleClass() 294 + " " 295 + (StringUtils.isEmpty(getOnclick()) ? "" : " onClick=\"" 296 + getOnclick() + "\" ") 297 + (StringUtils.isEmpty(getTarget()) ? "" : " target=\"" 298 + getTarget() + "\" ") + ">" + getDisplayText() 299 + "</a>" + getAppendDisplayText(); 300 return completeHtmlTag; 301 } 302 303 /** 304 * @return the target 305 */ 306 public String getTarget() { 307 return this.target; 308 } 309 310 /** 311 * @param target 312 * the target to set 313 */ 314 public void setTarget(String target) { 315 this.target = target; 316 } 317 318 /** 319 * @return the style 320 */ 321 public String getStyle() { 322 return this.style; 323 } 324 325 /** 326 * @param style the style to set 327 */ 328 public void setStyle(String style) { 329 this.style = style; 330 } 331 332 /** 333 * @return the styleClass 334 */ 335 public String getStyleClass() { 336 return this.styleClass; 337 } 338 339 /** 340 * @param styleClass the styleClass to set 341 */ 342 public void setStyleClass(String styleClass) { 343 this.styleClass = styleClass; 344 } 345 346 /** 347 * @return the onclick 348 */ 349 public String getOnclick() { 350 return this.onclick; 351 } 352 353 /** 354 * @param onclick the onclick to set 355 */ 356 public void setOnclick(String onclick) { 357 this.onclick = onclick; 358 } 359 360 /** 361 * @return the href 362 */ 363 public String getHref() { 364 return this.href; 365 } 366 367 /** 368 * @return the methodToCall 369 */ 370 public String getMethodToCall() { 371 return this.methodToCall; 372 } 373 374 } 375 376 /** 377 * 378 * This class is an extension of HtmlData. It represents an input tag. 379 * 380 * @author Kuali Rice Team (rice.collab@kuali.org) 381 * 382 */ 383 public static class InputHtmlData extends HtmlData { 384 public static final String CHECKBOX_INPUT_TYPE = "checkbox"; 385 public static final String CHECKBOX_CHECKED_VALUE = "checked"; 386 387 protected String inputType = ""; 388 protected String src = ""; 389 protected String styleClass = ""; 390 protected String border = "0"; 391 protected String checked = ""; 392 protected String value = ""; 393 394 public InputHtmlData(String name, String inputType) { 395 this.name = name; 396 this.inputType = inputType; 397 } 398 399 public InputHtmlData(String name, String inputType, String src) { 400 this.name = name; 401 this.inputType = inputType; 402 this.src = src; 403 } 404 405 /*********************************************************************** 406 * 407 * This method contructs an input tag. 408 * 409 * @see HtmlData#constructCompleteHtmlTag() 410 */ 411 public String constructCompleteHtmlTag() { 412 return getPrependDisplayText() 413 + "<input title=\"" 414 + title 415 + "\"" 416 + " name=\"" 417 + getName() 418 + "\"" 419 + (StringUtils.isEmpty(src) ? "" 420 : " src=\"" + src + "\" ") 421 + " type=\"" 422 + getInputType() 423 + "\"" 424 + (StringUtils.isEmpty(value) ? "" 425 : " value=\"" + value + "\" ") 426 + (StringUtils.isEmpty(checked) ? "" 427 : " checked=\"" + checked + "\" ") 428 + (StringUtils.isEmpty(getStyleClass()) ? "" 429 : " styleClass=\"" + getStyleClass() + "\" ") 430 + " border=\"" + getBorder() + "\"" + " value=\"" 431 + getDisplayText() + "\"" + "/>" + getAppendDisplayText(); 432 } 433 434 /** 435 * @return the inputType 436 */ 437 public String getInputType() { 438 return this.inputType; 439 } 440 441 /** 442 * @return the src 443 */ 444 public String getSrc() { 445 return this.src; 446 } 447 448 /** 449 * @return the border 450 */ 451 public String getBorder() { 452 return this.border; 453 } 454 455 /** 456 * @param border 457 * the border to set 458 */ 459 public void setBorder(String border) { 460 this.border = border; 461 } 462 463 /** 464 * @return the styleClass 465 */ 466 public String getStyleClass() { 467 return this.styleClass; 468 } 469 470 /** 471 * @param styleClass 472 * the styleClass to set 473 */ 474 public void setStyleClass(String styleClass) { 475 this.styleClass = styleClass; 476 } 477 478 /** 479 * @param checked the checked to set 480 */ 481 public void setChecked(String checked) { 482 this.checked = checked; 483 } 484 485 /** 486 * @param value the value to set 487 */ 488 public void setValue(String value) { 489 this.value = value; 490 } 491 492 } 493 494 public static class MultipleAnchorHtmlData extends AnchorHtmlData { 495 protected List<AnchorHtmlData> anchorHtmlData; 496 protected static final String ANCHORS_SEPARATOR = ", "; 497 498 /** 499 * Needed by inquiry framework 500 */ 501 public MultipleAnchorHtmlData(List<AnchorHtmlData> anchorHtmlData) { 502 this.anchorHtmlData = anchorHtmlData; 503 } 504 505 /** 506 * 507 * This method generates anchor tag. 508 * 509 * @see HtmlData#constructCompleteHtmlTag() 510 */ 511 public String constructCompleteHtmlTag() { 512 StringBuffer completeHtmlTag = new StringBuffer(); 513 for(AnchorHtmlData anchor: anchorHtmlData){ 514 completeHtmlTag.append(anchor.constructCompleteHtmlTag()+","); 515 } 516 if(completeHtmlTag.toString().endsWith(ANCHORS_SEPARATOR)) 517 completeHtmlTag.delete(completeHtmlTag.length()-ANCHORS_SEPARATOR.length(), completeHtmlTag.length()); 518 return completeHtmlTag.toString(); 519 } 520 521 /** 522 * @return the anchorHtmlData 523 */ 524 public List<AnchorHtmlData> getAnchorHtmlData() { 525 return this.anchorHtmlData; 526 } 527 528 } 529 530 /** 531 * @return the maxLength 532 */ 533 public int getMaxLength() { 534 try{ 535 return Integer.parseInt(this.maxLength); 536 } catch(Exception ex){ 537 return -1; 538 } 539 } 540 541 /** 542 * @param maxLength the maxLength to set 543 */ 544 public void setMaxLength(String maxLength) { 545 this.maxLength = maxLength; 546 } 547 548 }