use of org.apache.ofbiz.base.util.string.FlexibleStringExpander in project ofbiz-framework by apache.
the class MacroFormRenderer method renderFieldTitle.
public void renderFieldTitle(Appendable writer, Map<String, Object> context, ModelFormField modelFormField) throws IOException {
String titleText = modelFormField.getTitle(context);
String style = modelFormField.getTitleStyle();
String id = modelFormField.getCurrentContainerId(context);
StringBuilder sb = new StringBuilder();
if (UtilValidate.isNotEmpty(titleText)) {
if (" ".equals(titleText)) {
executeMacro(writer, "<@renderFormatEmptySpace />");
} else {
titleText = UtilHttp.encodeAmpersands(titleText);
titleText = encode(titleText, modelFormField, context);
if (UtilValidate.isNotEmpty(modelFormField.getHeaderLink())) {
StringBuilder targetBuffer = new StringBuilder();
FlexibleStringExpander target = FlexibleStringExpander.getInstance(modelFormField.getHeaderLink());
String fullTarget = target.expandString(context);
targetBuffer.append(fullTarget);
String targetType = CommonWidgetModels.Link.DEFAULT_URL_MODE;
if (UtilValidate.isNotEmpty(targetBuffer.toString()) && targetBuffer.toString().toLowerCase(Locale.getDefault()).startsWith("javascript:")) {
targetType = "plain";
}
StringWriter sr = new StringWriter();
makeHyperlinkString(sr, modelFormField.getHeaderLinkStyle(), targetType, targetBuffer.toString(), null, titleText, "", modelFormField, this.request, this.response, context, "");
String title = sr.toString().replace("\"", "\'");
sr = new StringWriter();
sr.append("<@renderHyperlinkTitle ");
sr.append(" name=\"");
sr.append(modelFormField.getModelForm().getName());
sr.append("\" title=\"");
sr.append(encodeDoubleQuotes(title));
sr.append("\" />");
executeMacro(writer, sr.toString());
} else if (modelFormField.isSortField()) {
renderSortField(writer, context, modelFormField, titleText);
} else if (modelFormField.isRowSubmit()) {
StringWriter sr = new StringWriter();
sr.append("<@renderHyperlinkTitle ");
sr.append(" name=\"");
sr.append(modelFormField.getModelForm().getName());
sr.append("\" title=\"");
sr.append(titleText);
sr.append("\" showSelectAll=\"Y\"/>");
executeMacro(writer, sr.toString());
} else {
sb.append(titleText);
}
}
}
if (!sb.toString().isEmpty()) {
// check for required field style on single forms
if ("single".equals(modelFormField.getModelForm().getType()) && modelFormField.getRequiredField()) {
String requiredStyle = modelFormField.getRequiredFieldStyle();
if (UtilValidate.isNotEmpty(requiredStyle)) {
style = requiredStyle;
}
}
StringWriter sr = new StringWriter();
sr.append("<@renderFieldTitle ");
sr.append(" style=\"");
sr.append(style);
String displayHelpText = UtilProperties.getPropertyValue("widget", "widget.form.displayhelpText");
if ("Y".equals(displayHelpText)) {
Delegator delegator = WidgetWorker.getDelegator(context);
Locale locale = (Locale) context.get("locale");
String entityName = modelFormField.getEntityName();
String fieldName = modelFormField.getFieldName();
String helpText = UtilHelpText.getEntityFieldDescription(entityName, fieldName, delegator, locale);
sr.append("\" fieldHelpText=\"");
sr.append(encodeDoubleQuotes(helpText));
}
sr.append("\" title=\"");
sr.append(sb.toString());
if (UtilValidate.isNotEmpty(id)) {
sr.append("\" id=\"");
sr.append(id);
sr.append("_title");
// Render "for"
sr.append("\" for=\"");
sr.append(id);
}
sr.append("\" />");
executeMacro(writer, sr.toString());
}
}
use of org.apache.ofbiz.base.util.string.FlexibleStringExpander in project ofbiz-framework by apache.
the class MacroFormRenderer method renderFieldGroupOpen.
public void renderFieldGroupOpen(Appendable writer, Map<String, Object> context, ModelForm.FieldGroup fieldGroup) throws IOException {
String style = fieldGroup.getStyle();
String id = fieldGroup.getId();
FlexibleStringExpander titleNotExpanded = FlexibleStringExpander.getInstance(fieldGroup.getTitle());
String title = titleNotExpanded.expandString(context);
Boolean collapsed = fieldGroup.initiallyCollapsed();
String collapsibleAreaId = fieldGroup.getId() + "_body";
Boolean collapsible = fieldGroup.collapsible();
String expandToolTip = "";
String collapseToolTip = "";
if (UtilValidate.isNotEmpty(style) || UtilValidate.isNotEmpty(id) || UtilValidate.isNotEmpty(title)) {
if (fieldGroup.collapsible()) {
Map<String, Object> uiLabelMap = UtilGenerics.checkMap(context.get("uiLabelMap"));
if (uiLabelMap != null) {
expandToolTip = (String) uiLabelMap.get("CommonExpand");
collapseToolTip = (String) uiLabelMap.get("CommonCollapse");
}
}
}
StringWriter sr = new StringWriter();
sr.append("<@renderFieldGroupOpen ");
sr.append(" style=\"");
if (style != null) {
sr.append(style);
}
sr.append("\" id=\"");
sr.append(id);
sr.append("\" title=\"");
sr.append(title);
sr.append("\" collapsed=");
sr.append(Boolean.toString(collapsed));
sr.append(" collapsibleAreaId=\"");
sr.append(collapsibleAreaId);
sr.append("\" collapsible=");
sr.append(Boolean.toString(collapsible));
sr.append(" expandToolTip=\"");
sr.append(expandToolTip);
sr.append("\" collapseToolTip=\"");
sr.append(collapseToolTip);
sr.append("\" />");
executeMacro(writer, sr.toString());
}
use of org.apache.ofbiz.base.util.string.FlexibleStringExpander in project ofbiz-framework by apache.
the class MacroFormRenderer method renderFieldGroupClose.
public void renderFieldGroupClose(Appendable writer, Map<String, Object> context, ModelForm.FieldGroup fieldGroup) throws IOException {
String style = fieldGroup.getStyle();
String id = fieldGroup.getId();
FlexibleStringExpander titleNotExpanded = FlexibleStringExpander.getInstance(fieldGroup.getTitle());
String title = titleNotExpanded.expandString(context);
StringWriter sr = new StringWriter();
sr.append("<@renderFieldGroupClose ");
sr.append(" style=\"");
if (style != null) {
sr.append(style);
}
sr.append("\" id=\"");
if (id != null) {
sr.append(id);
}
sr.append("\" title=\"");
if (title != null) {
sr.append(title);
}
sr.append("\" />");
executeMacro(writer, sr.toString());
}
use of org.apache.ofbiz.base.util.string.FlexibleStringExpander in project ofbiz-framework by apache.
the class ServiceEcaSetField method eval.
public void eval(Map<String, Object> context) {
if (fieldName != null) {
// try to expand the envName
if (UtilValidate.isNotEmpty(this.envName) && this.envName.startsWith("${")) {
FlexibleStringExpander exp = FlexibleStringExpander.getInstance(this.envName);
String s = exp.expandString(context);
if (UtilValidate.isNotEmpty(s)) {
value = s;
}
Debug.logInfo("Expanded String: " + s, module);
}
// TODO: rewrite using the ContextAccessor.java see hack below to be able to use maps for email notifications
// check if target is a map and create/get from contaxt
Map<String, Object> valueMap = null;
if (UtilValidate.isNotEmpty(this.mapName) && context.containsKey(this.mapName)) {
valueMap = UtilGenerics.checkMap(context.get(mapName));
} else {
valueMap = new HashMap<String, Object>();
}
// process the context changes
Object newValue = null;
if (UtilValidate.isNotEmpty(this.value)) {
newValue = this.format(this.value, context);
} else if (UtilValidate.isNotEmpty(this.envName) && context.get(this.envName) != null) {
newValue = this.format((String) context.get(this.envName), context);
}
if (newValue != null) {
if (UtilValidate.isNotEmpty(this.mapName)) {
valueMap.put(this.fieldName, newValue);
context.put(this.mapName, valueMap);
} else {
context.put(this.fieldName, newValue);
}
}
}
}
use of org.apache.ofbiz.base.util.string.FlexibleStringExpander in project ofbiz-framework by apache.
the class FinAccountProductServices method createPartyFinAccountFromPurchase.
public static Map<String, Object> createPartyFinAccountFromPurchase(DispatchContext dctx, Map<String, Object> context) {
// this service should always be called via FULFILLMENT_EXTASYNC
LocalDispatcher dispatcher = dctx.getDispatcher();
Delegator delegator = dctx.getDelegator();
Locale locale = (Locale) context.get("locale");
GenericValue orderItem = (GenericValue) context.get("orderItem");
GenericValue userLogin = (GenericValue) context.get("userLogin");
// order ID for tracking
String orderId = orderItem.getString("orderId");
String orderItemSeqId = orderItem.getString("orderItemSeqId");
// the order header for store info
GenericValue orderHeader;
try {
orderHeader = orderItem.getRelatedOne("OrderHeader", false);
} catch (GenericEntityException e) {
Debug.logError(e, "Unable to get OrderHeader from OrderItem", module);
return ServiceUtil.returnError(UtilProperties.getMessage(resourceOrderError, "OrderCannotGetOrderHeader", UtilMisc.toMap("orderId", orderId), locale));
}
String productId = orderItem.getString("productId");
GenericValue featureAndAppl;
try {
List<GenericValue> featureAndAppls = EntityQuery.use(delegator).from("ProductFeatureAndAppl").where("productId", productId, "productFeatureTypeId", "TYPE", "productFeatureApplTypeId", "STANDARD_FEATURE").queryList();
featureAndAppls = EntityUtil.filterByDate(featureAndAppls);
featureAndAppl = EntityUtil.getFirst(featureAndAppls);
} catch (GenericEntityException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
// financial account data; pulled from the TYPE feature of the product
// default
String finAccountTypeId = "BALANCE_ACCOUNT";
String finAccountName = "Customer Financial Account";
if (featureAndAppl != null) {
if (UtilValidate.isNotEmpty(featureAndAppl.getString("idCode"))) {
finAccountTypeId = featureAndAppl.getString("idCode");
}
if (UtilValidate.isNotEmpty(featureAndAppl.getString("description"))) {
finAccountName = featureAndAppl.getString("description");
}
}
// locate the financial account type
GenericValue finAccountType;
try {
finAccountType = EntityQuery.use(delegator).from("FinAccountType").where("finAccountTypeId", finAccountTypeId).queryOne();
} catch (GenericEntityException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
String replenishEnumId = finAccountType.getString("replenishEnumId");
// get the order read helper
OrderReadHelper orh = new OrderReadHelper(orderHeader);
// get the currency
String currency = orh.getCurrency();
// make sure we have a currency
if (currency == null) {
currency = EntityUtilProperties.getPropertyValue("general", "currency.uom.id.default", "USD", delegator);
}
// get the product store
String productStoreId = null;
if (orderHeader != null) {
productStoreId = orh.getProductStoreId();
}
if (productStoreId == null) {
Debug.logFatal("Unable to create financial accout; no productStoreId on OrderHeader : " + orderId, module);
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "AccountingFinAccountCannotCreate", UtilMisc.toMap("orderId", orderId), locale));
}
// party ID (owner)
GenericValue billToParty = orh.getBillToParty();
String partyId = null;
if (billToParty != null) {
partyId = billToParty.getString("partyId");
}
// payment method info
List<GenericValue> payPrefs = orh.getPaymentPreferences();
String paymentMethodId = null;
if (payPrefs != null) {
for (GenericValue pref : payPrefs) {
// needs to be a CC or EFT account
String type = pref.getString("paymentMethodTypeId");
if ("CREDIT_CARD".equals(type) || "EFT_ACCOUNT".equals(type)) {
paymentMethodId = pref.getString("paymentMethodId");
}
}
}
// some person data for expanding
GenericValue partyGroup = null;
GenericValue person = null;
GenericValue party = null;
if (billToParty != null) {
try {
party = billToParty.getRelatedOne("Party", false);
} catch (GenericEntityException e) {
Debug.logError(e, module);
}
if (party != null) {
String partyTypeId = party.getString("partyTypeId");
if ("PARTY_GROUP".equals(partyTypeId)) {
partyGroup = billToParty;
} else if ("PERSON".equals(partyTypeId)) {
person = billToParty;
}
}
}
// create the context for FSE
Map<String, Object> expContext = new HashMap<>();
expContext.put("orderHeader", orderHeader);
expContext.put("orderItem", orderItem);
expContext.put("party", party);
expContext.put("person", person);
expContext.put("partyGroup", partyGroup);
// expand the name field to dynamically add information
FlexibleStringExpander exp = FlexibleStringExpander.getInstance(finAccountName);
finAccountName = exp.expandString(expContext);
// price/amount/quantity to create initial deposit amount
BigDecimal quantity = orderItem.getBigDecimal("quantity");
BigDecimal price = orderItem.getBigDecimal("unitPrice");
BigDecimal deposit = price.multiply(quantity).setScale(FinAccountHelper.decimals, FinAccountHelper.rounding);
// create the financial account
Map<String, Object> createCtx = new HashMap<>();
String finAccountId;
createCtx.put("finAccountTypeId", finAccountTypeId);
createCtx.put("finAccountName", finAccountName);
createCtx.put("productStoreId", productStoreId);
createCtx.put("ownerPartyId", partyId);
createCtx.put("currencyUomId", currency);
createCtx.put("statusId", "FNACT_ACTIVE");
createCtx.put("userLogin", userLogin);
// if we auto-replenish this type; set the level to the initial deposit
if (replenishEnumId != null && "FARP_AUTOMATIC".equals(replenishEnumId)) {
createCtx.put("replenishLevel", deposit);
createCtx.put("replenishPaymentId", paymentMethodId);
}
Map<String, Object> createResp;
try {
createResp = dispatcher.runSync("createFinAccountForStore", createCtx);
} catch (GenericServiceException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
if (ServiceUtil.isError(createResp)) {
Debug.logFatal(ServiceUtil.getErrorMessage(createResp), module);
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(createResp));
}
finAccountId = (String) createResp.get("finAccountId");
// create the owner role
Map<String, Object> roleCtx = new HashMap<>();
roleCtx.put("partyId", partyId);
roleCtx.put("roleTypeId", "OWNER");
roleCtx.put("finAccountId", finAccountId);
roleCtx.put("userLogin", userLogin);
roleCtx.put("fromDate", UtilDateTime.nowTimestamp());
Map<String, Object> roleResp;
try {
roleResp = dispatcher.runSync("createFinAccountRole", roleCtx);
} catch (GenericServiceException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
if (ServiceUtil.isError(roleResp)) {
Debug.logFatal(ServiceUtil.getErrorMessage(roleResp), module);
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(roleResp));
}
// create the initial deposit
Map<String, Object> depositCtx = new HashMap<>();
depositCtx.put("finAccountId", finAccountId);
depositCtx.put("productStoreId", productStoreId);
depositCtx.put("currency", currency);
depositCtx.put("partyId", partyId);
depositCtx.put("orderId", orderId);
depositCtx.put("orderItemSeqId", orderItemSeqId);
depositCtx.put("amount", deposit);
depositCtx.put("reasonEnumId", "FATR_IDEPOSIT");
depositCtx.put("userLogin", userLogin);
Map<String, Object> depositResp;
try {
depositResp = dispatcher.runSync("finAccountDeposit", depositCtx);
} catch (GenericServiceException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
if (ServiceUtil.isError(depositResp)) {
Debug.logFatal(ServiceUtil.getErrorMessage(depositResp), module);
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(depositResp));
}
Map<String, Object> result = ServiceUtil.returnSuccess();
result.put("finAccountId", finAccountId);
return result;
}
Aggregations