Source

main / Source / WebClient / Web.Client / Extensions / DocumentDetailViewModelExtension.cs

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Web;
using DataUp.Web.Client.ViewModel;
using DataUp.Web.BizLogic;
using DataUp.Web.Client.Model;
using System.Web.Mvc;

namespace DataUp.Web.Client
{
    /// <summary>
    /// Class that represents the Extension for the DocumentDetail ViewModel.
    /// </summary>
    public static class DocumentDetailViewModelExtension
    {
        public static void SetValuesFrom(this DocumentDetailViewModel viewmodel, DocumentDetail document)
        {
            if (viewmodel != null && document != null)
            {
                viewmodel.DocumentName = document.FileName;
                viewmodel.DocumentId = document.Id;
            }
        }

        public static void UpdateErrors(this DocumentDetailViewModel viewmodel, IEnumerable<ErrorDetails> errors)
        {
            viewmodel.SheetList = new List<DocumentSheet>();
            List<DocumentSheet> docSheet = new List<DocumentSheet>();
            List<DocumentError> docError = new List<DocumentError>();

            foreach (var error in errors)
            {
                if (!error.SheetNameOnly)
                {
                    docError.Add(new DocumentError()
                    {
                        Error = error.Error,
                        Title = error.ControlName,
                        Description = error.Description,
                        Recommendation = error.Recommendation,
                        ErrorAddress = error.ErrorAddress,
                        SheetName = error.SheetName,
                        SheetId = Convert.ToInt32(error.SheetId),
                        CanFix = error.CanFix
                    });
                }
            }

            foreach (var item in errors.Where(err => err.SheetNameOnly == true)
                                        .Select(q => new{SheetName = q.SheetName, SheetId = q.SheetId }))
            {
                var sheetErrors = from error in docError
                                  where string.Compare(error.SheetName, item.SheetName, true) == 0
                                  select error;

                docSheet.Add(new DocumentSheet()
                {
                    Name = item.SheetName.Trim(),
                    SheetId = item.SheetId,
                    Errors = sheetErrors.OrderByDescending(a => a.CanFix).ToList(),
                });
            }
            viewmodel.EntityNameList = new SelectList(docSheet, "Name", "Name");
            viewmodel.SheetList = docSheet;
        }

        public static void UpdateMetadata(this DocumentDetailViewModel viewmodel, IEnumerable<MetadataDetail> metadataList)
        {
            if (viewmodel != null && metadataList.Count() > 0)
            {
                viewmodel.MetadataList = new List<DocumentMetadata>();

                foreach (var item in metadataList)
                {
                    DocumentMetadata metadata = new DocumentMetadata();

                    metadata.Label = item.Name;
                    metadata.Value = item.Value;
                    metadata.Description = item.Description;
                    metadata.IsCompulsory = item.IsRequired;
                    metadata.Validationtype = item.Type;

                    viewmodel.MetadataList.Add(metadata);
                }
            }
        }

        public static void UpdateParameterMetadata(this DocumentDetailViewModel viewmodel, IEnumerable<ParameterMetadataDetail> parameterMetadataList)
        {
            viewmodel.ParameterMetadataList = new List<ParameterMetadataViewModel>();
            if (viewmodel != null && parameterMetadataList.Count() > 0)
            {
                foreach (var item in parameterMetadataList)
                {
                    item.AttributeList.ForEach(q =>
                        viewmodel.ParameterMetadataList.Add(new ParameterMetadataViewModel
                        {
                            EntityName = item.EntityName,
                            SelectedEntityNameId = item.EntityName,
                            Description = q.Description,
                            Name = q.Name,
                            EntityDescription = item.EntityDescription,
                            Type = q.Type,
                            Units = q.Units
                        })
                   );
                }
            }
            else
            {
                // If the parameter metadata list is 0, then we need to add blank row.
                viewmodel.ParameterMetadataList.Add(new ParameterMetadataViewModel() { IsBlankRow = true });
            }
        }

        public static void UpdateCitation(this DocumentDetailViewModel viewmodel, DocumentDetail document, IEnumerable<MetadataDetail> metadataList)
        {
            if (viewmodel != null && document != null)
            {
                viewmodel.Citation = new DocumentCitation();
                viewmodel.Citation.Identifier = document.Identifier;
                viewmodel.Citation.Citation = document.Citation;
                //viewmodel.Citation.Version = document.v

                viewmodel.Citation.Title = document.Title;

                var publisher = metadataList.Where(item => string.Compare(item.Name, "Creator: First name", true) == 0).FirstOrDefault();
                if (publisher != null)
                {
                    viewmodel.Citation.Publisher = publisher.Value;
                }
            }
        }

        public static MetadataDetailList GetMetadataList(this DocumentDetailViewModel viewmodel)
        {
            MetadataDetailList metadataDetailList = new MetadataDetailList();

            if (viewmodel != null && viewmodel.MetadataList.Count() > 0)
            {
                metadataDetailList.Metadata = new Collection<MetadataDetail>();

                foreach (var item in viewmodel.MetadataList)
                {
                    MetadataDetail metadata = new MetadataDetail();

                    metadata.Name = item.Label;
                    metadata.Value = item.Value;
                    metadata.Description = item.Description;

                    metadataDetailList.Metadata.Add(metadata);
                }
                var identifierNode = metadataDetailList.Metadata.Where(m => m.Name.EndsWith(Common.Constants.IdentifierKeyName, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
                if (identifierNode != null)
                {
                    identifierNode.Value = viewmodel.Citation.Identifier;
                }

                var citationNode = metadataDetailList.Metadata.Where(m => m.Name.EndsWith(Common.Constants.CitationContent, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
                if (citationNode != null)
                {
                    citationNode.Value = viewmodel.Citation.Citation;
                }
            }

            if (viewmodel != null && viewmodel.ParameterMetadataList != null && viewmodel.ParameterMetadataList.Count() > 0)
            {
                metadataDetailList.ParamterMetadata = new Collection<ParameterMetadataDetail>();
                var paramData = from val in viewmodel.ParameterMetadataList
                                group val by val.EntityName into param
                                select new
                                {
                                    EntityName = param.Key,
                                    Description = param.Select(q => (String.IsNullOrEmpty(q.EntityDescription)? "": q.EntityDescription)).FirstOrDefault(q => !String.IsNullOrEmpty(q)),
                                    Values = param.ToList()
                                };

                foreach (var data in paramData)
                {
                    metadataDetailList.ParamterMetadata.Add(new ParameterMetadataDetail
                    {
                        EntityName = data.EntityName,
                        EntityDescription = data.Description,
                        AttributeList = data.Values.Select(q => new ParameterAttribute
                                                { Name = q.Name, Description = q.Description, Type = q.Type, Units = q.Units }).ToList()
       
                    });
                }
            }

            return metadataDetailList;
        }
    }
}