use of org.jaffa.presentation.portlet.FormBase in project jaffa-framework by jaffa-projects.
the class EditBoxTag method otherDoStartTagOperations.
/**
* This generates the HTML for the tag.
*/
public void otherDoStartTagOperations() throws JspException {
log.debug(this.NAME + ".otherDoStartTagOperations()...");
try {
super.otherDoStartTagOperations();
} catch (JspException e) {
log.error(this.NAME + ".otherDoStartTagOperations(): error=" + e);
}
// Preprocess if within a Property widget
IPropertyRuleIntrospector propertyRuleIntrospector = lookupPropertyTag();
// raise an error, if the 'field' is null
if (getField() == null) {
String str = "The " + TAG_NAME + " requires 'field' parameter to be supplied or it should be within a PropertyTag";
log.error(str);
throw new MissingParametersRuntimeException(str);
}
// Get the formtag from the page & register the widget
FormTag formTag = TagHelper.getFormTag(pageContext);
if (formTag == null) {
String str = "The " + TAG_NAME + " should be inside a FormTag";
log.error(str);
throw new OuterFormTagMissingRuntimeException(str);
}
// Will hold the required html
String html = null;
// Get the model
SimpleWidgetModel model = null;
try {
model = (SimpleWidgetModel) TagHelper.getModel(pageContext, getField(), TAG_NAME);
} catch (ClassCastException e) {
String str = "Wrong WidgetModel for " + TAG_NAME + " on field " + getField();
log.error(str, e);
throw new JspWriteRuntimeException(str, e);
}
// Check for an IPropertyRuleIntrospector, if this tag is not within a Property widget
try {
if (propertyRuleIntrospector == null)
propertyRuleIntrospector = TagHelper.getPropertyRuleIntrospector(pageContext, getField());
// Wrap the propertyRuleIntrospector
propertyRuleIntrospector = TagHelper.wrapPropertyRuleIntrospector(propertyRuleIntrospector, model);
if (log.isDebugEnabled())
log.debug(this.NAME + " [field=" + getField() + "] PropertyRuleIntrospector is " + propertyRuleIntrospector);
} catch (JspException e) {
log.error("EditBoxTag.otherDoStartTagOperations(): error=" + e);
}
if (propertyRuleIntrospector.isHidden()) {
// Display the (Restricted) text for a hidden field
html = TagHelper.getTextForHiddenField(pageContext);
} else {
if (model != null) {
if (propertyRuleIntrospector.isReadOnly()) {
if (!m_password) {
// Just display the text for a readOnly field
html = StringHelper.convertToHTML(propertyRuleIntrospector.format(model.getWidgetValue()));
html = convertTextToHtml(html, getRows() > 1);
}
} else {
String formName = TagHelper.getFormTag(pageContext).getHtmlName();
String idPrefix = getHtmlIdPrefix();
String eventPrefix = getJaffaEventNamePrefix();
String classPrefix = propertyRuleIntrospector.isMandatory() ? "<span class=\"editboxMandatoryPrefix\"> </span>" : "<span class=\"editboxOptionalPrefix\"> </span>";
String classSuffix = propertyRuleIntrospector.isMandatory() ? "<span class=\"editboxMandatorySuffix\"> </span>" : "<span class=\"editboxOptionalSuffix\"> </span>";
// Set the layout on the model from the introspector
model.setLayout(propertyRuleIntrospector.getLayout());
// Generate the HTML
try {
// @TODO - need to use the getHtmlTextArea regardless of rows, if this is a LIFO/FIFO comment style field
String commentStyle = propertyRuleIntrospector.getCommentStyle();
if (PLAIN.equals(commentStyle))
commentStyle = null;
// Default the trim attribute to 'false' for textarea and password fields. Default to 'true' for all others
if (!m_trimAttributePassed)
m_trim = getRows() > 1 || commentStyle != null || m_password ? false : true;
if (getRows() > 1 || commentStyle != null) {
// retrieve the original text from the orignal model. Create one if the orignal model does not exist when LIFO/FIFO.
String originalText = null;
if (LIFO.equals(commentStyle) || FIFO.equals(commentStyle)) {
FormBase formObject = TagHelper.getFormBase(pageContext);
SimpleWidgetModel origWM = (SimpleWidgetModel) formObject.getWidgetCache().getModel(getField() + "_original");
if (origWM == null) {
// this is the first time this comment tag has been executed
formObject.getWidgetCache().addModel(getField() + "_original", new SimpleWidgetModel(model));
originalText = model.getStringValue();
model.setWidgetValue(null);
} else {
// the original text was cached
originalText = origWM.getStringValue();
}
}
// create the html string
StringBuffer sb = new StringBuffer();
if (FIFO.equals(commentStyle)) {
if (originalText != null)
sb.append(convertTextToHtml(originalText, true)).append("<br/>");
}
sb.append(classPrefix).append(getHtmlTextArea(idPrefix, eventPrefix, formName, model, propertyRuleIntrospector)).append(classSuffix);
if (LIFO.equals(commentStyle)) {
if (originalText != null)
sb.append("<br/>").append(convertTextToHtml(originalText, true));
}
html = sb.toString();
} else {
html = classPrefix + getHtml(idPrefix, eventPrefix, formName, model, propertyRuleIntrospector) + classSuffix;
}
} catch (JspException e) {
log.error("EditBox.otherDoStartTagOperations(): error in getHtml call: " + e);
}
}
}
}
if (html != null) {
// Write the HTML
JspWriter out = pageContext.getOut();
try {
out.print(html);
} catch (IOException e) {
String str = "Exception in writing the " + TAG_NAME;
log.error(str, e);
throw new JspWriteRuntimeException(str, e);
}
}
}
use of org.jaffa.presentation.portlet.FormBase in project jaffa-framework by jaffa-projects.
the class FormTag method doStartTagExt.
/**
* This method sets the component, componentId, tokenError(if found) on the FormBean
* It also invokes the initForm() method of the FormBean
*
* This has been extended so that is now set the security context for the thread
* of execution within the <xxx:form> tag.
*/
private void doStartTagExt() throws JspException {
try {
Object f = pageContext.findAttribute(getBeanName());
if (log.isDebugEnabled())
log.debug(this.NAME + ".doStartTagExt: Started FORM tag for bean " + getBeanName() + " id=" + getHtmlIdPrefix());
// Get the request stream
ServletRequest request = pageContext.getRequest();
if (!(request instanceof HttpServletRequest)) {
log.warn("Security Problem, Can't Set Thread Context Since Request is not HTTP");
} else {
try {
SecurityTag.setThreadContext((HttpServletRequest) request);
m_securityContextSet = true;
} catch (SecurityException e) {
log.error(this.NAME + ".doStartTagExt: Tag Failed, Could Not Set Security Context", e);
throw new JspException("Can't Set Security Context");
}
}
// Special handelling if this is a Jaffa Component
if (f instanceof FormBase) {
FormBase form = (FormBase) f;
// Get the UserSession so we can lookup the component
UserSession us = UserSession.getUserSession((HttpServletRequest) request);
// Set the component on the form
FormKey fk = (FormKey) request.getAttribute(FormKey.class.getName());
if (fk != null) {
String componentId = fk.getComponentId();
form.setComponent(us.getComponent(componentId));
}
// invoke the default initialization of the form-bean
if (log.isDebugEnabled())
log.debug(this.NAME + ".doStartTagExt: Invoking the initForm method of the Form");
form.initForm();
}
// -------------------------------------
// Initialize the PageContext attributes
// -------------------------------------
// The FormTag Object
pageContext.setAttribute(TagHelper.ATTRIBUTE_FORM_TAG, this, pageContext.REQUEST_SCOPE);
// The FormBean Object
pageContext.setAttribute(TagHelper.ATTRIBUTE_FORM_BASE, f, pageContext.REQUEST_SCOPE);
// The Name of the Struts Form Bean
pageContext.setAttribute(TagHelper.ATTRIBUTE_FORM_NAME, getBeanName(), pageContext.REQUEST_SCOPE);
// Inidcator that any inner tag is enclosed in a form tag
pageContext.setAttribute(TagHelper.ATTRIBUTE_ENCLOSED, Boolean.FALSE, pageContext.REQUEST_SCOPE);
// Html Id of the Form, to be appended to for inner widget id's
pageContext.setAttribute(TagHelper.ATTRIBUTE_ID_PREFIX, getHtmlIdPrefix(), pageContext.REQUEST_SCOPE);
// Jaffa Event prefix for the 'eventId' hidden field
pageContext.setAttribute(TagHelper.ATTRIBUTE_EVENT_PREFIX, "", pageContext.REQUEST_SCOPE);
// Cache previous attribute
m_oldForm = pageContext.findAttribute(TagHelper.ATTRIBUTE_EL_FORM);
// The FormBean Object for Expression Scripting
pageContext.setAttribute(TagHelper.ATTRIBUTE_EL_FORM, f, pageContext.REQUEST_SCOPE);
// write out the span tag <SPAN ID="formName_EntirePage" STYLE="display:none">
JspWriter writer = pageContext.getOut();
StringBuffer buf = new StringBuffer();
// TODO: use HeaderCache
buf.append("\n<span id='" + getHtmlIdPrefix() + ENTIRE_PAGE_SUFFIX + "' class='FormGuard'>");
writer.println(buf.toString());
} catch (IOException e) {
throw new JspException("error in FormTag: " + e);
}
}
use of org.jaffa.presentation.portlet.FormBase in project jaffa-framework by jaffa-projects.
the class TagHelper method getFieldValue.
/**
* For an enclosed tag, this will return the fieldValue from the pageContext attribute, and it may be null.
* If the tag is not enclosed, it will get the fieldValue from the FormBase, and it will throw a RuntimeException if the property is not found.
* @param pageContext The PageContext of the jsp.
* @param fieldName The field name.
* @param tagName The tag name.
* @return a value for the field.
*/
public static Object getFieldValue(PageContext pageContext, String fieldName, String tagName) {
Object value = null;
if (isEnclosed(pageContext)) {
Map map = getModelMap(pageContext);
if (map != null)
value = map.get(fieldName);
} else {
FormBase formObject = getFormBase(pageContext);
if (formObject != null) {
try {
value = BeanHelper.getField(formObject, fieldName);
} catch (NoSuchMethodException e) {
// Lets look for a getXyzWM() method
try {
value = BeanHelper.getField(formObject, fieldName + "WM");
} catch (NoSuchMethodException e1) {
String str = "Getter Method for field " + fieldName + " not found while evaluating the " + tagName;
log.error(str, e);
throw new WidgetModelAccessMethodNotFoundRuntimeException(str, e);
}
}
} else {
String str = "The " + tagName + " for field " + fieldName + " should be inside a FormTag";
log.error(str);
throw new OuterFormTagMissingRuntimeException(str);
}
}
if (value != null && value instanceof SimpleWidgetModel)
value = ((SimpleWidgetModel) value).getWidgetValue();
return value;
}
use of org.jaffa.presentation.portlet.FormBase in project jaffa-framework by jaffa-projects.
the class TagHelper method getModel.
/**
* For an enclosed tag, this will return the model from the pageContext attribute, and it may be null.
* If the tag is not enclosed, it will get the model from the FormBase, and it will throw a RuntimeException if the model is not found.
* @param pageContext The PageContext of the jsp.
* @param fieldName The field name.
* @param tagName The tag name.
* @return a WidgetModel object.
*/
public static Object getModel(PageContext pageContext, String fieldName, String tagName) {
Object model = null;
if (isEnclosed(pageContext)) {
Map map = getModelMap(pageContext);
if (map != null)
model = map.get(fieldName);
} else {
FormBase formObject = getFormBase(pageContext);
if (formObject != null) {
// Work out the method to get the model...should be like getFieldNameWM()
Class formClass = formObject.getClass();
String methodStr = "get" + StringHelper.getUpper1(fieldName) + "WM";
if (log.isDebugEnabled())
log.debug("Introspect. Looking for " + methodStr + " on " + formClass.getName());
Method method = null;
try {
method = formClass.getMethod(methodStr, new Class[] {});
} catch (NoSuchMethodException e) {
String str = "Method :" + methodStr + " not found";
log.error(str, e);
throw new WidgetModelAccessMethodNotFoundRuntimeException(str, e);
}
try {
model = method.invoke(formObject, new Object[] {});
if (log.isDebugEnabled())
log.debug("Introspect. Got Model from FormBean");
} catch (Exception e) {
String str = "Error while invoking the method :" + methodStr;
log.error(str, e);
throw new WidgetModelAccessMethodInvocationRuntimeException(str, e);
}
} else {
String str = "The " + tagName + " for field " + fieldName + " should be inside a FormTag";
log.error(str);
throw new OuterFormTagMissingRuntimeException(str);
}
}
return model;
}
use of org.jaffa.presentation.portlet.FormBase in project jaffa-framework by jaffa-projects.
the class FormTag method doEndTagExt1.
/**
* This method will write out the hidden-fields
*/
private void doEndTagExt1(JspWriter writer) throws IOException {
Object formObj = pageContext.findAttribute(getBeanName());
if (formObj != null && formObj instanceof FormBase) {
FormBase f = (FormBase) formObj;
StringBuffer buf = new StringBuffer();
buf.append("<input type='hidden' name='" + PARAMETER_COMPONENT_ID + "' value='" + (f.getComponent() == null ? "" : f.getComponent().getComponentId()) + "'>\n");
buf.append("<input type='hidden' name='" + PARAMETER_EVENT_ID + "' value=''>\n");
buf.append("<input type='hidden' id='" + PARAMETER_DATESTAMP_ID + "' value='" + m_dateTime.timeInMillis() + "'>\n");
// buf.append("<input type=\"hidden\" name=\"" + PARAMETER_TOKEN_ID + "\" value=\"" +
// (UserSession.getUserSession((HttpServletRequest) pageContext.getRequest()).getCurrentToken()) + "\">\n");
buf.append("</span>");
writer.println(buf.toString());
}
}
Aggregations