Java tutorial
/* * Copyright (c) 2014 Nils Andreas Svee. * This file is part of ACacher. * * ACacher is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * ACacher is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with ACacher. If not, see <http://www.gnu.org/licenses/>. */ import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import java.util.Set; class Storage { private static SessionFactory factory; /** * Adds a new anime to the database. * * @param name * @param summary * @param imageurl * @return Returns the ID of the new entry (in this case the MD5 of the * name) or null if an exception is thrown */ public static Anime addAnime(String name, String summary, String imageurl, Genre... genres) { return addAnime(name, summary, imageurl, SetUtils.fromArray(genres)); } public static Anime addAnime(String name, String summary, String imageurl, Set<Genre> genres) { Session session = factory.openSession(); Transaction tx = null; Anime anime = new Anime(); anime.setGenres(genres); anime.setImageurl(imageurl); anime.setName(name); anime.setSummary(summary); try { tx = session.beginTransaction(); session.save(anime); tx.commit(); } catch (HibernateException e) { if (tx != null) { tx.rollback(); } e.printStackTrace(); return null; } finally { session.close(); } return anime; } public static Episode addEpisode(String number, String title, Anime anime) { Session session = factory.openSession(); Transaction tx = null; Episode episode = new Episode(); episode.setAnime(anime); episode.setNumber(number); episode.setTitle(title); try { tx = session.beginTransaction(); session.save(episode); tx.commit(); } catch (HibernateException e) { if (tx != null) { tx.rollback(); } e.printStackTrace(); return null; } finally { session.close(); } return episode; } public static Genre addGenre(String name) { Session session = factory.openSession(); Transaction tx = null; Genre genre = new Genre(); genre.setName(name); try { tx = session.beginTransaction(); genre.setId((Integer) session.save(genre)); tx.commit(); } catch (HibernateException e) { if (tx != null) { tx.rollback(); } e.printStackTrace(); return null; } finally { session.close(); } return genre; } public static Source addSource(boolean direct, boolean dubbed, String stream, String url, Episode episode) { Session session = factory.openSession(); Transaction tx = null; Source source = new Source(); source.setDirect(direct); source.setDubbed(dubbed); source.setEpisode(episode); source.setStream(stream); source.setUrl(url); try { tx = session.beginTransaction(); session.save(source); tx.commit(); } catch (HibernateException e) { if (tx != null) { tx.rollback(); } e.printStackTrace(); return null; } finally { session.close(); } return source; } public static Anime findAnime(String name) { Session session = factory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); Anime anime = (Anime) session.createQuery("FROM Anime WHERE name = '" + name + "'").uniqueResult(); tx.commit(); return anime; } catch (HibernateException e) { if (tx != null) tx.rollback(); e.printStackTrace(); return null; } finally { session.close(); } } public static Episode findEpisode(int id) { Session session = factory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); Episode episode = (Episode) session.byId(Episode.class).load(id); tx.commit(); return episode; } catch (HibernateException e) { if (tx != null) tx.rollback(); e.printStackTrace(); return null; } finally { session.close(); } } public static Episode findEpisode(String number, Anime anime) { Session session = factory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); Episode episode = (Episode) session .createQuery( "FROM Episode WHERE anime_id = '" + anime.getId() + "' AND number = '" + number + "'") .uniqueResult(); tx.commit(); return episode; } catch (HibernateException e) { if (tx != null) tx.rollback(); e.printStackTrace(); return null; } finally { session.close(); } } public static Genre findGenre(String name) { Session session = factory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); Genre genre = (Genre) session.createQuery("FROM Genre WHERE name = '" + name + "'").uniqueResult(); tx.commit(); return genre; } catch (HibernateException e) { if (tx != null) tx.rollback(); return null; } finally { session.close(); } } public static Source findSource(String url) { Session session = factory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); Source source = (Source) session.createQuery("FROM Source WHERE url = '" + url + "'").uniqueResult(); tx.commit(); return source; } catch (HibernateException e) { if (tx != null) tx.rollback(); e.printStackTrace(); return null; } finally { session.close(); } } /** * Creates a new SessionFactory instance */ public static void initialize() { if (factory == null) { try { Configuration configuration = new Configuration(); configuration.configure(); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()).build(); factory = configuration.buildSessionFactory(serviceRegistry); } catch (Throwable ex) { System.err.println("Failed to create sessionFactory object." + ex); throw new ExceptionInInitializerError(ex); } } } }