Phares Mike (CSC FI SPS MESLEO) f110fba4cd Merged PR 13298: Added just one approval back in after removing the method call from bug 239935
Added just one approval back in after removing the method call from bug 239935

Added IExcelDataReader support into MK Project

Changed instructions below the ECN Title field to align with Windchill

Related work items: #225480, #244087
2025-03-19 21:19:06 +01:00

314 lines
14 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using Fab2ApprovalSystem.DMO;
using Fab2ApprovalSystem.Models;
namespace Fab2ApprovalSystem.Misc;
public class MRBHelper {
public static string NotifyApprovers(AppSettings appSettings, int mrbNumber, List<string> emailIst) {
string emailSentList = "";
string emailTemplate = "MRBAssigned.txt";
string userEmail = string.Empty;
string subject = "MRB Assignment";
string senderName = "MRB";
foreach (string email in emailIst) {
subject = "MRB Assignment";
EmailNotification en = new(appSettings, subject, emailTemplate);
string[] emailparams = new string[3];
emailparams[0] = mrbNumber.ToString();
emailparams[1] = mrbNumber.ToString();
emailparams[2] = GlobalVars.hostURL;
userEmail = email;
en.SendNotificationEmail(emailTemplate, GlobalVars.SENDER_EMAIL, senderName, userEmail, null, subject, emailparams);
emailSentList += email + ",";
}
return emailSentList;
}
public static string ImportRemoveQDBFlag(AppSettings appSettings, MRB_DMO mrbDMO, string operation, out string physicalPath, string userIdentityName, string a, string b, string c, string fullFileName, Stream stream) {
IEnumerable<string> lotDataList = null;
var guid = Guid.NewGuid().ToString();
var fileExtension = Path.GetExtension(fullFileName);
physicalPath = Path.Combine(appSettings.LotTempPipeLine, guid + "." + fileExtension);
using (FileStream fileStream = new(physicalPath, FileMode.Create, FileAccess.Write)) {
stream.CopyTo(fileStream);
}
ExcelData x = new(physicalPath);
lotDataList = x.ReadQDBFlagData();
foreach (string lotData in lotDataList) {
mrbDMO.InsertMRB_QDB_HoldFlag(guid, lotData, operation);
}
FileInfo f = new(physicalPath);
if (f.Exists)
f.Delete();
// Send the data to SPN
if (SendQDBFlagToSPN(appSettings, mrbDMO, guid, userIdentityName, a, b, c))
mrbDMO.UpdateMRB_QDB_HoldFlag(guid, true);
else {
mrbDMO.UpdateMRB_QDB_HoldFlag(guid, false);
return "Problems while uploading to SPN";
}
return string.Empty;
}
public static bool BatchFTP(AppSettings appSettings, string sourceFile, string destFile, string ftpLogDirectory, string userIdentityName, string a, string b, string c) {
FileInfo sourcefile = new(sourceFile);
try {
ProcessStartInfo psiFab1 = new();
Process procFab1 = new();
StringBuilder sb = new();
if (GlobalVars.DBConnection.ToUpper() == "TEST" || GlobalVars.DBConnection.ToUpper() == "QUALITY") {
psiFab1.FileName = a;
} else {
psiFab1.FileName = b;
}
psiFab1.Arguments = sourcefile.FullName + " " + destFile;
psiFab1.RedirectStandardOutput = true;
psiFab1.UseShellExecute = false;
psiFab1.WorkingDirectory = c;
procFab1.StartInfo = psiFab1;
procFab1.OutputDataReceived += (sender, args) => sb.AppendLine(args.Data);
;
procFab1.Start();
procFab1.BeginOutputReadLine();
procFab1.WaitForExit(4000);
File.WriteAllText(Path.Combine(ftpLogDirectory, sourcefile.Name + ".txt"), sb.ToString());
return true;
} catch (Exception e) {
Functions.WriteEvent(appSettings, userIdentityName + "\r\n Approve\r\n" + e.Message.ToString(), EventLogEntryType.Error);
return false;
}
}
public static bool SendQDBFlagToSPN(AppSettings appSettings, MRB_DMO mrbDMO, string guid, string userIdentityName, string a, string b, string c) {
StringBuilder output = new();
try {
IEnumerable<string> data = mrbDMO.GetMRB_QDB_HoldFlags(guid);
foreach (string tempData in data) {
output.Append(tempData.Trim() + Environment.NewLine);
}
try {
if (output.Length > 0) {
DateTime dt = DateTime.Now;
string newsourceFileName = "S" + dt.Day.ToString("00") + dt.Month.ToString("00") + dt.Year.ToString("00") + dt.Hour.ToString("00") + dt.Minute.ToString("00") + dt.Second.ToString("00") + ".mrb";
string newDestFileName = "S" + dt.Hour.ToString("00") + dt.Minute.ToString("00") + dt.Second.ToString("00") + ".mrb";
string outputFile = appSettings.HoldFlagDirectory + newsourceFileName;
File.WriteAllText(outputFile, output.ToString());
#if (DEBUG)
Thread.Sleep(1000);
#endif
try {
if (BatchFTP(appSettings, outputFile, newDestFileName, appSettings.SPNMRBHoldFlagFTPLogDirectory, userIdentityName, a, b, c)) {
mrbDMO.UpdateMRB_QDB_HoldFlag(guid, true);
} else {
mrbDMO.UpdateMRB_QDB_HoldFlag(guid, false);
}
} catch (Exception e) {
string exceptionString = e.Message.ToString().Trim().Length > 500 ? "MRB =" + guid.ToString() + " FTPToSPN(): FTP Upload Error " + e.Message.ToString().Substring(0, 250) : e.Message.ToString();
EventLogDMO.Add(new WinEventLog() { IssueID = -1, UserID = userIdentityName, DocumentType = "MRB", OperationType = "Error", Comments = exceptionString });
return false;
}
}
return true;
} catch (Exception e) {
string exceptionString = e.Message.ToString().Trim().Length > 500 ? "MRB =" + guid.ToString() + " SPN Hold Flag(SendToSPN) " + e.Message.ToString().Substring(0, 250) : e.Message.ToString();
Functions.WriteEvent(appSettings, userIdentityName + "\r\n Approve\r\n" + e.Message.ToString(), EventLogEntryType.Error);
EventLogDMO.Add(new WinEventLog() { IssueID = -1, UserID = userIdentityName, DocumentType = "MRB", OperationType = "Error", Comments = exceptionString });
return false;
}
} catch (Exception e) {
string exceptionString = e.Message.ToString().Trim().Length > 500 ? "GUID =" + guid.ToString() + " SPN Hold Flag(SendToSPN) " + e.Message.ToString().Substring(0, 250) : e.Message.ToString();
Functions.WriteEvent(appSettings, userIdentityName + "\r\n Approve\r\n" + e.Message.ToString(), EventLogEntryType.Error);
EventLogDMO.Add(new WinEventLog() { IssueID = -1, UserID = userIdentityName, DocumentType = "MRB", OperationType = "Error", Comments = exceptionString });
return false;
}
}
public static string ImportAddQDBFlag(AppSettings appSettings, MRB_DMO mrbDMO, string operation, out string physicalPath, string userIdentityName, string a, string b, string c, string fullFileName, Stream stream) {
IEnumerable<string> lotDataList = null;
var guid = Guid.NewGuid().ToString();
var fileExtension = Path.GetExtension(fullFileName);
physicalPath = Path.Combine(appSettings.LotTempPipeLine, guid + "." + fileExtension);
using (FileStream fileStream = new(physicalPath, FileMode.Create, FileAccess.Write)) {
stream.CopyTo(fileStream);
}
ExcelData x = new(physicalPath);
lotDataList = x.ReadQDBFlagData();
foreach (string lotData in lotDataList) {
mrbDMO.InsertMRB_QDB_HoldFlag(guid, lotData, operation);
}
FileInfo f = new(physicalPath);
if (f.Exists)
f.Delete();
if (SendQDBFlagToSPN(appSettings, mrbDMO, guid, userIdentityName, a, b, c))
mrbDMO.UpdateMRB_QDB_HoldFlag(guid, true);
else {
mrbDMO.UpdateMRB_QDB_HoldFlag(guid, false);
return "Problems while uploading to SPN";
}
return string.Empty;
}
public static void AttachSave(AppSettings appSettings, MRB_DMO mrbDMO, int mrbNumber, int userId, string fullFileName, Stream stream) {
// Some browsers send file names with full path.
// We are only interested in the file name.
var fileName = Path.GetFileName(fullFileName);
string physicalFileName;
string physicalPath;
// Check to see if this filename is in use
var attachments = mrbDMO.GetMRBAttachments(mrbNumber);
if (attachments.Count() > 0) {
if (attachments.Count(a => string.Equals(a.FileName, fileName, StringComparison.OrdinalIgnoreCase)) > 0) {
// This filename is used on this MRB
// So we want to delete those records so the new record replaces them
foreach (var a in attachments) {
mrbDMO.DeleteMRBAttachment(a.AttachmentID);
physicalFileName = a.Path;
if (string.IsNullOrEmpty(physicalFileName))
physicalFileName = a.FileName;
physicalPath = Path.Combine(appSettings.AttachmentFolder + "MRB", physicalFileName);
if (File.Exists(physicalPath))
File.Delete(physicalPath);
}
}
}
physicalFileName = mrbNumber.ToString() + "_" + Guid.NewGuid().ToString() + Path.GetExtension(fileName);
physicalPath = Path.Combine(appSettings.AttachmentFolder + "MRB", physicalFileName);
using (FileStream fileStream = new(physicalPath, FileMode.Create, FileAccess.Write)) {
stream.CopyTo(fileStream);
}
MRBAttachment attach = new() {
MRBNumber = mrbNumber,
FileName = fileName,
Path = physicalFileName,
#if (DEBUG)
UserID = 114,
#endif
#if (!DEBUG)
UserID = userId,
#endif
};
mrbDMO.InsertMRBAttachment(attach);
}
public static string ExcelLotOpen(MRB_DMO mrbDMO, int mrbNumber, StringBuilder warnings, IEnumerable<Disposition> dispos, string userIdentityName, string lotTempPipeLine, string fullFileName, Stream stream) {
var fileName = Path.GetFileName(fullFileName);
var fileExtension = Path.GetExtension(fullFileName);
string fName = userIdentityName + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
string physicalPath = Path.Combine(lotTempPipeLine, fName + "." + fileExtension);
IEnumerable<ExcelData.ExcelLotInfo> lotNumbers;
try {
using (FileStream fileStream = new(physicalPath, FileMode.Create, FileAccess.Write)) {
stream.CopyTo(fileStream);
}
ExcelData x = new(physicalPath);
lotNumbers = x.ReadData();
} catch (Exception ex) {
throw new Exception(string.Format("Invalid file format for {0}: {1}", fileName, ex.Message));
}
// Get Tool, Issue Start and End Date
MRB mrbInfo = mrbDMO.GetToolIssueStartEndDateData(mrbNumber, null);
foreach (var lotInfo in lotNumbers) {
if (lotInfo.LotDispo.Length == 1) {
if (dispos.Count(d => d.DispositionType.Trim().ToUpper() == lotInfo.LotDispo.Trim().ToUpper()) == 0) {
throw new Exception(string.Format("Invalid lot disposition {0} for lot no {1}",
lotInfo.LotDispo, lotInfo.LotNo));
}
}
}
// RJK - 12/17
// Only find the child Splits when a Tool or a list of Tools is provided
if (!mrbInfo.ToolCSV.ToUpper().Equals("NA")) {
foreach (var lotInfo in lotNumbers) {
bool existingLotUpdated;
Lot l = new();
l.LotNumber = lotInfo.LotNo ?? string.Empty;
if (lotInfo.LotDispo.Length == 1) {
l.DispoType = lotInfo.LotDispo[0];
}
l.MRBNumber = mrbNumber;
mrbDMO.InsertLot(l, true, out existingLotUpdated);
// cannot do the check below , because what if the parent lot had splits after the prior lot split analysis
if (!mrbDMO.InsertLotSplitsAffectedByIncident(mrbNumber, l.LotNumber, mrbInfo.ToolCSV, mrbInfo.IssueStartDate, mrbInfo.IssueEndDate)) {
warnings.AppendFormat("Lot number {0} is not affected by these tools and issue start/end time.\n Uploaded without Lot Genealogy tracing", l.LotNumber);
}
}
// Not required - Will be using each lot's Insert Date time stamp,
// as lot could be added using search functionality
// UpdateLastLotSplitAnalysisTime(mrbNumber);
} else {
// RJK - 12/17
// Only find the child Splits when a Tool or a list of Tools is provided
foreach (var lotInfo in lotNumbers) {
bool existingLotUpdated;
Lot l = new();
l.LotNumber = lotInfo.LotNo ?? string.Empty;
if (lotInfo.LotDispo.Length == 1) {
l.DispoType = lotInfo.LotDispo[0];
}
l.MRBNumber = mrbNumber;
// do not insert any new lots when importing from excel
mrbDMO.InsertLot(l, true, out existingLotUpdated);
}
}
FileInfo f = new(physicalPath);
if (f.Exists)
f.Delete();
return physicalPath;
}
}