Ejemplo n.º 1
0
        /// <summary>
        /// Fire mergingin event  returning whether the merge shoudl be cancelled
        /// </summary>
        protected bool FireUnmerging(Guid surviorKey, IEnumerable <Guid> linkedKeys)
        {
            var dpe = new DataMergingEventArgs <TModel>(surviorKey, linkedKeys);

            this.UnMerging?.Invoke(this, dpe);
            return(dpe.Cancel);
        }
            /// <summary>
            /// Merges the specified duplicates into the master
            /// </summary>
            public virtual RecordMergeResult Merge(Guid masterKey, IEnumerable <Guid> linkedDuplicates)
            {
                var mergeEventArgs = new DataMergingEventArgs <TModel>(masterKey, linkedDuplicates);

                this.Merging?.Invoke(this, mergeEventArgs);
                if (mergeEventArgs.Cancel)
                {
                    this.m_tracer.TraceInfo("Pre-Event trigger indicated cancel merge");
                    return(new RecordMergeResult(RecordMergeStatus.Cancelled, null, null));
                }

                // The invoke may have changed the master
                masterKey = mergeEventArgs.SurvivorKey;

                var master = ApplicationServiceContext.Current.GetService <IDataPersistenceService <TModel> >().Get(masterKey, null, true, AuthenticationContext.Current.Principal);
                // We'll update the parameters from the candidate to create a single master record
                // TODO: Verify this in edge cases
                Bundle persistenceBundle = new Bundle();

                foreach (var l in linkedDuplicates)
                {
                    var local = ApplicationServiceContext.Current.GetService <IDataPersistenceService <TModel> >().Get(l, null, true, AuthenticationContext.Current.Principal);
                    master.CopyObjectData(local, false); // Copy data which is different

                    // Add replaces and nullify
                    if (l == Guid.Empty)
                    {
                        if (master is Act actMaster)
                        {
                            actMaster.Relationships.Add(new ActRelationship(ActRelationshipTypeKeys.Replaces, l));
                        }
                        else if (master is Entity entityMaster)
                        {
                            entityMaster.Relationships.Add(new EntityRelationship(EntityRelationshipTypeKeys.Replaces, l));
                        }
                        persistenceBundle.Add(local);
                    }
                    else // Not persisted yet
                    {
                        if (master is Act actMaster)
                        {
                            actMaster.Relationships.Add(new ActRelationship(ActRelationshipTypeKeys.Replaces, masterKey)
                            {
                                TargetActKey = l
                            });
                        }
                        else if (master is Entity entityMaster)
                        {
                            entityMaster.Relationships.Add(new EntityRelationship(EntityRelationshipTypeKeys.Replaces, masterKey)
                            {
                                TargetEntityKey = l
                            });
                        }
                    }
                    (local as IHasState).StatusConceptKey = StatusKeys.Nullified;
                }
                master.Key = masterKey;
                persistenceBundle.Add(master);

                // Persist
                ApplicationServiceContext.Current.GetService <IDataPersistenceService <Bundle> >().Update(persistenceBundle, TransactionMode.Commit, AuthenticationContext.SystemPrincipal);
                this.Merged?.Invoke(this, new DataMergeEventArgs <TModel>(masterKey, linkedDuplicates));
                return(new RecordMergeResult(RecordMergeStatus.Success, new Guid[] { masterKey }, linkedDuplicates.ToArray()));
            }