Bug Fix #49: Update group when attachment removed
This commit is contained in:
+70
-25
@@ -18,7 +18,8 @@ namespace Disco.BI.DocumentTemplateBI.ManagedGroups
|
|||||||
private const string CategoryDescriptionFormat = "Related Devices Linked Group";
|
private const string CategoryDescriptionFormat = "Related Devices Linked Group";
|
||||||
private const string GroupDescriptionFormat = "{0} [Document Template Devices]";
|
private const string GroupDescriptionFormat = "{0} [Document Template Devices]";
|
||||||
|
|
||||||
private IDisposable repositorySubscription;
|
private IDisposable repositoryAddSubscription;
|
||||||
|
private IDisposable repositoryRemoveSubscription;
|
||||||
private IDisposable deviceRenameRepositorySubscription;
|
private IDisposable deviceRenameRepositorySubscription;
|
||||||
private IDisposable jobCloseRepositorySubscription;
|
private IDisposable jobCloseRepositorySubscription;
|
||||||
private IDisposable deviceAssignmentRepositorySubscription;
|
private IDisposable deviceAssignmentRepositorySubscription;
|
||||||
@@ -46,24 +47,33 @@ namespace Disco.BI.DocumentTemplateBI.ManagedGroups
|
|||||||
{
|
{
|
||||||
case DocumentTemplate.DocumentTemplateScopes.Device:
|
case DocumentTemplate.DocumentTemplateScopes.Device:
|
||||||
// Observe Device Attachments
|
// Observe Device Attachments
|
||||||
repositorySubscription = DocumentTemplateManagedGroups.DeviceScopeRepositoryEvents.Value
|
repositoryAddSubscription = DocumentTemplateManagedGroups.DeviceAttachmentAddRepositoryEvents.Value
|
||||||
.Where(e => ((DeviceAttachment)e.Entity).DocumentTemplateId == DocumentTemplateId)
|
.Where(e => ((DeviceAttachment)e.Entity).DocumentTemplateId == DocumentTemplateId)
|
||||||
.Subscribe(ProcessDeviceRepositoryEvent);
|
.Subscribe(ProcessDeviceAttachmentAddEvent);
|
||||||
|
repositoryRemoveSubscription = DocumentTemplateManagedGroups.DeviceAttachmentRemoveEvents.Value
|
||||||
|
.Where(e => e.Item3 == DocumentTemplateId)
|
||||||
|
.Subscribe(ProcessDeviceAttachmentRemoveEvent);
|
||||||
break;
|
break;
|
||||||
case DocumentTemplate.DocumentTemplateScopes.Job:
|
case DocumentTemplate.DocumentTemplateScopes.Job:
|
||||||
// Observe Job Attachments
|
// Observe Job Attachments
|
||||||
repositorySubscription = DocumentTemplateManagedGroups.UserScopeRepositoryEvents.Value
|
repositoryAddSubscription = DocumentTemplateManagedGroups.JobAttachmentAddRepositoryEvents.Value
|
||||||
.Where(e => ((JobAttachment)e.Entity).DocumentTemplateId == DocumentTemplateId)
|
.Where(e => ((JobAttachment)e.Entity).DocumentTemplateId == DocumentTemplateId)
|
||||||
.Subscribe(ProcessJobRepositoryEvent);
|
.Subscribe(ProcessJobAttachmentAddEvent);
|
||||||
|
repositoryRemoveSubscription = DocumentTemplateManagedGroups.JobAttachmentRemoveEvents.Value
|
||||||
|
.Where(e => e.Item3 == DocumentTemplateId)
|
||||||
|
.Subscribe(ProcessJobAttachmentRemoveEvent);
|
||||||
// Observe Job Close/Reopen
|
// Observe Job Close/Reopen
|
||||||
jobCloseRepositorySubscription = DocumentTemplateManagedGroups.JobCloseRepositoryEvents.Value
|
jobCloseRepositorySubscription = DocumentTemplateManagedGroups.JobCloseRepositoryEvents.Value
|
||||||
.Subscribe(ProcessJobCloseRepositoryEvent);
|
.Subscribe(ProcessJobCloseRepositoryEvent);
|
||||||
break;
|
break;
|
||||||
case DocumentTemplate.DocumentTemplateScopes.User:
|
case DocumentTemplate.DocumentTemplateScopes.User:
|
||||||
// Observe User Attachments
|
// Observe User Attachments
|
||||||
repositorySubscription = DocumentTemplateManagedGroups.UserScopeRepositoryEvents.Value
|
repositoryAddSubscription = DocumentTemplateManagedGroups.UserAttachmentAddRepositoryEvents.Value
|
||||||
.Where(e => ((UserAttachment)e.Entity).DocumentTemplateId == DocumentTemplateId)
|
.Where(e => ((UserAttachment)e.Entity).DocumentTemplateId == DocumentTemplateId)
|
||||||
.Subscribe(ProcessUserRepositoryEvent);
|
.Subscribe(ProcessUserAttachmentAddEvent);
|
||||||
|
repositoryRemoveSubscription = DocumentTemplateManagedGroups.UserAttachmentRemoveEvents.Value
|
||||||
|
.Where(e => e.Item3 == DocumentTemplateId)
|
||||||
|
.Subscribe(ProcessUserAttachmentRemoveEvent);
|
||||||
// Observe Device Assignments
|
// Observe Device Assignments
|
||||||
deviceAssignmentRepositorySubscription = DocumentTemplateManagedGroups.DeviceAssignmentRepositoryEvents.Value
|
deviceAssignmentRepositorySubscription = DocumentTemplateManagedGroups.DeviceAssignmentRepositoryEvents.Value
|
||||||
.Subscribe(ProcessDeviceAssignmentRepositoryEvent);
|
.Subscribe(ProcessDeviceAssignmentRepositoryEvent);
|
||||||
@@ -205,15 +215,26 @@ namespace Disco.BI.DocumentTemplateBI.ManagedGroups
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ProcessDeviceRepositoryEvent(RepositoryMonitorEvent e)
|
private void ProcessDeviceAttachmentAddEvent(RepositoryMonitorEvent e)
|
||||||
{
|
{
|
||||||
var attachment = (DeviceAttachment)e.Entity;
|
var attachment = (DeviceAttachment)e.Entity;
|
||||||
|
|
||||||
string deviceAccountId;
|
string deviceAccountId;
|
||||||
if (DeviceContainsAttachment(e.Database, attachment.DeviceSerialNumber, out deviceAccountId))
|
if (DeviceContainsAttachment(e.Database, attachment.DeviceSerialNumber, out deviceAccountId))
|
||||||
AddMember(attachment.DeviceSerialNumber, (database) => new string[] { deviceAccountId });
|
AddMember(attachment.DeviceSerialNumber, (database) => new string[] { deviceAccountId });
|
||||||
else if (deviceAccountId != null)
|
}
|
||||||
RemoveMember(attachment.DeviceSerialNumber, (database) => new string[] { deviceAccountId });
|
private void ProcessDeviceAttachmentRemoveEvent(Tuple<DiscoDataContext, int, string, string> e)
|
||||||
|
{
|
||||||
|
var deviceSerialNumber = e.Item3;
|
||||||
|
|
||||||
|
RemoveMember(deviceSerialNumber, (database) =>
|
||||||
|
{
|
||||||
|
string deviceAccountId;
|
||||||
|
if (!DeviceContainsAttachment(database, deviceSerialNumber, out deviceAccountId) && deviceAccountId != null)
|
||||||
|
return new string[] { deviceAccountId };
|
||||||
|
else
|
||||||
|
return null;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@@ -251,7 +272,7 @@ namespace Disco.BI.DocumentTemplateBI.ManagedGroups
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ProcessJobRepositoryEvent(RepositoryMonitorEvent e)
|
private void ProcessJobAttachmentAddEvent(RepositoryMonitorEvent e)
|
||||||
{
|
{
|
||||||
var attachment = (JobAttachment)e.Entity;
|
var attachment = (JobAttachment)e.Entity;
|
||||||
|
|
||||||
@@ -259,8 +280,24 @@ namespace Disco.BI.DocumentTemplateBI.ManagedGroups
|
|||||||
string deviceSerialNumber;
|
string deviceSerialNumber;
|
||||||
if (JobsContainAttachment(e.Database, attachment.JobId, out deviceAccountId, out deviceSerialNumber))
|
if (JobsContainAttachment(e.Database, attachment.JobId, out deviceAccountId, out deviceSerialNumber))
|
||||||
AddMember(deviceSerialNumber, (database) => new string[] { deviceAccountId });
|
AddMember(deviceSerialNumber, (database) => new string[] { deviceAccountId });
|
||||||
else if (deviceSerialNumber != null && deviceAccountId != null)
|
}
|
||||||
RemoveMember(deviceSerialNumber, (database) => new string[] { deviceAccountId });
|
private void ProcessJobAttachmentRemoveEvent(Tuple<DiscoDataContext, int, string, int> e)
|
||||||
|
{
|
||||||
|
var jobId = e.Item4;
|
||||||
|
string deviceSerialNumber = e.Item1.Jobs.Where(j => j.Id == jobId && j.DeviceSerialNumber != null).Select(j => j.DeviceSerialNumber).FirstOrDefault();
|
||||||
|
|
||||||
|
if (deviceSerialNumber != null)
|
||||||
|
{
|
||||||
|
RemoveMember(deviceSerialNumber, (database) =>
|
||||||
|
{
|
||||||
|
string deviceAccountId;
|
||||||
|
if (!JobsContainAttachment(database, jobId, out deviceAccountId, out deviceSerialNumber) &&
|
||||||
|
deviceSerialNumber != null && deviceAccountId != null)
|
||||||
|
return new string[] { deviceAccountId };
|
||||||
|
else
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@@ -291,21 +328,26 @@ namespace Disco.BI.DocumentTemplateBI.ManagedGroups
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ProcessUserRepositoryEvent(RepositoryMonitorEvent e)
|
private void ProcessUserAttachmentAddEvent(RepositoryMonitorEvent e)
|
||||||
{
|
{
|
||||||
var attachment = (UserAttachment)e.Entity;
|
var attachment = (UserAttachment)e.Entity;
|
||||||
|
|
||||||
List<Tuple<string, string>> devices;
|
List<Tuple<string, string>> devices;
|
||||||
if (DeviceUserContainAttachment(e.Database, attachment.UserId, out devices))
|
if (DeviceUserContainAttachment(e.Database, attachment.UserId, out devices) && devices != null)
|
||||||
|
devices.ForEach(d => AddMember(d.Item2, (database) => new string[] { d.Item1 }));
|
||||||
|
}
|
||||||
|
private void ProcessUserAttachmentRemoveEvent(Tuple<DiscoDataContext, int, string, string> e)
|
||||||
|
{
|
||||||
|
var userId = e.Item4;
|
||||||
|
|
||||||
|
RemoveMember(userId, (database) =>
|
||||||
{
|
{
|
||||||
if (devices != null)
|
List<Tuple<string, string>> devices;
|
||||||
devices.ForEach(d => AddMember(d.Item2, (database) => new string[] { d.Item1 }));
|
if (!DeviceUserContainAttachment(database, userId, out devices) && devices != null)
|
||||||
}
|
return devices.Select(d => d.Item1);
|
||||||
else
|
else
|
||||||
{
|
return null;
|
||||||
if (devices != null)
|
});
|
||||||
devices.ForEach(d => RemoveMember(d.Item2, (database) => new string[] { d.Item1 }));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@@ -425,8 +467,11 @@ namespace Disco.BI.DocumentTemplateBI.ManagedGroups
|
|||||||
|
|
||||||
public override void Dispose()
|
public override void Dispose()
|
||||||
{
|
{
|
||||||
if (repositorySubscription != null)
|
if (repositoryAddSubscription != null)
|
||||||
repositorySubscription.Dispose();
|
repositoryAddSubscription.Dispose();
|
||||||
|
|
||||||
|
if (repositoryRemoveSubscription != null)
|
||||||
|
repositoryRemoveSubscription.Dispose();
|
||||||
|
|
||||||
if (deviceRenameRepositorySubscription != null)
|
if (deviceRenameRepositorySubscription != null)
|
||||||
deviceRenameRepositorySubscription.Dispose();
|
deviceRenameRepositorySubscription.Dispose();
|
||||||
|
|||||||
@@ -4,14 +4,19 @@ using Disco.Models.Repository;
|
|||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reactive.Linq;
|
using System.Reactive.Linq;
|
||||||
|
using System.Reactive.Subjects;
|
||||||
|
|
||||||
namespace Disco.BI.DocumentTemplateBI.ManagedGroups
|
namespace Disco.BI.DocumentTemplateBI.ManagedGroups
|
||||||
{
|
{
|
||||||
public static class DocumentTemplateManagedGroups
|
public static class DocumentTemplateManagedGroups
|
||||||
{
|
{
|
||||||
internal static Lazy<IObservable<RepositoryMonitorEvent>> DeviceScopeRepositoryEvents;
|
internal static Lazy<IObservable<RepositoryMonitorEvent>> DeviceAttachmentAddRepositoryEvents;
|
||||||
internal static Lazy<IObservable<RepositoryMonitorEvent>> JobScopeRepositoryEvents;
|
internal static Lazy<IObservable<RepositoryMonitorEvent>> JobAttachmentAddRepositoryEvents;
|
||||||
internal static Lazy<IObservable<RepositoryMonitorEvent>> UserScopeRepositoryEvents;
|
internal static Lazy<IObservable<RepositoryMonitorEvent>> UserAttachmentAddRepositoryEvents;
|
||||||
|
|
||||||
|
internal static Lazy<Subject<Tuple<DiscoDataContext, int, string, string>>> DeviceAttachmentRemoveEvents;
|
||||||
|
internal static Lazy<Subject<Tuple<DiscoDataContext, int, string, int>>> JobAttachmentRemoveEvents;
|
||||||
|
internal static Lazy<Subject<Tuple<DiscoDataContext, int, string, string>>> UserAttachmentRemoveEvents;
|
||||||
|
|
||||||
internal static Lazy<IObservable<RepositoryMonitorEvent>> DeviceRenameRepositoryEvents;
|
internal static Lazy<IObservable<RepositoryMonitorEvent>> DeviceRenameRepositoryEvents;
|
||||||
internal static Lazy<IObservable<RepositoryMonitorEvent>> JobCloseRepositoryEvents;
|
internal static Lazy<IObservable<RepositoryMonitorEvent>> JobCloseRepositoryEvents;
|
||||||
@@ -19,32 +24,35 @@ namespace Disco.BI.DocumentTemplateBI.ManagedGroups
|
|||||||
|
|
||||||
static DocumentTemplateManagedGroups()
|
static DocumentTemplateManagedGroups()
|
||||||
{
|
{
|
||||||
DeviceScopeRepositoryEvents =
|
DeviceAttachmentAddRepositoryEvents =
|
||||||
new Lazy<IObservable<RepositoryMonitorEvent>>(() =>
|
new Lazy<IObservable<RepositoryMonitorEvent>>(() =>
|
||||||
RepositoryMonitor.StreamAfterCommit.Where(e =>
|
RepositoryMonitor.StreamAfterCommit.Where(e =>
|
||||||
e.EntityType == typeof(DeviceAttachment) &&
|
e.EntityType == typeof(DeviceAttachment) &&
|
||||||
((DeviceAttachment)e.Entity).DocumentTemplateId != null && (
|
((DeviceAttachment)e.Entity).DocumentTemplateId != null &&
|
||||||
(e.EventType == RepositoryMonitorEventType.Added) ||
|
e.EventType == RepositoryMonitorEventType.Added
|
||||||
(e.EventType == RepositoryMonitorEventType.Deleted)
|
|
||||||
)
|
|
||||||
));
|
));
|
||||||
JobScopeRepositoryEvents =
|
JobAttachmentAddRepositoryEvents =
|
||||||
new Lazy<IObservable<RepositoryMonitorEvent>>(() =>
|
new Lazy<IObservable<RepositoryMonitorEvent>>(() =>
|
||||||
RepositoryMonitor.StreamAfterCommit.Where(e =>
|
RepositoryMonitor.StreamAfterCommit.Where(e =>
|
||||||
e.EntityType == typeof(JobAttachment) && (
|
e.EntityType == typeof(JobAttachment) &&
|
||||||
(e.EventType == RepositoryMonitorEventType.Added) ||
|
((JobAttachment)e.Entity).DocumentTemplateId != null &&
|
||||||
(e.EventType == RepositoryMonitorEventType.Deleted)
|
e.EventType == RepositoryMonitorEventType.Added
|
||||||
)
|
|
||||||
));
|
));
|
||||||
UserScopeRepositoryEvents =
|
UserAttachmentAddRepositoryEvents =
|
||||||
new Lazy<IObservable<RepositoryMonitorEvent>>(() =>
|
new Lazy<IObservable<RepositoryMonitorEvent>>(() =>
|
||||||
RepositoryMonitor.StreamAfterCommit.Where(e =>
|
RepositoryMonitor.StreamAfterCommit.Where(e =>
|
||||||
e.EntityType == typeof(UserAttachment) && (
|
e.EntityType == typeof(UserAttachment) &&
|
||||||
(e.EventType == RepositoryMonitorEventType.Added) ||
|
((UserAttachment)e.Entity).DocumentTemplateId != null &&
|
||||||
(e.EventType == RepositoryMonitorEventType.Deleted)
|
e.EventType == RepositoryMonitorEventType.Added
|
||||||
)
|
|
||||||
));
|
));
|
||||||
|
|
||||||
|
DeviceAttachmentRemoveEvents =
|
||||||
|
new Lazy<Subject<Tuple<DiscoDataContext, int, string, string>>>(() => new Subject<Tuple<DiscoDataContext, int, string, string>>());
|
||||||
|
JobAttachmentRemoveEvents =
|
||||||
|
new Lazy<Subject<Tuple<DiscoDataContext, int, string, int>>>(() => new Subject<Tuple<DiscoDataContext, int, string, int>>());
|
||||||
|
UserAttachmentRemoveEvents =
|
||||||
|
new Lazy<Subject<Tuple<DiscoDataContext, int, string, string>>>(() => new Subject<Tuple<DiscoDataContext, int, string, string>>());
|
||||||
|
|
||||||
DeviceRenameRepositoryEvents =
|
DeviceRenameRepositoryEvents =
|
||||||
new Lazy<IObservable<RepositoryMonitorEvent>>(() =>
|
new Lazy<IObservable<RepositoryMonitorEvent>>(() =>
|
||||||
RepositoryMonitor.StreamBeforeCommit.Where(e =>
|
RepositoryMonitor.StreamBeforeCommit.Where(e =>
|
||||||
@@ -80,5 +88,23 @@ namespace Disco.BI.DocumentTemplateBI.ManagedGroups
|
|||||||
DocumentTemplateUsersManagedGroup.Initialize(dp);
|
DocumentTemplateUsersManagedGroup.Initialize(dp);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void TriggerDeviceAttachmentDeleted(DiscoDataContext Database, int AttachmentId, string DocumentTemplateId, string DeviceSerialNumber)
|
||||||
|
{
|
||||||
|
if (DocumentTemplateId != null)
|
||||||
|
DeviceAttachmentRemoveEvents.Value.OnNext(Tuple.Create(Database, AttachmentId, DocumentTemplateId, DeviceSerialNumber));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void TriggerJobAttachmentDeleted(DiscoDataContext Database, int AttachmentId, string DocumentTemplateId, int JobId)
|
||||||
|
{
|
||||||
|
if (DocumentTemplateId != null)
|
||||||
|
JobAttachmentRemoveEvents.Value.OnNext(Tuple.Create(Database, AttachmentId, DocumentTemplateId, JobId));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void TriggerUserAttachmentDeleted(DiscoDataContext Database, int AttachmentId, string DocumentTemplateId, string UserId)
|
||||||
|
{
|
||||||
|
if (DocumentTemplateId != null)
|
||||||
|
UserAttachmentRemoveEvents.Value.OnNext(Tuple.Create(Database, AttachmentId, DocumentTemplateId, UserId));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,8 @@ namespace Disco.BI.DocumentTemplateBI.ManagedGroups
|
|||||||
private const string CategoryDescriptionFormat = "Related Users Linked Group";
|
private const string CategoryDescriptionFormat = "Related Users Linked Group";
|
||||||
private const string GroupDescriptionFormat = "{0} [Document Template Users]";
|
private const string GroupDescriptionFormat = "{0} [Document Template Users]";
|
||||||
|
|
||||||
private IDisposable repositorySubscription;
|
private IDisposable repositoryAddSubscription;
|
||||||
|
private IDisposable repositoryRemoveSubscription;
|
||||||
private IDisposable jobCloseRepositorySubscription;
|
private IDisposable jobCloseRepositorySubscription;
|
||||||
private IDisposable deviceAssignmentRepositorySubscription;
|
private IDisposable deviceAssignmentRepositorySubscription;
|
||||||
private string DocumentTemplateId;
|
private string DocumentTemplateId;
|
||||||
@@ -45,27 +46,36 @@ namespace Disco.BI.DocumentTemplateBI.ManagedGroups
|
|||||||
{
|
{
|
||||||
case DocumentTemplate.DocumentTemplateScopes.Device:
|
case DocumentTemplate.DocumentTemplateScopes.Device:
|
||||||
// Observe Device Attachments
|
// Observe Device Attachments
|
||||||
repositorySubscription = DocumentTemplateManagedGroups.DeviceScopeRepositoryEvents.Value
|
repositoryAddSubscription = DocumentTemplateManagedGroups.DeviceAttachmentAddRepositoryEvents.Value
|
||||||
.Where(e => ((DeviceAttachment)e.Entity).DocumentTemplateId == DocumentTemplateId)
|
.Where(e => ((DeviceAttachment)e.Entity).DocumentTemplateId == DocumentTemplateId)
|
||||||
.Subscribe(ProcessDeviceRepositoryEvent);
|
.Subscribe(ProcessDeviceAttachmentAddEvent);
|
||||||
|
repositoryRemoveSubscription = DocumentTemplateManagedGroups.DeviceAttachmentRemoveEvents.Value
|
||||||
|
.Where(e => e.Item3 == DocumentTemplateId)
|
||||||
|
.Subscribe(ProcessDeviceAttachmentRemoveEvent);
|
||||||
// Observe Device Assignments
|
// Observe Device Assignments
|
||||||
deviceAssignmentRepositorySubscription = DocumentTemplateManagedGroups.DeviceAssignmentRepositoryEvents.Value
|
deviceAssignmentRepositorySubscription = DocumentTemplateManagedGroups.DeviceAssignmentRepositoryEvents.Value
|
||||||
.Subscribe(ProcessDeviceAssignmentRepositoryEvent);
|
.Subscribe(ProcessDeviceAssignmentRepositoryEvent);
|
||||||
break;
|
break;
|
||||||
case DocumentTemplate.DocumentTemplateScopes.Job:
|
case DocumentTemplate.DocumentTemplateScopes.Job:
|
||||||
// Observe Job Attachments
|
// Observe Job Attachments
|
||||||
repositorySubscription = DocumentTemplateManagedGroups.UserScopeRepositoryEvents.Value
|
repositoryAddSubscription = DocumentTemplateManagedGroups.UserAttachmentAddRepositoryEvents.Value
|
||||||
.Where(e => ((JobAttachment)e.Entity).DocumentTemplateId == DocumentTemplateId)
|
.Where(e => ((JobAttachment)e.Entity).DocumentTemplateId == DocumentTemplateId)
|
||||||
.Subscribe(ProcessJobRepositoryEvent);
|
.Subscribe(ProcessJobAttachmentAddEvent);
|
||||||
|
repositoryRemoveSubscription = DocumentTemplateManagedGroups.JobAttachmentRemoveEvents.Value
|
||||||
|
.Where(e => e.Item3 == DocumentTemplateId)
|
||||||
|
.Subscribe(ProcessJobAttachmentRemoveEvent);
|
||||||
// Observe Job Close/Reopen
|
// Observe Job Close/Reopen
|
||||||
jobCloseRepositorySubscription = DocumentTemplateManagedGroups.JobCloseRepositoryEvents.Value
|
jobCloseRepositorySubscription = DocumentTemplateManagedGroups.JobCloseRepositoryEvents.Value
|
||||||
.Subscribe(ProcessJobCloseRepositoryEvent);
|
.Subscribe(ProcessJobCloseRepositoryEvent);
|
||||||
break;
|
break;
|
||||||
case DocumentTemplate.DocumentTemplateScopes.User:
|
case DocumentTemplate.DocumentTemplateScopes.User:
|
||||||
// Observe User Attachments
|
// Observe User Attachments
|
||||||
repositorySubscription = DocumentTemplateManagedGroups.UserScopeRepositoryEvents.Value
|
repositoryAddSubscription = DocumentTemplateManagedGroups.UserAttachmentAddRepositoryEvents.Value
|
||||||
.Where(e => ((UserAttachment)e.Entity).DocumentTemplateId == DocumentTemplateId)
|
.Where(e => ((UserAttachment)e.Entity).DocumentTemplateId == DocumentTemplateId)
|
||||||
.Subscribe(ProcessUserRepositoryEvent);
|
.Subscribe(ProcessUserAttachmentAddEvent);
|
||||||
|
repositoryRemoveSubscription = DocumentTemplateManagedGroups.UserAttachmentRemoveEvents.Value
|
||||||
|
.Where(e => e.Item3 == DocumentTemplateId)
|
||||||
|
.Subscribe(ProcessUserAttachmentRemoveEvent);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -82,7 +92,7 @@ namespace Disco.BI.DocumentTemplateBI.ManagedGroups
|
|||||||
case DocumentTemplate.DocumentTemplateScopes.Job:
|
case DocumentTemplate.DocumentTemplateScopes.Job:
|
||||||
return string.Format(DescriptionFormat, DocumentTemplateScope, DocumentTemplateDescription);
|
return string.Format(DescriptionFormat, DocumentTemplateScope, DocumentTemplateDescription);
|
||||||
case DocumentTemplate.DocumentTemplateScopes.User:
|
case DocumentTemplate.DocumentTemplateScopes.User:
|
||||||
return string.Format(UserDescriptionFormat, DocumentTemplateDescription);
|
return string.Format(UserDescriptionFormat, DocumentTemplateDescription);
|
||||||
default:
|
default:
|
||||||
throw new ArgumentException("Unknown Document Template Scope", "Scope");
|
throw new ArgumentException("Unknown Document Template Scope", "Scope");
|
||||||
}
|
}
|
||||||
@@ -183,15 +193,29 @@ namespace Disco.BI.DocumentTemplateBI.ManagedGroups
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ProcessDeviceRepositoryEvent(RepositoryMonitorEvent e)
|
private void ProcessDeviceAttachmentAddEvent(RepositoryMonitorEvent e)
|
||||||
{
|
{
|
||||||
var attachment = (DeviceAttachment)e.Entity;
|
var attachment = (DeviceAttachment)e.Entity;
|
||||||
|
|
||||||
string userId;
|
string userId;
|
||||||
if (DeviceContainsAttachment(e.Database, attachment.DeviceSerialNumber, out userId))
|
if (DeviceContainsAttachment(e.Database, attachment.DeviceSerialNumber, out userId) && userId != null)
|
||||||
AddMember(userId, (database) => new string[] { userId });
|
AddMember(userId, (database) => new string[] { userId });
|
||||||
else if (userId != null)
|
}
|
||||||
RemoveMember(userId, (database) => new string[] { userId });
|
private void ProcessDeviceAttachmentRemoveEvent(Tuple<DiscoDataContext, int, string, string> e)
|
||||||
|
{
|
||||||
|
var deviceSerialNumber = e.Item4;
|
||||||
|
string userId = e.Item1.Devices.Where(d => d.SerialNumber == deviceSerialNumber && d.AssignedUserId != null).Select(j => j.AssignedUserId).FirstOrDefault();
|
||||||
|
|
||||||
|
if (userId != null)
|
||||||
|
{
|
||||||
|
RemoveMember(userId, (database) =>
|
||||||
|
{
|
||||||
|
if (DeviceContainsAttachment(database, deviceSerialNumber, out userId) && userId != null)
|
||||||
|
return new string[] { userId };
|
||||||
|
else
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@@ -217,15 +241,29 @@ namespace Disco.BI.DocumentTemplateBI.ManagedGroups
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ProcessJobRepositoryEvent(RepositoryMonitorEvent e)
|
private void ProcessJobAttachmentAddEvent(RepositoryMonitorEvent e)
|
||||||
{
|
{
|
||||||
var attachment = (JobAttachment)e.Entity;
|
var attachment = (JobAttachment)e.Entity;
|
||||||
|
|
||||||
string userId;
|
string userId;
|
||||||
if (JobsContainAttachment(e.Database, attachment.JobId, out userId))
|
if (JobsContainAttachment(e.Database, attachment.JobId, out userId) && userId != null)
|
||||||
AddMember(userId, (database) => new string[] { userId });
|
AddMember(userId, (database) => new string[] { userId });
|
||||||
else if (userId != null)
|
}
|
||||||
RemoveMember(userId, (database) => new string[] { userId });
|
private void ProcessJobAttachmentRemoveEvent(Tuple<DiscoDataContext, int, string, int> e)
|
||||||
|
{
|
||||||
|
var jobId = e.Item4;
|
||||||
|
string userId = e.Item1.Jobs.Where(j => j.Id == jobId && j.UserId != null).Select(j => j.UserId).FirstOrDefault();
|
||||||
|
|
||||||
|
if (userId != null)
|
||||||
|
{
|
||||||
|
RemoveMember(userId, (database) =>
|
||||||
|
{
|
||||||
|
if (JobsContainAttachment(database, jobId, out userId) && userId != null)
|
||||||
|
return new string[] { userId };
|
||||||
|
else
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@@ -239,15 +277,25 @@ namespace Disco.BI.DocumentTemplateBI.ManagedGroups
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ProcessUserRepositoryEvent(RepositoryMonitorEvent e)
|
private void ProcessUserAttachmentAddEvent(RepositoryMonitorEvent e)
|
||||||
{
|
{
|
||||||
var attachment = (UserAttachment)e.Entity;
|
var attachment = (UserAttachment)e.Entity;
|
||||||
var userId = attachment.UserId;
|
var userId = attachment.UserId;
|
||||||
|
|
||||||
if (UserContainAttachment(e.Database, userId))
|
if (UserContainAttachment(e.Database, userId) && userId != null)
|
||||||
AddMember(userId, (database) => new string[] { userId });
|
AddMember(userId, (database) => new string[] { userId });
|
||||||
else
|
}
|
||||||
RemoveMember(userId, (database) => new string[] { userId });
|
private void ProcessUserAttachmentRemoveEvent(Tuple<DiscoDataContext, int, string, string> e)
|
||||||
|
{
|
||||||
|
var userId = e.Item4;
|
||||||
|
|
||||||
|
RemoveMember(userId, (database) =>
|
||||||
|
{
|
||||||
|
if (!UserContainAttachment(database, userId))
|
||||||
|
return new string[] { userId };
|
||||||
|
else
|
||||||
|
return null;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@@ -301,8 +349,11 @@ namespace Disco.BI.DocumentTemplateBI.ManagedGroups
|
|||||||
|
|
||||||
public override void Dispose()
|
public override void Dispose()
|
||||||
{
|
{
|
||||||
if (repositorySubscription != null)
|
if (repositoryAddSubscription != null)
|
||||||
repositorySubscription.Dispose();
|
repositoryAddSubscription.Dispose();
|
||||||
|
|
||||||
|
if (repositoryRemoveSubscription != null)
|
||||||
|
repositoryRemoveSubscription.Dispose();
|
||||||
|
|
||||||
if (jobCloseRepositorySubscription != null)
|
if (jobCloseRepositorySubscription != null)
|
||||||
jobCloseRepositorySubscription.Dispose();
|
jobCloseRepositorySubscription.Dispose();
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ using Disco.Models.Repository;
|
|||||||
using Disco.Data.Repository;
|
using Disco.Data.Repository;
|
||||||
using Disco.Services.Users;
|
using Disco.Services.Users;
|
||||||
using Disco.Services.Authorization;
|
using Disco.Services.Authorization;
|
||||||
|
using Disco.BI.DocumentTemplateBI.ManagedGroups;
|
||||||
|
|
||||||
namespace Disco.BI.Extensions
|
namespace Disco.BI.Extensions
|
||||||
{
|
{
|
||||||
@@ -29,8 +30,14 @@ namespace Disco.BI.Extensions
|
|||||||
if (!da.CanDelete())
|
if (!da.CanDelete())
|
||||||
throw new InvalidOperationException("Deletion of Attachment is Denied");
|
throw new InvalidOperationException("Deletion of Attachment is Denied");
|
||||||
|
|
||||||
|
var attachmentId = da.Id;
|
||||||
|
var documentTemplateId = da.DocumentTemplateId;
|
||||||
|
var deviceSerialNumber = da.DeviceSerialNumber;
|
||||||
|
|
||||||
da.RepositoryDelete(Database);
|
da.RepositoryDelete(Database);
|
||||||
Database.DeviceAttachments.Remove(da);
|
Database.DeviceAttachments.Remove(da);
|
||||||
|
|
||||||
|
DocumentTemplateManagedGroups.TriggerDeviceAttachmentDeleted(Database, attachmentId, documentTemplateId, deviceSerialNumber);
|
||||||
}
|
}
|
||||||
public static bool CanDelete(this JobAttachment ja)
|
public static bool CanDelete(this JobAttachment ja)
|
||||||
{
|
{
|
||||||
@@ -48,8 +55,14 @@ namespace Disco.BI.Extensions
|
|||||||
if (!ja.CanDelete())
|
if (!ja.CanDelete())
|
||||||
throw new InvalidOperationException("Deletion of Attachment is Denied");
|
throw new InvalidOperationException("Deletion of Attachment is Denied");
|
||||||
|
|
||||||
|
var attachmentId = ja.Id;
|
||||||
|
var documentTemplateId = ja.DocumentTemplateId;
|
||||||
|
var jobId = ja.JobId;
|
||||||
|
|
||||||
ja.RepositoryDelete(Database);
|
ja.RepositoryDelete(Database);
|
||||||
Database.JobAttachments.Remove(ja);
|
Database.JobAttachments.Remove(ja);
|
||||||
|
|
||||||
|
DocumentTemplateManagedGroups.TriggerJobAttachmentDeleted(Database, attachmentId, documentTemplateId, jobId);
|
||||||
}
|
}
|
||||||
public static bool CanDelete(this UserAttachment ua)
|
public static bool CanDelete(this UserAttachment ua)
|
||||||
{
|
{
|
||||||
@@ -67,8 +80,14 @@ namespace Disco.BI.Extensions
|
|||||||
if (!ua.CanDelete())
|
if (!ua.CanDelete())
|
||||||
throw new InvalidOperationException("Deletion of Attachment is Denied");
|
throw new InvalidOperationException("Deletion of Attachment is Denied");
|
||||||
|
|
||||||
|
var attachmentId = ua.Id;
|
||||||
|
var documentTemplateId = ua.DocumentTemplateId;
|
||||||
|
var userId = ua.UserId;
|
||||||
|
|
||||||
ua.RepositoryDelete(Database);
|
ua.RepositoryDelete(Database);
|
||||||
Database.UserAttachments.Remove(ua);
|
Database.UserAttachments.Remove(ua);
|
||||||
|
|
||||||
|
DocumentTemplateManagedGroups.TriggerUserAttachmentDeleted(Database, attachmentId, documentTemplateId, userId);
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user