/// <summary>
        /// Returns a list of tier queue (support group) enumerations that the specified ConsoleUser is a member of
        /// </summary>
        /// <param name="authToken">AuthorizationToken to use</param>
        /// <param name="user">ConsoleUser token</param>
        /// <returns></returns>
        internal static async Task <List <Enumeration> > GetUsersTierQueueEnumerations(AuthorizationToken authToken, ConsoleUser user)
        {
            if (!authToken.IsValid)
            {
                throw new InvalidCredentialException("AuthorizationToken is not valid.");
            }

            string endpointUrl = GET_TIER_QUEUES_ENDPOINT + "?id=" + user.Id.ToString("D");

            try
            {
                PortalHttpHelper helper = new PortalHttpHelper(authToken);
                string           result = await helper.GetAsync(endpointUrl);

                dynamic obj = JsonConvert.DeserializeObject <List <ExpandoObject> >(result, new ExpandoObjectConverter());

                List <Enumeration> returnList = new List <Enumeration>();

                foreach (var enumJson in obj)
                {
                    returnList.Add(new Enumeration(enumJson.Id, enumJson.Text, enumJson.Name, true, false));
                }

                return(returnList);
            }
            catch (Exception)
            {
                throw; // Rethrow exceptions
            }
        }
        /// <summary>
        /// Creates an object projection from the specified template, by the specified user.
        /// </summary>
        /// <typeparam name="T">Type of projection to create</typeparam>
        /// <param name="authToken">AuthorizationToken to use</param>
        /// <param name="templateId">ID of the object template to project</param>
        /// <param name="creatingUserId">ID of the user creating the object</param>
        /// <returns></returns>
        internal static async Task <T> CreateObjectFromTemplate <T>(AuthorizationToken authToken, Guid templateId, Guid creatingUserId) where T : TypeProjection
        {
            if (!authToken.IsValid)
            {
                throw new InvalidCredentialException("AuthorizationToken is not valid.");
            }

            string endpointUrl = CREATE_PROJECTION_BY_TEMPLATE_ENDPOINT + "?id=" + templateId.ToString("D") + "&createdById=" + creatingUserId.ToString("D");

            try
            {
                // Initialize the HTTP helper and get going
                PortalHttpHelper helper = new PortalHttpHelper(authToken);
                string           result = await helper.GetAsync(endpointUrl);

                // Convert the result into a TypeProjection and return it
                ExpandoObjectConverter converter = new ExpandoObjectConverter();
                dynamic jsonObject = JsonConvert.DeserializeObject <ExpandoObject>(result, converter);

                // Instantiate!
                BindingFlags flags        = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
                CultureInfo  culture      = null;
                T            instanceType = (T)Activator.CreateInstance(typeof(T), flags, null, null, culture);

                instanceType.CurrentObject  = (jsonObject as ExpandoObject).DeepCopy();
                instanceType.OriginalObject = jsonObject;
                instanceType.ReadOnly       = false;

                return(instanceType);
            }
            catch (Exception)
            {
                throw; // Rethrow exceptions
            }
        }
        /// <summary>
        /// Fetches a list of enumerations from the server
        /// </summary>
        /// <param name="authToken">Authorization token</param>
        /// <param name="enumList">Enumeration list to fetch</param>
        /// <param name="flatten">If true, flatten the entire enumeration tree into one list; if false, only return the first-level items</param>
        /// <param name="sort">If true, sorts the list before returning it</param>
        /// <param name="insertNullItem">If true, add a null item to the list as the first item</param>
        /// <returns></returns>
        /// 
        public static async Task<List<Enumeration>> GetEnumerationList(AuthorizationToken authToken, Guid enumList, bool flatten, bool sort, bool insertNullItem)
        {
            if (!authToken.IsValid)
            {
                throw new InvalidCredentialException("AuthorizationToken is not valid.");
            }

            string endpoint = (flatten ? LIST_ENDPOINT_FLAT : LIST_ENDPOINT_TREE);
            endpoint += "/" + enumList.ToString() + "/" + (flatten ? "?itemFilter=" : "");

            try
            {
                // Initialize the HTTP helper and get going
                PortalHttpHelper helper = new PortalHttpHelper(authToken);
                string result = await helper.GetAsync(endpoint);

                List<Enumeration> returnList = new List<Enumeration>();

                if (flatten)
                {
                    // A flat enumeration list has null Ordinals, so we use the base EnumJson class
                    List<EnumJson> jEnumList = JsonConvert.DeserializeObject<List<EnumJson>>(result);
                    foreach (var jEnum in jEnumList)
                    {
                        // Skip empty enumerations
                        if (jEnum.ID != Guid.Empty.ToString())
                            returnList.Add(new Enumeration(new Guid(jEnum.ID), jEnum.Text, jEnum.Name, flatten, jEnum.HasChildren));
                    }
                }
                else
                {
                    // A non-flat enumeration list has non-null Ordinals, so we have to use a different conversion class
                    List<EnumJsonOrdinal> jEnumList = JsonConvert.DeserializeObject<List<EnumJsonOrdinal>>(result);
                    foreach (var jEnum in jEnumList)
                    {
                        // Skip empty enumerations
                        if (jEnum.ID != Guid.Empty.ToString())
                            returnList.Add(new Enumeration(new Guid(jEnum.ID), jEnum.Text, jEnum.Name, flatten, jEnum.HasChildren, jEnum.Ordinal));
                    }
                }

                if (sort)
                {
                    returnList.Sort(new EnumerationComparer());
                }

                if (insertNullItem)
                {
                    returnList.Insert(0, new Enumeration(Guid.Empty, string.Empty, string.Empty, true, false));
                }

                return returnList;
            }
            catch (Exception e)
            {
                throw; // Rethrow exceptions
            }
        }
Beispiel #4
0
        /// <summary>
        /// Fetches a list of enumerations from the server
        /// </summary>
        /// <param name="authToken">Authorization token</param>
        /// <param name="enumList">Enumeration list to fetch</param>
        /// <param name="flatten">If true, flatten the entire enumeration tree into one list; if false, only return the first-level items</param>
        /// <param name="sort">If true, sorts the list before returning it</param>
        /// <param name="insertNullItem">If true, add a null item to the list as the first item</param>
        /// <returns></returns>
        ///
        public static async Task <List <Enumeration> > GetEnumerationList(AuthorizationToken authToken, Guid enumList, bool flatten, bool sort, bool insertNullItem)
        {
            if (!authToken.IsValid)
            {
                throw new InvalidCredentialException("AuthorizationToken is not valid.");
            }

            string endpoint = (flatten ? LIST_ENDPOINT_FLAT : LIST_ENDPOINT_TREE);

            endpoint += "?id=" + enumList.ToString() + (flatten ? "&itemFilter=" : "");

            try
            {
                // Initialize the HTTP helper and get going
                PortalHttpHelper helper = new PortalHttpHelper(authToken);
                string           result = await helper.GetAsync(endpoint);

                List <Enumeration> returnList = new List <Enumeration>();

                if (flatten)
                {
                    // A flat enumeration list has null Ordinals, so we use the base EnumJson class
                    List <EnumJson> jEnumList = JsonConvert.DeserializeObject <List <EnumJson> >(result);
                    foreach (var jEnum in jEnumList)
                    {
                        // Skip empty enumerations
                        if (jEnum.ID != Guid.Empty.ToString())
                        {
                            returnList.Add(new Enumeration(new Guid(jEnum.ID), jEnum.Text, jEnum.Name, flatten, jEnum.HasChildren));
                        }
                    }
                }
                else
                {
                    // A non-flat enumeration list has non-null Ordinals, so we have to use a different conversion class
                    List <EnumJsonOrdinal> jEnumList = JsonConvert.DeserializeObject <List <EnumJsonOrdinal> >(result);
                    foreach (var jEnum in jEnumList)
                    {
                        // Skip empty enumerations
                        if (jEnum.ID != Guid.Empty.ToString())
                        {
                            returnList.Add(new Enumeration(new Guid(jEnum.ID), jEnum.Text, jEnum.Name, flatten, jEnum.HasChildren, jEnum.Ordinal));
                        }
                    }
                }

                if (sort)
                {
                    returnList.Sort(new EnumerationComparer());
                }

                if (insertNullItem)
                {
                    returnList.Insert(0, new Enumeration(Guid.Empty, string.Empty, string.Empty, true, false));
                }

                return(returnList);
            }
            catch (Exception)
            {
                throw; // Rethrow exceptions
            }
        }
        /// <summary>
        /// Returns a list of tier queue (support group) enumerations that the specified ConsoleUser is a member of
        /// </summary>
        /// <param name="authToken">AuthorizationToken to use</param>
        /// <param name="user">ConsoleUser token</param>
        /// <returns></returns>
        internal static async Task<List<Enumeration>> GetUsersTierQueueEnumerations(AuthorizationToken authToken, ConsoleUser user)
        {
            if (!authToken.IsValid)
            {
                throw new InvalidCredentialException("AuthorizationToken is not valid.");
            }

            string endpointUrl = GET_TIER_QUEUES_ENDPOINT + "/" + user.Id.ToString("D");

            try
            {
                PortalHttpHelper helper = new PortalHttpHelper(authToken);
                string result = await helper.GetAsync(endpointUrl);

                dynamic obj = JsonConvert.DeserializeObject<List<ExpandoObject>>(result, new ExpandoObjectConverter());

                List<Enumeration> returnList = new List<Enumeration>();

                foreach (var enumJson in obj)
                {
                    returnList.Add(new Enumeration(enumJson.Id, enumJson.Text, enumJson.Name, true, false));
                }

                return returnList;
            }
            catch (Exception e)
            {
                throw; // Rethrow exceptions
            }
        }