2025-05-08 10:18:50 -04:00

413 lines
11 KiB
C#

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<EPFormatFiles, EPFormatFile>))]
public class EPFormatFiles : vlnFormatList<EPFormatFile>
{
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<string> _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<string> _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<int?> _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<int> 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<int>("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<EPField>
{
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<string> _name;
public string name
{
get
{
return LazyLoad(ref _name, "@name");
}
}
private LazyLoad<string> _type;
public string type
{
get
{
return LazyLoad(ref _type, "@type");
}
}
private LazyLoad<string> _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<string> _text;
public string text
{
get
{
return LazyLoad(ref _text, "@text");
}
}
//roid of group item that individual sub-items will be the choices for the list/combobox for ROSINGLE and ROMULTI
private LazyLoad<string> _rosource;
public string rosource
{
get
{
return LazyLoad(ref _rosource, "@rosource");
}
}
//the columns in the RO that will be included in the exports
private LazyLoad<string> _returncols;
public List<string> returncols()
{
try
{
string tmp = LazyLoad(ref _returncols, "@returncols");
if (string.IsNullOrEmpty(tmp))
return new List<string>();
else
return tmp.Split(',').Select(p => p.Trim()).ToList();
}
catch
{
throw new ArgumentException($"Error in returncols for EP file: {((EPFormatFile)MyParentFormat).Name}.xml, field: {name}");
}
}
//number of lines for a multi-line text box to span
private LazyLoad<int?> _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<string> _validforsteptypes;
public List<string> validforsteptypes()
{
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)
{
List<string> tmp = validforsteptypes();
return tmp.Contains(StepType);
}
//return a list of items based on the ROsource specified in the EPFormat File
//will return all RO items under the Group that's roid = the rosource
public List<ROListItem> getROList(AnnotationInfo currAnn, bool includeblank)
{
if (string.IsNullOrEmpty(rosource))
return new List<ROListItem>();
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<ROListItem> mylist = children.Select(x => new ROListItem(x.title, x.roid.Substring(0, 12))).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}");
}
}
//return a list of values for the specified ROID
//given the EP items return columns
//will return all RO items under the Group that's roid = the rosource
public List<string> getROValuesList(AnnotationInfo currAnn, string roid)
{
if (string.IsNullOrEmpty(roid))
return new List<string>();
List<string> values = new List<string>();
DocVersionInfo MyDocVersion = currAnn.MyItem.MyDocVersion;
ROFSTLookup lookup = MyDocVersion.DocVersionAssociations[0].MyROFst.GetROFSTLookup(MyDocVersion);
rochild ro = lookup.GetRoChild(roid);
List<string> rtncols = returncols();
if (rtncols.Count == 0)
{
values.Add(ro.value);
}
else
{
foreach (string rcol in rtncols)
{
rochild ro_indiv = Array.Find(ro.children, x => x.appid.EndsWith($".{rcol}"));
if (ro_indiv.value != null) values.Add(ro_indiv.value);
}
}
return values;
}
}
#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
}