use of org.apache.ofbiz.widget.model.ModelForm in project ofbiz-framework by apache.
the class MacroFormRenderer method makeHyperlinkByType.
public void makeHyperlinkByType(Appendable writer, String linkType, String linkStyle, String targetType, String target, Map<String, String> parameterMap, String description, String targetWindow, String confirmation, ModelFormField modelFormField, HttpServletRequest request, HttpServletResponse response, Map<String, Object> context) throws IOException {
String realLinkType = WidgetWorker.determineAutoLinkType(linkType, target, targetType, request);
String encodedDescription = encode(description, modelFormField, context);
// get the parameterized pagination index and size fields
int paginatorNumber = WidgetWorker.getPaginatorNumber(context);
ModelForm modelForm = modelFormField.getModelForm();
ModelTheme modelTheme = visualTheme.getModelTheme();
String viewIndexField = modelForm.getMultiPaginateIndexField(context);
String viewSizeField = modelForm.getMultiPaginateSizeField(context);
int viewIndex = Paginator.getViewIndex(modelForm, context);
int viewSize = Paginator.getViewSize(modelForm, context);
if (("viewIndex" + "_" + paginatorNumber).equals(viewIndexField)) {
viewIndexField = "VIEW_INDEX" + "_" + paginatorNumber;
}
if (("viewSize" + "_" + paginatorNumber).equals(viewSizeField)) {
viewSizeField = "VIEW_SIZE" + "_" + paginatorNumber;
}
if ("hidden-form".equals(realLinkType)) {
parameterMap.put(viewIndexField, Integer.toString(viewIndex));
parameterMap.put(viewSizeField, Integer.toString(viewSize));
if ("multi".equals(modelForm.getType())) {
WidgetWorker.makeHiddenFormLinkAnchor(writer, linkStyle, encodedDescription, confirmation, modelFormField, request, response, context);
// this is a bit trickier, since we can't do a nested form we'll have to put the link to submit the form in place, but put the actual form def elsewhere, ie after the big form is closed
Map<String, Object> wholeFormContext = UtilGenerics.checkMap(context.get("wholeFormContext"));
Appendable postMultiFormWriter = wholeFormContext != null ? (Appendable) wholeFormContext.get("postMultiFormWriter") : null;
if (postMultiFormWriter == null) {
postMultiFormWriter = new StringWriter();
wholeFormContext.put("postMultiFormWriter", postMultiFormWriter);
}
WidgetWorker.makeHiddenFormLinkForm(postMultiFormWriter, target, targetType, targetWindow, parameterMap, modelFormField, request, response, context);
} else {
WidgetWorker.makeHiddenFormLinkForm(writer, target, targetType, targetWindow, parameterMap, modelFormField, request, response, context);
WidgetWorker.makeHiddenFormLinkAnchor(writer, linkStyle, encodedDescription, confirmation, modelFormField, request, response, context);
}
} else {
if ("layered-modal".equals(realLinkType)) {
String uniqueItemName = "Modal_".concat(UUID.randomUUID().toString().replace("-", "_"));
String width = (String) this.request.getAttribute("width");
if (UtilValidate.isEmpty(width)) {
width = String.valueOf(modelTheme.getLinkDefaultLayeredModalWidth());
this.request.setAttribute("width", width);
}
String height = (String) this.request.getAttribute("height");
if (UtilValidate.isEmpty(height)) {
height = String.valueOf(modelTheme.getLinkDefaultLayeredModalHeight());
this.request.setAttribute("height", height);
}
this.request.setAttribute("uniqueItemName", uniqueItemName);
makeHyperlinkString(writer, linkStyle, targetType, target, parameterMap, encodedDescription, confirmation, modelFormField, request, response, context, targetWindow);
this.request.removeAttribute("uniqueItemName");
this.request.removeAttribute("height");
this.request.removeAttribute("width");
} else {
makeHyperlinkString(writer, linkStyle, targetType, target, parameterMap, encodedDescription, confirmation, modelFormField, request, response, context, targetWindow);
}
}
}
use of org.apache.ofbiz.widget.model.ModelForm in project ofbiz-framework by apache.
the class MacroFormRenderer method renderDropDownField.
public void renderDropDownField(Appendable writer, Map<String, Object> context, DropDownField dropDownField) throws IOException {
ModelFormField modelFormField = dropDownField.getModelFormField();
ModelForm modelForm = modelFormField.getModelForm();
String currentValue = modelFormField.getEntry(context);
String conditionGroup = modelFormField.getConditionGroup();
List<ModelFormField.OptionValue> allOptionValues = dropDownField.getAllOptionValues(context, WidgetWorker.getDelegator(context));
ModelFormField.AutoComplete autoComplete = dropDownField.getAutoComplete();
String event = modelFormField.getEvent();
String action = modelFormField.getAction(context);
Integer textSize = Integer.valueOf(0);
if (UtilValidate.isNotEmpty(dropDownField.getTextSize())) {
try {
textSize = Integer.parseInt(dropDownField.getTextSize());
} catch (NumberFormatException nfe) {
Debug.logError(nfe, "Error reading size of a field fieldName=" + dropDownField.getModelFormField().getFieldName() + " FormName= " + dropDownField.getModelFormField().getModelForm().getName(), module);
}
if (textSize > 0 && UtilValidate.isNotEmpty(currentValue) && currentValue.length() > textSize) {
currentValue = currentValue.substring(0, textSize - 8) + "..." + currentValue.substring(currentValue.length() - 5);
}
}
boolean ajaxEnabled = autoComplete != null && this.javaScriptEnabled;
String className = "";
String alert = "false";
String name = modelFormField.getParameterName(context);
String id = modelFormField.getCurrentContainerId(context);
String multiple = dropDownField.getAllowMultiple() ? "multiple" : "";
String otherFieldName = "";
String formName = modelForm.getName();
String size = dropDownField.getSize();
String dDFCurrent = dropDownField.getCurrent();
String firstInList = "";
String explicitDescription = "";
String allowEmpty = "";
StringBuilder options = new StringBuilder();
StringBuilder ajaxOptions = new StringBuilder();
if (UtilValidate.isNotEmpty(modelFormField.getWidgetStyle())) {
className = modelFormField.getWidgetStyle();
if (modelFormField.shouldBeRed(context)) {
alert = "true";
}
}
// 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 currentDescription = null;
if (UtilValidate.isNotEmpty(currentValue)) {
for (ModelFormField.OptionValue optionValue : allOptionValues) {
if (optionValue.getKey().equals(currentValue)) {
currentDescription = optionValue.getDescription();
break;
}
}
}
int otherFieldSize = dropDownField.getOtherFieldSize();
if (otherFieldSize > 0) {
otherFieldName = dropDownField.getParameterNameOther(context);
}
// if the current value should go first, stick it in
if (UtilValidate.isNotEmpty(currentValue) && "first-in-list".equals(dropDownField.getCurrent())) {
firstInList = "first-in-list";
}
explicitDescription = (currentDescription != null ? currentDescription : dropDownField.getCurrentDescription(context));
if (UtilValidate.isEmpty(explicitDescription)) {
explicitDescription = (FieldInfoWithOptions.getDescriptionForOptionKey(currentValue, allOptionValues));
}
if (textSize > 0 && UtilValidate.isNotEmpty(explicitDescription) && explicitDescription.length() > textSize) {
explicitDescription = explicitDescription.substring(0, textSize - 8) + "..." + explicitDescription.substring(explicitDescription.length() - 5);
}
explicitDescription = encode(explicitDescription, modelFormField, context);
// if allow empty is true, add an empty option
if (dropDownField.getAllowEmpty()) {
allowEmpty = "Y";
}
List<String> currentValueList = null;
if (UtilValidate.isNotEmpty(currentValue) && dropDownField.getAllowMultiple()) {
// If currentValue is Array, it will start with [
if (currentValue.startsWith("[")) {
currentValueList = StringUtil.toList(currentValue);
} else {
currentValueList = UtilMisc.toList(currentValue);
}
}
options.append("[");
Iterator<ModelFormField.OptionValue> optionValueIter = allOptionValues.iterator();
int count = 0;
while (optionValueIter.hasNext()) {
ModelFormField.OptionValue optionValue = optionValueIter.next();
if (options.length() > 1) {
options.append(",");
}
options.append("{'key':'");
String key = encode(optionValue.getKey(), modelFormField, context);
options.append(key);
options.append("'");
options.append(",'description':'");
String description = optionValue.getDescription();
if (textSize > 0 && description.length() > textSize) {
description = description.substring(0, textSize - 8) + "..." + description.substring(description.length() - 5);
}
// replaceAll("'", "\\\\\'") related to OFBIZ-6504
options.append(encode(description.replaceAll("'", "\\\\\'"), modelFormField, context));
if (UtilValidate.isNotEmpty(currentValueList)) {
options.append("'");
options.append(",'selected':'");
if (currentValueList.contains(optionValue.getKey())) {
options.append("selected");
} else {
options.append("");
}
}
options.append("'}");
if (ajaxEnabled) {
count++;
ajaxOptions.append(optionValue.getKey()).append(": ");
ajaxOptions.append(" '").append(optionValue.getDescription()).append("'");
if (count != allOptionValues.size()) {
ajaxOptions.append(", ");
}
}
}
options.append("]");
String noCurrentSelectedKey = dropDownField.getNoCurrentSelectedKey(context);
String otherValue = "", fieldName = "";
// http://www.cs.tut.fi/~jkorpela/forms/combo.html
if (otherFieldSize > 0) {
fieldName = modelFormField.getParameterName(context);
Map<String, ? extends Object> dataMap = modelFormField.getMap(context);
if (dataMap == null) {
dataMap = context;
}
Object otherValueObj = dataMap.get(otherFieldName);
otherValue = (otherValueObj == null) ? "" : otherValueObj.toString();
}
String frequency = "";
String minChars = "";
String choices = "";
String autoSelect = "";
String partialSearch = "";
String partialChars = "";
String ignoreCase = "";
String fullSearch = "";
if (ajaxEnabled) {
frequency = autoComplete.getFrequency();
minChars = autoComplete.getMinChars();
choices = autoComplete.getChoices();
autoSelect = autoComplete.getAutoSelect();
partialSearch = autoComplete.getPartialSearch();
partialChars = autoComplete.getPartialChars();
ignoreCase = autoComplete.getIgnoreCase();
fullSearch = autoComplete.getFullSearch();
}
String tabindex = modelFormField.getTabindex();
StringWriter sr = new StringWriter();
sr.append("<@renderDropDownField ");
sr.append("name=\"");
sr.append(name);
sr.append("\" className=\"");
sr.append(className);
sr.append("\" alert=\"");
sr.append(alert);
sr.append("\" id=\"");
sr.append(id);
sr.append("\" multiple=\"");
sr.append(multiple);
sr.append("\" formName=\"");
sr.append(formName);
sr.append("\" otherFieldName=\"");
sr.append(otherFieldName);
sr.append("\" event=\"");
if (event != null) {
sr.append(event);
}
sr.append("\" action=\"");
if (action != null) {
sr.append(action);
}
sr.append("\" size=\"");
sr.append(size);
sr.append("\" firstInList=\"");
sr.append(firstInList);
sr.append("\" currentValue=\"");
sr.append(currentValue);
sr.append("\" explicitDescription=\"");
sr.append(explicitDescription);
sr.append("\" allowEmpty=\"");
sr.append(allowEmpty);
sr.append("\" options=");
sr.append(options.toString());
sr.append(" fieldName=\"");
sr.append(fieldName);
sr.append("\" otherFieldName=\"");
sr.append(otherFieldName);
sr.append("\" otherValue=\"");
sr.append(otherValue);
sr.append("\" otherFieldSize=");
sr.append(Integer.toString(otherFieldSize));
sr.append(" dDFCurrent=\"");
sr.append(dDFCurrent);
sr.append("\" ajaxEnabled=");
sr.append(Boolean.toString(ajaxEnabled));
sr.append(" noCurrentSelectedKey=\"");
sr.append(noCurrentSelectedKey);
sr.append("\" ajaxOptions=\"");
sr.append(ajaxOptions.toString());
sr.append("\" frequency=\"");
sr.append(frequency);
sr.append("\" minChars=\"");
sr.append(minChars);
sr.append("\" choices=\"");
sr.append(choices);
sr.append("\" autoSelect=\"");
sr.append(autoSelect);
sr.append("\" partialSearch=\"");
sr.append(partialSearch);
sr.append("\" partialChars=\"");
sr.append(partialChars);
sr.append("\" ignoreCase=\"");
sr.append(ignoreCase);
sr.append("\" fullSearch=\"");
sr.append(fullSearch);
sr.append("\" conditionGroup=\"");
sr.append(conditionGroup);
sr.append("\" tabindex=\"");
sr.append(tabindex);
sr.append("\" />");
executeMacro(writer, sr.toString());
ModelFormField.SubHyperlink subHyperlink = dropDownField.getSubHyperlink();
if (subHyperlink != null && subHyperlink.shouldUse(context)) {
makeHyperlinkString(writer, subHyperlink, context);
}
this.appendTooltip(writer, context, modelFormField);
}
use of org.apache.ofbiz.widget.model.ModelForm in project ofbiz-framework by apache.
the class MacroFormRenderer method renderSortField.
public void renderSortField(Appendable writer, Map<String, Object> context, ModelFormField modelFormField, String titleText) {
boolean ajaxEnabled = false;
ModelForm modelForm = modelFormField.getModelForm();
List<ModelForm.UpdateArea> updateAreas = modelForm.getOnSortColumnUpdateAreas();
if (updateAreas == null) {
// For backward compatibility.
updateAreas = modelForm.getOnPaginateUpdateAreas();
}
if (this.javaScriptEnabled) {
if (UtilValidate.isNotEmpty(updateAreas)) {
ajaxEnabled = true;
}
}
String paginateTarget = modelForm.getPaginateTarget(context);
if (paginateTarget.isEmpty() && updateAreas == null) {
Debug.logWarning("Cannot sort because the paginate target URL is empty for the form: " + modelForm.getName(), module);
return;
}
String oldSortField = modelForm.getSortField(context);
String sortFieldStyle = modelFormField.getSortFieldStyle();
// if the entry-name is defined use this instead of field name
String columnField = modelFormField.getEntryName();
if (UtilValidate.isEmpty(columnField)) {
columnField = modelFormField.getFieldName();
}
// switch between asc/desc order
String newSortField = columnField;
if (UtilValidate.isNotEmpty(oldSortField)) {
if (oldSortField.equals(columnField)) {
newSortField = "-" + columnField;
sortFieldStyle = modelFormField.getSortFieldStyleDesc();
} else if (("-" + columnField).equals(oldSortField)) {
newSortField = columnField;
sortFieldStyle = modelFormField.getSortFieldStyleAsc();
}
}
String queryString = UtilHttp.getQueryStringFromTarget(paginateTarget).replace("?", "");
Map<String, Object> paramMap = UtilHttp.getQueryStringOnlyParameterMap(queryString);
String qbeString = (String) context.get("_QBESTRING_");
if (qbeString != null) {
qbeString = qbeString.replaceAll("&", "&");
paramMap.putAll(UtilHttp.getQueryStringOnlyParameterMap(qbeString));
}
paramMap.put(modelForm.getSortFieldParameterName(), newSortField);
UtilHttp.canonicalizeParameterMap(paramMap);
String linkUrl = null;
if (ajaxEnabled) {
linkUrl = createAjaxParamsFromUpdateAreas(updateAreas, paramMap, null, context);
} else {
StringBuilder sb = new StringBuilder("?");
Iterator<Map.Entry<String, Object>> iter = paramMap.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<String, Object> entry = iter.next();
sb.append(entry.getKey()).append("=").append(entry.getValue());
if (iter.hasNext()) {
sb.append("&");
}
}
String newQueryString = sb.toString();
String urlPath = UtilHttp.removeQueryStringFromTarget(paginateTarget);
linkUrl = rh.makeLink(this.request, this.response, urlPath.concat(newQueryString));
}
StringWriter sr = new StringWriter();
sr.append("<@renderSortField ");
sr.append(" style=\"");
sr.append(sortFieldStyle);
sr.append("\" title=\"");
sr.append(titleText);
sr.append("\" linkUrl=r\"");
sr.append(linkUrl);
sr.append("\" ajaxEnabled=");
sr.append(Boolean.toString(ajaxEnabled));
String tooltip = modelFormField.getSortFieldHelpText(context);
if (!tooltip.isEmpty()) {
sr.append(" tooltip=\"").append(tooltip).append("\"");
}
sr.append(" />");
executeMacro(writer, sr.toString());
}
use of org.apache.ofbiz.widget.model.ModelForm in project ofbiz-framework by apache.
the class MacroFormRenderer method renderLookupField.
public void renderLookupField(Appendable writer, Map<String, Object> context, LookupField lookupField) throws IOException {
ModelFormField modelFormField = lookupField.getModelFormField();
String lookupFieldFormName = lookupField.getFormName(context);
String conditionGroup = modelFormField.getConditionGroup();
String className = "";
String alert = "false";
if (UtilValidate.isNotEmpty(modelFormField.getWidgetStyle())) {
className = modelFormField.getWidgetStyle();
if (modelFormField.shouldBeRed(context)) {
alert = "true";
}
}
// 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 name = modelFormField.getParameterName(context);
String value = modelFormField.getEntry(context, lookupField.getDefaultValue(context));
if (value == null) {
value = "";
}
String size = Integer.toString(lookupField.getSize());
Integer maxlength = lookupField.getMaxlength();
String id = modelFormField.getCurrentContainerId(context);
List<ModelForm.UpdateArea> updateAreas = modelFormField.getOnChangeUpdateAreas();
// add default ajax auto completer to all lookup fields
if (UtilValidate.isEmpty(updateAreas) && UtilValidate.isNotEmpty(lookupFieldFormName)) {
String autoCompleterTarget = null;
if (lookupFieldFormName.indexOf('?') == -1) {
autoCompleterTarget = lookupFieldFormName + "?";
} else {
autoCompleterTarget = lookupFieldFormName + "&amp;";
}
autoCompleterTarget = autoCompleterTarget + "ajaxLookup=Y";
updateAreas = new LinkedList<>();
updateAreas.add(new ModelForm.UpdateArea("change", id, autoCompleterTarget));
}
boolean ajaxEnabled = UtilValidate.isNotEmpty(updateAreas) && this.javaScriptEnabled;
String autocomplete = "";
if (!lookupField.getClientAutocompleteField() || !ajaxEnabled) {
autocomplete = "off";
}
String event = modelFormField.getEvent();
String action = modelFormField.getAction(context);
boolean readonly = lookupField.getReadonly();
// add lookup pop-up button
String descriptionFieldName = lookupField.getDescriptionFieldName();
ModelForm modelForm = modelFormField.getModelForm();
String formName = modelFormField.getParentFormName();
if (UtilValidate.isEmpty(formName)) {
formName = FormRenderer.getCurrentFormName(modelForm, context);
}
StringBuilder targetParameterIter = new StringBuilder();
StringBuilder imgSrc = new StringBuilder();
// FIXME: refactor using the StringUtils methods
List<String> targetParameterList = lookupField.getTargetParameterList();
targetParameterIter.append("[");
for (String targetParameter : targetParameterList) {
if (targetParameterIter.length() > 1) {
targetParameterIter.append(",");
}
targetParameterIter.append("'");
targetParameterIter.append(targetParameter);
targetParameterIter.append("'");
}
targetParameterIter.append("]");
this.appendContentUrl(imgSrc, "/images/fieldlookup.gif");
String ajaxUrl = "";
if (ajaxEnabled) {
ajaxUrl = createAjaxParamsFromUpdateAreas(updateAreas, "", context);
}
String lookupPresentation = lookupField.getLookupPresentation();
if (UtilValidate.isEmpty(lookupPresentation)) {
lookupPresentation = "";
}
String lookupHeight = lookupField.getLookupHeight();
String lookupWidth = lookupField.getLookupWidth();
String lookupPosition = lookupField.getLookupPosition();
String fadeBackground = lookupField.getFadeBackground();
if (UtilValidate.isEmpty(fadeBackground)) {
fadeBackground = "false";
}
Boolean isInitiallyCollapsed = lookupField.getInitiallyCollapsed();
String clearText = "";
Map<String, Object> uiLabelMap = UtilGenerics.checkMap(context.get("uiLabelMap"));
if (uiLabelMap != null) {
clearText = (String) uiLabelMap.get("CommonClear");
} else {
Debug.logWarning("Could not find uiLabelMap in context", module);
}
Boolean showDescription = lookupField.getShowDescription();
if (showDescription == null) {
showDescription = "Y".equals(visualTheme.getModelTheme().getLookupShowDescription());
}
// lastViewName, used by lookup to remember the real last view name
// Try to get it from parameters firstly
String lastViewName = request.getParameter("_LAST_VIEW_NAME_");
if (UtilValidate.isEmpty(lastViewName)) {
// get from session
lastViewName = (String) request.getSession().getAttribute("_LAST_VIEW_NAME_");
}
if (UtilValidate.isEmpty(lastViewName)) {
lastViewName = "";
}
String tabindex = modelFormField.getTabindex();
StringWriter sr = new StringWriter();
sr.append("<@renderLookupField ");
sr.append(" className=\"");
sr.append(className);
sr.append("\" alert=\"");
sr.append(alert);
sr.append("\" name=\"");
sr.append(name);
sr.append("\" value=\"");
sr.append(value);
sr.append("\" size=\"");
sr.append(size);
sr.append("\" maxlength=\"");
sr.append((maxlength != null ? Integer.toString(maxlength) : ""));
sr.append("\" id=\"");
sr.append(id);
sr.append("\" event=\"");
if (event != null) {
sr.append(event);
}
sr.append("\" action=\"");
if (action != null) {
sr.append(action);
}
sr.append("\" readonly=");
sr.append(Boolean.toString(readonly));
sr.append(" autocomplete=\"");
sr.append(autocomplete);
sr.append("\" descriptionFieldName=\"");
sr.append(descriptionFieldName);
sr.append("\" formName=\"");
sr.append(formName);
sr.append("\" fieldFormName=\"");
sr.append(lookupFieldFormName);
sr.append("\" targetParameterIter=");
sr.append(targetParameterIter.toString());
sr.append(" imgSrc=\"");
sr.append(imgSrc.toString());
sr.append("\" ajaxUrl=\"");
sr.append(ajaxUrl);
sr.append("\" ajaxEnabled=");
sr.append(Boolean.toString(ajaxEnabled));
sr.append(" presentation=\"");
sr.append(lookupPresentation);
if (UtilValidate.isNotEmpty(lookupHeight)) {
sr.append("\" height=\"");
sr.append(lookupHeight);
}
if (UtilValidate.isNotEmpty(lookupWidth)) {
sr.append("\" width=\"");
sr.append(lookupWidth);
}
if (UtilValidate.isNotEmpty(lookupPosition)) {
sr.append("\" position=\"");
sr.append(lookupPosition);
}
sr.append("\" fadeBackground=\"");
sr.append(fadeBackground);
sr.append("\" clearText=\"");
sr.append(clearText);
sr.append("\" showDescription=\"");
sr.append(Boolean.toString(showDescription));
sr.append("\" initiallyCollapsed=\"");
sr.append(Boolean.toString(isInitiallyCollapsed));
sr.append("\" lastViewName=\"");
sr.append(lastViewName);
sr.append("\" conditionGroup=\"");
sr.append(conditionGroup);
sr.append("\" tabindex=\"");
sr.append(tabindex);
sr.append("\" delegatorName=\"");
sr.append(((HttpSession) context.get("session")).getAttribute("delegatorName").toString());
sr.append("\" />");
executeMacro(writer, sr.toString());
this.addAsterisks(writer, context, modelFormField);
this.makeHyperlinkString(writer, lookupField.getSubHyperlink(), context);
this.appendTooltip(writer, context, modelFormField);
}
use of org.apache.ofbiz.widget.model.ModelForm in project ofbiz-framework by apache.
the class MacroScreenRenderer method renderScreenletPaginateMenu.
protected void renderScreenletPaginateMenu(Appendable writer, Map<String, Object> context, ModelScreenWidget.Form form) throws IOException {
HttpServletResponse response = (HttpServletResponse) context.get("response");
HttpServletRequest request = (HttpServletRequest) context.get("request");
ModelForm modelForm;
try {
modelForm = form.getModelForm(context);
} catch (Exception e) {
throw new IOException(e);
}
modelForm.runFormActions(context);
Paginator.preparePager(modelForm, context);
String targetService = modelForm.getPaginateTarget(context);
if (targetService == null) {
targetService = "${targetService}";
}
// get the parametrized pagination index and size fields
int paginatorNumber = WidgetWorker.getPaginatorNumber(context);
String viewIndexParam = modelForm.getMultiPaginateIndexField(context);
String viewSizeParam = modelForm.getMultiPaginateSizeField(context);
int viewIndex = Paginator.getViewIndex(modelForm, context);
int viewSize = Paginator.getViewSize(modelForm, context);
int listSize = Paginator.getListSize(context);
int highIndex = Paginator.getHighIndex(context);
int actualPageSize = Paginator.getActualPageSize(context);
// if this is all there seems to be (if listSize < 0, then size is unknown)
if (actualPageSize >= listSize && listSize >= 0) {
return;
}
// needed for the "Page" and "rows" labels
Map<String, String> uiLabelMap = UtilGenerics.cast(context.get("uiLabelMap"));
String ofLabel = "";
if (uiLabelMap == null) {
Debug.logWarning("Could not find uiLabelMap in context", module);
} else {
ofLabel = uiLabelMap.get("CommonOf");
ofLabel = ofLabel.toLowerCase(Locale.getDefault());
}
// for legacy support, the viewSizeParam is VIEW_SIZE and viewIndexParam is VIEW_INDEX when the fields are "viewSize" and "viewIndex"
if (("viewIndex" + "_" + paginatorNumber).equals(viewIndexParam)) {
viewIndexParam = "VIEW_INDEX" + "_" + paginatorNumber;
}
if (("viewSize" + "_" + paginatorNumber).equals(viewSizeParam)) {
viewSizeParam = "VIEW_SIZE" + "_" + paginatorNumber;
}
ServletContext ctx = (ServletContext) request.getAttribute("servletContext");
RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
Map<String, Object> inputFields = UtilGenerics.toMap(context.get("requestParameters"));
// strip out any multi form fields if the form is of type multi
if ("multi".equals(modelForm.getType())) {
inputFields = UtilHttp.removeMultiFormParameters(inputFields);
}
String queryString = UtilHttp.urlEncodeArgs(inputFields);
// strip legacy viewIndex/viewSize params from the query string
queryString = UtilHttp.stripViewParamsFromQueryString(queryString, "" + paginatorNumber);
// strip parametrized index/size params from the query string
HashSet<String> paramNames = new HashSet<>();
paramNames.add(viewIndexParam);
paramNames.add(viewSizeParam);
queryString = UtilHttp.stripNamedParamsFromQueryString(queryString, paramNames);
String anchor = "";
String paginateAnchor = modelForm.getPaginateTargetAnchor();
if (paginateAnchor != null) {
anchor = "#" + paginateAnchor;
}
// preparing the link text, so that later in the code we can reuse this and just add the viewIndex
String prepLinkText = "";
prepLinkText = targetService;
if (prepLinkText.indexOf('?') < 0) {
prepLinkText += "?";
} else if (!prepLinkText.endsWith("?")) {
prepLinkText += "&";
}
if (UtilValidate.isNotEmpty(queryString) && !"null".equals(queryString)) {
prepLinkText += queryString + "&";
}
prepLinkText += viewSizeParam + "=" + viewSize + "&" + viewIndexParam + "=";
String linkText;
// The current screenlet title bar navigation syling requires rendering
// these links in reverse order
// Last button
String lastLinkUrl = "";
if (highIndex < listSize) {
int lastIndex = UtilMisc.getViewLastIndex(listSize, viewSize);
linkText = prepLinkText + lastIndex + anchor;
lastLinkUrl = rh.makeLink(request, response, linkText);
}
String nextLinkUrl = "";
if (highIndex < listSize) {
linkText = prepLinkText + (viewIndex + 1) + anchor;
// - make the link
nextLinkUrl = rh.makeLink(request, response, linkText);
}
String previousLinkUrl = "";
if (viewIndex > 0) {
linkText = prepLinkText + (viewIndex - 1) + anchor;
previousLinkUrl = rh.makeLink(request, response, linkText);
}
String firstLinkUrl = "";
if (viewIndex > 0) {
linkText = prepLinkText + 0 + anchor;
firstLinkUrl = rh.makeLink(request, response, linkText);
}
Map<String, Object> parameters = new HashMap<>();
parameters.put("lowIndex", Paginator.getLowIndex(context));
parameters.put("actualPageSize", actualPageSize);
parameters.put("ofLabel", ofLabel);
parameters.put("listSize", listSize);
parameters.put("paginateLastStyle", modelForm.getPaginateLastStyle());
parameters.put("lastLinkUrl", lastLinkUrl);
parameters.put("paginateLastLabel", modelForm.getPaginateLastLabel(context));
parameters.put("paginateNextStyle", modelForm.getPaginateNextStyle());
parameters.put("nextLinkUrl", nextLinkUrl);
parameters.put("paginateNextLabel", modelForm.getPaginateNextLabel(context));
parameters.put("paginatePreviousStyle", modelForm.getPaginatePreviousStyle());
parameters.put("paginatePreviousLabel", modelForm.getPaginatePreviousLabel(context));
parameters.put("previousLinkUrl", previousLinkUrl);
parameters.put("paginateFirstStyle", modelForm.getPaginateFirstStyle());
parameters.put("paginateFirstLabel", modelForm.getPaginateFirstLabel(context));
parameters.put("firstLinkUrl", firstLinkUrl);
executeMacro(writer, "renderScreenletPaginateMenu", parameters);
}
Aggregations