/src/java/com/bpp/hibernate/BudgetTimetableActivitiesHibernateHelper.java
https://bitbucket.org/SayomiOla/bpp · Java · 272 lines · 196 code · 58 blank · 18 comment · 28 complexity · e8984a45662d095267b2ae2eac6a0874 MD5 · raw file
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package com.bpp.hibernate;
- import com.bpp.utility.Utility;
- import com.google.gson.Gson;
- import java.util.HashMap;
- import java.util.List;
- import org.hibernate.Criteria;
- import org.hibernate.Query;
- import org.hibernate.Session;
- import org.hibernate.HibernateException;
- import org.hibernate.SQLQuery;
- import org.hibernate.Transaction;
- /**
- * Budget Timetable Activities Hibernate Helper class
- *
- * @author Lekan
- * @since 17/6/2017
- */
- public class BudgetTimetableActivitiesHibernateHelper {
-
- public static final String TABLE_NAME = "BudgetTimetableActivities";
- public static final String RAW_TABLE_NAME = "Budget_Timetable_Activities";
- public synchronized String insert(BudgetTimetableActivities btt) {
- BudgetTimetableActivities checkBtt = exists(btt.getName(), btt.getBudgetTimetable().getId());
-
- if (checkBtt == null) {
- final Session session = HibernateUtil.getSessionFactory().getCurrentSession();
- Transaction tx = null;
-
- try {
- tx = session.beginTransaction();
-
- session.save(btt);
- tx.commit();
- } catch (HibernateException e) {
- if (tx != null) {
- tx.rollback();
- }
- e.printStackTrace();
- return "";
- } finally {
- //session.close();
- }
-
- return Utility.ActionResponse.INSERTED.toString();
- } else {
- return Utility.ActionResponse.RECORD_EXISTS.toString();
- }
- }
- public synchronized String update(BudgetTimetableActivities btt) {
- BudgetTimetableActivities checkBtt = exists(btt.getName(), btt.getBudgetTimetable().getId());
-
- if (checkBtt != null) {
- final Session session = HibernateUtil.getSessionFactory().getCurrentSession();
- Transaction tx = null;
-
- try {
- tx = session.beginTransaction();
-
- session.merge(btt);
- tx.commit();
- } catch (HibernateException e) {
- System.out.println("error: "+e.getMessage());
- if (tx != null) {
- tx.rollback();
- }
- e.printStackTrace();
- return "";
- } finally {
- //session.close();
- }
-
- return Utility.ActionResponse.UPDATED.toString();
- } else {
- return Utility.ActionResponse.NO_RECORD.toString();
- }
- }
- public synchronized String delete(BudgetTimetableActivities btt) {
- BudgetTimetableActivities checkBtt = exists(btt.getName(), btt.getBudgetTimetable().getId());
-
- if (checkBtt != null) {
- final Session session = HibernateUtil.getSessionFactory().getCurrentSession();
- Transaction tx = null;
-
- try {
- tx = session.beginTransaction();
-
- session.delete(btt);
- tx.commit();
- } catch (HibernateException e) {
- if (tx != null) {
- tx.rollback();
- }
- e.printStackTrace();
- return "";
- } finally {
- //session.close();
- }
- return Utility.ActionResponse.DELETED.toString();
- } else {
- return Utility.ActionResponse.NO_RECORD.toString();
- }
- }
- public synchronized BudgetTimetableActivities exists(String name, int budgetTimetableID) {
- BudgetTimetableActivities btt = null;
- final Session session = HibernateUtil.getSessionFactory().getCurrentSession();
- Transaction tx = null;
-
- try {
- tx = session.beginTransaction();
-
- Query q = session.createQuery("from " + TABLE_NAME + " as a where a.name = '" + name +
- "' and a.budgetTimetable.id = " + budgetTimetableID);
- btt = (BudgetTimetableActivities) q.uniqueResult();
- tx.commit();
- }
- catch (HibernateException e) {
- if (tx != null) {
- tx.rollback();
- }
- e.printStackTrace();
- }
- finally {
- //session.close();
- }
-
- return btt;
- }
- public synchronized String getMaxserialNo() {
- String tablename = "dbo." + RAW_TABLE_NAME;
-
- List recordlist = null;
- String resp = "";
- final Session session = HibernateUtil.getSessionFactory().getCurrentSession();
- Transaction tx = null;
-
- try {
- tx = session.beginTransaction();
-
- String sql = "select max(id) as maxserialno from " + tablename;
- SQLQuery q = session.createSQLQuery(sql);
- q.setMaxResults(1);
- q.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
-
- recordlist = q.list();
- HashMap hmap = (HashMap) recordlist.get(0);
- if (hmap.get("maxserialno") == null) {
- resp = "0";
- } else {
- resp = hmap.get("maxserialno").toString();
- }
- tx.commit();
- } catch (HibernateException e) {
- if (tx != null) {
- tx.rollback();
- }
- e.printStackTrace();
- } finally {
- //session.close();
- }
- return resp;
- }
-
- public synchronized BudgetTimetableActivities fetchObj(int id) {
- BudgetTimetableActivities budgetTimetableActivity = null;
- final Session session = HibernateUtil.getSessionFactory().getCurrentSession();
- Transaction tx = null;
-
- try {
- tx = session.beginTransaction();
-
- Query q = session.createQuery("from " + TABLE_NAME + " as a where a.id = " + id);
- budgetTimetableActivity = (BudgetTimetableActivities) q.uniqueResult();
-
- tx.commit();
- } catch (HibernateException e) {
- if (tx != null) {
- tx.rollback();
- }
- e.printStackTrace();
- } finally {
- //session.close();
- }
-
- return budgetTimetableActivity;
- }
-
- public synchronized String fetch(int id) {
- List budgetTimetableList = null;
- String jsonList = "";
- final Session session = HibernateUtil.getSessionFactory().getCurrentSession();
- Transaction tx = null;
-
- try {
- tx = session.beginTransaction();
-
- String sql = "select * from " + RAW_TABLE_NAME + " as a where a.id = " + id;
-
- SQLQuery q = session.createSQLQuery(sql);
- budgetTimetableList = q.list();
- Gson gson = new Gson();
- jsonList = gson.toJson(budgetTimetableList);
-
- tx.commit();
- } catch (HibernateException e) {
- if (tx != null) {
- tx.rollback();
- }
- e.printStackTrace();
- } finally {
- //session.close();
- }
-
- return jsonList;
- }
-
- public synchronized String fetchAll(int budgetTimetableID) {
- List budgetTimetableList = null;
- String jsonList = "";
- final Session session = HibernateUtil.getSessionFactory().getCurrentSession();
- Transaction tx = null;
-
- try {
- tx = session.beginTransaction();
-
- String sql = "select a.id, a.name, a.description, a.from_date, a.to_date, b.name as phase from " + RAW_TABLE_NAME + " a " +
- "inner join " + BudgetPhasesHibernateHelper.RAW_TABLE_NAME + " b on a.budget_phase_id = b.id " +
- "where a.budget_timetable_id = " + budgetTimetableID + " order by a.id";
-
- SQLQuery q = session.createSQLQuery(sql);
- budgetTimetableList = q.list();
- Gson gson = new Gson();
- jsonList = gson.toJson(budgetTimetableList);
-
- tx.commit();
- } catch (HibernateException e) {
- if (tx != null) {
- tx.rollback();
- }
- e.printStackTrace();
- } finally {
- //session.close();
- }
-
- return jsonList;
- }
- }