git checkout -b feature-branch

This commit is contained in:
2023-01-13 18:13:01 -07:00
parent 0468c8d42d
commit c292328abe
855 changed files with 61144 additions and 112 deletions

View File

@ -0,0 +1,37 @@
using OI.Metrology.Server.Models;
using OI.Metrology.Shared.Models.Stateless;
using System.Text.Json;
namespace OI.Metrology.Server.Repository;
public class AppSettingsRepository : IAppSettingsRepository
{
private readonly AppSettings _AppSettings;
public AppSettingsRepository(AppSettings appSettings) => _AppSettings = appSettings;
public List<string> GetAppSettings()
{
List<string> results = new();
string json = JsonSerializer.Serialize(_AppSettings);
JsonElement jsonElement = JsonSerializer.Deserialize<JsonElement>(json);
JsonProperty[] jsonProperties = jsonElement.EnumerateObject().ToArray();
foreach (JsonProperty jsonProperty in jsonProperties)
results.Add(jsonProperty.Value.ToString());
if (!_AppSettings.IsDevelopment)
throw new Exception("Shouldn't expose!");
return results;
}
List<string> IAppSettingsRepository.GetAppSettings() => GetAppSettings();
public string GetBuildNumberAndGitCommitSeven()
{
string result = string.Concat(_AppSettings.BuildNumber, '-', _AppSettings.GitCommitSeven);
return result;
}
string IAppSettingsRepository.GetBuildNumberAndGitCommitSeven() => GetBuildNumberAndGitCommitSeven();
}

View File

@ -0,0 +1,36 @@
using OI.Metrology.Server.Models;
using OI.Metrology.Shared.Models.Stateless;
using System.Net;
namespace OI.Metrology.Server.Repository;
public class ClientSettingsRepository : IClientSettingsRepository
{
private readonly AppSettings _AppSettings;
public ClientSettingsRepository(AppSettings appSettings) => _AppSettings = appSettings;
public List<string> GetClientSettings(IPAddress? remoteIpAddress)
{
List<string> results = new();
if (remoteIpAddress is null)
results.Add(nameof(remoteIpAddress));
else
results.Add(remoteIpAddress.ToString());
if (!_AppSettings.IsDevelopment)
throw new Exception("Shouldn't expose!");
return results;
}
List<string> IClientSettingsRepository.GetClientSettings(IPAddress? remoteIpAddress) => GetClientSettings(remoteIpAddress);
public string GetIpAddress(IPAddress? remoteIpAddress)
{
string result = remoteIpAddress is null ? string.Empty : remoteIpAddress.ToString();
return result;
}
string IClientSettingsRepository.GetIpAddress(IPAddress? remoteIpAddress) => GetIpAddress(remoteIpAddress);
}

View File

@ -0,0 +1,88 @@
using Newtonsoft.Json.Linq;
using OI.Metrology.Shared.DataModels;
using OI.Metrology.Shared.Models;
using OI.Metrology.Shared.Models.Stateless;
using OI.Metrology.Shared.Services;
using System.Net;
namespace OI.Metrology.Server.Repository;
public class InboundRepository : IInboundRepository
{
private readonly Serilog.ILogger _Log;
public InboundRepository() => _Log = Serilog.Log.ForContext<InboundRepository>();
bool IInboundRepository.IsIPAddressAllowed(string inboundApiAllowedIPList, IPAddress? remoteIP)
{
if (string.IsNullOrWhiteSpace(inboundApiAllowedIPList))
return true;
if (remoteIP is null)
return false;
byte[] remoteIPBytes = remoteIP.GetAddressBytes();
string[] allowedIPs = inboundApiAllowedIPList.Split(';');
foreach (string ip in allowedIPs)
{
IPAddress? parsedIP;
if (IPAddress.TryParse(ip, out parsedIP))
{
if (parsedIP.GetAddressBytes().SequenceEqual(remoteIPBytes))
return true;
}
}
return false;
}
// this is the main endpoint, it accepts a JSON message that contains both the header and data records together
// tooltype is the ToolTypeName column from the ToolType table
// JToken is how you can accept a JSON message without deserialization.
// Using "string" doesn't work because ASP.NET Core will expect a json encoded string, not give you the actual string.
DataResponse IInboundRepository.Data(IMetrologyRepository metrologyRepository, IInboundDataService inboundDataService, string tooltype, JToken jsonbody)
{
DataResponse result = new();
ToolType? toolType = metrologyRepository.GetToolTypeByName(tooltype);
if (toolType is null)
result.Errors.Add("Invalid tool type: " + tooltype);
else
{
List<ToolTypeMetadata> metaData = metrologyRepository.GetToolTypeMetadataByToolTypeID(toolType.ID).ToList();
if (metaData is null)
result.Errors.Add("Invalid metadata for tool type: " + tooltype);
else if (jsonbody is null)
result.Errors.Add("Invalid json");
else
inboundDataService.ValidateJSONFields(jsonbody, 0, metaData, result.Errors, result.Warnings);
if (metaData is not null && jsonbody is not null && !result.Errors.Any())
{
try
{
result.HeaderID = inboundDataService.DoSQLInsert(jsonbody, toolType, metaData);
result.Success = result.HeaderID > 0;
}
catch (Exception ex) { result.Errors.Add(ex.Message); }
}
}
return result;
}
// this is the endpoint for attaching a field. It is not JSON, it is form-data/multipart like an HTML form because that's the normal way.
// header ID is the ID value from the Header table
// datauniqueid is the Title value from the Data/Detail table
string? IInboundRepository.AttachFile(IMetrologyRepository metrologyRepository, IAttachmentsService attachmentsService, string tooltype, long headerid, string datauniqueid, string fileName, object uploadedFile)
{
string? result = null;
ToolType toolType = metrologyRepository.GetToolTypeByName(tooltype);
if (toolType is null)
result = $"Invalid tool type: {tooltype}";
string filename = Path.GetFileName(fileName);
if (string.IsNullOrWhiteSpace(filename))
result = "Empty filename";
if (filename.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0)
result = "Invalid filename";
if (result is null && toolType is not null)
attachmentsService.SaveAttachment(toolType, headerid, datauniqueid, filename, uploadedFile);
return result;
}
}

View File

@ -0,0 +1,761 @@
using Dapper;
using Microsoft.Extensions.Caching.Memory;
using Newtonsoft.Json.Linq;
using OI.Metrology.Shared.DataModels;
using OI.Metrology.Shared.Models.Stateless;
using OI.Metrology.Shared.Repositories;
using System.Data;
using System.Data.Common;
using System.Transactions;
#pragma warning disable CS8600, CS8602, CS8603, CS8604, CS8625
namespace OI.Metrology.Server.Repositories;
public class MetrologyRepository : IMetrologyRepository
{
private readonly IMemoryCache _Cache;
private readonly IDbConnectionFactory _DBConnectionFactory;
public MetrologyRepository(IDbConnectionFactory dbConnectionFactory, IMemoryCache memoryCache)
{
_Cache = memoryCache;
_DBConnectionFactory = dbConnectionFactory;
}
private DbConnection GetDbConnection() => _DBConnectionFactory.GetDbConnection();
protected DbProviderFactory GetDbProviderFactory(IDbConnection conn) =>
DbProviderFactories.GetFactory(conn.GetType().Namespace);
public bool IsTestDatabase()
{
int c = 0;
using (DbConnection conn = GetDbConnection())
{
c = conn.Query<int>(
"SELECT COUNT(*) " +
"FROM Configuration " +
"WHERE KeyName = 'TestDatabase' " +
"AND ValueString = '1'").FirstOrDefault();
}
return c > 0;
}
public TransactionScope StartTransaction() => new();
protected void CacheItem(string key, object v)
{
System.Diagnostics.Debug.WriteLine("CacheItem: " + key);
_ = _Cache.Set(key, v, new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromHours(1)));
}
public IEnumerable<ToolType> GetToolTypes()
{
IEnumerable<ToolType> cached;
string cacheKey = "GetToolTypes";
if (_Cache.TryGetValue(cacheKey, out cached))
return cached;
using DbConnection conn = GetDbConnection();
IEnumerable<ToolType> r = conn.Query<ToolType>("SELECT * FROM ToolType");
CacheItem(cacheKey, r);
return r;
}
public ToolType GetToolTypeByName(string name)
{
ToolType cached;
string cacheKey = "GetToolTypeByName_" + name;
if (_Cache.TryGetValue(cacheKey, out cached))
return cached;
using DbConnection conn = GetDbConnection();
ToolType r = conn.QueryFirstOrDefault<ToolType>(
"SELECT * FROM ToolType WHERE ToolTypeName = @name",
new { name });
CacheItem(cacheKey, r);
return r;
}
public ToolType GetToolTypeByID(int id)
{
ToolType cached;
string cacheKey = "GetToolTypeByID_" + id.ToString();
if (_Cache.TryGetValue(cacheKey, out cached))
return cached;
using DbConnection conn = GetDbConnection();
ToolType r = conn.QueryFirstOrDefault<ToolType>(
"SELECT * FROM ToolType WHERE ID = @id",
new { id });
CacheItem(cacheKey, r);
return r;
}
public IEnumerable<ToolTypeMetadata> GetToolTypeMetadataByToolTypeID(int id)
{
IEnumerable<ToolTypeMetadata> cached;
string cacheKey = "GetToolTypeMetadataByToolTypeID_" + id.ToString();
if (_Cache.TryGetValue(cacheKey, out cached))
return cached;
using DbConnection conn = GetDbConnection();
IEnumerable<ToolTypeMetadata> r = conn.Query<ToolTypeMetadata>(
"SELECT * FROM ToolTypeMetadata WHERE ToolTypeID = @id",
new { id });
CacheItem(cacheKey, r);
return r;
}
public long InsertToolDataJSON(JToken jsonrow, long headerId, List<ToolTypeMetadata> metaData, string tableName)
{
long r = -1;
using (DbConnection conn = GetDbConnection())
{
bool isHeader = headerId <= 0;
// get fields from metadata
List<ToolTypeMetadata> fields = metaData.Where(md => md.Header == isHeader).ToList();
// maps ApiName to ColumnName
Dictionary<string, string> fieldmap = new();
// store property name of container field
string containerField = null;
// maps container ApiName to ColumnName
Dictionary<string, string> containerFieldMap = new();
// build field map
foreach (ToolTypeMetadata f in fields)
{
if ((f.ApiName is not null) && f.ApiName.Contains('\\'))
{
string n = f.ApiName.Split('\\')[0].Trim().ToUpper();
if (containerField is null)
containerField = n;
else if (!string.Equals(containerField, n))
throw new Exception("Only one container field is allowed");
string pn = f.ApiName.Split('\\')[1].Trim().ToUpper();
containerFieldMap.Add(pn, f.ColumnName.Trim());
}
else if (!string.IsNullOrWhiteSpace(f.ApiName) && !string.IsNullOrWhiteSpace(f.ColumnName))
{
fieldmap.Add(f.ApiName.Trim().ToUpper(), f.ColumnName.Trim());
}
}
if (containerField is null)
{
// No container field, just insert a single row
r = InsertRowFromJSON(conn, tableName, jsonrow, fieldmap, headerId, null, null);
}
else
{
// Find the container field in the json
JProperty contJP = jsonrow.Children<JProperty>().Where(c => string.Equals(c.Name.Trim(), containerField, StringComparison.OrdinalIgnoreCase)).SingleOrDefault();
if ((contJP is not null) && (contJP.Value is JArray array))
{
JArray contRows = array;
// Insert a row for each row in the container field
foreach (JToken contRow in contRows)
{
r = InsertRowFromJSON(conn, tableName, jsonrow, fieldmap, headerId, contRow, containerFieldMap);
}
}
else
throw new Exception("Invalid container field type");
}
}
return r;
}
protected void AddParameter(IDbCommand cmd, string name, object value)
{
IDbDataParameter p = cmd.CreateParameter();
p.ParameterName = name;
p.Value = value;
_ = cmd.Parameters.Add(p);
}
protected long InsertRowFromJSON(
IDbConnection conn,
string tableName,
JToken jsonrow,
Dictionary<string, string> fieldmap,
long headerId,
JToken containerrow,
Dictionary<string, string> containerFieldmap)
{
// Translate the json into a SQL INSERT using the field map
IDbCommand cmd = conn.CreateCommand();
string columns = "INSERT INTO [" + tableName + "](";
string parms = ") SELECT ";
int parmnumber = 1;
if (headerId > 0)
{
columns += "HeaderID,";
parms += "@HeaderID,";
AddParameter(cmd, "@HeaderID", headerId);
}
foreach (JProperty jp in jsonrow.Children<JProperty>())
{
string apifield = jp.Name.Trim().ToUpper();
if (fieldmap.TryGetValue(apifield, out string value))
{
string parmname = string.Format("@p{0}", parmnumber);
columns += string.Format("[{0}],", value);
parms += parmname;
parms += ",";
parmnumber += 1;
object sqlValue = ((JValue)jp.Value).Value;
sqlValue ??= DBNull.Value;
AddParameter(cmd, parmname, sqlValue);
}
}
if ((containerrow is not null) && (containerFieldmap is not null))
{
foreach (JProperty jp in containerrow.Children<JProperty>())
{
string apifield = jp.Name.Trim().ToUpper();
if (containerFieldmap.TryGetValue(apifield, out string value))
{
string parmname = string.Format("@p{0}", parmnumber);
columns += string.Format("[{0}],", value);
parms += parmname;
parms += ",";
parmnumber += 1;
object sqlValue = ((JValue)jp.Value).Value;
sqlValue ??= DBNull.Value;
AddParameter(cmd, parmname, sqlValue);
}
}
}
if (parmnumber == 1)
throw new Exception("JSON had no fields");
cmd.CommandText = columns.TrimEnd(',') + parms.TrimEnd(',') + ";SELECT SCOPE_IDENTITY();";
object o = cmd.ExecuteScalar();
if ((o is null) || Convert.IsDBNull(o))
throw new Exception("Unexpected query result");
return Convert.ToInt64(o);
}
public DataTable ExportData(string spName, DateTime startTime, DateTime endTime)
{
DataTable dt = new();
DateTime endTimeLocal = endTime.ToLocalTime();
DateTime startTimeLocal = startTime.ToLocalTime();
using (DbConnection conn = GetDbConnection())
{
DbProviderFactory factory = GetDbProviderFactory(conn);
DbCommand cmd = factory.CreateCommand();
cmd.Connection = conn;
cmd.CommandText = spName;
cmd.CommandType = CommandType.StoredProcedure;
AddParameter(cmd, "@StartTime", startTimeLocal);
AddParameter(cmd, "@EndTime", endTimeLocal);
DbDataAdapter da = factory.CreateDataAdapter();
da.SelectCommand = cmd;
_ = da.Fill(dt);
}
return dt;
}
protected string FormDynamicSelectQuery(IEnumerable<ToolTypeMetadata> fields, string tableName)
{
System.Text.StringBuilder sb = new();
_ = sb.Append("SELECT ");
bool firstField = true;
foreach (ToolTypeMetadata f in fields)
{
if (!string.IsNullOrWhiteSpace(f.ColumnName))
{
if (!firstField)
_ = sb.Append(',');
if (f.GridAttributes is not null && f.GridAttributes.Contains("isNull"))
{
_ = sb.AppendFormat("{0}", "ISNULL(" + f.ColumnName + ", '')[" + f.ColumnName + "]");
}
else
{
_ = sb.AppendFormat("[{0}]", f.ColumnName);
}
firstField = false;
}
}
_ = sb.AppendFormat(" FROM [{0}] ", tableName);
return sb.ToString();
}
public DataTable GetHeaders(int toolTypeId, DateTime? startTime, DateTime? endTime, int? pageNo, int? pageSize, long? headerId, out long totalRecords)
{
ToolType tt = GetToolTypeByID(toolTypeId);
if (tt is null)
throw new Exception("Invalid tool type ID");
IEnumerable<ToolTypeMetadata> md = GetToolTypeMetadataByToolTypeID(toolTypeId);
if (md is null)
throw new Exception("Invalid tool type metadata");
DataTable dt = new();
using (DbConnection conn = GetDbConnection())
{
System.Text.StringBuilder sb = new();
_ = sb.Append(
FormDynamicSelectQuery(
md.Where(m => m.Header == true).ToList(),
tt.HeaderTableName)
);
DbProviderFactory factory = GetDbProviderFactory(conn);
DbCommand cmd = factory.CreateCommand();
string whereClause = "";
if (headerId.HasValue && headerId.Value > 0)
{
whereClause = "ID = @HeaderID ";
AddParameter(cmd, "@HeaderID", headerId.Value);
}
else
{
if (startTime.HasValue)
{
whereClause = "[Date] >= @StartTime ";
DateTime startTimeLocal = startTime.Value.ToLocalTime();
AddParameter(cmd, "@StartTime", startTimeLocal);
}
if (endTime.HasValue)
{
DateTime endTimeLocal = endTime.Value.ToLocalTime();
TimeSpan timeSpan = new(DateTime.Now.Ticks - endTimeLocal.Ticks);
if (timeSpan.TotalMinutes > 5)
{
if (whereClause.Length > 0)
whereClause += "AND ";
whereClause += "[Date] <= @EndTime ";
AddParameter(cmd, "@EndTime", endTimeLocal);
}
}
}
if (whereClause.Length > 0)
{
_ = sb.Append("WHERE ");
_ = sb.Append(whereClause);
}
if (pageNo.HasValue && pageSize.HasValue)
{
_ = sb.Append("ORDER BY [Date] DESC OFFSET @PageNum * @PageSize ROWS FETCH NEXT @PageSize ROWS ONLY");
AddParameter(cmd, "@PageNum", pageNo.Value);
AddParameter(cmd, "@PageSize", pageSize.Value);
}
else
{
_ = sb.Append("ORDER BY [Date] DESC");
}
cmd.Connection = conn;
cmd.CommandText = sb.ToString();
cmd.CommandType = CommandType.Text;
DbDataAdapter da = factory.CreateDataAdapter();
da.SelectCommand = cmd;
_ = da.Fill(dt);
cmd.CommandText = "SELECT COUNT(*) FROM [" + tt.HeaderTableName + "] ";
if (whereClause.Length > 0)
{
cmd.CommandText += "WHERE ";
cmd.CommandText += whereClause;
}
totalRecords = Convert.ToInt64(cmd.ExecuteScalar());
}
return dt;
}
public DataTable GetData(int toolTypeId, long headerid)
{
ToolType tt = GetToolTypeByID(toolTypeId);
if (tt is null)
throw new Exception("Invalid tool type ID");
IEnumerable<ToolTypeMetadata> md = GetToolTypeMetadataByToolTypeID(toolTypeId);
if (md is null)
throw new Exception("Invalid tool type metadata");
DataTable dt = new();
using (DbConnection conn = GetDbConnection())
{
System.Text.StringBuilder sb = new();
_ = sb.Append(
FormDynamicSelectQuery(
md.Where(m => m.Header == false).OrderBy(m => m.GridDisplayOrder).ToList(),
tt.DataTableName)
);
DbProviderFactory factory = GetDbProviderFactory(conn);
DbCommand cmd = factory.CreateCommand();
_ = sb.Append("WHERE [HeaderID] = @HeaderID ");
if (!string.IsNullOrWhiteSpace(tt.DataGridSortBy))
{
_ = sb.AppendFormat("ORDER BY {0} ", tt.DataGridSortBy);
}
AddParameter(cmd, "@HeaderID", headerid);
cmd.Connection = conn;
cmd.CommandText = sb.ToString();
cmd.CommandType = CommandType.Text;
DbDataAdapter da = factory.CreateDataAdapter();
da.SelectCommand = cmd;
_ = da.Fill(dt);
}
// this code will add a couple of rows with stats calculations
if (!string.IsNullOrWhiteSpace(tt.DataGridStatsColumn))
{
if (dt.Columns.Contains(tt.DataGridStatsColumn))
{
double sumAll = 0;
double sumAllQ = 0;
foreach (DataRow dr in dt.Rows)
{
try
{
object v = dr[tt.DataGridStatsColumn];
if (!Convert.IsDBNull(v))
{
double d = Convert.ToDouble(v);
sumAll += d;
sumAllQ += d * d;
}
}
catch
{
}
}
double length = Convert.ToDouble(dt.Rows.Count);
double meanAverage = Math.Round(sumAll / length, 4);
double stdDev = Math.Sqrt((sumAllQ - sumAll * sumAll / length) * (1.0d / (length - 1)));
string stdDevStr = "";
if (tt.DataGridStatsStdDevType == "%")
stdDevStr = Math.Round(stdDev / meanAverage * 100.0d, 2).ToString("0.####") + "%";
else
stdDevStr = Math.Round(stdDev, 4).ToString("0.####");
int labelIndex = dt.Columns[tt.DataGridStatsColumn].Ordinal - 1;
DataRow newrow = dt.NewRow();
newrow["ID"] = -1;
newrow[labelIndex] = "Average";
newrow[tt.DataGridStatsColumn] = meanAverage;
dt.Rows.Add(newrow);
newrow = dt.NewRow();
newrow["ID"] = -2;
newrow[labelIndex] = "Std Dev";
newrow[tt.DataGridStatsColumn] = stdDevStr;
dt.Rows.Add(newrow);
}
}
return dt;
}
public Guid GetHeaderAttachmentID(int toolTypeId, long headerId)
{
ToolType tt = GetToolTypeByID(toolTypeId);
if (tt is null)
throw new Exception("Invalid tool type ID");
using DbConnection conn = GetDbConnection();
string sql =
$"UPDATE [{tt.HeaderTableName}] SET AttachmentID = NEWID() WHERE ID = @HeaderID AND AttachmentID IS NULL; " +
$"SELECT AttachmentID FROM [{tt.HeaderTableName}] WHERE ID = @HeaderID";
return conn.ExecuteScalar<Guid>(sql, param: new { HeaderID = headerId });
}
public string GetHeaderInsertDate(int toolTypeId, long headerId)
{
ToolType tt = GetToolTypeByID(toolTypeId);
if (tt is null)
throw new Exception("Invalid tool type ID");
using DbConnection conn = GetDbConnection();
string sql =
$"SELECT CONVERT(varchar, case when [InsertDate] < [Date] or [Date] is null then [InsertDate] else [Date] end, 120) d FROM[{tt.HeaderTableName}] where ID = @HeaderID";
return conn.ExecuteScalar<string>(sql, param: new { HeaderID = headerId });
}
public string GetAttachmentInsertDateByGUID(string tableName, Guid attachmentId)
{
using DbConnection conn = GetDbConnection();
string sql = "";
if (tableName is "SP1RunData" or "TencorRunData")
{
sql = $"SELECT [InsertDate] FROM[{tableName}] where AttachmentID = @AttachmentID";
}
else
{
sql = $"SELECT [InsertDate] FROM[{tableName}] where AttachmentID = @AttachmentID";
}
return conn.ExecuteScalar<string>(sql, param: new { AttachmentID = attachmentId });
}
public void SetHeaderDirName(string tableName, long headerId, string dateDir)
{
using DbConnection conn = GetDbConnection();
_ = conn.Execute($"UPDATE [{tableName}] SET AttachDirName = @AttachDirName WHERE ID = @HeaderID;", new { HeaderID = headerId, AttachDirName = dateDir });
}
public Guid GetDataAttachmentID(int toolTypeId, long headerId, string title)
{
ToolType tt = GetToolTypeByID(toolTypeId);
if (tt is null)
throw new Exception("Invalid tool type ID");
using DbConnection conn = GetDbConnection();
string sql =
$"UPDATE [{tt.DataTableName}] SET AttachmentID = NEWID() WHERE HeaderID = @HeaderID AND Title = @Title AND AttachmentID IS NULL; " +
$"SELECT AttachmentID FROM [{tt.DataTableName}] WHERE HeaderID = @HeaderID AND Title = @Title";
return conn.ExecuteScalar<Guid>(sql, param: new { HeaderID = headerId, Title = title });
}
// J Ouellette Added
public string GetDataInsertDate(int toolTypeId, long headerId, string title)
{
ToolType tt = GetToolTypeByID(toolTypeId);
if (tt is null)
throw new Exception("Invalid tool type ID");
using DbConnection conn = GetDbConnection();
string sql = "";
if (tt.DataTableName is "" or "")
{
sql = $"SELECT InsertDate FROM [{tt.DataTableName}] WHERE HeaderID = @HeaderID AND Title = @Title";
}
else
{
sql = $"SELECT [InsertDate] FROM[{tt.DataTableName}] WHERE HeaderID = @HeaderID AND Title = @Title";
}
return conn.ExecuteScalar<string>(sql, param: new { HeaderID = headerId, Title = title });
}
public void SetDataDirName(string tableName, long headerId, string title, string dateDir)
{
using DbConnection conn = GetDbConnection();
string sql =
$"UPDATE [{tableName}] SET AttachDirName = @AttachDirName WHERE HeaderID = @HeaderID AND Title = @Title;";
_ = conn.Execute(sql, param: new { HeaderID = headerId, Title = title, AttachDirName = dateDir });
}
public void PurgeExistingData(int toolTypeId, string title)
{
using DbConnection conn = GetDbConnection();
_ = conn.Execute("PurgeExistingData", param: new { ToolTypeID = toolTypeId, Title = title }, commandType: CommandType.StoredProcedure);
}
public DataSet GetOIExportData(int toolTypeId, long headerid)
{
ToolType tt = GetToolTypeByID(toolTypeId);
if (tt is null)
throw new Exception("Invalid tool type ID");
if (string.IsNullOrWhiteSpace(tt.OIExportSPName))
throw new Exception("OpenInsight export not available for " + tt.ToolTypeName);
DataSet ds = new();
using (DbConnection conn = GetDbConnection())
{
DbProviderFactory factory = GetDbProviderFactory(conn);
DbCommand cmd = factory.CreateCommand();
cmd.Connection = conn;
cmd.CommandText = tt.OIExportSPName;
cmd.CommandType = CommandType.StoredProcedure;
AddParameter(cmd, "@ID", headerid);
DbDataAdapter da = factory.CreateDataAdapter();
da.SelectCommand = cmd;
_ = da.Fill(ds);
}
return ds;
}
public IEnumerable<HeaderCommon> GetHeaderTitles(int toolTypeId, int? pageNo, int? pageSize, out long totalRecords)
{
ToolType tt = GetToolTypeByID(toolTypeId);
if (tt is null)
throw new Exception("Invalid tool type ID");
using DbConnection conn = GetDbConnection();
string sql = $"SELECT ID, InsertDate, AttachmentID, Title, [Date] FROM {tt.HeaderTableName} ORDER BY [Date] DESC ";
IEnumerable<HeaderCommon> headers;
if (pageNo.HasValue && pageSize.HasValue)
{
sql += "OFFSET @PageNum * @PageSize ROWS FETCH NEXT @PageSize ROWS ONLY";
headers = conn.Query<HeaderCommon>(sql, param: new { PageNum = pageNo.Value, PageSize = pageSize.Value }).ToList();
}
else
{
headers = conn.Query<HeaderCommon>(sql).ToList();
}
sql = $"SELECT COUNT(*) FROM [{tt.HeaderTableName}] ";
totalRecords = Convert.ToInt64(conn.ExecuteScalar(sql));
return headers;
}
public IEnumerable<KeyValuePair<string, string>> GetHeaderFields(int toolTypeId, long headerid)
{
ToolType tt = GetToolTypeByID(toolTypeId);
if (tt is null)
throw new Exception("Invalid tool type ID");
IEnumerable<ToolTypeMetadata> md = GetToolTypeMetadataByToolTypeID(toolTypeId);
if (md is null)
throw new Exception("Invalid tool type metadata");
List<KeyValuePair<string, string>> r = new();
using (DbConnection conn = GetDbConnection())
{
DbProviderFactory factory = GetDbProviderFactory(conn);
DbCommand cmd = factory.CreateCommand();
cmd.Connection = conn;
cmd.CommandText = $"SELECT * FROM [{tt.HeaderTableName}] WHERE ID = @HeaderID";
AddParameter(cmd, "@HeaderID", headerid);
DataTable dt = new();
DbDataAdapter da = factory.CreateDataAdapter();
da.SelectCommand = cmd;
_ = da.Fill(dt);
DataRow dr = null;
if (dt.Rows.Count > 0)
dr = dt.Rows[0];
foreach (ToolTypeMetadata m in md.Where(m => m.Header == true && m.TableDisplayOrder > 0).OrderBy(m => m.TableDisplayOrder))
{
string v = "";
if (dr is not null)
{
object o = dr[m.ColumnName];
if (o is not null && !Convert.IsDBNull(o))
v = Convert.ToString(o);
}
KeyValuePair<string, string> kvp = new(m.DisplayTitle, v);
r.Add(kvp);
}
}
return r;
}
public IEnumerable<AwaitingDisposition> GetAwaitingDisposition()
{
using DbConnection conn = GetDbConnection();
return conn.Query<AwaitingDisposition>("GetAwaitingDispo", commandType: CommandType.StoredProcedure);
}
public int UpdateReviewDate(int toolTypeId, long headerId, bool clearDate)
{
ToolType tt = GetToolTypeByID(toolTypeId);
if (tt is null)
throw new Exception("Invalid tool type ID");
using DbConnection conn = GetDbConnection();
if (clearDate)
{
// if it's already past the 6 hour window, then it won't show in queue anyway, so need to return value so we can show that
string sql = $"SELECT DATEDIFF(HH, INSERTDATE, GETDATE()) FROM [{tt.HeaderTableName}] WHERE ID = @HeaderID";
int hrs = conn.ExecuteScalar<int>(sql, param: new { HeaderId = headerId });
_ = conn.Execute($"UPDATE [{tt.HeaderTableName}] SET ReviewDate = NULL WHERE ID = @HeaderID", new { HeaderID = headerId });
return hrs;
}
else
{
_ = conn.Execute($"UPDATE [{tt.HeaderTableName}] SET ReviewDate = GETDATE() WHERE ID = @HeaderID", new { HeaderID = headerId });
return 1;
}
}
public Guid GetHeaderAttachmentIDByTitle(int toolTypeId, string title)
{
ToolType tt = GetToolTypeByID(toolTypeId);
if (tt is null)
throw new Exception("Invalid tool type ID");
using DbConnection conn = GetDbConnection();
string sql =
$"SELECT TOP 1 AttachmentID FROM [{tt.HeaderTableName}] WHERE Title = @Title ORDER BY InsertDate DESC";
return conn.ExecuteScalar<Guid>(sql, param: new { Title = title });
}
public Guid GetDataAttachmentIDByTitle(int toolTypeId, string title)
{
ToolType tt = GetToolTypeByID(toolTypeId);
if (tt is null)
throw new Exception("Invalid tool type ID");
using DbConnection conn = GetDbConnection();
string sql =
$"SELECT TOP 1 AttachmentID FROM [{tt.DataTableName}] WHERE Title = @Title ORDER BY InsertDate DESC";
return conn.ExecuteScalar<Guid>(sql, param: new { Title = title });
}
DataTable IMetrologyRepository.GetDataSharePoint(int toolTypeId, string headerId) => throw new NotImplementedException();
}

View File

@ -0,0 +1,56 @@
using OI.Metrology.Shared.Models.Stateless;
using OI.Metrology.Shared.ViewModels;
using Serilog.Context;
using System.Text.Json;
namespace OI.Metrology.Server.Repository;
public class ServiceShopOrderRepository : IServiceShopOrderRepository
{
private readonly Serilog.ILogger _Log;
public ServiceShopOrderRepository() => _Log = Serilog.Log.ForContext<ServiceShopOrderRepository>();
private static ServiceShopOrder[] GetServiceShopOrders(Shared.Models.ServiceShop? serviceShop)
{
ServiceShopOrder[] result = IServiceShopOrder.GetServiceShopOrders(serviceShop);
return result;
}
private static Shared.Models.ServiceShop? GetServiceShopOrders()
{
Shared.Models.ServiceShop? result;
string jsonFile;
string checkDirectory = Path.Combine(AppContext.BaseDirectory, "Data/Mike");
if (Directory.Exists(checkDirectory))
jsonFile = Path.Combine(checkDirectory, "service-shop.json");
else
jsonFile = Path.Combine(AppContext.BaseDirectory, "service-shop.json");
string json = File.ReadAllText(jsonFile);
result = JsonSerializer.Deserialize<Shared.Models.ServiceShop>(json);
return result;
}
async Task<ServiceShopOrder[]> IServiceShopOrderRepository.GetAllServiceShopOrders()
{
ServiceShopOrder[] results;
string? methodName = IMethodName.GetActualAsyncMethodName();
using (LogContext.PushProperty("MethodName", methodName))
{
_Log.Debug("() => ...");
Shared.Models.ServiceShop? serviceShop = await Task.Run(GetServiceShopOrders);
results = GetServiceShopOrders(serviceShop);
}
return results.ToArray();
}
async Task<ServiceShopOrder[]> IServiceShopOrderRepository.GetServiceShopOrders(string id)
{
ServiceShopOrder[] results;
Shared.Models.ServiceShop? serviceShop = await Task.Run(GetServiceShopOrders);
results = GetServiceShopOrders(serviceShop).Where(l => l.Id == id).ToArray();
return results;
}
}

View File

@ -0,0 +1,174 @@
using OI.Metrology.Shared.DataModels;
using OI.Metrology.Shared.Models.Stateless;
using OI.Metrology.Shared.Services;
using System.Data;
namespace OI.Metrology.Server.Repository;
public class ToolTypesRepository : IToolTypesRepository
{
private readonly Serilog.ILogger _Log;
public ToolTypesRepository() => _Log = Serilog.Log.ForContext<ToolTypesRepository>();
// Get a list of tooltypes, returns just Name and ID
Result<ToolTypeNameId[]> IToolTypesRepository.Index(IMetrologyRepository metrologyRepository)
{
ToolTypeNameId[] toolTypeNameIdCollection = metrologyRepository.GetToolTypes().Select(tt => new ToolTypeNameId() { ToolTypeName = tt.ToolTypeName, ID = tt.ID }).ToArray();
Result<ToolTypeNameId[]> r = new()
{
Results = toolTypeNameIdCollection,
TotalRows = toolTypeNameIdCollection.Length,
};
return r;
}
// Gets the metadata for a tooltype, accepts a parameter which sorts the results
// This is used to setup the grid displays on the UI
Result<ToolTypeMetadataResult> IToolTypesRepository.GetToolTypeMetadata(IMetrologyRepository metrologyRepository, int id, string sortby)
{
ToolType tt = metrologyRepository.GetToolTypeByID(id);
IEnumerable<ToolTypeMetadata> md = metrologyRepository.GetToolTypeMetadataByToolTypeID(id);
if (string.Equals(sortby, "grid", StringComparison.OrdinalIgnoreCase))
md = md.OrderBy(f => f.GridDisplayOrder).ToList();
if (string.Equals(sortby, "table", StringComparison.OrdinalIgnoreCase))
md = md.OrderBy(f => f.GridDisplayOrder).ToList();
ToolTypeMetadataResult toolTypeMetadataResult = new(tt, md.ToArray());
int totalRows = toolTypeMetadataResult.Metadata is not null ? toolTypeMetadataResult.Metadata.Length : 0;
Result<ToolTypeMetadataResult> r = new()
{
Results = toolTypeMetadataResult,
TotalRows = totalRows,
};
return r;
}
// Gets headers, request/response format is to allow paging with igniteUI
// The headerid parameter is used for navigating directly to a header, when the button is clicked in Awaiting Dispo
Result<DataTable> IToolTypesRepository.GetHeaders(IMetrologyRepository metrologyRepository, int id, DateTime? datebegin, DateTime? dateend, int? page, int? pagesize, long? headerid)
{
long totalRecs;
DataTable dataTable = metrologyRepository.GetHeaders(id, datebegin, dateend, page, pagesize, headerid, out totalRecs);
Result<DataTable> r = new()
{
Results = dataTable,
TotalRows = totalRecs,
};
return r;
}
// Gets header titles, used in the Run Headers UI
Result<HeaderCommon[]> IToolTypesRepository.GetHeaderTitles(IMetrologyRepository metrologyRepository, int id, int? page, int? pagesize)
{
long totalRecs;
HeaderCommon[] headerCommonCollection = metrologyRepository.GetHeaderTitles(id, page, pagesize, out totalRecs).ToArray();
Result<HeaderCommon[]> r = new()
{
Results = headerCommonCollection,
TotalRows = totalRecs,
};
return r;
}
// Get all of the fields for a header, used with the Run Header UI
Result<ColumnValue[]> IToolTypesRepository.GetHeaderFields(IMetrologyRepository metrologyRepository, int id, long headerid)
{
ColumnValue[] columnValueCollection = metrologyRepository.GetHeaderFields(id, headerid).Select(x => new ColumnValue() { Column = x.Key, Value = x.Value }).ToArray();
Result<ColumnValue[]> r = new()
{
Results = columnValueCollection,
TotalRows = columnValueCollection.Length,
};
return r;
}
// Get the data for a header, used with the Run Info UI
Result<DataTable> IToolTypesRepository.GetData(IMetrologyRepository metrologyRepository, int id, long headerid)
{
DataTable dataTable = metrologyRepository.GetData(id, headerid);
Result<DataTable> r = new()
{
Results = dataTable,
TotalRows = dataTable.Rows.Count,
};
return r;
}
// Display an attachment, used for Run Info - note it is by tool type ID and attachment GUID, so it is best for internal use
(string?, string?, Stream?) IToolTypesRepository.GetAttachment(IMetrologyRepository metrologyRepository, IAttachmentsService attachmentsService, int toolTypeId, string tabletype, string attachmentId, string filename)
{
Stream? stream = null;
string? message = null;
Guid attachmentIdParsed;
string? contenttype = null;
ToolType tt = metrologyRepository.GetToolTypeByID(toolTypeId);
bool header = !string.Equals(tabletype.Trim(), "data", StringComparison.OrdinalIgnoreCase);
if (!Guid.TryParse(attachmentId, out attachmentIdParsed))
message = "Invalid attachment id";
else
{
try
{
// figure out what content type to use. this is very simple because there are only two types being used
contenttype = "application/pdf";
if (filename.ToLower().TrimEnd().EndsWith(".txt"))
contenttype = "text/plain";
// Get attachment stream and feed it to the client
stream = attachmentsService.GetAttachmentStreamByAttachmentId(tt, header, attachmentIdParsed, filename);
}
catch (Exception ex) { message = ex.Message; }
}
return new(message, contenttype, stream);
}
// This endpoint triggers writing of the OI Export file
Exception? IToolTypesRepository.OIExport(IMetrologyRepository metrologyRepository, string oiExportPath, int toolTypeId, long headerid)
{
Exception? result = null;
// Call the export stored procedure
_Log.Debug($"Exporting to <{oiExportPath}>");
DataSet ds = metrologyRepository.GetOIExportData(toolTypeId, headerid);
try
{
// The SP must return 3 result tables
if (ds.Tables.Count != 3)
throw new Exception("Error exporting, invalid results");
// The first table has just one row, which is the export filename
if (ds.Tables[0].Rows.Count != 1)
throw new Exception("Error exporting, invalid filename");
string? filename = Convert.ToString(ds.Tables[0].Rows[0][0]);
// The second table has the header data
if (ds.Tables[1].Rows.Count != 1)
throw new Exception("Error exporting, invalid header data");
System.Text.StringBuilder sb = new();
foreach (object? o in ds.Tables[1].Rows[0].ItemArray)
{
if ((o is not null) && (!Convert.IsDBNull(o)))
_ = sb.Append(Convert.ToString(o));
_ = sb.Append('\t');
}
// The third table has the detail data
foreach (DataRow dr in ds.Tables[2].Rows)
{
foreach (object? o in dr.ItemArray)
{
if ((o is not null) && (!Convert.IsDBNull(o)))
_ = sb.Append(Convert.ToString(o));
_ = sb.Append('\t');
}
}
_ = sb.AppendLine();
// The output file will only have one line, the header columns are output first
// Then each detail rows has it's columns appended
// H1, H2, H3, D1.1, D1.2, D1.3, D2.1, D2.2, D2.3, etc
// Write the file
File.WriteAllText(Path.Join(oiExportPath, filename), sb.ToString());
}
catch (Exception ex) { result = ex; }
return result;
}
}