123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- using System;
- using System.Collections.ObjectModel;
- using System.ComponentModel;
- using System.Linq;
- using System.Windows;
- using System.Windows.Media;
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Core;
- using InABox.WPF;
- using Syncfusion.UI.Xaml.Diagram;
- using Syncfusion.UI.Xaml.Diagram.Layout;
- using Color = System.Drawing.Color;
- namespace PRSDesktop
- {
- public class OrgChartDataModel : DiagramViewModel
- {
- private OrgChartAppearance _appearance = OrgChartAppearance.Vertical;
- private OrgChartLayout _layout = OrgChartLayout.Employee;
- public OrgChartDataModel()
- {
- Active = false;
- Menu = null;
- Tool = Tool.ZoomPan;
- DataSourceSettings = new DataSourceSettings
- {
- ParentId = "ParentID",
- Id = "ID"
- };
- LayoutManager = new LayoutManager { RefreshFrequency = RefreshFrequency.Reset };
- Data = new OrgChartEntries();
- DataSourceSettings.DataSource = new ObservableCollection<OrgChartEntry>();
- }
- public bool Active { get; set; }
- public OrgChartAppearance Appearance
- {
- get => _appearance;
- set
- {
- if (_appearance != value)
- {
- _appearance = value;
- if (Active)
- Refresh();
- }
- }
- }
- public OrgChartLayout Layout
- {
- get => _layout;
- set
- {
- if (_layout != value)
- {
- _layout = value;
- if (Active)
- UpdateLayout();
- }
- }
- }
- public OrgChartEntries Data { get; }
- public void UpdateLayout()
- {
- switch (_appearance)
- {
- case OrgChartAppearance.Vertical:
- LayoutManager.Layout = new DirectedTreeLayout
- {
- Type = LayoutType.Organization,
- HorizontalSpacing = 40,
- VerticalSpacing = 50,
- AvoidSegmentOverlapping = false
- };
- DefaultConnectorType = ConnectorType.Orthogonal;
- break;
- case OrgChartAppearance.Horizontal:
- LayoutManager.Layout = new DirectedTreeLayout
- {
- Type = LayoutType.Hierarchical,
- HorizontalSpacing = 40,
- VerticalSpacing = 50,
- AvoidSegmentOverlapping = false
- };
- DefaultConnectorType = ConnectorType.Orthogonal;
- break;
- case OrgChartAppearance.Radial:
- LayoutManager.Layout = new RadialTreeLayout
- {
- HorizontalSpacing = 40,
- VerticalSpacing = 50,
- Bounds = new Rect(0, 0, 1000, 1000)
- };
- DefaultConnectorType = ConnectorType.Line;
- break;
- }
- }
- private void GetThumbnails(OrgChartEntries entries)
- {
- var thumbids = entries.Select(x => x.ImageID).Where(x => x != Guid.Empty).Distinct().ToArray();
- if (thumbids.Any())
- {
- var thumbs = new Client<Document>().Query(
- new Filter<Document>(x => x.ID).InList(thumbids),
- new Columns<Document>(
- x => x.ID,
- x => x.Data
- )
- );
- foreach (var row in thumbs.Rows)
- {
- var entry = entries.FirstOrDefault(x => x.ImageID == row.Get<Document, Guid>(c => c.ID));
- if (entry != null)
- {
- var data = row.Get<Document, byte[]>(c => c.Data);
- var image = ImageUtils.LoadImage(data);
- entry.Image = new ImageBrush(image);
- }
- }
- }
- }
- public void Refresh()
- {
- var query = new MultiQuery();
- query.Add<EmployeePosition>();
- query.Add<Role>();
- query.Add<EmployeeRole>();
- query.Add(
- LookupFactory.DefineFilter<Employee>(),
- new Columns<Employee>(
- x => x.ID,
- x => x.Name,
- x => x.Position.ID,
- x => x.Position.Description,
- x => x.OrgChart.ReportsTo.ID,
- x => x.OrgChart.Color,
- x => x.OrgChart.Visible,
- x => x.Thumbnail.ID
- )
- );
- query.Query();
- var employees = query.Get<Employee>();
- Data.Clear();
- if (_layout == OrgChartLayout.Employee)
- {
- foreach (var row in employees.Rows)
- {
- var entry = new OrgChartEntry
- {
- ID = row.Get<Employee, Guid>(x => x.ID),
- ParentID = row.Get<Employee, Guid>(x => x.OrgChart.ReportsTo.ID),
- Name = row.Get<Employee, string>(x => x.Name),
- Position = row.Get<Employee, string>(x => x.Position.Description),
- ImageID = row.Get<Employee, Guid>(x => x.Thumbnail.ID),
- Color = row.Get<Employee, string>(x => x.OrgChart.Color),
- Visible = row.Get<Employee, bool>(x => x.OrgChart.Visible)
- };
- Data.Add(entry);
- }
- }
- else if (_layout == OrgChartLayout.Position)
- {
- var positions = query.Get<EmployeePosition>();
- foreach (var row in positions.Rows)
- {
- var entry = new OrgChartEntry
- {
- ID = row.Get<EmployeePosition, Guid>(x => x.ID),
- ParentID = row.Get<EmployeePosition, Guid>(x => x.OrgChart.ReportsTo.ID),
- Position = row.Get<EmployeePosition, string>(x => x.Description),
- ImageID = Guid.Empty,
- Color = row.Get<EmployeePosition, string>(x => x.OrgChart.Color),
- Visible = row.Get<Employee, bool>(x => x.OrgChart.Visible)
- };
- entry.Name = string.Join("\n",
- employees.Rows
- .Where(r => r.Get<Employee, Guid>(c => c.Position.ID) == entry.ID)
- .Select(r => r.Get<Employee, string>(c => c.Name))
- .OrderBy(x => x)
- );
- if (string.IsNullOrWhiteSpace(entry.Name))
- {
- entry.WarningColor = ImageUtils.ColorToString(Color.Red);
- entry.Name = "VACANT";
- }
- Data.Add(entry);
- }
- }
- else if (_layout == OrgChartLayout.Role)
- {
- var roles = query.Get<Role>();
- var emproles = query.Get<EmployeeRole>();
- foreach (var row in roles.Rows)
- {
- var id = row.Get<Role, Guid>(x => x.ID);
- var parentid = row.Get<Role, Guid>(x => x.OrgChart.ReportsTo.ID);
- var entry = new OrgChartEntry
- {
- ID = row.Get<Role, Guid>(x => x.ID),
- ParentID = row.Get<Role, Guid>(x => x.OrgChart.ReportsTo.ID),
- Position = row.Get<Role, string>(x => x.Name),
- ImageID = Guid.Empty,
- Color = row.Get<Role, string>(x => x.OrgChart.Color),
- Visible = row.Get<Employee, bool>(x => x.OrgChart.Visible)
- };
- entry.Name = string.Join("\n",
- emproles.Rows
- .Where(
- r => r.Get<EmployeeRole, Guid>(c => c.RoleLink.ID) == entry.ID
- )
- .Select(r => r.Get<EmployeeRole, string>(c => c.EmployeeLink.Name))
- .OrderBy(x => x)
- );
- if (string.IsNullOrWhiteSpace(entry.Name))
- {
- entry.WarningColor = ImageUtils.ColorToString(Color.Red);
- entry.Name = "VACANT";
- }
- Data.Add(entry);
- }
- }
- GetThumbnails(Data);
- UpdateLayout();
- var source = DataSourceSettings.DataSource as ObservableCollection<OrgChartEntry>;
- source.Clear();
- foreach (var entry in Data.Where(x => x.Visible))
- source.Add(entry);
- //= Data.Where(x=>x.Visible).ToList();
- }
- public event PropertyChangedEventHandler PropertyChanged;
- private void onPropertyChanged(string name)
- {
- if (PropertyChanged != null) PropertyChanged.Invoke(this, new PropertyChangedEventArgs(name));
- }
- }
- }
|