123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Core;
- using InABox.DynamicGrid;
- using javax.print.attribute.standard;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- namespace PRSDesktop
- {
- public class StagingSetoutGrid : DynamicDataGrid<StagingSetout>
- {
- List<string> _existingFileNames = new List<string>();
- List<Job> _jobs = new List<Job>();
- public StagingSetoutGrid()
- {
- HiddenColumns.Add(x => x.Setout.ID);
- HiddenColumns.Add(x => x.JobLink.ID);
- Options.Add(DynamicGridOption.FilterRows);
- Options.Add(DynamicGridOption.SelectColumns);
- Options.Add(DynamicGridOption.RecordCount);
- LoadJobs();
- ScanFiles();
- }
- private void LoadJobs()
- {
- CoreTable table = new Client<Job>().Query(null, new Columns<Job>(x => x.ID, x => x.JobNumber, x => x.Name));
- if (table.Rows.Any())
- foreach (CoreRow row in table.Rows)
- _jobs.Add(row.ToObject<Job>());
- }
- public void ScanFiles()
- {
- IEnumerable<FileInfo> files = GetFiles();
- LoadExistingStaging();
- List<Document> documents = new List<Document>();
- List<StagingSetoutDocument> stagingdocs = new List<StagingSetoutDocument>();
- foreach (var file in files)
- {
- if (!file.FullName.EndsWith(".pdf"))
- continue;
- if (CheckStagingSetoutExists(file))
- continue;
- documents.Add(ReadFile(file));
- }
- new Client<Document>().Save(documents, "Created from sync setout function");
- var messages = new List<string>();
- foreach (var doc in documents)
- {
- var stagingsetout = new StagingSetout();
- stagingsetout.Number = doc.FileName.Substring(0, doc.FileName.Length - 4);
- stagingsetout.JobLink.ID = FindJob(doc.FileName.Substring(0, 4));
- new Client<StagingSetout>().Save(stagingsetout, "Created from sync setout function");
- var stagingsetoutdoc = new StagingSetoutDocument();
- stagingsetoutdoc.EntityLink.ID = stagingsetout.ID;
- stagingsetoutdoc.DocumentLink.ID = doc.ID;
- stagingdocs.Add(stagingsetoutdoc);
- if (stagingsetout.JobLink.ID == Guid.Empty)
- messages.Add(doc.FileName);
- }
- if (messages.Any())
- MessageBox.Show("Job(s) not found for the following document(s): " + string.Join("," + Environment.NewLine, messages), "Warning");
- new Client<StagingSetoutDocument>().Save(stagingdocs, "Created from sync setout function");
- }
- private Guid FindJob(string filePreFix)
- {
- var job = _jobs.FirstOrDefault(x => x.JobNumber.Equals(filePreFix));
- if (job == null)
- return Guid.Empty;
- else
- return job.ID;
- }
- private Document ReadFile(FileInfo file)
- {
- Document doc = new Document();
- doc.FileName = file.Name;
- doc.Data = GetData(file);
- return doc;
- }
- private byte[] GetData(FileInfo file)
- {
- Stream stream = new FileStream(file.FullName, FileMode.Open, FileAccess.Read, FileShare.Read);
- byte[] pdfData = new byte[stream.Length];
- stream.Read(pdfData, 0, System.Convert.ToInt32(pdfData.Length));
- stream.Dispose();
- return pdfData;
- }
- private IEnumerable<FileInfo> GetFiles()
- {
- string folder = @"U:\" + "9999" + " " + "Test Job" + @"\COM-AL SETOUTS";
- var dir = new DirectoryInfo(folder);
- //return dir.EnumerateFiles();
- return dir.GetFiles();
- }
- public void ReloadFile(IEntityDocument document)
- {
- var files = GetFiles();
- foreach (var file in files)
- {
- if (file.Name == document.DocumentLink.FileName)
- {
- CoreTable table = new Client<Document>().Query(new Filter<Document>(x => x.ID).IsEqualTo(document.DocumentLink.ID));
- var doc = table.Rows.FirstOrDefault().ToObject<Document>();
- doc.Data = GetData(file);
- new Client<Document>().Save(doc, "Reloaded Marked up Document");
- return;
- }
- }
- }
- private void LoadExistingStaging()
- {
- CoreTable stagingseouttable = new Client<StagingSetout>().Query(
- new Filter<StagingSetout>(x => x.Archived).IsEqualTo(DateTime.MinValue),
- new Columns<StagingSetout>(x => x.Number));
- var ids = new List<Guid>();
- foreach (var row in stagingseouttable.Rows)
- _existingFileNames.Add(row.Get<StagingSetout, string>(x => x.Number));
- }
- private bool CheckStagingSetoutExists(FileInfo file)
- {
- foreach (var existing in _existingFileNames)
- {
- if (file.Name.Substring(0, file.Name.Length - 4).Equals(existing))
- return true;
- }
- return false;
- }
- protected override void Reload(Filters<StagingSetout> criteria, Columns<StagingSetout> columns, ref SortOrder<StagingSetout>? sort, Action<CoreTable?, Exception?> action)
- {
- criteria.Add(new Filter<StagingSetout>(x => x.Archived).IsEqualTo(DateTime.MinValue));
- base.Reload(criteria, columns, ref sort, action);
- }
- public void DeleteFile(IEntityDocument document)
- {
- var file = GetFiles().FirstOrDefault(x => x.Name == document.DocumentLink.FileName);
- if (file != null)
- file.Delete();
- }
- }
- }
|