Ejemplo n.º 1
0
  public boolean createUser(User user) {
    boolean result = false;
    if (user.getEmployeeId() != null) {
      User userById = getUserByEmployeeId(user.getEmployeeId());
      if (userById != null) {
        MessageUtils.createMessage("Brugeren eksisterer allerede.");
        return false;
      }
    }
    Session session = SessionFactoryUtil.getInstance().getCurrentSession();
    Transaction transaction = session.beginTransaction();
    try {
      session.saveOrUpdate(user);
      result = true;
      session.flush();
      transaction.commit();
    } catch (StaleObjectStateException sose) {
      System.out.println("StaleObjectException");
      transaction.rollback();
    } catch (HibernateException e) {
      e.printStackTrace();
      transaction.rollback();
      result = false;
    } finally {
      if (session.isOpen()) {
        session.close();
      }
    }

    return result;
  }
Ejemplo n.º 2
0
  public List<Team> getTeams() {
    Session session = SessionFactoryUtil.getInstance().getCurrentSession();
    List result = null;
    try {
      Transaction transaction = session.beginTransaction();
      result = session.createQuery("from Team").list();
      session.flush();
      transaction.commit();
    } catch (HibernateException e) {
      e.printStackTrace();
    } finally {
      if (session.isOpen()) {
        session.close();
      }
    }

    List<Team> teams = new ArrayList<Team>();
    for (Object o : result) {
      if (o instanceof Team) {
        teams.add((Team) o);
      }
    }

    return teams;
  }
Ejemplo n.º 3
0
  public ClassificationValue getClassification(String value) {
    ClassificationValue result = null;
    Session session = SessionFactoryUtil.getInstance().getCurrentSession();
    try {
      Transaction transaction = session.beginTransaction();

      Object o =
          session
              .createQuery(
                  "from ClassificationValue as classificationValue where classificationValue.value like '"
                      + value
                      + "'")
              .uniqueResult();

      transaction.commit();
      if (o != null) {
        if (o instanceof ClassificationValue) {
          result = (ClassificationValue) o;
        }
      }
    } finally {
      if (session.isOpen()) {
        session.flush();
        session.disconnect();
        session.close();
      }
    }
    return result;
  }
Ejemplo n.º 4
0
  public void saveEvent(Event event) {
    Event event1 = getEvent(event);

    Session session = SessionFactoryUtil.getInstance().getCurrentSession();
    try {
      if (event1 == null) {
        Transaction transaction = session.beginTransaction();
        try {
          session.save(event);
          transaction.commit();
        } catch (HibernateException e) {
          transaction.rollback();
          e.printStackTrace();
        }
      } else {
        Transaction transaction = session.beginTransaction();
        try {
          session.update(event);
          transaction.commit();
        } catch (HibernateException e) {
          transaction.rollback();
          e.printStackTrace();
        }
      }
    } finally {
      if (session.isOpen()) {
        session.flush();
        session.disconnect();
        session.close();
      }
    }
  }
Ejemplo n.º 5
0
 public List<Activity> getActivities(Event parent) {
   List<Activity> result = new ArrayList<Activity>();
   Session session = SessionFactoryUtil.getInstance().getCurrentSession();
   try {
     Transaction transaction = session.beginTransaction();
     Object event =
         session
             .createQuery("from Event as event where event.idevent= " + parent.getIdevent())
             .uniqueResult();
     transaction.commit();
     if (event != null) {
       Event eventEntity = (Event) event;
       Collection<Activity> activityList = eventEntity.getActivities();
       for (Activity activity : activityList) {
         result.add(activity);
       }
     }
   } finally {
     if (session.isOpen()) {
       session.flush();
       session.disconnect();
       session.close();
     }
   }
   return result;
 }
Ejemplo n.º 6
0
  public Location getLocation(String s) {
    Location result = null;
    Session session = SessionFactoryUtil.getInstance().getCurrentSession();
    try {
      Transaction transaction = session.beginTransaction();
      Object o =
          session
              .createQuery("from Location as location where location.name like '" + s + "'")
              .uniqueResult();

      transaction.commit();
      if (o != null) {
        if (o instanceof Location) {
          result = (Location) o;
        }
      }
    } finally {
      if (session.isOpen()) {
        session.flush();
        session.disconnect();
        session.close();
      }
    }
    return result;
  }
  /** {@inheritDoc} */
  @Override
  public void txEnd(GridCacheTx tx, boolean commit) throws GridException {
    init();

    Session ses = tx.removeMeta(ATTR_SES);

    if (ses != null) {
      Transaction hTx = ses.getTransaction();

      if (hTx != null) {
        try {
          if (commit) {
            ses.flush();

            hTx.commit();
          } else hTx.rollback();

          if (log.isDebugEnabled())
            log.debug("Transaction ended [xid=" + tx.xid() + ", commit=" + commit + ']');
        } catch (HibernateException e) {
          throw new GridException(
              "Failed to end transaction [xid=" + tx.xid() + ", commit=" + commit + ']', e);
        } finally {
          ses.close();
        }
      }
    }
  }
Ejemplo n.º 8
0
  @Override
  public ArrayList<User> getAllUsers() throws Exception {
    dLog.info("Entering method getAllUsers");
    ArrayList<User> result = new ArrayList<User>();
    Session session = null;

    try {
      session = getSession();
      session.clear();
      Transaction tranx = session.beginTransaction();

      String hql = "select from User";
      Query query = session.createQuery(hql);
      List<?> list = query.list();

      for (int n = 0; n < list.size(); n++) {
        result.add((User) list.get(n));
      }

      tranx.commit();
      session.flush();
      session.evict(User.class);
    } catch (Exception e) {
      dLog.error("Exception in getAllUsers", e);
    } finally {
      // ensure that session is close regardless of the errors in try/catch
      if (session != null) {
        session.close();
      }
    }

    return result;
  }
Ejemplo n.º 9
0
  public FacturaAlmacen actualiza(FacturaAlmacen otraFactura, Usuario usuario)
      throws NoEstaAbiertaException {
    FacturaAlmacen factura =
        (FacturaAlmacen) currentSession().get(FacturaAlmacen.class, otraFactura.getId());
    switch (factura.getEstatus().getNombre()) {
      case Constantes.ABIERTA:
        Session session = currentSession();
        factura.setVersion(otraFactura.getVersion());
        factura.setFecha(otraFactura.getFecha());
        factura.setComentarios(otraFactura.getComentarios());
        factura.setIva(otraFactura.getIva());
        factura.setTotal(otraFactura.getTotal());
        factura.setCliente(otraFactura.getCliente());
        Date fecha = new Date();
        factura.setFechaModificacion(fecha);
        session.update(factura);

        audita(factura, usuario, Constantes.ACTUALIZAR, fecha);

        session.flush();
        return factura;
      default:
        throw new NoEstaAbiertaException("No se puede actualizar una factura que no este abierta");
    }
  }
Ejemplo n.º 10
0
  public Location getLocation(int id) {
    Location result = null;
    Session session = SessionFactoryUtil.getInstance().getCurrentSession();
    try {
      Transaction transaction = session.beginTransaction();
      Object o =
          session
              .createQuery("from Location as location where location.idlocation = " + id)
              .uniqueResult();

      transaction.commit();
      if (o != null) {
        if (o instanceof Location) {
          Location location = (Location) o;
          result = new Location();
          result.setName(location.getName());
          result.setIdlocation(location.getIdlocation());
        }
      }
    } finally {
      if (session.isOpen()) {
        session.flush();
        session.disconnect();
        session.close();
      }
    }
    return result;
  }
Ejemplo n.º 11
0
 public void guardaActualiza(TbAplicativo tbAplicativo) {
   try {
     iniciaOperacion();
     sesion.saveOrUpdate(tbAplicativo);
     tx.commit();
     sesion.flush();
   } catch (HibernateException he) {
     manejaExcepcion(he);
     throw he;
   } finally {
     sesion.close();
   }
 }
Ejemplo n.º 12
0
 public void eliminar(TbAplicativo tbAplicativo) {
   try {
     iniciaOperacion();
     sesion.delete(tbAplicativo);
     tx.commit();
     sesion.flush();
   } catch (HibernateException he) {
     manejaExcepcion(he);
     throw he;
   } finally {
     sesion.close();
   }
 }
Ejemplo n.º 13
0
 public void deleteObject(Object o) {
   Session session = SessionFactoryUtil.getInstance().getCurrentSession();
   try {
     Transaction transaction = session.beginTransaction();
     session.delete(o);
     session.flush();
     transaction.commit();
   } finally {
     if (session.isOpen()) {
       session.close();
     }
   }
 }
Ejemplo n.º 14
0
 public void updateObject(Object entity) {
   Session session = SessionFactoryUtil.getInstance().getCurrentSession();
   try {
     Transaction transaction = session.beginTransaction();
     session.update(entity);
     session.flush();
     transaction.commit();
   } finally {
     if (session.isOpen()) {
       session.disconnect();
       session.close();
     }
   }
 }
Ejemplo n.º 15
0
  public void deletePartnerRequest(ActivityPartnerRequest activityPartnerRequest) {
    Session session = SessionFactoryUtil.getInstance().getCurrentSession();
    try {
      Transaction transaction = session.beginTransaction();
      session.delete(activityPartnerRequest);
      session.flush();
      transaction.commit();

    } finally {
      if (session.isOpen()) {
        session.disconnect();
        session.close();
      }
    }
  }
Ejemplo n.º 16
0
  public static void main(String[] args) {

    Session session = null;
    try {

      session = HibernateUtility.getSession();
      // Create new instance of Contact and set values in it by reading them from form object
      System.out.println("Inserting Record");
      Transaction tx = session.beginTransaction();

      Contact contact1 = new Contact();
      contact1.setId(1L);
      contact1.setFirstName("poornima");
      contact1.setLastName("baby");
      contact1.setEmail("*****@*****.**");
      session.save(contact1);

      Contact contact2 = new Contact();
      contact2.setId(2L);
      contact2.setFirstName("Raj");
      contact2.setLastName("Kapoor");
      contact2.setEmail("*****@*****.**");
      session.save(contact2);

      Contact contact3 = new Contact();
      contact3.setId(3l);
      contact3.setFirstName("Boney");
      contact3.setLastName("Kapoor");
      contact3.setEmail("*****@*****.**");
      session.save(contact3);

      Contact contact4 = new Contact();
      contact4.setId(4l);
      contact4.setFirstName("Raj");
      contact4.setLastName("babbar");
      contact4.setEmail("*****@*****.**");
      session.save(contact4);
      tx.commit();
      System.out.println("Done");
    } catch (Exception e) {
      // tx.rollback();
      System.out.println(e.getMessage());
    } finally {
      // Actual contact insertion will happen at this step
      session.flush();
      session.close();
    }
  }
Ejemplo n.º 17
0
 public List<Event> getEvents() {
   Session session = SessionFactoryUtil.getInstance().getCurrentSession();
   List<Event> result = new ArrayList<Event>();
   try {
     Transaction transaction = session.beginTransaction();
     List list = session.createQuery("from Event").list();
     transaction.commit();
     for (Object o : list) {
       result.add((Event) o);
     }
   } finally {
     if (session.isOpen()) {
       session.flush();
       session.disconnect();
       session.close();
     }
   }
   return result;
 }
Ejemplo n.º 18
0
  public FacturaAlmacen crea(FacturaAlmacen factura, Usuario usuario) {
    Session session = currentSession();
    if (usuario != null) {
      factura.setAlmacen(usuario.getAlmacen());
    }
    Query query = currentSession().createQuery("select e from Estatus e where e.nombre = :nombre");
    query.setString("nombre", Constantes.ABIERTA);
    Estatus estatus = (Estatus) query.uniqueResult();
    factura.setEstatus(estatus);
    factura.setFolio(getFolioTemporal(factura.getAlmacen()));
    Date fecha = new Date();
    factura.setFechaCreacion(fecha);
    factura.setFechaModificacion(fecha);
    session.save(factura);

    audita(factura, usuario, Constantes.CREAR, fecha);

    session.flush();
    return factura;
  }
Ejemplo n.º 19
0
  public List<Activity> getActivities() {
    List<Activity> result = new ArrayList<Activity>();
    Session session = SessionFactoryUtil.getInstance().getCurrentSession();
    try {
      Transaction transaction = session.beginTransaction();
      List list = session.createQuery("from Activity").list();
      transaction.commit();

      for (Object o : list) {
        Activity activity = (Activity) o;
        result.add(activity);
      }
    } finally {
      if (session.isOpen()) {
        session.flush();
        session.disconnect();
        session.close();
      }
    }
    return result;
  }
  /** {@inheritDoc} */
  @Override
  public void onSessionEnd(CacheStoreSession ses, boolean commit) {
    Session hibSes = ses.attach(null);

    if (hibSes != null) {
      try {
        Transaction tx = hibSes.getTransaction();

        if (commit) {
          hibSes.flush();

          if (tx.isActive()) tx.commit();
        } else if (tx.isActive()) tx.rollback();
      } catch (HibernateException e) {
        throw new CacheWriterException(
            "Failed to end store session [tx=" + ses.transaction() + ']', e);
      } finally {
        hibSes.close();
      }
    }
  }
Ejemplo n.º 21
0
  public List<ClassificationType> getClassificationTypes() {
    List<ClassificationType> result = new ArrayList<ClassificationType>();
    Session session = SessionFactoryUtil.getInstance().getCurrentSession();
    try {
      Transaction transaction = session.beginTransaction();
      List list = session.createQuery("from ClassificationType").list();
      transaction.commit();

      if (list != null) {
        for (Object o : list) {
          ClassificationType classificationType = (ClassificationType) o;
          result.add(classificationType);
        }
      }
    } finally {
      if (session.isOpen()) {
        session.flush();
        session.disconnect();
        session.close();
      }
    }
    return result;
  }
Ejemplo n.º 22
0
 public Location getLocation(Location location) {
   Session session = SessionFactoryUtil.getInstance().getCurrentSession();
   try {
     Transaction transaction = session.beginTransaction();
     Object o =
         session
             .createQuery(
                 "from Location as location where location.idlocation = "
                     + location.getIdlocation())
             .uniqueResult();
     transaction.commit();
     if (o != null) {
       return (Location) o;
     }
   } finally {
     if (session.isOpen()) {
       session.flush();
       session.disconnect();
       session.close();
     }
   }
   return null;
 }
Ejemplo n.º 23
0
  public List<Location> getLocations() {
    List<Location> result = new ArrayList<Location>();
    Session session = SessionFactoryUtil.getInstance().getCurrentSession();
    try {
      Transaction transaction = session.beginTransaction();
      List list = session.createQuery("from Location").list();
      transaction.commit();

      if (list != null) {
        for (Object o : list) {
          Location location = (Location) o;
          result.add(location);
        }
      }
    } finally {
      if (session.isOpen()) {
        session.flush();
        session.disconnect();
        session.close();
      }
    }
    return result;
  }
Ejemplo n.º 24
0
 public Activity getActivity(Activity activity) {
   Activity result = null;
   Session session = SessionFactoryUtil.getInstance().getCurrentSession();
   try {
     Transaction transaction = session.beginTransaction();
     Object o =
         session
             .createQuery(
                 "from Activity as activity where activity.idactivity= "
                     + activity.getIdactivity())
             .uniqueResult();
     transaction.commit();
     if (o != null) {
       result = (Activity) o;
     }
   } finally {
     if (session.isOpen()) {
       session.flush();
       session.disconnect();
       session.close();
     }
   }
   return result;
 }
Ejemplo n.º 25
0
  public boolean saveUser(User user) {
    boolean result = false;

    Session session = SessionFactoryUtil.getInstance().getCurrentSession();
    Transaction transaction = session.beginTransaction();
    try {
      session.update(user);
      result = true;
      session.flush();
      transaction.commit();
    } catch (StaleObjectStateException sose) {
      System.out.println("StaleObjectException");
      transaction.rollback();
    } catch (HibernateException e) {
      e.printStackTrace();
      transaction.rollback();
      result = false;
    } finally {
      if (session.isOpen()) {
        session.close();
      }
    }
    return result;
  }