/**
  * Creating all new Applies received as an argument
  *
  * @param app value of Applies
  */
 public void createApply(ArrayList<Apply> app) {
   sessionFactory = HibernateUtil.configureSessionFactory();
   session = sessionFactory.openSession();
   tx = session.beginTransaction();
   for (Apply a : app) session.persist(a);
   tx.commit();
   session.close();
 }
  /**
   * Creating a new environment
   *
   * @param env value of the environment
   */
  public void createEnvironment(Environment env) {
    sessionFactory = HibernateUtil.configureSessionFactory();
    session = sessionFactory.openSession();
    tx = session.beginTransaction();

    session.persist(env);
    tx.commit();
    session.close();
  }
  /**
   * Creating all new Expressions send as an argument
   *
   * @param exp value of Expressions
   */
  public void createExpression(ArrayList<Expression> exp) {
    sessionFactory = HibernateUtil.configureSessionFactory();
    session = sessionFactory.openSession();
    tx = session.beginTransaction();

    for (Expression e : exp) session.persist(e);
    tx.commit();
    session.close();
  }
  /**
   * Creating a new Condition
   *
   * @param cond value of Condition
   */
  public void createCondition(Condition cond) {
    sessionFactory = HibernateUtil.configureSessionFactory();
    session = sessionFactory.openSession();
    tx = session.beginTransaction();

    session.persist(cond);
    tx.commit();
    session.close();
  }
  /**
   * Creating a new environment
   *
   * @param description description of the environment
   * @param environment value of the environment
   */
  public void createEnvironment(String description, String environment) {
    sessionFactory = HibernateUtil.configureSessionFactory();
    session = sessionFactory.openSession();
    tx = session.beginTransaction();

    Environment env = new Environment(environment, description);
    session.persist(env);
    tx.commit();
    session.close();
  }
  /**
   * Fetching list of environment elements from database
   *
   * @return list of environment elements
   */
  @SuppressWarnings("unchecked")
  public List<Environment> selectEnvironment() {
    sessionFactory = HibernateUtil.configureSessionFactory();
    session = sessionFactory.openSession();
    tx = session.beginTransaction();
    query = session.createQuery("from Environment");
    List<Environment> env = query.list();

    tx.commit();
    session.close();
    return env;
  }
  /**
   * Deleting an environment
   *
   * @param pkEnvironment primary key of the environment
   */
  public void deleteEnvironment(Long pkEnvironment) {

    sessionFactory = HibernateUtil.configureSessionFactory();
    session = sessionFactory.openSession();
    tx = session.beginTransaction();

    query = session.createQuery("from Environment e where e.pkEnvironment = " + pkEnvironment);
    Environment env = (Environment) query.uniqueResult();

    session.delete(env);
    tx.commit();
    session.close();
  }
  public List<Resource> getResource() {
    sessionFactory = HibernateUtil.configureSessionFactory();
    session = sessionFactory.openSession();
    tx = session.beginTransaction();

    Query query = session.createQuery("from Resource");
    @SuppressWarnings("unchecked")
    List<Resource> res = query.list();

    tx.commit();
    session.close();
    return res;
  }
  public List<Action> getAction() {
    sessionFactory = HibernateUtil.configureSessionFactory();
    session = sessionFactory.openSession();
    tx = session.beginTransaction();

    Query query = session.createQuery("from Action");
    @SuppressWarnings("unchecked")
    List<Action> act = query.list();

    tx.commit();
    session.close();
    return act;
  }
  public List<Subject> getSubject() {
    sessionFactory = HibernateUtil.configureSessionFactory();
    session = sessionFactory.openSession();
    tx = session.beginTransaction();

    Query query = session.createQuery("from Subject");
    @SuppressWarnings("unchecked")
    List<Subject> subj = query.list();

    tx.commit();
    session.close();
    return subj;
  }
  /**
   * Deleting a Condition
   *
   * @param cond value of condition
   */
  public void deleteCondition(Condition cond) {
    sessionFactory = HibernateUtil.configureSessionFactory();
    session = sessionFactory.openSession();
    tx = session.beginTransaction();

    Expression testApply = cond.getExpression();

    session.delete(testApply);
    // session.delete(cond);

    tx.commit();
    session.close();
  }
  /**
   * Updating an environment
   *
   * @param pkEenvironment primary key of the environment
   * @param description description of the environment
   * @param environment value of the environment
   */
  public void updateEnvironment(Long pkEnvironment, String description, String environment) {
    sessionFactory = HibernateUtil.configureSessionFactory();
    session = sessionFactory.openSession();
    tx = session.beginTransaction();

    query = session.createQuery("from Environment e where e.pkEnvironment =" + pkEnvironment);
    Environment env = (Environment) query.uniqueResult();
    env.setDescription(description);
    env.setEnvironmentName(environment);

    session.persist(env);
    tx.commit();
    session.close();
  }
  /**
   * Creating a new environment
   *
   * @param description description of the environment
   * @param environment value of the environment
   * @param envAttr list of environment attributes
   */
  public void createEnvironment(
      String description, String environment, List<EnvironmentAttribute> envAttr) {
    sessionFactory = HibernateUtil.configureSessionFactory();
    session = sessionFactory.openSession();
    tx = session.beginTransaction();

    Environment env = new Environment(environment, description);

    env.setEnvironmentAttributes(new HashSet<EnvironmentAttribute>(envAttr));
    session.persist(env);

    for (EnvironmentAttribute attr : envAttr) {
      attr.setEnvironment(env);
      session.persist(attr);

      for (EnvAttrValues val : attr.getEnvAttrValues()) {
        val.setEnvironmentAttribute(attr);
        session.persist(val);
      }
    }
    tx.commit();
    session.close();
  }