Files
SourceCode/PROMS/VEPROMS User Interface/dlgPrintAllApprovedProcedures.cs

203 lines
7.3 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using VEPROMS.CSLA.Library;
using JR.Utils.GUI.Forms;
namespace VEPROMS
{
public partial class dlgPrintAllApprovedProcedures : DevComponents.DotNetBar.Office2007Form
{
private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private StringBuilder NotApproved;
private DocVersionInfo _DocVersionInfo = null;
private int unitId = 0;
public dlgPrintAllApprovedProcedures(DocVersionInfo dvi)
{
InitializeComponent();
_DocVersionInfo = dvi;
unitId = (_DocVersionInfo.DocVersionConfig.SelectedSlave < 0) ? 0 : _DocVersionInfo.DocVersionConfig.SelectedSlave; // set unitId to zero if not Parent/Child
NotApproved = new StringBuilder();
txbApprovedPDFsPath.Text = BuildInitialPDFPath(); // set to default approved PDF path
}
// create an approved PDFs path based on the user's Documents folder and the tree path to the working draft
private string BuildInitialPDFPath()
{
// start with the SearchDVPath which is the node path staring with the top of the PROMS Procedure tree (VEPROMS)
string rtnstr = _DocVersionInfo.ActiveParent.SearchDVPath;
// remove top tree node (VEPROMS) and put a " - " between each tree node name
rtnstr = rtnstr.Substring(rtnstr.IndexOf("\a") + 1).Replace("\a", " - ");
// add the user's path to the My Documments folder to the start of the path
rtnstr = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\" + rtnstr;
// check if we are printing Approved Child Procedures
// if SelectedSlave is > 0 then we are printing Approved Child Procedures and
// subtract one from the index (unitId) into the list of child names (UnitNames)
if (unitId > 0)
rtnstr += "\\" + _DocVersionInfo.UnitNames[unitId - 1]; // append Child name to path
return rtnstr;
}
private void ppBtnPDFLoc_Click(object sender, EventArgs e)
{
if (txbApprovedPDFsPath.Text.Length > 0)
ApprovedPDFsFolderDlg.SelectedPath = txbApprovedPDFsPath.Text;
DialogResult dr = ApprovedPDFsFolderDlg.ShowDialog();
if (dr == DialogResult.OK)
{
txbApprovedPDFsPath.Text = ApprovedPDFsFolderDlg.SelectedPath;
}
}
private bool FolderIsWritable(string dirPath)
{
try
{
using (FileStream fs = File.Create(Path.Combine(dirPath, Path.GetRandomFileName()), 1, FileOptions.DeleteOnClose))
{ }
return true;
}
catch
{
return false;
}
}
private void btnPrntAllAprv_Click(object sender, EventArgs e)
{
try
{
// Create folder if needed, clear the folder
if (!Directory.Exists(txbApprovedPDFsPath.Text))
Directory.CreateDirectory(txbApprovedPDFsPath.Text);
// check if the folder location is writable
if (!FolderIsWritable(txbApprovedPDFsPath.Text))
{
MessageBox.Show("Cannot Write to this folder.\n\nSelect a different location.", "Invalid Folder Access", MessageBoxButtons.OK, MessageBoxIcon.Error);
btnPrntAllAprv.Enabled = false;
}
else
{
int pdfCount = 0;
DeleteExistingPDFs(); // delete existing PDFs in the target folder
// Get the Child index for Parent/Child procedure - if not Parent/Child this will be zero
foreach (ProcedureInfo myProc in _DocVersionInfo.Procedures)
{
RevisionInfoList ril = RevisionInfoList.GetByItemID(myProc.ItemID);
if (ril.Count == 0)
{
NotApproved.AppendLine(string.Format("No approved Version for {0}", (myProc.PDFNumber.Length > 0) ? myProc.PDFNumber : myProc.DisplayText));
}
else
{
bool foundApproved = false;
foreach (RevisionInfo revinfo in ril)
{
// if not Parent/Child, "unitId" and "Applicability_index" will be zero
if (unitId == revinfo.MyConfig.Applicability_Index)
{
if (revinfo.LatestVersion.MyStage.IsApproved > 0)
{
foundApproved = true;
ItemInfo ii = ItemInfo.Get(revinfo.ItemID);
ii.MyDocVersion.DocVersionConfig.SelectedSlave = unitId;
ProcedureInfo prcInfo = ProcedureInfo.Get(ii.ItemID);
SaveApprovedPDFToFolder(revinfo, prcInfo.PDFNumber);// save PDF to folder
pdfCount++;
break; // got the latest Approved - jump out of foreach revision info loop
}
}
}
if (!foundApproved) NotApproved.AppendLine(string.Format("No approved Version for {0}", myProc.PDFNumber));
}
}
// if no Approved PDFs were save, display general message and exit
if (pdfCount == 0)
{
string msg = "There are no Approved Procedure PDFs in this procedure set.\n\n Approved Procedures PDFs are created when a procedure is approved using the PROMS Approval function.";
FlexibleMessageBox.Show(msg, "Print PDFs Completed", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else // we saved some approved PDFs tell user how many were saved and list procedures that were not approved
{
string msg = string.Format("{0} PDFs were saved at\n\n{1}", pdfCount, txbApprovedPDFsPath.Text);
if (NotApproved.Length > 0)
{
msg += string.Format("\n\n--------------------------------------------\n\nPDFs for the Following were not Generated:\n\n{0}", NotApproved.ToString());
}
FlexibleMessageBox.Show(msg, "Print PDFs Completed", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
catch (Exception ex)
{
_MyLog.Error("Print All Approved PDFs", ex);// save error in PROMS error log
MessageBox.Show(ex.Message, ex.GetType().FullName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
this.Close(); // close dialog
}
private void SaveApprovedPDFToFolder(RevisionInfo revInfo,string PDFName)
{
byte[] buffer = revInfo.LatestVersion.PDF;
string PDFFilePath = string.Format("{0}\\{1}.PDF", txbApprovedPDFsPath.Text, PDFName);
try
{
FileStream fs = new FileStream(PDFFilePath, FileMode.Create);
fs.Write(buffer, 0, buffer.Length);
fs.Close();
}
catch (Exception ex)
{
string str = string.Format("Could not create {0}", PDFFilePath);
NotApproved.AppendLine(str); // save to list of procedure that were not approved
_MyLog.Info(str, ex);// save error in PROMS error log
}
}
private void DeleteExistingPDFs()
{
DirectoryInfo di = new DirectoryInfo(txbApprovedPDFsPath.Text);
FileInfo[] fis;
//DirectoryInfo[] diAry = di.GetDirectories(txbApprovedPDFsPath.Text);
//DirectoryInfo di_fmtgen;
// remove all of the PDF fils
//di_fmtgen = diAry[0];
try
{
fis = di.GetFiles("*.pdf");
foreach (FileInfo fi in fis)
{
if (fi.IsReadOnly) fi.IsReadOnly = false;
fi.Delete();
}
}
catch (Exception ex)
{
string str = string.Format("Cannot delete files in {0}", txbApprovedPDFsPath.Text);
MessageBox.Show(str, "Error deleting files", MessageBoxButtons.OK, MessageBoxIcon.Error);
_MyLog.Info(str, ex); // save error in PROMS error log
}
}
private void txbApprovedPDFsPath_TextChanged(object sender, EventArgs e)
{
btnPrntAllAprv.Enabled = txbApprovedPDFsPath.Text.Length > 0;
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
}
}