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.List; import olym.HibernateUtil; import olym.Korisnik; import olym.Spordisc; import olym.Sport; import olym.Sportista; import olym.Zemlja; 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 OlymHelper { public static boolean checkVodjaExists(Zemlja zemlja) {//sve dohvata hib Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tr = null; boolean exists = false; Korisnik vodja = null; try { tr = session.beginTransaction(); Criteria criteria = (Criteria) session.createCriteria(Korisnik.class); criteria.add(Restrictions.eq("zemlja", zemlja)); criteria.add(Restrictions.eq("tip", 2)); vodja = (Korisnik) criteria.uniqueResult(); tr.commit(); if (vodja != null) { exists = true; } } catch (HibernateException e) { if (tr != null) { tr.rollback(); } } finally { session.close(); } return exists; } public static Korisnik getUserByName(String username) {//ne dohvata zemlju|| dohvata nakon setfetch stavljanja Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tr = null; Korisnik user = null; try { tr = session.beginTransaction(); // Query q = session.createQuery("from Korisnik where username='" + username + "'"); Criteria criteria = (Criteria) session.createCriteria(Korisnik.class).setFetchMode("zemlja", FetchMode.JOIN); criteria.add(Restrictions.eq("username", username)); user = (Korisnik) criteria.uniqueResult(); tr.commit(); } catch (HibernateException e) { if (tr != null) { tr.rollback(); } } finally { session.close(); } return user; } public static Sport getSportByName(String sport) {//hib sve dohvata Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tr = null; Sport sp = null; boolean good = false; try { tr = session.beginTransaction(); Query q = session.createQuery("from Sport where naziv='" + sport + "'"); sp = (Sport) q.uniqueResult(); tr.commit(); good = true; } catch (HibernateException e) { if (tr != null) { tr.rollback(); } } finally { session.close(); } return sp; } public static List<Spordisc> getDiscByName(String disc) {//nigde se ne koristi? Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tr = null; List<Spordisc> dc = null; try { tr = session.beginTransaction(); Criteria criteria = (Criteria) session.createCriteria(Spordisc.class); criteria.add(Restrictions.eq("disciplina", disc)); dc = criteria.list(); tr.commit(); } catch (HibernateException e) { if (tr != null) { tr.rollback(); } } finally { session.close(); } return dc; } public static boolean insertUser(Korisnik user) { Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tr = null; boolean res = false; try { tr = session.beginTransaction(); //Save the employee in database session.save(user); //Commit the transaction tr.commit(); res = true;//uspesno uradjeno } catch (HibernateException e) { if (tr != null) { tr.rollback(); } } finally { session.close(); } return res; } public static Zemlja getCountryByName(String country) {//sve hib dohvata Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tr = null; Zemlja zemlja = null; boolean good = false; try { tr = session.beginTransaction(); Query q = session.createQuery("from Zemlja where naziv='" + country + "'"); zemlja = (Zemlja) q.uniqueResult(); tr.commit(); good = true; } catch (HibernateException e) { if (tr != null) { tr.rollback(); } } finally { session.close(); } return zemlja; } public static List<Sport> getAllSports() {//hib sve dohvata Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tr = null; List<Sport> sports = null; try { tr = session.beginTransaction(); Criteria criteria = session.createCriteria(Sport.class) .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); ; criteria.addOrder(Order.asc("naziv")); sports = criteria.list(); tr.commit(); } catch (HibernateException e) { if (tr != null) { tr.rollback(); } } finally { session.close(); } return sports; } public static List<Spordisc> getAllDisciplines() {//nigde se ne koristi? Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tr = null; List<Spordisc> disc = null; try { tr = session.beginTransaction(); Criteria criteria = session.createCriteria(Spordisc.class) .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); criteria.add(Restrictions.ne("disciplina", "/")); criteria.addOrder(Order.asc("disciplina")); disc = criteria.list(); // Query q = session.createSQLQuery("select DISTINCT disciplina from spordisc where disciplina!='/' order by disciplina asc"); //disc = q.list();//zbog ovog distinct i uzimanja samo jedne kolone, mozes u index da koristis samo discItem tr.commit(); } catch (HibernateException e) { if (tr != null) { tr.rollback(); } } finally { session.close(); } return disc; } public static List<Zemlja> getAllCountries(boolean sort) {//hib sve dohvata Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tr = null; List<Zemlja> zemlje = null; try { tr = session.beginTransaction(); String askfor = "from Zemlja"; if (sort) { askfor += " order by total_m desc"; } Query q = session.createQuery(askfor); zemlje = q.list(); tr.commit(); } catch (HibernateException e) { if (tr != null) { tr.rollback(); } } finally { session.close(); } return zemlje; } //dohvata sve public static List<Sportista> searchSportists(String sportista, Zemlja zemlja, Sport sport, int disciplina, String gender, boolean medal) { Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tr = null; List<Sportista> sportists = null; try { tr = session.beginTransaction(); Criteria criteria = session.createCriteria(Sportista.class) .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); Criteria criteria2 = null; if (sportista != null) { if (!sportista.isEmpty()) { criteria.add(Restrictions.ilike("name", sportista, MatchMode.ANYWHERE)); } } if (zemlja != null) { criteria.add(Restrictions.eq("zemlja", zemlja)); } if (disciplina != 0) {//i sport i disciplina izabrani if (criteria2 == null) criteria2 = criteria.createCriteria("spordisc"); criteria2.add(Restrictions.eq("id", disciplina)); } if (sport != null && disciplina == 0) {//samo sport izabran if (criteria2 == null) criteria2 = criteria.createCriteria("spordisc"); criteria2.add(Restrictions.eq("sport", sport)); } //constraint nad spordisc tabelom da naziv discipline bude ogranicen na onaj dati string if (gender != null) { criteria.add(Restrictions.eq("gender", gender)); } if (medal) { criteria.add(Restrictions.gt("medals", 0)); } sportists = criteria.list(); tr.commit(); boolean good = true; } catch (HibernateException e) { if (tr != null) { tr.rollback(); } } finally { session.close(); } return sportists; } public static boolean changeUserData(Korisnik user) { Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tr = null; boolean res = false; try { tr = session.beginTransaction(); //Save the employee in database session.update(user); //Commit the transaction tr.commit(); res = true;//uspesno uradjeno } catch (HibernateException e) { if (tr != null) { tr.rollback(); } } finally { session.close(); } return res; } /* public static int getMedalCountBySportist(String name) { Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tr = null; BigDecimal bd=null; int medals = 0; try { tr = session.beginTransaction(); Query q = session.createSQLQuery("select sum(medals) from Sportista where name='" + name + "'"); bd=(BigDecimal)q.uniqueResult(); medals = Integer.valueOf(bd.intValue()); tr.commit(); } catch (HibernateException e) { if (tr != null) { tr.rollback(); } } finally { session.close(); } return medals; }*/ }