Beispiel #1
0
        private QvDataContractResponse GetPreview(UserParameter parameter, string appId, string objectId)
        {
            q2gconhypercubemain.Connection connection = null;

            try
            {
                var oId = GetObjectId(objectId);
                if (String.IsNullOrEmpty(oId))
                {
                    throw new Exception("no object id for preview table found.");
                }
                var config  = QlikApp.CreateConfig(parameter, appId);
                var qlikApp = new QlikApp(parameter);
                connection = qlikApp.CreateNewConnection(config);
                var script      = ScriptCode.Create(appId, oId);
                var resultTable = tableFunctions.GetTableInfosFromApp("PreviewTable", script, connection.CurrentApp);
                if (resultTable == null)
                {
                    throw new Exception("no preview table found.");
                }
                return(PreviewResponse.Create(resultTable.Preview));
            }
            catch (Exception ex)
            {
                logger.Error(ex, $"fields from app {appId} and table {objectId} not loaded.");
                return(new PreviewResponse());
            }
            finally
            {
                connection?.Close();
            }
        }
 public static PreviewResponse Create(IPreviewResponse preview)
 {
     try
     {
         var result = new PreviewResponse()
         {
             MaxCount = preview.MaxCount,
         };
         result.qPreview.AddRange(preview.qPreview);
         return(result);
     }
     catch (Exception ex)
     {
         logger.Error(ex, "The preview result could not create.");
         return(new PreviewResponse());
     }
 }
        public TableHelper GetTableInfosFromApp(string tableName, ScriptCode script, ConnectorParameter parameter, QlikApp qlikApp = null)
        {
            try
            {
                var resultTable = new QvxTable()
                {
                    TableName = tableName,
                };

                HyperCubePager    pager       = null;
                IEnumerable <int> columnOrder = null;
                var fields = new List <QvxField>();
                var rows   = new List <QvxDataRow>();
                var size   = new Size();

                if (qlikApp == null)
                {
                    qlikApp = AppInstance.GetQlikInstance(parameter, script.AppId);
                }

                if (qlikApp == null)
                {
                    return(null);
                }

                var masterObject = qlikApp.FirstSession.CurrentApp.GetMasterObjectAsync(script.ObjectId).Result;
                if (masterObject != null)
                {
                    logger.Debug($"find master object: {script.ObjectId}");
                    var genericObject = qlikApp.FirstSession.CurrentApp.CreateGenericSessionObjectAsync(masterObject.Properties).Result;
                    pager = genericObject.GetAllHyperCubePagers().FirstOrDefault() ?? null;
                    var tableLayout = genericObject.GetLayout().As <TableLayout>();
                    var hyperCube   = tableLayout.HyperCube;
                    columnOrder = hyperCube.ColumnOrder;
                    size        = hyperCube.Size;
                    fields.AddRange(GetHyperCubeFields(hyperCube.DimensionInfo, hyperCube.MeasureInfo, script));
                }
                var table = qlikApp.FirstSession.CurrentApp.GetObjectAsync <Table>(script.ObjectId).Result;
                if (table != null)
                {
                    logger.Debug($"table object: {script.ObjectId}");
                    pager       = table.HyperCubePager;
                    columnOrder = table.ColumnOrder;
                    size        = table.Size;
                    fields.AddRange(GetHyperCubeFields(table.DimensionInfo, table.MeasureInfo, script));
                }

                var preview = new PreviewResponse()
                {
                    MaxCount = 15,
                };

                if (script != null)
                {
                    var initalPage = new NxPage {
                        Top = 0, Left = 0, Width = size.cx, Height = preview.MaxCount
                    };
                    var allPages = new List <IEnumerable <NxDataPage> >();
                    allPages.Add(pager.GetData(new List <NxPage>()
                    {
                        initalPage
                    }));
                    if (script.Full)
                    {
                        var pageHeight = Math.Min(size.cy * size.cx, 5000) / size.cx;
                        logger.Debug($"read data - column count: {size.cx}");
                        initalPage = new NxPage {
                            Top = 0, Left = 0, Width = size.cx, Height = pageHeight
                        };
                        allPages         = pager.IteratePages(new[] { initalPage }, Pager.Next).ToList();
                        preview.MaxCount = 0;
                    }
                    if (allPages == null)
                    {
                        throw new Exception($"no dimension in table {script.ObjectId} exits.");
                    }
                    logger.Debug($"read pages - count {allPages.Count}");
                    foreach (var page in allPages)
                    {
                        var allMatrix = page?.SelectMany(p => p.Matrix);
                        foreach (var matrix in allMatrix)
                        {
                            var row  = new QvxDataRow();
                            var hrow = new PreviewRow();
                            var drow = new PreviewRow();
                            foreach (var order in columnOrder)
                            {
                                if (order < fields.Count)
                                {
                                    var field = fields[order];
                                    row[field] = matrix[order].Text;
                                    if (!preview.qPreview.Any(s => s.qValues.Contains(field.FieldName)))
                                    {
                                        hrow.qValues.Add(field.FieldName);
                                    }
                                    if (preview.qPreview.Count <= preview.MaxCount)
                                    {
                                        drow.qValues.Add(matrix[order].Text);
                                    }
                                }
                            }
                            rows.Add(row);
                            if (hrow.qValues.Count > 0)
                            {
                                preview.qPreview.Add(hrow);
                            }
                            if (drow.qValues.Count > 0)
                            {
                                preview.qPreview.Add(drow);
                            }
                        }
                    }
                }

                resultTable.Fields  = fields.ToArray();
                resultTable.GetRows = () => { return(rows); };
                logger.Debug($"return table {resultTable.TableName}");
                return(new TableHelper(resultTable, preview));
            }
            catch (Exception ex)
            {
                logger.Error(ex, "can´t read table infos.");
                return(null);
            }
        }
 public TableHelper(QvxTable table, PreviewResponse preview)
 {
     QvxTable = table;
     Preview  = preview;
 }