at.thinkingco2.databasemanager.JourneyAccess.java Source code

Java tutorial

Introduction

Here is the source code for at.thinkingco2.databasemanager.JourneyAccess.java

Source

package at.thinkingco2.databasemanager;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.Query;
import org.hibernate.Session;

import at.thinkingco2.databasemanager.databeans.ChildBean;
import at.thinkingco2.databasemanager.databeans.GroupBean;
import at.thinkingco2.databasemanager.databeans.JourneyBean;

/**
 * AccessClass for accessing the JourneyBean in Database.
 * 
 * @author lukashaidacher
 * 
 */
public class JourneyAccess extends DatabaseManager {
    private static final JourneyAccess INSTANCE;

    static {
        INSTANCE = new JourneyAccess();
    }

    private JourneyAccess() {
        super();
    }

    /**
     * This function returns a static INSTANCE of JourneyAccess.
     * 
     * @return a static INSTANCE of JourneyAccess.
     */
    public static JourneyAccess getInstance() {
        return INSTANCE;
    }

    /**
     * This function saves a JourneyBean to Database.
     * 
     * @param entity
     *            the JourneyBean
     */
    public void addJourney(JourneyBean entity) {
        addObject(entity);
    }

    /**
     * This function deletes a JourneyBean from Database.
     * 
     * @param entity
     *            the JourneyBean
     */
    public void deleteJourney(JourneyBean entity) {
        deleteObject(entity);
    }

    /**
     * This function updates a JourneyBean in Database.
     * 
     * @param entity
     *            the JourneyBean
     */
    public void updateJourney(JourneyBean entity) {
        updateObject(entity);
    }

    /**
     * This function deletes all JourneyBeans from Database.
     */
    public void deleteAllJourneyes() {
        deleteTableData(JourneyBean.class);
    }

    /**
     * This function returns all JourneyBeans from Database
     * 
     * @return List of JourneyBeans
     */
    @SuppressWarnings("unchecked")
    public List<JourneyBean> getAllJourneyBeans() {
        return (List<JourneyBean>) getAllObjectsFromBean(JourneyBean.class);
    }

    /**
     * This Function returns a list of JourneyBeans, which have no current drive
     * (JounreyBean which searches a lift).
     * 
     * @return a List of JounreyBean
     */
    @SuppressWarnings("unchecked")
    public List<JourneyBean> getJourneysWithoutDriver() {
        Session session = getSession();
        List<JourneyBean> list = null;
        Query q = null;

        q = session.createQuery("FROM JourneyBean WHERE licenseNumber IS NULL");

        list = q.list();
        session.close();

        return list;
    }

    /**
     * This Function returns a list of JourneyBeans, which have a current driver
     * 
     * @return a List of JounreyBeans
     */
    @SuppressWarnings("unchecked")
    public List<JourneyBean> getJourneysWithDriver() {
        Session session = getSession();
        List<JourneyBean> list = null;
        Query q = null;

        q = session.createQuery("FROM JourneyBean WHERE licenseNumber NOT NULL");

        list = q.list();
        session.close();

        return list;
    }

    /**
     * This function returns the Number of used Seats in a given Route
     * 
     * @param routeId
     *            The routeId
     * @return Number of used Seats
     */
    @SuppressWarnings("unchecked")
    public Integer getUsedSeats(Integer routeId) {
        Session session = getSession();
        List<JourneyBean> journeyList = null;
        Integer seats = 0;
        Query q = null;

        q = session.createQuery("FROM JourneyBean WHERE routeId = :routeId");
        q.setParameter("routeId", routeId);

        journeyList = q.list();

        for (JourneyBean journey : journeyList) {
            seats += journey.getChildren().size();
        }

        session.close();

        return seats + 1; // +1 = + parent
    }

    @SuppressWarnings("unchecked")
    public List<JourneyBean> getJourneysWithDriver(Set<GroupBean> groups) {
        Session session = getSession();

        Set<GroupBean> groupSet = null;
        Iterator<GroupBean> it = null;
        ArrayList<JourneyBean> journeyList = new ArrayList<JourneyBean>();

        List<JourneyBean> temp = session.createQuery("FROM JourneyBean WHERE licenseNumber IS NOT NULL").list();

        for (JourneyBean journey : temp) {
            groupSet = journey.getGroups();
            it = groups.iterator();
            while (it.hasNext()) {
                if (groupSet.contains(it.next())) {
                    journeyList.add(journey);
                }
            }
        }

        session.close();

        return journeyList;
    }

    @SuppressWarnings("unchecked")
    public List<JourneyBean> getJourneysWithoutDriver(Set<GroupBean> groups) {
        Session session = getSession();

        Set<GroupBean> groupSet = null;
        Iterator<GroupBean> it = null;
        ArrayList<JourneyBean> journeyList = new ArrayList<JourneyBean>();

        List<JourneyBean> temp = session.createQuery("FROM JourneyBean WHERE licenseNumber IS NULL").list();

        for (JourneyBean journey : temp) {
            groupSet = journey.getGroups();
            it = groups.iterator();
            while (it.hasNext()) {
                if (groupSet.contains(it.next())) {
                    journeyList.add(journey);
                }
            }
        }

        session.close();

        return journeyList;
    }

    @SuppressWarnings("unchecked")
    public List<JourneyBean> getJourneysByRouteID(Integer routeId) {
        Session session = getSession();
        List<JourneyBean> list = null;

        Query q = session.createQuery("FROM JourneyBean WHERE routeId = :routeId");
        q.setParameter("routeId", routeId);

        list = q.list();
        session.close();

        return list;

    }

    public JourneyBean getJourney(int id) {
        Session session = getSession();

        Query q = session.createQuery("FROM JourneyBean WHERE journeyId = :id");
        q.setParameter("id", id);

        JourneyBean res = (JourneyBean) q.uniqueResult();
        session.close();

        return res;
    }

    public JourneyBean getJourney(JourneyBean journeyBean) {
        Session session = getSession();

        Query q = session
                .createQuery("FROM JourneyBean WHERE description = :description AND arrivleTime = :arrivleTime");
        q.setParameter("description", journeyBean.getDescription());
        q.setParameter("arrivleTime", journeyBean.getArrivleTime());

        JourneyBean res = (JourneyBean) q.uniqueResult();
        session.close();

        return res;
    }
}