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

using System;
using System.ComponentModel;
using System.Globalization;
using System.Text.RegularExpressions;
using DataUp.Excel.BizLogic;

namespace DataUp.Excel.Addin
{
    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class MetadataViewModel : ViewModelBase, IDataErrorInfo
    {
        private string name;
        private string type;
        private bool isRequired;
        private string description;
        private string value;

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

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

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

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

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

        public FieldType FieldType { get; set; }

        #region IDataErrorInfo Members

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

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

        public string Validate()
        {
            string result = null;

            if (this.IsRequired)
            {
                if (string.IsNullOrWhiteSpace(this.Value))
                {
                    return string.Format("{0} is required", this.Name);
                }
            }

            if (!string.IsNullOrWhiteSpace(this.Value))
            {
                switch (this.FieldType)
                {
                    case FieldType.DateTime:
                        DateTime intermediate;
                        if (!DateTime.TryParseExact(this.Value, Common.Constants.DefaultDateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out intermediate))
                        {
                            result = string.Format("{0} should be of format '{1}' .", this.Name, Common.Constants.DefaultDateFormat.ToLower());
                        }
                        break;
                    case FieldType.Email:
                        Regex emailRegex = new Regex(Common.Constants.ValidateEmail);
                        if (!emailRegex.IsMatch(this.Value))
                        {
                            result = string.Format("{0} should be a valid email address.", this.Name);
                        }
                        break;
                    case FieldType.Phone:
                        Regex phoneRegex = new Regex(Common.Constants.ValidatePhone);
                        if (!phoneRegex.IsMatch(this.Value))
                        {
                            result = string.Format("{0} should be a valid phone number.", this.Name);
                        }
                        break;
                    case FieldType.String:
                    default:
                        break;
                }
            }

            return result;
        }

        #endregion
    }
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.