com.model.manegers.Agent_maneger.java Source code

Java tutorial

Introduction

Here is the source code for com.model.manegers.Agent_maneger.java

Source

/*
 * 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 com.model.manegers;

import com.util.HibernateUtil;
import static com.util.HibernateUtil.getSessionFactory;
import com.util.Agents;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Khalil Sharkawi
 */
public class Agent_maneger {

    public static List<Agents> getAgents() {
        Session session = getSessionFactory().openSession();
        List<Agents> l = session.createQuery("FROM Agents").list();
        //        session.close();
        return l;
    }

    public static void AddAgent(Agents p) {
        Transaction trs = null;
        Session session = getSessionFactory().openSession();
        try {
            trs = session.beginTransaction();
            session.save(p);
            session.getTransaction().commit();
        } catch (RuntimeException e) {
            if (trs != null) {
                trs.rollback();
            }
        } finally {
            session.flush();
            session.close();
        }

    }

    public static void DeleteAgent(int id) {

        Agents myObject;
        Transaction trs = null;
        Session session = getSessionFactory().openSession();
        try {
            trs = session.beginTransaction();
            myObject = (Agents) session.load(Agents.class, new Integer((int) id));
            session.delete(myObject);
            session.getTransaction().commit();
        } catch (RuntimeException e) {
            if (trs != null) {
                trs.rollback();
            }
        } finally {
            session.flush();
            session.close();
        }
    }

    public static void updateAgent(Agents pro) {
        Transaction trns = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            trns = session.beginTransaction();
            //Abeer
            Agents soso = new Agents();
            soso.setId(pro.getId());
            soso.setFirstname(pro.getFirstname());
            soso.setLastname(pro.getLastname());
            soso.setAddress(pro.getAddress());
            soso.setTelephone(pro.getTelephone());
            soso.setEmail(pro.getEmail());
            soso.setImportAmmount(pro.getImportAmmount());
            soso.setImportType(pro.getImportType());
            soso.setDiscount(pro.getDiscount());

            //
            session.saveOrUpdate(soso);
            session.getTransaction().commit();
        } catch (RuntimeException e) {
            if (trns != null) {
                trns.rollback();
            }
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
    }

    public static Agents getByid(int id) {
        Agents myObject = null;
        Transaction trs = null;
        Session session = getSessionFactory().openSession();
        try {
            //            trs = session.beginTransaction();
            myObject = (Agents) session.load(Agents.class, new Integer((int) id));
            //            session.delete(myObject);
            //            session.getTransaction().commit();
        } catch (RuntimeException e) {
            if (trs != null) {
                //                trs.rollback();
            }
        } finally {
            session.flush();
            session.close();
            return myObject;
        }
    }

    public static Agents getByname(String name) {
        Agents myObject = null;
        Transaction trs = null;
        Session session = getSessionFactory().openSession();
        try {
            List<Agents> l = session.createQuery("FROM Agents").list();
            for (Agents a : l) {
                if ((a.getFirstname() + " " + a.getLastname()).compareTo(name) == 0) {
                    myObject = a;
                }
            }
        } catch (RuntimeException e) {
            if (trs != null) {
            }
        } finally {
            session.flush();
            session.close();
            return myObject;
        }
    }

}