/// <summary> /// Partials the delete entity. /// </summary> /// <typeparam name="TObject">The type of the object.</typeparam> /// <param name="dbCollectionName">Name of the database collection.</param> /// <param name="parser">The parser.</param> /// <param name="uri">The URI.</param> /// <exception cref="WitsmlException"></exception> protected void PartialDeleteEntity <TObject>(string dbCollectionName, WitsmlQueryParser parser, EtpUri uri) { try { Logger.DebugFormat("Partial Deleting {0} MongoDb collection", dbCollectionName); var collection = GetCollection <TObject>(dbCollectionName); var current = GetEntity <TObject>(uri, dbCollectionName); var updates = MongoDbUtility.CreateUpdateFields <TObject>(); var ignores = MongoDbUtility.CreateIgnoreFields <TObject>(GetIgnoredElementNamesForUpdate(parser)); var partialDelete = new MongoDbDelete <TObject>(Container, collection, parser, IdPropertyName, ignores); partialDelete.PartialDelete(current, uri, updates); var transaction = Transaction; transaction.Attach(MongoDbAction.Update, dbCollectionName, IdPropertyName, current.ToBsonDocument(), uri); transaction.Save(); AuditPartialDelete(uri); } catch (MongoException ex) { Logger.ErrorFormat("Error partial deleting {0} MongoDb collection: {1}", dbCollectionName, ex); throw new WitsmlException(ErrorCodes.ErrorUpdatingInDataStore, ex); } }
public void MongoDbUtility_CreateIgnoreFields_Returns_List_Of_common_Objects_To_Ignore() { var commonPropertiesToIgnore = MongoDbUtility.CreateIgnoreFields <Well>(null); Assert.IsTrue(commonPropertiesToIgnore.Count > 0); commonPropertiesToIgnore = MongoDbUtility.CreateIgnoreFields <Witsml200.Well>(null); Assert.IsTrue(commonPropertiesToIgnore.Count > 0); var ignoredList = new List <string> { "dTimLicense" }; commonPropertiesToIgnore = MongoDbUtility.CreateIgnoreFields <Well>(ignoredList); Assert.IsTrue(commonPropertiesToIgnore.Count > 1); Assert.IsTrue(commonPropertiesToIgnore.Contains("dTimLicense")); }
/// <summary> /// Replaces an object in the data store. /// </summary> /// <typeparam name="TObject">The type of the object.</typeparam> /// <param name="dbCollectionName">The name of the database collection.</param> /// <param name="entity">The object to be replaced.</param> /// <param name="uri">The data object URI.</param> /// <param name="ignoreServerProperties">if set to <c>true</c> ignores server properties.</param> /// <exception cref="WitsmlException"></exception> protected void ReplaceEntity <TObject>(string dbCollectionName, TObject entity, EtpUri uri, bool ignoreServerProperties = true) { try { Logger.DebugFormat("Replacing {0} MongoDb collection", dbCollectionName); var collection = GetCollection <TObject>(dbCollectionName); var current = GetEntity <TObject>(uri, dbCollectionName); //var updates = MongoDbUtility.CreateUpdateFields<TObject>(); var ignores = MongoDbUtility.CreateIgnoreFields <TObject>(null, true); if (ignoreServerProperties) { ignores.AddRange(GetIgnoredElementNamesForUpdate(null) ?? Enumerable.Empty <string>()); } var mapper = new DataObjectMapper <TObject>(Container, null, ignores); mapper.Map(current, entity); // Update Last Change Date AuditHistoryAdapter.SetDateTimeLastChange(entity); var filter = GetEntityFilter <TObject>(uri, IdPropertyName); collection.ReplaceOne(filter, entity); var transaction = Transaction; transaction.Attach(MongoDbAction.Update, dbCollectionName, IdPropertyName, current.ToBsonDocument(), uri); transaction.Save(); AuditUpdate(uri); } catch (MongoException ex) { Logger.ErrorFormat("Error replacing {0} MongoDb collection: {1}", dbCollectionName, ex); throw new WitsmlException(ErrorCodes.ErrorReplacingInDataStore, ex); } }