Added a Library Document Usage report

added methods to create a library document usage report
This commit is contained in:
2011-07-19 20:14:50 +00:00
parent dd98679139
commit 8b1498080a
4 changed files with 386 additions and 30 deletions

View File

@@ -5,14 +5,22 @@ using VEPROMS.CSLA.Library;
using iTextSharp.text.pdf;
using iTextSharp.text;
using System.IO;
using VEPROMS.CSLA.Library;
using System.Text.RegularExpressions;
//using Volian.Generic;
namespace Volian.Print.Library
{
public class PDFReport
{
public enum ReportType
{
SearchResults = 0,
LibraryDocUsage = 1,
ReferencedObjectsUsage = 2,
TransitionUsage = 3
}
private ICollection<ItemInfo> _ResultList;
public ICollection<ItemInfo> ResultList
{
@@ -50,12 +58,32 @@ namespace Volian.Print.Library
get { return _SearchString; }
set { _SearchString = VEPROMS.CSLA.Library.ItemInfo.ConvertToDisplayText(value); }
}
private DocumentInfoList _LibDocList;
public DocumentInfoList LibDocList
{
get { return _LibDocList; }
set { _LibDocList = value; }
}
private int _ReportType = 0;
private string _ByLine;
public PDFReport(string reportTitle, string typesSelected, ICollection<ItemInfo> resultList, string fileName)
{
_ResultList = resultList;
_FileName = fileName;
_ReportTitle = VEPROMS.CSLA.Library.ItemInfo.ConvertToDisplayText(reportTitle);
_TypesSelected = VEPROMS.CSLA.Library.ItemInfo.ConvertToDisplayText(typesSelected);
_ReportType = (int)ReportType.SearchResults;
_ByLine = "PROMS Search Results";
}
public PDFReport(string reportTitle, DocumentInfoList libDocList, string fileName)
{
_LibDocList = libDocList;
_FileName = fileName;
_ReportTitle = VEPROMS.CSLA.Library.ItemInfo.ConvertToDisplayText(reportTitle);
_ReportType = (int)ReportType.LibraryDocUsage;
_ByLine = "Generated By PROMS";
}
//public void Build2()
//{
@@ -163,7 +191,7 @@ namespace Volian.Print.Library
// cell.BackgroundColor = bgColor;
// datatable.AddCell(cell);
//}
private string AddGroup(PdfPTable datatable, string dvPath, string lastDvPath, Font f2, bool StripLast, Color bgColor)
private string AddGroup(PdfPTable datatable, string dvPath, string lastDvPath, Font f2, bool StripLast, Color bgColor, bool skipFirst)
{
int level = 0;
string retval = "";
@@ -174,13 +202,14 @@ namespace Volian.Print.Library
string sep = "";
bool foundMisMatch = false;
int n = NewPath.Length;
int jstart = (skipFirst) ? 1 : 0;
if (StripLast)
{
retval = NewPath[--n];
if (retval.Contains("\x11"))
retval = retval.Substring(0, retval.IndexOf("\x11"));
}
for (int j = 0; j < n; j++)
for (int j = jstart; j < n; j++)
{
if (NewPath[j] != "" && (foundMisMatch || OldPath.Length < j + 1 || NewPath[j] != OldPath[j]))
{
@@ -200,13 +229,104 @@ namespace Volian.Print.Library
}
return retval;
}
private string GetCurProcNum(string path)
{
string[] NewPath = path.Split("\x7".ToCharArray());
return NewPath[1];
}
private int FindSpitLevel()
{
int level = 0;
bool foundMisMatch = false;
string lastPath = "";
foreach (string p in ProcSetList)
{
string[] NewPath = p.Split("\x7".ToCharArray());
if (lastPath != "")
{
string[] OldPath = lastPath.Split("\x7".ToCharArray());
int n = NewPath.Length;
int lvl = 0;
foundMisMatch = false;
for (int j = 0; j < n && !foundMisMatch; j++)
{
if (NewPath[j] != "" && (OldPath.Length < j + 1 || NewPath[j] != OldPath[j]))
foundMisMatch = true;
lvl++;
}
if (level == 0) level = lvl;
if (foundMisMatch)
level = Math.Min(level, lvl);
}
lastPath = p;
if (ProcSetList.Count == 1)
level = NewPath.Length + 1;
}
if (level > 0) level--;
return level;
}
public void AddMainPathGroup(PdfPTable datatable, string dvPath, int splitLevel, Font f2, Color bgColor, int colSpan)
{
int level = 0;
StringBuilder sb = new StringBuilder();
string[] NewPath = dvPath.Split("\x7".ToCharArray());
string sep = "";
int n = NewPath.Length;
for (int j = 1; j < n && j < splitLevel; j++)
{
sb.Append(sep + "".PadLeft(2 * level) + NewPath[j].Replace("\x11", " ")).Replace(@"\u8209?", "-");
sep = "\r\n";
level++;
}
if (sb.ToString() != "")
{
PdfPCell cell = new PdfPCell(new Phrase(sb.ToString(), f2));
cell.Colspan = colSpan;
cell.BackgroundColor = bgColor;
cell.BorderWidthTop = 1;
datatable.AddCell(cell);
}
}
public void AddSubPathGroup(PdfPTable datatable, string dvPath, int splitLevel, Font f2, Color bgColor, int colSpan)
{
int level = 0;
StringBuilder sb = new StringBuilder();
string[] NewPath = dvPath.Split("\x7".ToCharArray());
string sep = "";
int n = NewPath.Length;
if (splitLevel < n)
{
for (int j = splitLevel; j < n; j++)
{
sb.Append(sep + "".PadLeft(2 * level) + NewPath[j].Replace("\x11", " ")).Replace(@"\u8209?", "-");
sep = "\r\n";
level++;
}
if (sb.ToString() != "")
{
PdfPCell cell = new PdfPCell(new Phrase(sb.ToString(), f2));
cell.Colspan = colSpan;
cell.BackgroundColor = bgColor;
cell.BorderWidthTop = 1;
datatable.AddCell(cell);
}
}
}
public void Build()
{
iTextSharp.text.Document document = new iTextSharp.text.Document(PageSize.LETTER, 36, 36, 36, 36);
if (!CreateResultsPDF(document)) return;
try
{
BuildTable1(document);
switch (_ReportType) // set in the PDFReport constructor
{
case (int)ReportType.SearchResults:
BuildSearchResultsTable(document);
break;
case (int)ReportType.LibraryDocUsage:
BuildLibDocUsageReport(document);
break;
}
}
catch (Exception ex)
{
@@ -247,7 +367,7 @@ namespace Volian.Print.Library
try
{
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(fileName, FileMode.Create));
writer.PageEvent = new MyPageHelper(); // JSJ - 7/8/2011
writer.PageEvent = new MyPageHelper(_ByLine); // JSJ - 7/8/2011
document.SetMargins(36, 36, 36, 36);
document.Open();
_FileName = fileName;
@@ -291,11 +411,12 @@ namespace Volian.Print.Library
/// Use Cells within Cells to see if I can limit the way Cells break from Page to Page
/// </summary>
/// <param name="document"></param>
private void BuildTable1(iTextSharp.text.Document document)
private void BuildSearchResultsTable(iTextSharp.text.Document document)
{
float[] headerwidths = { 20, 80 };
PdfPTable datatable = new PdfPTable(headerwidths);
datatable.HeaderRows = 3;
PdfPTable datatable = new PdfPTable(1);
PdfPTable colHeader = new PdfPTable(headerwidths);
datatable.HeaderRows = 4;
//datatable.FooterRows = 1;
datatable.TotalWidth = document.PageSize.Width - document.LeftMargin - document.RightMargin;
datatable.LockedWidth = true;
@@ -309,19 +430,27 @@ namespace Volian.Print.Library
//iTextSharp.text.Font f3 = pdf.GetFont("Prestige Elite Tall", 9, 0, Color.BLACK);
PdfPCell cell = new PdfPCell(new Phrase(ReportTitle, f1));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.Colspan = 2;
//cell.Colspan = 2;
cell.BackgroundColor = new Color(0xD0, 0xF0, 0xF0);
datatable.AddCell(cell);
cell = new PdfPCell(new Phrase(TypesSelected,f3));
cell.HorizontalAlignment = Element.ALIGN_LEFT;
cell.Colspan = 2;
//cell.Colspan = 2;
cell.BackgroundColor = new Color(0xD0, 0xF0, 0xF0);
datatable.AddCell(cell);
BuildSearchResultsProcSetList();
int splitAt = FindSpitLevel(); // find the split level of the common path - for all procedure sets that use these library documents
AddMainPathGroup(datatable, ProcSetList[0].ToString(), splitAt, f2, Color.LIGHT_GRAY, 0);
Color subHeaderColor = new Color(0xD0, 0xF0, 0xD0);
datatable.DefaultCell.Padding = 4;
datatable.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
AddCell(datatable, "Step", f2, subHeaderColor);
AddCell(datatable, "Text", f2, subHeaderColor);
//AddCell(datatable, "Step", f2, subHeaderColor);
//AddCell(datatable, "Text", f2, subHeaderColor);
AddCell(colHeader, "Step", f2, subHeaderColor);
AddCell(colHeader, "Text", f2, subHeaderColor);
datatable.AddCell(colHeader);
// This puts "Step" and "Text" column headers at the bottom of the page
//AddCell(datatable, "Step", f2, subHeaderColor);
//AddCell(datatable, "Text", f2, subHeaderColor);
@@ -330,29 +459,47 @@ namespace Volian.Print.Library
string lastPath = "";
Color AnnoColor = new Color(0xFF, 0xFF, 0xC0);
Color TextColor = Color.WHITE;
Color SectColor = Color.LIGHT_GRAY;
Color SectColor = new Color(0xE0, 0xFF, 0xE0);//new Color(0xC0, 0xFF, 0xC0);//Color.LIGHT_GRAY;
PdfPTable subTable = new PdfPTable(headerwidths);
string lastProcNum = "";
foreach (ItemInfo item in _ResultList)
{
if (item.SearchDVPath != lastDvPath)
AddGroup(datatable, item.SearchDVPath, lastDvPath, f2, false, new Color(0xC0, 0xFF, 0xC0));
string curProcNum = GetCurProcNum(item.SearchPath);
if (lastProcNum != "" && curProcNum != lastProcNum)
{
datatable.AddCell(subTable);
subTable = new PdfPTable(headerwidths);
}
lastProcNum = curProcNum;
//if (item.SearchDVPath != lastDvPath)
// AddGroup(datatable, item.SearchDVPath, lastDvPath, f2, false, Color.LIGHT_GRAY, true);
//AddGroup(datatable, item.SearchDVPath, lastDvPath, f2, false, new Color(0xC0, 0xFF, 0xC0), true);
lastDvPath = item.SearchDVPath;
string stepPath = AddGroup(datatable, item.SearchPath, lastPath, f2, true, new Color(0xE0, 0xFF, 0xE0));
//string stepPath = AddGroup(datatable, item.SearchPath, lastPath, f2, true, new Color(0xE0, 0xFF, 0xE0), true);
//string stepPath = AddGroup(datatable, item.SearchPath, lastPath, f2, true, new Color(0xC0, 0xFF, 0xC0), true);
string stepPath = AddGroup(subTable, item.SearchPath, lastPath, f2, true, new Color(0xC0, 0xFF, 0xC0), true);
lastPath = item.SearchPath;
AddCell(datatable, stepPath, f2, (item.IsSection ? SectColor : TextColor));
//AddCell(datatable, stepPath, f2, (item.IsSection ? SectColor : TextColor));
AddCell(subTable, stepPath, f2, (item.IsSection ? SectColor : TextColor));
// This was for the old 16-bit style of table - jsj 7/7/2011
//if (item.DisplayText.Contains("|")) // Need a better way to determine if it is a table.
// AddCell(datatable, item.DisplayText, f3, TextColor);
//else
AddCell(datatable, item.DisplayText, f2, (item.IsSection ? SectColor : TextColor));
if (item.ItemAnnotationCount > 0)
//AddCell(datatable, item.DisplayText, f2, (item.IsSection ? SectColor : TextColor));
AddCell(subTable, item.DisplayText, f2, (item.IsSection ? SectColor : TextColor));
if (item.ItemAnnotationCount > 0)
{
foreach (AnnotationInfo ai in item.ItemAnnotations)
{
AddCell(datatable, ai.MyAnnotationType.Name, f2, AnnoColor);
AddCell(datatable, ai.SearchText, f2, AnnoColor);
//AddCell(datatable, ai.MyAnnotationType.Name, f2, AnnoColor);
//AddCell(datatable, ai.SearchText, f2, AnnoColor);
AddCell(subTable, ai.MyAnnotationType.Name, f2, AnnoColor);
AddCell(subTable, ai.SearchText, f2, AnnoColor);
}
}
}
datatable.AddCell(subTable);
document.Add(datatable);
}
private void AddCell(PdfPTable datatable, string str, iTextSharp.text.Font fnt, Color bgColor)
@@ -365,6 +512,18 @@ namespace Volian.Print.Library
datatable.AddCell(cell);
}
private void AddColSpanCell(PdfPTable datatable, string str, iTextSharp.text.Font fnt, Color bgColor, int span, int txtAlign)
{
iTextSharp.text.Font fntBold = new Font(fnt.BaseFont, fnt.Size, Font.BOLD, Color.RED);
Phrase p = BuildPhrase2(str, fnt, fntBold);
PdfPCell cell = new PdfPCell(p);
cell.PaddingBottom = 4;
cell.BackgroundColor = bgColor;
cell.Colspan = span;
cell.HorizontalAlignment = txtAlign;
datatable.AddCell(cell);
}
private Phrase BuildPhrase(string str, Font fnt, Font fntBold)
{
Phrase p = new Phrase();
@@ -421,14 +580,148 @@ namespace Volian.Print.Library
p.Add(new Phrase(str.Substring(lastIndex), fnt));
return p;
}
private void BuildLibDocUsageReport(iTextSharp.text.Document document)
{
float[] headerwidths = { 20, 80 };
PdfPTable datatable = new PdfPTable(1);// (headerwidths);
PdfPTable colheader = new PdfPTable(headerwidths);
datatable.HeaderRows = 3;
datatable.TotalWidth = document.PageSize.Width - document.LeftMargin - document.RightMargin;
datatable.LockedWidth = true;
iTextSharp.text.Font f1 = pdf.GetFont("Arial Unicode MS", 14, 1, Color.BLACK);
iTextSharp.text.Font f2 = pdf.GetFont("Arial Unicode MS", 10, 0, Color.BLACK);
iTextSharp.text.Font f3 = pdf.GetFont("Arial Unicode MS", 12, 0, Color.BLACK);
PdfPCell cell = new PdfPCell(new Phrase(ReportTitle, f1));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
//cell.Colspan = 2;
cell.BackgroundColor = new Color(0xD0, 0xF0, 0xF0);
datatable.AddCell(cell);
BuildLibDocProcSetList();
int splitAt = FindSpitLevel(); // find the split level of the common path - for all procedure sets that use these library documents
AddMainPathGroup(datatable, ProcSetList[0].ToString(), splitAt, f2, Color.LIGHT_GRAY, 0);//2);
Color subHeaderColor = new Color(0xD0, 0xF0, 0xD0);
datatable.DefaultCell.Padding = 4;
datatable.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
//AddCell(datatable, "Procedure", f2, subHeaderColor);
//AddCell(datatable, "Section", f2, subHeaderColor);
AddCell(colheader, "Procedure", f2, subHeaderColor);
AddCell(colheader, "Section", f2, subHeaderColor);
datatable.AddCell(colheader);
datatable.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT;
string lastDvPath = "";
Color AnnoBackgroundColor = new Color(0xFF, 0xFF, 0xC0);
Color TextBackgroundColor = Color.WHITE;
Color NotUsedBackgroundColor = Color.WHITE;
//BuildProcSetList();
//int splitAt = FindSpitLevel(); // find the split level of the common path - for all procedure sets that use these library documents
//bool doMainProcSetpath = true;
foreach (DocumentInfo di in _LibDocList)
{
PdfPTable subtable = new PdfPTable(headerwidths);
subtable.HeaderRows = 0;
//if (doMainProcSetpath)
//{
// AddMainPathGroup(datatable, ProcSetList[0].ToString(), splitAt, f2, Color.LIGHT_GRAY, 0);//2);
// doMainProcSetpath = false;
//}
//AddGroup(datatable, di.DocumentTitle, "", f2, false, new Color(0xC0, 0xFF, 0xC0)); // Library Document Title
AddGroup(subtable, di.DocumentTitle, "", f2, false, new Color(0xC0, 0xFF, 0xC0), false); // Library Document Title
if (di.DocumentConfig.LibDoc_Comment != null && di.DocumentConfig.LibDoc_Comment.Length > 0)
{
//AddCell(datatable, "Comment", f2, AnnoBackgroundColor);
//AddCell(datatable, di.DocumentConfig.LibDoc_Comment, f2, AnnoBackgroundColor); // lib doc comment
AddCell(subtable, "Comment", f2, AnnoBackgroundColor);
AddCell(subtable, di.DocumentConfig.LibDoc_Comment, f2, AnnoBackgroundColor); // lib doc comment
}
if (di.DocumentEntries == null || di.DocumentEntries.Count == 0)
{
//AddColSpanCell(datatable, "*No References To This Library Document", f2, NotUsedBackgroundColor, 2, Element.ALIGN_LEFT);
AddColSpanCell(subtable, "*No References To This Library Document", f2, NotUsedBackgroundColor, 2, Element.ALIGN_LEFT);
}
else
{
System.Collections.ArrayList LibDocUsageList = GetSortedItemInfoList(di.LibraryDocumentUsageList);
lastDvPath = "";
foreach (ItemInfo item in LibDocUsageList)//di.LibraryDocumentUsageList)
{
string ProcNumber = item.MyProcedure.ProcedureConfig.Number;
ProcNumber = ProcNumber.Replace(@"\u8209?", "-"); // repace unicode with dash char
ProcNumber = ProcNumber.Replace(@"\u160?", " "); // replace Hard Space with normal Space
//if (item.SearchDVPath != lastDvPath)
// AddGroup(datatable, item.SearchDVPath, lastDvPath, f2, false, Color.LIGHT_GRAY);//new Color(0xC0, 0xFF, 0xC0));
//if (item.SearchDVPath != lastDvPath)
// AddSubPathGroup(datatable, item.SearchDVPath,splitAt, f2, Color.LIGHT_GRAY, 2);//new Color(0xC0, 0xFF, 0xC0));
if (item.SearchDVPath != lastDvPath)
AddSubPathGroup(subtable, item.SearchDVPath, splitAt, f2, Color.LIGHT_GRAY, 2);//new Color(0xC0, 0xFF, 0xC0));
lastDvPath = item.SearchDVPath;
//AddCell(datatable, ProcNumber, f2, TextBackgroundColor);
//AddCell(datatable, item.MyContent.Number + " - " + item.MyContent.Text, f2, TextBackgroundColor);
AddCell(subtable, ProcNumber, f2, TextBackgroundColor);
AddCell(subtable, item.MyContent.Number + " - " + item.MyContent.Text, f2, TextBackgroundColor);
}
}
datatable.AddCell(subtable);
}
document.Add(datatable);
}
private System.Collections.ArrayList ProcSetList;
private void BuildSearchResultsProcSetList()
{
ProcSetList = new System.Collections.ArrayList();
foreach (ItemInfo item in _ResultList)
{
if (!ProcSetList.Contains(item.SearchDVPath))
ProcSetList.Add(item.SearchDVPath);
}
}
private void BuildLibDocProcSetList()
{
ProcSetList = new System.Collections.ArrayList();
foreach (DocumentInfo di in _LibDocList)
{
if (di.LibraryDocumentUsageList!= null && di.LibraryDocumentUsageList.Count > 0)
foreach (ItemInfo item in di.LibraryDocumentUsageList)
{
if (!ProcSetList.Contains(item.SearchDVPath))
ProcSetList.Add(item.SearchDVPath);
}
}
}
private System.Collections.ArrayList GetSortedItemInfoList(ItemInfoList orgIFL)
{
//ICollection<ItemInfo> rtnIFL;
//get list of different procedure sets
System.Collections.ArrayList libDocProcSetList = new System.Collections.ArrayList();
System.Collections.ArrayList rtnIFL = new System.Collections.ArrayList();
foreach (ItemInfo item in orgIFL)
{
if (!libDocProcSetList.Contains(item.SearchDVPath))
libDocProcSetList.Add(item.SearchDVPath);
}
foreach (string pset in libDocProcSetList)
{
foreach (ItemInfo item in orgIFL)
{
if (item.SearchDVPath.Equals(pset))
rtnIFL.Add(item);
}
}
return rtnIFL;
}
}
public class MyPageHelper : PdfPageEventHelper
{
protected PdfTemplate total;
protected BaseFont helv;
private int ptSize = 10; // text point size of page number and page total
string _ByLine = "PROMS";
public MyPageHelper(string byLine)
{
_ByLine = byLine;
}
public override void OnOpenDocument(PdfWriter writer, iTextSharp.text.Document document)
{
total = writer.DirectContent.CreateTemplate(100, 100);
@@ -482,7 +775,7 @@ namespace Volian.Print.Library
{
PdfContentByte cb = writer.DirectContent;
cb.SaveState();
String text = "PROMS Search Results";
String text = _ByLine;//"PROMS Search Results";
float textBase = document.Bottom - 19;//20;
float textSize = helv.GetWidthPoint(text, ptSize);
cb.BeginText();