Esempio n. 1
0
        public void BuildUnionDTO(SearchRequestDto searchDto, ApplicationSchemaDefinition originalSchema)
        {
            var unionSchema = MetadataProvider.FindSchemaDefinition(originalSchema.UnionSchema);

            unionDTO = new SearchRequestDto();
            unionDTO.BuildProjection(unionSchema);
            unionDTO.FilterFixedWhereClause = searchDto.UnionFilterFixedWhereClause;
            if (String.IsNullOrWhiteSpace(SearchParams))
            {
                return;
            }

            var sb               = new StringBuilder(SearchParams);
            var sbReplacingIdx   = 0;
            var nullParamCounter = 0;

            var searchParameters = GetParameters();
            var parameters       = Regex.Split(SearchParams, SearchUtils.SearchParamSpliter).Where(f => !String.IsNullOrWhiteSpace(f));

            foreach (var parameterSt in parameters)
            {
                var parameter = searchParameters[parameterSt];

                var key = parameterSt.Split('.').Last();

                var fields = originalSchema.Fields;
                var idx    = 0;

                ApplicationFieldDefinition unionParameter = null;
                foreach (var field in fields)
                {
                    // need to recover field from the original schema, since on the client side it will pass this as the parameter name.
                    // the union schema has to be declared on the same order, but could have different field aliases
                    if (field.Attribute.EndsWith(key))
                    {
                        unionParameter = unionSchema.Fields[idx];
                        break;
                    }
                    idx++;
                }

                string newSearchParam;
                if (unionParameter != null)
                {
                    newSearchParam = unionParameter.Attribute + "_union";
                }
                else
                {
                    newSearchParam = "null" + nullParamCounter++;
                }

                var idxToReplace = sb.ToString().IndexOf(parameterSt, sbReplacingIdx, StringComparison.Ordinal);
                sb.Replace(parameterSt, newSearchParam, idxToReplace, parameterSt.Length);
                sbReplacingIdx += newSearchParam.Length;
            }

            unionDTO.SearchParams = sb.ToString();
            unionDTO.SearchValues = SearchValues;

            HandleUnionZeroedValueCases(unionDTO);
        }
Esempio n. 2
0
 public string BuildWhereClause(string entityName, QueryCacheKey.QueryMode queryMode, SearchRequestDto searchDto = null)
 {
     return(BuildWhereClause(entityName));
 }
Esempio n. 3
0
        private static IEnumerable <string> getParametersOnTheRightOrder(IDictionary <string, SearchParameter> searchParameters, SearchRequestDto listDto)
        {
            var baseList = Regex.Split(listDto.SearchParams, SearchParamSpliter).Where(f => !String.IsNullOrWhiteSpace(f));
            var result   = new List <string>();
            var i        = 0;

            foreach (var param in baseList)
            {
                if (searchParameters.ContainsKey(i + param))
                {
                    //if there are multiple parameters for the same column (ex: a fixed whereclause + a grid filter status&&status --> status&&1status)
                    result.Add(i + param);
                }
                else
                {
                    result.Add(param);
                }
                i++;
            }
            return(result);
        }
Esempio n. 4
0
 public SearchUtils(SearchRequestDto searchDto, String entityName, string tableName)
 {
     _searchDTO  = searchDto;
     _entityName = entityName;
     _tableName  = tableName;
 }
Esempio n. 5
0
        private static string HandleSearchParams(SearchRequestDto listDto, string entityName)
        {
            var sb             = new StringBuilder();
            var sbReplacingIdx = 0;


            sb.Append(listDto.SearchParams).Replace("#", "");
            var searchParameters = listDto.GetParameters();
            var parameters       = getParametersOnTheRightOrder(searchParameters, listDto);
            var j = 0;

            foreach (var parameter in parameters)
            {
                var searchParameter = searchParameters[parameter];
                if (parameter.StartsWith("null"))
                {
                    if (searchParameter.SearchOperator == SearchOperator.BLANK)
                    {
                        //HAP-1106-- > if blank operator, then all null union fields are acceptable, otherwise none
                        sb.Replace(parameter, "1=1");
                    }
                    else
                    {
                        sb.Replace(parameter, "1!=1");
                    }
                    //ignore null parameters, as of union grids
                    continue;
                }

                //need to fetch from here to keep order correct


                var param     = parameter;
                var statement = new StringBuilder();

                var parameterData  = GetParameterData(entityName, searchParameter, param);
                var operatorPrefix = searchParameter.SearchOperator.OperatorPrefix();

                if (searchParameter.SearchOperator == SearchOperator.BETWEEN)
                {
                    statement.Append("( " + parameterData.Item1 + " >= :" + param + "_start" + "&&" + parameterData.Item1 + " <= :" + param + "_end" + ")");
                }
                else if (searchParameter.SearchOperator == SearchOperator.ORCONTAINS)
                {
                    var values = (searchParameter.Value as IEnumerable).Cast <string>().ToList();
                    if (values != null)
                    {
                        statement.Append("( ");

                        for (var i = 0; i < values.Count; i++)
                        {
                            statement.Append(parameterData.Item1);
                            // this next line would be the ideal, but it will be complicade passing this parameters to BaseHibernateDAO.
                            //statement.Append(GetDefaultParam(operatorPrefix, param + i));
                            // TODO: refactor later
                            statement.Append(operatorPrefix + "'%" + values[i] + "%'");
                            statement.Append(" OR ");
                        }
                        statement.Remove(statement.Length - 4, 4); // remove the last " OR "
                        statement.Append(" )");
                    }
                }
                else
                {
                    searchParameter.IsNumber = parameterData.Item2 == ParameterType.Number;

                    statement.Append("( " + parameterData.Item1);

                    if (searchParameter.IsList)
                    {
                        statement.Append(operatorPrefix).Append(String.Format(HibernateUtil.ListParameterPrefixPattern, param));
                    }
                    else if (searchParameter.IsDate || parameterData.Item2 == ParameterType.Date)
                    {
                        statement.Append(HandleDateAttribute(param, searchParameter, operatorPrefix));
                    }
                    else if (searchParameter.SearchOperator == SearchOperator.BLANK && (searchParameter.IsNumber || searchParameter.IsDate))
                    {
                        statement.Append(" IS NULL ");
                    }
                    else
                    {
                        statement.Append(GetDefaultParam(operatorPrefix, param));
                    }

                    if (searchParameter.SearchOperator == SearchOperator.NOTEQ)
                    {
                        statement.Append(" OR " + parameterData.Item1 + " IS NULL " + " )");
                    }
                    else
                    {
                        statement.Append(" )");
                    }
                }
                var idxToReplace = sb.ToString().IndexOf(param, sbReplacingIdx, StringComparison.Ordinal);
                if (idxToReplace == -1 && param.IndexOf(j.ToString(CultureInfo.InvariantCulture), StringComparison.Ordinal) != -1)
                {
                    //if the parameter contains a number, let´s try now replacing it
                    //this is used on scenarios where we have a fixed query and grid query where we constrain one of the parameters of the original query with a gridfilter value
                    //then we would have two parameters with the same name. To vaoid it, we rename it to the position+ name (ex: 5status;)
                    param        = param.Replace(j.ToString(CultureInfo.InvariantCulture), "");
                    idxToReplace = sb.ToString().IndexOf(param, sbReplacingIdx, StringComparison.Ordinal);
                }
                if (idxToReplace != -1)
                {
                    //there´s just one case which was done as a workaround for fetching the commodities. see HapagImacDataSet#GetAssetCommodities
                    sb.Replace(param, statement.ToString(), idxToReplace, param.Length);
                    sbReplacingIdx += statement.ToString().Length;
                }
                j++;
            }


            sb.Replace("&&", " AND ");
            sb.Replace("||,", " OR ");
            return(sb.ToString());
        }