001: package nl.knowlogy.validation.jsf.component.html;
002:
003: import java.util.ArrayList;
004: import java.util.Collections;
005: import java.util.Iterator;
006: import java.util.List;
007:
008: import javax.faces.application.FacesMessage;
009: import javax.faces.component.UIComponent;
010: import javax.faces.component.UIMessage;
011: import javax.faces.context.FacesContext;
012: import javax.faces.el.ValueBinding;
013:
014: import nl.knowlogy.validation.Messages;
015: import nl.knowlogy.validation.jsf.MessagesUtil;
016: import nl.knowlogy.validation.jsf.utils.JsfUtils;
017:
018: import org.apache.commons.logging.Log;
019: import org.apache.commons.logging.LogFactory;
020:
021: /**
022: *
023: */
024: public class ValidationHtmlMessage extends UIMessage {
025:
026: private static final Log log = LogFactory
027: .getLog(ValidationHtmlMessage.class);
028:
029: public static final String COMPONENT_TYPE = "nl.knowlogy.validation.ValidationHtmlMessage";
030:
031: public static final String DEFAULT_RENDERER_TYPE = "nl.knowlogy.validation.ValidationHtmlMessage";
032:
033: private static final boolean DEFAULT_TOOLTIP = false;
034:
035: private String _errorClass = "errorMessage";
036:
037: private String _errorStyle = null;
038:
039: private String _fatalClass = null;
040:
041: private String _fatalStyle = null;
042:
043: private String _infoClass = "infoMessage";
044:
045: private String _infoStyle = null;
046:
047: private String _style = null;
048:
049: private String _styleClass = null;
050:
051: private String _title = null;
052:
053: private Boolean _tooltip = null;
054:
055: private String _warnClass = "warnMessage";
056:
057: private String _warnStyle = null;
058:
059: private Object forObject = null;
060:
061: private String property = null;
062:
063: private String objectExpression;
064: private Boolean initializedObjectExpression = Boolean.FALSE;
065: private Boolean objectLevelOnly = Boolean.FALSE;
066: private String fieldName;
067:
068: public ValidationHtmlMessage() {
069: setRendererType(DEFAULT_RENDERER_TYPE);
070: }
071:
072: public void setErrorClass(String errorClass) {
073: _errorClass = errorClass;
074: }
075:
076: public String getErrorClass() {
077: if (_errorClass != null)
078: return _errorClass;
079: ValueBinding vb = getValueBinding("errorClass");
080: return vb != null ? JsfUtils.getStringValue(getFacesContext(),
081: vb) : null;
082: }
083:
084: public void setErrorStyle(String errorStyle) {
085: _errorStyle = errorStyle;
086: }
087:
088: public String getErrorStyle() {
089: if (_errorStyle != null)
090: return _errorStyle;
091: ValueBinding vb = getValueBinding("errorStyle");
092: return vb != null ? JsfUtils.getStringValue(getFacesContext(),
093: vb) : null;
094: }
095:
096: public void setFatalClass(String fatalClass) {
097: _fatalClass = fatalClass;
098: }
099:
100: public String getFatalClass() {
101: if (_fatalClass != null)
102: return _fatalClass;
103: ValueBinding vb = getValueBinding("fatalClass");
104: return vb != null ? JsfUtils.getStringValue(getFacesContext(),
105: vb) : null;
106: }
107:
108: public void setFatalStyle(String fatalStyle) {
109: _fatalStyle = fatalStyle;
110: }
111:
112: public String getFatalStyle() {
113: if (_fatalStyle != null)
114: return _fatalStyle;
115: ValueBinding vb = getValueBinding("fatalStyle");
116: return vb != null ? JsfUtils.getStringValue(getFacesContext(),
117: vb) : null;
118: }
119:
120: public void setInfoClass(String infoClass) {
121: _infoClass = infoClass;
122: }
123:
124: public String getInfoClass() {
125: if (_infoClass != null)
126: return _infoClass;
127: ValueBinding vb = getValueBinding("infoClass");
128: return vb != null ? JsfUtils.getStringValue(getFacesContext(),
129: vb) : null;
130: }
131:
132: public void setInfoStyle(String infoStyle) {
133: _infoStyle = infoStyle;
134: }
135:
136: public String getInfoStyle() {
137: if (_infoStyle != null)
138: return _infoStyle;
139: ValueBinding vb = getValueBinding("infoStyle");
140: return vb != null ? JsfUtils.getStringValue(getFacesContext(),
141: vb) : null;
142: }
143:
144: public void setStyle(String style) {
145: _style = style;
146: }
147:
148: public String getStyle() {
149: if (_style != null)
150: return _style;
151: ValueBinding vb = getValueBinding("style");
152: return vb != null ? JsfUtils.getStringValue(getFacesContext(),
153: vb) : null;
154: }
155:
156: public void setStyleClass(String styleClass) {
157: _styleClass = styleClass;
158: }
159:
160: public String getStyleClass() {
161: if (_styleClass != null)
162: return _styleClass;
163: ValueBinding vb = getValueBinding("styleClass");
164: return vb != null ? JsfUtils.getStringValue(getFacesContext(),
165: vb) : null;
166: }
167:
168: public void setTitle(String title) {
169: _title = title;
170: }
171:
172: public String getTitle() {
173: if (_title != null)
174: return _title;
175: ValueBinding vb = getValueBinding("title");
176: return vb != null ? JsfUtils.getStringValue(getFacesContext(),
177: vb) : null;
178: }
179:
180: public void setTooltip(boolean tooltip) {
181: _tooltip = Boolean.valueOf(tooltip);
182: }
183:
184: public boolean isTooltip() {
185: if (_tooltip != null)
186: return _tooltip.booleanValue();
187: ValueBinding vb = getValueBinding("tooltip");
188: Boolean v = vb != null ? (Boolean) vb
189: .getValue(getFacesContext()) : null;
190: return v != null ? v.booleanValue() : DEFAULT_TOOLTIP;
191: }
192:
193: public void setWarnClass(String warnClass) {
194: _warnClass = warnClass;
195: }
196:
197: public String getWarnClass() {
198: if (_warnClass != null)
199: return _warnClass;
200: ValueBinding vb = getValueBinding("warnClass");
201: return vb != null ? JsfUtils.getStringValue(getFacesContext(),
202: vb) : null;
203: }
204:
205: public void setWarnStyle(String warnStyle) {
206: _warnStyle = warnStyle;
207: }
208:
209: public String getWarnStyle() {
210: if (_warnStyle != null)
211: return _warnStyle;
212: ValueBinding vb = getValueBinding("warnStyle");
213: return vb != null ? JsfUtils.getStringValue(getFacesContext(),
214: vb) : null;
215: }
216:
217: public void setForObject(Object forObject) {
218: this .forObject = forObject;
219: }
220:
221: public Object getForObject() {
222: if (forObject != null)
223: return forObject;
224: ValueBinding vb = getValueBinding("forObject");
225: return vb != null ? vb.getValue(getFacesContext()) : null;
226: }
227:
228: public void setProperty(String property) {
229: this .property = property;
230: }
231:
232: public String getProperty() {
233: if (property != null)
234: return property;
235: ValueBinding vb = getValueBinding("property");
236: return vb != null ? JsfUtils.getStringValue(getFacesContext(),
237: vb) : null;
238:
239: }
240:
241: public Object saveState(FacesContext context) {
242: Object values[] = new Object[19];
243: values[0] = super .saveState(context);
244: values[1] = _errorClass;
245: values[2] = _errorStyle;
246: values[3] = _fatalClass;
247: values[4] = _fatalStyle;
248: values[5] = _infoClass;
249: values[6] = _infoStyle;
250: values[7] = _style;
251: values[8] = _styleClass;
252: values[9] = _title;
253: values[10] = _tooltip;
254: values[11] = _warnClass;
255: values[12] = _warnStyle;
256: values[13] = objectExpression;
257: values[14] = objectLevelOnly;
258: values[15] = fieldName;
259: values[16] = initializedObjectExpression;
260: values[17] = forObject;
261: values[18] = property;
262:
263: return ((Object) (values));
264: }
265:
266: public void restoreState(FacesContext context, Object state) {
267: Object values[] = (Object[]) state;
268: super .restoreState(context, values[0]);
269: _errorClass = (String) values[1];
270: _errorStyle = (String) values[2];
271: _fatalClass = (String) values[3];
272: _fatalStyle = (String) values[4];
273: _infoClass = (String) values[5];
274: _infoStyle = (String) values[6];
275: _style = (String) values[7];
276: _styleClass = (String) values[8];
277: _title = (String) values[9];
278: _tooltip = (Boolean) values[10];
279: _warnClass = (String) values[11];
280: _warnStyle = (String) values[12];
281: objectExpression = (String) values[13];
282: objectLevelOnly = (Boolean) values[14];
283: fieldName = (String) values[15];
284: initializedObjectExpression = (Boolean) values[16];
285: forObject = values[17];
286: property = (String) values[18];
287: }
288:
289: public String getPropertyName(FacesContext context) {
290: String propertyName = getProperty();
291: if (propertyName == null) {
292: propertyName = getFieldName(context);
293: }
294: return propertyName;
295: }
296:
297: public List getValidationMessages(FacesContext context) {
298:
299: ValueBinding valueBinding = context.getApplication()
300: .createValueBinding(MessagesUtil.VALUE_BINDING_NAME);
301:
302: Object object = valueBinding.getValue(context);
303:
304: if (object != null && object instanceof Messages) {
305:
306: Messages messages = (Messages) object;
307:
308: Object forObject = getObjectToValidate(context);
309: if (forObject != null) {
310: if (log.isDebugEnabled()) {
311: log.debug("Retrieved object " + forObject
312: + " fieldName " + fieldName
313: + " objectLevelOnly " + objectLevelOnly);
314: }
315: List messageList;
316: String propertyName = getPropertyName(context);
317:
318: if (propertyName != null) {
319: messageList = messages.getMessages(forObject,
320: propertyName);
321: } else {
322: messageList = messages.getMessages(forObject,
323: objectLevelOnly.booleanValue());
324: }
325: return messageList;
326: }
327: }
328: return Collections.EMPTY_LIST;
329: }
330:
331: /**
332: * Locates the component identified by <code>forComponent</code>
333: *
334: * @param forComponent -
335: * the component to search for
336: * @param component -
337: * the starting point in which to begin the search
338: * @return the component with the the <code>id</code that matches
339: * <code>forComponent</code> otheriwse null if no match is found.
340: */
341: protected UIComponent getForComponent(FacesContext context,
342: String forComponent, UIComponent component) {
343: UIComponent result = JsfUtils.getForComponent(context,
344: forComponent, this );
345: if (result == null) {
346: if (log.isWarnEnabled()) {
347: log.warn("COMPONENT_NOT_FOUND_IN_VIEW_WARNING_ID"
348: //Util.getExceptionMessageString( Util.COMPONENT_NOT_FOUND_IN_VIEW_WARNING_ID,
349: //new Object[] { forComponent })
350: );
351: }
352: }
353: return result;
354: }
355:
356: protected Object getObjectToValidate(FacesContext context) {
357:
358: Object objectToValidate = getForObject();
359: if (objectToValidate == null) {
360:
361: String objectExpresion = getObjectExpression(context);
362: if (objectExpresion != null) {
363: objectToValidate = JsfUtils.getBindedObject(context,
364: objectExpresion);
365: }
366: }
367: return objectToValidate;
368: }
369:
370: private String getFieldName(FacesContext context) {
371: if (!initializedObjectExpression.booleanValue()) {
372: if (getFor() != null) {
373: UIComponent forUIComponent = getForComponent(context,
374: getFor(), this );
375: objectExpression = JsfUtils
376: .getObjectPart(forUIComponent);
377: fieldName = JsfUtils
378: .getPropertyNamePart(forUIComponent);
379: }
380: initializedObjectExpression = Boolean.TRUE;
381: }
382: return fieldName;
383: }
384:
385: private String getObjectExpression(FacesContext context) {
386: if (!initializedObjectExpression.booleanValue()) {
387: if (getFor() != null) {
388: UIComponent forUIComponent = getForComponent(context,
389: getFor(), this);
390: objectExpression = JsfUtils
391: .getObjectPart(forUIComponent);
392: fieldName = JsfUtils
393: .getPropertyNamePart(forUIComponent);
394: }
395: initializedObjectExpression = Boolean.TRUE;
396: }
397: return objectExpression;
398: }
399: }
|