/// <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); } }
/// <summary> /// Deletes resource from the netowrk model. /// </summary> /// <param name="rd">Description of the resource that should be deleted</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); // check if entity exists if (!this.EntityExists(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); } // get entity to be deleted IdentifiedObject io = GetEntity(globalId); // check if entity could be deleted (if it is not referenced by any other entity) 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); } // find property ids List <ModelCode> propertyIds = resourcesDescs.GetAllSettablePropertyIdsForEntityId(io.GlobalId); // remove references Property property = null; foreach (ModelCode propertyId in propertyIds) { PropertyType propertyType = Property.GetPropertyType(propertyId); if (propertyType == PropertyType.Reference) { property = io.GetProperty(propertyId); if (propertyType == PropertyType.Reference) { // get target entity and remove reference to another entity long targetGlobalId = property.AsReference(); if (targetGlobalId != 0) { // get target entity IdentifiedObject targetEntity = GetEntity(targetGlobalId); // remove reference to another entity targetEntity.RemoveReference(propertyId, globalId); } } } } // remove entity form netowrk model DMSType type = (DMSType)ModelCodeHelper.ExtractTypeFromGlobalId(globalId); Container container = GetContainer(type); 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); } }
/// <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); } }
/// <summary> /// Updates entity in block model. /// </summary> /// <param name="rd">Description of the resource that should be updated</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(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); } IdentifiedObject io = GetEntity(globalId); // updating properties of entity foreach (Property property in rd.Properties) { if (property.Type == PropertyType.Reference) { long oldTargetGlobalId = io.GetProperty(property.Id).AsReference(); if (oldTargetGlobalId != 0) { IdentifiedObject oldTargetEntity = GetEntity(oldTargetGlobalId); oldTargetEntity.RemoveReference(property.Id, globalId); } // updating reference of entity long targetGlobalId = property.AsReference(); if (targetGlobalId != 0) { if (!EntityExists(targetGlobalId)) { string message = string.Format("Failed to get target entity with GID: 0x{0:X16}.", targetGlobalId); throw new Exception(message); } IdentifiedObject targetEntity = GetEntity(targetGlobalId); targetEntity.AddReference(property.Id, globalId); } // update value of the property in specified entity io.SetProperty(property); } else { // update value of the property in specified entity io.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); } }