Source

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

using System.ComponentModel;
using System.Windows.Input;

namespace DataUp.Excel.Addin
{
    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class RepositoryDetailsViewModel : ViewModelBase, IDataErrorInfo
    {
        private string repositoryName;
        private string repositoryType;
        private string userName;
        private string userPassword;
        private bool isPasswordRequired;
        private bool isAgreementAccepted;

        private bool showUserAgreement;
        private string userAgreement;

        private ICommand getUserAgreementCommand;

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

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

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

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

        public bool IsPasswordRequired
        {
            get
            {
                return this.isPasswordRequired;
            }
            set
            {
                this.isPasswordRequired = value;
                OnNotifyPropertyChanged("IsPasswordRequired");
            }
        }

        public bool IsAgreementAccepted
        {
            get
            {
                return this.isAgreementAccepted;
            }
            set
            {
                this.isAgreementAccepted = value;
                OnNotifyPropertyChanged("IsAgreementAccepted");
            }
        }

        public bool ShowUserAgreement
        {
            get
            {
                return showUserAgreement;
            }
            set
            {
                showUserAgreement = value;
                OnNotifyPropertyChanged("ShowUserAgreement");
            }
        }

        public string UserAgreement
        {
            get
            {
                return userAgreement;
            }
            set
            {
                userAgreement = value;
                OnNotifyPropertyChanged("UserAgreement");
            }
        }

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

        public RepositoryDetailsViewModel()
        {
            InitCommands();
        }

        private void InitCommands()
        {
            this.GetUserAgreementCommand = new DelegateCommand(GetUserAgreement);

        }

        private void GetUserAgreement()
        {
            if (!string.IsNullOrWhiteSpace(this.RepositoryName))
            {
                this.UserAgreement = Controller.Instance.GetUserAgreement(this.RepositoryName);
                this.ShowUserAgreement = true;
            }
        }

        public void LoadFromCache()
        {
            var cacheRepository = Controller.CacheRepositoryDetails.Find(c => string.Compare(c.RepositoryName, this.RepositoryName, true) == 0);

            if (cacheRepository != null)
            {
                this.RepositoryType = cacheRepository.RepositoryType;
                this.UserName = cacheRepository.UserName;
                this.UserPassword = cacheRepository.UserPassword;
                this.IsAgreementAccepted = cacheRepository.IsAgreementAccepted;
            }
        }

        public void Clear()
        {
            this.RepositoryType = string.Empty;
            this.UserName = string.Empty;
            this.UserPassword = string.Empty;
            this.IsAgreementAccepted = false;
            this.IsPasswordRequired = false;
            this.ShowUserAgreement = false;
        }

        public void UpdateCache()
        {
            var cacheRepository = Controller.CacheRepositoryDetails.Find(c => string.Compare(c.RepositoryName, this.RepositoryName, true) == 0);
            if (cacheRepository == null)
            {
                cacheRepository = new BizLogic.RepositoryModel();
                Controller.CacheRepositoryDetails.Add(cacheRepository);
            }

            cacheRepository.RepositoryName = this.RepositoryName;
            cacheRepository.RepositoryType = this.repositoryType;
            cacheRepository.UserName = this.UserName;
            cacheRepository.UserPassword = this.UserPassword;
            cacheRepository.IsAgreementAccepted = this.IsAgreementAccepted;
        }

        public bool IsValid()
        {
            bool isValid = false;

            if (this.IsPasswordRequired)
            {
                isValid = this.IsAgreementAccepted && !string.IsNullOrWhiteSpace(this.RepositoryName) && !string.IsNullOrWhiteSpace(this.UserName) && !string.IsNullOrWhiteSpace(this.UserPassword);
            }
            else
            {
                isValid = this.IsAgreementAccepted && !string.IsNullOrWhiteSpace(this.RepositoryName);
            }

            return isValid;
        }

        #region IDataErrorInfo Members

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

        public string this[string columnName]
        {
            get
            {
                string result = null;
                switch (columnName)
                {
                    case "RepositoryName":
                        if (string.IsNullOrWhiteSpace(this.RepositoryName))
                        {
                            result = "Repository Name is required.";
                        }
                        break;
                    case "UserName":
                        if (this.IsPasswordRequired && string.IsNullOrWhiteSpace(this.UserName))
                        {
                            result = "User name is required.";
                        }
                        break;
                    case "UserPassword":
                        if (this.IsPasswordRequired && string.IsNullOrWhiteSpace(this.UserPassword))
                        {
                            result = "User password is required.";
                        }
                        break;
                    default:
                        break;
                }

                return result;
            }
        }
        #endregion
    }
}