Exemple #1
0
        private void UpdateFileBaseReference <T>(string listname, int oldId, int newId) where T : FileBase
        {
            var db         = new LiteDb(_helpers, _aweCsomeTable, _connectionString);
            var collection = db.GetCollection <T>();
            var atts       = collection.Find(q => q.List == listname && q.ReferenceId == oldId);

            if (atts != null)
            {
                foreach (var att in atts)
                {
                    att.ReferenceId = newId;
                    collection.Update(att);
                }
            }
        }
        public int CountItems <T>()
        {
            var guid   = StartMeasurement();
            var result = _db.GetCollection <T>().Count();

            StopMeasurement(guid, "CountItems (LiteDB)");
            return(result);
        }
Exemple #3
0
        private void UpdateLookups(Type baseType, string changedListname, int oldId, int newId)
        {
            var           db = new LiteDb(_helpers, _aweCsomeTable, _connectionString);
            List <string> collectionNames = db.GetCollectionNames().ToList();
            var           subTypes        = baseType.Assembly.GetTypes();

            foreach (var subType in subTypes)
            {
                if (!collectionNames.Contains(subType.Name))
                {
                    continue;
                }

                bool modifyId = FindLookupProperties(subType, changedListname, out List <PropertyInfo> lookupProperties, out List <PropertyInfo> virtualStaticProperties, out List <PropertyInfo> virtualDynamicProperties);
                if (modifyId)
                {
                    var  collection     = db.GetCollection(subType.Name);
                    var  elements       = collection.FindAll();
                    bool elementChanged = false;
                    foreach (var element in elements)
                    {
                        foreach (var lookupProperty in lookupProperties)
                        {
                            if (element[lookupProperty.Name].IsNull)
                            {
                                continue;
                            }
                            var targetType = element[lookupProperty.Name].GetType();
                            if (targetType == typeof(LiteDB.BsonDocument))
                            {
                                var bson = (LiteDB.BsonDocument)element[lookupProperty.Name];
                                var id   = bson["_id"];
                                if (id == oldId)
                                {
                                    bson["_id"] = newId;
                                    element[lookupProperty.Name] = bson;
                                    elementChanged = true;
                                }
                            }
                            else
                            {
                                bool isId  = false;
                                int? tmpId = null;
                                try
                                {
                                    tmpId = (int?)element[lookupProperty.Name];
                                    isId  = true;
                                }
                                catch (Exception ex)
                                {
                                    _log.Warn($"Cannot cast as int. List to check: {subType.Name} TargetType: {targetType.FullName}, lookupProperty: {lookupProperty.Name}");
                                }
                                if (isId)
                                {
                                    if (tmpId == oldId)
                                    {
                                        element[lookupProperty.Name] = newId;
                                        elementChanged = true;
                                    }
                                }
                                else
                                {
                                    var idProperty = targetType.GetProperty("Id");
                                    if (idProperty == null)
                                    {
                                        _log.Warn($"Unexpected LookupType. List to check: {subType.Name} TargetType: {targetType.FullName}, lookupProperty: {lookupProperty.Name}");
                                    }
                                    else
                                    {
                                        var id = idProperty.GetValue(element[lookupProperty.Name]);
                                        if (id.Equals(oldId))
                                        {
                                            idProperty.SetValue(element[lookupProperty.Name], newId);
                                            elementChanged = true;
                                        }
                                    }
                                }
                            }
                        }
                        foreach (var virtualStaticProperty in virtualStaticProperties)
                        {
                            if (!element.ContainsKey(virtualStaticProperty.Name))
                            {
                                continue;
                            }
                            if ((int?)element[virtualStaticProperty.Name] == oldId)
                            {
                                element[virtualStaticProperty.Name] = newId;
                                elementChanged = true;
                            }
                        }
                        foreach (var virtualDynamicProperty in virtualDynamicProperties)
                        {
                            var attribute = virtualDynamicProperty.GetCustomAttribute <VirtualLookupAttribute>();
                            if (!element.ContainsKey(attribute.DynamicTargetProperty))
                            {
                                continue;
                            }

                            if (element[attribute.DynamicTargetProperty] == changedListname)
                            {
                                if ((int?)element[virtualDynamicProperty.Name] == oldId)
                                {
                                    element[virtualDynamicProperty.Name] = newId;
                                    elementChanged = true;
                                }
                            }
                        }
                        if (elementChanged)
                        {
                            collection.Update(element);
                        }
                    }
                }
            }
        }