/// <summary> /// Attempts to infer the type of cells present by obtaining /// the first 30 cells and counting the possible types. /// </summary> /// <returns>The cell type for column.</returns> /// <param name="column">The column to use.</param> public Type InferCellTypeForColumn(string column) { string startRow = "1"; string endRow = "30"; ICollection<ICell> cells = _dataSource.GetCells (column, startRow, endRow); IDictionary<Type, int> typeCounts = new Dictionary<Type, int>(); foreach (ICell cell in cells) { int oldValue = 0; if (typeCounts.TryGetValue(cell.GetType(), out oldValue)) { typeCounts.Add(cell.GetType(), oldValue + 1); } else { typeCounts.Add (cell.GetType(), 0); } } int max = typeCounts.Max (k => k.Value); return typeCounts.Where(k => k.Value.Equals(max)).FirstOrDefault().Key; }
private List<KeyValuePair<AiAction, int>> GetPotentialActions() { var potential = new Dictionary<AiAction, int>(); foreach (var t in _actions) { int n; if (t.Trigger(out n)) potential.Add(t, n); } if (potential.Count == 0) return new List<KeyValuePair<AiAction, int>>(); var max = potential.Max(kvp => kvp.Value); var matches = potential.Where(kvp => kvp.Value == max).ToList(); return matches; }
/// <summary> /// Builds the report. /// </summary> /// <param name="reportDocument">The report document.</param> public void BuildReport(ICustomReport reportDocument) { var currentReport = reportDocument.Reports.First(); SetIdentity(currentReport); //_originalPageHeaderHeight = ((PageHeaderSection) currentReport.Items["pageHeader"]).Height; //This report parameter holds the current template system name. _processName = currentReport.ReportParameters["processName"].Value.ToString(); //This report parameter holds the currently visible columns of the grid and in what order they're in var cols = currentReport.ReportParameters["columns"].Value.ToString(); var columns = cols.Split(';').ToList(); _columns = new List<ColumnInfo>(); foreach (var col in columns) { _columns.Add(new ColumnInfo(col)); } var groupsStr = currentReport.ReportParameters["groups"].Value.ToString(); _groups = (string.IsNullOrEmpty(groupsStr) ? new string[0] : groupsStr.Split(';')).ToList(); //TODO: Uncomment if needed // _columns.AddRange(_groups); if (reportDocument.FitToPage) { if (currentReport.GetType().Name.ToLower().Contains("landscape")) { FitToPageColumnWidth = Math.Min(1, 10.0 / _columns.Count); FitToPageColumnCount = (short) Math.Max(10, _columns.Count); } if (currentReport.GetType().Name.ToLower().Contains("portrait")) { FitToPageColumnWidth = Math.Min(1.1, 7.0 / _columns.Count); FitToPageColumnCount = (short) Math.Max(7, _columns.Count); } currentReport.Width = new Unit(_columns.Count * FitToPageColumnWidth, UnitType.Inch); } _groupColumn = currentReport.ReportParameters["groupColumn"].Value.ToString(); var fldList = new List<FieldInfo>(_columns.Count); var processProperties = new Dictionary<PropertyInfo, string>(); var processType = TheDynamicTypeManager.GetInfoType<IInfoClass>(_processName); if (processType != null) { processProperties = processType.GetAllPropertiesWithPaths(); var properties = processProperties.Where(p => _columns.Any(c=>c.SystemName == p.Value)); foreach (var column in _columns) { foreach (var property in properties) { if (property.Value == column.SystemName) { fldList.Add(new FieldInfo(property.Key, column)); break; } } } _fieldAttrDict = GetFieldAttributes(processProperties.Select(x => x.Key)); } if (_fieldAttrDict == null) _fieldAttrDict = new Dictionary<PropertyInfo, FieldParametres>(); _colDict = new SortedDictionary<int, string[]>(); //Grouping foreach (var colName in _groups) { var group = new Telerik.Reporting.Group(); group.Name = colName; var groupHeaderSection = new GroupHeaderSection(); var groupFooterSection = new GroupFooterSection(); group.GroupHeader = groupHeaderSection; group.GroupFooter = groupFooterSection; var txt = new TextBox(); txt.Style.Font.Size = new Unit(10, UnitType.Point); txt.Style.Font.Bold = true; txt.Style.BackgroundColor = Color.FromArgb(255, 225, 225, 225); txt.Style.TextAlign = HorizontalAlign.Left; txt.Style.VerticalAlign = VerticalAlign.Middle; groupHeaderSection.Items.Add(txt); groupHeaderSection.Height = new Unit(21); groupHeaderSection.Style.BorderStyle.Default = BorderType.Solid; groupHeaderSection.Style.BorderColor.Default = Color.White; groupHeaderSection.Style.BorderColor.Left = Color.Transparent; var panel = new Panel(); panel.Height = new Unit(20); panel.Style.LineStyle = LineStyle.Solid; panel.Style.LineWidth = new Unit(2.0); panel.Style.BorderColor.Default = Color.Black; panel.Style.BorderWidth.Default = new Unit(2.0); panel.Style.BorderStyle.Default = BorderType.None; panel.Style.BorderStyle.Top = BorderType.Solid; panel.Style.BorderStyle.Bottom = BorderType.Solid; groupFooterSection.Items.Add(panel); groupFooterSection.Height = new Unit(25); //groupFooterSection.Style.BorderStyle.Default = BorderType.Solid; //groupFooterSection.Style.BorderColor.Default = Color.White; groupFooterSection.Style.BorderColor.Left = Color.Transparent; groupFooterSection.Style.Padding.Top = new Unit(2); groupFooterSection.Style.Padding.Bottom = new Unit(2); //if you need to filter the data, apply filtering //group1.Filters.Add(new Telerik.Reporting.Data.Filter("=Fields.ProductID", Telerik.Reporting.Data.FilterOperator.Equal, "=10")); currentReport.Groups.Add(group); } var aggregateData = currentReport.ReportParameters["aggregates"].Value.ToString(); _aggregateDict = ParseAggregateData(aggregateData); var nextLeft = _groups.Count > 0 ? (_groups.Count - 1) * 0.15 : 0; var availableWidth = currentReport.Width.Value - nextLeft; var ctrlSize = availableWidth / (fldList.Count() - _groups.Count); _indentOffset = 0; _reportItemCounter = new Dictionary<string, int>(); var counter = 0; double fitToPageFooterLocationX = 0; //posess fields foreach (var fld in fldList) { if (_reportItemCounter.ContainsKey(fld.Property.Name)) { continue; } var fieldValueMap = GetFieldValueMap(currentReport, fld.Property, processProperties[fld.Property]); if (_groups.Contains(processProperties[fld.Property] ?? string.Empty)) { //create group var fieldGroup = currentReport.Groups.FirstOrDefault(x => x.Name == processProperties[fld.Property]); if (fieldGroup != null) { _indentOffset = 0.15 * _groups.IndexOf(processProperties[fld.Property]); fieldGroup.Groupings.Add(new Grouping(fieldValueMap)); ((TextBox)fieldGroup.GroupHeader.Items[0]).Value = string.Format("=' ' + {0} + ' Total: ' + Exec('{1}', Count(IsNull({0},1)))", fieldValueMap.TrimStart('='), fieldGroup.Name); ((TextBox)fieldGroup.GroupHeader.Items[0]).Location = new PointU(new Unit(_indentOffset, UnitType.Inch), new Unit(0, UnitType.Inch)); ((TextBox)fieldGroup.GroupHeader.Items[0]).Size = new SizeU(new Unit(currentReport.Width.Value - _indentOffset, UnitType.Inch), new Unit(20, UnitType.Pixel)); //AddGroupLevelLines(fieldGroup.GroupHeader.Items); ((Panel)fieldGroup.GroupFooter.Items[0]).Location = new PointU(new Unit(_indentOffset, UnitType.Inch), new Unit(0, UnitType.Inch)); ((Panel)fieldGroup.GroupFooter.Items[0]).Size = new SizeU(new Unit(currentReport.Width.Value - _indentOffset, UnitType.Inch), new Unit(20, UnitType.Pixel)); //AddGroupLevelLines(fieldGroup.GroupFooter.Items); } continue; } var cPoint = new PointU(new Unit(nextLeft, UnitType.Inch), new Unit(0.4, UnitType.Inch)); var uSize = new SizeU(new Unit(ctrlSize, UnitType.Inch), new Unit(0.2, UnitType.Inch)); var attr = fld.Property.GetCustomAttributes(false); var displayAttr = attr.FirstOrDefault(x => x is DisplayAttribute) as DisplayAttribute; string colName = "<NoName>"; if (displayAttr == null) { colName = fld.Column.Header; //var infoInstance = Activator.CreateInstance(processType); //if (infoInstance != null) //{ // var field = processType.GetField(fld.Name + "Property"); // if (field != null) // { // var fieldValue = field.GetValue(infoInstance) as IPropertyInfo; // if (fieldValue != null) // { // colName = fieldValue.FriendlyName; // } // } //} } else { colName = displayAttr.GetName(); } var headerLabel = new TextBox { Value = colName, Location = cPoint, Size = uSize, StyleName = "Caption", Multiline = true, CanGrow = true }; headerLabel.Style.BackgroundColor = Color.Transparent; headerLabel.Style.Font.Size = new Unit(10, UnitType.Point); headerLabel.Style.VerticalAlign = VerticalAlign.Top; var dataTextBox = new TextBox(); var dataTextBoxName = string.Format("{0}TextBox", processProperties[fld.Property]); dataTextBox.Name = dataTextBoxName; var propertyName = processProperties[fld.Property]; var isExistsBackgroudColor = processProperties.Any(x => x.Key.Name == propertyName + Constants.FieldBackColorPostfix); dataTextBox.TextWrap = true; dataTextBox.Location = new PointU(new Unit(nextLeft, UnitType.Inch), new Unit(0, UnitType.Inch)); dataTextBox.Size = uSize; dataTextBox.CanGrow = true; if (isExistsBackgroudColor) { if (fieldValueMap.StartsWith("=")) { fieldValueMap = fieldValueMap.Remove(0, 1); fieldValueMap = string.Format("=GetValueWithBackgroundColor({0}, Fields.[{1}{2}])", fieldValueMap, processProperties[fld.Property], Constants.FieldBackColorPostfix); EventHandler itemDataBoundHandler = (sender, args) => { var textBox = sender as Telerik.Reporting.Processing.TextBox; if (textBox == null) { return; } var colorAsLong = ((KeyValuePair<object, long>)textBox.Value).Value; if (colorAsLong != 0) { var bytes = BitConverter.GetBytes(colorAsLong); if (bytes[3] == 0) bytes[3] = 255; var color = Color.FromArgb(bytes[3], bytes[2], bytes[1], bytes[0]); ((Telerik.Reporting.Processing.ProcessingElement)sender).Style.BackgroundColor = color; } textBox.Value = ((KeyValuePair<object, long>)textBox.Value).Key; }; dataTextBox.ItemDataBound += itemDataBoundHandler; EventHandler dataTextBoxDisposedHandler = null; dataTextBoxDisposedHandler = (sender, args) => { dataTextBox.ItemDataBound -= itemDataBoundHandler; dataTextBox.Disposed -= dataTextBoxDisposedHandler; }; dataTextBox.Disposed += dataTextBoxDisposedHandler; } } dataTextBox.Value = fieldValueMap; if (!reportDocument.FitToPage) { dataTextBox.ItemDataBound += OnItemDataBound; EventHandler dataTextBoxDisposedHandler = null; dataTextBoxDisposedHandler = (sender, args) => { dataTextBox.ItemDataBound -= OnItemDataBound; dataTextBox.Disposed -= dataTextBoxDisposedHandler; }; dataTextBox.Disposed += dataTextBoxDisposedHandler; } var pictureBox = new PictureBox { Value = fieldValueMap }; pictureBox.Name = string.Format("{0}TextBox", processProperties[fld.Property]); pictureBox.Sizing = ImageSizeMode.ScaleProportional; if (!reportDocument.FitToPage) { pictureBox.ItemDataBound += OnItemDataBound; EventHandler pictureBoxDisposedHandler = null; pictureBoxDisposedHandler = (sender, args) => { pictureBox.ItemDataBound -= OnItemDataBound; pictureBox.Disposed -= pictureBoxDisposedHandler; }; pictureBox.Disposed += pictureBoxDisposedHandler; } nextLeft = nextLeft + ctrlSize; if (reportDocument.FitToPage) { currentReport.Items["labelsGroupHeaderSection"].Items.Add(headerLabel); } else { currentReport.Items["pageHeader"].Items.Add(headerLabel); } if (_fieldAttrDict[fld.Property].FieldEditor == "Image") { currentReport.Items["detail"].Items["rowPanel"].Items.Add(pictureBox); } else { currentReport.Items["detail"].Items["rowPanel"].Items.Add(dataTextBox); } _reportItemCounter.Add(processProperties[fld.Property], 0); if (reportDocument.FitToPage) { _colDict.Add(currentReport.Items["labelsGroupHeaderSection"].Items.Count - 1, new[] { processProperties[fld.Property], headerLabel.Value }); } else { _colDict.Add(currentReport.Items["pageHeader"].Items.Count - 1, new[] { processProperties[fld.Property], headerLabel.Value }); } if (reportDocument.FitToPage) { fitToPageFooterLocationX = counter % FitToPageColumnCount == 0 ? 0 : fitToPageFooterLocationX; if (_aggregateDict.Keys.All(x => x != fld.Property.Name)) { var footerTextBox = new TextBox { Name = string.Format("{0}{1}FooterTextBox", fld.Property.Name, fldList.IndexOf(fld)), Location = new PointU(new Unit(fitToPageFooterLocationX, UnitType.Inch), new Unit(0, UnitType.Inch)), Size = new SizeU(new Unit(FitToPageColumnWidth, UnitType.Inch), new Unit(0.15, UnitType.Inch)) }; currentReport.Items["pageFooter"].Items.Add(footerTextBox); } //calculate server-side aggregates var aggregateDefinitions = _aggregateDict.ToList() .SelectMany(i => i.Value) .Where(d => !AggregateHelper.IsPageAggregateType(d.SummaryType)) .ToList(); var queryCriteria = new AggregateDataSourceQueryCriteria { ProcessName = _processName, FilterDefinition = GetFilterDefinition(currentReport) }; var serverAggregateResults = AggregateProcessor.CalculateAggregates(aggregateDefinitions, queryCriteria); foreach (var aggregateItem in _aggregateDict) { if (aggregateItem.Key != fld.Property.Name) { continue; } double fitToPageFooterLocationY = 0; for (var i = 0; i < aggregateItem.Value.Count; i++) { var footerTextBox = new TextBox { Name = string.Format("{0}{1}FooterTextBox", fld.Property.Name, aggregateItem.Value[i].SummaryType), // telerik recommends set unique names for all controls Size = new SizeU(new Unit(GetWidthFitToPage(fld.Property.Name, i), UnitType.Inch), new Unit(0.15, UnitType.Inch)), Location = new PointU(new Unit(fitToPageFooterLocationX, UnitType.Inch), new Unit(fitToPageFooterLocationY, UnitType.Inch)) }; if (AggregateHelper.IsPageAggregateType(aggregateItem.Value[i].SummaryType)) { footerTextBox.Value = string.Format(@"='{0}: ' + AfterCalculateAggregates(PageExec('{1}TextBox', {2}(BeforeCalculateAggregates(Fields.[{1}], ""{2}"", ""{3}"", ""{4}"", ""{5}"", ""{6}""))), ""{2}"", ""{3}"", ""{4}"", ""{5}"", ""{6}"")", AggregateProcessor.GetAggregateFunctionShortName(aggregateItem.Value[i].SummaryType), aggregateItem.Key, aggregateItem.Value[i].SummaryType, aggregateItem.Value[i].CustomConverter, aggregateItem.Value[i].TargetType, aggregateItem.Value[i].TypeName, aggregateItem.Value[i].ConverterParameter); } else { var result = serverAggregateResults.FirstOrDefault(r => r.ColumnName == aggregateItem.Key && r.SummaryType == aggregateItem.Value[i].SummaryType); footerTextBox.Value = string.Format("{0}: {1}", AggregateHelper.GetAttribute(aggregateItem.Value[i].SummaryType).ShortName, result != null ? result.Value : Constants.Empty); } if (aggregateItem.Value[i].SummaryType == SummaryTypes.Count) { footerTextBox.Bindings.Add(new Binding("Visible", string.Format(@"=IIf(PageExec(""{0}"", Count(Fields.{1})) = 0, False, True)", dataTextBoxName, fld.Property.Name))); } if (aggregateItem.Value[i].SummaryType == SummaryTypes.Min || aggregateItem.Value[i].SummaryType == SummaryTypes.Max) { if (fld.Property.PropertyType != typeof(bool) && fld.Property.PropertyType != typeof(bool?))//VRN-5079 { footerTextBox.Bindings.Add(new Binding("Visible", string.Format(@"=FooterTextBoxVisibilityWithValue(PageExec(""{0}"", Min(Fields.{1})))", dataTextBoxName, fld.Property.Name))); } } fitToPageFooterLocationY += 0.15; currentReport.Items["pageFooter"].Items.Add(footerTextBox); } } fitToPageFooterLocationX += FitToPageColumnWidth; counter++; } } if (reportDocument.FitToPage) { var max = _aggregateDict.Count == 0 ? 0 : _aggregateDict.Max(x => x.Value.Count); if (max != 0) { ((PageFooterSection) currentReport.Items["pageFooter"]).Height = new Unit(max*0.15 + 0.2, UnitType.Inch); } currentReport.Items["pageFooter"].Items["currentTimeTextBox"].Visible = false; currentReport.Items["pageFooter"].Items["pageInfoTextBox"].Visible = false; } _indentOffset = _groups.Count > 0 ? (_groups.Count - 1) * 0.15 : 0; //AddGroupLevelLines(currentReport.Items["detail"].Items["rowPanel"].Items); }
public Element?[] Spell(string word, SearchAlgorithm algorithm) { Dictionary<int, Element> indexed = new Dictionary<int, Element>(); word = Regex.Replace(word.ToLower(), "[^a-z\\s]", ""); switch (algorithm) { case SearchAlgorithm.ElementBased: foreach (Element element in elements) { string symbol = element.Symbol.ToLower(); if (word.Contains(symbol)) { foreach (int i in word.IndexOfAll(symbol)) indexed.Add(i, element); word = word.Replace(symbol, new string ('_', symbol.Length)); } } break; case SearchAlgorithm.ChunkSearch: int maxElementLength = elements.Max(e => e.Symbol.Length); for (int searchLength = maxElementLength; searchLength > 0; searchLength--) { Element[] currentElements = elements.Where(e => e.Symbol.Length == searchLength).ToArray(); for (int x = 0; x < word.Length - searchLength + 1; x++) foreach(Element currentElement in currentElements) if (word.Substring(x, searchLength) == currentElement.Symbol.ToLower()) { indexed.Add(x, currentElement); ArrayList tmpList = new ArrayList(((ICollection)(Array)word.ToCharArray())); tmpList.SetRange(x, (ICollection)(Array)new string('_', searchLength).ToCharArray()); word = new string(Array.ConvertAll(tmpList.ToArray(), item => (char)item)); } } break; } List<Element?> spelled = new List<Element?>(); int max = indexed.Max(item => item.Key); Element value; for (int i = 0; i <= max; i++) { if (indexed.TryGetValue(i, out value)) spelled.Add(value); else if (word[i] == ' ') spelled.Add(null); } return spelled.ToArray(); }