JobDocumentSetTree.xaml.cs 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Documents;
  9. using System.Windows.Forms;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using Comal.Classes;
  13. using FastReport.Utils;
  14. using InABox.Clients;
  15. using InABox.Configuration;
  16. using InABox.Core;
  17. using InABox.Core.Reports;
  18. using InABox.DynamicGrid;
  19. using InABox.Wpf;
  20. using InABox.Wpf.Reports;
  21. using InABox.WPF;
  22. using javax.xml.crypto;
  23. using NPOI.SS.Formula.Functions;
  24. using org.apache.commons.lang3;
  25. using Syncfusion.Compression.Zip;
  26. using Syncfusion.Data.Extensions;
  27. using Syncfusion.UI.Xaml.Grid;
  28. using Syncfusion.UI.Xaml.TreeGrid;
  29. using Syncfusion.UI.Xaml.TreeGrid.Helpers;
  30. using Syncfusion.Windows.Controls.Cells;
  31. using Syncfusion.XlsIO;
  32. using Color = System.Drawing.Color;
  33. using Environment = System.Environment;
  34. using GridSelectionChangedEventArgs = Syncfusion.UI.Xaml.Grid.GridSelectionChangedEventArgs;
  35. using JobDocumentSetFolder = Comal.Classes.JobDocumentSetFolder;
  36. using MessageBox = System.Windows.MessageBox;
  37. using UserControl = System.Windows.Controls.UserControl;
  38. namespace PRSDesktop
  39. {
  40. public class JobDocumentSetTreeSettings : IUserConfigurationSettings
  41. {
  42. public bool DetailsVisible { get; set; }
  43. public JobDocumentSetTreeSettings()
  44. {
  45. DetailsVisible = true;
  46. }
  47. }
  48. public delegate void JobDocumentSetMileStoneSelected(JobDocumentSetMileStoneBlock block);
  49. public partial class JobDocumentSetTree : UserControl
  50. {
  51. public event JobDocumentSetMileStoneSelected MileStoneSelected;
  52. private struct MileStone
  53. {
  54. public Guid TypeID { get; set; }
  55. public CoreRow Row { get; set; }
  56. }
  57. private struct MileStoneType
  58. {
  59. public String Code { get; set; }
  60. public String Description { get; set; }
  61. public Dictionary<Guid,List<CoreRow>> SetMileStones { get; set; }
  62. public List<String> Columns { get; set; }
  63. }
  64. public Job Job { get; set; }
  65. public Guid[] FolderIDs{ get; set; }
  66. //public bool DisciplineVisible { get; set; }
  67. public Guid DisciplineID { get; set; }
  68. //public bool TypeVisible { get; set; }
  69. public Guid TypeID { get; set; }
  70. //public bool CategoryVisible { get; set; }
  71. public Guid CategoryID { get; set; }
  72. //public bool AreaVisible { get; set; }
  73. public Guid AreaID { get; set; }
  74. public String SearchText { get; set; }
  75. private Dictionary<Guid,MileStoneType> _types = null;
  76. private CoreTable _milestones = null;
  77. public CoreTable Data { get; private set; } = null;
  78. private CoreTable _files = null;
  79. private bool _hidesuperceded = false;
  80. private bool _flatlist = false;
  81. private bool _includeretired = false;
  82. private DocumentSetNodes _documentsets = null;
  83. private JobDocumentSetTreeSettings _settings;
  84. public JobDocumentSetTree()
  85. {
  86. InitializeComponent();
  87. AddImage.Source = PRSDesktop.Resources.add.AsBitmapImage();
  88. EditImage.Source = PRSDesktop.Resources.pencil.AsBitmapImage();
  89. DeleteImage.Source = PRSDesktop.Resources.delete.AsBitmapImage();
  90. _settings = new UserConfiguration<JobDocumentSetTreeSettings>().Load();
  91. treeGrid.Loaded += (o, e) =>
  92. {
  93. treeGrid.GetTreePanel().RowHeights[1] = 0;
  94. treeGrid.UpdateDataRow(1);
  95. };
  96. }
  97. public void Refresh()
  98. {
  99. using (new WaitCursor())
  100. {
  101. var scrollviewer = WPFUtils.FindVisualChildren<ScrollViewer>(treeGrid).FirstOrDefault();
  102. var verticalOffset = scrollviewer != null ? scrollviewer.VerticalOffset : 0;
  103. var horizontalOffset = treeGrid.SelectedItem != null ? scrollviewer.HorizontalOffset : 0;
  104. treeGrid.ItemsSource = null;
  105. var setfilter = new Filter<JobDocumentSet>(x => x.Job.ID).IsEqualTo(Job.ID);
  106. if ((FolderIDs?.Any() == true) && !FolderIDs.Contains(CoreUtils.FullGuid))
  107. setfilter = setfilter.And(x => x.Folder.ID).InList(FolderIDs);
  108. if (DisciplineID != Guid.Empty)
  109. setfilter = setfilter.And(x => x.Discipline.ID).IsEqualTo(DisciplineID);
  110. if (TypeID != Guid.Empty)
  111. setfilter = setfilter.And(x => x.Type.ID).IsEqualTo(TypeID);
  112. if (CategoryID != Guid.Empty)
  113. setfilter = setfilter.And(x => x.Category.ID).IsEqualTo(CategoryID);
  114. if (AreaID != Guid.Empty)
  115. setfilter = setfilter.And(x => x.Area.ID).IsEqualTo(AreaID);
  116. if (!_includeretired)
  117. setfilter = setfilter.And(x => x.Retired).IsEqualTo(DateTime.MinValue);
  118. if (!String.IsNullOrWhiteSpace(SearchText))
  119. setfilter = setfilter.TextSearch(SearchText, x => x.Code, x => x.Description);
  120. MultiQuery query = new MultiQuery();
  121. query.Add(
  122. setfilter,
  123. new Columns<JobDocumentSet>(x => x.ID)
  124. .Add(x => x.Parent.ID)
  125. .Add(x => x.Code)
  126. .Add(x => x.Description)
  127. .Add(x => x.Date)
  128. .Add(x => x.Size)
  129. .Add(x => x.Scale)
  130. .Add(x => x.Employee.Name)
  131. .Add(x=>x.Folder.ID)
  132. .Add(x=>x.Discipline.Description)
  133. .Add(x=>x.Category.Description)
  134. .Add(x=>x.Type.Description)
  135. .Add(x=>x.Area.Description),
  136. new SortOrder<JobDocumentSet>(x => x.Code)
  137. );
  138. var milestonefilter = new Filter<JobDocumentSetMileStone>(x => x.DocumentSet.Job.ID).IsEqualTo(Job.ID);
  139. if ((FolderIDs?.Any() == true) && !FolderIDs.Contains(CoreUtils.FullGuid))
  140. milestonefilter = milestonefilter.And(x => x.DocumentSet.Folder.ID).InList(FolderIDs);
  141. query.Add(
  142. milestonefilter,
  143. new Columns<JobDocumentSetMileStone>(x => x.ID)
  144. .Add(x => x.DocumentSet.ID)
  145. .Add(x => x.DocumentSet.Code)
  146. .Add(x => x.Type.ID)
  147. .Add(x => x.Type.Code)
  148. .Add(x => x.Status)
  149. .Add(x => x.Notes)
  150. .Add(x => x.Revision)
  151. .Add(x => x.Due)
  152. .Add(x => x.Submitted)
  153. .Add(x => x.Closed)
  154. .Add(x => x.Attachments)
  155. .Add(x => x.Kanbans)
  156. .Add(x => x.Watermark)
  157. );
  158. if (_types == null)
  159. {
  160. query.Add<JobDocumentSetMileStoneType>(
  161. null,
  162. new Columns<JobDocumentSetMileStoneType>(x => x.ID)
  163. .Add(x => x.Code)
  164. .Add(x => x.Description),
  165. new SortOrder<JobDocumentSetMileStoneType>(x => x.Sequence)
  166. );
  167. }
  168. query.Query();
  169. Data = query.Get<JobDocumentSet>();
  170. _milestones = query.Get<JobDocumentSetMileStone>();
  171. if (_types == null)
  172. {
  173. _types = query.Get<JobDocumentSetMileStoneType>().ToDictionary<JobDocumentSetMileStoneType, Guid, MileStoneType>(
  174. x => x.ID,
  175. r => new MileStoneType()
  176. {
  177. Code = r.Get<JobDocumentSetMileStoneType, String>(c => c.Code),
  178. Description = r.Get<JobDocumentSetMileStoneType, String>(c => c.Description),
  179. SetMileStones = new Dictionary<Guid, List<CoreRow>>(),
  180. Columns = new List<string>()
  181. }
  182. );
  183. }
  184. else
  185. {
  186. foreach (var typeid in _types.Keys)
  187. {
  188. _types[typeid].Columns.Clear();
  189. _types[typeid].SetMileStones.Clear();
  190. }
  191. }
  192. var milestones = _milestones.ToLookup<JobDocumentSetMileStone, Guid, MileStone>(
  193. x => x.DocumentSet.ID,
  194. r => new MileStone()
  195. {
  196. TypeID = r.Get<JobDocumentSetMileStone, Guid>(c => c.Type.ID),
  197. Row = r
  198. }
  199. );
  200. foreach (var milestone in milestones)
  201. {
  202. foreach (var entry in milestone)
  203. {
  204. if (!_types[entry.TypeID].SetMileStones.ContainsKey(milestone.Key))
  205. _types[entry.TypeID].SetMileStones[milestone.Key] = new List<CoreRow>();
  206. if (_hidesuperceded)
  207. _types[entry.TypeID].SetMileStones[milestone.Key].Clear();
  208. _types[entry.TypeID].SetMileStones[milestone.Key].Add(entry.Row);
  209. }
  210. }
  211. List<String> columns = new List<string>();
  212. foreach (var typeid in _types.Keys)
  213. {
  214. int count = 1;
  215. foreach (var setkey in _types[typeid].SetMileStones.Keys)
  216. count = Math.Max(count, _types[typeid].SetMileStones[setkey].Count);
  217. for (int i = 1; i <= count; i++)
  218. {
  219. String column = String.Format("{0}_{1}", _types[typeid].Code, i);
  220. columns.Add(column);
  221. _types[typeid].Columns.Add(String.Format("Blocks[{0}]", column));
  222. }
  223. }
  224. _documentsets = new DocumentSetNodes(columns);
  225. foreach (var setrow in Data.Rows)
  226. {
  227. Guid setid = setrow.Get<JobDocumentSet, Guid>(x => x.ID);
  228. Guid parentid = _flatlist ? Guid.Empty : setrow.Get<JobDocumentSet, Guid>(x => x.Parent.ID);
  229. String code = setrow.Get<JobDocumentSet, String>(c => c.Code);
  230. String description = setrow.Get<JobDocumentSet, String>(c => c.Description);
  231. var tags = new List<String>()
  232. {
  233. setrow.Get<JobDocumentSet, String>(c => c.Discipline.Description),
  234. setrow.Get<JobDocumentSet, String>(c => c.Type.Description),
  235. setrow.Get<JobDocumentSet, String>(c => c.Category.Description),
  236. setrow.Get<JobDocumentSet, String>(c => c.Area.Description)
  237. }.Where(x=>!String.IsNullOrWhiteSpace(x)).Distinct().ToArray();
  238. var node = _documentsets.Add(setid, parentid);
  239. JobDocumentSetDescriptionBlock desc = new JobDocumentSetDescriptionBlock(
  240. setid, code, description, tags);
  241. node.Description = Serialization.Serialize(desc);
  242. JobDocumentSetDetailsBlock dets = new JobDocumentSetDetailsBlock()
  243. {
  244. ID = setid,
  245. Date = setrow.Get<JobDocumentSet, DateTime>(c => c.Date),
  246. Size = setrow.Get<JobDocumentSet, PaperSize>(c => c.Size),
  247. Scale = setrow.Get<JobDocumentSet, String>(c => c.Scale),
  248. Employee = setrow.Get<JobDocumentSet, String>(c => c.Employee.Name)
  249. };
  250. node.Details = Serialization.Serialize(dets);
  251. foreach (var typeid in _types.Keys)
  252. {
  253. if (_types[typeid].SetMileStones.TryGetValue(setid, out var rows))
  254. {
  255. int i = 1;
  256. foreach (var row in rows)
  257. {
  258. JobDocumentSetMileStoneBlock block = new JobDocumentSetMileStoneBlock();
  259. block.ID = row.Get<JobDocumentSetMileStone, Guid>(c => c.ID);
  260. block.Revision = row.Get<JobDocumentSetMileStone, String>(c => c.Revision);
  261. block.Status = row.Get<JobDocumentSetMileStone, JobDocumentSetMileStoneStatus>(c => c.Status);
  262. block.Date = (block.Status == JobDocumentSetMileStoneStatus.Approved) ||
  263. (block.Status == JobDocumentSetMileStoneStatus.Cancelled) ||
  264. (block.Status == JobDocumentSetMileStoneStatus.Rejected)
  265. ? row.Get<JobDocumentSetMileStone, DateTime>(c => c.Closed)
  266. : block.Status == JobDocumentSetMileStoneStatus.Submitted
  267. ? block.Date = row.Get<JobDocumentSetMileStone, DateTime>(c => c.Submitted)
  268. : row.Get<JobDocumentSetMileStone, DateTime>(c => c.Due);
  269. String[] notes = row.Get<JobDocumentSetMileStone, String[]>(c => c.Notes);
  270. block.Notes = notes != null ? String.Join("\n", notes) : "";
  271. block.Attachments = row.Get<JobDocumentSetMileStone, int>(c => c.Attachments);
  272. block.Kanbans = row.Get<JobDocumentSetMileStone, int>(c => c.Kanbans);
  273. block.Watermark = row.Get<JobDocumentSetMileStone, String>(c => c.Watermark);
  274. node.Blocks[String.Format("{0}_{1}", _types[typeid].Code, i)] = Serialization.Serialize(block);
  275. i++;
  276. }
  277. }
  278. }
  279. }
  280. ConfigureColumns(_documentsets);
  281. ConfigureStackedHeader();
  282. treeGrid.ItemsSource = _documentsets.Nodes;
  283. DocumentCount.Content = $"{_documentsets.Nodes.Count} {(_documentsets.Nodes.Count > 1 ? "Records" : "Record")}";
  284. if (scrollviewer != null)
  285. {
  286. scrollviewer.ScrollToVerticalOffset(verticalOffset);
  287. scrollviewer.ScrollToHorizontalOffset(horizontalOffset);
  288. }
  289. }
  290. }
  291. #region Grid Configuration
  292. private void ConfigureColumns(DocumentSetNodes documentsets)
  293. {
  294. treeGrid.Columns.Clear();
  295. treeGrid.Columns.Add(new TreeGridTemplateColumn()
  296. {
  297. CellTemplate = FindResource("descriptionTemplate") as DataTemplate,
  298. MappingName = "Description",
  299. SetCellBoundValue = true,
  300. MinimumWidth = 250,
  301. ColumnSizer = TreeColumnSizer.Star
  302. });
  303. treeGrid.Columns.Add(new TreeGridTemplateColumn()
  304. {
  305. CellTemplate = FindResource("detailsTemplate") as DataTemplate,
  306. MappingName = "Details",
  307. SetCellBoundValue = true,
  308. Width = _settings.DetailsVisible ? 120 : 0
  309. });
  310. foreach (var column in documentsets.Columns)
  311. {
  312. var col = new TreeGridTemplateColumn()
  313. {
  314. CellTemplate = FindResource("milestoneTemplate") as DataTemplate,
  315. MappingName = String.Format("Blocks[{0}]",column),
  316. SetCellBoundValue = true,
  317. HeaderText = " ",
  318. Width = 80,
  319. ShowToolTip = true
  320. };
  321. treeGrid.Columns.Add(col);
  322. }
  323. }
  324. private void ConfigureStackedHeader()
  325. {
  326. stackedHeaderRow.StackedColumns.Clear();
  327. stackedHeaderRow.StackedColumns.Add(new StackedColumn()
  328. {
  329. ChildColumns = "Description,Details",
  330. HeaderText = "Document Register"
  331. });
  332. foreach (var typeid in _types.Keys)
  333. {
  334. stackedHeaderRow.StackedColumns.Add(new StackedColumn()
  335. {
  336. ChildColumns = String.Join(",", _types[typeid].Columns),
  337. HeaderText = _types[typeid].Code
  338. });
  339. }
  340. }
  341. private void TreeGrid_OnItemsSourceChanged(object? sender, TreeGridItemsSourceChangedEventArgs e)
  342. {
  343. var panel = treeGrid.GetTreePanel();
  344. panel.RowHeights[1] = 0;
  345. }
  346. private void TreeGrid_OnNodeCollapsing(object? sender, NodeCollapsingEventArgs e)
  347. {
  348. e.Cancel = true;
  349. }
  350. public MenuItem CreateCalendar(ContextMenu menu, string text, DateTime startDate, CoreRow[] milestones, Action<CoreRow[], DateTime?>? action)
  351. {
  352. var item = new MenuItem();
  353. var calendarItem = new MenuItem();
  354. var calendar = new System.Windows.Controls.Calendar { DisplayDate = startDate, SelectedDate = null};
  355. calendar.SelectedDatesChanged += (o, e) =>
  356. {
  357. action?.Invoke(milestones, calendar.SelectedDate);
  358. menu.IsOpen = false;
  359. };
  360. calendarItem.Header = calendar;
  361. calendarItem.Style = DynamicGridUtils.Resources["NonHighlightMenuItem"] as Style;
  362. item.Header = text;
  363. item.Items.Add(calendarItem);
  364. item.IsCheckable = false;
  365. return item;
  366. }
  367. private IEnumerable<DocumentSetNode> GetSelectedNodes()
  368. {
  369. return treeGrid.SelectedItems
  370. .Select(x => x as DocumentSetNode)
  371. .NotNull();
  372. }
  373. private RowColumnIndex GetMouseRowColumnIndex(Point? mousePos = null)
  374. {
  375. var rci = treeGrid.GetTreePanel().PointToCellRowColumnIndex(mousePos ?? Mouse.GetPosition(treeGrid));
  376. return new RowColumnIndex(rci.RowIndex, rci.ColumnIndex);
  377. }
  378. private IEnumerable<JobDocumentSetMileStoneBlock> GetSelectedBlocks(Point? mousePos = null)
  379. {
  380. var rowColumnIndex = GetMouseRowColumnIndex(mousePos);
  381. if (rowColumnIndex.IsEmpty)
  382. {
  383. return Enumerable.Empty<JobDocumentSetMileStoneBlock>();
  384. }
  385. var treeNodeAtRowIndex = treeGrid.GetNodeAtRowIndex(rowColumnIndex.RowIndex);
  386. var mappingname = treeGrid.Columns[rowColumnIndex.ColumnIndex].MappingName;
  387. var blockkey = mappingname.Replace("Blocks[", "").Replace("]", "");
  388. return treeGrid.SelectedItems
  389. .Select(x => (x as DocumentSetNode)?.Blocks[blockkey])
  390. .Where(x => !x.IsNullOrWhiteSpace())
  391. .Select(x => Serialization.Deserialize<JobDocumentSetMileStoneBlock>(x))
  392. .NotNull();
  393. }
  394. private DocumentSetNode? GetHoveredSet(Point? mousePos = null)
  395. {
  396. var rowColumnIndex = GetMouseRowColumnIndex(mousePos);
  397. if (rowColumnIndex.IsEmpty)
  398. return null;
  399. var treeNodeAtRowIndex = treeGrid.GetNodeAtRowIndex(rowColumnIndex.RowIndex);
  400. return treeNodeAtRowIndex.Item as DocumentSetNode;
  401. }
  402. private JobDocumentSetMileStoneBlock? GetHoveredBlock(Point? mousePos = null)
  403. {
  404. var rowColumnIndex = GetMouseRowColumnIndex(mousePos);
  405. if (rowColumnIndex.IsEmpty)
  406. return null;
  407. var mappingname = treeGrid.Columns[rowColumnIndex.ColumnIndex].MappingName;
  408. var blockkey = mappingname.Replace("Blocks[", "").Replace("]", "");
  409. var treeNodeAtRowIndex = treeGrid.GetNodeAtRowIndex(rowColumnIndex.RowIndex);
  410. var block = (treeNodeAtRowIndex.Item as DocumentSetNode)?.Blocks[blockkey];
  411. if (block.IsNullOrWhiteSpace()) return null;
  412. return Serialization.Deserialize<JobDocumentSetMileStoneBlock>(block);
  413. }
  414. private bool CanCreateNewMileStone(Guid typeID, Guid[] setIDs)
  415. {
  416. foreach (var setID in setIDs)
  417. {
  418. var openmilestones = _milestones.Rows.Any(r =>
  419. Guid.Equals(r.Get<JobDocumentSetMileStone, Guid>(c => c.DocumentSet.ID), setID)
  420. && Guid.Equals(r.Get<JobDocumentSetMileStone, Guid>(c => c.Type.ID), typeID)
  421. && (r.Get<JobDocumentSetMileStone, DateTime>(c => c.Closed).IsEmpty() ||
  422. (r.Get<JobDocumentSetMileStone, JobDocumentSetMileStoneStatus>(c => c.Status) == JobDocumentSetMileStoneStatus.Approved))
  423. );
  424. if (openmilestones)
  425. return false;
  426. }
  427. return true;
  428. }
  429. private void Grid_Drop(object sender, System.Windows.DragEventArgs e)
  430. {
  431. var block = GetHoveredBlock(e.GetPosition(treeGrid));
  432. if (block is not null)
  433. {
  434. var milestoneRow = _milestones.Rows.FirstOrDefault(r => r.Get<JobDocumentSetMileStone, Guid>(c => c.ID) == block.ID);
  435. if (milestoneRow is null) return;
  436. var result = DocumentUtils.HandleFileDrop(e.Data);
  437. if (result is null) return;
  438. UploadFiles(new CoreRow[] { milestoneRow }, result);
  439. }
  440. else
  441. {
  442. var rowColumnIndex = GetMouseRowColumnIndex(e.GetPosition(treeGrid));
  443. if (rowColumnIndex.IsEmpty) return;
  444. var mappingname = treeGrid.Columns[rowColumnIndex.ColumnIndex].MappingName;
  445. var typeID = _types.FirstOrDefault(x => x.Value.Columns.Contains(mappingname)).Key;
  446. var setID = GetHoveredSet(e.GetPosition(treeGrid))?.ID ?? Guid.Empty;
  447. if (setID == Guid.Empty) return;
  448. if(CanCreateNewMileStone(typeID, new Guid[] { setID }))
  449. {
  450. var result = DocumentUtils.HandleFileDrop(e.Data);
  451. if (result is null) return;
  452. var documents = result.Select(x => CreateDocument(x.Item1, x.Item2)).ToList();
  453. var milestone = new JobDocumentSetMileStone();
  454. milestone.DocumentSet.ID = setID;
  455. milestone.Type.ID = typeID;
  456. milestone.Status = JobDocumentSetMileStoneStatus.NotStarted;
  457. milestone.Due = DateTime.Today;
  458. var grid = new JobDocumentSetMileStoneGrid();
  459. if (grid.EditItems(new JobDocumentSetMileStone[] { milestone }))
  460. {
  461. Client.Save(documents, "Uploaded by user.");
  462. var files = documents.Select(doc =>
  463. {
  464. var file = new JobDocumentSetMileStoneFile();
  465. file.DocumentLink.ID = doc.ID;
  466. file.EntityLink.ID = milestone.ID;
  467. return file;
  468. });
  469. Client.Save(files, "Uploaded by user.");
  470. Refresh();
  471. }
  472. }
  473. else
  474. {
  475. MessageWindow.ShowMessage("Cannot create a new milestone here.", "Error", image: MessageWindow.WarningImage);
  476. }
  477. }
  478. }
  479. private void TreeGrid_OnContextMenuOpening(object sender, ContextMenuEventArgs e)
  480. {
  481. if (treeGrid.SelectedItem == null)
  482. {
  483. e.Handled = true;
  484. return;
  485. }
  486. MileStoneMenu.Items.Clear();
  487. var rowColumnIndex = GetMouseRowColumnIndex();
  488. if (rowColumnIndex.IsEmpty)
  489. return;
  490. var treeNodeAtRowIndex = treeGrid.GetNodeAtRowIndex(rowColumnIndex.RowIndex);
  491. if (rowColumnIndex.ColumnIndex < 2)
  492. {
  493. var documents = GetSelectedNodes().ToArray();
  494. var ids = documents.Select(x => x.ID).ToArray();
  495. MileStoneMenu.AddItem("Edit Document Set", null, ids, EditDocumentSets);
  496. if (documents.Length == 1)
  497. {
  498. MileStoneMenu.AddSeparator();
  499. MileStoneMenu.AddItem("Add Child", null, documents.First(), AddChildDocument);
  500. }
  501. var movetofolder = new MenuItem();
  502. movetofolder.Header = "Move To Folder";
  503. bool hasfolders = PopulateFolders(movetofolder, documents);
  504. if (hasfolders)
  505. {
  506. MileStoneMenu.AddSeparator();
  507. MileStoneMenu.Items.Add(movetofolder);
  508. }
  509. MileStoneMenu.AddSeparator();
  510. MileStoneMenu.AddItem((treeGrid.Columns[1].Width > 0) ? "Hide Detail Column" : "Show Detail Column", null, ShowHideDetailsColumn);
  511. return;
  512. }
  513. var mappingname = treeGrid.Columns[rowColumnIndex.ColumnIndex].MappingName;
  514. var typeID = _types.FirstOrDefault(x => x.Value.Columns.Contains(mappingname)).Key;
  515. var setIDs = GetSelectedNodes().Select(x => x.ID).ToArray();
  516. var milestoneIDs = GetSelectedBlocks().Select(x => x.ID).ToArray();
  517. var milestoneRows = _milestones.Rows.Where(r => milestoneIDs.Contains(r.Get<JobDocumentSetMileStone, Guid>(c => c.ID))).ToArray();
  518. if (CanCreateNewMileStone(typeID, setIDs))
  519. {
  520. MileStoneMenu.AddItem("New Milestone", null, () => CreateMileStone(setIDs, typeID, DateTime.Today));
  521. }
  522. if (milestoneRows.Any())
  523. {
  524. var setStatus = MileStoneMenu.AddItem("Change Status", null, null);
  525. foreach (JobDocumentSetMileStoneStatus newstatus in Enum.GetValues(typeof(JobDocumentSetMileStoneStatus)))
  526. {
  527. MenuItem setstatus2 = null;
  528. switch (newstatus)
  529. {
  530. case JobDocumentSetMileStoneStatus.Unknown:
  531. break;
  532. case JobDocumentSetMileStoneStatus.NotStarted:
  533. case JobDocumentSetMileStoneStatus.InProgress:
  534. case JobDocumentSetMileStoneStatus.OnHold:
  535. case JobDocumentSetMileStoneStatus.InfoRequired:
  536. setStatus.AddItem(
  537. newstatus.ToString().SplitCamelCase(),
  538. null,
  539. () => ChangeMileStoneStatus(milestoneRows, newstatus, DateTime.MinValue, DateTime.MinValue));
  540. break;
  541. case JobDocumentSetMileStoneStatus.Submitted:
  542. setStatus.Items.Add(CreateCalendar(
  543. MileStoneMenu,
  544. newstatus.ToString().SplitCamelCase(),
  545. DateTime.Today,
  546. milestoneRows,
  547. (r, t) => { ChangeMileStoneStatus(milestoneRows, newstatus, t, DateTime.MinValue); }
  548. ));
  549. break;
  550. case JobDocumentSetMileStoneStatus.Approved:
  551. case JobDocumentSetMileStoneStatus.Cancelled:
  552. case JobDocumentSetMileStoneStatus.Rejected:
  553. setStatus.Items.Add(CreateCalendar(
  554. MileStoneMenu,
  555. newstatus.ToString().SplitCamelCase(),
  556. DateTime.Today,
  557. milestoneRows,
  558. (r, t) => { ChangeMileStoneStatus(milestoneRows, newstatus, null, t); }
  559. ));
  560. break;
  561. }
  562. }
  563. MileStoneMenu.AddItem("Edit Milestone", null, milestoneRows, EditMileStones);
  564. if (setIDs.Length == 1 && milestoneRows.Length == 1)
  565. {
  566. var attachments = milestoneRows[0].Get<JobDocumentSetMileStone, int>(x => x.Attachments);
  567. if (attachments > 1)
  568. {
  569. MileStoneMenu.AddItem("Split MileStone", null, () => { SplitMileStone(setIDs[0], milestoneRows[0]); });
  570. }
  571. }
  572. if (milestoneRows.Any())
  573. {
  574. MileStoneMenu.AddSeparator();
  575. MileStoneMenu.AddItem(milestoneRows.Length > 1 ? "Upload and Match File Names" : "Upload Files", null, milestoneRows, UploadFiles);
  576. var download = MileStoneMenu.AddItem("Download Files", null, null);
  577. download.Items.Add(new MenuItem());
  578. download.SubmenuOpened += (o, e) =>
  579. {
  580. download.Items.Clear();
  581. var files = new Client<JobDocumentSetMileStoneFile>().Query(
  582. new Filter<JobDocumentSetMileStoneFile>(x => x.EntityLink.ID).InList(milestoneIDs),
  583. new Columns<JobDocumentSetMileStoneFile>(x => x.ID)
  584. .Add(x => x.DocumentLink.FileName)
  585. .Add(x => x.DocumentLink.ID),
  586. new SortOrder<JobDocumentSetMileStoneFile>(x => x.DocumentLink.FileName)
  587. );
  588. if (files.Rows.Any())
  589. {
  590. foreach (var row in files.Rows)
  591. {
  592. download.AddItem(
  593. row.Get<JobDocumentSetMileStoneFile, String>(x => x.DocumentLink.FileName),
  594. null,
  595. () => DownloadFiles(
  596. new CoreRow[] { milestoneRows[0] },
  597. row.Get<JobDocumentSetMileStoneFile, Guid>(x => x.DocumentLink.ID)));
  598. }
  599. if (download.Items.Count > 1)
  600. {
  601. download.AddSeparator();
  602. download.AddItem("Download All", null, () => DownloadFiles(milestoneRows, Guid.Empty));
  603. }
  604. }
  605. else
  606. {
  607. download.AddItem("No Files to download", null, null, enabled: false);
  608. }
  609. };
  610. }
  611. MileStoneMenu.AddSeparator();
  612. MileStoneMenu.AddItem("Export Files", null, milestoneRows, ExportFiles);
  613. if(milestoneIDs.Length == 1)
  614. {
  615. MileStoneMenu.AddSeparatorIfNeeded();
  616. var item = MileStoneMenu.AddItem("Forms", PRSDesktop.Resources.kanban, null);
  617. DynamicGridUtils.PopulateFormMenu<JobDocumentSetMileStoneForm, JobDocumentSetMileStone, JobDocumentSetMileStoneLink>(
  618. item,
  619. milestoneIDs[0],
  620. () => new Client<JobDocumentSetMileStone>().Load(new Filter<JobDocumentSetMileStone>(x => x.ID).IsEqualTo(milestoneIDs[0])).First(),
  621. editOnAdd: true,
  622. customiseEditor: (editor) =>
  623. {
  624. if (Security.IsAllowed<CanPrintReports>())
  625. {
  626. editor.CustomButtons.Add(new DynamicFormEditButton("Attach", AttachForm));
  627. }
  628. });
  629. }
  630. MileStoneMenu.AddSeparator();
  631. MileStoneMenu.AddItem("Delete MileStone", null, milestoneRows, DeleteMileStone);
  632. }
  633. if (MileStoneMenu.Items.Count == 0)
  634. e.Handled = true;
  635. }
  636. private void AttachForm(DynamicFormEditWindow window, DynamicFormEditButton button)
  637. {
  638. var dataModel = window.DataModel;
  639. if(dataModel is null)
  640. {
  641. return;
  642. }
  643. var menu = new ContextMenu();
  644. var model = new DigitalFormReportDataModel<JobDocumentSetMileStoneForm>(
  645. new Filter<JobDocumentSetMileStoneForm>(x => x.ID).IsEqualTo(dataModel.Instance.ID),
  646. dataModel.Instance.Form.ID);
  647. model.AddFormData(dataModel.Instance.ID, window.SaveValues().ToLoadStorage());
  648. var reports = ReportUtils.LoadReports(dataModel.Instance.Form.ID.ToString(), model).Where(x => x.Visible).ToList();
  649. if(reports.Count == 1)
  650. {
  651. AttachReport((reports[0], model, dataModel));
  652. return;
  653. }
  654. else if(reports.Count > 1)
  655. {
  656. foreach (var report in reports)
  657. {
  658. menu.AddItem(report.Name, null, (report, model, dataModel), AttachReport);
  659. }
  660. }
  661. if(menu.Items.Count == 0)
  662. {
  663. menu.AddItem("No reports", null, null, enabled: false);
  664. }
  665. menu.IsOpen = true;
  666. }
  667. private void AttachReport((ReportTemplate report, DigitalFormReportDataModel<JobDocumentSetMileStoneForm> model, IDigitalFormDataModel dataModel) arg)
  668. {
  669. Progress.ShowModal("Please wait", (progress) =>
  670. {
  671. var (report, model, dataModel) = arg;
  672. var data = ReportUtils.ReportToPDF(report, model);
  673. var document = new Document
  674. {
  675. Data = data,
  676. FileName = $"Digital Form - {report.Name} - {DateTime.Now:yyyy-MM-dd hh:mm:ss.fff}.pdf",
  677. CRC = CoreUtils.CalculateCRC(data),
  678. TimeStamp = DateTime.Now
  679. };
  680. Client.Save(document, $"Generated from form report {report.Name}");
  681. var file = new JobDocumentSetMileStoneFile();
  682. file.DocumentLink.ID = document.ID;
  683. file.EntityLink.ID = dataModel.Entity.ID;
  684. Client.Save(file, "Attached from form.");
  685. });
  686. Refresh();
  687. }
  688. private void ExportFiles(CoreRow[] milestones)
  689. {
  690. var sfd = new SaveFileDialog();
  691. sfd.Filter = "Compressed Files (*.zip)|*.zip";
  692. sfd.AddExtension = true;
  693. if (sfd.ShowDialog() != DialogResult.OK)
  694. return;
  695. Progress.ShowModal("Exporting Files", (progress) =>
  696. {
  697. progress.Report("Getting File Links");
  698. var milestoneids = milestones.Select(r => r.Get<JobDocumentSetMileStone, Guid>(c => c.ID)).ToArray();
  699. var links = new Client<JobDocumentSetMileStoneFile>().Query(
  700. new Filter<JobDocumentSetMileStoneFile>(c => c.EntityLink.ID).InList(milestoneids),
  701. new Columns<JobDocumentSetMileStoneFile>(x => x.EntityLink.ID)
  702. .Add(x=>x.EntityLink.DocumentSet.Code)
  703. .Add(x=>x.EntityLink.Type.Code)
  704. .Add(x=>x.EntityLink.Revision)
  705. .Add(x=>x.EntityLink.Status)
  706. .Add(x => x.DocumentLink.ID)
  707. .Add(x=>x.DocumentLink.FileName)
  708. );
  709. Syncfusion.Compression.Zip.ZipArchive zip = new Syncfusion.Compression.Zip.ZipArchive();
  710. int i = 0;
  711. foreach (var row in links.Rows)
  712. {
  713. i++;
  714. String code = row.Get<JobDocumentSetMileStoneFile, String>(c => c.EntityLink.DocumentSet.Code);
  715. String filename = Path.GetFileNameWithoutExtension(row.Get<JobDocumentSetMileStoneFile, String>(c => c.DocumentLink.FileName));
  716. String extension = Path.GetExtension(row.Get<JobDocumentSetMileStoneFile, String>(c => c.DocumentLink.FileName));
  717. String type = $"{row.Get<JobDocumentSetMileStoneFile,String>(c=>c.EntityLink.Type.Code)} {row.Get<JobDocumentSetMileStoneFile,String>(c=>c.EntityLink.Revision)}".Trim();
  718. var status = row.Get<JobDocumentSetMileStoneFile, JobDocumentSetMileStoneStatus>(c => c.EntityLink.Status);
  719. filename = $"{code}/{filename} {type} ({status}){extension}";
  720. progress.Report($"Processing {i} of {links.Rows.Count} files");
  721. Guid docid = row.Get<JobDocumentSetMileStoneFile, Guid>(c => c.DocumentLink.ID);
  722. var data = new Client<Document>().Query(
  723. new Filter<Document>(x => x.ID).IsEqualTo(docid),
  724. new Columns<Document>(x=>x.ID).Add(x => x.Data)
  725. ).Rows.Select(r=>r.Get<Document,byte[]>(c=>c.Data)).FirstOrDefault();
  726. if (data != null)
  727. {
  728. var item = new ZipArchiveItem(zip, filename, new MemoryStream(data), true, FileAttributes.Normal);
  729. zip.AddItem(item);
  730. }
  731. }
  732. progress.Report("Closing archive");
  733. zip.Save(sfd.FileName);
  734. zip.Close();
  735. });
  736. MessageBox.Show("All Done!");
  737. }
  738. private void ShowHideDetailsColumn()
  739. {
  740. _settings.DetailsVisible = !_settings.DetailsVisible;
  741. new UserConfiguration<JobDocumentSetTreeSettings>().Save(_settings);
  742. treeGrid.Columns[1].Width = _settings.DetailsVisible ? 120: 0;
  743. }
  744. private bool PopulateFolders(MenuItem menu, IEnumerable<DocumentSetNode> documents)
  745. {
  746. var data = Client.Query(
  747. new Filter<JobDocumentSetFolder>(x => x.Job.ID).IsEqualTo(Job.ID),
  748. new Columns<JobDocumentSetFolder>(x => x.ID)
  749. .Add(x => x.Parent.ID)
  750. .Add(x => x.Name)
  751. );
  752. if (!data.Rows.Any())
  753. return false;
  754. var folders = new CoreTreeNodes();
  755. folders.Load<JobDocumentSetFolder>(data, x => x.ID, x => x.Parent.ID, x => x.Name);
  756. foreach (var folder in folders.Nodes)
  757. DoPopulateFolder(menu, folder, documents);
  758. return true;
  759. }
  760. private void DoPopulateFolder(MenuItem header, CoreTreeNode folder, IEnumerable<DocumentSetNode> documents)
  761. {
  762. var menu = header.AddItem(folder.Description, null, () => MoveToFolder(documents, folder));
  763. foreach (var childfolder in folder.Children)
  764. DoPopulateFolder(menu, childfolder, documents);
  765. }
  766. private void MoveToFolder(IEnumerable<DocumentSetNode> documents, CoreTreeNode folder)
  767. {
  768. using (new WaitCursor())
  769. {
  770. var updates = new List<JobDocumentSet>();
  771. foreach (var document in documents)
  772. {
  773. var folderid = Data.Rows.FirstOrDefault(r => r.Get<JobDocumentSet, Guid>(c => c.ID) == document.ID)?.Get<JobDocumentSet, Guid>(c => c.Folder.ID) ?? Guid.Empty;
  774. if (folderid != folder.ID)
  775. {
  776. var update = new JobDocumentSet();
  777. update.ID = document.ID;
  778. update.CommitChanges();
  779. update.Folder.ID = folder.ID;
  780. update.Parent.ID = Guid.Empty;
  781. updates.Add(update);
  782. }
  783. }
  784. if (updates.Any())
  785. new Client<JobDocumentSet>().Save(updates, "Moved to Folder: " + folder.Description);
  786. else
  787. MessageBox.Show("Nothing to Do!");
  788. }
  789. Refresh();
  790. }
  791. private void SplitMileStone(Guid setid, CoreRow milestone)
  792. {
  793. if (!MessageWindow.ShowYesNo("Are you sure you wish to split this Document Set?", "Confirm Split"))
  794. return;
  795. Guid milestoneid = milestone.Get<JobDocumentSetMileStone, Guid>(c => c.ID);
  796. var dlg = new MultiSelectDialog<JobDocumentSetMileStoneFile>(
  797. new Filter<JobDocumentSetMileStoneFile>(c => c.EntityLink.ID).IsEqualTo(milestoneid),
  798. null,
  799. true
  800. );
  801. if (dlg.ShowDialog() == true)
  802. {
  803. var files = dlg.Items();
  804. Progress.ShowModal("Splitting Document Set", (progress) =>
  805. {
  806. JobDocumentSet newset = new Client<JobDocumentSet>().Query(
  807. new Filter<JobDocumentSet>(x => x.ID).IsEqualTo(setid)
  808. ).Rows.FirstOrDefault()?.ToObject<JobDocumentSet>();
  809. if (newset != null)
  810. {
  811. newset.ID = Guid.Empty;
  812. newset.CommitChanges();
  813. newset.Parent.ID = setid;
  814. newset.Code = String.Format("{0} (COPY)", newset.Code);
  815. //newset.Description = "New Child";
  816. new Client<JobDocumentSet>().Save(newset, "Created by Splitting MileStone");
  817. progress.Report("Creating Milestone");
  818. JobDocumentSetMileStone newms = new Client<JobDocumentSetMileStone>().Query(
  819. new Filter<JobDocumentSetMileStone>(c=>c.ID).IsEqualTo(milestoneid)
  820. ).Rows.FirstOrDefault()?.ToObject<JobDocumentSetMileStone>();
  821. if (newms != null)
  822. {
  823. newms.ID = Guid.Empty;
  824. newset.CommitChanges();
  825. newms.DocumentSet.ID = newset.ID;
  826. new Client<JobDocumentSetMileStone>().Save(newms, "Created By Splitting MileStone");
  827. progress.Report("Moving Files");
  828. foreach (var file in files)
  829. file.EntityLink.ID = newms.ID;
  830. new Client<JobDocumentSetMileStoneFile>().Save(files, "Moved when Splitting MileStone");
  831. }
  832. }
  833. });
  834. Refresh();
  835. }
  836. }
  837. private void AddChildDocument(DocumentSetNode node)
  838. {
  839. if (node == null)
  840. return;
  841. var folderid = Data.Rows.FirstOrDefault(r => r.Get<JobDocumentSet, Guid>(c => c.ID) == node.ID)?.Get<JobDocumentSet, Guid>(c => c.Folder.ID) ?? Guid.Empty;
  842. JobDocumentSet newset = new JobDocumentSet();
  843. newset.Parent.ID = node.ID;
  844. newset.Job.ID = Job.ID;
  845. newset.Job.Synchronise(Job);
  846. newset.Folder.ID = folderid;
  847. var grid = new DynamicDataGrid<JobDocumentSet>();
  848. if (grid.EditItems(new[] { newset }))
  849. Refresh();
  850. }
  851. private void DownloadFiles(CoreRow[] rows, Guid id)
  852. {
  853. FolderBrowserDialog dlg = new FolderBrowserDialog();
  854. if (dlg.ShowDialog() == DialogResult.OK)
  855. {
  856. Progress.ShowModal("Downloading Files", (progress) =>
  857. {
  858. foreach (var row in rows)
  859. {
  860. var status = row.Get<JobDocumentSetMileStone, JobDocumentSetMileStoneStatus>(c => c.Status);
  861. var stage = row.Get<JobDocumentSetMileStone, String>(c => c.Type.Code);
  862. var revision = row.Get<JobDocumentSetMileStone, String>(c => c.Revision);
  863. String tag = String.Format(" - {0}{1} ({2})", stage, String.IsNullOrWhiteSpace(revision) ? "" : " - Rev " + revision,
  864. status.ToString().SplitCamelCase());
  865. var filter = id == Guid.Empty
  866. ? new Filter<Document>(x => x.ID).InQuery(
  867. new Filter<JobDocumentSetMileStoneFile>(x => x.EntityLink.ID).IsEqualTo(
  868. row.Get<JobDocumentSetMileStone, Guid>(c => c.ID)),
  869. x => x.DocumentLink.ID
  870. )
  871. : new Filter<Document>(x => x.ID).IsEqualTo(id);
  872. var files = new Client<Document>().Query(filter);
  873. foreach (var filerow in files.Rows)
  874. {
  875. string filename = filerow.Get<Document, String>(c => c.FileName);
  876. string extension = Path.GetExtension(filename);
  877. string basefilename = Path.GetFileNameWithoutExtension(filename);
  878. filename = String.Format("{0}{1}{2}", basefilename, tag, extension);
  879. filename = Path.Combine(dlg.SelectedPath, filename);
  880. File.WriteAllBytes(filename, filerow.Get<Document, byte[]>(c => c.Data));
  881. }
  882. }
  883. });
  884. Process.Start(new ProcessStartInfo(dlg.SelectedPath) { UseShellExecute = true });
  885. }
  886. }
  887. private bool SelectFiles(out string[] files)
  888. {
  889. Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
  890. dlg.Filter = "PDF Files (*.pdf)|*.pdf";
  891. dlg.Multiselect = true;
  892. if ((dlg.ShowDialog() == true) && (dlg.FileNames.Length > 0))
  893. {
  894. files = dlg.FileNames.ToArray();
  895. return true;
  896. }
  897. files = null;
  898. return false;
  899. }
  900. private Document CreateDocument(string file, Stream? stream)
  901. {
  902. var filename = Path.GetFileName(file).ToLower();
  903. Document doc;
  904. if (Path.GetExtension(filename) != ".pdf")
  905. {
  906. filename = Path.ChangeExtension(filename, ".pdf");
  907. if (stream is null)
  908. {
  909. var data = DataEntryReGroupWindow.RenderToPDF(file).SaveToBytes();
  910. doc = new Document
  911. {
  912. Data = data,
  913. FileName = filename,
  914. CRC = CoreUtils.CalculateCRC(data),
  915. TimeStamp = new FileInfo(filename).LastWriteTime
  916. };
  917. }
  918. else
  919. {
  920. var data = DataEntryReGroupWindow.RenderToPDF(file, stream).SaveToBytes();
  921. doc = new Document
  922. {
  923. Data = data,
  924. FileName = filename,
  925. CRC = CoreUtils.CalculateCRC(data),
  926. TimeStamp = DateTime.Now
  927. };
  928. }
  929. }
  930. else if (stream is null)
  931. {
  932. doc = Document.FromFile(file);
  933. doc.FileName = filename;
  934. }
  935. else
  936. {
  937. byte[] data;
  938. using (var ms = new MemoryStream())
  939. {
  940. stream.CopyTo(ms);
  941. data = ms.ToArray();
  942. }
  943. doc = new Document
  944. {
  945. Data = data,
  946. FileName = filename,
  947. CRC = CoreUtils.CalculateCRC(data),
  948. TimeStamp = new FileInfo(file).LastWriteTime
  949. };
  950. }
  951. return doc;
  952. }
  953. private void UploadFiles(CoreRow[] rows, IEnumerable<Tuple<string, Stream?>> fileStreams)
  954. {
  955. var files = fileStreams.AsArray();
  956. var setlookups = rows.Length > 1
  957. ? new Dictionary<string, Guid>(
  958. rows.Select(
  959. r => new KeyValuePair<String, Guid>(
  960. r.Get<JobDocumentSetMileStone, String>(c => c.DocumentSet.Code),
  961. r.Get<JobDocumentSetMileStone, Guid>(c => c.ID)
  962. )
  963. )
  964. )
  965. : null;
  966. if ((setlookups != null) && (rows.Length > 1))
  967. {
  968. var unmatched = files.Where(x => !setlookups.Keys.Any(key => Path.GetFileName(x.Item1).ToLower().ToLower().StartsWith(key.ToLower())));
  969. if (unmatched.Any())
  970. {
  971. MessageBox.Show("Unable to match the following files:\n" + String.Join("\n", unmatched));
  972. return;
  973. }
  974. }
  975. var milestoneids = rows.Select(r => r.Get<JobDocumentSetMileStone, Guid>(c => c.ID)).ToArray();
  976. var jobdocsetfiles = Client.Query(
  977. new Filter<JobDocumentSetMileStoneFile>(x => x.EntityLink.ID).InList(milestoneids),
  978. new Columns<JobDocumentSetMileStoneFile>(x => x.ID, x => x.DocumentLink.ID)
  979. .Add(x => x.DocumentLink.FileName))
  980. .ToObjects<JobDocumentSetMileStoneFile>().ToList();
  981. var currentfiles = jobdocsetfiles.ToDictionary(x => x.DocumentLink.FileName.ToLower(), x => x.DocumentLink.ID);
  982. var matched = files.Select(x => x.Item1).Where(x => currentfiles.ContainsKey(Path.GetFileName(x).ToLower()));
  983. var replace = false;
  984. if (matched.Any())
  985. {
  986. var result = MessageWindow.New()
  987. .Title("Replace Files")
  988. .Message("The following files already exist!\n\n Do you wish to replace them?\n\n" + String.Join("\n", matched))
  989. .AddYesButton("Replace Files")
  990. .AddNoButton("Keep Existing")
  991. .AddCancelButton()
  992. .Display().Result;
  993. if (result == MessageWindowResult.Cancel) return;
  994. replace = result == MessageWindowResult.Yes;
  995. }
  996. int doccount = 0;
  997. Progress.ShowModal("Uploading Files", (progress) =>
  998. {
  999. var documents = new List<Document>();
  1000. var newDocuments = new List<Document>();
  1001. var linked = new List<JobDocumentSetMileStoneFile>();
  1002. foreach (var (file, stream) in files)
  1003. {
  1004. var filename = Path.GetFileName(file).ToLower();
  1005. if (replace || !matched.Contains(filename))
  1006. {
  1007. var doc = CreateDocument(file, stream);
  1008. documents.Add(doc);
  1009. if (currentfiles.TryGetValue(filename, out var docID))
  1010. {
  1011. doc.SetObserving(false);
  1012. doc.ID = docID;
  1013. doc.SetObserving(true);
  1014. var linkedfile = jobdocsetfiles.FirstOrDefault(x => x.DocumentLink.ID == doc.ID);
  1015. if (linkedfile != null)
  1016. {
  1017. linkedfile.DocumentLink.ID = doc.ID;
  1018. // Regenerate the thumbnail
  1019. linkedfile.Thumbnail = null;
  1020. linked.Add(linkedfile);
  1021. }
  1022. }
  1023. else
  1024. {
  1025. newDocuments.Add(doc);
  1026. }
  1027. }
  1028. }
  1029. Client.Save(documents, "Uploaded by User");
  1030. progress.Report("Updating Links");
  1031. foreach (var document in newDocuments)
  1032. {
  1033. var link = new JobDocumentSetMileStoneFile();
  1034. if (setlookups != null)
  1035. {
  1036. var filename = Path.GetFileName(document.FileName).ToLower();
  1037. var code = setlookups.Keys.FirstOrDefault(key => filename.StartsWith(key.ToLower()));
  1038. link.EntityLink.ID = setlookups[code];
  1039. }
  1040. else
  1041. link.EntityLink.ID = rows.First().Get<JobDocumentSetMileStone, Guid>(c => c.ID);
  1042. link.DocumentLink.ID = document.ID;
  1043. linked.Add(link);
  1044. }
  1045. Client.Save(linked, "Uploaded by User");
  1046. doccount = documents.Count;
  1047. });
  1048. MessageWindow.ShowMessage(string.Format("{0} Files Uploaded", doccount > 0 ? doccount : "No"), "Success");
  1049. Refresh();
  1050. }
  1051. private void UploadFiles(CoreRow[] rows)
  1052. {
  1053. if (rows.Length < 1)
  1054. {
  1055. MessageBox.Show("No Rows Selected");
  1056. return;
  1057. }
  1058. if (SelectFiles(out String[] filenames))
  1059. {
  1060. UploadFiles(rows, filenames.Select(x => new Tuple<string, Stream?>(x, null)));
  1061. }
  1062. }
  1063. private Dictionary<Guid, JobDocumentSetMileStone> GetPreviousMileStones(Guid[] setids, Guid typeid)
  1064. {
  1065. var result = new Dictionary<Guid, JobDocumentSetMileStone>();
  1066. foreach (var setid in setids)
  1067. {
  1068. var typeindex = _types.Keys.IndexOf(typeid);
  1069. JobDocumentSetMileStone? last = null;
  1070. while ((last == null) && (typeindex > 0))
  1071. {
  1072. last = _milestones.Rows.LastOrDefault(r =>
  1073. (r.Get<JobDocumentSetMileStone, Guid>(c => c.DocumentSet.ID) == setid) &&
  1074. (r.Get<JobDocumentSetMileStone, Guid>(c => c.Type.ID) == _types.Keys.ToArray()[typeindex]))
  1075. ?.ToObject<JobDocumentSetMileStone>();
  1076. typeindex--;
  1077. }
  1078. if (last != null)
  1079. result[setid] = last;
  1080. }
  1081. return result;
  1082. }
  1083. private void CreateMileStone(Guid[] setids, Guid typeid, DateTime duedate)
  1084. {
  1085. bool bCopy = false;
  1086. var lastmilestones = GetPreviousMileStones(setids, typeid);
  1087. if (lastmilestones.Any(x => x.Value.Attachments > 0))
  1088. {
  1089. var confirm = MessageBox.Show("Do you wish to copy the files from the previous milestones?", "Copy Files",
  1090. MessageBoxButton.YesNoCancel);
  1091. if (confirm == MessageBoxResult.Cancel)
  1092. return;
  1093. bCopy = confirm == MessageBoxResult.Yes;
  1094. }
  1095. Dictionary<JobDocumentSetMileStone,JobDocumentSetMileStoneFile[]> updates = new Dictionary<JobDocumentSetMileStone, JobDocumentSetMileStoneFile[]>();
  1096. foreach (var setid in setids)
  1097. {
  1098. JobDocumentSetMileStone milestone = new JobDocumentSetMileStone();
  1099. milestone.DocumentSet.ID = setid;
  1100. milestone.Type.ID = typeid;
  1101. milestone.Status = JobDocumentSetMileStoneStatus.NotStarted;
  1102. milestone.Due = duedate;
  1103. JobDocumentSetMileStoneFile[] files = new JobDocumentSetMileStoneFile[] { };
  1104. if (bCopy && lastmilestones.TryGetValue(setid, out var lastmilestone))
  1105. {
  1106. if (lastmilestone.Attachments > 0)
  1107. {
  1108. files = new Client<JobDocumentSetMileStoneFile>().Query(
  1109. new Filter<JobDocumentSetMileStoneFile>(x => x.EntityLink.ID).InList(lastmilestone.ID),
  1110. new Columns<JobDocumentSetMileStoneFile>(x=>x.EntityLink.DocumentSet.ID)
  1111. .Add(x => x.DocumentLink.FileName)
  1112. .Add(x => x.DocumentLink.ID),
  1113. new SortOrder<JobDocumentSetMileStoneFile>(x => x.DocumentLink.FileName)
  1114. ).Rows.Select(r=>r.ToObject<JobDocumentSetMileStoneFile>()).ToArray();
  1115. }
  1116. }
  1117. updates[milestone] = files;
  1118. }
  1119. var grid = new JobDocumentSetMileStoneGrid();
  1120. grid.OnAfterSave += (editor, items) =>
  1121. {
  1122. if (updates.Keys.Count == 1)
  1123. return;
  1124. List<JobDocumentSetMileStoneFile> fileupdates = new List<JobDocumentSetMileStoneFile>();
  1125. foreach (var milestone in updates.Keys)
  1126. {
  1127. foreach (var file in updates[milestone])
  1128. {
  1129. file.EntityLink.ID = milestone.ID;
  1130. fileupdates.Add(file);
  1131. }
  1132. }
  1133. if (fileupdates.Any())
  1134. new Client<JobDocumentSetMileStoneFile>().Save(fileupdates,"");
  1135. };
  1136. if (grid.EditItems(updates.Keys.ToArray(), (t) =>
  1137. {
  1138. if ((t == typeof(JobDocumentSetMileStoneFile)) && (updates.Keys.Count == 1))
  1139. {
  1140. CoreTable result = new CoreTable();
  1141. result.LoadColumns(typeof(JobDocumentSetMileStoneFile));
  1142. result.LoadRows(updates[updates.Keys.First()]);
  1143. return result;
  1144. }
  1145. return null;
  1146. }, true))
  1147. {
  1148. Refresh();
  1149. }
  1150. }
  1151. private void ChangeMileStoneStatus(CoreRow[] rows, JobDocumentSetMileStoneStatus newstatus, DateTime? issued, DateTime? closed)
  1152. {
  1153. var milestones = rows.Select(r=>r.ToObject<JobDocumentSetMileStone>()).ToArray();
  1154. foreach (var milestone in milestones)
  1155. {
  1156. if (issued.HasValue)
  1157. milestone.Submitted = issued.Value;
  1158. if (closed.HasValue)
  1159. milestone.Closed = closed.Value;
  1160. milestone.Status = newstatus;
  1161. }
  1162. using (new WaitCursor())
  1163. new Client<JobDocumentSetMileStone>().Save(milestones, "Changed Status to " + newstatus.ToString().SplitCamelCase());
  1164. Refresh();
  1165. }
  1166. private void EditMileStones(CoreRow[] rows)
  1167. {
  1168. var ids = rows.Select(r => r.Get<JobDocumentSetMileStone, Guid>(x => x.ID)).ToArray();
  1169. var milestones = new Client<JobDocumentSetMileStone>().Query(
  1170. new Filter<JobDocumentSetMileStone>(x => x.ID).InList(ids)
  1171. ).Rows.Select(r=>r.ToObject<JobDocumentSetMileStone>()).ToArray();
  1172. var grid = new JobDocumentSetMileStoneGrid();
  1173. if (grid.EditItems(milestones))
  1174. Refresh();
  1175. }
  1176. private void DeleteMileStone(CoreRow[] rows)
  1177. {
  1178. var milestones = rows.Select(r=>r.ToObject<JobDocumentSetMileStone>()).ToArray();
  1179. using (new WaitCursor())
  1180. new Client<JobDocumentSetMileStone>().Delete(milestones,"Deleted by User");
  1181. Refresh();
  1182. }
  1183. private void TreeGrid_OnCellToolTipOpening(object? sender, TreeGridCellToolTipOpeningEventArgs e)
  1184. {
  1185. var column = e.Column.MappingName.Replace("Blocks[", "").Replace("]", "");
  1186. var data = (e.Record as DocumentSetNode).Blocks[column];
  1187. if (String.IsNullOrWhiteSpace(data))
  1188. return;
  1189. var block = Serialization.Deserialize<JobDocumentSetMileStoneBlock>(data.ToString());
  1190. Guid id = block.ID;
  1191. TextBlock text = new TextBlock();
  1192. if (!String.IsNullOrWhiteSpace(block.Notes))
  1193. {
  1194. text.Inlines.Add(new Run("Milestone Notes\n") { FontWeight = FontWeights.Bold, TextDecorations = TextDecorations.Underline });
  1195. text.Inlines.Add(new Run(block.Notes.Replace("=", "").Replace("\n\n", "\n")) { FontStyle = FontStyles.Italic });
  1196. }
  1197. MultiQuery query = new MultiQuery();
  1198. if (block.Attachments > 0)
  1199. {
  1200. query.Add<JobDocumentSetMileStoneFile>(
  1201. new Filter<JobDocumentSetMileStoneFile>(x => x.EntityLink.ID).IsEqualTo(block.ID),
  1202. new Columns<JobDocumentSetMileStoneFile>(x => x.DocumentLink.FileName),
  1203. new SortOrder<JobDocumentSetMileStoneFile>(x => x.DocumentLink.FileName)
  1204. );
  1205. }
  1206. if (block.Kanbans > 0)
  1207. {
  1208. query.Add<JobDocumentSetMileStoneKanban>(
  1209. new Filter<JobDocumentSetMileStoneKanban>(x => x.Entity.ID).IsEqualTo(block.ID),
  1210. new Columns<JobDocumentSetMileStoneKanban>(x => x.Kanban.Number)
  1211. .Add(x => x.Kanban.Title)
  1212. .Add(x => x.Kanban.Completed),
  1213. new SortOrder<JobDocumentSetMileStoneKanban>(x => x.Kanban.Number)
  1214. );
  1215. }
  1216. if (query.Count > 0)
  1217. {
  1218. query.Query();
  1219. if (query.Contains<JobDocumentSetMileStoneFile>())
  1220. {
  1221. var files = query.Get<JobDocumentSetMileStoneFile>();
  1222. if (files.Rows.Any())
  1223. {
  1224. if (text.Inlines.Any())
  1225. text.Inlines.Add(new Run("\n\n"));
  1226. text.Inlines.Add(new Run("Uploaded Files") { FontWeight = FontWeights.Bold, TextDecorations = TextDecorations.Underline });
  1227. foreach (var row in files.Rows)
  1228. text.Inlines.Add(new Run("\n" + row.Get<JobDocumentSetMileStoneFile, String>(c => c.DocumentLink.FileName))
  1229. { FontStyle = FontStyles.Italic });
  1230. }
  1231. }
  1232. if (query.Contains<JobDocumentSetMileStoneKanban>())
  1233. {
  1234. var tasks = query.Get<JobDocumentSetMileStoneKanban>();
  1235. if (tasks.Rows.Any())
  1236. {
  1237. if (text.Inlines.Any())
  1238. text.Inlines.Add(new Run("\n\n"));
  1239. text.Inlines.Add(new Run("Task History") { FontWeight = FontWeights.Bold, TextDecorations = TextDecorations.Underline });
  1240. foreach (var row in tasks.Rows)
  1241. text.Inlines.Add(
  1242. new Run(
  1243. String.Format(
  1244. "\n{0}: {1}",
  1245. row.Get<JobDocumentSetMileStoneKanban, int>(c => c.Kanban.Number),
  1246. row.Get<JobDocumentSetMileStoneKanban, String>(c => c.Kanban.Title)
  1247. )
  1248. )
  1249. {
  1250. FontStyle = FontStyles.Italic,
  1251. TextDecorations = row.Get<JobDocumentSetMileStoneKanban, DateTime>(x => x.Kanban.Completed).IsEmpty()
  1252. ? null
  1253. : TextDecorations.Strikethrough
  1254. });
  1255. }
  1256. }
  1257. }
  1258. if (!text.Inlines.Any())
  1259. {
  1260. e.ToolTip.Template = null;
  1261. return;
  1262. }
  1263. e.ToolTip.Template = TemplateGenerator.CreateControlTemplate(
  1264. typeof(System.Windows.Controls.ToolTip),
  1265. () =>
  1266. {
  1267. var border = new Border
  1268. {
  1269. BorderBrush = new SolidColorBrush(Colors.Gray),
  1270. BorderThickness = new Thickness(0.75),
  1271. CornerRadius = new CornerRadius(5),
  1272. Background = new SolidColorBrush(Colors.LightYellow),
  1273. Padding = new Thickness(5),
  1274. Child = text
  1275. };
  1276. return border;
  1277. }
  1278. );
  1279. }
  1280. #endregion
  1281. #region Button Bar Actions
  1282. private void AddTypes(MenuItem parent, Action<Guid> addfunction)
  1283. {
  1284. if(_types.Count == 0)
  1285. {
  1286. MenuItem item = new MenuItem() { Header = "No Document Milestones", IsEnabled = false };
  1287. parent.Items.Add(item);
  1288. }
  1289. else
  1290. {
  1291. foreach (var type in _types.Keys)
  1292. {
  1293. MenuItem item = new MenuItem() { Header = _types[type].Description, Tag = type };
  1294. item.Click += (o, e) => addfunction(type);
  1295. parent.Items.Add(item);
  1296. }
  1297. }
  1298. }
  1299. private void Add_OnClick(object sender, RoutedEventArgs e)
  1300. {
  1301. if (FolderIDs?.Any() != true)
  1302. {
  1303. MessageBox.Show("Please choose a Folder first!");
  1304. return;
  1305. }
  1306. else if(FolderIDs.First() == CoreUtils.FullGuid)
  1307. {
  1308. MessageBox.Show("Cannot add items to this folder.");
  1309. return;
  1310. }
  1311. ContextMenu menu = new ContextMenu();
  1312. var onetoone = new MenuItem() { Header = "Add Individual Files" };
  1313. AddTypes(onetoone, AddOneToOneFiles);
  1314. menu.Items.Add(onetoone);
  1315. var manytoone = new MenuItem() { Header = "Add Sets of Files" };
  1316. AddTypes(manytoone, AddManyToOneFiles);
  1317. menu.Items.Add(manytoone);
  1318. menu.Items.Add(new Separator());
  1319. var manual = new MenuItem() { Header = "Add Document Set Manually" };
  1320. manual.Click += (o, e) => { AddDocumentSet(); };
  1321. menu.Items.Add(manual);
  1322. menu.IsOpen = true;
  1323. }
  1324. private void AddOneToOneFiles(Guid type)
  1325. {
  1326. Guid folderid = FolderIDs?.FirstOrDefault() ?? Guid.Empty;
  1327. if (!SelectFiles(out String[] filenames))
  1328. return;
  1329. Progress.ShowModal("Preparing Upload", (progress) =>
  1330. {
  1331. Dictionary<String, Tuple<Document, JobDocumentSet, JobDocumentSetMileStone, JobDocumentSetMileStoneFile>> map =
  1332. new Dictionary<string, Tuple<Document, JobDocumentSet, JobDocumentSetMileStone, JobDocumentSetMileStoneFile>>();
  1333. foreach (var filename in filenames)
  1334. {
  1335. var data = File.ReadAllBytes(filename);
  1336. Document document = new Document()
  1337. {
  1338. FileName = Path.GetFileName(filename).ToLower(),
  1339. Data = data,
  1340. CRC = CoreUtils.CalculateCRC(data),
  1341. TimeStamp = new FileInfo(filename).LastWriteTime
  1342. };
  1343. JobDocumentSet set = new JobDocumentSet();
  1344. set.Job.ID = Job.ID;
  1345. set.Job.Synchronise(Job);
  1346. set.Folder.ID = folderid;
  1347. set.Code = Path.GetFileNameWithoutExtension(filename).ToUpper();
  1348. set.Description = Path.GetFileNameWithoutExtension(filename).ToUpper();
  1349. set.Discipline.ID = DisciplineID;
  1350. set.Type.ID = TypeID;
  1351. set.Category.ID = CategoryID;
  1352. set.Area.ID = AreaID;
  1353. JobDocumentSetMileStone milestone = new JobDocumentSetMileStone();
  1354. milestone.Type.ID = type;
  1355. milestone.Status = JobDocumentSetMileStoneStatus.InProgress;
  1356. milestone.Due = DateTime.Today;
  1357. JobDocumentSetMileStoneFile file = new JobDocumentSetMileStoneFile();
  1358. map[filename] = new Tuple<Document, JobDocumentSet, JobDocumentSetMileStone, JobDocumentSetMileStoneFile>(
  1359. document,
  1360. set,
  1361. milestone,
  1362. file
  1363. );
  1364. }
  1365. progress.Report("Uploading Files");
  1366. var docs = map.Select(x => x.Value.Item1);
  1367. new Client<Document>().Save(docs, "Uploaded By File Selection");
  1368. progress.Report("Creating Document Sets");
  1369. var sets = map.Select(x => x.Value.Item2);
  1370. new Client<JobDocumentSet>().Save(sets, "Uploaded by File Selection");
  1371. progress.Report("Creating MileStones");
  1372. foreach (var key in map.Keys)
  1373. map[key].Item3.DocumentSet.ID = map[key].Item2.ID;
  1374. var milestones = map.Select(x => x.Value.Item3);
  1375. new Client<JobDocumentSetMileStone>().Save(milestones, "Uploaded by File Selection");
  1376. progress.Report("Linking Documents");
  1377. foreach (var key in map.Keys)
  1378. {
  1379. map[key].Item4.EntityLink.ID = map[key].Item3.ID;
  1380. map[key].Item4.DocumentLink.ID = map[key].Item1.ID;
  1381. }
  1382. var files = map.Select(x => x.Value.Item4);
  1383. new Client<JobDocumentSetMileStoneFile>().Save(files, "Uploaded by File Selection");
  1384. });
  1385. MessageBox.Show(String.Format("{0} Document Sets Created", filenames.Length));
  1386. Refresh();
  1387. }
  1388. private void AddManyToOneFiles(Guid type)
  1389. {
  1390. Guid folderid = FolderIDs?.FirstOrDefault() ?? Guid.Empty;
  1391. if (!SelectFiles(out String[] filenames))
  1392. return;
  1393. JobDocumentSet set = new JobDocumentSet();
  1394. set.Job.ID = Job.ID;
  1395. set.Job.Synchronise(Job);
  1396. set.Folder.ID = folderid;
  1397. set.Discipline.ID = DisciplineID;
  1398. set.Type.ID = TypeID;
  1399. set.Category.ID = CategoryID;
  1400. set.Area.ID = AreaID;
  1401. var grid = new DynamicDataGrid<JobDocumentSet>();
  1402. grid.OnAfterSave += (form, items) =>
  1403. {
  1404. Progress.ShowModal("Creating MileStone", (progress) =>
  1405. {
  1406. JobDocumentSetMileStone milestone = new JobDocumentSetMileStone();
  1407. milestone.DocumentSet.ID = set.ID;
  1408. milestone.Type.ID = type;
  1409. milestone.Status = JobDocumentSetMileStoneStatus.InProgress;
  1410. milestone.Due = DateTime.Today;
  1411. new Client<JobDocumentSetMileStone>().Save(milestone, "Uploaded By File Selection");
  1412. progress.Report("Uploading Files");
  1413. List<Document> documents = new List<Document>();
  1414. foreach (var filename in filenames)
  1415. {
  1416. var data = File.ReadAllBytes(filename);
  1417. Document document = new Document()
  1418. {
  1419. FileName = Path.GetFileName(filename).ToLower(),
  1420. Data = data,
  1421. CRC = CoreUtils.CalculateCRC(data),
  1422. TimeStamp = new FileInfo(filename).LastWriteTime
  1423. };
  1424. documents.Add(document);
  1425. new Client<Document>().Save(documents, "Uploaded by File Selection");
  1426. }
  1427. progress.Report("Creating File Links");
  1428. List<JobDocumentSetMileStoneFile> files = new List<JobDocumentSetMileStoneFile>();
  1429. foreach (var document in documents)
  1430. {
  1431. JobDocumentSetMileStoneFile file = new JobDocumentSetMileStoneFile();
  1432. file.EntityLink.ID = milestone.ID;
  1433. file.DocumentLink.ID = document.ID;
  1434. files.Add(file);
  1435. }
  1436. new Client<JobDocumentSetMileStoneFile>().Save(files, "Uploaded by File Selection");
  1437. });
  1438. };
  1439. if (grid.EditItems(new[] { set }))
  1440. {
  1441. MessageBox.Show(String.Format("{0} files uploaded", filenames.Length));
  1442. Refresh();
  1443. }
  1444. }
  1445. private void AddDocumentSet()
  1446. {
  1447. Guid folderid = FolderIDs?.FirstOrDefault() ?? Guid.Empty;
  1448. JobDocumentSet set = new JobDocumentSet();
  1449. set.Job.ID = Job.ID;
  1450. set.Job.Synchronise(Job);
  1451. set.Folder.ID = folderid;
  1452. set.Discipline.ID = DisciplineID;
  1453. set.Type.ID = TypeID;
  1454. set.Category.ID = CategoryID;
  1455. set.Area.ID = AreaID;
  1456. var grid = new DynamicDataGrid<JobDocumentSet>();
  1457. if (grid.EditItems(new[] { set }))
  1458. Refresh();
  1459. }
  1460. private void Edit_OnClick(object sender, RoutedEventArgs e)
  1461. {
  1462. if (treeGrid.SelectedItem == null)
  1463. {
  1464. MessageBox.Show("Please choose a Document Set first");
  1465. return;
  1466. }
  1467. Guid[] setIDs = treeGrid.SelectedItems.Select(x => (x as DocumentSetNode).ID).ToArray();
  1468. EditDocumentSets(setIDs);
  1469. }
  1470. private void EditDocumentSets(Guid[] setIDs)
  1471. {
  1472. var sets = new Client<JobDocumentSet>().Query(
  1473. new Filter<JobDocumentSet>(x => x.ID).InList(setIDs)
  1474. ).Rows.Select(x => x.ToObject<JobDocumentSet>()).ToArray();
  1475. var grid = new DynamicDataGrid<JobDocumentSet>();
  1476. // grid.OnCustomiseEditor += (form, items, column, editor) =>
  1477. // {
  1478. // if (String.Equals(column.ColumnName, "Discipline.ID"))
  1479. // editor.Editable = DisciplineVisible ? Editable.Enabled : Editable.Hidden;
  1480. // if (String.Equals(column.ColumnName, "Type.ID"))
  1481. // editor.Editable = TypeVisible ? Editable.Enabled : Editable.Hidden;
  1482. // if (String.Equals(column.ColumnName, "Category.ID"))
  1483. // editor.Editable = CategoryVisible ? Editable.Enabled : Editable.Hidden;
  1484. // if (String.Equals(column.ColumnName, "Area.ID"))
  1485. // editor.Editable = AreaVisible ? Editable.Enabled : Editable.Hidden;
  1486. // };
  1487. if (grid.EditItems(sets))
  1488. UpdateNodes(sets);
  1489. }
  1490. private void UpdateNodes(IEnumerable<JobDocumentSet> sets)
  1491. {
  1492. if (_documentsets == null)
  1493. return;
  1494. foreach (var set in sets)
  1495. {
  1496. var node = _documentsets.GetNode(set.ID);
  1497. if (node != null)
  1498. {
  1499. var tags = new List<String>()
  1500. {
  1501. set.Discipline.Description,
  1502. set.Type.Description,
  1503. set.Category.Description,
  1504. set.Area.Description
  1505. }.Where(x=>!String.IsNullOrWhiteSpace(x)).Distinct().ToArray();
  1506. JobDocumentSetDescriptionBlock desc = new JobDocumentSetDescriptionBlock(
  1507. set.ID, set.Code, set.Description, tags);
  1508. node.Description = Serialization.Serialize(desc);
  1509. JobDocumentSetDetailsBlock dets = new JobDocumentSetDetailsBlock()
  1510. {
  1511. ID = set.ID,
  1512. Date = set.Date,
  1513. Size = set.Size,
  1514. Scale = set.Scale,
  1515. Employee = set.Employee.Name
  1516. };
  1517. node.Details = Serialization.Serialize(dets);
  1518. }
  1519. }
  1520. }
  1521. private void HideRejected_OnClick(object sender, RoutedEventArgs e)
  1522. {
  1523. _hidesuperceded = !_hidesuperceded;
  1524. HideSupercededLabel.Content = _hidesuperceded ? "Show All" : "Last Only";
  1525. Refresh();
  1526. }
  1527. private void Delete_OnClick(object sender, RoutedEventArgs e)
  1528. {
  1529. if ((treeGrid.SelectedItems == null) || !treeGrid.SelectedItems.Any())
  1530. {
  1531. MessageBox.Show("Please choose a Document Set first");
  1532. return;
  1533. }
  1534. if (MessageBox.Show(
  1535. "Are you sure you wish to delete the selected Document Sets?",
  1536. "Confirm Delete",
  1537. MessageBoxButton.YesNo
  1538. ) != MessageBoxResult.Yes)
  1539. return;
  1540. List<JobDocumentSet> updates = new List<JobDocumentSet>();
  1541. List<DocumentSetNode> orphans = new List<DocumentSetNode>();
  1542. var items = treeGrid.SelectedItems.Select(x => (DocumentSetNode)x).ToArray();
  1543. foreach (DocumentSetNode item in items)
  1544. {
  1545. var children = item.Children.Where(x => !items.Contains(x));
  1546. if (children.Any())
  1547. orphans.AddRange(children);
  1548. }
  1549. if (orphans.Any())
  1550. {
  1551. var confirm = MessageBox.Show(
  1552. "These Document Sets contain children!\nDo you wish to delete these as well?",
  1553. "Delete Children",
  1554. MessageBoxButton.YesNoCancel
  1555. );
  1556. if (confirm == MessageBoxResult.Cancel)
  1557. return;
  1558. if (confirm == MessageBoxResult.No)
  1559. {
  1560. foreach (var orphan in orphans)
  1561. {
  1562. var update = new JobDocumentSet();
  1563. update.ID = orphan.ID;
  1564. update.Parent.ID = Guid.Empty;
  1565. updates.Add(update);
  1566. }
  1567. return;
  1568. }
  1569. }
  1570. Progress.ShowModal("Deleting Document Set",(progress) =>
  1571. {
  1572. if (updates.Any())
  1573. new Client<JobDocumentSet>().Save(updates, "Parent Document Deleted");
  1574. var deletes = items.Select(x=>new JobDocumentSet() { ID = x.ID }).ToArray();
  1575. new Client<JobDocumentSet>().Delete(deletes, "Deleted By User");
  1576. });
  1577. Refresh();
  1578. }
  1579. #endregion
  1580. private void FlatList_OnClick(object sender, RoutedEventArgs e)
  1581. {
  1582. _flatlist = !_flatlist;
  1583. FlatListLabel.Content = _flatlist ? "Tree View" : "Flat List";
  1584. Refresh();
  1585. }
  1586. private void IncludeRetired_OnClick(object sender, RoutedEventArgs e)
  1587. {
  1588. _includeretired = !_includeretired;
  1589. FlatListLabel.Content = _includeretired ? "Active Only" : "Include Retired";
  1590. Refresh();
  1591. }
  1592. private void TreeGrid_OnSelectionChanged(object? sender, GridSelectionChangedEventArgs e)
  1593. {
  1594. //var treeColumn = treeGrid.Columns[e.CurrentRowColumnIndex.ColumnIndex];
  1595. //var column = treeColumn.MappingName.Replace("Blocks[","").Replace("]","");
  1596. // var column = e.Column.MappingName.Replace("Blocks[","").Replace("]","");
  1597. // var data = (e.Record as DocumentSetNode).Blocks[column];
  1598. // if (String.IsNullOrWhiteSpace(data))
  1599. // return;
  1600. //
  1601. // var block = Serialization.Deserialize<JobDocumentSetMileStoneBlock>(data.ToString());
  1602. // Guid id = block.ID;
  1603. }
  1604. private void TreeGrid_OnCurrentCellActivated(object? sender, CurrentCellActivatedEventArgs e)
  1605. {
  1606. var node = treeGrid.CurrentItem as DocumentSetNode;
  1607. if (node == null)
  1608. return;
  1609. var treeColumn = treeGrid.Columns[e.CurrentRowColumnIndex.ColumnIndex];
  1610. var column = treeColumn.MappingName.Replace("Blocks[","").Replace("]","");
  1611. if (!node.Blocks.ContainsKey(column))
  1612. MileStoneSelected(null);
  1613. else
  1614. {
  1615. var block = Serialization.Deserialize<JobDocumentSetMileStoneBlock>(node.Blocks[column]);
  1616. MileStoneSelected?.Invoke(block);
  1617. }
  1618. }
  1619. private void TreeGrid_OnCellDoubleTapped(object? sender, TreeGridCellDoubleTappedEventArgs e)
  1620. {
  1621. var set = e.Record as DocumentSetNode;
  1622. if (set != null)
  1623. EditDocumentSets(new Guid[] { set.ID });
  1624. }
  1625. private void Export_OnClick(object sender, RoutedEventArgs e)
  1626. {
  1627. var engine = new ExcelEngine();
  1628. var application = engine.Excel;
  1629. var workbook = application.Workbooks.Create(1);
  1630. workbook.Version = ExcelVersion.Excel2007;
  1631. var sheet = workbook.Worksheets[0];
  1632. sheet.Name = "Document Register";
  1633. int iRow = 1;
  1634. int iCol = 1;
  1635. SetHeader(sheet, iRow, iCol++, "Document Number", 40, false, false);
  1636. SetHeader(sheet, iRow, iCol++, "Description", 80, false, false);
  1637. SetHeader(sheet, iRow, iCol++, "Discipline", 15, true, false);
  1638. SetHeader(sheet, iRow, iCol++, "Type", 15, true, false);
  1639. SetHeader(sheet, iRow, iCol++, "Category", 15, true, false);
  1640. SetHeader(sheet, iRow, iCol++, "ITP Area", 15, true, false);
  1641. Dictionary<String, int> blkmap = new Dictionary<string, int>();
  1642. foreach (var key in _types.Keys)
  1643. {
  1644. var _type = _types[key];
  1645. SetHeader(sheet, iRow, iCol, _type.Code, 5, true, true);
  1646. sheet.Range[iRow, iCol].Text = _type.Code;
  1647. int iCount = _types[key].Columns.Count;
  1648. if (iCount > 1)
  1649. sheet.Range[iRow, iCol, iRow, iCol + iCount - 1].Merge();
  1650. foreach (var col in _type.Columns)
  1651. {
  1652. sheet.SetColumnWidth(iCol,5);
  1653. blkmap[col] = iCol++;
  1654. }
  1655. }
  1656. iRow++;
  1657. foreach (var node in _documentsets.Nodes)
  1658. {
  1659. var desc = Serialization.Deserialize<JobDocumentSetDescriptionBlock>(node.Description);
  1660. CoreRow row = Data.Rows.FirstOrDefault(r => r.Get<JobDocumentSet, Guid>(c => c.ID) == desc.ID);
  1661. if (row != null)
  1662. {
  1663. iCol = 1;
  1664. SetContent(sheet, iRow, iCol++, desc.Code, false, false, System.Drawing.Color.Transparent);
  1665. SetContent(sheet, iRow, iCol++, desc.Description, false, true, System.Drawing.Color.Transparent);
  1666. SetContent(sheet, iRow, iCol++, row.Get<JobDocumentSet, String>(c => c.Discipline.Description), true, false, System.Drawing.Color.Transparent);
  1667. SetContent(sheet, iRow, iCol++, row.Get<JobDocumentSet, String>(c => c.Type.Description), true, false, System.Drawing.Color.Transparent);
  1668. SetContent(sheet, iRow, iCol++, row.Get<JobDocumentSet, String>(c => c.Category.Description), true, false, System.Drawing.Color.Transparent);
  1669. SetContent(sheet, iRow, iCol++, row.Get<JobDocumentSet, String>(c => c.Area.Description), true, false, System.Drawing.Color.Transparent);
  1670. foreach (var key in node.Blocks.Keys)
  1671. {
  1672. if (!String.IsNullOrWhiteSpace(node.Blocks[key]))
  1673. {
  1674. var block = Serialization.Deserialize<JobDocumentSetMileStoneBlock>(node.Blocks[key]);
  1675. iCol = blkmap[$"Blocks[{key}]"];
  1676. var color = JobDocumentSetMileStoneConverter.StatusColors[block.Status];
  1677. var status = String.IsNullOrWhiteSpace(block.Revision) ? "--" : block.Revision;
  1678. SetContent(sheet, iRow, iCol, status, true, false, color);
  1679. }
  1680. }
  1681. }
  1682. iRow++;
  1683. }
  1684. sheet.UsedRange.BorderAround();
  1685. sheet.UsedRange.BorderInside();
  1686. sheet.UsedRange.AutofitRows();
  1687. foreach (var row in sheet.UsedRange.Rows)
  1688. {
  1689. row.RowHeight += 5;
  1690. row.VerticalAlignment = ExcelVAlign.VAlignCenter;
  1691. }
  1692. var dlg = new SaveFileDialog();
  1693. dlg.Filter = "Excel Files (*.xlsx)|*.xlsx";
  1694. dlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  1695. dlg.FileName = string.Format("Document Register {0:yyyy-MM-dd hh-mm-ss}.xlsx", DateTime.Now);
  1696. if (dlg.ShowDialog() == DialogResult.OK)
  1697. {
  1698. try
  1699. {
  1700. workbook.SaveAs(dlg.FileName, ExcelSaveType.SaveAsXLS);
  1701. Process.Start(new ProcessStartInfo(dlg.FileName) { UseShellExecute = true });
  1702. }
  1703. catch (Exception e2)
  1704. {
  1705. MessageBox.Show("Error saving spreadsheet!\n\n" + e2.Message);
  1706. }
  1707. }
  1708. }
  1709. private void SetContent(IWorksheet sheet, int row, int col, string text, bool center, bool wrap, Color? color)
  1710. {
  1711. var range = sheet.Range[row, col];
  1712. range.Text = text;
  1713. range.WrapText = wrap;
  1714. if (center)
  1715. range.CellStyle.HorizontalAlignment = ExcelHAlign.HAlignCenter;
  1716. if (color != null)
  1717. range.CellStyle.Color = color.Value;
  1718. }
  1719. private void SetHeader(IWorksheet sheet, int row, int col, string text, double width, bool center, bool rotate)
  1720. {
  1721. var range = sheet.Range[row, col];
  1722. range.Text = text;
  1723. range.CellStyle.Color = System.Drawing.Color.Silver;
  1724. sheet.SetColumnWidth(col,width);
  1725. if (center)
  1726. range.CellStyle.HorizontalAlignment = ExcelHAlign.HAlignCenter;
  1727. if (rotate)
  1728. range.CellStyle.Rotation = 90;
  1729. }
  1730. }
  1731. }