Source

main-vinayFork / Source / Excel / Excel.BizLogic / Managers / MetadataManager.cs

Full commit
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using DataUp.Excel.Common;
using Microsoft.Office.Interop.Excel;
using Tools = Microsoft.Office.Tools.Excel;

namespace DataUp.Excel.BizLogic
{
    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class MetadataManager : IMetadataManager
    {
        private Tools.ApplicationFactory Factory { get; set; }

        /// <summary>
        /// Gets or sets the workbook instance.
        /// </summary>
        public _Workbook Workbook { get; set; }

        /// <summary>
        /// Creates an instance of MetadataManager class.
        /// </summary>
        /// <param name="workbook">Workbook instance.</param>
        /// <param name="factory">ApplicationFactory instance.</param>
        public MetadataManager(_Workbook workbook, Tools.ApplicationFactory factory)
        {
            this.Workbook = workbook;
            this.Factory = factory;
        }

        /// <summary>
        /// This function can be used to get the details of the parameter metadata.
        /// </summary>
        /// <returns>List of parameter metadata Detail.</returns>
        public List<object> GetHeaderRow()
        {
            List<object> obj = new List<object>();
            _Worksheet sheet = this.Workbook.ActiveSheet;
            if (sheet != null)
            {
                Range range = sheet.Cells.Application.Selection as Range;

                // Range range = sheet.UsedRange;
                obj = range.GetHeaderRow();
            }

            return obj;
        }

        public string ActiveSheetName(Range range)
        {
            return range.Worksheet.Name;
            
        }

        /// <summary>
        /// This function can be used to get the details of the parameter metadata.
        /// </summary>
        /// <param name="range">Range object</param>
        /// <returns>List of parameter metadata Detail.</returns>
        public List<object> GetHeaderRow(Range range)
        {
            List<object> obj = new List<object>();
            if (range != null)
            {
                // Range range = sheet.UsedRange;
                obj = range.GetHeaderRow();
            }
            return obj;
        }

        /// <summary>
        /// Updates the parameter metadata.
        /// </summary>
        /// <param name="metadata">parameter metadata Detail List.</param>
        public void UpdateParaMetadata(List<object> parametadata, string sheetName=null)
        {
            List<object> list = new List<object>();
            _Worksheet metadataSheet = this.Workbook.XLGetMetadataWorksheet();

            if (metadataSheet == null)
            {
                metadataSheet = this.Workbook.XLCreateMetadataWorksheet();
            }

            List<ParameterDetail> paramterMetadata = GetParamterMetadata();

            foreach (var item in parametadata)
            {
                if (item != null)
                {
                    var existingParameter = paramterMetadata.Find(p => string.Compare(item.ToString().Trim(), p.Name.Trim(), true) == 0);
                    if (existingParameter == null)
                    {
                        paramterMetadata.Add(new ParameterDetail()
                            {
                                EntityName = sheetName,
                                Name = item.ToString().Trim()
                            });
                    }
                }
            }

            metadataSheet.UpdateparaMetadata(paramterMetadata.Where(p => !string.IsNullOrWhiteSpace(p.Name)).ToList(), this.Factory);

            metadataSheet.ActivateSheet(this.Workbook);
        }

        /// <summary>
        /// Get the list of parameter metadata
        /// </summary>
        /// <returns>list of parameter metadata</returns>
        public List<ParameterDetail> GetParamterMetadata()
        {
            List<ParameterDetail> paramterMetadata = new List<ParameterDetail>();
            _Worksheet metadataSheet = this.Workbook.XLGetMetadataWorksheet();

            if (metadataSheet != null)
            {
                ListObject obj = metadataSheet.GetParaMetadataTable();
                if (obj != null)
                {
                    for (int rCnt = 2; rCnt <= obj.Range.Rows.Count; rCnt++)
                    {
                        paramterMetadata.Add(new ParameterDetail()
                        {
                            EntityName = obj.Range.Cells[rCnt, 1].Value != null ? obj.Range.Cells[rCnt, 1].Value.ToString().Trim() : string.Empty,
                            EntityDescription = obj.Range.Cells[rCnt, 2].Value != null ? obj.Range.Cells[rCnt, 2].Value.ToString() : string.Empty,
                            Name = obj.Range.Cells[rCnt, 3].Value != null ? obj.Range.Cells[rCnt, 3].Value.ToString().Trim() : string.Empty,
                            Description = obj.Range.Cells[rCnt, 4].Value != null ? obj.Range.Cells[rCnt, 4].Value.ToString() : string.Empty,
                            Type = obj.Range.Cells[rCnt, 5].Value != null ? obj.Range.Cells[rCnt, 5].Value.ToString() : string.Empty,
                            Units = obj.Range.Cells[rCnt, 6].Value != null ? obj.Range.Cells[rCnt, 6].Value.ToString() : string.Empty,
                        });
                    }
                }
            }

            return paramterMetadata;
        }

        /// <summary>
        /// This function can be used to get the details of the metadata.
        /// </summary>
        /// <returns>List of Metadata Detail.</returns>
        public List<MetadataDetail> GetMetadata()
        {
            List<MetadataDetail> metadata = new List<MetadataDetail>();

            metadata = ReadDefaultMetadata();

            UpdateExistingMetadata(metadata);

            return metadata;
        }

        /// <summary>
        /// Updates the metadata.
        /// </summary>
        /// <param name="metadata">Metadata Detail List.</param>
        public void UpdateMetadata(List<MetadataDetail> metadata, List<ParameterDetail> parameterList)
        {
            _Worksheet metadataSheet = this.Workbook.XLGetMetadataWorksheet();

            if (metadataSheet == null)
            {
                metadataSheet = this.Workbook.XLCreateMetadataWorksheet();
            }

            metadataSheet.UpdateMetadata(metadata, this.Factory);

            metadataSheet.UpdateparaMetadata(parameterList.Where(p => !string.IsNullOrWhiteSpace(p.Name)).ToList(), this.Factory);

            metadataSheet.ActivateSheet(this.Workbook);
        }

        /// <summary>
        /// Clears the parameter metadata.
        /// </summary>
        public void ClearAllParameter()
        {
            _Worksheet metadataSheet = this.Workbook.XLGetMetadataWorksheet();
            if (metadataSheet != null)
            {
                metadataSheet.ClearParameter(this.Factory);
            }
        }

        /// <summary>
        /// Check if the metadata sheet is available or not.
        /// </summary>
        /// <returns>True if metadata sheet is available;otherwise false.</returns>
        public bool IsMetadataSheetAvailable()
        {
            var worksheet = this.Workbook.XLGetMetadataWorksheet();

            return worksheet != null;
        }

        /// <summary>
        /// Check if the metadata table is available or not.
        /// </summary>
        /// <returns>True if metadata table is available;otherwise false.</returns>
        public bool IsGeneralMetadataPresent()
        {
            bool isPresent = false;

            _Worksheet metadataSheet = this.Workbook.XLGetMetadataWorksheet();

            if (metadataSheet != null)
            {
                var metadataList = metadataSheet.GetMetadataTable();

                isPresent = metadataList != null;
            }

            return isPresent;
        }

        private List<MetadataDetail> ReadDefaultMetadata()
        {
            List<MetadataDetail> metadataDetailList = new List<MetadataDetail>();
            string metadataXmlPath = Common.Constants.MetadataXmlPath;
            string metadataValue = string.Empty;

            if (File.Exists(metadataXmlPath))
            {
                metadataValue = File.ReadAllText(metadataXmlPath);
            }
            else
            {
                string filepath = Helper.GetAppInstallationLocation();

                metadataValue = File.ReadAllText(Path.Combine(filepath, "NewScientificMetadata.xml"));
            }

            if (!string.IsNullOrWhiteSpace(metadataValue))
            {
                var metadata = metadataValue.DeserializeXML<metadatalist>();

                if (metadata != null)
                {
                    foreach (var group in metadata.metadatagroup)
                    {
                        foreach (var item in group.metadata)
                        {
                            metadataDetailList.Add(new MetadataDetail()
                            {
                                Name = item.field.Trim(),
                                Description = item.description.Trim(),
                                Type = !string.IsNullOrWhiteSpace(item.type) ? item.type.Trim() : string.Empty,
                                IsRequired = item.isrequired,
                                FieldType = !string.IsNullOrWhiteSpace(item.type) ? item.type.Trim().ToEnum<string, FieldType>(FieldType.String) : FieldType.String
                            });
                        }
                    }
                }
            }
            return metadataDetailList;
        }

        private void UpdateExistingMetadata(List<MetadataDetail> newMetadata)
        {
            _Worksheet metadataSheet = this.Workbook.XLGetMetadataWorksheet();

            if (metadataSheet != null)
            {
                var exisitingData = metadataSheet.GetExistingMetadata(this.Factory);

                foreach (var item in newMetadata)
                {
                    if (exisitingData.ContainsKey(item.Name))
                    {
                        switch (item.FieldType)
                        {
                            case FieldType.DateTime:
                                DateTime date;
                                item.Value = DateTime.TryParse(exisitingData[item.Name], out date) ? date.ToString(Common.Constants.DefaultDateFormat) : string.Empty;
                                break;
                            default:
                                item.Value = exisitingData[item.Name];
                                break;
                        }
                    }
                }
            }
        }
    }
}