View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kns.lookup;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.web.format.DateFormatter;
20  import org.kuali.rice.kns.document.authorization.BusinessObjectRestrictions;
21  import org.kuali.rice.kns.document.authorization.FieldRestriction;
22  import org.kuali.rice.kns.service.BusinessObjectAuthorizationService;
23  import org.kuali.rice.kns.service.KNSServiceLocator;
24  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
25  import org.kuali.rice.krad.util.KRADConstants;
26  import org.kuali.rice.krad.util.ObjectUtils;
27  
28  import java.io.Serializable;
29  import java.sql.Date;
30  import java.util.HashMap;
31  import java.util.Iterator;
32  import java.util.List;
33  import java.util.Map;
34  
35  /**
36   * This class holds details of html data for an action url.
37   * 
38   * @author Kuali Rice Team (rice.collab@kuali.org)
39   * 
40   */
41  @Deprecated
42  public abstract class HtmlData implements Serializable {
43  
44  	protected static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(HtmlData.class);
45  
46  	public static final String ANCHOR_HTML_DATA_TYPE = AnchorHtmlData.class.getName();
47  	public static final String INPUT_HTML_DATA_TYPE = InputHtmlData.class.getName();
48  	
49  	protected String name = "";
50  	protected String title = "";
51  	protected String methodToCall = "";
52  	protected String displayText = "";
53  	protected String prependDisplayText = "";
54  	protected String appendDisplayText = "";
55  	protected List<HtmlData> childUrlDataList;
56  	protected String maxLength;
57  	
58  	/**
59  	 * 
60  	 * This method constructs the complete html tag based on the class attribute
61  	 * values.
62  	 * 
63  	 * @return
64  	 */
65  	public abstract String constructCompleteHtmlTag();
66  
67  	/**
68  	 * @return the appendDisplayText
69  	 */
70  	public String getAppendDisplayText() {
71  		return this.appendDisplayText;
72  	}
73  
74  	/**
75  	 * @param appendDisplayText the appendDisplayText to set
76  	 */
77  	public void setAppendDisplayText(String appendDisplayText) {
78  		this.appendDisplayText = appendDisplayText;
79  	}
80  
81  	/**
82  	 * @return the childUrlDataList
83  	 */
84  	public List<HtmlData> getChildUrlDataList() {
85  		return this.childUrlDataList;
86  	}
87  
88  	/**
89  	 * @param childUrlDataList the childUrlDataList to set
90  	 */
91  	public void setChildUrlDataList(List<HtmlData> childUrlDataList) {
92  		this.childUrlDataList = childUrlDataList;
93  	}
94  
95  	/**
96  	 * @return the prependDisplayText
97  	 */
98  	public String getPrependDisplayText() {
99  		return this.prependDisplayText;
100 	}
101 
102 	/**
103 	 * @param prependDisplayText the prependDisplayText to set
104 	 */
105 	public void setPrependDisplayText(String prependDisplayText) {
106 		this.prependDisplayText = prependDisplayText;
107 	}
108 
109 	/**
110 	 * @return the title
111 	 */
112 	public String getTitle() {
113 		return this.title;
114 	}
115 
116 	/**
117 	 * @param title the title to set
118 	 */
119 	public void setTitle(String title) {
120 		this.title = title;
121 	}
122 
123 	/**
124 	 * @return the name
125 	 */
126 	public String getName() {
127 		return this.name;
128 	}
129 
130 	/**
131 	 * @param name the name to set
132 	 */
133 	public void setName(String name) {
134 		this.name = name;
135 	}
136 
137 	/**
138 	 * @return the displayText
139 	 */
140 	public String getDisplayText() {
141 		return this.displayText;
142 	}
143 
144 	/**
145 	 * @param displayText the displayText to set
146 	 */
147 	public void setDisplayText(String displayText) {
148 		this.displayText = displayText;
149 	}
150 
151 	/**
152 	 * @return the methodToCall
153 	 */
154 	public String getMethodToCall() {
155 		return this.methodToCall;
156 	}
157 
158 	/**
159 	 * @param methodToCall the methodToCall to set
160 	 */
161 	public void setMethodToCall(String methodToCall) {
162 		this.methodToCall = methodToCall;
163 	}
164 
165 	public String getTitle(String prependText, Class bo, List keys) {
166 		return KRADConstants.EMPTY_STRING;
167 	}
168 
169 	/**
170 	 * KFSMI-658 This method gets the title text for a link/control
171 	 * 
172 	 * @param prependText
173 	 * @param dataObject
174 	 * @param fieldConversions
175 	 * @param returnKeys
176 	 * @return title text
177 	 */
178 	public static String getTitleText(String prependText, Object dataObject, List<String> keys, BusinessObjectRestrictions businessObjectRestrictions) {
179 		if (dataObject == null)
180 			return KRADConstants.EMPTY_STRING;
181 
182 		Map<String, String> keyValueMap = new HashMap<String, String>();
183 		Iterator keysIt = keys.iterator();
184 		while (keysIt.hasNext()) {
185 			String fieldNm = (String) keysIt.next();
186 			Object fieldVal = ObjectUtils.getPropertyValue(dataObject, fieldNm);
187 			
188 			FieldRestriction fieldRestriction = null;
189 			if (businessObjectRestrictions != null) {
190 				fieldRestriction = businessObjectRestrictions.getFieldRestriction(fieldNm);
191 			}
192 			if (fieldRestriction != null && (fieldRestriction.isMasked() || fieldRestriction.isPartiallyMasked())) {
193 				fieldVal = fieldRestriction.getMaskFormatter().maskValue(fieldVal);
194 			} else if (fieldVal == null) {
195 				fieldVal = KRADConstants.EMPTY_STRING;
196 			} else if (fieldVal instanceof Date) {
197 				// need to format date in url
198 				DateFormatter dateFormatter = new DateFormatter();
199 				fieldVal = dateFormatter.format(fieldVal);
200 			}
201 			keyValueMap.put(fieldNm, fieldVal.toString());
202 		}
203 		return getTitleText(prependText, dataObject.getClass(), keyValueMap);
204 	}
205 	
206 	private static BusinessObjectAuthorizationService businessObjectAuthorizationService;
207 	private static BusinessObjectAuthorizationService getBusinessObjectAuthorizationService() {
208 		if (businessObjectAuthorizationService == null) {
209 			businessObjectAuthorizationService = KNSServiceLocator.getBusinessObjectAuthorizationService();
210 		}
211 		return businessObjectAuthorizationService;
212 	}
213 
214 	public static String getTitleText(String prependText, Class<?> dataObjectClass, Map<String, String> keyValueMap) {
215 		StringBuffer titleText = new StringBuffer(prependText);
216 		for (String key : keyValueMap.keySet()) {
217 			String fieldVal = keyValueMap.get(key).toString();
218 			
219 			titleText.append(KRADServiceLocatorWeb.getDataDictionaryService()
220 					.getAttributeLabel(dataObjectClass, key)
221 					+ "=" + fieldVal.toString() + " ");
222 		}
223 		return titleText.toString();
224 	}
225 
226 	/**
227 	 * 
228 	 * This class is an extension of HtmlData. It represents an anchor tag.
229 	 * 
230 	 * @author Kuali Rice Team (rice.collab@kuali.org)
231 	 * 
232 	 */
233 	public static class AnchorHtmlData extends HtmlData {
234 		public static final String TARGET_BLANK = "_blank";
235 		protected String href = "";
236 		protected String target = "";
237 		protected String style = "";
238 		protected String styleClass ="";
239 		protected String onclick ="";
240 
241 		/**
242 		 * Needed by inquiry framework
243 		 */
244 		public AnchorHtmlData() {
245 		}
246 
247 		public AnchorHtmlData(String href, String title) {
248 			this.href = href;
249 			this.title = title;
250 		}
251 
252 		public AnchorHtmlData(String href, String methodToCall,
253 				String displayText) {
254 			this.href = href;
255 			this.methodToCall = methodToCall;
256 			this.displayText = displayText;
257 		}
258 
259 		/**
260 		 * @param href the href to set
261 		 */
262 		public void setHref(String href) {
263 			this.href = href;
264 		}
265 
266 		/**
267 		 * 
268 		 * This method generates anchor tag.
269 		 * 
270 		 * @see HtmlData#constructCompleteHtmlTag()
271 		 */
272 		public String constructCompleteHtmlTag() {
273 			String completeHtmlTag;
274 			if (StringUtils.isEmpty(getHref()))
275 				completeHtmlTag = getDisplayText();
276 			else
277 				completeHtmlTag = getPrependDisplayText()
278 						+ "<a title=\""
279 						+ title
280 						+ "\""
281 						+ " href=\""
282 						+ getHref()
283 						+ "\""
284 						+ getStyle()
285 						+ " "
286 						+ getStyleClass()
287 						+ " "
288 						+ (StringUtils.isEmpty(getOnclick()) ? "" : " onClick=\""
289 							+ getOnclick() + "\" ")
290 						+ (StringUtils.isEmpty(getTarget()) ? "" : " target=\""
291 								+ getTarget() + "\" ") + ">" + getDisplayText()
292 						+ "</a>" + getAppendDisplayText();
293 			return completeHtmlTag;
294 		}
295 
296 		/**
297 		 * @return the target
298 		 */
299 		public String getTarget() {
300 			return this.target;
301 		}
302 
303 		/**
304 		 * @param target
305 		 *            the target to set
306 		 */
307 		public void setTarget(String target) {
308 			this.target = target;
309 		}
310 
311 		/**
312          * @return the style
313          */
314         public String getStyle() {
315         	return this.style;
316         }
317 
318 		/**
319          * @param style the style to set
320          */
321         public void setStyle(String style) {
322         	this.style = style;
323         }
324 
325 		/**
326          * @return the styleClass
327          */
328         public String getStyleClass() {
329         	return this.styleClass;
330         }
331 
332 		/**
333          * @param styleClass the styleClass to set
334          */
335         public void setStyleClass(String styleClass) {
336         	this.styleClass = styleClass;
337         }
338         
339 		/**
340 		 * @return the onclick
341 		 */
342 		public String getOnclick() {
343 			return this.onclick;
344 		}
345 
346 		/**
347 		 * @param onclick the onclick to set
348 		 */
349 		public void setOnclick(String onclick) {
350 			this.onclick = onclick;
351 		}        
352 
353 		/**
354 		 * @return the href
355 		 */
356 		public String getHref() {
357 			return this.href;
358 		}
359 
360 		/**
361 		 * @return the methodToCall
362 		 */
363 		public String getMethodToCall() {
364 			return this.methodToCall;
365 		}
366 
367 	}
368 
369 	/**
370 	 * 
371 	 * This class is an extension of HtmlData. It represents an input tag.
372 	 * 
373 	 * @author Kuali Rice Team (rice.collab@kuali.org)
374 	 * 
375 	 */
376 	public static class InputHtmlData extends HtmlData {
377 		public static final String CHECKBOX_INPUT_TYPE = "checkbox";
378 		public static final String CHECKBOX_CHECKED_VALUE = "checked";
379 
380 		protected String inputType = "";
381 		protected String src = "";
382 		protected String styleClass = "";
383 		protected String border = "0";
384 		protected String checked = "";
385 		protected String value = "";
386 
387 		public InputHtmlData(String name, String inputType) {
388 			this.name = name;
389 			this.inputType = inputType;
390 		}
391 
392 		public InputHtmlData(String name, String inputType, String src) {
393 			this.name = name;
394 			this.inputType = inputType;
395 			this.src = src;
396 		}
397 
398 		/***********************************************************************
399 		 * 
400 		 * This method contructs an input tag.
401 		 * 
402 		 * @see HtmlData#constructCompleteHtmlTag()
403 		 */
404 		public String constructCompleteHtmlTag() {
405 			return getPrependDisplayText()
406 					+ "<input title=\""
407 					+ title
408 					+ "\""
409 					+ " name=\""
410 					+ getName()
411 					+ "\""
412 					+ (StringUtils.isEmpty(src) ? ""
413 							: " src=\"" + src + "\" ")
414 					+ " type=\""
415 					+ getInputType()
416 					+ "\""
417 					+ (StringUtils.isEmpty(value) ? ""
418 							: " value=\"" + value + "\" ")
419 					+ (StringUtils.isEmpty(checked) ? ""
420 							: " checked=\"" + checked + "\" ")
421 					+ (StringUtils.isEmpty(getStyleClass()) ? ""
422 							: " styleClass=\"" + getStyleClass() + "\" ")
423 					+ " border=\"" + getBorder() + "\"" + " value=\""
424 					+ getDisplayText() + "\"" + "/>" + getAppendDisplayText();
425 		}
426 
427 		/**
428 		 * @return the inputType
429 		 */
430 		public String getInputType() {
431 			return this.inputType;
432 		}
433 
434 		/**
435 		 * @return the src
436 		 */
437 		public String getSrc() {
438 			return this.src;
439 		}
440 
441 		/**
442 		 * @return the border
443 		 */
444 		public String getBorder() {
445 			return this.border;
446 		}
447 
448 		/**
449 		 * @param border
450 		 *            the border to set
451 		 */
452 		public void setBorder(String border) {
453 			this.border = border;
454 		}
455 
456 		/**
457 		 * @return the styleClass
458 		 */
459 		public String getStyleClass() {
460 			return this.styleClass;
461 		}
462 
463 		/**
464 		 * @param styleClass
465 		 *            the styleClass to set
466 		 */
467 		public void setStyleClass(String styleClass) {
468 			this.styleClass = styleClass;
469 		}
470 
471 		/**
472 		 * @param checked the checked to set
473 		 */
474 		public void setChecked(String checked) {
475 			this.checked = checked;
476 		}
477 
478 		/**
479 		 * @param value the value to set
480 		 */
481 		public void setValue(String value) {
482 			this.value = value;
483 		}
484 
485 	}
486 
487 	public static class MultipleAnchorHtmlData extends AnchorHtmlData {
488 		protected List<AnchorHtmlData> anchorHtmlData;
489 		protected static final String ANCHORS_SEPARATOR = ", ";
490 		
491 		/**
492 		 * Needed by inquiry framework
493 		 */
494 		public MultipleAnchorHtmlData(List<AnchorHtmlData> anchorHtmlData) {
495 			this.anchorHtmlData = anchorHtmlData;
496 		}
497 		
498 		/**
499 		 * 
500 		 * This method generates anchor tag.
501 		 * 
502 		 * @see HtmlData#constructCompleteHtmlTag()
503 		 */
504 		public String constructCompleteHtmlTag() {
505 			StringBuffer completeHtmlTag = new StringBuffer();
506 			for(AnchorHtmlData anchor: anchorHtmlData){
507 				completeHtmlTag.append(anchor.constructCompleteHtmlTag()+",");
508 			}
509 	        if(completeHtmlTag.toString().endsWith(ANCHORS_SEPARATOR))
510 	        	completeHtmlTag.delete(completeHtmlTag.length()-ANCHORS_SEPARATOR.length(), completeHtmlTag.length());
511 			return completeHtmlTag.toString();
512 		}
513 
514 		/**
515 		 * @return the anchorHtmlData
516 		 */
517 		public List<AnchorHtmlData> getAnchorHtmlData() {
518 			return this.anchorHtmlData;
519 		}
520 
521 	}
522 
523 	/**
524 	 * @return the maxLength
525 	 */
526 	public int getMaxLength() {
527 		try{
528 			return Integer.parseInt(this.maxLength);
529 		} catch(Exception ex){
530 			return -1;
531 		}
532 	}
533 
534 	/**
535 	 * @param maxLength the maxLength to set
536 	 */
537 	public void setMaxLength(String maxLength) {
538 		this.maxLength = maxLength;
539 	}
540 
541 }