Biblioteca Java - Rev 9
Subversion Repositories:
(root)/Spring/SpringWebMVC/InventoryHibernate/src/main/java/com/linkscreens/inventory/dao/InventoryDaoImpl.java
package com.linkscreens.inventory.dao;
import com.linkscreens.inventory.entity.FilterParameters;
import com.linkscreens.inventory.entity.InventoryItem;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
*/
@Repository
public class InventoryDaoImpl implements InventoryDao {
private static final Logger LOGGER = LoggerFactory.getLogger(InventoryDaoImpl.class);
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sf){
this.sessionFactory = sf;
}
@Override
public void addNewInventoryItem(InventoryItem item) {
Session session = this.sessionFactory.getCurrentSession();
session.persist(item);
LOGGER.info("Item saved successfully, Item Details=" + item);
}
@Override
public InventoryItem getItemById(Long itemId) {
Session session = this.sessionFactory.getCurrentSession();
InventoryItem item = (InventoryItem) session.load(InventoryItem.class, itemId);
LOGGER.info("Item loaded successfully, Item details=" + item);
return item;
}
@Override
public List<InventoryItem> getAllInventoryItems() {
Session session = this.sessionFactory.getCurrentSession();
Query query = session.createQuery("FROM InventoryItem WHERE deleted IS NULL OR deleted = FALSE ORDER BY cont,anLunaAdaugare");
List<InventoryItem> items = query.list();
return items;
}
@Override
public List<InventoryItem> geFilteredInventoryItems(FilterParameters filter) {
Session session = this.sessionFactory.getCurrentSession();
Query query = session.createQuery("FROM InventoryItem WHERE (deleted IS NULL OR deleted = FALSE) and (anLunaAdaugare >= :start"
+ " AND anLunaAdaugare <= :end) ORDER BY cont,anLunaAdaugare");
query.setParameter("start", filter.getStartYear() + "/" + filter.getStartMonth());
query.setParameter("end", filter.getEndYear() + "/" + filter.getEndMonth());
List<InventoryItem> items = query.list();
return items;
}
@Override
public void updateInventoryItem(InventoryItem updatedItem) {
Session session = this.sessionFactory.getCurrentSession();
session.update(updatedItem);
LOGGER.info("Item updated successfully, Item Details=" + updatedItem);
}
@Override
public void deleteItem(Long itemId) {
Session session = this.sessionFactory.getCurrentSession();
InventoryItem item = (InventoryItem) session.load(InventoryItem.class, itemId);
if (null != item){
item.setDeleted(true);
session.update(item);
}
LOGGER.info("Item deleted successfully, Item details=" + item);
}
}
import com.linkscreens.inventory.entity.FilterParameters;
import com.linkscreens.inventory.entity.InventoryItem;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
*/
@Repository
public class InventoryDaoImpl implements InventoryDao {
private static final Logger LOGGER = LoggerFactory.getLogger(InventoryDaoImpl.class);
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sf){
this.sessionFactory = sf;
}
@Override
public void addNewInventoryItem(InventoryItem item) {
Session session = this.sessionFactory.getCurrentSession();
session.persist(item);
LOGGER.info("Item saved successfully, Item Details=" + item);
}
@Override
public InventoryItem getItemById(Long itemId) {
Session session = this.sessionFactory.getCurrentSession();
InventoryItem item = (InventoryItem) session.load(InventoryItem.class, itemId);
LOGGER.info("Item loaded successfully, Item details=" + item);
return item;
}
@Override
public List<InventoryItem> getAllInventoryItems() {
Session session = this.sessionFactory.getCurrentSession();
Query query = session.createQuery("FROM InventoryItem WHERE deleted IS NULL OR deleted = FALSE ORDER BY cont,anLunaAdaugare");
List<InventoryItem> items = query.list();
return items;
}
@Override
public List<InventoryItem> geFilteredInventoryItems(FilterParameters filter) {
Session session = this.sessionFactory.getCurrentSession();
Query query = session.createQuery("FROM InventoryItem WHERE (deleted IS NULL OR deleted = FALSE) and (anLunaAdaugare >= :start"
+ " AND anLunaAdaugare <= :end) ORDER BY cont,anLunaAdaugare");
query.setParameter("start", filter.getStartYear() + "/" + filter.getStartMonth());
query.setParameter("end", filter.getEndYear() + "/" + filter.getEndMonth());
List<InventoryItem> items = query.list();
return items;
}
@Override
public void updateInventoryItem(InventoryItem updatedItem) {
Session session = this.sessionFactory.getCurrentSession();
session.update(updatedItem);
LOGGER.info("Item updated successfully, Item Details=" + updatedItem);
}
@Override
public void deleteItem(Long itemId) {
Session session = this.sessionFactory.getCurrentSession();
InventoryItem item = (InventoryItem) session.load(InventoryItem.class, itemId);
if (null != item){
item.setDeleted(true);
session.update(item);
}
LOGGER.info("Item deleted successfully, Item details=" + item);
}
}