Exemple #1
0
        /// <summary>
        /// Handles file changed events.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FileSystemWatcher_Changed(object sender, FileSystemEventArgs e)
        {
            if (!lastFileChanged.Equals(e.Name))
            {
                string fileName = e.Name;

                if (fileName.Contains(".xml"))
                {
                    fileName = fileName.Replace(".xml", "");

                    if (fileName.Contains("Database"))
                    {
                        OnDatabaseChanged?.Invoke(this, new OnDatabaseChangedEventArgs(fileName, DateTime.Now));
                    }
                }

                lastFileChanged = e.Name;
            }
            else
            {
                lastFileChanged = string.Empty;
            }

            Console.WriteLine("Changed " + e.FullPath);
        }
Exemple #2
0
        public void Add <T>(T t) where T : IDbEntity
        {
            AddType <T>();

            Type type = typeof(T);

            int id = ++IndexTracker[type];

            t.EntityId = id;
            Database[type].Add(t);

            ApplyChanges();

            OnDatabaseChanged?.Invoke(this);

            OnEntityAdded?.Invoke(t);
        }
Exemple #3
0
        /// <summary>
        /// Clears the current database handler. This causes the all internal properties to be null and the workspace to be deleted.
        /// </summary>
        public void ClearHandler()
        {
            StopMonitoringDirectory();

            //Checks if the current workspace is still exists
            if (Directory.Exists(CurrentWorkspace))
            {
                //Fixes GitHub issue #4
                foreach (var database in CurrentDatabases)
                {
                    //Will delete all files matching a database class name
                    var filesWithDBName = Directory.GetFiles(CurrentWorkspace, database.Name + ".*", SearchOption.TopDirectoryOnly);
                    if (filesWithDBName.Any())
                    {
                        //Deletes XML files associated with the current database
                        filesWithDBName.ForEach(f => File.Delete(f));
                    }
                }
            }

            //Clears internal data
            CurrentWorkspace = null;
            CurrentDatabases = null;
            paths            = null;
            lastFileChanged  = null;

            //Disengage events
            if (!OnDatabaseChanged.IsNull())
            {
                OnDatabaseChanged = null;
            }
            if (!OnDatabaseExported.IsNull())
            {
                OnDatabaseExported = null;
            }
            if (!OnDatabaseImported.IsNull())
            {
                OnDatabaseImported = null;
            }
        }
Exemple #4
0
        /// <summary>
        /// Saves a selection of items in its respective database.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="items"></param>
        public void Save <T>(ICollection <T> items) where T : ICollectableObject
        {
            if (items.IsNull())
            {
                throw new ArgumentNullException("The parameter items cannot be null.");
            }

            DatabaseItemInfo <T> dbInfo = GetDatabaseItemInfo <T>();

            var db = GetType().GetMethod("GetDatabase", BindingFlags.NonPublic | BindingFlags.Instance).MakeGenericMethod(dbInfo.DatabaseType).Invoke(this, null);

            Type   typeOfCollection = db.GetType().GetProperty(dbInfo.CollectionName).PropertyType;
            object newItems         = EnumerateCollection <T>(items);

            if (typeOfCollection.BaseType == typeof(Array))
            {
                newItems = (newItems as ICollection <T>).ToArray();
            }
            else if (typeOfCollection.BaseType == typeof(Collection <T>))
            {
                newItems = (newItems as ICollection <T>).ToObservableCollection();
            }

            db.GetType().GetProperty(dbInfo.CollectionName).SetValue(db, newItems);

            string path = GetDatabasePath(dbInfo.DatabaseType);

            lock (lockObject)
            {
                var xml = db.Serialize();

                try
                {
                    try
                    {
                        if (!mutex.IsNull())
                        {
                            mutex.WaitOne();
                        }

                        xml.Save(path);

                        //Will fire the database changed event when the FileSystemWatcher is unavailable.
                        //This should happen only when using Xamarin.Forms as there is no native support for that class.
                        if (fileSystemWatcher.IsNull())
                        {
                            OnDatabaseChanged?.Invoke(this, new OnDatabaseChangedEventArgs(dbInfo.DatabaseType.Name, DateTime.Now));
                        }
                    }
                    catch
                    {
                        throw;
                    }
                    finally
                    {
                        if (!mutex.IsNull())
                        {
                            mutex.ReleaseMutex();
                        }
                    }
                }
                catch
                {
                    throw;
                }
                finally
                {
                }
            }
        }
Exemple #5
0
        /// <summary>
        /// Remove entity from Db
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        public void Remove <T>(T t) where T : IDbEntity
        {
            AddType <T>();
            Type type = typeof(T);

            Database[type].Remove(t);

            // check entity references
            foreach (KeyValuePair <Type, HashSet <IDbEntity> > kv in Database)
            {
                Type entityType = kv.Key;

                EntityInfo infos = GetEntityInfo(entityType);

                foreach (IDbEntity entity in kv.Value)
                {
                    bool changed = false;

                    // single refs
                    foreach (SingleReferenceProperty referenceProperty in infos.SingleReferenceInjectors)
                    {
                        IDbReference dbReference = (IDbReference)referenceProperty.PropertyAccessor.GetValue(entity);

                        if (dbReference.ReferenceId == t.EntityId && dbReference.EntityType == type)
                        {
                            dbReference.ReferenceId = null;
                            dbReference.Entity      = null;

                            changed = true;
                        }
                    }

                    // multiple refs
                    foreach (MultipleReferenceProperty referenceProperty in infos.MultipleReferenceInjectors)
                    {
                        IList dbReference = (IList)referenceProperty.PropertyAccessor.GetValue(entity);

                        if (dbReference == null)
                        {
                            continue;
                        }


                        for (int i = dbReference.Count - 1; i >= 0; i--)
                        {
                            IDbReference reference = (IDbReference)dbReference[i];

                            if (reference.ReferenceId == t.EntityId && reference.EntityType == type)
                            {
                                dbReference.RemoveAt(i);
                                changed = true;
                            }
                        }
                    }

                    if (changed)
                    {
                        OnEntityChanged?.Invoke(entity);
                    }
                }
            }

            OnDatabaseChanged?.Invoke(this);

            OnEntityRemoved?.Invoke(t);
        }