com.overhead.markeugie.manager.ParticipantManager.java Source code

Java tutorial

Introduction

Here is the source code for com.overhead.markeugie.manager.ParticipantManager.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.overhead.markeugie.manager;

import com.overhead.markeugie.entity.Participants;
import com.overhead.markeugie.services.ParticipantService;
import com.overhead.markeugie.util.IdGeneratorUtil;
import com.overhead.markeugie.util.RegistrationHibernateUtil;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author markeugie
 */
public class ParticipantManager extends UnicastRemoteObject implements ParticipantService {

    private static ParticipantManager instance;

    public ParticipantManager() throws RemoteException {

    }

    public static ParticipantManager getInstance() throws RemoteException {
        if (instance == null) {
            instance = new ParticipantManager();
        }
        return instance;
    }

    @Override
    public Participants add(Participants participant, String subcamp) throws RemoteException {

        Session session = RegistrationHibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Criteria criteria = session.createCriteria(Participants.class);
        //        System.out.println(criteria.list().size());
        String generatedId = IdGeneratorUtil.generateId(criteria.list().size(), subcamp);

        //        String lastId = criteria.list() + "";
        //        System.out.println(lastId);
        //        String numberOfZeroes = null;
        //        for (int i = 0; i < 4 - lastId.length(); i++) {
        //            numberOfZeroes = numberOfZeroes + "0";
        //        }
        //        }
        //        criteria.setProjection(Projections.max("id"));
        //        criteria.setMaxResults(1);
        //        String lastId = (String) criteria.uniqueResult();
        //        System.out.println(lastId);
        //        int id = Integer.parseInt(lastId.substring(1));
        //        System.out.println(id);
        //        String generatedId = IdGeneratorUtil.generateId(id, subcamp);
        //        System.out.println(generatedId);
        //        String strId = id + "";
        //        switch (subcamp) {
        //            case "Subcamp A":
        //                for (int i = 0; i < 4 - strId.length(); i++) {
        //                    numberOfZeroes = numberOfZeroes.concat("0");
        //                }
        //                lastId = "A" + numberOfZeroes + strId;
        //                System.out.println(lastId);
        //                break;
        //
        //            case "Subcamp B":
        //                for (int i = 0; i < 4 - strId.length(); i++) {
        //                    numberOfZeroes = numberOfZeroes.concat("0");
        //                }
        //                lastId = "B" + numberOfZeroes + strId;
        //                break;
        //
        //            case "Subcamp C":
        //                for (int i = 0; i < 4 - strId.length(); i++) {
        //                    numberOfZeroes = numberOfZeroes.concat("0");
        //                }
        //                lastId = "C" + numberOfZeroes + strId;
        //                break;
        //
        //            case "Subcamp D":
        //                for (int i = 0; i < 4 - strId.length(); i++) {
        //                    numberOfZeroes = numberOfZeroes.concat("0");
        //                }
        //                lastId = "D" + numberOfZeroes + strId;
        //                break;
        //        }
        //        if (lastId == null || lastId.equals("")) {
        //            switch (subcamp) {
        //                case "Subcamp A":
        //                    lastId = "A0000";
        //                    break;
        //                case "Subcamp B":
        //                    lastId = "B0000";
        //                    break;
        //                case "Subcamp C":
        //                    lastId = "C0000";
        //                    break;
        //                case "Subcamp D":
        //                    lastId = "D0000";
        //                    break;
        //            }
        //
        //        }
        participant.setId(generatedId);
        session.save(participant);
        session.getTransaction().commit();
        session.close();

        return participant;
    }

    @Override
    public void edit() throws RemoteException {
    }

    @Override
    public void delete() throws RemoteException {
    }

    @Override
    public Participants getById(String id) throws RemoteException {

        Participants participant = null;
        Session session = RegistrationHibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Criteria criteria = session.createCriteria(Participants.class);
        criteria.add(Restrictions.eq("id", id));
        criteria.setMaxResults(1);
        participant = (Participants) criteria.uniqueResult();
        session.getTransaction().commit();
        session.close();
        return participant;
    }

    @Override
    public List<Participants> getByLastName(String lastName) throws RemoteException {

        Session session = RegistrationHibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Criteria criteria = session.createCriteria(Participants.class);
        criteria.add(Restrictions.eq("lastName", lastName));
        List<Participants> participants = criteria.list();
        session.getTransaction().commit();
        session.close();
        return participants;
    }

    @Override
    public Participants getByFirstNameAndLastName(String firstName, String lastName) throws RemoteException {

        Session session = RegistrationHibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Criteria criteria = session.createCriteria(Participants.class);
        criteria.add(Restrictions.eq("firstName", firstName));
        criteria.add(Restrictions.eq("lastName", lastName));
        criteria.setMaxResults(1);
        Participants participant = (Participants) criteria.uniqueResult();
        session.getTransaction().commit();
        session.close();

        return participant;
    }

    @Override
    public List<Participants> getAllParticipants() throws RemoteException {

        List<Participants> participants;
        Session session = RegistrationHibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Criteria criteria = session.createCriteria(Participants.class);
        participants = (List<Participants>) criteria.list();
        session.getTransaction().commit();
        session.close();

        return participants;

    }

    //    public synchronized String generateId(Session session) {
    //        String id = "";
    //        try {
    //            id = IdGeneratorUtil.generateId(Integer.parseInt(getLastId(session).substring(4)));
    //        } catch (RemoteException ex) {
    //            Logger.getLogger(ParticipantManager.class.getName()).log(Level.SEVERE, null, ex);
    //        }
    //        return id;
    //    }
    //    private String getLastId() throws RemoteException {
    //        Session session = RegistrationHibernateUtil.getSessionFactory().openSession();
    //        session.beginTransaction();
    //        Criteria criteria = session.createCriteria(Participants.class);
    //        criteria.setProjection(Projections.max("id"));
    //        criteria.setMaxResults(1);
    //        String lastId = (String) criteria.uniqueResult();
    //        if (lastId == null || lastId.equals("")) {
    //            lastId = "Y0000";
    //        }
    //        session.getTransaction().commit();
    //        session.close();
    //
    //        return lastId;
    //    }
    //    private String getLastId(Session session) throws RemoteException {
    ////        session = RegistrationHibernateUtil.getSessionFactory().openSession();
    ////        session.beginTransaction();
    //        Criteria criteria = session.createCriteria(Participants.class);
    //        criteria.setProjection(Projections.max("id"));
    //        criteria.setMaxResults(1);
    //        String lastId = (String) criteria.uniqueResult();
    //        if (lastId == null || lastId.equals("")) {
    //            lastId = "Y0000";
    //        }
    ////        session.getTransaction().commit();
    ////        session.close();
    //
    //        return lastId;
    //    }
    @Override
    public int getMaxId() throws RemoteException {
        int id = 0;
        Session session = RegistrationHibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Criteria criteria = session.createCriteria(Participants.class);
        criteria.setProjection(Projections.max("id"));
        criteria.setMaxResults(1);
        String lastId = (String) criteria.uniqueResult();
        if (lastId == null || lastId.equals("")) {
            id = 0;
        } else {
            id = Integer.parseInt(lastId);
        }
        session.getTransaction().commit();
        session.close();

        return id;
    }

    @Override
    public List<Participants> getStudentsBySchool(String school) throws RemoteException {

        Session session = RegistrationHibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Criteria criteria = session.createCriteria(Participants.class);
        criteria.add(Restrictions.eq("school", school));
        List<Participants> participants = criteria.list();
        sortByLastName(participants);
        return participants;
    }

    private void sortByLastName(List<Participants> participants) {

        Collections.sort(participants, new Comparator<Participants>() {
            @Override
            public int compare(Participants o1, Participants o2) {

                return o1.getLastName().compareTo(o2.getLastName());
            }
        });
    }

    @Override
    public List<Participants> getAllParticipantsSortedBySchoolAndLastName() throws RemoteException {

        List<Participants> participants;

        Session session = RegistrationHibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Criteria criteria = session.createCriteria(Participants.class);
        participants = (List<Participants>) criteria.list();
        session.getTransaction().commit();
        sortBySchoolAndLastName(participants);
        session.close();

        return participants;

    }

    private void sortBySchoolAndLastName(List<Participants> participants) {

        Collections.sort(participants, new Comparator<Participants>() {
            @Override
            public int compare(Participants o1, Participants o2) {

                return o1.getSchool().compareTo(o2.getSchool());

            }
        });

        Collections.sort(participants, new Comparator<Participants>() {
            @Override
            public int compare(Participants o1, Participants o2) {

                return o1.getLastName().compareTo(o2.getLastName());
            }
        });
    }

    @Override
    public Participants updateParticipant(Participants participant) throws RemoteException {

        Session session = RegistrationHibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.update(participant);
        session.getTransaction().commit();
        session.close();

        return participant;
    }

    public static void main(String[] args) {
        try {
            getInstance().add(new Participants(), "Subcamp B");
        } catch (RemoteException ex) {
            Logger.getLogger(ParticipantManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}