public void MongoDbUtility_LookUpIdField_Returns_IdField_For_Specified_Type()
        {
            var well141 = MongoDbUtility.LookUpIdField(typeof(Well));

            Assert.AreEqual(ObjectTypes.Uid, well141);
            var well200 = MongoDbUtility.LookUpIdField(typeof(Witsml200.Well), ObjectTypes.Uuid);

            Assert.AreEqual(ObjectTypes.Uuid, well200);
            var well200ChannelIndex = MongoDbUtility.LookUpIdField(typeof(Witsml200.ComponentSchemas.ChannelIndex));

            Assert.AreEqual("Mnemonic", well200ChannelIndex);
        }
Example #2
0
        private void PartialDeleteArrayElements(List <XElement> elements, PropertyInfo propertyInfo, object propertyValue, Type type, string parentPath)
        {
            Logger.DebugFormat($"Partial deleting array elements: {parentPath} {propertyInfo?.Name}");

            var updateBuilder = Builders <T> .Update;
            var filterBuilder = Builders <T> .Filter;
            var idField       = MongoDbUtility.LookUpIdField(type);
            var filterPath    = GetPropertyPath(parentPath, idField);
            var properties    = GetPropertyInfo(type);

            var ids       = new List <string>();
            var itemsById = GetItemsById((IEnumerable)propertyValue, properties, idField, ids);

            var updateList = elements
                             .Select(element =>
            {
                var elementId = GetElementId(element, idField);
                if (string.IsNullOrEmpty(elementId) || propertyInfo == null)
                {
                    return(null);
                }

                var filters = new List <FilterDefinition <T> >()
                {
                    _entityFilter
                };

                object current;
                itemsById.TryGetValue(elementId, out current);

                if (current == null)
                {
                    return(null);
                }

                var elementFilter = Builders <T> .Filter.EqIgnoreCase(filterPath, elementId);
                filters.Add(elementFilter);
                var filter = filterBuilder.And(filters);

                if (element.Elements().Any())
                {
                    var position     = ids.IndexOf(elementId);
                    var positionPath = parentPath + "." + position;

                    var update = updateBuilder.Set(GetPropertyPath(positionPath, idField), elementId);

                    var saveUpdate = Context.Update;
                    Context.Update = update;

                    PushPropertyInfo(propertyInfo, current);
                    NavigateElement(element, type, positionPath);
                    PopPropertyInfo();

                    var model      = new UpdateOneModel <T>(filter, Context.Update);
                    Context.Update = saveUpdate;
                    return(model);
                }
                else
                {
                    if (IsRequired(propertyInfo) && itemsById.Count == 1)
                    {
                        throw new WitsmlException(ErrorCodes.MustRetainOneRecurringNode);
                    }

                    var childFilter = MongoDbExtensions.EqualsIgnoreCase(type, idField, elementId);
                    var update      = MongoDbExtensions.PullFilter(typeof(T), type, parentPath, childFilter) as UpdateDefinition <T>;

                    if (childFilter != null && update != null)
                    {
                        //var update = updateBuilder.Pull(parentPath, current);
                        AddToPullCollection(parentPath, new UpdateOneModel <T>(filter, update));
                    }

                    return(null);
                }
            })
                             .Where(x => x != null)
                             .ToList();

            if (updateList.Count > 0)
            {
                _collection.BulkWrite(updateList);
            }
        }
Example #3
0
        /// <summary>
        /// Updates the array elements.
        /// </summary>
        /// <param name="elements">The elements.</param>
        /// <param name="propertyInfo">The property information.</param>
        /// <param name="propertyValue">The property value.</param>
        /// <param name="type">The type.</param>
        /// <param name="parentPath">The parent path.</param>
        protected override void UpdateArrayElements(List <XElement> elements, PropertyInfo propertyInfo, object propertyValue, Type type, string parentPath)
        {
            Logger.DebugFormat($"Merge array elements: {parentPath} {propertyInfo?.Name}");

            if (MergeDelete && RemoveAll(elements))
            {
                var list = propertyValue as IList;
                list?.Clear();
                return;
            }

            var idField    = MongoDbUtility.LookUpIdField(type);
            var properties = GetPropertyInfo(type);

            var ids       = new List <string>();
            var itemsById = GetItemsById((IEnumerable)propertyValue, properties, idField, ids);

            foreach (var element in elements)
            {
                var elementId = GetElementId(element, idField);
                if (string.IsNullOrEmpty(elementId) || propertyInfo == null)
                {
                    continue;
                }

                object current;
                itemsById.TryGetValue(elementId, out current);

                if (current == null)
                {
                    if (MergeDelete)
                    {
                        continue;
                    }

                    ValidateArrayElement(element, properties);
                    ValidateArrayElement(propertyInfo, type, element, parentPath);

                    if (Context.ValidationOnly)
                    {
                        continue;
                    }

                    var item = ParseNestedElement(type, element);
                    if (propertyValue == null)
                    {
                        continue;
                    }

                    var list = propertyValue as IList;
                    list?.Add(item);
                }
                else
                {
                    if (MergeDelete && RemoveItem(element))
                    {
                        var list = propertyValue as IList;
                        list?.Remove(current);
                    }
                    else
                    {
                        var position     = ids.IndexOf(elementId);
                        var positionPath = parentPath + "." + position;
                        ValidateArrayElement(element, properties, false);

                        PushPropertyInfo(propertyInfo, current);
                        NavigateElement(element, type, positionPath);
                        PopPropertyInfo();
                    }
                }
            }
        }
Example #4
0
        /// <summary>
        /// Updates the array elements.
        /// </summary>
        /// <param name="elements">The elements.</param>
        /// <param name="propertyInfo">The property information.</param>
        /// <param name="propertyValue">The property value.</param>
        /// <param name="type">The type.</param>
        /// <param name="parentPath">The parent path.</param>
        protected override void UpdateArrayElements(List <XElement> elements, PropertyInfo propertyInfo, object propertyValue, Type type, string parentPath)
        {
            Logger.DebugFormat($"Updating array elements: {parentPath} {propertyInfo?.Name}");

            var updateBuilder = Builders <T> .Update;
            var filterBuilder = Builders <T> .Filter;
            var idField       = MongoDbUtility.LookUpIdField(type);
            var filterPath    = GetPropertyPath(parentPath, idField);
            var properties    = GetPropertyInfo(type);

            var ids       = new List <string>();
            var itemsById = GetItemsById((IEnumerable)propertyValue, properties, idField, ids);

            var updateList = elements
                             .Select(element =>
            {
                var elementId = GetElementId(element, idField);
                if (string.IsNullOrEmpty(elementId) || propertyInfo == null)
                {
                    return(null);
                }

                var filters = new List <FilterDefinition <T> >()
                {
                    _entityFilter
                };

                object current;
                itemsById.TryGetValue(elementId, out current);

                if (current == null)
                {
                    ValidateArrayElement(element, properties);
                    ValidateArrayElement(propertyInfo, type, element, parentPath);

                    if (Context.ValidationOnly)
                    {
                        return(null);
                    }

                    var item   = ParseNestedElement(type, element);
                    var filter = filterBuilder.And(filters);

                    var update = propertyValue == null
                            ? updateBuilder.Set(parentPath, CreateList(propertyInfo.PropertyType, item))
                            : updateBuilder.Push(parentPath, item);

                    return(new UpdateOneModel <T>(filter, update));
                }
                else
                {
                    var position     = ids.IndexOf(elementId);
                    var positionPath = parentPath + "." + position;
                    ValidateArrayElement(element, properties, false);

                    var elementFilter = Builders <T> .Filter.EqIgnoreCase(filterPath, elementId);
                    filters.Add(elementFilter);

                    var filter = filterBuilder.And(filters);
                    var update = updateBuilder.Set(GetPropertyPath(positionPath, idField), elementId);

                    var saveUpdate = Context.Update;
                    Context.Update = update;

                    PushPropertyInfo(propertyInfo, current);
                    NavigateElement(element, type, positionPath);
                    PopPropertyInfo();

                    var model      = new UpdateOneModel <T>(filter, Context.Update);
                    Context.Update = saveUpdate;
                    return(model);
                }
            })
                             .Where(x => x != null)
                             .ToList();

            if (updateList.Count > 0)
            {
                Collection.BulkWrite(updateList);
            }
        }