use of org.apache.ofbiz.widget.model.ModelForm in project ofbiz-framework by apache.
the class ArtifactInfoFactory method prepareTaskForComponentAnalysis.
private Callable<Void> prepareTaskForComponentAnalysis(final ComponentConfig componentConfig) {
return new Callable<Void>() {
@Override
public Void call() throws Exception {
String componentName = componentConfig.getGlobalName();
String rootComponentPath = componentConfig.getRootLocation();
List<File> screenFiles = new ArrayList<File>();
List<File> formFiles = new ArrayList<File>();
List<File> controllerFiles = new ArrayList<File>();
try {
screenFiles = FileUtil.findXmlFiles(rootComponentPath, null, "screens", "widget-screen.xsd");
} catch (IOException ioe) {
Debug.logWarning(ioe.getMessage(), module);
}
try {
formFiles = FileUtil.findXmlFiles(rootComponentPath, null, "forms", "widget-form.xsd");
} catch (IOException ioe) {
Debug.logWarning(ioe.getMessage(), module);
}
try {
controllerFiles = FileUtil.findXmlFiles(rootComponentPath, null, "site-conf", "site-conf.xsd");
} catch (IOException ioe) {
Debug.logWarning(ioe.getMessage(), module);
}
for (File screenFile : screenFiles) {
String screenFilePath = screenFile.getAbsolutePath();
screenFilePath = screenFilePath.replace('\\', '/');
String screenFileRelativePath = screenFilePath.substring(rootComponentPath.length());
String screenLocation = "component://" + componentName + "/" + screenFileRelativePath;
Map<String, ModelScreen> modelScreenMap = null;
try {
modelScreenMap = ScreenFactory.getScreensFromLocation(screenLocation);
} catch (Exception exc) {
Debug.logWarning(exc.getMessage(), module);
}
for (String screenName : modelScreenMap.keySet()) {
getScreenWidgetArtifactInfo(screenName, screenLocation);
}
}
for (File formFile : formFiles) {
String formFilePath = formFile.getAbsolutePath();
formFilePath = formFilePath.replace('\\', '/');
String formFileRelativePath = formFilePath.substring(rootComponentPath.length());
String formLocation = "component://" + componentName + "/" + formFileRelativePath;
Map<String, ModelForm> modelFormMap = null;
try {
modelFormMap = FormFactory.getFormsFromLocation(formLocation, getEntityModelReader(), getDispatchContext());
} catch (Exception exc) {
Debug.logWarning(exc.getMessage(), module);
}
for (String formName : modelFormMap.keySet()) {
try {
getFormWidgetArtifactInfo(formName, formLocation);
} catch (GeneralException ge) {
Debug.logWarning(ge.getMessage(), module);
}
}
}
for (File controllerFile : controllerFiles) {
URL controllerUrl = null;
try {
controllerUrl = controllerFile.toURI().toURL();
} catch (MalformedURLException mue) {
Debug.logWarning(mue.getMessage(), module);
}
if (controllerUrl == null)
continue;
ControllerConfig cc = ConfigXMLReader.getControllerConfig(controllerUrl);
for (String requestUri : cc.getRequestMapMap().keySet()) {
try {
getControllerRequestArtifactInfo(controllerUrl, requestUri);
} catch (GeneralException e) {
Debug.logWarning(e.getMessage(), module);
}
}
for (String viewUri : cc.getViewMapMap().keySet()) {
try {
getControllerViewArtifactInfo(controllerUrl, viewUri);
} catch (GeneralException e) {
Debug.logWarning(e.getMessage(), module);
}
}
}
return null;
}
};
}
use of org.apache.ofbiz.widget.model.ModelForm in project ofbiz-framework by apache.
the class DataResourceWorker method renderDataResourceAsText.
public static void renderDataResourceAsText(LocalDispatcher dispatcher, Delegator delegator, String dataResourceId, Appendable out, Map<String, Object> templateContext, Locale locale, String targetMimeTypeId, boolean cache, List<GenericValue> webAnalytics) throws GeneralException, IOException {
if (delegator == null) {
delegator = dispatcher.getDelegator();
}
if (dataResourceId == null) {
throw new GeneralException("Cannot lookup data resource with for a null dataResourceId");
}
if (templateContext == null) {
templateContext = new HashMap<>();
}
if (UtilValidate.isEmpty(targetMimeTypeId)) {
targetMimeTypeId = "text/html";
}
if (locale == null) {
locale = Locale.getDefault();
}
// FIXME correctly propagate the theme, then fixes also the related FIXME below
VisualTheme visualTheme = ThemeFactory.getVisualThemeFromId("COMMON");
ModelTheme modelTheme = visualTheme.getModelTheme();
// if the target mimeTypeId is not a text type, throw an exception
if (!targetMimeTypeId.startsWith("text/")) {
throw new GeneralException("The desired mime-type is not a text type, cannot render as text: " + targetMimeTypeId);
}
// get the data resource object
GenericValue dataResource = EntityQuery.use(delegator).from("DataResource").where("dataResourceId", dataResourceId).cache(cache).queryOne();
if (dataResource == null) {
throw new GeneralException("No data resource object found for dataResourceId: [" + dataResourceId + "]");
}
// a data template attached to the data resource
String dataTemplateTypeId = dataResource.getString("dataTemplateTypeId");
// no template; or template is NONE; render the data
if (UtilValidate.isEmpty(dataTemplateTypeId) || "NONE".equals(dataTemplateTypeId)) {
DataResourceWorker.writeDataResourceText(dataResource, targetMimeTypeId, locale, templateContext, delegator, out, cache);
} else {
// a template is defined; render the template first
templateContext.put("mimeTypeId", targetMimeTypeId);
// FTL template
if ("FTL".equals(dataTemplateTypeId)) {
try {
// get the template data for rendering
String templateText = getDataResourceText(dataResource, targetMimeTypeId, locale, templateContext, delegator, cache);
// if use web analytics.
if (UtilValidate.isNotEmpty(webAnalytics)) {
StringBuffer newTemplateText = new StringBuffer(templateText);
String webAnalyticsCode = "<script language=\"JavaScript\" type=\"text/javascript\">";
for (GenericValue webAnalytic : webAnalytics) {
StringWrapper wrapString = StringUtil.wrapString((String) webAnalytic.get("webAnalyticsCode"));
webAnalyticsCode += wrapString.toString();
}
webAnalyticsCode += "</script>";
newTemplateText.insert(templateText.lastIndexOf("</head>"), webAnalyticsCode);
templateText = newTemplateText.toString();
}
// render the FTL template
boolean useTemplateCache = cache && !UtilProperties.getPropertyAsBoolean("content", "disable.ftl.template.cache", false);
// Do not use dataResource.lastUpdatedStamp for dataResource template caching as it may use ftl file or electronicText
// If dataResource using ftl file use nowTimestamp to avoid freemarker caching
Timestamp lastUpdatedStamp = UtilDateTime.nowTimestamp();
// If dataResource is type of ELECTRONIC_TEXT then only use the lastUpdatedStamp of electronicText entity for freemarker caching
if ("ELECTRONIC_TEXT".equals(dataResource.getString("dataResourceTypeId"))) {
GenericValue electronicText = dataResource.getRelatedOne("ElectronicText", true);
if (electronicText != null) {
lastUpdatedStamp = electronicText.getTimestamp("lastUpdatedStamp");
}
}
FreeMarkerWorker.renderTemplateFromString("delegator:" + delegator.getDelegatorName() + ":DataResource:" + dataResourceId, templateText, templateContext, out, lastUpdatedStamp.getTime(), useTemplateCache);
} catch (TemplateException e) {
throw new GeneralException("Error rendering FTL template", e);
}
} else if ("XSLT".equals(dataTemplateTypeId)) {
File targetFileLocation = new File(System.getProperty("ofbiz.home") + "/runtime/tempfiles/docbook.css");
// This is related with the other FIXME above: we need to correctly propagate the theme.
String defaultVisualThemeId = EntityUtilProperties.getPropertyValue("general", "VISUAL_THEME", delegator);
visualTheme = ThemeFactory.getVisualThemeFromId(defaultVisualThemeId);
modelTheme = visualTheme.getModelTheme();
String docbookStylesheet = modelTheme.getProperty("VT_DOCBOOKSTYLESHEET").toString();
File sourceFileLocation = new File(System.getProperty("ofbiz.home") + "/themes" + docbookStylesheet.substring(1, docbookStylesheet.length() - 1));
UtilMisc.copyFile(sourceFileLocation, targetFileLocation);
// get the template data for rendering
String templateLocation = DataResourceWorker.getContentFile(dataResource.getString("dataResourceTypeId"), dataResource.getString("objectInfo"), (String) templateContext.get("contextRoot")).toString();
// render the XSLT template and file
String outDoc = null;
try {
outDoc = XslTransform.renderTemplate(templateLocation, (String) templateContext.get("docFile"));
} catch (TransformerException c) {
Debug.logError("XSL TransformerException: " + c.getMessage(), module);
}
out.append(outDoc);
// Screen Widget template
} else if ("SCREEN_COMBINED".equals(dataTemplateTypeId)) {
try {
MapStack<String> context = MapStack.create(templateContext);
context.put("locale", locale);
// prepare the map for preRenderedContent
String textData = (String) context.get("textData");
if (UtilValidate.isNotEmpty(textData)) {
Map<String, Object> prc = new HashMap<>();
String mapKey = (String) context.get("mapKey");
if (mapKey != null) {
prc.put(mapKey, mapKey);
}
// used for default screen defs
prc.put("body", textData);
context.put("preRenderedContent", prc);
}
// get the screen renderer; or create a new one
ScreenRenderer screens = (ScreenRenderer) context.get("screens");
if (screens == null) {
// TODO: replace "screen" to support dynamic rendering of different output
ScreenStringRenderer screenStringRenderer = new MacroScreenRenderer(modelTheme.getType("screen"), modelTheme.getScreenRendererLocation("screen"));
screens = new ScreenRenderer(out, context, screenStringRenderer);
screens.getContext().put("screens", screens);
}
// render the screen
ModelScreen modelScreen = null;
ScreenStringRenderer renderer = screens.getScreenStringRenderer();
String combinedName = dataResource.getString("objectInfo");
if ("URL_RESOURCE".equals(dataResource.getString("dataResourceTypeId")) && UtilValidate.isNotEmpty(combinedName) && combinedName.startsWith("component://")) {
modelScreen = ScreenFactory.getScreenFromLocation(combinedName);
} else {
// stored in a single file, long or short text
Document screenXml = UtilXml.readXmlDocument(getDataResourceText(dataResource, targetMimeTypeId, locale, templateContext, delegator, cache), true, true);
Map<String, ModelScreen> modelScreenMap = ScreenFactory.readScreenDocument(screenXml, "DataResourceId: " + dataResource.getString("dataResourceId"));
if (UtilValidate.isNotEmpty(modelScreenMap)) {
// get first entry, only one screen allowed per file
Map.Entry<String, ModelScreen> entry = modelScreenMap.entrySet().iterator().next();
modelScreen = entry.getValue();
}
}
if (UtilValidate.isNotEmpty(modelScreen)) {
modelScreen.renderScreenString(out, context, renderer);
} else {
throw new GeneralException("The dataResource file [" + dataResourceId + "] could not be found");
}
} catch (SAXException | ParserConfigurationException e) {
throw new GeneralException("Error rendering Screen template", e);
} catch (TemplateException e) {
throw new GeneralException("Error creating Screen renderer", e);
}
} else if ("FORM_COMBINED".equals(dataTemplateTypeId)) {
try {
Map<String, Object> context = UtilGenerics.checkMap(templateContext.get("globalContext"));
context.put("locale", locale);
context.put("simpleEncoder", UtilCodec.getEncoder(modelTheme.getEncoder("screen")));
HttpServletRequest request = (HttpServletRequest) context.get("request");
HttpServletResponse response = (HttpServletResponse) context.get("response");
ModelForm modelForm = null;
ModelReader entityModelReader = delegator.getModelReader();
String formText = getDataResourceText(dataResource, targetMimeTypeId, locale, templateContext, delegator, cache);
Document formXml = UtilXml.readXmlDocument(formText, true, true);
Map<String, ModelForm> modelFormMap = FormFactory.readFormDocument(formXml, entityModelReader, dispatcher.getDispatchContext(), null);
if (UtilValidate.isNotEmpty(modelFormMap)) {
// get first entry, only one form allowed per file
Map.Entry<String, ModelForm> entry = modelFormMap.entrySet().iterator().next();
modelForm = entry.getValue();
}
String formrenderer = modelTheme.getFormRendererLocation("screen");
MacroFormRenderer renderer = new MacroFormRenderer(formrenderer, request, response);
FormRenderer formRenderer = new FormRenderer(modelForm, renderer);
formRenderer.render(out, context);
} catch (SAXException | ParserConfigurationException e) {
throw new GeneralException("Error rendering Screen template", e);
} catch (TemplateException e) {
throw new GeneralException("Error creating Screen renderer", e);
} catch (Exception e) {
throw new GeneralException("Error rendering Screen template", e);
}
} else {
throw new GeneralException("The dataTemplateTypeId [" + dataTemplateTypeId + "] is not yet supported");
}
}
}
use of org.apache.ofbiz.widget.model.ModelForm in project ofbiz-framework by apache.
the class MacroFormRenderer method renderSubmitField.
public void renderSubmitField(Appendable writer, Map<String, Object> context, SubmitField submitField) throws IOException {
ModelFormField modelFormField = submitField.getModelFormField();
ModelForm modelForm = modelFormField.getModelForm();
String event = modelFormField.getEvent();
String action = modelFormField.getAction(context);
String title = modelFormField.getTitle(context);
String name = modelFormField.getParameterName(context);
String buttonType = submitField.getButtonType();
String formName = FormRenderer.getCurrentFormName(modelForm, context);
String imgSrc = submitField.getImageLocation(context);
String confirmation = submitField.getConfirmation(context);
String className = "";
String alert = "false";
if (UtilValidate.isNotEmpty(modelFormField.getWidgetStyle())) {
className = modelFormField.getWidgetStyle();
if (modelFormField.shouldBeRed(context)) {
alert = "true";
}
}
String formId = FormRenderer.getCurrentContainerId(modelForm, context);
List<ModelForm.UpdateArea> updateAreas = modelForm.getOnSubmitUpdateAreas();
// This is here for backwards compatibility. Use on-event-update-area
// elements instead.
String backgroundSubmitRefreshTarget = submitField.getBackgroundSubmitRefreshTarget(context);
if (UtilValidate.isNotEmpty(backgroundSubmitRefreshTarget)) {
if (updateAreas == null) {
updateAreas = new LinkedList<>();
}
updateAreas.add(new ModelForm.UpdateArea("submit", formId, backgroundSubmitRefreshTarget));
}
boolean ajaxEnabled = (UtilValidate.isNotEmpty(updateAreas) || UtilValidate.isNotEmpty(backgroundSubmitRefreshTarget)) && this.javaScriptEnabled;
String ajaxUrl = "";
if (ajaxEnabled) {
ajaxUrl = createAjaxParamsFromUpdateAreas(updateAreas, "", context);
}
String tabindex = modelFormField.getTabindex();
StringWriter sr = new StringWriter();
sr.append("<@renderSubmitField ");
sr.append("buttonType=\"");
sr.append(buttonType);
sr.append("\" className=\"");
sr.append(className);
sr.append("\" alert=\"");
sr.append(alert);
sr.append("\" formName=\"");
sr.append(formName);
sr.append("\" title=\"");
sr.append(encode(title, modelFormField, context));
sr.append("\" name=\"");
sr.append(name);
sr.append("\" event=\"");
if (event != null) {
sr.append(event);
}
sr.append("\" action=\"");
if (action != null) {
sr.append(action);
}
sr.append("\" imgSrc=\"");
sr.append(imgSrc);
sr.append("\" containerId=\"");
if (ajaxEnabled) {
sr.append(formId);
}
sr.append("\" confirmation =\"");
sr.append(confirmation);
sr.append("\" ajaxUrl=\"");
if (ajaxEnabled) {
sr.append(ajaxUrl);
}
sr.append("\" tabindex=\"");
sr.append(tabindex);
sr.append("\" />");
executeMacro(writer, sr.toString());
this.appendTooltip(writer, context, modelFormField);
}
use of org.apache.ofbiz.widget.model.ModelForm in project ofbiz-framework by apache.
the class MacroFormRenderer method renderDateFindField.
public void renderDateFindField(Appendable writer, Map<String, Object> context, DateFindField dateFindField) throws IOException {
ModelFormField modelFormField = dateFindField.getModelFormField();
Locale locale = (Locale) context.get("locale");
String opEquals = UtilProperties.getMessage("conditionalUiLabels", "equals", locale);
String opGreaterThan = UtilProperties.getMessage("conditionalUiLabels", "greater_than", locale);
String opSameDay = UtilProperties.getMessage("conditionalUiLabels", "same_day", locale);
String opGreaterThanFromDayStart = UtilProperties.getMessage("conditionalUiLabels", "greater_than_from_day_start", locale);
String opLessThan = UtilProperties.getMessage("conditionalUiLabels", "less_than", locale);
String opUpToDay = UtilProperties.getMessage("conditionalUiLabels", "up_to_day", locale);
String opUpThruDay = UtilProperties.getMessage("conditionalUiLabels", "up_thru_day", locale);
String opIsEmpty = UtilProperties.getMessage("conditionalUiLabels", "is_empty", locale);
String conditionGroup = modelFormField.getConditionGroup();
Map<String, String> uiLabelMap = UtilGenerics.checkMap(context.get("uiLabelMap"));
if (uiLabelMap == null) {
Debug.logWarning("Could not find uiLabelMap in context", module);
}
String localizedInputTitle = "", localizedIconTitle = "";
String className = "";
String alert = "false";
if (UtilValidate.isNotEmpty(modelFormField.getWidgetStyle())) {
className = modelFormField.getWidgetStyle();
if (modelFormField.shouldBeRed(context)) {
alert = "true";
}
}
String name = modelFormField.getParameterName(context);
// the default values for a timestamp
int size = 25;
int maxlength = 30;
String dateType = dateFindField.getType();
if ("date".equals(dateType)) {
size = maxlength = 10;
if (uiLabelMap != null) {
localizedInputTitle = uiLabelMap.get("CommonFormatDate");
}
} else if ("time".equals(dateFindField.getType())) {
size = maxlength = 8;
if (uiLabelMap != null) {
localizedInputTitle = uiLabelMap.get("CommonFormatTime");
}
} else {
if (uiLabelMap != null) {
localizedInputTitle = uiLabelMap.get("CommonFormatDateTime");
}
}
String value = modelFormField.getEntry(context, dateFindField.getDefaultValue(context));
if (value == null) {
value = "";
}
// search for a localized label for the icon
if (uiLabelMap != null) {
localizedIconTitle = uiLabelMap.get("CommonViewCalendar");
}
String formName = "";
String defaultDateTimeString = "";
StringBuilder imgSrc = new StringBuilder();
// add calendar pop-up button and seed data IF this is not a "time" type date-find
if (!"time".equals(dateFindField.getType())) {
ModelForm modelForm = modelFormField.getModelForm();
formName = FormRenderer.getCurrentFormName(modelForm, context);
defaultDateTimeString = UtilHttp.encodeBlanks(modelFormField.getEntry(context, dateFindField.getDefaultDateTimeString(context)));
this.appendContentUrl(imgSrc, "/images/cal.gif");
}
String defaultOptionFrom = dateFindField.getDefaultOptionFrom(context);
String defaultOptionThru = dateFindField.getDefaultOptionThru(context);
String value2 = modelFormField.getEntry(context);
if (value2 == null) {
value2 = "";
}
if (context.containsKey("parameters")) {
Map<String, Object> parameters = UtilGenerics.checkMap(context.get("parameters"));
if (parameters.containsKey(name + "_fld0_value")) {
value = (String) parameters.get(name + "_fld0_value");
}
if (parameters.containsKey(name + "_fld1_value")) {
value2 = (String) parameters.get(name + "_fld1_value");
}
}
String titleStyle = "";
if (UtilValidate.isNotEmpty(modelFormField.getTitleStyle())) {
titleStyle = modelFormField.getTitleStyle();
}
String tabindex = modelFormField.getTabindex();
StringWriter sr = new StringWriter();
sr.append("<@renderDateFindField ");
sr.append(" className=\"");
sr.append(className);
sr.append("\" alert=\"");
sr.append(alert);
sr.append("\" name=\"");
sr.append(name);
sr.append("\" localizedInputTitle=\"");
sr.append(localizedInputTitle);
sr.append("\" value=\"");
sr.append(value);
sr.append("\" value2=\"");
sr.append(value2);
sr.append("\" size=\"");
sr.append(Integer.toString(size));
sr.append("\" maxlength=\"");
sr.append(Integer.toString(maxlength));
sr.append("\" dateType=\"");
sr.append(dateType);
sr.append("\" formName=\"");
sr.append(formName);
sr.append("\" defaultDateTimeString=\"");
sr.append(defaultDateTimeString);
sr.append("\" imgSrc=\"");
sr.append(imgSrc.toString());
sr.append("\" conditionGroup=\"");
sr.append(conditionGroup);
sr.append("\" localizedIconTitle=\"");
sr.append(localizedIconTitle);
sr.append("\" titleStyle=\"");
sr.append(titleStyle);
sr.append("\" defaultOptionFrom=\"");
sr.append(defaultOptionFrom);
sr.append("\" defaultOptionThru=\"");
sr.append(defaultOptionThru);
sr.append("\" opEquals=\"");
sr.append(opEquals);
sr.append("\" opSameDay=\"");
sr.append(opSameDay);
sr.append("\" opGreaterThanFromDayStart=\"");
sr.append(opGreaterThanFromDayStart);
sr.append("\" opGreaterThan=\"");
sr.append(opGreaterThan);
sr.append("\" opGreaterThan=\"");
sr.append(opGreaterThan);
sr.append("\" opLessThan=\"");
sr.append(opLessThan);
sr.append("\" opUpToDay=\"");
sr.append(opUpToDay);
sr.append("\" opUpThruDay=\"");
sr.append(opUpThruDay);
sr.append("\" opIsEmpty=\"");
sr.append(opIsEmpty);
sr.append("\" tabindex=\"");
sr.append(tabindex);
sr.append("\" />");
executeMacro(writer, sr.toString());
this.appendTooltip(writer, context, modelFormField);
}
use of org.apache.ofbiz.widget.model.ModelForm in project ofbiz-framework by apache.
the class MacroFormRenderer method renderDateTimeField.
public void renderDateTimeField(Appendable writer, Map<String, Object> context, DateTimeField dateTimeField) throws IOException {
ModelFormField modelFormField = dateTimeField.getModelFormField();
String paramName = modelFormField.getParameterName(context);
String defaultDateTimeString = dateTimeField.getDefaultDateTimeString(context);
String className = "";
String alert = "false";
String name = "";
String formattedMask = "";
String event = modelFormField.getEvent();
String action = modelFormField.getAction(context);
if (UtilValidate.isNotEmpty(modelFormField.getWidgetStyle())) {
className = modelFormField.getWidgetStyle();
if (modelFormField.shouldBeRed(context)) {
alert = "true";
}
}
boolean useTimeDropDown = "time-dropdown".equals(dateTimeField.getInputMethod());
String stepString = dateTimeField.getStep();
int step = 1;
StringBuilder timeValues = new StringBuilder();
if (useTimeDropDown && UtilValidate.isNotEmpty(step)) {
try {
step = Integer.parseInt(stepString);
} catch (IllegalArgumentException e) {
Debug.logWarning("Invalid value for step property for field[" + paramName + "] with input-method=\"time-dropdown\" " + " Found Value [" + stepString + "] " + e.getMessage(), module);
}
timeValues.append("[");
for (int i = 0; i <= 59; ) {
if (i != 0) {
timeValues.append(", ");
}
timeValues.append(i);
i += step;
}
timeValues.append("]");
}
Map<String, String> uiLabelMap = UtilGenerics.checkMap(context.get("uiLabelMap"));
if (uiLabelMap == null) {
Debug.logWarning("Could not find uiLabelMap in context", module);
}
String localizedInputTitle = "", localizedIconTitle = "";
// whether the date field is short form, yyyy-mm-dd
boolean shortDateInput = ("date".equals(dateTimeField.getType()) || useTimeDropDown ? true : false);
if (useTimeDropDown) {
name = UtilHttp.makeCompositeParam(paramName, "date");
} else {
name = paramName;
}
// the default values for a timestamp
int size = 25;
int maxlength = 30;
if (shortDateInput) {
size = maxlength = 10;
if (uiLabelMap != null) {
localizedInputTitle = uiLabelMap.get("CommonFormatDate");
}
} else if ("time".equals(dateTimeField.getType())) {
size = maxlength = 8;
if (uiLabelMap != null) {
localizedInputTitle = uiLabelMap.get("CommonFormatTime");
}
} else {
if (uiLabelMap != null) {
localizedInputTitle = uiLabelMap.get("CommonFormatDateTime");
}
}
/*
* FIXME: Using a builder here is a hack. Replace the builder with appropriate code.
*/
ModelFormFieldBuilder builder = new ModelFormFieldBuilder(modelFormField);
boolean memEncodeOutput = modelFormField.getEncodeOutput();
if (useTimeDropDown) {
// If time-dropdown deactivate encodingOutput for found hour and minutes
// FIXME: Encoding should be controlled by the renderer, not by the model.
builder.setEncodeOutput(false);
}
// FIXME: modelFormField.getEntry ignores shortDateInput when converting Date objects to Strings.
if (useTimeDropDown) {
builder.setEncodeOutput(memEncodeOutput);
}
modelFormField = builder.build();
String contextValue = modelFormField.getEntry(context, dateTimeField.getDefaultValue(context));
String value = contextValue;
if (UtilValidate.isNotEmpty(value)) {
if (value.length() > maxlength) {
value = value.substring(0, maxlength);
}
}
String id = modelFormField.getCurrentContainerId(context);
ModelForm modelForm = modelFormField.getModelForm();
String formName = FormRenderer.getCurrentFormName(modelForm, context);
String timeDropdown = dateTimeField.getInputMethod();
String timeDropdownParamName = "";
String classString = "";
boolean isTwelveHour = false;
String timeHourName = "";
int hour2 = 0, hour1 = 0, minutes = 0;
String timeMinutesName = "";
String amSelected = "", pmSelected = "", ampmName = "";
String compositeType = "";
// search for a localized label for the icon
if (uiLabelMap != null) {
localizedIconTitle = uiLabelMap.get("CommonViewCalendar");
}
if (!"time".equals(dateTimeField.getType())) {
String tempParamName;
if (useTimeDropDown) {
tempParamName = UtilHttp.makeCompositeParam(paramName, "date");
} else {
tempParamName = paramName;
}
timeDropdownParamName = tempParamName;
defaultDateTimeString = UtilHttp.encodeBlanks(modelFormField.getEntry(context, defaultDateTimeString));
}
// dropdowns
if (useTimeDropDown) {
className = modelFormField.getWidgetStyle();
classString = (className != null ? className : "");
isTwelveHour = "12".equals(dateTimeField.getClock());
// set the Calendar to the default time of the form or now()
Calendar cal = null;
try {
Timestamp defaultTimestamp = Timestamp.valueOf(contextValue);
cal = Calendar.getInstance();
cal.setTime(defaultTimestamp);
} catch (IllegalArgumentException e) {
Debug.logWarning("Form widget field [" + paramName + "] with input-method=\"time-dropdown\" was not able to understand the default time [" + defaultDateTimeString + "]. The parsing error was: " + e.getMessage(), module);
}
timeHourName = UtilHttp.makeCompositeParam(paramName, "hour");
if (cal != null) {
int hour = cal.get(Calendar.HOUR_OF_DAY);
hour2 = hour;
if (hour == 0) {
hour = 12;
}
if (hour > 12) {
hour -= 12;
}
hour1 = hour;
minutes = cal.get(Calendar.MINUTE);
}
timeMinutesName = UtilHttp.makeCompositeParam(paramName, "minutes");
compositeType = UtilHttp.makeCompositeParam(paramName, "compositeType");
// if 12 hour clock, write the AM/PM selector
if (isTwelveHour) {
amSelected = ((cal != null && cal.get(Calendar.AM_PM) == Calendar.AM) ? "selected" : "");
pmSelected = ((cal != null && cal.get(Calendar.AM_PM) == Calendar.PM) ? "selected" : "");
ampmName = UtilHttp.makeCompositeParam(paramName, "ampm");
}
}
// check for required field style on single forms
if ("single".equals(modelFormField.getModelForm().getType()) && modelFormField.getRequiredField()) {
String requiredStyle = modelFormField.getRequiredFieldStyle();
if (UtilValidate.isEmpty(requiredStyle)) {
requiredStyle = "required";
}
if (UtilValidate.isEmpty(className)) {
className = requiredStyle;
} else {
className = requiredStyle + " " + className;
}
}
String mask = dateTimeField.getMask();
if ("Y".equals(mask)) {
if ("date".equals(dateTimeField.getType())) {
formattedMask = "9999-99-99";
} else if ("time".equals(dateTimeField.getType())) {
formattedMask = "99:99:99";
} else if ("timestamp".equals(dateTimeField.getType())) {
formattedMask = "9999-99-99 99:99:99";
}
}
String tabindex = modelFormField.getTabindex();
StringWriter sr = new StringWriter();
sr.append("<@renderDateTimeField ");
sr.append("name=\"");
sr.append(name);
sr.append("\" className=\"");
sr.append(className);
sr.append("\" alert=\"");
sr.append(alert);
sr.append("\" value=\"");
sr.append(value);
sr.append("\" title=\"");
sr.append(localizedInputTitle);
sr.append("\" size=\"");
sr.append(Integer.toString(size));
sr.append("\" maxlength=\"");
sr.append(Integer.toString(maxlength));
sr.append("\" step=\"");
sr.append(Integer.toString(step));
sr.append("\" timeValues=\"");
sr.append(timeValues.toString());
sr.append("\" id=\"");
sr.append(id);
sr.append("\" event=\"");
sr.append(event);
sr.append("\" action=\"");
sr.append(action);
sr.append("\" dateType=\"");
sr.append(dateTimeField.getType());
sr.append("\" shortDateInput=");
sr.append(Boolean.toString(shortDateInput));
sr.append(" timeDropdownParamName=\"");
sr.append(timeDropdownParamName);
sr.append("\" defaultDateTimeString=\"");
sr.append(defaultDateTimeString);
sr.append("\" localizedIconTitle=\"");
sr.append(localizedIconTitle);
sr.append("\" timeDropdown=\"");
sr.append(timeDropdown);
sr.append("\" timeHourName=\"");
sr.append(timeHourName);
sr.append("\" classString=\"");
sr.append(classString);
sr.append("\" hour1=");
sr.append(Integer.toString(hour1));
sr.append(" hour2=");
sr.append(Integer.toString(hour2));
sr.append(" timeMinutesName=\"");
sr.append(timeMinutesName);
sr.append("\" minutes=");
sr.append(Integer.toString(minutes));
sr.append(" isTwelveHour=");
sr.append(Boolean.toString(isTwelveHour));
sr.append(" ampmName=\"");
sr.append(ampmName);
sr.append("\" amSelected=\"");
sr.append(amSelected);
sr.append("\" pmSelected=\"");
sr.append(pmSelected);
sr.append("\" compositeType=\"");
sr.append(compositeType);
sr.append("\" formName=\"");
sr.append(formName);
sr.append("\" mask=\"");
sr.append(formattedMask);
sr.append("\" tabindex=\"");
sr.append(tabindex);
sr.append("\" />");
executeMacro(writer, sr.toString());
this.addAsterisks(writer, context, modelFormField);
this.appendTooltip(writer, context, modelFormField);
}
Aggregations