示例#1
0
        /// <summary>
        /// Brisanje entiteta iz kopije
        /// </summary>
        /// <param name="rd">Resource description</param>
        private void DeleteEntity(ResourceDescription rd)
        {
            if (rd == null)
            {
                CommonTrace.WriteTrace(CommonTrace.TraceInfo, "Delete entity is not done because update operation is empty.");
                return;
            }

            try
            {
                long globalId = rd.Id;

                CommonTrace.WriteTrace(CommonTrace.TraceVerbose, "Deleting entity with GID ({0:x16}).", globalId);

                //Proveravamo da li GlobalId postoji u kopiji, ako ne postoji zavrsavamo
                if (!this.EntityExists(copy, globalId))
                {
                    string message = String.Format("Failed to delete entity because entity with specified GID ({0:x16}) does not exist in network model.", globalId);
                    CommonTrace.WriteTrace(CommonTrace.TraceError, message);
                    throw new Exception(message);
                }

                //Trazimo entitet koji se brise
                IdentifiedObject io = GetEntity(copy, globalId);

                //Proveravamo da li entitet moze da se brise (moze da se brise ako nije referanciran od drugog entiteta)
                if (io.IsReferenced)
                {
                    Dictionary <ModelCode, List <long> > references = new Dictionary <ModelCode, List <long> >();
                    io.GetReferences(references, TypeOfReference.Target);

                    StringBuilder sb = new StringBuilder();

                    foreach (KeyValuePair <ModelCode, List <long> > kvp in references)
                    {
                        foreach (long referenceGlobalId in kvp.Value)
                        {
                            sb.AppendFormat("0x{0:x16}, ", referenceGlobalId);
                        }
                    }

                    string message = String.Format("Failed to delete entity (GID = 0x{0:x16}) because it is referenced by entities with GIDs: {1}.", globalId, sb.ToString());
                    CommonTrace.WriteTrace(CommonTrace.TraceError, message);
                    throw new Exception(message);
                }

                //Pronadjem sve moled kodove property-a za dati entitet
                List <ModelCode> propertyIds = resourcesDescs.GetAllSettablePropertyIdsForEntityId(io.GlobalId);

                //Brisem reference
                Property property = null;
                foreach (ModelCode propertyId in propertyIds)
                {
                    PropertyType propertyType = Property.GetPropertyType(propertyId);

                    //Ako model kod nije referenca, odbacujemo ga i proveravamo drugi
                    if (propertyType != PropertyType.Reference)
                    {
                        continue;
                    }

                    //Ako jeste uzimamo property sa tim model kodom
                    property = io.GetProperty(propertyId);

                    //Dobijamo id target entiteta
                    long targetGlobalId = property.AsReference();

                    if (targetGlobalId != 0)
                    {
                        //Dobijem target entitet i uradim njegov deep copy
                        IdentifiedObject targetEntity     = GetEntity(copy, targetGlobalId);
                        IdentifiedObject copyTargetEntity = targetEntity.DeepCopy();

                        //Dobijem kontejner target elementa uradim ShallowCopy kontejnera
                        DMSType typeTarget = (DMSType)ModelCodeHelper.ExtractTypeFromGlobalId(targetEntity.GlobalId);
                        ConatinerShallowCopy(typeTarget);

                        //Zamena originalnog entiteta sa kopijom u novom kontejneru
                        copy[typeTarget].Entities[copyTargetEntity.GlobalId] = copyTargetEntity;

                        //Brise se referenca
                        copyTargetEntity.RemoveReference(propertyId, globalId);
                    }
                }

                //Radimo shallow copy kontejnera
                DMSType type = (DMSType)ModelCodeHelper.ExtractTypeFromGlobalId(globalId);
                ConatinerShallowCopy(type);
                Container container = GetContainer(copy, type);

                //Brisemo entitet iz kopije
                container.RemoveEntity(globalId);

                CommonTrace.WriteTrace(CommonTrace.TraceVerbose, "Deleting entity with GID ({0:x16}) successfully finished.", globalId);
            }
            catch (Exception ex)
            {
                string message = String.Format("Failed to delete entity (GID = 0x{0:x16}) from model. {1}", rd.Id, ex.Message);
                CommonTrace.WriteTrace(CommonTrace.TraceError, message);
                throw new Exception(message);
            }
        }
示例#2
0
        /// <summary>
        /// Update entiteta u kopiji
        /// </summary>
        /// <param name="rd">Resource description</param>
        private void UpdateEntity(ResourceDescription rd)
        {
            if (rd == null || rd.Properties == null && rd.Properties.Count == 0)
            {
                CommonTrace.WriteTrace(CommonTrace.TraceInfo, "Update entity is not done because update operation is empty.");
                return;
            }

            try
            {
                long globalId = rd.Id;

                CommonTrace.WriteTrace(CommonTrace.TraceVerbose, "Updating entity with GID ({0:x16}).", globalId);

                if (!this.EntityExists(original, globalId))
                {
                    string message = String.Format("Failed to update entity because entity with specified GID ({0:x16}) does not exist in network model.", globalId);
                    CommonTrace.WriteTrace(CommonTrace.TraceError, message);
                    throw new Exception(message);
                }

                //Dobijam tip objekta koji se ubacuje iz njegovog globalnog identifikatora
                DMSType type = (DMSType)ModelCodeHelper.ExtractTypeFromGlobalId(globalId);

                //Pravim Shallow kopiju kontejnera tako sto napravim novi kontejner i u njega prevezem sve reference iz originala
                //Ideja: Sve sto dodirnem kopiram
                Container containerCopy = ConatinerShallowCopy(type);

                //Dobijam postojeci objekat koji je isti i za kopiju i za original
                IdentifiedObject io = GetEntity(copy, globalId);

                //Vrsi se deep kopi tog objekta
                IdentifiedObject copyIo = io.DeepCopy();
                //Na mesto original objekta u copy kontejneru ubacujem kopiju tog objekta
                copy[type].Entities[io.GlobalId] = copyIo;

                //Prolazimo kroz sve properties i radimo njihov update
                foreach (Property property in rd.Properties)
                {
                    //Prvo proveravamo da li je property referenca
                    if (property.Type == PropertyType.Reference)
                    {
                        long oldTargetGlobalId = copyIo.GetProperty(property.Id).AsReference();

                        if (oldTargetGlobalId != 0)
                        {
                            //Pronadjem objekat sa kojim je objekat povezan
                            IdentifiedObject oldTargetEntity = GetEntity(copy, oldTargetGlobalId);

                            //Pravi se kopija target entiteta
                            IdentifiedObject copyOldTargetEntity = oldTargetEntity.DeepCopy();

                            //Dobijem kontejner target elementa uradim ShallowCopy kontejnera
                            DMSType typeTarget = (DMSType)ModelCodeHelper.ExtractTypeFromGlobalId(oldTargetEntity.GlobalId);
                            ConatinerShallowCopy(typeTarget);

                            //Zamena originalnog entiteta sa kopijom u novom kontejneru
                            copy[typeTarget].Entities[copyOldTargetEntity.GlobalId] = copyOldTargetEntity;

                            //Ukolonim tu referencu
                            copyOldTargetEntity.RemoveReference(property.Id, globalId);
                        }

                        //Postavim id nove reference
                        long targetGlobalId = property.AsReference();

                        if (targetGlobalId != 0)
                        {
                            if (!EntityExists(copy, targetGlobalId))
                            {
                                string message = string.Format("Failed to get target entity with GID: 0x{0:X16}.", targetGlobalId);
                                throw new Exception(message);
                            }

                            //Pronadjem novi obejak na koji je povezan i setujem mu referencu
                            IdentifiedObject targetEntity = GetEntity(copy, targetGlobalId);

                            //Pravii se kopija target entiteta
                            IdentifiedObject copyTargetEntity = targetEntity.DeepCopy();

                            //Dobijem kontejner target elementa uradim ShallowCopy kontejnera
                            DMSType typeTarget = (DMSType)ModelCodeHelper.ExtractTypeFromGlobalId(targetEntity.GlobalId);
                            ConatinerShallowCopy(typeTarget);

                            //Zamena originalnog entiteta sa kopijom u novom kontejneru
                            copy[typeTarget].Entities[targetEntity.GlobalId] = copyTargetEntity;

                            copyTargetEntity.AddReference(property.Id, globalId);
                        }

                        //Odradi update ostalih properties
                        copyIo.SetProperty(property);
                    }
                    else
                    {
                        //Odradi update ostalih properties
                        copyIo.SetProperty(property);
                    }
                }

                CommonTrace.WriteTrace(CommonTrace.TraceVerbose, "Updating entity with GID ({0:x16}) successfully finished.", globalId);
            }
            catch (Exception ex)
            {
                string message = String.Format("Failed to update entity (GID = 0x{0:x16}) in model. {1} ", rd.Id, ex.Message);
                CommonTrace.WriteTrace(CommonTrace.TraceError, message);
                throw new Exception(message);
            }
        }