/** * This method initialize the database * * @throws CantInitializeNetworkServiceDatabaseException */ private void initializeDataBase() throws CantInitializeNetworkServiceDatabaseException { try { /* * Open new database connection */ this.abstractCommunicationNetworkServiceDatabase = this.pluginDatabaseSystem.openDatabase( pluginId, CommunicationNetworkServiceDatabaseConstants.DATA_BASE_NAME); } catch (CantOpenDatabaseException cantOpenDatabaseException) { /* * The database exists but cannot be open. I can not handle this situation. */ errorManager.reportUnexpectedPluginException( this.getPluginVersionReference(), UnexpectedPluginExceptionSeverity.DISABLES_THIS_PLUGIN, cantOpenDatabaseException); throw new CantInitializeNetworkServiceDatabaseException(cantOpenDatabaseException); } catch (DatabaseNotFoundException e) { /* * The database no exist may be the first time the plugin is running on this device, * We need to create the new database */ CommunicationNetworkServiceDatabaseFactory communicationNetworkServiceDatabaseFactory = new CommunicationNetworkServiceDatabaseFactory(pluginDatabaseSystem); try { /* * We create the new database */ this.abstractCommunicationNetworkServiceDatabase = communicationNetworkServiceDatabaseFactory.createDatabase( pluginId, CommunicationNetworkServiceDatabaseConstants.DATA_BASE_NAME); } catch (CantCreateDatabaseException cantOpenDatabaseException) { /* * The database cannot be created. I can not handle this situation. */ errorManager.reportUnexpectedPluginException( this.getPluginVersionReference(), UnexpectedPluginExceptionSeverity.DISABLES_SOME_FUNCTIONALITY_WITHIN_THIS_PLUGIN, cantOpenDatabaseException); throw new CantInitializeNetworkServiceDatabaseException(cantOpenDatabaseException); } } }
/** * This method open or creates the database i'll be working with * * @throws CantInitializeDatabaseException */ public void initializeDatabase(final String tableId) throws CantInitializeDatabaseException { switch (tableId) { case FanActorNetworkServiceDatabaseConstants.FAN_ACTOR_NETWORK_SERVICE_DATABASE_NAME: try { database = this.pluginDatabaseSystem.openDatabase(pluginId, tableId); } catch (final CantOpenDatabaseException e) { throw new CantInitializeDatabaseException( e, "tableId: " + tableId, "Error trying to open the database."); } catch (final DatabaseNotFoundException e) { final FanActorNetworkServiceDatabaseFactory fanActorNetworkServiceDatabaseFactory = new FanActorNetworkServiceDatabaseFactory(pluginDatabaseSystem); try { database = fanActorNetworkServiceDatabaseFactory.createDatabase(pluginId, tableId); } catch (final CantCreateDatabaseException z) { throw new CantInitializeDatabaseException( z, "tableId: " + tableId, "Error trying to create the database."); } } break; case CommunicationNetworkServiceDatabaseConstants.DATA_BASE_NAME: try { this.database = this.pluginDatabaseSystem.openDatabase( pluginId, CommunicationNetworkServiceDatabaseConstants.DATA_BASE_NAME); } catch (CantOpenDatabaseException e) { throw new CantInitializeDatabaseException( e, "tableId: " + tableId, "Error trying to open the database."); } catch (DatabaseNotFoundException e) { CommunicationNetworkServiceDatabaseFactory communicationLayerNetworkServiceDatabaseFactory = new CommunicationNetworkServiceDatabaseFactory(pluginDatabaseSystem); try { this.database = communicationLayerNetworkServiceDatabaseFactory.createDatabase( pluginId, CommunicationNetworkServiceDatabaseConstants.DATA_BASE_NAME); } catch (CantCreateDatabaseException z) { throw new CantInitializeDatabaseException( z, "tableId: " + tableId, "Error trying to create the database."); } } } }