/// <summary> /// Evaluates the formula /// </summary> public static string EvaluateFormula(string formula, BaseClasses.Data.BaseRecord dataSourceForEvaluate, string format, string name) { BaseFormulaEvaluator e = new BaseFormulaEvaluator(); if(dataSourceForEvaluate != null) e.Evaluator.Variables.Add(name, dataSourceForEvaluate); e.DataSource = dataSourceForEvaluate; object resultObj = e.Evaluate(formula); if (resultObj == null) return ""; if (!string.IsNullOrEmpty(format)) return BaseFormulaUtils.Format(resultObj, format); else return resultObj.ToString(); }
/// <summary> /// designed to be used on Quick Selector, repeater row /// </summary> /// <param name="datasource"></param> /// <returns>text to display on quick selector</returns> /// <remarks></remarks> public static string GetQuickSelectorDisplayText(BaseClasses.Data.BaseRecord datasource) { if (!string.IsNullOrEmpty(URL("Formula"))) { return(BaseFormulaUtils.EvaluateFormula(URL("Formula"), datasource)); } if (!string.IsNullOrEmpty(URL("DFKA"))) { BaseColumn col = datasource.TableAccess.TableDefinition.ColumnList.GetByAnyName(URL("DFKA")); return(datasource.GetValue(col).ToString()); } if (!string.IsNullOrEmpty(URL("IndexField"))) { BaseColumn col = datasource.TableAccess.TableDefinition.ColumnList.GetByAnyName(URL("IndexField")); return(datasource.GetValue(URL("IndexField")).ToString()); } return(""); }
public virtual string EvaluateExpressions(string redirectUrl, string redirectArgument, Object rec, bool bEncrypt, bool includeSession) { const string PREFIX_NO_ENCODE = "NoUrlEncode:"; string finalRedirectUrl = redirectUrl; string finalRedirectArgument = redirectArgument; if ((finalRedirectUrl == null || finalRedirectUrl.Length == 0)) { return(finalRedirectUrl); } else if ((finalRedirectUrl.IndexOf('{') < 0)) { return(finalRedirectUrl); } else { // The old way was to pass separate URL and arguments and use String.Format to // do the replacement. Example: // URL: EditProductsRecord?Products={0} // Argument: PK // The new way to is pass the arguments directly in the URL. Example: // URL: EditProductsRecord?Products={PK} // If the old way is passsed, convert it to the new way. if (finalRedirectArgument != null && finalRedirectArgument.Length > 0) { string[] arguments = finalRedirectArgument.Split(','); for (int i = 0; i <= (arguments.Length - 1); i++) { finalRedirectUrl = finalRedirectUrl.Replace("{" + i.ToString() + "}", "{" + arguments[i] + "}"); } finalRedirectArgument = ""; } // Evaluate all of the expressions in the RedirectURL // Expressions can be of the form [ControlName:][NoUrlEncode:]Key[:Value] string remainingUrl = finalRedirectUrl; while ((remainingUrl.IndexOf('{') >= 0) & (remainingUrl.IndexOf('}') > 0) & (remainingUrl.IndexOf('{') < remainingUrl.IndexOf('}'))) { int leftIndex = remainingUrl.IndexOf('{'); int rightIndex = remainingUrl.IndexOf('}'); string expression = remainingUrl.Substring(leftIndex + 1, rightIndex - leftIndex - 1); string origExpression = expression; remainingUrl = remainingUrl.Substring(rightIndex + 1); bool skip = false; bool returnEmptyStringOnFail = false; string prefix = null; // Check to see if this control must evaluate the expression if ((expression.IndexOf(":") > 0)) { prefix = expression.Substring(0, expression.IndexOf(":")); } if ((prefix != null) && (prefix.Length > 0) && (!((StringUtils.InvariantLCase(prefix) == StringUtils.InvariantLCase(PREFIX_NO_ENCODE)))) && (!(BaseRecord.IsKnownExpressionPrefix(prefix)))) { // Remove the ASCX Prefix string IdString = this.ID; if (IdString.StartsWith("_")) { IdString = IdString.Remove(0, 1); } // The prefix is a control name. if (prefix == IdString) { // This control is responsible for evaluating the expression, // so if it can't be evaluated then return an empty string. returnEmptyStringOnFail = true; expression = expression.Substring(expression.IndexOf(":") + 1); } else { // It's not for this control to evaluate so skip. skip = true; } } if (!skip) { bool bUrlEncode = true; if ((StringUtils.InvariantLCase(expression).StartsWith(StringUtils.InvariantLCase(PREFIX_NO_ENCODE)))) { bUrlEncode = false; expression = expression.Substring(PREFIX_NO_ENCODE.Length); } object result = null; try { if (rec != null) { result = ((IRecord)rec).EvaluateExpression(expression); } } catch (Exception) { //Fall through } if (result != null) { result = result.ToString(); } if (result == null) { if (!returnEmptyStringOnFail) { return(finalRedirectUrl); } else { result = string.Empty; } } if ((bUrlEncode)) { result = System.Web.HttpUtility.UrlEncode(((string)(result))); if (result == null) { result = string.Empty; } } if (bEncrypt) { if (result != null) { if (includeSession) { result = ((BaseApplicationPage)(this.Page)).Encrypt((string)result); } else { result = BaseFormulaUtils.EncryptData((string)result); } } } finalRedirectUrl = finalRedirectUrl.Replace("{" + origExpression + "}", ((string)(result))); } } } // If there are still expressions to evaluate. Forward to the page for further processing. return(finalRedirectUrl); }
/// <summary> /// Return the value of the given variable if it exists in the data source /// </summary> /// <param name="sender">The input whose absolute value is to be found.</param> /// <param name="e">The input whose absolute value is to be found.</param> protected void variables_ResolveVariableValue(object sender, ResolveVariableValueEventArgs e) { BaseColumn col = default(BaseColumn); // Default value is Nothing e.VariableValue = null; // If no DataSource was set, we do not have variables that we can use // directly. We should not get here since the request for Type should have // caught this. if (DataSource == null) { return; } try { // Find a column in the datasource using a variable name. col = DataSource.TableAccess.TableDefinition.ColumnList.GetByCodeName(e.VariableName); if (col == null) { // if the variable name ended with "DefaultValue", remmove it and then try to get the column name again. if (e.VariableName.ToLower().EndsWith("defaultvalue")) { col = DataSource.TableAccess.TableDefinition.ColumnList.GetByCodeName(e.VariableName.Substring(0, e.VariableName.Length - 12)); } if (col != null) { switch (col.ColumnType) { case BaseColumn.ColumnTypes.Number: case BaseColumn.ColumnTypes.Percentage: case BaseColumn.ColumnTypes.Star: // The Number and Percentage values are saved as Single. So we first // retrieve the Single value and then convert to Decimal. Our policy is // always to return Decimal (never to return Single or Double) to be constent // and avoid type conversion in the evaluator. e.VariableValue = BaseFormulaUtils.ParseDecimal(col.DefaultValue); break; case BaseColumn.ColumnTypes.Currency: e.VariableValue = BaseFormulaUtils.ParseDecimal(col.DefaultValue); break; case BaseColumn.ColumnTypes.Boolean: e.VariableValue = col.DefaultValue; break; case BaseColumn.ColumnTypes.Credit_Card_Date: case BaseColumn.ColumnTypes.Date: e.VariableValue = BaseFormulaUtils.ParseDate(col.DefaultValue); break; case BaseColumn.ColumnTypes.Country: case BaseColumn.ColumnTypes.Credit_Card_Number: case BaseColumn.ColumnTypes.Email: case BaseColumn.ColumnTypes.Password: case BaseColumn.ColumnTypes.String: case BaseColumn.ColumnTypes.Unique_Identifier: case BaseColumn.ColumnTypes.USA_Phone_Number: case BaseColumn.ColumnTypes.USA_State: case BaseColumn.ColumnTypes.USA_Zip_Code: case BaseColumn.ColumnTypes.Very_Large_String: case BaseColumn.ColumnTypes.Web_Url: e.VariableValue = col.DefaultValue; break; case BaseColumn.ColumnTypes.File: case BaseColumn.ColumnTypes.Image: // Can't do anything here. e.VariableValue = null; break; default: e.VariableValue = null; break; } } } else { switch (col.ColumnType) { case BaseColumn.ColumnTypes.Number: case BaseColumn.ColumnTypes.Percentage: case BaseColumn.ColumnTypes.Star: // The Number and Percentage values are saved as Single. So we first // retrieve the Single value and then convert to Decimal. Our policy is // always to return Decimal (never to return Single or Double) to be constent // and avoid type conversion in the evaluator. e.VariableValue = Decimal.Parse(this.DataSource.GetValue(col).ToDouble().ToString()); break; case BaseColumn.ColumnTypes.Currency: e.VariableValue = this.DataSource.GetValue(col).ToDecimal(); break; case BaseColumn.ColumnTypes.Boolean: e.VariableValue = this.DataSource.GetValue(col).ToBoolean(); break; case BaseColumn.ColumnTypes.Credit_Card_Date: case BaseColumn.ColumnTypes.Date: e.VariableValue = this.DataSource.GetValue(col).ToDateTime(); break; case BaseColumn.ColumnTypes.Country: case BaseColumn.ColumnTypes.Credit_Card_Number: case BaseColumn.ColumnTypes.Email: case BaseColumn.ColumnTypes.Password: case BaseColumn.ColumnTypes.String: case BaseColumn.ColumnTypes.Unique_Identifier: case BaseColumn.ColumnTypes.USA_Phone_Number: case BaseColumn.ColumnTypes.USA_State: case BaseColumn.ColumnTypes.USA_Zip_Code: case BaseColumn.ColumnTypes.Very_Large_String: case BaseColumn.ColumnTypes.Web_Url: e.VariableValue = this.DataSource.GetValue(col).ToString(); break; case BaseColumn.ColumnTypes.File: case BaseColumn.ColumnTypes.Image: // Can't do anything here. e.VariableValue = null; break; default: e.VariableValue = null; break; } } } catch (Exception) { // Ignore the error in case we cannot find the variable or its type - simply say that // the Variable Type is Nothing - implying that we do not recognize this variable. } }