JobDocumentSetTree.xaml.cs 82 KB

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