Exemplo n.º 1
0
        protected IOriCollection Intern(ICUDResult cudResult, IMethodDescription methodDescription, IList <MergeOperation> mergeOperationSequence,
                                        IncrementalMergeState state)
        {
            IList <IChangeContainer> allChanges   = cudResult.AllChanges;
            IList <Object>           originalRefs = cudResult.GetOriginalRefs();
            IdentityHashMap <IChangeContainer, int> changeToChangeIndexDict = new IdentityHashMap <IChangeContainer, int>();

            for (int a = allChanges.Count; a-- > 0;)
            {
                changeToChangeIndexDict.Put(allChanges[a], a);
            }
            IObjRef[] objRefs      = new IObjRef[allChanges.Count];
            long[]    allChangedOn = new long[allChanges.Count];
            String[]  allChangedBy = new String[allChanges.Count];

            CHashSet <long>   changedOnSet = new CHashSet <long>();
            CHashSet <String> changedBySet = new CHashSet <String>();

            for (int a = 0, size = mergeOperationSequence.Count; a < size; a++)
            {
                MergeOperation         mergeOperation        = mergeOperationSequence[a];
                IMergeServiceExtension mergeServiceExtension = mergeOperation.MergeServiceExtension;

                IList <IChangeContainer> changesForMergeService = mergeOperation.ChangeContainer;
                ICUDResult msCudResult = BuildCUDResult(changesForMergeService, changeToChangeIndexDict, originalRefs);

                IOriCollection msOriCollection = mergeServiceExtension.Merge(msCudResult, methodDescription);

                MergeController.ApplyChangesToOriginals(msCudResult, msOriCollection, state.GetStateCache());

                IList <IObjRef> allChangeORIs = msOriCollection.AllChangeORIs;

                long?  msDefaultChangedOn = msOriCollection.ChangedOn;
                String msDefaultChangedBy = msOriCollection.ChangedBy;

                long[]   msAllChangedOn = msOriCollection.AllChangedOn;
                String[] msAllChangedBy = msOriCollection.AllChangedBy;
                for (int b = changesForMergeService.Count; b-- > 0;)
                {
                    int index = changeToChangeIndexDict.Get(changesForMergeService[b]);
                    objRefs[index] = allChangeORIs[b];

                    if (msAllChangedOn != null)
                    {
                        long msChangedOn = msAllChangedOn[b];
                        allChangedOn[index] = msChangedOn;
                        changedOnSet.Add(msChangedOn);
                    }
                    else
                    {
                        allChangedOn[index] = msDefaultChangedOn.Value;
                    }
                    if (msAllChangedBy != null)
                    {
                        String msChangedBy = msAllChangedBy[b];
                        allChangedBy[index] = msChangedBy;
                        changedBySet.Add(msChangedBy);
                    }
                    else
                    {
                        allChangedBy[index] = msDefaultChangedBy;
                    }
                }
                if (msDefaultChangedOn != null)
                {
                    changedOnSet.Add(msDefaultChangedOn.Value);
                }
                if (msDefaultChangedBy != null)
                {
                    changedBySet.Add(msDefaultChangedBy);
                }
            }
            OriCollection oriCollection = new OriCollection();

            oriCollection.AllChangeORIs = new List <IObjRef>(objRefs);

            if (changedBySet.Count == 1)
            {
                Iterator <String> iter = changedBySet.Iterator();
                iter.MoveNext();
                oriCollection.ChangedBy = iter.Current;
            }
            else
            {
                oriCollection.AllChangedBy = allChangedBy;
            }
            if (changedOnSet.Count == 1)
            {
                Iterator <long> iter = changedOnSet.Iterator();
                iter.MoveNext();
                oriCollection.ChangedOn = iter.Current;
            }
            else
            {
                oriCollection.AllChangedOn = allChangedOn;
            }
            foreach (IMergeListener mergeListener in mergeListeners.GetExtensions())
            {
                mergeListener.PostMerge(cudResult, objRefs);
            }
            if (originalRefs != null)
            {
                // Set each original ref to null in order to suppress a post-processing in a potentially calling IMergeProcess
                for (int a = originalRefs.Count; a-- > 0;)
                {
                    originalRefs[a] = null;
                }
            }
            // TODO DCE must be fired HERE <---
            return(oriCollection);
        }
Exemplo n.º 2
0
        public IOriCollection Merge(ICUDResult cudResult, IMethodDescription methodDescription)
        {
            int         localDcId;
            IDataChange dataChange;
            IList <IChangeContainer> allChanges    = cudResult.AllChanges;
            IList <IObjRef>          resultOriList = new List <IObjRef>(allChanges.Count);
            String changedBy = "anonymous";
            long   changedOn;

            Lock writeLock = PersistenceMock.GetWriteLock();

            writeLock.Lock();
            try
            {
                localDcId = ++dcId;
                changedOn = DateTimeUtil.CurrentTimeMillis();

                IList <IDataChangeEntry> inserts = new List <IDataChangeEntry>();
                IList <IDataChangeEntry> updates = new List <IDataChangeEntry>();
                IList <IDataChangeEntry> deletes = new List <IDataChangeEntry>();
                for (int a = 0, size = allChanges.Count; a < size; a++)
                {
                    IChangeContainer changeContainer = allChanges[a];
                    IObjRef          reference       = changeContainer.Reference;
                    if (changeContainer is DeleteContainer)
                    {
                        PersistenceMock.RemoveObject(reference);
                        resultOriList.Add(null);
                        deletes.Add(new DataChangeEntry(reference.RealType, reference.IdNameIndex, reference.Id, reference.Version));
                    }
                    else if (changeContainer is UpdateContainer)
                    {
                        resultOriList.Add(reference);
                        reference.Version = ((int)reference.Version) + 1;
                        PersistenceMock.ChangeObject(reference, ((UpdateContainer)changeContainer).Primitives, ((UpdateContainer)changeContainer).Relations, changedBy, changedOn);
                        updates.Add(new DataChangeEntry(reference.RealType, reference.IdNameIndex, reference.Id, reference.Version));
                    }
                    else if (changeContainer is CreateContainer)
                    {
                        Object newId = AcquireIdForEntityType(reference.RealType);
                        if (newId == null)
                        {
                            throw new Exception("Id must be valid");
                        }
                        IEntityMetaData metaData = EntityMetaDataProvider.GetMetaData(reference.RealType);
                        newId             = ConversionHelper.ConvertValueToType(metaData.IdMember.ElementType, newId);
                        reference.Id      = newId;
                        reference.Version = 1;
                        ((IDirectObjRef)reference).Direct = null;
                        resultOriList.Add(reference);
                        PersistenceMock.AddObject(reference, ((CreateContainer)changeContainer).Primitives, ((CreateContainer)changeContainer).Relations, changedBy, changedOn);
                        inserts.Add(new DataChangeEntry(reference.RealType, ObjRef.PRIMARY_KEY_INDEX, reference.Id, reference.Version));
                    }
                }
                OriCollection oriColl = new OriCollection(resultOriList);
                oriColl.ChangedBy = changedBy;
                oriColl.ChangedOn = changedOn;
                MergeController.ApplyChangesToOriginals(cudResult, oriColl, null);
                dataChange = new DataChangeEvent(inserts, updates, deletes, DateTimeUtil.ConvertJavaMillisToDateTime(changedOn), false);
            }
            finally
            {
                writeLock.Unlock();
            }
            EventDispatcher.DispatchEvent(dataChange, dataChange.ChangeTime, localDcId);

            OriCollection oriCollection = new OriCollection(resultOriList);

            oriCollection.ChangedBy = changedBy;
            oriCollection.ChangedOn = changedOn;
            return(oriCollection);
        }