Ejemplo n.º 1
0
        /// <summary>
        /// Generates DevExpress report.
        /// </summary>
        /// <param name="userConnection">User connection.</param>
        /// <param name="configuration">Provides configuration for report generator.</param>
        /// <returns>Returns prepared report.</returns>
        public ReportData Generate(UserConnection userConnection, ReportGeneratorConfiguration configuration)
        {
            var    templateId = configuration.ReportTemplateId;
            var    schemaName = GetSchemaNameByTemplateId(userConnection, templateId);
            var    recordUId  = configuration.RecordId;
            string reportName = GetTemplateNameByUId(userConnection, templateId);

            byte[] data           = GetTemplateSource(userConnection, templateId);
            var    openXmlUtility = new OpenXmlUtility(userConnection);

            using (MemoryStream stream = new MemoryStream()) {
                stream.Write(data, 0, data.Length);
                using (WordprocessingDocument docx = WordprocessingDocument.Open(stream, true)) {
                    Collection <string>            captions = openXmlUtility.GetMergeFieldName(docx);
                    Dictionary <string, DataValue> dic      = GetEntityValuesByColumnName(userConnection, recordUId, schemaName, captions, templateId);
                    WordprocessingDocument         report   = openXmlUtility.GetWordReport(docx, dic);
                    var additionalMacrosUtility             = new AdditionalMacrosUtility(userConnection);
                    additionalMacrosUtility.FillAdditionalMacros(report, recordUId, schemaName, templateId);
                    docx.Close();
                }
                stream.Seek(0, SeekOrigin.Begin);
                return(new ReportData {
                    Caption = reportName,
                    Format = "docx",
                    Data = stream.ToArray()
                });
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Returns entity values by column name.
        /// </summary>
        /// <param name="entityId">Entity id.</param>
        /// <param name="entityName">Entity name.</param>
        /// <param name="captions">Collection of captions.</param>
        /// <param name="templateId">Template id.</param>
        /// <returns>Object.</returns>
        private Dictionary <string, DataValue> GetEntityValuesByColumnName(UserConnection userConnection, Guid entityId, string entityName,
                                                                           Collection <string> captions, Guid templateId)
        {
            Dictionary <string, string>    dicEsq  = new Dictionary <string, string>();
            Dictionary <string, DataValue> fillDic = new Dictionary <string, DataValue>();

            if (captions.Count == 0)
            {
                return(fillDic);
            }
            var expressionConverterHelper = new ExpressionConverterHelper();
            Dictionary <string, ColumnMacros> dicMacros = new Dictionary <string, ColumnMacros>();
            var    entitySchemaManager = userConnection.EntitySchemaManager;
            var    entitySchema        = entitySchemaManager.GetInstanceByName(entityName);
            var    esq        = new EntitySchemaQuery(entitySchemaManager, entitySchema.Name);
            string macroslist = GetMacrosListByTemplateId(userConnection, templateId);

            if (string.IsNullOrEmpty(macroslist))
            {
                return(fillDic);
            }
            var macrosList = Json.Deserialize(macroslist) as JArray;

            foreach (string item in captions)
            {
                var fullColumnName    = item.Trim();
                var currentMacrosList = expressionConverterHelper.MacrosList(fullColumnName);
                var columnName        = expressionConverterHelper.GetColumnName(fullColumnName);
                if (currentMacrosList.Count > 0 && !dicMacros.ContainsKey(fullColumnName))
                {
                    dicMacros.Add(fullColumnName, new ColumnMacros {
                        Name       = fullColumnName,
                        ColumnName = columnName,
                        MacrosList = currentMacrosList
                    });
                }
                if (!dicEsq.ContainsKey(columnName))
                {
                    AddColumnToEntitySchemaQuery(userConnection, esq, columnName, macrosList, dicEsq);
                }
            }
            EntitySchemaQueryExpressionCollection selectingQueryExpressions = esq.Columns.GetSelectingExpressions();

            if (selectingQueryExpressions.Count == 0)
            {
                return(fillDic);
            }
            esq.Filters.Add(esq.CreateFilterWithParameters(FilterComparisonType.Equal, "Id", entityId));
            EntityCollection entityCollection = esq.GetEntityCollection(userConnection);

            if (entityCollection.Count > 0)
            {
                AdditionalMacrosUtility additionalMacrosUtility = new AdditionalMacrosUtility(userConnection);
                foreach (KeyValuePair <string, string> item in dicEsq)
                {
                    Entity             entity           = entityCollection[0];
                    EntitySchemaColumn entityColumnName = entity.Schema.Columns.GetByName(item.Value);
                    object             value            = additionalMacrosUtility.GetReportColumnValue(entity, entityColumnName);
                    fillDic.Add(item.Key, new DataValue(entityColumnName.DataValueType, value));
                    if (dicMacros.Count(d => d.Value.ColumnName == item.Key) > 0)
                    {
                        foreach (var macrosItem in dicMacros.Where(d => d.Value.ColumnName == item.Key))
                        {
                            ColumnMacros             macros      = macrosItem.Value;
                            ExpressionConverterValue macrosValue =
                                expressionConverterHelper.GetValue(macros.MacrosList, value);
                            fillDic.Add(macrosItem.Key, new DataValue(macrosValue.ValueType, macrosValue.Data));
                        }
                    }
                }
            }
            return(fillDic);
        }