WebAppActionElement.cs 3.76 KB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

using Vrh.XmlProcessing;


namespace Vrh.iScheduler
{
    /// <summary>
    /// WebAppAction elérési mód esetén 
    /// itt találhatóak a szükséges adatok.
    /// </summary>
    public class WebAppActionElement : XmlLinqBase
    {
        #region Built'in classes

        #region ElementNames class
        /// <summary>
        /// Az XML fájlban ilyen elem nevek találhatóak egy "WebAppAction" elemben.
        /// </summary>
        public new class ElementNames : XmlLinqBase.ElementNames
        {
            /// <summary>
            /// 'LoginCredentials' elem név.
            /// </summary>
            public const string LOGINCREDENTIALS = "LoginCredentials";

            /// <summary>
            /// 'LoginUrl' elem név.
            /// </summary>
            public const string LOGINURL = "LoginUrl";

            /// <summary>
            /// 'ResponseTimeout' elem név.
            /// </summary>
            public const string RESPONSETIMEOUT = "ResponseTimeout";

        }
        #endregion ElementNames class

        #region AttributeNames static class
        /// <summary>
        /// Az XML fájlban ilyen attribútum nevek találhatóak egy 'Plugin' elemben.
        /// </summary>
        public static class AttributeNames
        {
            /// <summary>
            /// 'Password' attribútum név.
            /// </summary>
            public const string PASSWORD = "Password";

            /// <summary>
            /// 'UserName' attribútum név.
            /// </summary>
            public const string USERNAME = "UserName";
        }
        #endregion AttributeNames static class

        #endregion Built'in classes

        #region Properties

        /// <summary>
        /// A bejelentkezéshez szükséges akció. Az az URL ahol a login form található.
        /// </summary>
        public UrlElement LoginUrl { get; set; }

        /// <summary>
        /// A felhasználó neve.
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        /// A felhasználó jelszava.
        /// </summary>
        public string Password { get; set; }

        /// <summary>
        /// Az akcióhívásokhoz beállítandó timout érték.
        /// </summary>
        public int ResponseTimeout { get; set; }

        #endregion Properties

        #region Constructors
        /// <summary>
        /// Példányosítás egy 'WebAppAction' XElement alapján.
        /// </summary>
        /// <param name="xelement">A 'WebAppAction' elem XElement objektuma.</param>
        public WebAppActionElement(XElement xelement)
        {
            if (xelement != null)
            {
                XElement urlXE = xelement.Element(ElementNames.LOGINURL);
                if (urlXE == null)
                {
                    throw new ApplicationException(string.Format(XmlLinqBase.Messages.ERR_MISSINGELEMENT,ElementNames.LOGINURL));
                }
                this.LoginUrl = new UrlElement(urlXE);
                XElement credXE = xelement.Element(ElementNames.LOGINCREDENTIALS);
                if (urlXE == null)
                {
                    throw new ApplicationException(string.Format(XmlLinqBase.Messages.ERR_MISSINGELEMENT, ElementNames.LOGINURL));
                }
                this.UserName = GetValue(AttributeNames.USERNAME, credXE, "", true, true);
                this.Password = GetValue(AttributeNames.PASSWORD, credXE, "");

                int timeout = GetValue(xelement.Element(ElementNames.RESPONSETIMEOUT), 60);
                this.ResponseTimeout = Math.Min(3600, Math.Max(10, timeout)) * 1000;
            }
        }
        #endregion Constructors
    }
}