secret
Last active

SpoolToOutput #2

  • Download Gist
Program.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using iTextSharp.text;
using iTextSharp.text.pdf;
 
namespace OrchardSpoolToOutput
{
class Program
{
//LIVE
//const string SpoolLocation = @"\\sun01\live_var\ra\spl\ehistpr\";
//const string OutputLocation = @"\\san\common\OrchardSpoolToPDF\";
 
static int NumberGenerated = 0;
const string SpoolLocation = @"C:\Users\Carl\Dropbox\Visual Studio 2010\Projects\OrchardSpoolToOutput\OrchardSpoolToOutput\ehistpr\";
 
static void Main(string[] args)
{
CheckForNewSpoolFiles();
 
if (NumberGenerated == 0)
{
Console.WriteLine("No files to process");
}
else
{
Console.WriteLine("Complete. Generated {0} PDFs", NumberGenerated);
}
 
Console.ReadLine();
}
 
private static void CheckForNewSpoolFiles()
{
//Get the filename of the last spoolfile to be converted.
//The files are sequentially named text docs, output from Orchard Housing, with no file extension
int LastSpoolRead = Convert.ToInt32(GetMostRecentSpoolFile());
string[] SpoolDirectory = Directory.GetFiles(SpoolLocation);
 
foreach (string File in SpoolDirectory)
{
string SpoolFileName = Path.GetFileNameWithoutExtension(File);
 
if (Convert.ToInt32(SpoolFileName) > LastSpoolRead)
{
SpoolFile ehistpr = new SpoolFile(File);
ehistpr.GeneratePDF();
ehistpr.InsertSpoolData();
NumberGenerated++;
}
}
}
 
static string GetMostRecentSpoolFile()
{
string FileName;
using (SqlConnection Connection = new SqlConnection(@"Data Source=localhost\sql2k8;Initial Catalog=OrchardSpool;Integrated Security=SSPI;"))
{
try
{
Connection.Open();
SqlCommand Command = new SqlCommand("Get_MostRecentSpoolFile", Connection);
Command.CommandType = CommandType.StoredProcedure;
FileName = Command.ExecuteScalar().ToString();
return FileName;
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
return null;
}
}
}
}
}
SpoolFile.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using iTextSharp.text;
using iTextSharp.text.pdf;
 
namespace OrchardSpoolToOutput
{
class SpoolFile
{
public string UserName;
public string Tenancy;
public string SpoolDate;
public string SpoolTime;
public string SpoolDateText;
public string SpoolTimeText;
public string ReportName;
public string SpoolFileName;
const int UsernameLength = 12;
const int UsernameLineNumber = 1;
const int TenancyRefStart = 20;
const int TenancyRefLength = 7;
const int TenancyRefLineNumber = 5;
const string FileType = @".pdf";
const string OutputLocation = @"C:\Users\Carl\Dropbox\Visual Studio 2010\Projects\OrchardSpoolToOutput\OrchardSpoolToOutput\ehistpr_processed\";
List<string> SpoolFileData = new List<string>();
 
public SpoolFile(string Spool)
{
Load(Spool);
}
 
private void Load(string Spool)
{
using (StreamReader SpoolReader = new StreamReader(Spool))
{
string SpoolLine;
SpoolFileName = Path.GetFileNameWithoutExtension(Spool);
while ((SpoolLine = SpoolReader.ReadLine()) != null)
{
SpoolFileData.Add(SpoolLine);
}
 
//Get various pieces of data from the Spool File. To be used in the filename and in the DB
UserName = SpoolFileData[UsernameLineNumber].Substring(0, UsernameLength).Trim().ToUpper().ToString();
Tenancy = SpoolFileData[TenancyRefLineNumber].Substring(TenancyRefStart, TenancyRefLength).ToString();
SpoolDate = SpoolFileData[0].Substring(55, 10).ToString();
SpoolTime = SpoolFileData[1].Substring(57, 8).ToString();
SpoolDateText = SpoolDate.Replace(@"/", "");
SpoolTimeText = SpoolTime.Replace(@":", "");
ReportName = SpoolFileData[0].Substring(0, SpoolFileData[0].IndexOf(" ")).ToUpper().ToString();
 
string Type = SpoolFileData[2].Trim().ToString();
string DatePeriod = SpoolFileData[3].Trim().Replace(@"/", "").ToString();
}
}
 
public void GeneratePDF()
{
//Use iTextSharp to generate a PDF version of the fixed width spool file.
//This is to be named "USERNAME_TENANCYREF_DATE_TIME.pdf"
 
string FileName = String.Concat(UserName, "_", Tenancy, "_", SpoolDateText, "_", SpoolTimeText, FileType);
string FontName = "Courier";
int FontSize = 10;
Font OrchardFont = FontFactory.GetFont(FontName, FontSize);
Document PDF = new Document(PageSize.A4, 20, 20, 10, 10);
try
{
using (FileStream Stream = File.Create(OutputLocation + FileName))
{
PdfWriter Writer = PdfWriter.GetInstance(PDF, Stream);
PDF.Open();
SpoolFileData.ForEach(line => PDF.Add(new Paragraph(line, OrchardFont)));
PDF.Close();
Console.WriteLine("{3}: Spool File {0} - PDF generated for tenancy {1} by user {2}", SpoolFileName, Tenancy, UserName, DateTime.Now.ToString());
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
 
public void InsertSpoolData()
{
using (SqlConnection Connection = new SqlConnection(@"Data Source=localhost\sql2k8;Initial Catalog=OrchardSpool;Integrated Security=SSPI;"))
{
try
{
Connection.Open();
SqlCommand Command = new SqlCommand("Insert_SpoolFileData", Connection);
Command.CommandType = CommandType.StoredProcedure;
Command.Parameters.Add(new SqlParameter("@FileName", SpoolFileName));
Command.Parameters.Add(new SqlParameter("@TenancyReference", Tenancy));
Command.Parameters.Add(new SqlParameter("@UserName", UserName));
Command.Parameters.Add(new SqlParameter("@SpoolDate", SpoolDate));
Command.Parameters.Add(new SqlParameter("@SpoolTime", SpoolTime));
Command.Parameters.Add(new SqlParameter("@ReportName", ReportName));
Command.ExecuteNonQuery();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
finally
{
 
}
}
}
 
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.