using System; using System.Collections.Generic; using System.Text; using System.ComponentModel; using System.Xml; using System.Linq; using System.Data.SqlClient; using System.Data; using Csla.Data; using static VEPROMS.CSLA.Library.ROFSTLookup; namespace VEPROMS.CSLA.Library { //C2025-023 - Electronic Procedures - Modifications to PROMS // class to handle storage and access of EPFormatFile and EPFormatFile details #region EPFormatFiles [TypeConverter(typeof(vlnListConverter))] public class EPFormatFiles : vlnFormatList { public EPFormatFiles(XmlNodeList xmlNodeList) : base(xmlNodeList) { } } #endregion #region EPFormatFile public class EPFormatFile : vlnFormatItem { #region Constructor public EPFormatFile(XmlNode xmlNode) : base(xmlNode) { } public EPFormatFile() : base() { } #endregion #region Business Fields // Name of the EP Viewer Format File private LazyLoad _Name; [DisplayName("Name")] [Description("EP Viewer File Name")] public string Name { get { return LazyLoad(ref _Name, "@Name"); } } // Name of the EP Viewer Format File private LazyLoad _Description; [DisplayName("Description")] [Description("EP Viewer Description")] public string Description { get { return LazyLoad(ref _Description, "@Description"); } } // Id of Annotation Type Associated with this file private LazyLoad _AnnotationTypeID; [DisplayName("AnnotationTypeID")] [Description("Id of Annotation Type Associated with this file")] public int? AnnotationTypeID { get { return LazyLoad(ref _AnnotationTypeID, "@AnnotationTypeID"); } } // returns a list of fields that are defined in the EP format's structure private EPFields _FieldList; public EPFields FieldList { get { XmlDocument xd = GetEPFormatData(Name); return _FieldList == null ? _FieldList = new EPFields(xd.SelectNodes("/EPFormat/EPField")) : _FieldList; } } #endregion #region Business Methods // update all in-use annotation types that have Electronic Procedures public static void UpdateAllInUseEPAnnotationTypes() { foreach (int formatid in GetAllInUseFormats()) { PlantFormat frmt = FormatInfo.Get(formatid).PlantFormat; foreach (EPFormatFile EP in frmt.EPFormatFiles) { UpdateAnnotationTypeAsEP((int) EP.AnnotationTypeID); } } } #endregion #region Data Access // static - Load EP Format details - save to db (insert/update) public static void UpdateEPFormat(string name, string data, string userID) { using (SqlConnection cn = Database.VEPROMS_SqlConnection) { using (SqlCommand cm = cn.CreateCommand()) { cm.CommandType = CommandType.StoredProcedure; cm.CommandText = "vesp_UpdateEPFormat"; cm.Parameters.AddWithValue("@name", name); cm.Parameters.AddWithValue("@data", data); cm.Parameters.AddWithValue("@userID", userID); cm.CommandTimeout = Database.DefaultTimeout; cm.ExecuteNonQuery(); } } } // load data for getting fieldlist for this EPFormat public static XmlDocument GetEPFormatData(string name) { name = name.Replace(".xml", ""); using (SqlConnection cn = Database.VEPROMS_SqlConnection) { using (SqlCommand cm = cn.CreateCommand()) { cm.CommandType = CommandType.Text; cm.CommandText = "Select Data FROM EPFormats where Name = @Name"; cm.Parameters.AddWithValue("@Name", name); cm.CommandTimeout = Database.DefaultTimeout; using (SafeDataReader dr = new SafeDataReader(cm.ExecuteReader())) { if (dr.Read()) { XmlDocument xd = new XmlDocument(); xd.XmlResolver = null; xd.LoadXml(dr.GetString("Data")); return xd; } } } } return null; } // Get all in-use formats public static List GetAllInUseFormats() { using (SqlConnection cn = Database.VEPROMS_SqlConnection) { using (SqlCommand cm = cn.CreateCommand()) { cm.CommandType = CommandType.Text; cm.CommandText = @"SELECT Distinct Formats.[FormatID] FROM ( SELECT FormatID FROM Contents UNION SELECT FormatID FROM DocVersions UNION SELECT FormatID FROM Folders ) inuse inner join Formats on inuse.FormatID = Formats.FormatID"; cm.CommandTimeout = Database.DefaultTimeout; using (DataTable dt = new DataTable()) { using (SqlDataAdapter da = new SqlDataAdapter(cm)) { da.Fill(dt); return dt.AsEnumerable().Select(x => x.Field("FormatID")).ToList(); } } } } } // update an annotation type as an Electronic Procedure Annotation Type public static void UpdateAnnotationTypeAsEP(int typeID) { using (SqlConnection cn = Database.VEPROMS_SqlConnection) { using (SqlCommand cm = cn.CreateCommand()) { cm.CommandType = CommandType.Text; cm.CommandText = @"Update AnnotationTypes SET IsEPAnnotationType = 1 WHERE TypeID = @typeID"; cm.Parameters.AddWithValue("@typeID", typeID); cm.CommandTimeout = Database.DefaultTimeout; cm.ExecuteNonQuery(); } } } // Returns true if there are any EP Annotation Types public static bool IsEPAnnotationType() { try { using (SqlConnection cn = Database.VEPROMS_SqlConnection) { using (SqlCommand cm = cn.CreateCommand()) { cm.CommandType = CommandType.Text; cm.CommandText = @"SELECT RESULT = CASE WHEN EXISTS(SELECT 1 FROM AnnotationTypes where IsEPAnnotationType = 1) THEN 1 ELSE 0 END"; cm.CommandTimeout = Database.DefaultTimeout; using (SafeDataReader dr = new SafeDataReader(cm.ExecuteReader())) { if (dr.Read() && dr.GetInt32("RESULT") == 1) return true; } } } } catch { // EP Support has not yet been added for this DB // IsEPAnnotationType does not exist // need to run PROMS Fixes // until then will ignore EP code // instead of crashing for a field not found } return false; } #endregion } public class EPFields : vlnFormatList { public EPFields() { } public EPFields(XmlNodeList xmlNodeList) : base(xmlNodeList) { } } // EP field class public class EPField : vlnFormatItem { public EPField(XmlNode xmlNode) : base(xmlNode) { } public EPField() : base() { } private LazyLoad _name; public string name { get { return LazyLoad(ref _name, "@name"); } } private LazyLoad _type; public string type { get { return LazyLoad(ref _type, "@type"); } } private LazyLoad _label; public string label { get { string tmp = LazyLoad(ref _label, "@label"); if (string.IsNullOrEmpty(tmp)) return LazyLoad(ref _name, "@name"); else return tmp; } } private LazyLoad _text; public string text { get { return LazyLoad(ref _text, "@text"); } } private LazyLoad _rosource; public string rosource { get { return LazyLoad(ref _rosource, "@rosource"); } } private LazyLoad _numlines; public int numlines { get { int? tmp = LazyLoad(ref _numlines, "@numlines"); if (tmp == null) return 1; return (int) tmp; } } //step types that the EPForma Item is valid for (as a list of types) private LazyLoad _validforsteptypes; public List validforsteptypes { get { try { string tmp = LazyLoad(ref _validforsteptypes, "@validforsteptypes"); return tmp.Split(',').Select(p => p.Trim()).ToList(); } catch { throw new ArgumentException($"Error in validforsteptypes for EP file: {((EPFormatFile) MyParentFormat).Name}.xml, field: {name}"); } } } public bool IsValidForStepType(string StepType) { return validforsteptypes.Contains(StepType); } //return a list of items based on the ROsource specified in the EPFormat File public List getROList(AnnotationInfo currAnn, bool includeblank) { if (string.IsNullOrEmpty(rosource)) return new List(); try { DocVersionInfo MyDocVersion = currAnn.MyItem.MyDocVersion; ROFSTLookup lookup = MyDocVersion.DocVersionAssociations[0].MyROFst.GetROFSTLookup(MyDocVersion); string roid = FormatRoidKey(rosource, false); rochild[] children = lookup.GetRoChildrenByRoid(roid); List mylist = children.Select(x => new ROListItem(x.title, x.roid)).ToList(); if (includeblank) mylist.Insert(0, new ROListItem("", "")); return mylist; } catch (Exception Ex) { throw new ArgumentException($"Error in rosource for EP file: {((EPFormatFile)MyParentFormat).Name}.xml, field: {name}"); } } } #endregion //C2025-023 - Electronic Procedures - Modifications to PROMS // class to handle return of RO Lists #region EPFormatFiles public class ROListItem { public string Text { get; private set; } public string Value { get; private set; } public ROListItem(string _text, string _value) { Text = _text; Value = _value; } } #endregion }