Source

main-vinayFork / Source / Excel / Excel.AddIn / Excel.AddIn / ViewModels / GenerateCitationPaneViewModel.cs

Full commit
using System;
using System.Linq;
using System.Windows.Input;
using DataUp.Excel.BizLogic;
using DataUp.Excel.Common;

namespace DataUp.Excel.Addin
{
    public class GenerateCitationPaneViewModel : ViewModelBase
    {
        private bool showIdentifier;

        private MetadataListViewModel metadataListViewModel;
        private CitationViewModel citationViewModel;
        private GetIdentifierViewModel getIdentifierViewModel;

        private ICommand saveCommand;

        public bool ShowIdentifier
        {
            get
            {
                return showIdentifier;
            }
            set
            {
                showIdentifier = value;
                OnNotifyPropertyChanged("ShowIdentifier");
            }
        }

        public MetadataListViewModel MetadataListViewModel
        {
            get
            {
                return metadataListViewModel;
            }
            set
            {
                metadataListViewModel = value;
                OnNotifyPropertyChanged("MetadataListViewModel");
            }
        }

        public CitationViewModel CitationViewModel
        {
            get
            {
                return citationViewModel;
            }
            set
            {
                citationViewModel = value;
                OnNotifyPropertyChanged("CitationViewModel");
            }
        }

        public GetIdentifierViewModel GetIdentifierViewModel
        {
            get
            {
                return getIdentifierViewModel;
            }
            set
            {
                getIdentifierViewModel = value;
                OnNotifyPropertyChanged("GetIdentifierViewModel");
            }
        }

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

        #region CustomEvent

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

        #endregion

        public GenerateCitationPaneViewModel()
        {
            this.SaveCommand = new DelegateCommand(UpdateCitation, SaveCommandCanExecute);

            this.MetadataListViewModel = new MetadataListViewModel();
            this.CitationViewModel = new CitationViewModel();
            this.GetIdentifierViewModel = new GetIdentifierViewModel();

            this.CitationViewModel.GetIdentifierClick += new EventHandler(OnGetIdentifierClick);
            this.GetIdentifierViewModel.GetIdentifierCompleted += new EventHandler(OnGetIdentifierCompleted);
            this.GetIdentifierViewModel.GetIdentifierCancelled += new EventHandler(OnGetIdentifierCancelled);
        }

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

        private bool SaveCommandCanExecute()
        {
            return this.CitationViewModel.IsValid();
        }

        private void OnGetIdentifierCompleted(object sender, EventArgs e)
        {
            try
            {
                RepositoryModel repositoryModel = new RepositoryModel()
                {
                    RepositoryName = this.GetIdentifierViewModel.RepositoryDetailsViewModel.RepositoryName,
                    UserName = this.GetIdentifierViewModel.RepositoryDetailsViewModel.UserName,
                    UserPassword = this.GetIdentifierViewModel.RepositoryDetailsViewModel.UserPassword,
                    IsPasswordRequired = this.GetIdentifierViewModel.RepositoryDetailsViewModel.IsPasswordRequired,
                    IsAgreementAccepted = this.GetIdentifierViewModel.RepositoryDetailsViewModel.IsAgreementAccepted
                };

                var getIdentifierResult = Controller.Instance.GetIdentifier(this.MetadataListViewModel, repositoryModel);
                if (getIdentifierResult.IsSuccess)
                {
                    this.CitationViewModel.Identifier = getIdentifierResult.Message;
                    this.CitationViewModel.EnableGetIdentifier = false;
                    this.ShowIdentifier = false;

                    this.GetIdentifierViewModel.RepositoryDetailsViewModel.UpdateCache();
                }
                else
                {
                    this.GetIdentifierViewModel.ShowError = true;
                    this.GetIdentifierViewModel.ErrorMessage = getIdentifierResult.Message;
                }

            }
            catch (Exception ex)
            {
                // TODO : Show Error Message to user and be in get identifier window.
                this.GetIdentifierViewModel.ShowError = true;
                this.GetIdentifierViewModel.ErrorMessage = ex.Message;
            }
        }

        private void UpdateCitation()
        {
            try
            {
                var identifierNode = this.MetadataListViewModel.MetadataList.Where(m => m.Name.EndsWith(Common.Constants.IdentifierKeyName, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
                if (identifierNode != null)
                {
                    identifierNode.Value = this.CitationViewModel.Identifier;
                }

                var citationNode = this.MetadataListViewModel.MetadataList.Where(m => m.Name.EndsWith(Common.Constants.CitationContent, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
                if (citationNode != null)
                {
                    citationNode.Value = this.CitationViewModel.Citation;
                }

                // Update in the Metadata Sheet.
                Controller.Instance.UpdateMetadata(this.MetadataListViewModel);

                this.OnRequestClose();
            }
            catch
            {
                // TODO: Exception handling
            }
        }

        private void OnGetIdentifierClick(object sender, EventArgs e)
        {
            this.GetIdentifierViewModel.LoadValues();
            this.GetIdentifierViewModel.ShowError = false;

            this.ShowIdentifier = true;
        }

        private void OnGetIdentifierCancelled(object sender, EventArgs e)
        {
            this.ShowIdentifier = false;
        }
    }
}