Source

main / Source / Excel / Excel.AddIn / Excel.AddIn / ViewModels / CitationViewModel.cs

Full commit
Naethra D ba1cb57 

























































































































































































































































































































using System;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Windows.Input;
using DataUp.Excel.Common;

namespace DataUp.Excel.Addin
{
    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class CitationViewModel : ViewModelBase, IDataErrorInfo
    {
        private string citation;
        private string identifier;
        private string publicationYear;
        private string title;
        private string version;
        private string publisher;
        private string identifierExistsMessage;

        private bool enableGetIdentifier;

        private ICommand getIdentifierCommand;
        private ICommand generateCitationCommand;

        #region CustomEvent

        /// <summary>
        /// Capture View window close request
        /// </summary>
        public event EventHandler RequestClose;

        public event EventHandler GetIdentifierClick;

        #endregion

        public string Citation
        {
            get
            {
                return this.citation;
            }
            set
            {
                this.citation = value;
                OnNotifyPropertyChanged("Citation");
            }
        }

        public string Identifier
        {
            get
            {
                return this.identifier;
            }
            set
            {
                this.identifier = value;
                OnNotifyPropertyChanged("Identifier");
            }
        }

        public string PublicationYear
        {
            get
            {
                return this.publicationYear;
            }
            set
            {
                this.publicationYear = value;
                OnNotifyPropertyChanged("PublicationYear");
            }
        }

        public string Title
        {
            get
            {
                return this.title;
            }
            set
            {
                this.title = value;
                OnNotifyPropertyChanged("Title");
            }
        }

        public string Version
        {
            get
            {
                return this.version;
            }
            set
            {
                this.version = value;
                OnNotifyPropertyChanged("Version");
            }
        }

        public string Publisher
        {
            get
            {
                return this.publisher;
            }
            set
            {
                this.publisher = value;
                OnNotifyPropertyChanged("Publisher");
            }
        }

        public bool EnableGetIdentifier
        {
            get
            {
                return enableGetIdentifier;
            }
            set
            {
                enableGetIdentifier = value;
                OnNotifyPropertyChanged("EnableGetIdentifier");
            }
        }

        public bool IsLoaded { get; set; }

        public string IdentifierExistsMessage
        {
            get
            {
                return identifierExistsMessage;
            }
            set
            {
                identifierExistsMessage = value;
                OnNotifyPropertyChanged("IdentifierExistsMessage");
            }
        }

        public ICommand GetIdentifierCommand
        {
            get
            {
                return getIdentifierCommand;
            }
            set
            {
                if (getIdentifierCommand != value)
                {
                    getIdentifierCommand = value;
                    OnNotifyPropertyChanged("GetIdentifierCommand");
                }
            }
        }

        public ICommand GenerateCitationCommand
        {
            get
            {
                return generateCitationCommand;
            }
            set
            {
                if (generateCitationCommand != value)
                {
                    generateCitationCommand = value;
                    OnNotifyPropertyChanged("GenerateCitationCommand");
                }
            }
        }

        public string Error
        {
            get
            {
                return string.Empty;
            }
        }

        public CitationViewModel()
        {
            InitCommands();

            this.EnableGetIdentifier = true;
        }

        public string this[string columnName]
        {
            get
            {
                string result = null;
                if (columnName == "PublicationYear")
                {
                    result = ValidatePublicationYear();
                }
                return result;
            }
        }

        private string ValidatePublicationYear()
        {
            string result = null;

            DateTime intermediate;
            if (!string.IsNullOrWhiteSpace(this.PublicationYear) && !DateTime.TryParseExact(this.PublicationYear, "yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out intermediate))
            {
                result = "Publication Year should be of format 'yyyy' .";
            }

            return result;
        }

        public bool IsValid()
        {
            return string.IsNullOrWhiteSpace(ValidatePublicationYear());
        }

        private void InitCommands()
        {
            this.GenerateCitationCommand = new DelegateCommand(UpdateCitation, GenerateCitationCommandCanExecute);
            this.GetIdentifierCommand = new DelegateCommand(GetIdentifier);
        }

        /// <summary>
        /// Raise window close event
        /// </summary>
        public void OnRequestClose()
        {
            RequestClose.OnFire(this, EventArgs.Empty);
        }

        private void GetIdentifier()
        {
            GetIdentifierClick.OnFire(this, EventArgs.Empty);
        }

        private bool GenerateCitationCommandCanExecute()
        {
            return IsValid();
        }

        public void LoadValues(MetadataListViewModel metadataListViewModel)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            var firstname = GetValue("Creator: First name", metadataListViewModel);
            var lastname = GetValue("Creator: Last name", metadataListViewModel);

            this.Publisher = string.Format("{1}, {0}", firstname, lastname);

            string value = GetValue("Today's date", metadataListViewModel);
            DateTime todayDate = DateTime.UtcNow;
            if (!string.IsNullOrWhiteSpace(value))
            {
                DateTime.TryParse(value, out todayDate);
            }

            this.PublicationYear = todayDate.ToString("yyyy");

            this.Title = GetValue("Title of dataset", metadataListViewModel);

            this.Identifier = GetValue(Common.Constants.IdentifierKeyName, metadataListViewModel);
            if (!string.IsNullOrWhiteSpace(this.Identifier))
            {
                this.IdentifierExistsMessage = "Please Click on Retrieve an identifier if you need get a new identifier for this document.";
            }
            else
            {
                this.IdentifierExistsMessage = string.Empty;
            }

            this.Citation = GetValue(Common.Constants.CitationContent, metadataListViewModel);

            if (!string.IsNullOrWhiteSpace(this.Citation))
            {
                string[] values = this.Citation.Split('.');

                if (values.Length >= 4)
                {
                    var version = values[values.Length - 3];
                    if (!string.IsNullOrWhiteSpace(version))
                    {
                        this.Version = version.Trim();
                    }
                }
            }
        }

        private void UpdateCitation()
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            this.Citation = string.Format(Common.Constants.CitationFormat, this.Publisher, this.PublicationYear, this.Title, this.Version, this.Publisher, this.Identifier);
        }

        private string GetValue(string keyName, MetadataListViewModel metadataListViewModel)
        {
            string value = string.Empty;

            var authorNode = metadataListViewModel.MetadataList.Where(m => m.Name.EndsWith(keyName, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
            if (authorNode != null)
            {
                value = authorNode.Value;
            }

            return value;
        }

    }
}