public static void initSessionFactory() throws FileNotFoundException, IOException, HibernateException, SQLException { types.put(POSTGRESQL, LocaleManager.getInstance().getString("db_postgresql")); types.put(MYSQL, LocaleManager.getInstance().getString("db_mysql")); types.put(HSQLDB, LocaleManager.getInstance().getString("db_hsqldb")); String dbName = HibernateUtil.getDefaultDatabase(); try { HibernateUtil.selectDatabase(dbName); } catch (FileNotFoundException e) { Database newDatabase = new Database(); newDatabase.setName("database"); newDatabase.setDefaultDB(true); newDatabase.setDialect(HSQLDB); newDatabase.setDriver(getDriverClass(HSQLDB)); newDatabase.setUsername("sa"); newDatabase.setPassword(""); newDatabase.setUrl( "jdbc:hsqldb:file:" + PropertyManager.getInstance().getPathProperty("path_resources") + PropertyManager.sep + "database"); newDatabase.saveToFile( new File( PropertyManager.getInstance().getPathProperty("path_database") + PropertyManager.sep + "database.properties")); File coverDir = new File( PropertyManager.getInstance().getPathProperty("path_cover") + PropertyManager.sep + "database"); if (!coverDir.mkdir()) { MessagePaneManager.showCheckErrorPane( LocaleManager.getInstance().getString("error_cover_dir")); } HibernateUtil.selectDatabase("database"); } HibernateUtil.loadSessionFactory(selectedDB); DatabaseUpgrader du = DatabaseUpgrader.getInstance(); try { if (!du.checkDatabase(selectedDB.getDriver(), selectedDB.getHibernateDialect())) du.upgradeDatabase(); } catch (SQLException e) { MessagePaneManager.showExceptionPane(e, true); e.printStackTrace(); } }
public static boolean changeDatabase(String DBname) { File dir = new File(PropertyManager.getInstance().getPathProperty("path_database")); Database rollbackDB = selectedDB; try { selectedDB.setDefaultDB(false); selectedDB.saveToFile(new File(dir.getAbsolutePath() + selectedDB.getName() + ".properties")); HibernateUtil.shutdown(); HibernateUtil.selectDatabase(DBname); selectedDB.setDefaultDB(true); selectedDB.saveToFile(new File(dir.getAbsolutePath() + selectedDB.getName() + ".properties")); HibernateUtil.initSessionFactory(); AlbumPane.getInstance().refresh(); BottomBarPane.getInstance().refreshStats(); } catch (Exception ex) { MessagePaneManager.showExceptionPane(ex, true); try { selectedDB.setDefaultDB(false); selectedDB.saveToFile( new File(dir.getAbsolutePath() + selectedDB.getName() + ".properties")); HibernateUtil.shutdown(); HibernateUtil.selectDatabase(rollbackDB.getName()); selectedDB.setDefaultDB(true); selectedDB.saveToFile( new File(dir.getAbsolutePath() + selectedDB.getName() + ".properties")); } catch (Exception e) { logger.error("Error during the rollback", e); } return false; } return true; }
public static ImageIcon getImageFromURL(String url) { String[] splitName = url.split("/"); String name = "unknown"; try { name = URLDecoder.decode( splitName[splitName.length - 1], Charset.defaultCharset().displayName()); } catch (UnsupportedEncodingException uee) { uee.printStackTrace(); logger.error("Error when decoding image URL", uee); } if (name.indexOf('?') != -1) { splitName = name.split("\\?"); name = splitName[0]; } // We get the extension for ImageIO splitName = url.split("\\."); String typeExt = splitName[splitName.length - 1]; File tmpDir = new File(tmpImagePath); // We check if we have already the file in order to not download it twice for (File f : tmpDir.listFiles()) { if (f.getName().equals(name)) { ImageIcon ii = new ImageIcon(tmpImagePath + PropertyManager.sep + name); ii.setDescription(tmpImagePath + PropertyManager.sep + name); return ii; } } BufferedImage image = null; try { image = ImageIO.read(new URL(url)); File fileImage = new File(tmpDir + PropertyManager.sep + name); ImageIO.write(image, typeExt, fileImage); logger.debug("Picture downloaded : " + fileImage.getName()); } catch (MalformedURLException mue) { mue.printStackTrace(); } catch (FileNotFoundException fnfe) { logger.debug("404 Error : " + url); } catch (IOException ioe) { ioe.printStackTrace(); logger.error("Error when fetching : " + url, ioe); MessagePaneManager.showCheckErrorPane( LocaleManager.getInstance().getString("connection_error")); } if (image != null) { ImageIcon ii = new ImageIcon(image); ii.setDescription(tmpImagePath + PropertyManager.sep + name); return ii; } return new ImageIcon(); }
public static void removeDatabase(String DBname) { File prop = new File( PropertyManager.getInstance().getPathProperty("path_database") + PropertyManager.sep + DBname + ".properties"); if (!prop.delete()) return; boolean success = true; prop = new File( PropertyManager.getInstance().getPathProperty("resources") + PropertyManager.sep + DBname + ".properties"); if (!prop.delete()) success &= false; File coverDir = new File( PropertyManager.getInstance().getPathProperty("path_cover") + PropertyManager.sep + DBname); File[] covers = coverDir.listFiles(); for (File f : covers) { if (!f.delete()) success &= false; } if (!coverDir.delete()) success &= false; if (!success) { MessagePaneManager.showInfoPane( LocaleManager.getInstance().getString("error_database_delete")); } else { MessagePaneManager.showInfoPane( LocaleManager.getInstance().getString("success_database_delete")); } }
public static void createDatabase(DatabaseGuiObject dgo) throws FileNotFoundException, IOException { Database db = new Database(); db.setName(dgo.getName()); db.setDefaultDB(dgo.isDefaultDB()); db.setDialect(dgo.getDialect()); if (dgo.getDialect().equals(HSQLDB)) { db.setUrl( "jdbc:hsqldb:file:" + PropertyManager.getInstance().getPathProperty("path_resources") + PropertyManager.sep + dgo.getName()); db.setPassword(""); db.setUsername("sa"); } else { db.setUrl(dgo.getUrl()); db.setPassword(dgo.getPassword()); db.setUsername(dgo.getUsername()); } db.setDriver(HibernateUtil.getDriverClass(dgo.getDialect())); db.saveToFile( new File( PropertyManager.getInstance().getPathProperty("path_database") + PropertyManager.sep + dgo.getName() + ".properties")); if (db.isDefaultDB()) { HibernateUtil.setDatabaseToDefault(db, false); } File coverDir = new File( PropertyManager.getInstance().getPathProperty("path_cover") + PropertyManager.sep + dgo.getName()); if (!coverDir.mkdir()) { MessagePaneManager.showCheckErrorPane( LocaleManager.getInstance().getString("error_cover_dir")); } }