Esempio n. 1
0
        public static object GetValue(string columnName, object value, List <KeyValuePair <string, Type> > columnTypes)
        {
            if (value != null)
            {
                if (columnTypes.All(c => c.Key != columnName))
                {
                    columnTypes.Add(new KeyValuePair <string, Type>(columnName, value.GetType()));
                }

                if (value is RFDate?)
                {
                    return((value as RFDate?).Value.ToString("yyyy-MMM-dd"));
                }
                else if (value is RFDate)
                {
                    return(((RFDate)value).ToString("yyyy-MMM-dd"));
                }
                else if (value is RFEnum)
                {
                    return(value.ToString());
                }
                else if (value is Enum)
                {
                    return(value.ToString());
                }
                else if (RFReflectionHelpers.IsNumber(value.GetType()))
                {
                    return(value);
                }

                return(value.ToString());
            }
            return(String.Empty);
        }
 public static List <RFCatalogKeyData> RefreshCache(IRFProcessingContext context, string username)
 {
     using (var dataEditor = new RFDataEditorActivity(context, username))
     {
         var cache = dataEditor.GetDocuments(null, null, null, 0, null, true).Select(d => new RFCatalogKeyData
         {
             Key             = JsonConvert.SerializeObject(d.Key),
             FriendlyString  = d.Key.FriendlyString(),
             KeyReference    = d.KeyReference,
             KeyType         = RFReflectionHelpers.TrimType(d.KeyType),
             KeyTypeFull     = d.KeyType,
             Plane           = d.Key.Plane.ToString(),
             ContentType     = RFReflectionHelpers.TrimType(d.ContentType),
             ContentTypeFull = d.ContentType,
             DataSize        = d.DataSize,
             Metadata        = JsonConvert.SerializeObject(d.Metadata),
             IsValid         = d.IsValid,
             UpdateTime      = d.UpdateTime,
             Instance        = d.Key.GraphInstance != null ? d.Key.GraphInstance.Name : null,
             ValueDate       = (d.Key.GraphInstance != null && d.Key.GraphInstance.ValueDate.HasValue && d.Key.GraphInstance.ValueDate.Value != RFDate.NullDate) ?
                               d.Key.GraphInstance.ValueDate.Value.ToJavascript() : null
         }).ToList();
         lock (_sync)
         {
             _cache = cache;
         }
         return(cache);
     }
 }
 public List <RFCatalogKeyMetadata> GetDocuments(string type, DateTime?startTime, DateTime?endTime, int limitResults, RFDate?valueDate, bool latestOnly)
 {
     if (string.IsNullOrWhiteSpace(type))
     {
         type = typeof(RFCatalogKey).FullName;
     }
     return(Context.SearchKeys(RFReflectionHelpers.GetTypeByFullName(type), startTime, endTime, limitResults, valueDate, latestOnly));
 }
        public RFDocument GetDocumentForDownload(string type, long keyReference)
        {
            var allKeys = Context.GetKeysByType(RFReflectionHelpers.GetTypeByFullName(type));

            if (allKeys.ContainsKey(keyReference))
            {
                return(Context.LoadEntry(allKeys[keyReference]) as RFDocument);
            }
            return(null);
        }
Esempio n. 5
0
        public static byte[] InjectDatasource(byte[] template, string sheetName, object datasource)
        {
            var ms = new MemoryStream();

            ms.Write(template, 0, template.Length);
            ms.Seek(0, SeekOrigin.Begin);

            using (var document = SpreadsheetDocument.Open(ms, true))
            {
                var worksheetPart = OpenXML.OpenXMLHelpers.GetWorksheetPartByName(document, sheetName);

                OpenXML.OpenXMLHelpers.ClearSheet(worksheetPart.Worksheet);

                uint rowNo = 1;
                foreach (var propertyInfo in datasource.GetType().GetProperties().OrderBy(p => p.Name))
                {
                    if (RFReflectionHelpers.IsDictionary(propertyInfo))
                    {
                        InjectDictionary(datasource, propertyInfo, worksheetPart, ref rowNo);
                    }
                    else
                    {
                        var value = propertyInfo.GetValue(datasource);
                        OpenXML.OpenXMLHelpers.SetCell(worksheetPart, propertyInfo.Name, "A", rowNo);
                        if (value != null)
                        {
                            OpenXML.OpenXMLHelpers.SetCell(worksheetPart, value.ToString(), "B", rowNo);
                        }
                        else
                        {
                            OpenXML.OpenXMLHelpers.SetCellNA(worksheetPart, "B", rowNo);
                        }
                        rowNo++;
                    }
                }

                // recalculate dimension
                try
                {
                    worksheetPart.Worksheet.SheetDimension.Reference = new StringValue("A1:B" + (rowNo + 1));
                }
                catch (Exception ex)
                {
                    RFStatic.Log.Error(typeof(DatasourceInjector), "Error generating XLSX: {0}", ex.Message);
                }

                worksheetPart.Worksheet.Save();
                OpenXML.OpenXMLHelpers.ForceCalculation(document);
                document.WorkbookPart.Workbook.Save();

                return(ms.ToArray());
            }
        }
        public bool UpdateDocument(string type, long keyReference, string data)
        {
            var allKeys = Context.GetKeysByType(RFReflectionHelpers.GetTypeByFullName(type));

            if (allKeys.ContainsKey(keyReference))
            {
                var existingDocument = Context.LoadEntry(allKeys[keyReference]) as RFDocument;
                existingDocument.Content = RFXMLSerializer.DeserializeContract(existingDocument.Type, data);
                Context.SaveDocument(existingDocument.Key, existingDocument.Content, false, CreateUserLogEntry("Update Document", String.Format("Saved document {0}", existingDocument.Key.FriendlyString()), null));
            }
            return(true);
        }
Esempio n. 7
0
        public static List <Dictionary <string, object> > GenerateRows(IRFDataSet dataSet)
        {
            var result  = new List <Dictionary <string, object> >();
            var rowType = dataSet.GetRowType();

            foreach (var r in dataSet.GetRows())
            {
                var row = new Dictionary <string, object>();
                foreach (var propertyInfo in rowType.GetProperties())
                {
                    if (RFReflectionHelpers.IsStruct(propertyInfo) || RFReflectionHelpers.IsMappingKey(propertyInfo))
                    {
                        var valueStruct = propertyInfo.GetValue(r);
                        foreach (var innerPropertyInfo in propertyInfo.PropertyType.GetProperties())
                        {
                            if (!row.ContainsKey(innerPropertyInfo.Name))
                            {
                                row.Add(innerPropertyInfo.Name, innerPropertyInfo.GetValue(valueStruct));
                            }
                            // also add children if no conflict, so 2 levels of drill-down
                            if (RFReflectionHelpers.IsStruct(innerPropertyInfo) || RFReflectionHelpers.IsMappingKey(innerPropertyInfo))
                            {
                                var innerStruct = innerPropertyInfo.GetValue(valueStruct);
                                foreach (var inner2PropertyInfo in innerPropertyInfo.PropertyType.GetProperties())
                                {
                                    if (!row.ContainsKey(inner2PropertyInfo.Name))
                                    {
                                        row.Add(inner2PropertyInfo.Name, inner2PropertyInfo.GetValue(innerStruct));
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        if (!row.ContainsKey(propertyInfo.Name))
                        {
                            row.Add(propertyInfo.Name, propertyInfo.GetValue(r));
                        }
                    }
                }
                result.Add(row);
            }

            return(result);
        }
Esempio n. 8
0
        public static string ExportToJSON(IRFDataSet dataSet, List <KeyValuePair <string, Type> > columnTypes)
        {
            var array = new JArray();

            // header row
            var rowType = dataSet.GetRowType();

            foreach (var r in dataSet.GetRows())
            {
                var obj = new JObject();
                foreach (var propertyInfo in rowType.GetProperties())
                {
                    if (RFReflectionHelpers.IsStruct(propertyInfo) || RFReflectionHelpers.IsMappingKey(propertyInfo))
                    {
                        var valueStruct = propertyInfo.GetValue(r);
                        foreach (var innerPropertyInfo in propertyInfo.PropertyType.GetProperties())
                        {
                            if (valueStruct != null)
                            {
                                var columnName = String.Format("{0},{1}", propertyInfo.Name, innerPropertyInfo.Name);
                                obj.Add(new JProperty(
                                            columnName,
                                            GetValue(columnName, innerPropertyInfo.GetValue(valueStruct), columnTypes)));
                            }
                        }
                    }
                    else
                    {
                        var columnName = propertyInfo.Name;
                        obj.Add(new JProperty(
                                    columnName,
                                    GetValue(columnName, propertyInfo.GetValue(r), columnTypes)));
                    }
                }
                array.Add(obj);
            }

            return(array.ToString());
        }
Esempio n. 9
0
        /*private static void AddToWorkbook(XSSFWorkbook wb, string sheetName, IRFDataSet dataSet)
         * {
         *  AddToWorkbook(wb, sheetName, dataSet.GetRows());
         * }*/

        private static void AddToWorkbook(XSSFWorkbook wb, string sheetName, IEnumerable <IRFDataRow> rows)
        {
            var sheet = wb.CreateSheet(sheetName);
            var cH    = wb.GetCreationHelper();

            var cellStyles = new Dictionary <string, ICellStyle>();
            var dataFormat = wb.CreateDataFormat();
            var dateStyle  = wb.CreateCellStyle();

            dateStyle.DataFormat = dataFormat.GetFormat("yyyy-MMM-dd");
            cellStyles.Add("date", dateStyle);

            if (rows.Any())
            {
                // header row
                var rowType   = rows.First().GetType();
                var headerRow = sheet.CreateRow(0);
                int colNo     = 0;
                foreach (var propertyInfo in rowType.GetProperties())
                {
                    if (RFReflectionHelpers.IsStruct(propertyInfo) || RFReflectionHelpers.IsMappingKey(propertyInfo))
                    {
                        foreach (var innerPropertyInfo in propertyInfo.PropertyType.GetProperties())
                        {
                            var cell = headerRow.CreateCell(colNo);
                            cell.SetCellValue(String.Format("{0}.{1}", propertyInfo.Name, innerPropertyInfo.Name));
                            colNo++;
                        }
                    }
                    else
                    {
                        var cell = headerRow.CreateCell(colNo);
                        cell.SetCellValue(propertyInfo.Name);
                        colNo++;
                    }
                }

                int rowNo = 1;
                foreach (var r in rows)
                {
                    var dataRow = sheet.CreateRow(rowNo);
                    colNo = 0;
                    foreach (var propertyInfo in rowType.GetProperties())
                    {
                        if (RFReflectionHelpers.IsStruct(propertyInfo) || RFReflectionHelpers.IsMappingKey(propertyInfo))
                        {
                            var valueStruct = propertyInfo.GetValue(r);
                            foreach (var innerPropertyInfo in propertyInfo.PropertyType.GetProperties())
                            {
                                var cell = dataRow.CreateCell(colNo);
                                if (valueStruct != null)
                                {
                                    SetValue(cell, innerPropertyInfo.GetValue(valueStruct), cellStyles);
                                }
                                colNo++;
                            }
                        }
                        else
                        {
                            var cell = dataRow.CreateCell(colNo);
                            SetValue(cell, propertyInfo.GetValue(r), cellStyles);
                            colNo++;
                        }
                    }
                    rowNo++;
                }
            }
        }