66using SimpleIdServer . Scim . Exceptions ;
77using SimpleIdServer . Scim . Extensions ;
88using SimpleIdServer . Scim . Helpers ;
9- using SimpleIdServer . Scim . Infrastructure . Lock ;
109using SimpleIdServer . Scim . Persistence ;
1110using SimpleIdServer . Scim . Resources ;
1211using System ;
1312using System . Collections . Generic ;
1413using System . Linq ;
15- using System . Threading ;
1614using System . Threading . Tasks ;
1715
1816namespace SimpleIdServer . Scim . Commands . Handlers
@@ -24,23 +22,20 @@ public class ReplaceRepresentationCommandHandler : BaseCommandHandler, IReplaceR
2422 private readonly ISCIMRepresentationHelper _scimRepresentationHelper ;
2523 private readonly ISCIMRepresentationCommandRepository _scimRepresentationCommandRepository ;
2624 private readonly IRepresentationReferenceSync _representationReferenceSync ;
27- private readonly IDistributedLock _distributedLock ;
2825
2926 public ReplaceRepresentationCommandHandler (
3027 ISCIMAttributeMappingQueryRepository scimAttributeMappingQueryRepository ,
3128 ISCIMSchemaQueryRepository scimSchemaQueryRepository ,
3229 ISCIMRepresentationHelper scimRepresentationHelper ,
3330 ISCIMRepresentationCommandRepository scimRepresentationCommandRepository ,
3431 IRepresentationReferenceSync representationReferenceSync ,
35- IDistributedLock distributedLock ,
3632 IBusControl busControl ) : base ( busControl )
3733 {
3834 _scimAttributeMappingQueryRepository = scimAttributeMappingQueryRepository ;
3935 _scimSchemaQueryRepository = scimSchemaQueryRepository ;
4036 _scimRepresentationHelper = scimRepresentationHelper ;
4137 _scimRepresentationCommandRepository = scimRepresentationCommandRepository ;
4238 _representationReferenceSync = representationReferenceSync ;
43- _distributedLock = distributedLock ;
4439 }
4540
4641 public async Task < SCIMRepresentation > Handle ( ReplaceRepresentationCommand replaceRepresentationCommand )
@@ -61,49 +56,40 @@ public async Task<SCIMRepresentation> Handle(ReplaceRepresentationCommand replac
6156 }
6257
6358 var schemas = await _scimSchemaQueryRepository . FindSCIMSchemaByIdentifiers ( requestedSchemas ) ;
64- var lockName = $ "representation-{ replaceRepresentationCommand . Id } ";
65- await _distributedLock . WaitLock ( lockName , CancellationToken . None ) ;
66- try
59+ var existingRepresentation = await _scimRepresentationCommandRepository . Get ( replaceRepresentationCommand . Id ) ;
60+ if ( existingRepresentation == null )
6761 {
68- var existingRepresentation = await _scimRepresentationCommandRepository . Get ( replaceRepresentationCommand . Id ) ;
69- if ( existingRepresentation == null )
70- {
71- throw new SCIMNotFoundException ( string . Format ( Global . ResourceNotFound , replaceRepresentationCommand . Id ) ) ;
72- }
62+ throw new SCIMNotFoundException ( string . Format ( Global . ResourceNotFound , replaceRepresentationCommand . Id ) ) ;
63+ }
7364
74- var oldRepresentation = ( SCIMRepresentation ) existingRepresentation . Clone ( ) ;
75- var mainSchema = schemas . First ( s => s . Id == schema . Id ) ;
76- var extensionSchemas = schemas . Where ( s => s . Id != schema . Id ) . ToList ( ) ;
77- var updatedRepresentation = _scimRepresentationHelper . ExtractSCIMRepresentationFromJSON (
78- replaceRepresentationCommand . Representation . Attributes ,
79- replaceRepresentationCommand . Representation . ExternalId ,
80- mainSchema ,
81- extensionSchemas ) ;
82- var updateResult = await UpdateExistingRepresentation ( replaceRepresentationCommand . ResourceType , existingRepresentation , updatedRepresentation ) ;
83- existingRepresentation . SetDisplayName ( updatedRepresentation . DisplayName ) ;
84- existingRepresentation . SetExternalId ( updatedRepresentation . ExternalId ) ;
85- existingRepresentation . SetUpdated ( DateTime . UtcNow ) ;
86- var isReferenceProperty = await _representationReferenceSync . IsReferenceProperty ( replaceRepresentationCommand . Representation . Attributes . GetKeys ( ) ) ;
87- var references = await _representationReferenceSync . Sync ( updateResult . AttributeMappingLst , replaceRepresentationCommand . ResourceType , oldRepresentation , existingRepresentation , replaceRepresentationCommand . Location , ! isReferenceProperty ) ;
88- using ( var transaction = await _scimRepresentationCommandRepository . StartTransaction ( ) )
65+ var oldRepresentation = ( SCIMRepresentation ) existingRepresentation . Clone ( ) ;
66+ var mainSchema = schemas . First ( s => s . Id == schema . Id ) ;
67+ var extensionSchemas = schemas . Where ( s => s . Id != schema . Id ) . ToList ( ) ;
68+ var updatedRepresentation = _scimRepresentationHelper . ExtractSCIMRepresentationFromJSON (
69+ replaceRepresentationCommand . Representation . Attributes ,
70+ replaceRepresentationCommand . Representation . ExternalId ,
71+ mainSchema ,
72+ extensionSchemas ) ;
73+ var updateResult = await UpdateExistingRepresentation ( replaceRepresentationCommand . ResourceType , existingRepresentation , updatedRepresentation ) ;
74+ existingRepresentation . SetDisplayName ( updatedRepresentation . DisplayName ) ;
75+ existingRepresentation . SetExternalId ( updatedRepresentation . ExternalId ) ;
76+ existingRepresentation . SetUpdated ( DateTime . UtcNow ) ;
77+ var isReferenceProperty = await _representationReferenceSync . IsReferenceProperty ( replaceRepresentationCommand . Representation . Attributes . GetKeys ( ) ) ;
78+ var references = await _representationReferenceSync . Sync ( updateResult . AttributeMappingLst , replaceRepresentationCommand . ResourceType , oldRepresentation , existingRepresentation , replaceRepresentationCommand . Location , ! isReferenceProperty ) ;
79+ using ( var transaction = await _scimRepresentationCommandRepository . StartTransaction ( ) )
80+ {
81+ await _scimRepresentationCommandRepository . Update ( existingRepresentation ) ;
82+ foreach ( var reference in references . Representations )
8983 {
90- await _scimRepresentationCommandRepository . Update ( existingRepresentation ) ;
91- foreach ( var reference in references . Representations )
92- {
93- await _scimRepresentationCommandRepository . Update ( reference ) ;
94- }
95-
96- await transaction . Commit ( ) ;
84+ await _scimRepresentationCommandRepository . Update ( reference ) ;
9785 }
9886
99- await Notify ( references ) ;
100- existingRepresentation . ApplyEmptyArray ( ) ;
101- return existingRepresentation ;
102- }
103- finally
104- {
105- await _distributedLock . ReleaseLock ( lockName , CancellationToken . None ) ;
87+ await transaction . Commit ( ) ;
10688 }
89+
90+ await Notify ( references ) ;
91+ existingRepresentation . ApplyEmptyArray ( ) ;
92+ return existingRepresentation ;
10793 }
10894
10995 private async Task < UpdateRepresentationResult > UpdateExistingRepresentation ( string resourceType , SCIMRepresentation existingRepresentation , SCIMRepresentation updatedRepresentation )
0 commit comments