/// <summary> /// selects all values that matches the wildcard expressions. Only one wild card can be given per expression /// </summary> /// <param name="variable"></param> /// <param name="query"></param> /// <returns></returns> public static PCAxis.Paxiom.Selection SelectAll(Variable variable, Query query) { PCAxis.Paxiom.Selection s = new PCAxis.Paxiom.Selection(query.Code); var values = new HashSet <string>(); foreach (var value in query.Selection.Values) { if (value.StartsWith("*")) { var variableValues = variable.Values.Where(v => v.Code.EndsWith(value.Substring(1))).Select(v => v.Code); foreach (var variableValue in variableValues) { values.Add(variableValue); } } else if (value.EndsWith("*")) { var variableValues = variable.Values.Where(v => v.Code.StartsWith(value.Substring(0, value.Length - 1))).Select(v => v.Code); foreach (var variableValue in variableValues) { values.Add(variableValue); } } else { throw new Exception("Invalid value supplied to ALL-filter"); } } //Remove duplicates s.ValueCodes.AddRange(values.Distinct().ToArray()); return(s); }
/// <summary> /// Selects all values from the variable that is in the query by the value codes /// </summary> /// <param name="variable"></param> /// <param name="query"></param> /// <returns></returns> public static PCAxis.Paxiom.Selection SelectItem(Variable variable, Query query) { PCAxis.Paxiom.Selection s = new PCAxis.Paxiom.Selection(query.Code); s.ValueCodes.AddRange( variable.Values .Where(val => query.Selection.Values.Contains(val.Code)) .Select(val => val.Code).ToArray()); if (s.ValueCodes.Count != query.Selection.Values.Length) { //TODO Error missmatch between the number of values } return(s); }
/// <summary> /// Selects all values from the variable that is in the query by the value codes /// </summary> /// <param name="variable"></param> /// <param name="query"></param> /// <returns></returns> public static PCAxis.Paxiom.Selection SelectItem(Variable variable, Query query) { PCAxis.Paxiom.Selection s = new PCAxis.Paxiom.Selection(query.Code); s.ValueCodes.AddRange( variable.Values .Where(val => query.Selection.Values.Contains(val.Code)) .Select(val => val.Code).ToArray()); if (s.ValueCodes.Count != query.Selection.Values.Length) { //Error missmatch between the number of values _logger.InfoFormat("User requests some values that does not exist for variable {0}. Found {1} matching values of the {2} requested. Returning null.", variable.Code, s.ValueCodes.Count, query.Selection.Values.Length); return(null); } return(s); }
/// <summary> /// Selects all values after a certain value. This filter is only applicable on a time variable. /// </summary> /// <param name="variable"></param> /// <param name="query"></param> /// <returns></returns> public static PCAxis.Paxiom.Selection SelectFrom(Variable variable, Query query) { if (!variable.IsTime) { throw new Exception(); //TODO only the time variable is applicable for this filter } if (query.Selection.Values.Length != 1) { throw new Exception();//TODO throw parameter exception there must be only one value } //Check if the time periods is order in ascending or descending order bool isDescending = true; if (variable.Values.Count > 1) { if (string.Compare(variable.Values[0].TimeValue, variable.Values[1].TimeValue) < 0) { isDescending = false; } } string value = query.Selection.Values[0]; int index = variable.Values.FindIndex(val => val.Code == value); PCAxis.Paxiom.Selection s = new PCAxis.Paxiom.Selection(query.Code); if (variable.Values.Count > 1) { if (isDescending) { s.ValueCodes.AddRange(variable.Values.Take(index + 1).Select(val => val.Code).ToArray()); } else { s.ValueCodes.AddRange(variable.Values.Skip(index).Take(variable.Values.Count - index).Select(val => val.Code).ToArray()); } } else { s.ValueCodes.Add(value); } return(s); }
/// <summary> /// Selects the values of the variable if the variable is time then the latest time periods for that variable is selected /// </summary> /// <param name="variable"></param> /// <param name="query"></param> /// <returns></returns> public static PCAxis.Paxiom.Selection SelectTop(Variable variable, Query query) { if (query.Selection.Values.Length != 1) { throw new Exception();//TODO throw parameter exception there must be only one value } int value; if (!int.TryParse(query.Selection.Values[0], out value)) { throw new Exception();//TODO throw parameter exception it must be an integer } if (value < 1) { throw new Exception();//TODO throw parameter exception the value must be larger than 0 } bool isDescending = true; if (variable.IsTime) { //Check if the time periods is order in ascending or descending order if (variable.Values.Count > 1) { if (string.Compare(variable.Values[0].Value, variable.Values[1].Value) < 0) { isDescending = false; } } } PCAxis.Paxiom.Selection s = new PCAxis.Paxiom.Selection(query.Code); if (isDescending) { s.ValueCodes.AddRange(variable.Values.Take(value).Select(val => val.Code).ToArray()); } else { s.ValueCodes.AddRange(variable.Values.Skip(Math.Max(0, variable.Values.Count() - value)).Take(value).Select(val => val.Code).ToArray()); } return(s); }
/// <summary> /// Calls BuildForPresentation and handles special case when we have the following condition: /// 1. Redirect to the selection page is demanded for the saved query using the ?select switch /// 2. The setting "Remove single content" is set to True /// 3. There is only one value selected for the content variable /// </summary> /// <param name="builder"></param> /// <param name="selection"></param> private void BuildModelForPresentation(IPXModelBuilder builder, PCAxis.Paxiom.Selection[] selection) { if (_format.Equals(PxUrl.PAGE_SELECT) && PCAxis.Paxiom.Settings.Metadata.RemoveSingleContent && (builder.Model.Meta.ContentVariable != null)) { // Handle the special case... Variable contentVar = builder.Model.Meta.ContentVariable.CreateCopyWithValues(); bool inHeading = (builder.Model.Meta.Heading.GetByCode(contentVar.Code) != null); bool addContent = false; PCAxis.Paxiom.Selection selCont = selection.First(s => s.VariableCode.Equals(contentVar.Code)); if (selCont.ValueCodes.Count == 1) { // Remove all values except the one selected contentVar.Values.RemoveAll(x => x.Code != selCont.ValueCodes[0]); addContent = true; } // The content variable will be eliminated in BuildForPresentation ... builder.BuildForPresentation(selection); // ... but we need it on the selection page so we add it manually to the model after the call to BuildForPresentation if (addContent) { builder.Model.Meta.Variables.Add(contentVar); if (inHeading) { builder.Model.Meta.Heading.Add(contentVar); } else { builder.Model.Meta.Stub.Add(contentVar); } } } else { builder.BuildForPresentation(selection); } }
private PXModel LoadData(PCAxis.Query.SavedQuery sq) { //Only loads the first table source otherwise redirects to a page if (sq.Sources.Count != 1) { //TODO redirect to error page incopatable query for PX-Web } TableSource src = sq.Sources[0]; IPXModelBuilder builder = null; if (src.Type == "CNMM") { if (RouteInstance.RouteExtender == null) { DatabaseInfo db = PXWeb.Settings.Current.General.Databases.GetCnmmDatabase(src.DatabaseId); if (db == null) { //TODO Redirect database does not exist return(null); } var tableName = QueryHelper.GetTableName(src); builder = PxContext.CreatePaxiomBuilder(src.DatabaseId, tableName); } else { DatabaseInfo db = PXWeb.Settings.Current.General.Databases.GetCnmmDatabase(RouteInstance.RouteExtender.GetDatabase()); var tableName = QueryHelper.GetTableName(src); builder = PxContext.CreatePaxiomBuilder(RouteInstance.RouteExtender.Db.Database.id, tableName); } } else if (src.Type == "PX") { DatabaseInfo db = PXWeb.Settings.Current.General.Databases.GetPxDatabase(src.DatabaseId); if (db == null) { } else { if (!db.HasLanguage(src.Language)) { //TODO Redirect that the database is missing return(null); } if (src.Source.StartsWith("-/")) { src.Source = src.DatabaseId + src.Source.Substring(1); } builder = PxContext.CreatePaxiomBuilder(src.DatabaseId, src.Source); } } else { //TODO redirect to error page incompatible datasource type return(null); } builder.SetPreferredLanguage(src.Language); //If languefe set in reques we must read all langauges to be able to run workflow operations if (_language != null) { builder.ReadAllLanguages = true; } builder.BuildForSelection(); var model = builder.Model; List <PCAxis.Paxiom.Selection> sel = new List <PCAxis.Paxiom.Selection>(); foreach (var variable in model.Meta.Variables) { var query = src.Quieries.FirstOrDefault(q => q.Code == variable.Code); PCAxis.Paxiom.Selection s = null; if (query == null) { //Selects all values for the variable if it can't be eliminated s = new PCAxis.Paxiom.Selection(variable.Code); if (variable.IsContentVariable || !variable.Elimination) { s.ValueCodes.AddRange(variable.Values.Select(v => v.Code).ToArray()); } } else { if (PCAxis.Query.QueryHelper.IsAggregation(query.Selection.Filter)) { s = QueryHelper.SelectAggregation(variable, query, builder); } else if (query.Selection.Filter.StartsWith("vs:", StringComparison.InvariantCultureIgnoreCase)) { s = QueryHelper.SelectValueSet(variable, query, builder); } else { switch (query.Selection.Filter) { case "item": s = QueryHelper.SelectItem(variable, query); break; case "top": s = QueryHelper.SelectTop(variable, query); break; case "from": s = QueryHelper.SelectFrom(variable, query); break; case "all": s = QueryHelper.SelectAll(variable, query); break; default: //TODO unsupported filter break; } } } if (s != null) { sel.Add(s); } } var selection = sel.ToArray(); //TODO fixa till //if (sq.Output.Type == "SCREEN") PCAxis.Query.TableQuery tbl = new PCAxis.Query.TableQuery(builder.Model, selection); PaxiomManager.QueryModel = tbl; BuildModelForPresentation(builder, selection); return(builder.Model); }
public static List <PCAxis.Paxiom.Selection> BuildSelections(this PCAxis.Paxiom.IPXModelBuilder builder, TableQuery tableQuery) { //Check to see that the variable exists int c = builder.Model.Meta.Variables.Where(var => tableQuery.Query.Select(q => q.Code).Contains(var.Code)).ToArray().Length; if (tableQuery.Query.Length > c) { throw new ArgumentException("Variable is not defined"); } var selections = new List <PCAxis.Paxiom.Selection>(); foreach (var variable in builder.Model.Meta.Variables) { PCAxis.Paxiom.Selection selection = new PCAxis.Paxiom.Selection(variable.Code); var query = tableQuery.Query.SingleOrDefault(q => q.Code == variable.Code); if (query != null) { // Process filters if (query.Selection.Filter.ToLower() == "all") // All { selection = QueryHelper.SelectAll(variable, query); } else if (query.Selection.Filter.ToLower() == "top") // Top { selection = QueryHelper.SelectTop(variable, query); } else if (PCAxis.Query.QueryHelper.IsAggregation(query.Selection.Filter)) { selection = QueryHelper.SelectAggregation(variable, query, builder); } else if (query.Selection.Filter.StartsWith("vs:", StringComparison.InvariantCultureIgnoreCase)) { selection = QueryHelper.SelectValueSet(variable, query, builder); } else { // Assume item selection = QueryHelper.SelectItem(variable, query); } } else { // Variable not specified in query if (!variable.Elimination) { selection = PCAxis.Paxiom.Selection.SelectAll(variable); } } if (selection != null) { selections.Add(selection); } else { //The user as requested an non vaild selection return(null); } } if (!ValidateSelection(builder, selections)) { return(null); } return(selections); }