• Decrease Text SizeIncrease Text Size

TaxonomyInfo Class

Posted Date: 5/3/2023
    Printer Friendly Version   Email A Friend   Add This   Increase Text Size   Decrease Text Size
this class provides you with information about the Taxonomy


using Centralpoint.Web;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;

namespace Centralpoint.WebSite
{
    public class TaxonomyInfo
    {
        private string _propertiesXml = String.Empty;
        private CpCollection _properties = null;
        private DataTable _trail = null;

        public Guid TaxonomyId { get; private set; }
        public Guid ParentTaxonomyId { get; private set; }
        public string SystemName { get; private set; }
        public string Path { get; private set; }
        public string Name { get; private set; }
        public string Description { get; private set; }
        public DateTime CreateDate { get; private set; }
        public DateTime ModifyDate { get; private set; }
        public string IntegrationId { get; private set; }
        public int SortOrder { get; private set; }
        public bool Exists { get; private set; } = false;
        public string Audiences { get; private set; }

        public TaxonomyInfo(Guid taxonomyId, bool returnException = true)
        {
            this.TaxonomyId = taxonomyId;
            using (var connection = new SqlConnection(Management.Application.SelectConnectionString))
            {
                connection.Open();
                using (var command = new SqlCommand($"SELECT TOP 1 ParentTaxonomyId, SystemName, [Path], [Name], [Description], CreateDate, ModifyDate, Properties, IntegrationId, SortOrder FROM cpsys_Taxonomy WHERE TaxonomyId = '{this.TaxonomyId}'", connection))
                {
                    using (var reader = command.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            this.Exists = true;
                            this.ParentTaxonomyId = reader.IsDBNull(0) ? Guid.Empty : reader.GetGuid(0);
                            this.SystemName = reader.GetString(1);
                            this.Path = reader.GetString(2);
                            this.Name = reader.GetString(3);
                            this.Description = reader.IsDBNull(4) ? String.Empty : reader.GetString(4);
                            this.CreateDate = reader.GetDateTime(5);
                            this.ModifyDate = reader.IsDBNull(6) ? DateTime.MinValue : reader.GetDateTime(6);
                            _propertiesXml = reader.IsDBNull(7) ? String.Empty : reader.GetString(7);
                            this.IntegrationId = reader.IsDBNull(8) ? String.Empty : reader.GetString(8);
                            this.SortOrder = reader.GetInt32(9);
                        }
                        else
                        {
                            string message = $"The {this.TaxonomyId} taxonomy could not be found.";
                            if (returnException)
                                throw new UnreportedException(message);
                            else
                            {
                                HttpContext.Current.Trace.Warn("TaxonomyInfo", $"{message}");
                            }
                        }
                    }
                    if (this.Exists)
					{
                        command.CommandText = $"SELECT AudienceId, IsCda, IsConsole FROM cpsys_TaxonomyInAudiences WHERE TaxonomyId = '{this.TaxonomyId}'";
                        using (var audienceReader = command.ExecuteReader())
						{
                            var audiences = new List<string>();
                            while (audienceReader.Read())
							{
                                var aid = audienceReader.GetGuid(0).ToString().ToLower();
                                var isCda = audienceReader.GetBoolean(1);
                                var isConsole = audienceReader.GetBoolean(2);
                                audiences.Add($"{aid}:{(isCda ? "1" : "0")}:{(isConsole ? "1" : "0")}");
                            }
                            this.Audiences = String.Join(", ", audiences);
						}
					}
                }
            }
        }

        public CpCollection Properties
        {
            get
            {
                if (_properties != null) return _properties;
                //HttpContext.Current.Trace.Write("TaxonomyInfo.Trail", "NOTCACHED");

                _properties = new CpCollection();
                if (!String.IsNullOrEmpty(_propertiesXml)) _properties.Load(_propertiesXml);
                return _properties;
            }
        }

        public DataView Trail
        {
            get
            {
                if (_trail != null) return _trail.DefaultView;
                //HttpContext.Current.Trace.Write("TaxonomyInfo.Trail", "NOTCACHED");

                string sql = String.Format("SELECT TaxonomyId, ParentTaxonomyId, SystemName, Name, Description FROM cpsys_Taxonomy WHERE TaxonomyId IN (Select AncestorTaxonomyId FROM cpsys_TaxonomyAncestors WHERE TaxonomyId = '{0}') ORDER BY TaxonomyId", this.TaxonomyId);
                HttpContext.Current.Trace.Write("TaxonomyInfo.Trail", sql);
                _trail = CpContent.GetTable("cpsys_Taxonomy", sql);
                _trail.DefaultView.Sort = "TaxonomyId";
                return _trail.DefaultView;
            }
        }

        public string GetTaxonomyValues(string field)
        {
            string value = String.Empty;
            switch (field.ToLower())
            {
                case "audiences":
                    value = this.Audiences;
                    break;
                case "parenttaxonomyid":
                    value = this.ParentTaxonomyId.ToString();
                    break;
                case "systemname":
                    value = this.SystemName;
                    break;
                case "path":
                    value = this.Path;
                    break;
                case "name":
                    value = this.Name;
                    break;
                case "description":
                    value = this.Description;
                    break;
                case "createdate":
                    value = this.CreateDate.ToString();
                    break;
                case "modifydate":
                    value = this.ModifyDate.ToString();
                    break;
                case "integrationid":
                    value = this.IntegrationId;
                    break;
                case "sort":
                case "order":
                case "sortorder":
                    value = this.SortOrder.ToString();
                    break;
                default:
                    if (Properties.Contains(field))
                        value = Properties.Get(field);
                    else
                    {
                        HttpContext.Current.Trace.Warn("GetTaxonomyValues", $"{field} wasen't found in Properties file.");
                        value = String.Empty;
                    }
                    break;
            }
            return value;
        }
    }
}




Keywords: Taxonomy



Related Taxonomy

Comments:

Be the first to leave a comment.
Please Login to post comments.