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