/// <summary> /// It is used to check whether database exists or not. /// </summary> protected static void DoesDatabaseExists() { ApplicationDescriptor applicationDescriptor = coreResourceManager.GetApplicationDescriptor(); IEnumerator <DatabaseDescriptor> databaseDescriptors = applicationDescriptor.GetDatabaseDescriptors(); bool databaseExists = true; while (databaseDescriptors.MoveNext()) { DatabaseDescriptor databaseDescriptor = databaseDescriptors.Current; String databasePath = new DatabaseUtils().GetDatabasePath(databaseDescriptor); String databaseName = databaseDescriptor.GetDatabaseName(); if (!databaseName.EndsWith(".db")) { databaseName = databaseName + ".db"; } bool fileExists = FileUtils.DoesFileExists(databasePath, databaseName, FileUtils.LOCAL_FOLDER); if (!fileExists) { databaseExists = false; } } if (!databaseExists) { firstTimeProcessed = true; } else { firstTimeProcessed = false; } }
/// <summary> /// Parse the entity descriptor descriptor defined /// </summary> /// <exception cref="Siminov.Core.Exception.SiminovException">Any exception during parsing the descriptor file</exception> public void Process() { ApplicationDescriptor applicationDescriptor = resourceManager.GetApplicationDescriptor(); if (applicationDescriptor == null) { Log.Log.Error(typeof(QuickEntityDescriptorReader).FullName, "Process", "Invalid Application Descriptor Found"); throw new DeploymentException(typeof(QuickEntityDescriptorReader).FullName, "Process", "Invalid Application Descriptor Found."); } if (!applicationDescriptor.IsDatabaseNeeded()) { doesMatch = false; return; } IEnumerator <DatabaseDescriptor> databaseDescriptors = applicationDescriptor.GetDatabaseDescriptors(); while (databaseDescriptors.MoveNext()) { DatabaseDescriptor databaseDescriptor = databaseDescriptors.Current; IEnumerator <String> entityDescriptors = databaseDescriptor.GetEntityDescriptorPaths(); while (entityDescriptors.MoveNext()) { String entityDescriptorPath = entityDescriptors.Current; Stream entityDescriptorStream = null; try { ParseMessage(entityDescriptorStream); } catch (SiminovException exception) { Log.Log.Error(typeof(QuickEntityDescriptorReader).FullName, "Process", "Exception caught while parsing ENTITY-DESCRIPTOR: " + entityDescriptorPath + ", " + exception.GetMessage()); throw new SiminovException(typeof(QuickEntityDescriptorReader).FullName, "Process", "Exception caught while parsing ENTITY-DESCRIPTOR: " + entityDescriptorPath + ", " + exception.GetMessage()); } if (doesMatch) { EntityDescriptorReader entityDescriptorParser = new EntityDescriptorReader(entityDescriptorPath); this.entityDescriptor = entityDescriptorParser.GetEntityDescriptor(); databaseDescriptor.AddEntityDescriptor(entityDescriptorPath, entityDescriptor); return; } } } }
/// <summary> /// It process all LibraryDescriptor.xml files defined by application, and stores in Resource Manager. /// </summary> protected static void ProcessLibraries() { ApplicationDescriptor applicationDescriptor = coreResourceManager.GetApplicationDescriptor(); IEnumerator <String> libraries = applicationDescriptor.GetLibraryDescriptorPaths(); while (libraries.MoveNext()) { String library = libraries.Current; /* * Parse LibraryDescriptor. */ LibraryDescriptorReader libraryDescriptorReader = new LibraryDescriptorReader(library); LibraryDescriptor libraryDescriptor = libraryDescriptorReader.GetLibraryDescriptor(); /* * Map Entity Descriptors */ IEnumerator <String> entityDescriptors = libraryDescriptor.GetEntityDescriptorPaths(); while (entityDescriptors.MoveNext()) { String libraryEntityDescriptorPath = entityDescriptors.Current; String databaseDescriptorName = libraryEntityDescriptorPath.Substring(0, libraryEntityDescriptorPath.IndexOf(Constants.LIBRARY_DESCRIPTOR_ENTITY_DESCRIPTOR_SEPRATOR)); String entityDescriptor = libraryEntityDescriptorPath.Substring(libraryEntityDescriptorPath.IndexOf(Constants.LIBRARY_DESCRIPTOR_ENTITY_DESCRIPTOR_SEPRATOR) + 1, ((libraryEntityDescriptorPath.Length - libraryEntityDescriptorPath.IndexOf(Constants.LIBRARY_DESCRIPTOR_ENTITY_DESCRIPTOR_SEPRATOR)) - 1)); IEnumerator <DatabaseDescriptor> databaseDescriptors = applicationDescriptor.GetDatabaseDescriptors(); while (databaseDescriptors.MoveNext()) { DatabaseDescriptor databaseDescriptor = databaseDescriptors.Current; if (databaseDescriptor.GetDatabaseName().Equals(databaseDescriptorName, StringComparison.OrdinalIgnoreCase)) { databaseDescriptor.AddEntityDescriptorPath(library.Replace(".", "/") + FileUtils.Separator + entityDescriptor); } } } } }
/// <summary> /// It process all EntityDescriptor.xml file defined in Application, and stores in Resource Manager. /// </summary> protected static void ProcessEntityDescriptors() { DoesDatabaseExists(); ApplicationDescriptor applicationDescriptor = coreResourceManager.GetApplicationDescriptor(); IEnumerator <DatabaseDescriptor> databaseDescriptors = applicationDescriptor.GetDatabaseDescriptors(); while (databaseDescriptors.MoveNext()) { DatabaseDescriptor databaseDescriptor = databaseDescriptors.Current; IEnumerator <String> entityDescriptorPaths = databaseDescriptor.GetEntityDescriptorPaths(); while (entityDescriptorPaths.MoveNext()) { String entityDescriptorPath = entityDescriptorPaths.Current; EntityDescriptorReader entityDescriptorParser = new EntityDescriptorReader(entityDescriptorPath); databaseDescriptor.AddEntityDescriptor(entityDescriptorPath, entityDescriptorParser.GetEntityDescriptor()); } } }
/// <summary> /// It process all DatabaseDescriptor.xml and initialize Database and stores in Resource Manager. /// </summary> protected static void ProcessDatabase() { ApplicationDescriptor applicationDescriptor = coreResourceManager.GetApplicationDescriptor(); IEnumerator <DatabaseDescriptor> databaseDescriptors = applicationDescriptor.GetDatabaseDescriptors(); while (databaseDescriptors.MoveNext()) { DatabaseDescriptor databaseDescriptor = databaseDescriptors.Current; String databasePath = new DatabaseUtils().GetDatabasePath(databaseDescriptor); DatabaseBundle databaseBundle = null; try { databaseBundle = DatabaseHelper.CreateDatabase(databaseDescriptor); } catch (DatabaseException databaseException) { Log.Log.Error(typeof(Siminov).FullName, "ProcessDatabase", "DatabaseException caught while getting database instance from database factory, DATABASE-DESCRIPTOR: " + databaseDescriptor.GetDatabaseName() + ", " + databaseException.GetMessage()); throw new DeploymentException(typeof(Siminov).FullName, "ProcessDatabase", databaseException.GetMessage()); } IDatabaseImpl database = databaseBundle.GetDatabase(); IQueryBuilder queryBuilder = databaseBundle.GetQueryBuilder(); /* * If Database exists then open and return. * If Database does not exists create the database. */ String databaseName = databaseDescriptor.GetDatabaseName(); if (!databaseName.EndsWith(".db")) { databaseName = databaseName + ".db"; } bool fileExists = FileUtils.DoesFileExists(databasePath, databaseName, FileUtils.LOCAL_FOLDER); if (fileExists) { /* * Open Database */ try { database.OpenOrCreate(databaseDescriptor); } catch (DatabaseException databaseException) { Log.Log.Error(typeof(Siminov).FullName, "ProcessDatabase", "DatabaseException caught while opening database, " + databaseException.GetMessage()); throw new DeploymentException(typeof(Siminov).FullName, "ProcessDatabase", databaseException.GetMessage()); } /* * Enable Foreign Key Constraints */ try { database.ExecuteQuery(databaseDescriptor, null, Constants.SQLITE_DATABASE_QUERY_TO_ENABLE_FOREIGN_KEYS_MAPPING); } catch (DatabaseException databaseException) { FileUtils.DeleteFile(databasePath, databaseDescriptor.GetDatabaseName(), FileUtils.LOCAL_FOLDER); Log.Log.Error(typeof(Siminov).FullName, "ProcessDatabase", "DatabaseException caught while executing query to enable foreign keys, " + databaseException.GetMessage()); throw new DeploymentException(typeof(Siminov).FullName, "ProcessDatabase", databaseException.GetMessage()); } /* * Safe MultiThread Transaction */ /*try * { * database.ExecuteMethod(Constants.SQLITE_DATABASE_ENABLE_LOCKING, databaseDescriptor.IsTransactionSafe()); * } * catch (DatabaseException databaseException) * { * FileUtils.DoesFileExists(databasePath, databaseDescriptor.GetDatabaseName(), FileUtils.LOCAL_FOLDER); * * Log.Log.Error(typeof(Siminov).FullName, "ProcessDatabase", "DatabaseException caught while enabling locking on database, " + databaseException.GetMessage()); * throw new DeploymentException(typeof(Siminov).FullName, "ProcessDatabase", databaseException.GetMessage()); * }*/ /* * Upgrade Database */ try { DatabaseHelper.UpgradeDatabase(databaseDescriptor); } catch (DatabaseException databaseException) { Log.Log.Error(typeof(Siminov).FullName, "ProcessDatabase", "DatabaseException caught while upgrading database, " + databaseException.GetMessage()); throw new DeploymentException(typeof(Siminov).FullName, "ProcessDatabase", databaseException.GetMessage()); } } else { /* * Create Database Directory */ try { FileUtils.GetFolder(databasePath, FileUtils.LOCAL_FOLDER); } catch (System.Exception exception) { Log.Log.Error(typeof(Siminov).FullName, "ProcessDatabase", "Exception caught while creating database directories, DATABASE-PATH: " + databasePath + ", DATABASE-DESCRIPTOR: " + databaseDescriptor.GetDatabaseName() + ", " + exception.Message); throw new DeploymentException(typeof(Siminov).FullName, "ProcessDatabase", exception.Message); } /* * Create Database File. */ try { database.OpenOrCreate(databaseDescriptor); } catch (DatabaseException databaseException) { FileUtils.DeleteFile(databasePath, databaseDescriptor.GetDatabaseName(), FileUtils.LOCAL_FOLDER); Log.Log.Error(typeof(Siminov).FullName, "ProcessDatabase", "DatabaseException caught while creating database, " + databaseException.GetMessage()); throw new DeploymentException(typeof(Siminov).FullName, "ProcessDatabase", databaseException.GetMessage()); } /* * Set Database Version */ IDictionary <String, Object> parameters = new Dictionary <String, Object>(); parameters.Add(IQueryBuilder.FORM_UPDATE_DATABASE_VERSION_QUERY_DATABASE_VERSION_PARAMETER, databaseDescriptor.GetVersion()); try { String updateDatabaseVersionQuery = queryBuilder.FormUpdateDatabaseVersionQuery(parameters); database.ExecuteQuery(databaseDescriptor, null, updateDatabaseVersionQuery); } catch (DatabaseException databaseException) { Log.Log.Error(typeof(Siminov).FullName, "ProcessDatabase", "Database Exception caught while updating database version, " + databaseException.GetMessage()); throw new DeploymentException(typeof(Siminov).FullName, "ProcessDatabase", databaseException.GetMessage()); } IDatabaseEvents databaseEventHandler = coreResourceManager.GetDatabaseEventHandler(); if (databaseEventHandler != null) { databaseEventHandler.OnDatabaseCreated(databaseDescriptor); } /* * Enable Foreign Key Constraints */ try { database.ExecuteQuery(databaseDescriptor, null, Constants.SQLITE_DATABASE_QUERY_TO_ENABLE_FOREIGN_KEYS_MAPPING); } catch (DatabaseException databaseException) { FileUtils.DeleteFile(databasePath, databaseDescriptor.GetDatabaseName(), FileUtils.LOCAL_FOLDER); Log.Log.Error(typeof(Siminov).FullName, "ProcessDatabase", "DatabaseException caught while executing query to enable foreign keys, " + databaseException.GetMessage()); throw new DeploymentException(typeof(Siminov).FullName, "ProcessDatabase", databaseException.GetMessage()); } /* * Safe MultiThread Transaction */ /*try * { * database.ExecuteMethod(Constants.SQLITE_DATABASE_ENABLE_LOCKING, databaseDescriptor.IsTransactionSafe()); * } * catch (DatabaseException databaseException) * { * FileUtils.DeleteFile(databasePath, databaseDescriptor.GetDatabaseName(), FileUtils.LOCAL_FOLDER); * * Log.Log.Error(typeof(Siminov).FullName, "ProcessDatabase", "DatabaseException caught while enabling locking on database, " + databaseException.GetMessage()); * throw new DeploymentException(typeof(Siminov).FullName, "ProcessDatabase", databaseException.GetMessage()); * }*/ /* * Create Tables */ try { DatabaseHelper.CreateTables(databaseDescriptor.OrderedEntityDescriptors()); } catch (DatabaseException databaseException) { FileUtils.DeleteFile(databasePath, databaseDescriptor.GetDatabaseName(), FileUtils.LOCAL_FOLDER); Log.Log.Error(typeof(Siminov).FullName, "ProcessDatabase", "DatabaseException caught while creating tables, " + databaseException.GetMessage()); throw new DeploymentException(typeof(Siminov).FullName, "ProcessDatabase", databaseException.GetMessage()); } } } }