Exemple #1
0
        public override void AddObject(IDataObject objectToAdd, bool replaceIfExists)
        {
            var existingObject = GetObject(objectToAdd);

            if (!replaceIfExists && existingObject != null)
            {
                throw new PulpException("Object already exists");
            }

            int newInternalId;

            if (existingObject != null)
            {
                //RemoveObject(existingObject);
                if (existingObject.InternalObjectId == null)
                {
                    _logEngine.LogError("Error while trying to Add Object to the PlaceToLocationObjectsDataSet", "The object you are trying to add doesn't have an InternalObjectId", "PlaceToLocationObjectsDataSet", null);
                    throw new PulpException("Error while trying to add an object to the dataset without InternalObjectId");
                }
                newInternalId = (int)existingObject.InternalObjectId;
                objectToAdd.InternalObjectId = newInternalId;
                existingObject.CopyValuesFrom(objectToAdd, false);
            }
            else
            {
                newInternalId = GetNextNewInternalObjectId();
                objectToAdd.InternalObjectId = newInternalId;

                var completed = false;
                var count     = 0;
                while (!completed && count++ < 15)
                {
                    completed = PlaceToLocationObjects.TryAdd(newInternalId, (PlaceToLocationDataObject)objectToAdd);
                }
            }

            if (!objectToAdd.IsNew && existingObject == null)
            {
                //The following if should not be necessary...
                var completed = false;
                if (PlaceToLocationObjectInternalIds.ContainsKey(((PlaceToLocationDataObject)objectToAdd).PrimaryKeysCollection))
                {
                    int value;
                    var count2 = 0;
                    while (!completed && count2++ < 15)
                    {
                        completed = PlaceToLocationObjectInternalIds.TryRemove(((PlaceToLocationDataObject)objectToAdd).PrimaryKeysCollection, out value);
                    }
                }

                completed = false;
                var count = 0;
                while (!completed && count++ < 15)
                {
                    completed = PlaceToLocationObjectInternalIds.TryAdd(((PlaceToLocationDataObject)objectToAdd).PrimaryKeysCollection, newInternalId);
                }
            }
            // Update relations including platform as "many" side or "one" side , pk side for one to one relations
            if ((objectToAdd as PlaceToLocationDataObject) == null)
            {
                _logEngine.LogError("Unable to Add an object which is null", "Unable to add an object which is null", "PlaceToLocationDataObject", null);
                throw new PulpException("Unexpected Error: Unable to Add an object which is Null.");
            }

            // Update the Location FK Index
            if ((objectToAdd as PlaceToLocationDataObject).LocationURI != null)
            {
                if (!Location_FKIndex.ContainsKey((objectToAdd as PlaceToLocationDataObject).LocationURI))
                {
                    var iscompleted = false;
                    var count2      = 0;
                    while (!iscompleted && count2++ < 15)
                    {
                        iscompleted = Location_FKIndex.TryAdd((objectToAdd as PlaceToLocationDataObject).LocationURI, new List <int>());
                    }
                }

                if (!Location_FKIndex[(objectToAdd as PlaceToLocationDataObject).LocationURI].Contains(newInternalId))
                {
                    Location_FKIndex[(objectToAdd as PlaceToLocationDataObject).LocationURI].Add(newInternalId);
                }

                LocationDataObject relatedLocation;
                if ((objectToAdd as PlaceToLocationDataObject)._location_NewObjectId != null)
                {
                    relatedLocation = _rootObjectDataSet.GetObject(new LocationDataObject()
                    {
                        IsNew = true, InternalObjectId = (objectToAdd as PlaceToLocationDataObject)._location_NewObjectId
                    });
                }
                else
                {
                    relatedLocation = _rootObjectDataSet.GetObject(new LocationDataObject((objectToAdd as PlaceToLocationDataObject).LocationURI)
                    {
                        IsNew = false
                    });
                }

                if (relatedLocation != null && this.RootObjectDataSet.NotifyChanges)
                {
                    relatedLocation.NotifyPropertyChanged("PlaceToLocationItems", new SeenObjectCollection());
                }
            }

            // Update the Place FK Index
            if ((objectToAdd as PlaceToLocationDataObject).PlaceURI != null)
            {
                if (!Place_FKIndex.ContainsKey((objectToAdd as PlaceToLocationDataObject).PlaceURI))
                {
                    var iscompleted = false;
                    var count2      = 0;
                    while (!iscompleted && count2++ < 15)
                    {
                        iscompleted = Place_FKIndex.TryAdd((objectToAdd as PlaceToLocationDataObject).PlaceURI, new List <int>());
                    }
                }

                if (!Place_FKIndex[(objectToAdd as PlaceToLocationDataObject).PlaceURI].Contains(newInternalId))
                {
                    Place_FKIndex[(objectToAdd as PlaceToLocationDataObject).PlaceURI].Add(newInternalId);
                }

                PlaceDataObject relatedPlace;
                if ((objectToAdd as PlaceToLocationDataObject)._place_NewObjectId != null)
                {
                    relatedPlace = _rootObjectDataSet.GetObject(new PlaceDataObject()
                    {
                        IsNew = true, InternalObjectId = (objectToAdd as PlaceToLocationDataObject)._place_NewObjectId
                    });
                }
                else
                {
                    relatedPlace = _rootObjectDataSet.GetObject(new PlaceDataObject((objectToAdd as PlaceToLocationDataObject).PlaceURI)
                    {
                        IsNew = false
                    });
                }

                if (relatedPlace != null && this.RootObjectDataSet.NotifyChanges)
                {
                    relatedPlace.NotifyPropertyChanged("PlaceToLocationItems", new SeenObjectCollection());
                }
            }
        }