Package service

Source Code of service.UserService

package service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;

import test.CatalogBean;
import test.NewsBean;
import test.OrarForm;
import test.ProfesorForm;
import test.StudentForm;

import nom.Catalog;
import nom.News;
import nom.Obiect;
import nom.Orar;
import nom.Profesor;
import nom.Student;
import nom.Grupa;

public class UserService {
  private static EntityManager em;

  public static List<String> generareListaObiecte(String nume, String prenume) {
    List<String> listaObiecte = new ArrayList<String>();
    Student s = new Student();
    Obiect o = new Obiect();
    Query query;
    EntityManagerFactory emf = Persistence
        .createEntityManagerFactory("modul3L2");
    em = emf.createEntityManager();
    em.getTransaction().begin();
    query = em
        .createQuery(
            "SELECT s FROM Student s WHERE s.nume=:v1 AND s.prenume=:v2")
        .setParameter("v1", nume).setParameter("v2", prenume);
    s = (Student) query.getSingleResult();   
    query=em.createNativeQuery("SELECT obiect.nume FROM Obiect WHERE obiect.id_obiect IN " +
        "(SELECT catalog.catalog_id_obiect FROM Catalog WHERE catalog.catalog_id_student=?1)")
        .setParameter(1, s.getIdStudent());
    listaObiecte=query.getResultList();
    em.getTransaction().commit();
    return listaObiecte;
  }

  public static List<String> generareListaDate(Date date1) {
    List<String> result = new ArrayList<String>();
    SimpleDateFormat formatDate = new SimpleDateFormat("EEEE dd/MM/YYYY");
    Calendar c = Calendar.getInstance();
    c.setTime(date1);

    for (int i = 0; i < 5; i++) {
      c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
      Date date = c.getTime();
      c.setTime(date);
      c.add(Calendar.DATE, i);
      date = c.getTime();
      result.add(formatDate.format(date));

    }
    return result;

  }

  public static String adaugaProfesor(Profesor profesor) {

      EntityManagerFactory emf = Persistence
        .createEntityManagerFactory("modul3L2");
    em = emf.createEntityManager();
    em.getTransaction().begin();
 
    em.persist(profesor);
    em.getTransaction().commit();
    return "succes";

  }
  public static void removeProfesor(Profesor prof){
    EntityManagerFactory emf = Persistence
        .createEntityManagerFactory("modul3L2");
    em = emf.createEntityManager();
    em.getTransaction().begin();
    Profesor profesor=em.find(Profesor.class, prof.getIdProfesor());
    em.remove(profesor);
    em.getTransaction().commit();
  }
  @SuppressWarnings("unchecked")
  public static List<NewsBean> creareListaNews() {

    List<News> n = new ArrayList<News>();
    List<NewsBean> data = new ArrayList<NewsBean>();
    NewsBean c = new NewsBean();
    EntityManagerFactory emf = Persistence
        .createEntityManagerFactory("modul3L2");
    em = emf.createEntityManager();
    em.getTransaction().begin();
    Query query = em.createQuery("SELECT n FROM News n");
    n = query.getResultList();   
    for (News ind : n) {
      c.setContinutNews(ind.getContinut());
      c.setContinutShortNews(ind.getContinut().substring(0, 90) + "...");
      data.add(c);
    }
    em.getTransaction().commit();
    em.close();
    return data;

  }

  public static List<OrarForm> listareOrar(String numeGrupa, String data) {

    List<Orar> o = new ArrayList<Orar>();
    List<Grupa> gr = new ArrayList<Grupa>();
    List<OrarForm> orar = new ArrayList<OrarForm>();
    OrarForm obiectOrar;

    EntityManagerFactory emf = Persistence
        .createEntityManagerFactory("modul3L2");
    em = emf.createEntityManager();
    em.getTransaction().begin();
    Query query = em.createQuery(
        "SELECT g FROM Grupa g WHERE g.numeGrupa=:v")
        .setParameter("v",numeGrupa);
    gr = (List<Grupa>) query.getResultList();
    SimpleDateFormat formatDate = new SimpleDateFormat("EEEE dd/MM/YYYY");
    if (!gr.isEmpty()) {
      o = gr.get(0).getOrar();
    }
    String numeObiect;
    for (Orar oIndex : o) {
      obiectOrar = new OrarForm();
      if (formatDate.format(oIndex.getData()).equals(data)) {
        numeObiect = (String) em
            .createQuery(
                "SELECT o.nume FROM Obiect o WHERE o.idObiect=:v")
            .setParameter("v", oIndex.getIdObiect())
            .getSingleResult();
        obiectOrar.setNumeObiect(numeObiect);
        obiectOrar.setData(formatDate.format(oIndex.getData()));
        obiectOrar.setInterval(oIndex.getIntervalOrar());
        obiectOrar.setSala(String.valueOf(oIndex.getSalaCurs()));
        orar.add(obiectOrar);
      }

    }
    em.getTransaction().commit();
    em.close();
    System.out.println(orar.size());
    return orar;

  }

  public static List<CatalogBean> listareCatalog(String obiect) {
    List<Catalog> c = new ArrayList<Catalog>();
    List<CatalogBean> catalog = new ArrayList<CatalogBean>();
    Obiect ol = new Obiect();

    EntityManagerFactory emf = Persistence
        .createEntityManagerFactory("modul3L2");
    em = emf.createEntityManager();
    em.getTransaction().begin();
    Query query = em
        .createQuery("SELECT o FROM Obiect o WHERE o.nume=:v1 ")
        .setParameter("v1", obiect);

    ol = (Obiect) query.getSingleResult();

    if (!ol.equals(null)) {
      c = ol.getCatalog();

    }
    SimpleDateFormat formatDate = new SimpleDateFormat("dd/MM/YYYY");
    int i = 0;
    for (Catalog cInd : c) {

      CatalogBean v = new CatalogBean();
     
      v.setData(formatDate.format(cInd.getDataNotei()));
      v.setC(cInd);
      v.setSelected(false);
      catalog.add(v);

    }

    em.getTransaction().commit();
    em.close();
    return catalog;
  }

  public static void aplliedAction(List<CatalogBean> modificari) {
    Obiect ol = new Obiect();
    Student st=new Student();
    List<Catalog>removals=new ArrayList<Catalog>();
    int idStudent=-1;
    int idObiect=-1;
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("modul3L2");
    em = emf.createEntityManager();
    em.getTransaction().begin();
    idObiect=modificari.get(0).getC().getIdObiect();
    idStudent=modificari.get(0).getC().getIdStudent();
   
    Query query = em
        .createQuery("SELECT o FROM Obiect o WHERE o.idObiect=:v1 ")
        .setParameter("v1", idObiect);

    ol = (Obiect) query.getSingleResult();
    query = em
        .createQuery("SELECT s FROM Student s WHERE s.idStudent=:v1 ")
        .setParameter("v1", idStudent);
    st=(Student)query.getSingleResult();
   
   
    if (!ol.equals(null)) {
     ol.setCatalog(new ArrayList<Catalog>());
    }
    if(!st.equals(null)){
      st.setCatalog(new ArrayList<Catalog>());
    }
    for(CatalogBean cInd:modificari){
      if((cInd.getC().getIdObiect()==0)&&(cInd.getC().getIdStudent()==0)){
        cInd.getC().setIdObiect(idObiect);
        cInd.getC().setIdStudent(idStudent);
      }
      ol.getCatalog().add(cInd.getC());
      st.getCatalog().add(cInd.getC());
    }   
    em.merge(ol);
    em.merge(st);
     em.createNativeQuery(
         "DELETE FROM Catalog WHERE catalog_id_student is null AND catalog_id_obiect is null")
         .executeUpdate();
     
    em.getTransaction().commit();
    em.close();

   

  }

  @SuppressWarnings("unchecked")
  public static List<Profesor> creareListaProfesori() {
    List<Profesor> p = new ArrayList<Profesor>();
   
    EntityManagerFactory emf = Persistence
        .createEntityManagerFactory("modul3L2");
    em = emf.createEntityManager();
    em.getTransaction().begin();
    Query query = em.createQuery("SELECT p FROM Profesor p");
    p = (List<Profesor>) query.getResultList();
   
    return p;

  }

  @SuppressWarnings("unchecked")
  public static List<String> creareListaGrupe() {
    List<String> numeGrupe = new ArrayList<String>();
    EntityManagerFactory emf = Persistence
        .createEntityManagerFactory("modul3L2");
    em = emf.createEntityManager();
    em.getTransaction().begin();
    Query query = em
        .createQuery("SELECT DISTINCT g.numeGrupa FROM Grupa g ");

    numeGrupe = (List<String>) query.getResultList();
    return numeGrupe;
  }

  @SuppressWarnings("unchecked")
  public static List<StudentForm> creareListaStudentiForm(String grupa) {
    List<StudentForm> stForm = new ArrayList<StudentForm>();
    List<Grupa> gr = new ArrayList<Grupa>();
    // System.out.println(grupa);
    EntityManagerFactory emf = Persistence
        .createEntityManagerFactory("modul3L2");
    em = emf.createEntityManager();
    em.getTransaction().begin();
    Query query = em.createQuery(
        "SELECT g FROM Grupa g WHERE g.numeGrupa=:v1").setParameter(
        "v1", grupa);
    gr = (List<Grupa>) query.getResultList();

    if (!gr.isEmpty()) {
      for (Grupa g : gr) {
        for (Student s : g.getStudenti()) {
          StudentForm sf = new StudentForm();
          sf.setNrMatricol(s.getNrMatricol());
          sf.setAn(s.getAnStudiu());
          sf.setNume(s.getNume() + " " + s.getPrenume());
          sf.setNumeBaza(s.getNume());
          sf.setGrupa(g.getNumeGrupa());
          sf.setEmail(s.getEmail());
          sf.setPrenume(s.getPrenume());
          sf.setOrar(g.getNumeGrupa());
          sf.setCatalog("Go to ...");
          stForm.add(sf);
        }
      }
      em.getTransaction().commit();
      em.close();
    }
    return stForm;
  }

  public static String validUser(String nrMatricol, String email) {
    List<Student> st = new ArrayList<Student>();

    try {
      EntityManagerFactory emf = Persistence
          .createEntityManagerFactory("modul3L2");
      em = emf.createEntityManager();
      em.getTransaction().begin();
      Query query = em
          .createQuery(
              "SELECT s FROM Student s WHERE s.nrMatricol=:v1 AND s.email=:v2")
          .setParameter("v1", nrMatricol).setParameter("v2", email);
      st = query.getResultList();

      em.getTransaction().commit();
      em.close();
    } catch (Exception e) {
      // log the exception
    }

    if (st.size() == 1) {

      return "second?faces-redirect=true";
    }

    return "failedlogin?faces-redirect=true";

  }

  @SuppressWarnings("unchecked")
  public static String adaugaStudent(StudentForm studentForm) {
    List<Student> st = new ArrayList<Student>();
    Query query;
    boolean q1 = true, q2 = true;
    try {
      EntityManagerFactory emf = Persistence
          .createEntityManagerFactory("modul3L2");
      em = emf.createEntityManager();
      em.getTransaction().begin();
      query = em
          .createQuery(
              "SELECT s FROM Student s WHERE s.nume=:v1 AND s.prenume=:v2")
          .setParameter("v1", studentForm.getNume())
          .setParameter("v2", studentForm.getPrenume());
      q1 = !query.getResultList().isEmpty();
      System.out.println(q1);
      query = em.createQuery(
          "SELECT s FROM Student s WHERE  s.nrMatricol=:v1")
          .setParameter("v1", studentForm.getNrMatricol());
      q2 = !query.getResultList().isEmpty();
      System.out.println(q2);
      em.getTransaction().commit();
      em.close();
    } catch (Exception e) {
      // log the exception
    }

    if (q1 || q2) {

      return "student existent";
    } else {
      Student s = new Student();
      s.setAnStudiu(Integer.valueOf(studentForm.getAn()).intValue());
      s.setNrMatricol(studentForm.getNrMatricol());
      s.setNume(studentForm.getNume());
      s.setPrenume(studentForm.getPrenume());
      s.setEmail(studentForm.getEmail());
      List<Grupa> g = new ArrayList<Grupa>();
      EntityManagerFactory emf = Persistence
          .createEntityManagerFactory("modul3L2");
      em = emf.createEntityManager();
      em.getTransaction().begin();
      query = em.createQuery(
          "SELECT g FROM Grupa g WHERE g.numeGrupa=:v1")
          .setParameter("v1", studentForm.getGrupa());
      g = query.getResultList();
      Grupa gr = new Grupa();
      if (g.size() == 1) {

        gr = g.get(0);
        s.setIdGrupa(gr.getIdGrupa());
        gr.getStudenti().add(s);
        em.merge(gr);

      } else {
        gr.setNumeGrupa(studentForm.getGrupa());
        s.setIdGrupa(0);
        gr.getStudenti().add(s);
        em.persist(gr);
      }

    }
    em.getTransaction().commit();
    em.close();
    return "succes";

  }

  public static StudentForm findStudent(String nrMatricol) {
    List<Student> st = new ArrayList<Student>();

    EntityManagerFactory emf = Persistence
        .createEntityManagerFactory("modul3L2");
    em = emf.createEntityManager();
    em.getTransaction().begin();
    Query query = em.createQuery(
        "SELECT s FROM Student s WHERE s.nrMatricol=:v1").setParameter(
        "v1", nrMatricol);

    st = query.getResultList();

    StudentForm result = new StudentForm();
    if (!st.isEmpty()) {
      String numeGrupa = (String) em
          .createNativeQuery(
              "SELECT grupa.NUME_GRUPA  FROM Grupa,Student WHERE grupa.ID_GRUPA=student.studenti_id_grupa AND student.NR_MATRICOL=?1")
          .setParameter(1, nrMatricol).getSingleResult();

      result.setAn(st.get(0).getAnStudiu());
      result.setEmail(st.get(0).getEmail());
      result.setGrupa(numeGrupa);
      result.setNrMatricol(nrMatricol);
      result.setNume(st.get(0).getNume());
      result.setPrenume(st.get(0).getPrenume());
    }
    em.getTransaction().commit();
    em.close();
    return result;
  }

  @SuppressWarnings("unchecked")
  public static void removeStudent(String nrMatricol) {
    List<Student> st = new ArrayList<Student>();
    Grupa g = new Grupa();
    Catalog c = new Catalog();
    EntityManagerFactory emf = Persistence
        .createEntityManagerFactory("modul3L2");
    em = emf.createEntityManager();
    em.getTransaction().begin();
    Query query = em.createQuery(
        "SELECT s FROM Student s WHERE s.nrMatricol=:v1").setParameter(
        "v1", nrMatricol);
    st = query.getResultList();
    if (!st.isEmpty()) {
      String numeGrupa = (String) em
          .createNativeQuery(
              "SELECT grupa.NUME_GRUPA  FROM Grupa,Student WHERE grupa.ID_GRUPA=student.studenti_id_grupa AND student.NR_MATRICOL=?1")
          .setParameter(1, nrMatricol).getSingleResult();
      g = (Grupa) em
          .createQuery("SELECT g FROM Grupa g WHERE g.numeGrupa=:v1")
          .setParameter("v1", numeGrupa).getSingleResult();
      g.getStudenti().remove(g.getStudenti().indexOf(st.get(0)));
    }
    em.merge(g);
    query = em.createQuery("DELETE FROM Student s WHERE s.nrMatricol=:v1")
        .setParameter("v1", nrMatricol);
    query.executeUpdate();

    em.getTransaction().commit();
    em.close();

  }

  public static void updateStudent(StudentForm studentUpdate,
      String nrMatricol) {

   
    Student toBeUpdatedStudent = new Student();
    Grupa g = new Grupa();
    EntityManagerFactory emf = Persistence
        .createEntityManagerFactory("modul3L2");
    em = emf.createEntityManager();
    em.getTransaction().begin();
    Query query = em.createQuery(
        "SELECT s FROM Student s WHERE s.nrMatricol=:v1").setParameter(
        "v1", nrMatricol);

    toBeUpdatedStudent = (Student) query.getSingleResult();
    String numeGrupa = (String) em
        .createNativeQuery(
            "SELECT grupa.NUME_GRUPA  FROM Grupa,Student WHERE grupa.ID_GRUPA=student.studenti_id_grupa AND student.NR_MATRICOL=?1")
        .setParameter(1, nrMatricol).getSingleResult();
    g = (Grupa) em
        .createQuery("SELECT g FROM Grupa g WHERE g.numeGrupa=:v1")
        .setParameter("v1", numeGrupa).getSingleResult();
    g.getStudenti().remove(g.getStudenti().indexOf(toBeUpdatedStudent));
    em.merge(g);
    toBeUpdatedStudent.setAnStudiu(Integer.valueOf(studentUpdate.getAn())
        .intValue());
    toBeUpdatedStudent.setNrMatricol(studentUpdate.getNrMatricol());
    toBeUpdatedStudent.setNume(studentUpdate.getNumeBaza());
    toBeUpdatedStudent.setPrenume(studentUpdate.getPrenume());
    toBeUpdatedStudent.setEmail(studentUpdate.getEmail());

    if (studentUpdate.getGrupa().equals(numeGrupa)) {
      g = (Grupa) em
          .createQuery("SELECT g FROM Grupa g WHERE g.numeGrupa=:v1")
          .setParameter("v1", numeGrupa).getSingleResult();
      g.getStudenti().add(toBeUpdatedStudent);
      em.merge(g);
    } else {

      g = (Grupa) em
          .createQuery("SELECT g FROM Grupa g WHERE g.numeGrupa=:v1")
          .setParameter("v1", studentUpdate.getGrupa())
          .getSingleResult();

      g.getStudenti().add(toBeUpdatedStudent);
      em.merge(g);
    }
    em.getTransaction().commit();
    em.close();

  }
  public static void updateProfesor(Profesor prof){
   
   
    EntityManagerFactory emf = Persistence
        .createEntityManagerFactory("modul3L2");
    em = emf.createEntityManager();
    em.getTransaction().begin();
   
    em.merge(prof);
    em.getTransaction().commit();
    em.close();
  }
}
TOP

Related Classes of service.UserService

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.