public AweCsomeTable(IAweCsomeTable baseTable, IAweCsomeHelpers helpers, string connectionString)
 {
     _baseTable = baseTable;
     _helpers   = helpers;
     _db        = new LiteDb(helpers, baseTable, connectionString);
     Queue      = new LiteDbQueue(helpers, baseTable, connectionString);
 }
Beispiel #2
0
        public object GetFromDbById(Type baseType, string fullyQualifiedName, int id)
        {
            var        db         = new LiteDb(_helpers, _aweCsomeTable, _connectionString);
            MethodInfo method     = GetMethod <LiteDb>(q => q.GetCollection <object>());
            dynamic    collection = CallGenericMethodByName(db, method, baseType, fullyQualifiedName, null);

            return(collection.FindById(id));
        }
Beispiel #3
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);
                }
            }
        }
Beispiel #4
0
        public void UpdateId(Type baseType, string fullyQualifiedName, int oldId, int newId)
        {
            var        db     = new LiteDb(_helpers, _aweCsomeTable, _connectionString);
            MethodInfo method = GetMethod <LiteDb>(q => q.GetCollection <object>());

            dynamic collection = CallGenericMethodByName(db, method, baseType, fullyQualifiedName, null);
            var     entity     = collection.FindById(oldId);

            if (entity == null)
            {
                _log.Error($"Cannot find {fullyQualifiedName} from id {oldId} to change to {newId}");
                throw new KeyNotFoundException();
            }
            entity.Id = newId;

            // Id CANNOT be updated in LiteDB. We have to delete and recreate instead:
            collection.Delete(oldId);
            collection.Insert(entity);

            UpdateLookups(baseType, GetListNameFromFullyQualifiedName(baseType, fullyQualifiedName), oldId, newId);
            UpdateFileLookups(baseType, GetListNameFromFullyQualifiedName(baseType, fullyQualifiedName), oldId, newId);
            UpdateQueueIds(fullyQualifiedName, oldId, newId);
        }
Beispiel #5
0
        public void DeleteAttachmentFromDbWithoutSyncing(BufferFileMeta meta)
        {
            var db = new LiteDb(_helpers, _aweCsomeTable, _connectionString);

            db.RemoveAttachment(meta);
        }
Beispiel #6
0
        public System.IO.MemoryStream GetAttachmentStreamFromDbById(string id, out string filename, out BufferFileMeta meta)
        {
            var db = new LiteDb(_helpers, _aweCsomeTable, _connectionString);

            return(db.GetAttachmentStreamById(id, out filename, out meta));
        }
Beispiel #7
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);
                        }
                    }
                }
            }
        }
Beispiel #8
0
        private void UpdateFileLookups(Type baseType, string changedListname, int oldId, int newId)
        {
            var db       = new LiteDb(_helpers, _aweCsomeTable, _connectionString);
            var allFiles = db.GetAllFiles();

            if (allFiles == null)
            {
                return;
            }
            foreach (var file in allFiles)
            {
                var meta = db.GetMetadataFromAttachment(file.Metadata);
                if (meta.AttachmentType == BufferFileMeta.AttachmentTypes.Attachment)
                {
                    if (meta.Listname == changedListname && meta.ParentId == oldId)
                    {
                        meta.ParentId = newId;
                        db.UpdateMetadata(file.Id, db.GetMetadataFromAttachment(meta));
                    }
                }
                else
                {
                    if (meta.AdditionalInformation != null)
                    {
                        Type targetType = baseType.Assembly.GetTypes().FirstOrDefault(q => q.FullName == meta.FullyQualifiedName);
                        if (targetType == null)
                        {
                            continue;
                        }
                        var entity = JsonConvert.DeserializeObject(meta.AdditionalInformation, targetType);
                        if (FindLookupProperties(targetType, changedListname, out List <PropertyInfo> lookupProperties, out List <PropertyInfo> virtualStaticProperties, out List <PropertyInfo> virtualDynamicProperties))
                        {
                            bool elementChanged = false;
                            foreach (var lookupProperty in lookupProperties)
                            {
                                if ((int?)lookupProperty.GetValue(entity) == oldId)
                                {
                                    lookupProperty.SetValue(entity, newId);
                                    elementChanged = true;
                                }
                            }
                            foreach (var virtualStaticPropery in virtualStaticProperties)
                            {
                                if ((int?)virtualStaticPropery.GetValue(entity) == oldId)
                                {
                                    virtualStaticPropery.SetValue(entity, newId);
                                    elementChanged = true;
                                }
                            }
                            foreach (var virtualDynamicProperty in virtualDynamicProperties)
                            {
                                var attribute  = virtualDynamicProperty.GetCustomAttribute <VirtualLookupAttribute>();
                                var targetList = (string)targetType.GetProperty(attribute.DynamicTargetProperty).GetValue(entity);
                                if (targetList == changedListname)
                                {
                                    if ((int?)virtualDynamicProperty.GetValue(entity) == oldId)
                                    {
                                        virtualDynamicProperty.SetValue(entity, newId);
                                        elementChanged = true;
                                    }
                                }
                            }
                            if (elementChanged)
                            {
                                meta.AdditionalInformation = JsonConvert.SerializeObject(entity, Formatting.Indented);
                                db.UpdateMetadata(file.Id, db.GetMetadataFromAttachment(meta));
                            }
                        }
                    }
                }
                UpdateFileBaseReferences(meta.AttachmentType, meta.Listname, oldId, newId);
            }
        }