Example usage for org.hibernate Session saveOrUpdate

List of usage examples for org.hibernate Session saveOrUpdate

Introduction

In this page you can find the example usage for org.hibernate Session saveOrUpdate.

Prototype

void saveOrUpdate(Object object);

Source Link

Document

Either #save(Object) or #update(Object) the given instance, depending upon resolution of the unsaved-value checks (see the manual for discussion of unsaved-value checking).

Usage

From source file:com.floreantpos.model.dao.PayOutTransactionDAO.java

License:Open Source License

public void saveTransaction(PayOutTransaction t, Terminal terminal) throws Exception {
    Session session = null;
    Transaction tx = null;/*  w w  w.j a va  2s  .c  o  m*/

    try {
        session = createNewSession();
        tx = session.beginTransaction();
        session.saveOrUpdate(t);
        session.saveOrUpdate(terminal);
        tx.commit();

    } catch (Exception e) {
        try {
            tx.rollback();
        } catch (Exception x) {
        }
        throw e;
    } finally {
        closeSession(session);
    }
}

From source file:com.floreantpos.model.dao.ShopTableDAO.java

License:Open Source License

public void bookedTables(List<ShopTable> tables) {
    if (tables == null) {
        return;//from  w w  w . j av a  2s.c o  m
    }

    Session session = null;
    Transaction tx = null;

    try {
        session = createNewSession();
        tx = session.beginTransaction();

        for (ShopTable shopTable : tables) {
            shopTable.setBooked(true);
            shopTable.setFree(false);
            session.saveOrUpdate(shopTable);
        }

        tx.commit();
    } catch (Exception e) {
        tx.rollback();
        LogFactory.getLog(ShopTableDAO.class).error(e);
        throw new RuntimeException(e);
    } finally {
        closeSession(session);
    }
}

From source file:com.floreantpos.model.dao.ShopTableDAO.java

License:Open Source License

public void freeTables(List<ShopTable> tables) {
    if (tables == null) {
        return;/*from w  w w .j  a v  a2s  .  co m*/
    }

    Session session = null;
    Transaction tx = null;

    try {
        session = createNewSession();
        tx = session.beginTransaction();

        for (ShopTable shopTable : tables) {
            shopTable.setBooked(false);
            shopTable.setFree(true);
            session.saveOrUpdate(shopTable);
        }

        tx.commit();
    } catch (Exception e) {
        tx.rollback();
        LogFactory.getLog(ShopTableDAO.class).error(e);
        throw new RuntimeException(e);
    } finally {
        closeSession(session);
    }
}

From source file:com.floreantpos.model.dao.TerminalDAO.java

License:Open Source License

public void performBatchSave(Object... objects) {
    Session session = null;
    Transaction tx = null;/*w  ww . ja v a 2s  .  co  m*/

    try {
        session = createNewSession();
        tx = session.beginTransaction();
        for (Object object : objects) {
            if (object != null) {
                session.saveOrUpdate(object);
            }
        }
        tx.commit();
    } catch (Exception e) {
        try {
            tx.rollback();
        } catch (Exception x) {
        }
        throw e;
    } finally {
        closeSession(session);
    }
}

From source file:com.floreantpos.model.dao.TicketDAO.java

License:Open Source License

@Override
public void saveOrUpdate(Ticket ticket, Session session) {
    //TODO: INVENTORY PLUGIN SUPPORT
    adjustInventoryItems(session, ticket);

    ticket.setActiveDate(Calendar.getInstance().getTime());

    adjustStockAmount(ticket, session);//from   w  ww . ja  v  a 2 s.  co m
    session.saveOrUpdate(ticket);

    ticket.clearDeletedItems();

    DataUpdateInfo lastUpdateInfo = DataUpdateInfoDAO.getLastUpdateInfo();
    lastUpdateInfo.setLastUpdateTime(new Date());
    session.update(lastUpdateInfo);
}

From source file:com.floreantpos.model.dao.TicketDAO.java

License:Open Source License

private void adjustInventoryItems(Session session, Ticket ticket) {
    List<TicketItem> ticketItems = ticket.getTicketItems();
    if (ticketItems == null) {
        return;/*  w  w  w .  j  a va  2s  .c  o  m*/
    }

    for (TicketItem ticketItem : ticketItems) {
        if (ticketItem.isInventoryHandled()) {
            continue;
        }

        Integer menuItemId = ticketItem.getItemId();
        MenuItem menuItem = MenuItemDAO.getInstance().get(menuItemId);

        if (menuItem == null) {
            continue;
        }

        Recepie recepie = menuItem.getRecepie();

        if (recepie == null) {
            continue;
        }

        List<RecepieItem> recepieItems = recepie.getRecepieItems();
        for (RecepieItem recepieItem : recepieItems) {
            if (!recepieItem.isInventoryDeductable()) {
                continue;
            }

            Double percentage = recepieItem.getPercentage() / 100.0;

            InventoryItem inventoryItem = recepieItem.getInventoryItem();
            //            inventoryItem.setTotalPackages(inventoryItem.getTotalPackages() - ticketItem.getItemCount());
            Double totalRecepieUnits = inventoryItem.getTotalRecepieUnits();
            inventoryItem.setTotalRecepieUnits(totalRecepieUnits - (ticketItem.getItemCount() * percentage));

            session.saveOrUpdate(inventoryItem);

            InventoryTransaction transaction = new InventoryTransaction();
            transaction.setType(InventoryTransactionType.OUT);
            transaction.setUnitPrice(inventoryItem.getUnitSellingPrice());
            transaction.setInventoryItem(inventoryItem);
            transaction.setQuantity(ticketItem.getItemCount());
            transaction.setRemark(Messages.getString("TicketDAO.0") + ticketItem.getName() //$NON-NLS-1$
                    + Messages.getString("TicketDAO.11") + ticket.getId()); //$NON-NLS-1$

            session.save(transaction);
        }

        ticketItem.setInventoryHandled(true);
    }

    List deletedItems = ticket.getDeletedItems();
    if (deletedItems == null) {
        return;
    }

    for (Object o : deletedItems) {
        if (!(o instanceof TicketItem)) {
            continue;
        }

        TicketItem ticketItem = (TicketItem) o;

        if (!ticketItem.isInventoryHandled()) {
            continue;
        }

        Integer menuItemId = ticketItem.getItemId();
        MenuItem menuItem = MenuItemDAO.getInstance().get(menuItemId);
        Recepie recepie = menuItem.getRecepie();

        if (recepie == null) {
            return;
        }

        List<RecepieItem> recepieItems = recepie.getRecepieItems();
        for (RecepieItem recepieItem : recepieItems) {
            if (!recepieItem.isInventoryDeductable()) {
                continue;
            }

            InventoryItem inventoryItem = recepieItem.getInventoryItem();
            inventoryItem.setTotalPackages(inventoryItem.getTotalPackages() + ticketItem.getItemCount());
            Double totalRecepieUnits = inventoryItem.getTotalRecepieUnits();
            inventoryItem.setTotalRecepieUnits(totalRecepieUnits + ticketItem.getItemCount());

            session.saveOrUpdate(inventoryItem);

            InventoryTransaction transaction = new InventoryTransaction();
            transaction.setType(InventoryTransactionType.IN);
            transaction.setUnitPrice(inventoryItem.getUnitSellingPrice());
            transaction.setInventoryItem(inventoryItem);
            transaction.setQuantity(ticketItem.getItemCount());
            transaction.setRemark(Messages.getString("TicketDAO.1") + ticketItem.getName() //$NON-NLS-1$
                    + " was canceled for ticket " + ticket.getId()); //$NON-NLS-1$

            session.save(transaction);
        }

        ticketItem.setInventoryHandled(true);
    }
}

From source file:com.floreantpos.model.dao.TicketDAO.java

License:Open Source License

private void adjustStockAmount(Ticket ticket, Session session) {

    List<TicketItem> items = ticket.getTicketItems();

    HashMap<Integer, Double> itemMap = new LinkedHashMap<Integer, Double>();

    if (!getAdjustedMap(items, itemMap)) {
        return;//from  w  w w . jav a2 s .c om
    }

    for (Integer ticketItemId : itemMap.keySet()) {

        MenuItem menuItem = MenuItemDAO.getInstance().get(ticketItemId);

        if (menuItem == null) {
            continue;
        }

        if (menuItem.getStockAmount() <= 0) {
            continue;
        }

        double availableStockAmount;
        double stockAmount = menuItem.getStockAmount();

        if (menuItem.isFractionalUnit()) {
            availableStockAmount = stockAmount - itemMap.get(ticketItemId);
        } else {
            availableStockAmount = stockAmount - itemMap.get(ticketItemId);
        }
        menuItem.setStockAmount(availableStockAmount);

        session.saveOrUpdate(menuItem);
    }
}

From source file:com.floreantpos.model.dao.UserDAO.java

License:Open Source License

public void saveClockIn(User user, AttendenceHistory attendenceHistory, Shift shift, Calendar currentTime) {
    Session session = null;
    Transaction tx = null;//  ww  w.j a v a 2s  .c  om

    try {
        session = getSession();
        tx = session.beginTransaction();

        session.saveOrUpdate(user);
        session.saveOrUpdate(attendenceHistory);

        tx.commit();
    } catch (Exception e) {
        PosLog.error(getClass(), e);

        if (tx != null) {
            try {
                tx.rollback();
            } catch (Exception x) {
            }
        }
        throw new PosException(Messages.getString("UserDAO.2"), e); //$NON-NLS-1$

    } finally {
        if (session != null) {
            closeSession(session);
        }
    }
}

From source file:com.floreantpos.model.dao.UserDAO.java

License:Open Source License

public void saveClockOut(User user, AttendenceHistory attendenceHistory, Shift shift, Calendar currentTime) {
    Session session = null;
    Transaction tx = null;//from  w w  w .  j  a  v  a2 s .  c  om

    try {
        session = getSession();
        tx = session.beginTransaction();

        session.saveOrUpdate(user);
        session.saveOrUpdate(attendenceHistory);

        tx.commit();
    } catch (Exception e) {
        if (tx != null) {
            try {
                tx.rollback();
            } catch (Exception x) {
            }
        }
        throw new PosException(Messages.getString("UserDAO.3"), e); //$NON-NLS-1$

    } finally {
        if (session != null) {
            closeSession(session);
        }
    }
}

From source file:com.floreantpos.model.dao.UserDAO.java

License:Open Source License

public void saveDriverOut(User user, EmployeeInOutHistory attendenceHistory, Shift shift,
        Calendar currentTime) {/*from  ww  w .  jav  a 2 s.  com*/
    Session session = null;
    Transaction tx = null;

    try {
        session = getSession();
        tx = session.beginTransaction();

        session.saveOrUpdate(user);
        session.saveOrUpdate(attendenceHistory);

        tx.commit();
    } catch (Exception e) {
        PosLog.error(getClass(), e);

        if (tx != null) {
            try {
                tx.rollback();
            } catch (Exception x) {
            }
        }
        throw new PosException(Messages.getString("UserDAO.2"), e); //$NON-NLS-1$

    } finally {
        if (session != null) {
            closeSession(session);
        }
    }
}