Retrieving Data from the AdvantageModuleEngine

 

Retrieving Data

AdvantageModuleEngine

Properties & Methods

The module engine is the core component use in retrieving data from the database.  There is no need for a developer to create tables or access the data directly.

The ModuleEngine Property is available on all front-end and back-end controls and already initialized for your current site.

 

Example

 

The following example uses an "Event" defined as the businessObject.  Supporting classes used in rendering are included.

Class Definition

Event Class (BusinessObject)

    public class Event : BusinessObject<Event>
    {

        public string Title { get; set; }

        public Guid EventType { get; set; }
        // public List<Guid> Categories { get; set; }

        public string Location { get; set; }

        private AdvantageImage _image = null;

        public AdvantageImage Image
        {
            get
            {
                if (_image == null)
                {
                    this._image = new AdvantageImage();

                    this._image.AddImageElement(new AdvantageImageElement() { ImageName = "Mobile", });
                    this._image.AddImageElement(new AdvantageImageElement() { ImageName = "Desktop", BreakPointName = "Small" });
                    this._image.DefaultElementName = "Mobile";
                }
                return _image;
            }
            set { _image = value; }
        }
        public string Content { get; set; }

        public string Summary { get; set; }
        public string Price { get; set; }
        public bool Accessible { get; set; }

        public bool Interpreters { get; set; }

        public bool Register { get; set; }

        public string RegistrationURL { get; set; }

  
        public DateTime StartDate { get; set; }=DateTime.Now.Date;

        public DateTime EndDate { get; set; }=DateTime.Now.Date;


        public TimeSpanXML StartTime { get; set; } = new TimeSpanXML();

        public TimeSpanXML EndTime { get; set; } = new TimeSpanXML();


        public List<string> Tags { get; set; }
        protected override void SetSummaryDataRow()
        {
            AddSummaryDataRow("Display", Title);
            AddSummaryDataRow("Date", StartDate);
        }

        protected override void SetSearchableProperties()
        {
            AddSearchableProperty("Title", Title);
            AddSearchableProperty("StartDate", StartDate);
            //if (Categories != null && Categories.Count > 0)
            //    foreach (Guid id in Categories) AddSearchableProperty("Category", id);
            if (EventType!=Guid.Empty)
                AddSearchableProperty("EventType",EventType);

            if (Tags != null) foreach (string tag in Tags) AddSearchableProperty("Tag", tag);

        }

        public override bool Validate(eCMSActions action, ActionArgs e, out eCMSEngineEventStatus status, out string message)
        {
            CalendarItem = createCalendarItem();  //save calendar item to DB if used in API.
            bool valid = true;
            status = eCMSEngineEventStatus.Success;
            message = string.Empty;


            if (string.IsNullOrEmpty(SEOName)) SEOName = Title.UrlFriendly();
            if (string.IsNullOrEmpty(Summary))
                Summary = Content.StripHTML().Shorten(200, false).TrimPunctuation() + "...";

            if (action == eCMSActions.Publish)
            {
                if (string.IsNullOrWhiteSpace(Title)) message += "Please enter a value for the event title.<br/>";
                if (string.IsNullOrWhiteSpace(Location)) message += "Please enter a value for the location.<br/>";
                if (string.IsNullOrWhiteSpace(Content)) message += "Please enter a value for the long description.<br/>";


                if (StartDate == new DateTime()) message += "Please select a value for the start date.<br/>";
                //if (StartTime == new DateTime()) message += "Please select a value for the start time.<br/>";
                if (EndDate == new DateTime()) message += "Please select a value for the end date.<br/>";
                //if (EndTime == new DateTime()) message += "Please select a value for the end time.<br/>";
                if (DateTime.Compare(StartDate, EndDate) > 0)
                {
                    message += "The start time cannot be later than the end time.<br/>";
                }
                valid = string.IsNullOrEmpty(message);
            }
 
            if (!valid)
            {
                status = eCMSEngineEventStatus.Exception;
            }
            return valid;
        }


        //Used in front end for calendar control
        private CalendarItem _calendarItem = null;
        public CalendarItem CalendarItem
        {
            get
            {
                if (_calendarItem == null) _calendarItem = createCalendarItem();
                return _calendarItem;
            }
            set
            {
                _calendarItem = value;
            }

        }

        private CalendarItem createCalendarItem()
        {
            var tmp = new CalendarItem();
            tmp.Id = MasterID.ToString();
            tmp.Title = Title;
            tmp.ClassName = "event_" + EventType.ToString();
            tmp.Url = RegistrationURL;
            tmp.Start = StartDate.ToString("yyyy-MM-dd");
            tmp.End = EndDate.ToString("yyyy-MM-dd");
            tmp.Summary = Summary;
            tmp.SEOName = SEOName;
            if (StartTime != null && StartTime.Time != null && StartTime.Time.HasValue)
            {
                tmp.AllDay = false;
                tmp.Start += "T" + ("0" + StartTime.Time.Value.Hours).Right(2) + ":" +
                                       ("0" + StartTime.Time.Value.Minutes).Right(2);

                tmp.End += "T" + ("0" + EndTime.Time.Value.Hours).Right(2) + ":" +
                                     ("0" + EndTime.Time.Value.Minutes).Right(2);
            }
            else
            {
                tmp.AllDay = true;
                tmp.End = EndDate.AddDays(1).ToString("yyyy-MM-dd");
            }

            return tmp;
        }
    }
    [Serializable]
    public class CalendarItem
    {
        [JsonProperty("id")]
        public string Id { get; set; }

        [JsonProperty("start")]
        public string Start { get; set; }
        [JsonProperty("end")]
        public string End { get; set; }

        [JsonProperty("allDay")]
        public bool AllDay { get; set; }

        [JsonProperty("title")]
        public string Title { get; set; }

        [JsonProperty("url")]
        public string Url { get; set; }

        [JsonProperty("className")]
        public string ClassName { get; set; }

        [JsonProperty("summary")]
        public string Summary { get; set; }

        [JsonProperty("seoName")]
        public string SEOName { get; set; }
    }
    public class TimeSpanXML : IXmlSerializable
    {
        public TimeSpan? Time { get; set; }

        public XmlSchema GetSchema() { return null; }

        public void ReadXml(XmlReader reader)
        {
            reader.MoveToContent();
            var _hours = reader.GetAttribute("Hours");
            var _minutes = reader.GetAttribute("Minutes");
            var _seconds = reader.GetAttribute("Seconds");

            Time = null;
            if (!string.IsNullOrEmpty(_hours) || !string.IsNullOrEmpty(_minutes) || !string.IsNullOrEmpty(_seconds))

                Time = new TimeSpan(_hours.ToInteger(0), _minutes.ToInteger(0), _seconds.ToInteger(0));

        }

        public void WriteXml(XmlWriter writer)
        {
            if (Time != null && Time.HasValue)
            {

                writer.WriteAttributeString("Hours", Time.Value.Hours.ToString());
                writer.WriteAttributeString("Minutes", Time.Value.Minutes.ToString());
                writer.WriteAttributeString("Seconds", Time.Value.Seconds.ToString());
            }
        }
    }

 

Full List Example

Returns a list of the objects completely populated

Full Object List Example

        private List<Event> _events = null;
        public List<Event> Events
        {
            get
            {
                if (_events == null)
                    _events = ModuleEngine.GetAllPublishedObjects<Event>();
                return _events;
            }
        }

 

Light List Example

Returns a list of the object only populated with specified fields

Light List Example

        private List<Event> _eventssummaryList = null;
        public List<Event> EventSummaryList
        {
            get
            {
                if (_eventssummaryList != null) return _eventssummaryList;
                List<string> fieldNames = new List<string>(){"Title","Summary","StartDate", "EndDate", "EventType" };
                _eventssummaryList = ModuleEngine.GetAllPublishedObjectsLight<Event>(fieldNames);
                return _eventssummaryList;
            }
        }

 

Key List Example

Returns a subset list based on a key/comparison value.

In this example the "Event BusinessObject" has set a key lookup to "EventType inside the class object.

Searchable Properties

        protected override void SetSearchableProperties()
        {
            AddSearchableProperty("Title", Title);
            AddSearchableProperty("StartDate", StartDate);
            //if (Categories != null && Categories.Count > 0)
            //    foreach (Guid id in Categories) AddSearchableProperty("Category", id);
            if (EventType!=Guid.Empty)
                AddSearchableProperty("EventType",EventType);

            if (Tags != null) foreach (string tag in Tags) AddSearchableProperty("Tag", tag);

        }
 

A "key" index lookup could be performed on Title, StartDate, EventType and Tag

List by Type Example

        public List<Event> EventByType(Guid eventType)
        {
            return ModuleEngine.GetPublishedObjectsBykey<Event>("EventType", eComparison.Equals, eventType);
        }

 

Multiple Criteria Key Example

 Returns a subset list based on multiple  key/comparison values.

Mulitiple "and" filters

        public List<Event> EventByTypeAndTag(Guid eventType, string tag)
        {

            //Defined the criteria for the saerch (and
            ObjectKeyCriteria oC = new ObjectKeyCriteria()
                { Key = "EventType", Value = eventType, Comparison =eComparison.Equals, DataType = eObjectKeyCriteriaDataType.Guid };
            ObjectKeyCriteria oC2 = new ObjectKeyCriteria()
            { Key = "Tag", Value = tag, Comparison = eComparison.Equals, DataType = eObjectKeyCriteriaDataType.String };

            return ModuleEngine.GetPublishedObjectsBykey<Event>(new List<ObjectKeyCriteria>() { oC, oC2 });
        

 

 

 

 

Back to Top Button