public override TDataObject GetObject <TDataObject>(TDataObject objectToGet) { int?objectToGetInternalId; if (objectToGet.IsNew) { objectToGetInternalId = objectToGet.InternalObjectId; } else { if ((objectToGet as PlaceToLocationDataObject) == null) { _logEngine.LogError("Unable to get value which value is null", "The object you are trying to get doesn't have a value", "PlaceToLocationObjectsDataSet", null); throw new PulpException("Unable to get an element which value is null."); } objectToGetInternalId = PlaceToLocationObjectInternalIds.ContainsKey((objectToGet as PlaceToLocationDataObject).PrimaryKeysCollection) ? (int?)PlaceToLocationObjectInternalIds[(objectToGet as PlaceToLocationDataObject).PrimaryKeysCollection] : null; } if (objectToGetInternalId != null) { return(PlaceToLocationObjects.ContainsKey((int)objectToGetInternalId) ? PlaceToLocationObjects[(int)objectToGetInternalId] as TDataObject : null); } return(null); }
public override void RemoveObject(IDataObject objectToRemove) { if (PlaceToLocationObjects == null) { return; } bool completed; int? objectToRemoveInternalId; if ((objectToRemove as PlaceToLocationDataObject) == null) { _logEngine.LogError("Unable to remove null object", "The object you are trying to remove is null", "PlaceToLocationObjectsDataSet.RemoveObject", null); throw new PulpException("Unable to remove Null Object."); } if (objectToRemove.IsNew) { objectToRemoveInternalId = objectToRemove.InternalObjectId; } else { objectToRemoveInternalId = PlaceToLocationObjectInternalIds.ContainsKey((objectToRemove as PlaceToLocationDataObject).PrimaryKeysCollection) ? (int?)PlaceToLocationObjectInternalIds[(objectToRemove as PlaceToLocationDataObject).PrimaryKeysCollection] : null; } if (objectToRemoveInternalId != null) { PlaceToLocationDataObject value; completed = false; var count = 0; while (!completed && count++ < 15) { completed = PlaceToLocationObjects.TryRemove((int)objectToRemoveInternalId, out value); } // Reinit InternalObjectId only if the object to remove is part of the current dataset if (ReferenceEquals(objectToRemove.ObjectsDataSet, this._rootObjectDataSet)) { objectToRemove.InternalObjectId = null; } if (!objectToRemove.IsNew) { int idvalue; completed = false; count = 0; while (!completed && count++ < 15) { completed = PlaceToLocationObjectInternalIds.TryRemove((objectToRemove as PlaceToLocationDataObject).PrimaryKeysCollection, out idvalue); } } // Delete the Location FK Index if ((objectToRemove as PlaceToLocationDataObject).LocationURI != null) { if (Location_FKIndex.ContainsKey((objectToRemove as PlaceToLocationDataObject).LocationURI) && Location_FKIndex[(objectToRemove as PlaceToLocationDataObject).LocationURI].Contains((int)objectToRemoveInternalId)) { Location_FKIndex[(objectToRemove as PlaceToLocationDataObject).LocationURI].Remove((int)objectToRemoveInternalId); if (!Location_FKIndex[(objectToRemove as PlaceToLocationDataObject).LocationURI].Any()) { List <int> outvalue; var iscompleted = false; var count2 = 0; while (!iscompleted && count2++ < 15) { iscompleted = Location_FKIndex.TryRemove((objectToRemove as PlaceToLocationDataObject).LocationURI, out outvalue); } } } LocationDataObject relatedLocation; if ((objectToRemove as PlaceToLocationDataObject)._location_NewObjectId != null) { relatedLocation = _rootObjectDataSet.GetObject(new LocationDataObject() { IsNew = true, InternalObjectId = (objectToRemove as PlaceToLocationDataObject)._location_NewObjectId }); } else { relatedLocation = _rootObjectDataSet.GetObject(new LocationDataObject((objectToRemove as PlaceToLocationDataObject).LocationURI) { IsNew = false }); } if (relatedLocation != null && this.RootObjectDataSet.NotifyChanges) { relatedLocation.NotifyPropertyChanged("PlaceToLocationItems", new SeenObjectCollection()); } } // Delete the Place FK Index if ((objectToRemove as PlaceToLocationDataObject).PlaceURI != null) { if (Place_FKIndex.ContainsKey((objectToRemove as PlaceToLocationDataObject).PlaceURI) && Place_FKIndex[(objectToRemove as PlaceToLocationDataObject).PlaceURI].Contains((int)objectToRemoveInternalId)) { Place_FKIndex[(objectToRemove as PlaceToLocationDataObject).PlaceURI].Remove((int)objectToRemoveInternalId); if (!Place_FKIndex[(objectToRemove as PlaceToLocationDataObject).PlaceURI].Any()) { List <int> outvalue; var iscompleted = false; var count2 = 0; while (!iscompleted && count2++ < 15) { iscompleted = Place_FKIndex.TryRemove((objectToRemove as PlaceToLocationDataObject).PlaceURI, out outvalue); } } } PlaceDataObject relatedPlace; if ((objectToRemove as PlaceToLocationDataObject)._place_NewObjectId != null) { relatedPlace = _rootObjectDataSet.GetObject(new PlaceDataObject() { IsNew = true, InternalObjectId = (objectToRemove as PlaceToLocationDataObject)._place_NewObjectId }); } else { relatedPlace = _rootObjectDataSet.GetObject(new PlaceDataObject((objectToRemove as PlaceToLocationDataObject).PlaceURI) { IsNew = false }); } if (relatedPlace != null && this.RootObjectDataSet.NotifyChanges) { relatedPlace.NotifyPropertyChanged("PlaceToLocationItems", new SeenObjectCollection()); } } } }
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()); } } }