|
|
@@ -77,6 +77,8 @@ public class StagingSetoutGrid : DynamicDataGrid<Setout>
|
|
|
public string OriginalPath { get; set; }
|
|
|
|
|
|
public string OriginalCRC { get; set; }
|
|
|
+
|
|
|
+ public bool Skipped { get; set; }
|
|
|
|
|
|
public SetoutDocument0(Document document, Setout setout, string originalPath, string originalCRC)
|
|
|
{
|
|
|
@@ -84,6 +86,7 @@ public class StagingSetoutGrid : DynamicDataGrid<Setout>
|
|
|
Setout = setout;
|
|
|
OriginalPath = originalPath;
|
|
|
OriginalCRC = originalCRC;
|
|
|
+ Skipped = false;
|
|
|
}
|
|
|
|
|
|
public void Deconstruct(out Document document, out Setout setout)
|
|
|
@@ -1331,165 +1334,126 @@ public class StagingSetoutGrid : DynamicDataGrid<Setout>
|
|
|
if (!documents.Any())
|
|
|
return;
|
|
|
|
|
|
- var newDocuments = documents.Select(x =>
|
|
|
- {
|
|
|
- var (filename, file) = x;
|
|
|
-
|
|
|
- var setout = new Setout
|
|
|
- {
|
|
|
- Number = Path.GetFileNameWithoutExtension(file.FileName)
|
|
|
- };
|
|
|
- setout.Job.ID = Job?.ID ?? Guid.Empty;
|
|
|
- setout.Job.Synchronise(Job ?? new Job());
|
|
|
-
|
|
|
- return new SetoutDocument0(file, setout, filename, file.CRC);
|
|
|
- }).ToList();
|
|
|
+ List<string> messages = new();
|
|
|
+ Progress.ShowModal("Uploading Setouts",
|
|
|
+ progress =>
|
|
|
+ {
|
|
|
+ var newDocuments = documents.Select(x =>
|
|
|
+ {
|
|
|
+ var (filename, file) = x;
|
|
|
+ var setout = new Setout
|
|
|
+ {
|
|
|
+ Number = Path.GetFileNameWithoutExtension(file.FileName)
|
|
|
+ };
|
|
|
+ setout.Job.ID = Job?.ID ?? Guid.Empty;
|
|
|
+ setout.Job.Synchronise(Job ?? new Job());
|
|
|
+ return new SetoutDocument0(file, setout, filename, file.CRC);
|
|
|
+ }).ToList();
|
|
|
+
|
|
|
+ var checkNumbers = newDocuments.Select(x => x.Setout.Number).ToArray();
|
|
|
+
|
|
|
+ progress.Report("Loading Existing Setouts");
|
|
|
+
|
|
|
+ MultiQuery query = new MultiQuery();
|
|
|
+ query.Add(
|
|
|
+ Filter<Setout>.Where(x => x.Number).InList(checkNumbers),
|
|
|
+ Columns.None<Setout>()
|
|
|
+ .Add(x => x.ID)
|
|
|
+ .Add(x => x.Number)
|
|
|
+ );
|
|
|
+
|
|
|
+ query.Add(
|
|
|
+ Filter<SetoutDocument>.Where(x => x.Entity.Number).InList(checkNumbers),
|
|
|
+ Columns.None<SetoutDocument>()
|
|
|
+ .Add(x => x.ID)
|
|
|
+ .Add(x => x.Entity.ID)
|
|
|
+ .Add(x => x.Document.CRC)
|
|
|
+ .Add(x=>x.Document.FileName)
|
|
|
+ .Add(x=>x.Superceded)
|
|
|
+ );
|
|
|
+
|
|
|
+ query.Query();
|
|
|
+ var _existingsetouts = query.Get<Setout>().ToList<Setout>();
|
|
|
+ var _existingdocuments = query.Get<SetoutDocument>().ToArray<SetoutDocument>();
|
|
|
+
|
|
|
+ foreach (var newDocument in newDocuments.ToArray())
|
|
|
+ {
|
|
|
+ // If this is a new setout, then we don't need to do anything!
|
|
|
+ var _existingsetout =
|
|
|
+ _existingsetouts.FirstOrDefault(x => string.Equals(x.Number, newDocument.Setout.Number));
|
|
|
+ if (_existingsetout is null)
|
|
|
+ {
|
|
|
+ messages.Add($"Adding new Setout ({newDocument.Setout.Number}");
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ newDocument.Setout = _existingsetout;
|
|
|
|
|
|
- var checkNumbers = newDocuments.Select(x => x.Setout.Number).ToArray();
|
|
|
-
|
|
|
- var _existingsetouts = new Client<Setout>()
|
|
|
- .Query(
|
|
|
- Filter<Setout>.Where(x => x.Number).InList(checkNumbers),
|
|
|
- Columns.None<Setout>().Add(x => x.ID).Add(x => x.Number))
|
|
|
- .ToList<Setout>();
|
|
|
-
|
|
|
- var _existingdocuments = new Client<SetoutDocument>()
|
|
|
- .Query(
|
|
|
- Filter<SetoutDocument>.Where(x => x.Entity.Number).InList(checkNumbers),
|
|
|
- Columns.None<SetoutDocument>().Add(x => x.ID).Add(x => x.Entity.ID).Add(x => x.Document.CRC).Add(x=>x.Document.FileName).Add(x=>x.Superceded))
|
|
|
- .ToArray<SetoutDocument>();
|
|
|
+ // if this is an existing setout, we want to check if this filename already exists
|
|
|
+ // if it doesn't exist, we notify and redirect newDocument.Setout to _existingsetout and continue (ie add this file to the existing setout)
|
|
|
+ var existingdocument = _existingdocuments.FirstOrDefault(x => string.Equals(x.Document.FileName,newDocument.Document.FileName) && x.Superceded.IsEmpty());
|
|
|
+ if (existingdocument is null)
|
|
|
+ {
|
|
|
+ //notify the user that we are adding a document to the setout
|
|
|
+ messages.Add($"Adding [{newDocument.Document.FileName}] to existing setout ({_existingsetout.Number})");
|
|
|
+ continue;
|
|
|
+ }
|
|
|
|
|
|
- List<string> messages = new();
|
|
|
- foreach (var newDocument in newDocuments.ToArray())
|
|
|
- {
|
|
|
- // If this is a new setout, then we don't need to do anything!
|
|
|
- var _existingsetout =
|
|
|
- _existingsetouts.FirstOrDefault(x => string.Equals(x.Number, newDocument.Setout.Number));
|
|
|
- if (_existingsetout is null)
|
|
|
- {
|
|
|
- messages.Add($"Adding new Setout ({newDocument.Setout.Number}");
|
|
|
- continue;
|
|
|
- }
|
|
|
+ // if the crc is different, we notify the user and supercede the old document
|
|
|
+ if (!existingdocument.Document.CRC.Equals(newDocument.Document.CRC))
|
|
|
+ {
|
|
|
+ messages.Add($"Superceding [{newDocument.Document.FileName}]");
|
|
|
+ existingdocument.Superceded = DateTime.Now;
|
|
|
+ new Client<SetoutDocument>().Save(existingdocument,"Replacing with updated file");
|
|
|
+ continue;
|
|
|
+ }
|
|
|
|
|
|
- // if this is an existing setout, we want to check if this filename already exists
|
|
|
- // if it doesn't exist, we notify and redirect newDocument.Setout to _existingsetout and continue (ie add this file to the existing setout)
|
|
|
- var existingdocument = _existingdocuments.FirstOrDefault(x => string.Equals(x.Document.FileName,newDocument.Document.FileName) && x.Superceded.IsEmpty());
|
|
|
- if (existingdocument is null)
|
|
|
- {
|
|
|
- //notify the user that we are adding a document to the setout
|
|
|
- messages.Add($"Adding [{newDocument.Document.FileName}] to existing setout ({_existingsetout.Number}).");
|
|
|
- newDocument.Setout = _existingsetout;
|
|
|
- continue;
|
|
|
- }
|
|
|
+ // otherwise (same file) -> we notify and skip
|
|
|
+ messages.Add($"Skipping existing file [{newDocument.Document.FileName}]");
|
|
|
+ newDocument.Skipped = true;
|
|
|
+ }
|
|
|
|
|
|
- // if the crc is different, we notify the user and supercede the old document
|
|
|
- if (!existingdocument.Document.CRC.Equals(newDocument.Document.CRC))
|
|
|
- {
|
|
|
- messages.Add($"Superceding [{newDocument.Document.FileName}]...");
|
|
|
- existingdocument.Superceded = DateTime.Now;
|
|
|
- new Client<SetoutDocument>().Save(existingdocument,"Replacing with updated file");
|
|
|
- newDocument.Setout = _existingsetout;
|
|
|
- continue;
|
|
|
+ progress.Report("Updating Database");
|
|
|
+ if (newDocuments.Any())
|
|
|
+ {
|
|
|
+ var _changedDocs = newDocuments.Where(x=>!x.Skipped).ToArray();
|
|
|
+ if (_changedDocs.Any())
|
|
|
+ {
|
|
|
+ progress.Report("Saving Documents");
|
|
|
+ Client.Save(_changedDocs.Select(x => x.Document), "Created from sync setout function");
|
|
|
+ }
|
|
|
+ progress.Report("Customising Setouts");
|
|
|
+ OnCustomiseSetouts?.Invoke(newDocuments);
|
|
|
+
|
|
|
+ progress.Report("Saving Changed Setouts");
|
|
|
+ Client.Save(newDocuments.Select(x=>x.Setout), "Created from sync setout function");
|
|
|
+
|
|
|
+ var stagingdocs = newDocuments
|
|
|
+ .Where(x=>!x.Skipped)
|
|
|
+ .Select(x =>
|
|
|
+ {
|
|
|
+ var stagingsetoutdoc = new SetoutDocument();
|
|
|
+ stagingsetoutdoc.Entity.ID = x.Setout.ID;
|
|
|
+ stagingsetoutdoc.Document.ID = x.Document.ID;
|
|
|
+ stagingsetoutdoc.Staging.OriginalPath = x.OriginalPath;
|
|
|
+ stagingsetoutdoc.Staging.OriginalCRC = x.OriginalCRC;
|
|
|
+ return stagingsetoutdoc;
|
|
|
+ }
|
|
|
+ );
|
|
|
+ if (stagingdocs.Any())
|
|
|
+ {
|
|
|
+ progress.Report("Saving Setout Documents");
|
|
|
+ Client.Save(stagingdocs, "Created from sync setout function");
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
- // otherwise (same file) -> we notify and skip
|
|
|
- messages.Add($"Skipping existing file [{newDocument.Document.FileName}]...");
|
|
|
- newDocuments.Remove(newDocument);
|
|
|
- }
|
|
|
+ );
|
|
|
|
|
|
MessageWindow.New()
|
|
|
.Title($"Import Results")
|
|
|
.Message(string.Join("\n",messages))
|
|
|
.AddOKButton()
|
|
|
.Display();
|
|
|
-
|
|
|
- // if (_existingsetouts.Count != 0)
|
|
|
- // {
|
|
|
- // foreach (var doc in _existingdocuments)
|
|
|
- // {
|
|
|
- // // This setout must exist based on the filter.
|
|
|
- // var setout = _existingsetouts.First(x => x.ID == doc.Entity.ID);
|
|
|
- //
|
|
|
- // // This document must exist also based on the filter, however it may have been removed.
|
|
|
- // var newDoc = newDocuments.FirstOrDefault(x => x.Setout.Number == setout.Number);
|
|
|
- // if (newDoc is not null)
|
|
|
- // {
|
|
|
- // string message;
|
|
|
- // if (doc.Document.CRC != newDoc.Document.CRC)
|
|
|
- // {
|
|
|
- // message = $"A different setout with the number '{setout.Number}' already exists.";
|
|
|
- // }
|
|
|
- // else
|
|
|
- // {
|
|
|
- // message = $"A matching setout with number '{setout.Number}' has already been uploaded.";
|
|
|
- // }
|
|
|
- //
|
|
|
- // var result = MessageWindow.New()
|
|
|
- // .Title($"Setout '{setout.Number}' already exists")
|
|
|
- // .Message($"{message} Do you wish to rename the new setout, or skip it?")
|
|
|
- // .AddYesButton("Rename setout")
|
|
|
- // .AddNoButton("Skip setout")
|
|
|
- // .AddCancelButton()
|
|
|
- // .Display()
|
|
|
- // .Result;
|
|
|
- // if (result == MessageWindowResult.Yes)
|
|
|
- // {
|
|
|
- // var number = setout.Number;
|
|
|
- // while (true)
|
|
|
- // {
|
|
|
- // if (!TextEdit.Execute("Provide new setout number: ", ref number))
|
|
|
- // {
|
|
|
- // MessageWindow.ShowMessage($"Skipping setout '{setout.Number}'", "Skipped");
|
|
|
- // newDocuments.Remove(newDoc);
|
|
|
- // break;
|
|
|
- // }
|
|
|
- //
|
|
|
- // var clash = Client.Query(
|
|
|
- // Filter<Setout>.Where(x => x.Number).IsEqualTo(number),
|
|
|
- // Columns.None<Setout>().Add(x => x.ID))
|
|
|
- // .Rows.Count > 0;
|
|
|
- // if (!clash)
|
|
|
- // {
|
|
|
- // newDoc.Setout.Number = number;
|
|
|
- // break;
|
|
|
- // }
|
|
|
- // else
|
|
|
- // {
|
|
|
- // MessageWindow.ShowMessage(
|
|
|
- // $"A setout with number '{number}' already exists. Please provide a different number.",
|
|
|
- // "Already exists");
|
|
|
- // }
|
|
|
- // }
|
|
|
- // }
|
|
|
- // else if (result == MessageWindowResult.No)
|
|
|
- // {
|
|
|
- // newDocuments.Remove(newDoc);
|
|
|
- // }
|
|
|
- // else if (result == MessageWindowResult.Cancel)
|
|
|
- // {
|
|
|
- // return;
|
|
|
- // }
|
|
|
- // }
|
|
|
- // }
|
|
|
- //
|
|
|
- // }
|
|
|
-
|
|
|
- Client.Save(newDocuments.Select(x => x.Document), "Created from sync setout function");
|
|
|
-
|
|
|
- OnCustomiseSetouts?.Invoke(newDocuments);
|
|
|
- Client.Save(newDocuments.Select(x => x.Setout), "Created from sync setout function");
|
|
|
-
|
|
|
- var stagingdocs = newDocuments.Select(x =>
|
|
|
- {
|
|
|
- var stagingsetoutdoc = new SetoutDocument();
|
|
|
- stagingsetoutdoc.Entity.ID = x.Setout.ID;
|
|
|
- stagingsetoutdoc.Document.ID = x.Document.ID;
|
|
|
- stagingsetoutdoc.Staging.OriginalPath = x.OriginalPath;
|
|
|
- stagingsetoutdoc.Staging.OriginalCRC = x.OriginalCRC;
|
|
|
- return stagingsetoutdoc;
|
|
|
- });
|
|
|
-
|
|
|
- Client.Save(stagingdocs, "Created from sync setout function");
|
|
|
}
|
|
|
|
|
|
public void AddDXFFiles(IEnumerable<string> fileNames, Action<IEnumerable<Tuple<string, Document>>> addAction)
|