protected void renderSelectBox(FormField formField, HtmlDocumentBuilder documentBuilder) { HtmlElementWriter selectBox = new HtmlElementWriter(SELECT_ELEMENT, false); addCommonFormFieldAttributes(formField, selectBox); // <select ...> documentBuilder.startElement(selectBox); // <option ...> renderSelectOptions(formField, documentBuilder); // </select> documentBuilder.endElement(); }
protected void renderInputField(FormField formField, HtmlDocumentBuilder documentBuilder) { HtmlElementWriter inputField = new HtmlElementWriter(INPUT_ELEMENT, true); addCommonFormFieldAttributes(formField, inputField); String inputType = !isBoolean(formField) ? TEXT_INPUT_TYPE : CHECKBOX_INPUT_TYPE; inputField.attribute(TYPE_ATTRIBUTE, inputType); // add default value Object defaultValue = formField.getDefaultValue(); if(defaultValue != null) { inputField.attribute(VALUE_ATTRIBUTE, defaultValue.toString()); } // <input ... /> documentBuilder.startElement(inputField).endElement(); }
protected void renderFormField(FormField formField, HtmlDocumentBuilder documentBuilder) { // start group HtmlElementWriter divElement = new HtmlElementWriter(DIV_ELEMENT) .attribute(CLASS_ATTRIBUTE, FORM_GROUP_CLASS); documentBuilder.startElement(divElement); String formFieldId = formField.getId(); String formFieldLabel = formField.getLabel(); // write label if (formFieldLabel != null && !formFieldLabel.isEmpty()) { HtmlElementWriter labelElement = new HtmlElementWriter(LABEL_ELEMENT) .attribute(FOR_ATTRIBUTE, formFieldId) .textContent(formFieldLabel); // <label for="...">...</label> documentBuilder.startElement(labelElement).endElement(); } // render form control if(isEnum(formField)) { // <select ...> renderSelectBox(formField, documentBuilder); } else if (isDate(formField)){ renderDatePicker(formField, documentBuilder); } else { // <input ...> renderInputField(formField, documentBuilder); } renderInvalidMessageElement(formField, documentBuilder); // end group documentBuilder.endElement(); }
protected void renderInvalidMessageElement(FormField formField, HtmlDocumentBuilder documentBuilder) { HtmlElementWriter divElement = new HtmlElementWriter(DIV_ELEMENT); String formFieldId = formField.getId(); String ifExpression = String.format(INVALID_EXPRESSION + " && " + DIRTY_EXPRESSION, formFieldId, formFieldId); divElement .attribute(NG_IF_ATTRIBUTE, ifExpression) .attribute(CLASS_ATTRIBUTE, HAS_ERROR_CLASS); // <div ng-if="....$invalid && ....$dirty"...> documentBuilder.startElement(divElement); if (!isDate(formField)) { renderInvalidValueMessage(formField, documentBuilder); renderInvalidTypeMessage(formField, documentBuilder); } else { renderInvalidDateMessage(formField, documentBuilder); } documentBuilder.endElement(); }
protected void addCommonFormFieldAttributes(FormField formField, HtmlElementWriter formControl) { String typeName = formField.getTypeName(); if (isEnum(formField) || isDate(formField)) { typeName = StringFormType.TYPE_NAME; } typeName = typeName.substring(0, 1).toUpperCase() + typeName.substring(1); String formFieldId = formField.getId(); formControl .attribute(CLASS_ATTRIBUTE, FORM_CONTROL_CLASS) .attribute(NAME_ATTRIBUTE, formFieldId) .attribute(CAM_VARIABLE_TYPE_ATTRIBUTE, typeName) .attribute(CAM_VARIABLE_NAME_ATTRIBUTE, formFieldId); // add validation constraints for (FormFieldValidationConstraint constraint : formField.getValidationConstraints()) { String constraintName = constraint.getName(); String configuration = (String) constraint.getConfiguration(); formControl.attribute(constraintName, configuration); } }
protected void renderDatePicker(FormField formField, HtmlDocumentBuilder documentBuilder) { boolean isReadOnly = isReadOnly(formField); HtmlElementWriter inputField = createInputField(formField);
protected HtmlElementWriter createInputField(FormField formField) { HtmlElementWriter inputField = new HtmlElementWriter(INPUT_ELEMENT, true); addCommonFormFieldAttributes(formField, inputField); inputField.attribute(TYPE_ATTRIBUTE, TEXT_INPUT_TYPE); return inputField; }
protected void renderInvalidTypeMessage(FormField formField, HtmlDocumentBuilder documentBuilder) { HtmlElementWriter divElement = new HtmlElementWriter(DIV_ELEMENT); String formFieldId = formField.getId(); String expression = String.format(TYPE_ERROR_EXPRESSION, formFieldId); String typeName = formField.getTypeName(); if (isEnum(formField)) { typeName = StringFormType.TYPE_NAME; } divElement .attribute(NG_SHOW_ATTRIBUTE, expression) .attribute(CLASS_ATTRIBUTE, HELP_BLOCK_CLASS) .textContent(String.format(TYPE_FIELD_MESSAGE, typeName)); documentBuilder .startElement(divElement) .endElement(); }
protected String renderFormData(FormData formData) { if(formData == null || (formData.getFormFields() == null || formData.getFormFields().isEmpty()) && (formData.getFormProperties() == null || formData.getFormProperties().isEmpty())) { return null; } else { HtmlElementWriter formElement = new HtmlElementWriter(FORM_ELEMENT) .attribute(NAME_ATTRIBUTE, GENERATED_FORM_NAME) .attribute(ROLE_ATTRIBUTE, FORM_ROLE); HtmlDocumentBuilder documentBuilder = new HtmlDocumentBuilder(formElement); // render fields for (FormField formField : formData.getFormFields()) { renderFormField(formField, documentBuilder); } // render deprecated form properties for (FormProperty formProperty : formData.getFormProperties()) { renderFormField(new FormPropertyAdapter(formProperty), documentBuilder); } // end document element documentBuilder.endElement(); return documentBuilder.getHtmlString(); } }
protected void renderInvalidMessageElement(FormField formField, HtmlDocumentBuilder documentBuilder) { HtmlElementWriter divElement = new HtmlElementWriter(DIV_ELEMENT); String formFieldId = formField.getId(); String ifExpression = String.format(INVALID_EXPRESSION + " && " + DIRTY_EXPRESSION, formFieldId, formFieldId); divElement .attribute(NG_IF_ATTRIBUTE, ifExpression) .attribute(CLASS_ATTRIBUTE, HAS_ERROR_CLASS); // <div ng-if="....$invalid && ....$dirty"...> documentBuilder.startElement(divElement); if (!isDate(formField)) { renderInvalidValueMessage(formField, documentBuilder); renderInvalidTypeMessage(formField, documentBuilder); } else { renderInvalidDateMessage(formField, documentBuilder); } documentBuilder.endElement(); }
protected void addCommonFormFieldAttributes(FormField formField, HtmlElementWriter formControl) { String typeName = formField.getTypeName(); if (isEnum(formField) || isDate(formField)) { typeName = StringFormType.TYPE_NAME; } typeName = typeName.substring(0, 1).toUpperCase() + typeName.substring(1); String formFieldId = formField.getId(); formControl .attribute(CLASS_ATTRIBUTE, FORM_CONTROL_CLASS) .attribute(NAME_ATTRIBUTE, formFieldId) .attribute(CAM_VARIABLE_TYPE_ATTRIBUTE, typeName) .attribute(CAM_VARIABLE_NAME_ATTRIBUTE, formFieldId); // add validation constraints for (FormFieldValidationConstraint constraint : formField.getValidationConstraints()) { String constraintName = constraint.getName(); String configuration = (String) constraint.getConfiguration(); formControl.attribute(constraintName, configuration); } }
protected void renderDatePicker(FormField formField, HtmlDocumentBuilder documentBuilder) { boolean isReadOnly = isReadOnly(formField); HtmlElementWriter inputField = createInputField(formField);
protected HtmlElementWriter createInputField(FormField formField) { HtmlElementWriter inputField = new HtmlElementWriter(INPUT_ELEMENT, true); addCommonFormFieldAttributes(formField, inputField); inputField.attribute(TYPE_ATTRIBUTE, TEXT_INPUT_TYPE); return inputField; }
protected void renderInvalidTypeMessage(FormField formField, HtmlDocumentBuilder documentBuilder) { HtmlElementWriter divElement = new HtmlElementWriter(DIV_ELEMENT); String formFieldId = formField.getId(); String expression = String.format(TYPE_ERROR_EXPRESSION, formFieldId); String typeName = formField.getTypeName(); if (isEnum(formField)) { typeName = StringFormType.TYPE_NAME; } divElement .attribute(NG_SHOW_ATTRIBUTE, expression) .attribute(CLASS_ATTRIBUTE, HELP_BLOCK_CLASS) .textContent(String.format(TYPE_FIELD_MESSAGE, typeName)); documentBuilder .startElement(divElement) .endElement(); }
protected String renderFormData(FormData formData) { if(formData == null || (formData.getFormFields() == null || formData.getFormFields().isEmpty()) && (formData.getFormProperties() == null || formData.getFormProperties().isEmpty())) { return null; } else { HtmlElementWriter formElement = new HtmlElementWriter(FORM_ELEMENT) .attribute(NAME_ATTRIBUTE, GENERATED_FORM_NAME) .attribute(ROLE_ATTRIBUTE, FORM_ROLE); HtmlDocumentBuilder documentBuilder = new HtmlDocumentBuilder(formElement); // render fields for (FormField formField : formData.getFormFields()) { renderFormField(formField, documentBuilder); } // render deprecated form properties for (FormProperty formProperty : formData.getFormProperties()) { renderFormField(new FormPropertyAdapter(formProperty), documentBuilder); } // end document element documentBuilder.endElement(); return documentBuilder.getHtmlString(); } }
protected void renderFormField(FormField formField, HtmlDocumentBuilder documentBuilder) { // start group HtmlElementWriter divElement = new HtmlElementWriter(DIV_ELEMENT) .attribute(CLASS_ATTRIBUTE, FORM_GROUP_CLASS); documentBuilder.startElement(divElement); String formFieldId = formField.getId(); String formFieldLabel = formField.getLabel(); // write label if (formFieldLabel != null && !formFieldLabel.isEmpty()) { HtmlElementWriter labelElement = new HtmlElementWriter(LABEL_ELEMENT) .attribute(FOR_ATTRIBUTE, formFieldId) .textContent(formFieldLabel); // <label for="...">...</label> documentBuilder.startElement(labelElement).endElement(); } // render form control if(isEnum(formField)) { // <select ...> renderSelectBox(formField, documentBuilder); } else if (isDate(formField)){ renderDatePicker(formField, documentBuilder); } else { // <input ...> renderInputField(formField, documentBuilder); } renderInvalidMessageElement(formField, documentBuilder); // end group documentBuilder.endElement(); }
protected void renderInvalidMessageElement(FormField formField, HtmlDocumentBuilder documentBuilder) { HtmlElementWriter divElement = new HtmlElementWriter(DIV_ELEMENT); String formFieldId = formField.getId(); String ifExpression = String.format(INVALID_EXPRESSION + " && " + DIRTY_EXPRESSION, formFieldId, formFieldId); divElement .attribute(NG_IF_ATTRIBUTE, ifExpression) .attribute(CLASS_ATTRIBUTE, HAS_ERROR_CLASS); // <div ng-if="....$invalid && ....$dirty"...> documentBuilder.startElement(divElement); if (!isDate(formField)) { renderInvalidValueMessage(formField, documentBuilder); renderInvalidTypeMessage(formField, documentBuilder); } else { renderInvalidDateMessage(formField, documentBuilder); } documentBuilder.endElement(); }
protected void renderSelectBox(FormField formField, HtmlDocumentBuilder documentBuilder) { HtmlElementWriter selectBox = new HtmlElementWriter(SELECT_ELEMENT, false); addCommonFormFieldAttributes(formField, selectBox); // <select ...> documentBuilder.startElement(selectBox); // <option ...> renderSelectOptions(formField, documentBuilder); // </select> documentBuilder.endElement(); }
protected void renderInputField(FormField formField, HtmlDocumentBuilder documentBuilder) { HtmlElementWriter inputField = new HtmlElementWriter(INPUT_ELEMENT, true); addCommonFormFieldAttributes(formField, inputField); String inputType = !isBoolean(formField) ? TEXT_INPUT_TYPE : CHECKBOX_INPUT_TYPE; inputField.attribute(TYPE_ATTRIBUTE, inputType); // add default value Object defaultValue = formField.getDefaultValue(); if(defaultValue != null) { inputField.attribute(VALUE_ATTRIBUTE, defaultValue.toString()); } // <input ... /> documentBuilder.startElement(inputField).endElement(); }
protected void addCommonFormFieldAttributes(FormField formField, HtmlElementWriter formControl) { String typeName = formField.getTypeName(); if (isEnum(formField) || isDate(formField)) { typeName = StringFormType.TYPE_NAME; } typeName = typeName.substring(0, 1).toUpperCase() + typeName.substring(1); String formFieldId = formField.getId(); formControl .attribute(CLASS_ATTRIBUTE, FORM_CONTROL_CLASS) .attribute(NAME_ATTRIBUTE, formFieldId) .attribute(CAM_VARIABLE_TYPE_ATTRIBUTE, typeName) .attribute(CAM_VARIABLE_NAME_ATTRIBUTE, formFieldId); // add validation constraints for (FormFieldValidationConstraint constraint : formField.getValidationConstraints()) { String constraintName = constraint.getName(); String configuration = (String) constraint.getConfiguration(); formControl.attribute(constraintName, configuration); } }