@Override
  public List<Role> getAllRoles() {

    Session session = SessionFactoryProvider.getSessionFactory().openSession();
    Transaction tx = null;
    List<Role> allRoles = new ArrayList<Role>();

    try {
      tx = session.beginTransaction();
      Criteria criteria = session.createCriteria(Role.class);
      List<Role> roles = criteria.list();

      for (Role r : roles) {
        allRoles.add(r);
      }

      tx.commit();
    } catch (HibernateException e) {
      if (tx != null) {
        e.printStackTrace();
      }
    } finally {
      session.close();
    }

    return allRoles;
  }
  public ArrayList<StaffEntity> getAllStaff() {

    Session session = SessionFactoryProvider.getSessionFactory().openSession();

    ArrayList<StaffEntity> departmentStaff = new ArrayList<StaffEntity>();
    Transaction dbTransaction = null;

    try {
      dbTransaction = session.beginTransaction();
      List staffInDB = session.createQuery("FROM StaffEntity ").list();

      for (Iterator iterator = staffInDB.iterator(); iterator.hasNext(); ) {

        StaffEntity staff = (StaffEntity) iterator.next();
        departmentStaff.add(staff);
      }

      dbTransaction.commit();

    } catch (HibernateException error) {
      if (dbTransaction != null) dbTransaction.rollback();
      error.printStackTrace();
    } finally {
      session.close();
    }

    log.info("Number of staff on the department: " + departmentStaff.size());

    return departmentStaff;
  }
示例#3
0
  public Integer createWordList(WordList record) {

    Session session = SessionFactoryProvider.getSessionFactory().openSession();
    Transaction tx = null;
    Integer listId = null;

    try {

      tx = session.beginTransaction();
      listId = (Integer) session.save(record);
      tx.commit();

    } catch (HibernateException hex) {

      if (tx != null) {

        tx.rollback();
      }

      hex.printStackTrace();

    } finally {

      session.close();
    }

    return listId;
  }
  public void updateStaff(StaffEntity staff) {
    Session session = SessionFactoryProvider.getSessionFactory().openSession();

    Transaction dbTransaction = null;
    try {
      dbTransaction = session.beginTransaction();

      StaffEntity staffUpdate = (StaffEntity) session.get(StaffEntity.class, staff.getStaffId());

      log.info("Updating: " + staffUpdate.toString());

      staffUpdate.setFireNumber(staff.getFireNumber());
      staffUpdate.setFirstName(staff.getFirstName());
      staffUpdate.setLastName(staff.getLastName());
      staffUpdate.setRank(staff.getRank());
      staffUpdate.setHireDate(staff.getHireDate());
      staffUpdate.setTermDate(staff.getTermDate());
      staffUpdate.setDepartmentId(staff.getDepartmentId());

      session.update(staffUpdate);
      dbTransaction.commit();

    } catch (HibernateException error) {
      if (dbTransaction != null) dbTransaction.rollback();
      error.printStackTrace();
    } finally {
      session.close();
    }
  }
  /**
   * Get all of the user reading list from the database
   *
   * @return all of the user reading lsit
   */
  @Override
  public List<UserReadingList> getAllUserReadingList() {
    Session session = SessionFactoryProvider.getSessionFactory().openSession();
    Transaction tx = null;
    List<UserReadingList> allUsersReadingList = new ArrayList<UserReadingList>();

    try {
      tx = session.beginTransaction();
      Criteria criteria = session.createCriteria(UserReadingList.class);
      List<UserReadingList> readingLists = criteria.list();

      for (UserReadingList u : readingLists) {
        allUsersReadingList.add(u);
      }

      tx.commit();
    } catch (HibernateException e) {
      if (tx != null) {
        log.error(e);
      }
    } finally {
      session.close();
    }
    return allUsersReadingList;
  }
 @Override
 public int addRole(Role role) {
   Session session = SessionFactoryProvider.getSessionFactory().openSession();
   Transaction tx = null;
   Integer id = null;
   try {
     tx = session.beginTransaction();
     id = (Integer) session.save(role);
     tx.commit();
     log.info("Added role: " + role + " with id of: " + id);
   } catch (HibernateException e) {
     if (tx != null) tx.rollback();
     log.error(e);
   } finally {
     session.close();
   }
   return id;
 }
  /**
   * Add a new user reading list
   *
   * @param userList the list to be added
   * @return the reading list id
   */
  @Override
  public int addUserReadingList(UserReadingList userList) {
    Session session = SessionFactoryProvider.getSessionFactory().openSession();
    Transaction tx = null;
    Integer id = 0;

    try {
      tx = session.beginTransaction();
      id = (Integer) session.save(userList);
      tx.commit();
      log.info("Added user: " + id);
    } catch (HibernateException e) {
      if (tx != null) tx.rollback();
      log.error(e);
    } finally {
      session.close();
      return id;
    }
  }
  @Override
  public void deleteRole(Role role) {

    Session session = SessionFactoryProvider.getSessionFactory().openSession();
    Transaction tx = null;

    try {
      tx = session.beginTransaction();
      session.delete(role);
      tx.commit();
    } catch (HibernateException e) {
      if (tx != null) {
        tx.rollback();
      }
      e.printStackTrace();
    } finally {
      session.close();
    }
  }
  /**
   * Delete a user readinglist
   *
   * @param userList the list to be deleted
   */
  @Override
  public void deleteUserReadingList(UserReadingList userList) {

    Session session = SessionFactoryProvider.getSessionFactory().openSession();
    Transaction tx = null;

    try {
      tx = session.beginTransaction();
      session.delete(userList);
      tx.commit();
    } catch (HibernateException e) {
      if (tx != null) {
        tx.rollback();
      }
      log.error(e);
    } finally {
      session.close();
    }
  }
  public Boolean deleteStaff(StaffEntity staff) {
    Session session = SessionFactoryProvider.getSessionFactory().openSession();
    Transaction dbTransaction = null;

    try {
      dbTransaction = session.beginTransaction();
      StaffEntity staffToDelete = (StaffEntity) session.get(StaffEntity.class, staff.getStaffId());
      session.delete(staffToDelete);
      dbTransaction.commit();

    } catch (HibernateException error) {
      if (dbTransaction != null) dbTransaction.rollback();
      error.printStackTrace();
      return false;
    } finally {
      session.close();
    }

    return true;
  }
示例#11
0
  public Integer createGameSession(GameSession record) {

    Session session = SessionFactoryProvider.getSessionFactory().openSession();
    Transaction tx = null;
    Integer sessionId = null;

    try {

      // First ensuring that the database isn't full with a max record limit of 10 per word list.
      List<GameSession> records = getGameSessionsForWordList(record.getListId());
      if (records.size() == 10) {

        Random random = new Random();
        deleteOlderGameSession(records.get(random.nextInt(10)).getSessionId());
      }

      // Re-validating to ensure that the changes went through.
      records = getGameSessionsForWordList(record.getListId());
      if ((records.isEmpty()) || (records.size() < 10)) {

        tx = session.beginTransaction();
        sessionId = (Integer) session.save(record);
        tx.commit();
      }

    } catch (HibernateException hex) {

      if (tx != null) {

        tx.rollback();
      }

      hex.printStackTrace();

    } finally {

      session.close();
    }

    return sessionId;
  }
  public int addStaff(StaffEntity staff) {

    Session session = SessionFactoryProvider.getSessionFactory().openSession();
    Transaction dbTransaction = null;
    Integer staffId = null;

    try {
      dbTransaction = session.beginTransaction();
      staffId = (Integer) session.save(staff);
      dbTransaction.commit();

      System.out.println("Added Staff with id of: " + staffId);

    } catch (HibernateException error) {
      if (dbTransaction != null) dbTransaction.rollback();
      log.error(error);
    } finally {
      session.close();
    }
    return staffId;
  }
示例#13
0
  public List<WordList> getAllWordLists() {

    Session session = SessionFactoryProvider.getSessionFactory().openSession();
    Transaction tx = null;
    List<WordList> records = new ArrayList<WordList>();

    try {

      tx = session.beginTransaction();
      records = (ArrayList<WordList>) session.createQuery("from WordList").list();

    } catch (HibernateException hex) {

      hex.printStackTrace();

    } finally {

      session.close();
    }

    return records;
  }
  public StaffEntity getStaffById(Integer staffId) {
    Session session = SessionFactoryProvider.getSessionFactory().openSession();
    List<StaffEntity> allStaff = new ArrayList<StaffEntity>();
    Transaction dbTransaction = null;

    String query = ("FROM StaffEntity A WHERE A.staffId = " + staffId);

    System.out.println("GetApparatusBy Query: " + query);

    try {
      dbTransaction = session.beginTransaction();
      allStaff = session.createQuery(query).list();

    } catch (HibernateException error) {
      if (dbTransaction != null) dbTransaction.rollback();
      error.printStackTrace();
    } finally {
      session.close();
    }

    return allStaff.get(0);
  }
示例#15
0
  public void deleteWordList(int listId) {

    Session session = SessionFactoryProvider.getSessionFactory().openSession();
    Transaction tx = null;
    String filePath = getWordListById(listId).getFilePath();

    try {

      deleteGameSessionsForWordList(listId);
      tx = session.beginTransaction();
      Query query = session.createQuery("delete WordList wl where wl.listId = :listId");
      query.setString("listId", String.valueOf(listId));
      query.executeUpdate();
      tx.commit();

    } catch (HibernateException hex) {

      if (tx != null) {

        tx.rollback();
      }

      hex.printStackTrace();

    } finally {

      session.close();
    }

    try {

      File file = new File(filePath);
      file.delete();

    } catch (Exception e) {

      e.printStackTrace();
    }
  }
示例#16
0
  public WordList getWordListById(int listId) {

    Session session = SessionFactoryProvider.getSessionFactory().openSession();
    Transaction tx = null;
    WordList record = null;

    try {

      tx = session.beginTransaction();
      Query query = session.createQuery("from WordList wl where wl.listId = :listId");
      query.setString("listId", String.valueOf(listId));
      record = (WordList) query.list().get(0);

    } catch (HibernateException hex) {

      hex.printStackTrace();

    } finally {

      session.close();
    }

    return record;
  }
示例#17
0
  public GameSession getGameSessionById(int sessionId) {

    Session session = SessionFactoryProvider.getSessionFactory().openSession();
    Transaction tx = null;
    GameSession record = null;

    try {

      tx = session.beginTransaction();
      Query query = session.createQuery("from GameSession gs where gs.sessionId = :sessionId");
      query.setString("sessionId", String.valueOf(sessionId));
      record = (GameSession) query.list().get(0);

    } catch (HibernateException hex) {

      hex.printStackTrace();

    } finally {

      session.close();
    }

    return record;
  }
示例#18
0
  public void deleteGameSessionsForWordList(int listId) {

    Session session = SessionFactoryProvider.getSessionFactory().openSession();
    Transaction tx = null;

    try {

      tx = session.beginTransaction();
      Query query = session.createQuery("delete GameSession gs where gs.listId = :listId");
      query.setString("listId", String.valueOf(listId));
      query.executeUpdate();
      tx.commit();

    } catch (HibernateException hex) {

      if (tx != null) {

        tx.rollback();
      }

      hex.printStackTrace();

    } finally {

      session.close();
    }

    try {

      FileUtils.deleteDirectory(new File("C:\\10DashingDigitsDB\\GameSessions\\List" + listId));

    } catch (IOException e) {

      e.printStackTrace();
    }
  }
示例#19
0
  public List<GameSession> getGameSessionsForWordList(int listId) {

    Session session = SessionFactoryProvider.getSessionFactory().openSession();
    Transaction tx = null;
    List<GameSession> records = new ArrayList<GameSession>();

    try {

      tx = session.beginTransaction();
      Query query = session.createQuery("from GameSession gs where gs.listId = :listId");
      query.setString("listId", String.valueOf(listId));
      records = (ArrayList<GameSession>) query.list();

    } catch (HibernateException hex) {

      hex.printStackTrace();

    } finally {

      session.close();
    }

    return records;
  }
  /** opens a new session for a hibernate transaction */
  public void beginSession() {

    session = SessionFactoryProvider.getSessionFactory().openSession();
  }