/// <summary>
        /// Get the content of the long message from DB throught Perergrine API 
        /// and supply the return string to home controller which will be populated into UI page : MsgInquiry.cshtml
        /// </summary>
        /// <param name="msg_id">The msg_id [string]</param>
        /// <returns> string </returns>
        public static string GetMessageContent(string msg_id)
        {
            int msg_id_int = Convert.ToInt32(msg_id);
            PeregrineService service = new PeregrineService();

            string content = service.getMessage(msg_id_int).Message1;
            return content;
        }
        /// <summary>
        /// This function is used for supply the list to autocomplete search
        /// The function is used to get the list of all available processes from DB throught Perergrine API 
        /// and supply the return list to home controller which will be populated into UI page : Index.cshtml and MsgInquiry.cshtml.
        /// </summary>
        /// <param name="search_string">search_string [string]</param>
        /// <returns>string</returns>
        public static string GetAutoCompleteList(string search_string)
        {
            List<string> AutoCompleteList = new List<string>();
            PeregrineService service = new PeregrineService();

            List<PeregrineDB.Process> process_list = service.searchProcessByName(search_string);
            foreach (PeregrineDB.Process p in process_list)
            {
                AutoCompleteList.Add(p.ProcessName.Trim());
            }

            string ans = string.Join(",", AutoCompleteList.Take(10).ToArray());

            return ans;
        }
        /// <summary>
        /// The function is used to get the list of jobs by a process from DB throught Perergrine API 
        /// and supply the return list to home controller which will be populated into UI page : Job.cshtml
        /// </summary>
        /// <param name="page">page [int]</param>
        /// <param name="pagesize">pagesize [int]</param>
        /// <param name="processID">processID [int]</param>
        /// <returns>PageData[Job]</returns>
        public static PageData<Job> GetJobByProcess(int page, int pagesize, int processID)
        {
            List<Job> JobByProcess      = new List<Job>();
            var pagingContext           = new PageData<Job>();
            PeregrineService service    = new PeregrineService();
            List<PeregrineDB.Job> Jobs = service.getPageOfJobsByProcessId(processID, 1, page * pagesize);

            foreach (PeregrineDB.Job j in Jobs)
            {
                JobByProcess.Add(new Job { JobContent = j.JobName, PercentProgress = (int)j.PercentComplete });
            }

            // Fill out the info of PageData var type
            pagingContext.Data = JobByProcess;
            pagingContext.CurrentPage = page;

            return pagingContext;
        }
        /// <summary>
        /// The function is used to get the list of messages by a process from DB throught Perergrine API 
        /// and supply the return list to home controller which will be populated into UI page : Message.cshtml
        /// </summary>
        /// <param name="page">page [int]</param>
        /// <param name="pagesize">pagesize [int]</param>
        /// <param name="processID">processID [int]</param>
        /// <returns>PageData[Message]</returns>
        public static PageData<Message> GetMessageByProcess(int page, int pagesize, int processID)
        {
            List<Message> MessageByProcess  = new List<Message>();
            var pagingContext               = new PageData<Message>();
            PeregrineService service        = new PeregrineService();
            List<PeregrineDB.Message> message_list = service.getPageOfMessagesByProcessId(processID, 1, page * pagesize);

            foreach (PeregrineDB.Message m in message_list)
            {
                MessageByProcess.Add(new Message { Category = m.Category, Date = m.Date, Content = m.Message1.Substring(0, Math.Min(Properties.Settings.Default.Message_Length, m.Message1.Length)), MessageLength = m.Message1.Length, MessageID = m.MessageID, Priority = m.Priority });
            }

            // Fill out the info of PageData var type
            pagingContext.Data = MessageByProcess;
            pagingContext.CurrentPage = page;

            return pagingContext;
        }
        /// <summary>
        /// The function is used to get the list of process's messages from DB throught Perergrine API 
        /// and supply the return list to home controller which will be populated into UI page : MessageList.cshtml
        /// </summary>
        /// <param name="page">page [int]</param>
        /// <param name="sortColumm">sortColumm [int]</param>
        /// <param name="sort_type">sort_type [int]</param>
        /// <param name="searchpriority">searchpriority [int]</param>
        /// <param name="searchprocess">searchprocess [string]</param>
        /// <param name="SU_SD_msg">SU_SD_msg [int]</param>
        /// <param name="pagesize">pagesize [int]</param>
        /// <returns>PageData[Message]</returns>
        public static PageData<Message> GetMessages(int page, int sortColumm, int sort_type, int searchpriority, string searchprocess, int SU_SD_msg, int pagesize)
        {
            List<Message> SummaryData = new List<Message>();
            var pagingContext = new PageData<Message>();
            PeregrineService service = new PeregrineService();
            List<GetPageOfMessageSummaryResult> MessageSummaryData;
            SortBy sort;
            SortDirection sortd;
            int Msg_Length_Threshold;

            switch (sortColumm)
            {
                case 0:
                    sort = SortBy.MESSAGE_CONTENT;
                    break;
                case 1:
                    sort = SortBy.PROCESS_NAME;
                    break;
                case 2:
                    sort = SortBy.MESSAGE_PRIORITY;
                    break;
                case 3:
                    sort = SortBy.MESSAGE_DATE;
                    break;
                default:
                    sort = SortBy.MESSAGE_DATE;
                    break;
            }

            switch (sort_type)
            {
                case 0:
                    sortd = SortDirection.ASSENDING;
                    break;
                case 1:
                    sortd = SortDirection.DESENDING;
                    break;
                default:
                    sortd = SortDirection.DESENDING;
                    break;
            }

            MessageSummaryData = service.getMessagesForMessageInq(searchprocess, page * pagesize, searchpriority, SU_SD_msg, sort, sortd);
            Msg_Length_Threshold = Properties.Settings.Default.Message_Length;

            foreach (GetPageOfMessageSummaryResult summary in MessageSummaryData)
            {
                String msgType = getMessageTypeString((Category)summary.Category);
                String state = getProcessStateString((int)summary.ProcState);
                SummaryData.Add(new Message
                {
                    MessageID = summary.MessageID,
                    ProcessID = (int)summary.ProcID,
                    ProcessName = summary.ProcName,
                    ProcessState = state,
                    Category = summary.Category,
                    Content = summary.Message.Substring(0, Math.Min(Msg_Length_Threshold, summary.Message.Length)),
                    MessageLength = summary.Message.Length,
                    Date = summary.Date,
                    Priority = summary.Priority,
                    MsgType = msgType
                });
            }

            // Fill out the info of PageData var type
            pagingContext.Data = SummaryData;
            pagingContext.CurrentPage = page;
            pagingContext.SortingType = (sortColumm * 2) + sort_type;

            return pagingContext;
        }
        /// <summary>
        /// The function is used to get the list of process from DB throught Perergrine API 
        /// and supply the return list to home controller which will be populated into UI page : ProcessList.cshtml
        /// </summary>
        /// <param name="page">page [int]</param>
        /// <param name="sortColumm">sortColumm [int]</param>
        /// <param name="sort_type">sort_type [int]</param>
        /// <param name="process_name">process_name [int]</param>
        /// <param name="pagesize">pagesize [int]</param>
        /// <returns> PageData[Process] </returns>
        public static PageData<Process> GetSummaryDataByPage(int page, int sortColumm, int sort_type, string process_name, int pagesize)
        {
            List<Process> SummaryData   = new List<Process>();
            PeregrineService service    = new PeregrineService();
            var pagingContext           = new PageData<Process>();
            List<GetProcessSummaryByNameResult> OneProcessSummary;
            List<GetPageOfProcessSummaryResult> ProcessSummaryData;

            if (process_name == "")
            {
                // Case that the the process search box in main page is empty
                SortBy sort;
                switch (sortColumm)
                {
                    case 0:
                        sort = SortBy.PROCESS_NAME;
                        break;
                    case 1:
                        sort = SortBy.MESSAGE_CONTENT;
                        break;
                    case 2:
                        sort = SortBy.MESSAGE_DATE;
                        break;
                    case 3:
                        sort = SortBy.PROCESS_STATE;
                        break;
                    default:
                        sort = SortBy.PROCESS_STATE;
                        break;
                }

                SortDirection sortd;
                switch (sort_type)
                {
                    case 0:
                        sortd = SortDirection.ASSENDING;
                        break;
                    case 1:
                        sortd = SortDirection.DESENDING;
                        break;
                    default:
                        sortd = SortDirection.DESENDING;
                        break;
                }

                ProcessSummaryData = service.getSummaryByPage(1, pagesize * page, sort, sortd);
                foreach (GetPageOfProcessSummaryResult summary in ProcessSummaryData)
                {
                    int percent, MsgType, MsgID;

                    if(summary.Percentage != null)
                        percent = (int)summary.Percentage;
                    else
                        percent = 0;

                    if (summary.MsgType != null)
                        MsgType = (int)summary.MsgType;
                    else
                        MsgType = 0;

                    if (summary.LastMsgID != null)
                        MsgID = (int)summary.LastMsgID;
                    else
                        MsgID = 0;

                    SummaryData.Add(new Process { ProcessId = summary.ProcessID, ProcessName = summary.ProcessName, LastAction = summary.LastMsg.Substring(0, Math.Min(Properties.Settings.Default.Message_Length, summary.LastMsg.Length)), MessageLength = summary.LastMsg.Length, MessageID = MsgID, MsgDate = (System.DateTime)summary.MsgDate, _ProcessState = summary.State.ToString(), MessageType = MsgType, JobPercentage = percent });
                }
            }
            else
            {
                // Case that the the process search box in main page is NOT empty
                // At most one process will be returned using this function
                OneProcessSummary = service.getProcessByName(process_name);
                foreach (GetProcessSummaryByNameResult summary in OneProcessSummary)
                {
                    int percent, MsgID, MsgType;
                    if (summary.Percentage != null)
                        percent = (int)summary.Percentage;
                    else
                        percent = 0;

                    if (summary.MsgType != null)
                        MsgType = (int)summary.MsgType;
                    else
                        MsgType = 0;

                    if (summary.LastMsgID != null)
                        MsgID = (int)summary.LastMsgID;
                    else
                        MsgID = 0;

                    SummaryData.Add(new Process { ProcessId = summary.ProcessID, ProcessName = summary.ProcessName, LastAction = summary.LastMsg.Substring(0, Math.Min(60, summary.LastMsg.Length)), MessageLength = summary.LastMsg.Length, MessageID = MsgID, MsgDate = (System.DateTime)summary.MsgDate, _ProcessState = summary.State.ToString(), MessageType = MsgType, JobPercentage = percent });
                }
            }

            // Fill out the info of PageData var type
            pagingContext.Data = SummaryData;
            pagingContext.CurrentPage = page;
            pagingContext.SortingType = (sortColumm * 2) + sort_type;

            return pagingContext;
        }