Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package helpers; import java.util.ArrayList; import java.util.List; import olym.HibernateUtil; import olym.Korisnik; import olym.Rekordi; import olym.Singltak; import olym.Spordisc; import olym.Sport; import olym.Sportista; import olym.Takmicenje; import olym.Team; import olym.Teamtak; import org.hibernate.Criteria; import org.hibernate.FetchMode; import org.hibernate.Hibernate; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.criterion.MatchMode; import org.hibernate.criterion.Order; import org.hibernate.criterion.Restrictions; /** * * @author Amaterasu */ public class OrgHelper { public static List<Spordisc> getSpordiscBySport(Sport sport) {//sport nije uhvatio, ostalo jeste Session session = HibernateUtil.getSessionFactory().openSession();//sa dodatkom fetchmode sve dohvatio Transaction tr = null; boolean good = false; List<Spordisc> sp = null; try { tr = session.beginTransaction(); Criteria criteria = (Criteria) session.createCriteria(Spordisc.class) .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).setFetchMode("sport", FetchMode.JOIN); criteria.add(Restrictions.eq("sport", sport)); sp = criteria.list(); tr.commit(); if (sp != null) { good = true; } } catch (HibernateException e) { if (tr != null) { tr.rollback(); } } finally { session.close(); } return sp; } public static void addContSing(List<String> chosen, Takmicenje tak) {//ne dohvata zemlju itd Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tr = null; boolean good = false; try { tr = session.beginTransaction(); for (String c : chosen) { Query q = session.createQuery("from Sportista where id=" + Integer.parseInt(c) + ""); Sportista sp = (Sportista) q.uniqueResult(); Singltak st = new Singltak(sp, tak); session.save(st); } tr.commit(); good = true; } catch (HibernateException e) { if (tr != null) { tr.rollback(); } } finally { session.close(); } } public static void addContTeams(List<String> chosen, Takmicenje tak) { Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tr = null; boolean good = false; try { tr = session.beginTransaction(); for (String c : chosen) { Query q = session.createQuery("from Team where id=" + Integer.parseInt(c) + ""); Team team = (Team) q.uniqueResult(); Teamtak tt = new Teamtak(tak, team); session.save(tt); } tr.commit(); good = true; } catch (HibernateException e) { if (tr != null) { tr.rollback(); } } finally { session.close(); } } public static Takmicenje addTak(Spordisc sd, String gender, String area, Korisnik delegat, java.sql.Date dateEnd, java.sql.Date dateStart, String lokacija) {//sve hib dohvata Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tr = null; Takmicenje tak = null; boolean good = false; try { tr = session.beginTransaction(); tak = new Takmicenje(delegat, sd, gender, dateStart, dateEnd, sd.getVrsta(), area, lokacija, false); session.save(tak); Criteria crit = session.createCriteria(Takmicenje.class) .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); crit.add(Restrictions.eq("gender", gender)); crit.add(Restrictions.eq("spordisc", sd)); tak = (Takmicenje) crit.uniqueResult(); tr.commit(); if (tak != null) { good = true; } } catch (HibernateException e) { session.delete(tak); if (tr != null) { tr.rollback(); } } finally { session.close(); } return tak; } public static boolean updateDelegates(String delegat) { Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tr = null; List<Korisnik> users = null; Korisnik user; boolean good = false; try { tr = session.beginTransaction(); Query q = session.createQuery("from Korisnik where username='" + delegat + "'"); user = (Korisnik) q.uniqueResult(); user.setNadgleda(user.getNadgleda() + 1); session.saveOrUpdate(user); tr.commit(); good = true; } catch (HibernateException e) { if (tr != null) { tr.rollback(); } } finally { session.close(); } return good; } public static List<Korisnik> getDelegates() {//sve hib dohvata Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tr = null; List<Korisnik> user = null; boolean res = false; try { tr = session.beginTransaction(); Criteria criteria = session.createCriteria(Korisnik.class).setFetchMode("zemlja", FetchMode.JOIN) .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); criteria.add(Restrictions.eq("tip", 1)); criteria.add(Restrictions.eq("odobren", true)); criteria.add(Restrictions.lt("nadgleda", 3)); /*Query q = session.createQuery("from Korisnik where tip=1 and odobren=1 and nadgleda<3"); user = q.list();*/ user = criteria.list(); tr.commit(); if (user != null) { res = true; } } catch (HibernateException e) { if (tr != null) { tr.rollback(); } } finally { session.close(); } return user; } public static boolean checkTak(Spordisc sd, String gender) {//valjda dohvata sve hib Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tr = null; boolean good = false; Takmicenje tak = null; try { tr = session.beginTransaction(); Criteria criteria = (Criteria) session.createCriteria(Takmicenje.class); criteria.add(Restrictions.eq("spordisc", sd)); criteria.add(Restrictions.eq("gender", gender)); tak = (Takmicenje) criteria.uniqueResult(); tr.commit(); if (tak != null) { good = true; } } catch (HibernateException e) { if (tr != null) { tr.rollback(); } } finally { session.close(); } return good; } public static List<Sportista> getSportistsBySporDiscGender(Spordisc sp, String gender) {//sve hib dohvata Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tr = null; List<Sportista> sps = null; try { tr = session.beginTransaction(); //Query q = session.createQuery("from Sportista where sport_discipline="+id+" and gender='"+gender+"'"); Criteria crit = (Criteria) session.createCriteria(Sportista.class) .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); crit.add(Restrictions.eq("gender", gender)); crit.add(Restrictions.eq("spordisc", sp)); sps = crit.list(); tr.commit(); } catch (HibernateException e) { if (tr != null) { tr.rollback(); } } finally { session.close(); } return sps; } public static List<Sportista> getTeamsBySporDiscGender(Spordisc sp, String gender) {//dohvata sve kod tog sportiste hib Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tr = null; List<Integer> idteams = null; List<Sportista> beans = new ArrayList<>(); List<Team> teams = new ArrayList<>(); boolean good = false; try { tr = session.beginTransaction(); int id = sp.getId(); Query q = session.createSQLQuery( "select distinct team_single from sportista where team_single is not null and sport_discipline=" + id + " and gender='" + gender + "'"); idteams = q.list(); for (int i = 0; i < idteams.size(); i++) {//svi npr muski timovi vaterpola(sve zemlje) Query q1 = session .createSQLQuery("select id from sportista where team_single='" + idteams.get(i) + "'"); List<Integer> ids = q1.list();//idevi sportista tog nekog jednog tima int z = ids.get(0);//id prvog sportiste //int idsp=Integer.parseInt(z); Criteria criteria = (Criteria) session.createCriteria(Sportista.class); criteria.add(Restrictions.eq("id", z)); Sportista dc = (Sportista) criteria.uniqueResult(); beans.add(dc);//dodajemo po jednog sportistu iz svakog tima da bi pristupili team_idju posle } if (idteams != null) { good = true; } tr.commit(); } catch (HibernateException e) { if (tr != null) { tr.rollback(); } } finally { session.close(); } return beans; } public static Spordisc getSporDiscById(int id) {//ne dohvata sport, hoce sa fetch mode Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tr = null; Spordisc dc = null; try { tr = session.beginTransaction(); Criteria criteria = (Criteria) session.createCriteria(Spordisc.class).setFetchMode("sport", FetchMode.JOIN); criteria.add(Restrictions.eq("id", id)); dc = (Spordisc) criteria.uniqueResult(); // Hibernate.initialize(dc.getSport()); tr.commit(); } catch (HibernateException e) { if (tr != null) { tr.rollback(); } } finally { session.close(); } return dc; } public static List<Korisnik> getUsersToApp() {//ne popunjava im zemlju|| hoce sa criterionom i fetchmodeom i transformerom Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tr = null; List<Korisnik> user = null; boolean good = false; try { tr = session.beginTransaction(); Criteria criteria = (Criteria) session.createCriteria(Korisnik.class) .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).setFetchMode("zemlja", FetchMode.JOIN); criteria.add(Restrictions.eq("odobren", false)); user = criteria.list(); // Query q = session.createQuery("from Korisnik where odobren=0"); // user = q.list(); tr.commit(); good = true; } catch (HibernateException e) { if (tr != null) { tr.rollback(); } } finally { session.close(); } return user; } public static boolean dropUser(String username) { Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tr = null; boolean good = false; try { tr = session.beginTransaction(); Query q = session.createSQLQuery("delete from Korisnik where username='" + username + "'"); q.executeUpdate(); tr.commit(); good = true; } catch (HibernateException e) { if (tr != null) { tr.rollback(); } } finally { session.close(); } return good; } public static List<Rekordi> getAllRecords(boolean pol) {//sve im vadi hib Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tr = null; List<Rekordi> records = null; try { String askfor; tr = session.beginTransaction(); if (pol) {//pol==true askfor = "female"; } else { askfor = "male"; } Criteria criteria = session.createCriteria(Rekordi.class) .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); criteria.add(Restrictions.eq("pol", askfor)); Criteria criteria2 = criteria.createCriteria("spordisc") .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); criteria2.addOrder(Order.asc("sport")); criteria2.addOrder(Order.asc("disciplina")); records = criteria.list(); /* for (Rekordi rec : records) { Hibernate.initialize(rec.getZemlja()); Hibernate.initialize(rec.getSpordisc()); }*/ tr.commit(); } catch (HibernateException e) { if (tr != null) { tr.rollback(); } } finally { session.close(); } return records; } public static boolean addSport(Sport sport) { Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tr = null; boolean res = false; try { tr = session.beginTransaction(); //Save the sport in database session.save(sport); //Commit the transaction tr.commit(); res = true;//uspesno uradjeno } catch (HibernateException e) { if (tr != null) { tr.rollback(); } } finally { session.close(); } return res; } public static boolean addSportDisc(Spordisc sd) { Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tr = null; boolean res = false; try { tr = session.beginTransaction(); //Save the sport in database session.save(sd); //Commit the transaction tr.commit(); res = true;//uspesno uradjeno } catch (HibernateException e) { if (tr != null) { tr.rollback(); } } finally { session.close(); } return res; } public static boolean checkSpordiscExists(String sport, String disc) {//sve popuni, dobro radi Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tr = null; boolean res = false; Spordisc sd = null; try { tr = session.beginTransaction(); Criteria criteria = session.createCriteria(Spordisc.class) .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); if (disc == null) { criteria.add(Restrictions.isNull("disciplina")); } else if (disc.isEmpty()) { criteria.add(Restrictions.isNull("disciplina")); } else { criteria.add(Restrictions.ilike("disciplina", disc, MatchMode.EXACT)); } criteria.createCriteria("sport").add(Restrictions.ilike("naziv", sport)); sd = (Spordisc) criteria.uniqueResult(); tr.commit(); if (sd != null) { res = true; } } catch (HibernateException e) { if (tr != null) { tr.rollback(); } } finally { session.close(); } return res; } }