Ejemplo n.º 1
0
        private void InsertEntity(ResourceDescription rd, out ResourceDescription newRd)
        {
            newRd = rd;
            if (rd == null)
            {
                CommonTrace.WriteTrace(CommonTrace.TraceVerbose, "Insert entity is not done because update operation is empty.");
                throw new Exception();
            }

            long globalId = rd.Id;

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

            // check if mapping for specified global id already exists
            if (this.EntityExists(globalId))
            {
                string message = string.Format("Failed to insert entity because entity with specified GID ({0:x16}) already exists in network model.", globalId);
                CommonTrace.WriteTrace(CommonTrace.TraceError, message);
                throw new Exception(message);
            }

            try
            {
                // find type
                DMSType type = (DMSType)ModelCodeHelper.ExtractTypeFromGlobalId(globalId);

                //check mrid already exist
                // if exist made mapping old-new mrids
                if (CheckMridExist(type, rd))
                {
                    var typedCollection = networkDataModelCopy[type];
                    var objMrid         = rd.Properties.Single(x => x.Id == ModelCode.IDOBJ_MRID);
                    var result          = typedCollection.Entities.Values.SingleOrDefault(x => x.MRID == objMrid.PropertyValue.StringValue);
                    GidHelper[globalId] = result.GID;
                    return;
                }

                Container container = null;

                // get container or create container
                if (ContainerExists(type))
                {
                    container = GetContainer(type);
                }
                else
                {
                    container = new Container();
                    networkDataModelCopy.Add(type, container);
                }

                // create entity and add it to container
                IdentifiedObject io = container.CreateEntity(globalId);

                // --------------------------------------------------
                affectedEntities.Add(globalId, DeltaOpType.Insert);
                // --------------------------------------------------

                // apply properties on created entity
                if (rd.Properties != null)
                {
                    foreach (Property property in rd.Properties)
                    {
                        // globalId must not be set as property
                        if (property.Id == ModelCode.IDOBJ_GID)
                        {
                            continue;
                        }

                        if (property.Type == PropertyType.Reference)
                        {
                            // if property is a reference to another entity
                            long targetGlobalId = property.AsReference();
                            if (GidHelper.ContainsKey(targetGlobalId))
                            {
                                //Update old-new mrids
                                targetGlobalId = GidHelper[targetGlobalId];
                                newRd.Properties.Single(x => x.Id == property.Id).SetValue(targetGlobalId);
                            }
                            if (targetGlobalId != 0)
                            {
                                if (!EntityExists(targetGlobalId))
                                {
                                    string message = string.Format("Failed to get target entity with GID: 0x{0:X16}. {1}", targetGlobalId);
                                    throw new Exception(message);
                                }

                                // get referenced entity for update
                                IdentifiedObject targetEntity = GetEntity(targetGlobalId);
                                targetEntity.AddReference(property.Id, io.GID);
                            }

                            io.SetProperty(property);
                        }
                        else
                        {
                            io.SetProperty(property);
                        }
                    }
                }

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