untuk membuat project tersebut pastikan kita punya tools text editor
untuk membangun project tersebut, disini saya menggunakan STS (Spring
Tools Suite) dan sediakan juga untuk aplikasi xampp untuk membuat
database dari project tersebut.
langsung saja pertama buatkan sebuah database dari project Inventori, setelah itu disini saya punya project tapi belum sepenuhnya berjalan, jika teman-teman menginginkan projectnya bisa langsung hubungi saya di suryaputraharas@gmail.com.
setelah itu lakukan import project inventorynya, pastikan project tersebut sudah di extract di tempat penyimpanan workspace nya. lalu lakukan import project maven dengan cara klik file–>import–>general lalu pilih maven project–> klikm ok.
jika sudah di import lakukan konfigurasi pada project tersebut di file propertis hibernate seperti ini :
setelah itu konfigurasi lagi di bagian maven build dengan mengisikan perintah di bawah ini :
setelah itu masukkan koding di setiap package nya, tapi project yang saya sudah buat ini sudah ada kodingnya, tapi jika teman-teman ingin membuat dari awal bisa memasukkan koding di bawah ini tiap package yang ada :
/**
* ItemController.java
*
* Created on May 10, 2011 2:19:09 PM
*/
package org.mycompany.controller;
import org.blueoxygen.cimande.commons.CimandeModuleAction;
import org.mycompany.entity.Item;
import org.mycompany.service.CategoryService;
import org.mycompany.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import com.opensymphony.xwork2.validator.annotations.IntRangeFieldValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;
/**
* @author Surya Putra Haras
*
*/
public class ItemController extends CimandeModuleAction {
@Autowired
private ItemService itemService;
@Autowired
private CategoryService itemCategoryService;
private Item item = new Item();
private int limit = 0;
private int page = 0;
public ItemController() {
model.put(“item”, item);
}
// POST /module/item/create
// POST /module/item/create/{id}
@Validations(requiredStrings = { @RequiredStringValidator(fieldName = “item.name”, message = “Name cannot be empty”, trim = true) }, intRangeFields = { @IntRangeFieldValidator(fieldName = “item.price”, min = “0”, message = “Must be greater than zero”) })
public String itemPost() {
itemService.saveItem(item);
return SUCCESS;
}
// GET /module/item/create
// GET /module/item/edit/{id}
public String itemGet() {
model.put(“item”, itemService.getItemById(item.getId() + “”));
model.put(“itemCategorys”, itemCategoryService.getAllItemCategory());
return INPUT;
}
// DELETE /module/item/edit/{id}
public String itemDelete() {
itemService.deleteItem(itemService.getItemById(item.getId()));
return SUCCESS;
}
// POST/GET /module/item/filter
public String filter() {
try {
limit = model.get(“rows”) == null ? 0 : new Integer(model.get(
“rows”).toString());
page = model.get(“page”) == null ? 0 : new Integer(model
.get(“page”).toString());
} catch (NumberFormatException e) {
e.printStackTrace();
}
if (limit == 0 && page == 0) {
limit = 10;
page = 1;
}
model.put(“rows”, limit);
model.put(“page”, page);
int count = (int) itemService.getItemCount(“”, “”);
int total = count / limit;
if (total % limit > 0)
total++;
model.put(“items”, itemService.getItemList(“”, “”, limit, page));
if(total==0)
total++;
model.put(“total”, total);
model.put(“records”, total);
return SUCCESS;
}
}
Buat class CategoryDao.java dan item ItemDao di package org.blueoxygen.mycompany.dao package org.mycompany.dao;
import java.util.List;
import org.blueoxygen.cimande.commons.LogInformation;
import org.mycompany.entity.Category;
import org.blueoxygen.cimande.persistence.hibernate.dao.HibernatePersistenceDaoManager;
import org.blueoxygen.cimande.security.User;
import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
@Repository
public class CategoryDao extends
HibernatePersistenceDaoManager<Category> {
public void saveItemCategory(Category itemCategory) {
if (itemCategory == null)
return;
if (itemCategory.getId() == null) {
createItemCategory(itemCategory);
} else if (itemCategory.getId().trim().equalsIgnoreCase(“”)) {
createItemCategory(itemCategory);
} else {
itemCategory.getLogInformation().setLastUpdateBy(
getCurrentUser().getId());
itemCategory.getLogInformation().setLastUpdateDate(getCurretTime());
merge(itemCategory);
}
}
public long getItemCategoryCountByCriteria(String name, String description) {
Criteria criteria = getItemCategoryCriteria(name, description);
criteria.setProjection(Projections.rowCount());
return new Long(criteria.uniqueResult() + “”);
}
@SuppressWarnings(“unchecked”)
public List<Category> getItemCategoryByCriteria(String name,
String description, int limit, int page) {
return getItemCategoryCriteria(name, description).setMaxResults(limit)
.setFirstResult((page – 1) * limit).list();
}
private Criteria getItemCategoryCriteria(String name, String description) {
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(
Category.class);
criteria.add(Restrictions.like(“name”, name, MatchMode.ANYWHERE));
criteria.add(Restrictions.like(“description”, description,
MatchMode.ANYWHERE));
criteria.addOrder(Order.asc(“code”));
return criteria;
}
private void createItemCategory(Category itemCategory) {
User user = getCurrentUser();
LogInformation logInformation = new LogInformation();
logInformation.setCreateBy(user.getId());
logInformation.setCreateDate(getCurretTime());
logInformation.setLastUpdateBy(user.getId());
logInformation.setLastUpdateDate(getCurretTime());
itemCategory.setId(null);
itemCategory.setLogInformation(logInformation);
persist(itemCategory);
}
}
/**
* ItemDao.java
*
* Created on May 10, 2011 2:19:49 PM
*/
package org.mycompany.dao;
import java.util.List;
import org.blueoxygen.cimande.commons.LogInformation;
import org.mycompany.entity.Item;
import org.blueoxygen.cimande.persistence.hibernate.dao.HibernatePersistenceDaoManager;
import org.blueoxygen.cimande.security.User;
import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
/**
* @author Surya Putra Haras
*
*/
@Repository
public class ItemDao extends HibernatePersistenceDaoManager<Item> {
public void saveItem(Item item) {
if (item == null)
return;
if (item.getId() == null) {
createItem(item);
} else if (item.getId().trim().equalsIgnoreCase(“”)) {
createItem(item);
} else {
item.getLogInformation().setLastUpdateBy(getCurrentUser().getId());
item.getLogInformation().setLastUpdateDate(getCurretTime());
merge(item);
}
}
public long getItemCountByCriteria(String name, String description) {
Criteria criteria = getItemCriteria(name, description);
criteria.setProjection(Projections.rowCount());
return new Long(criteria.uniqueResult() + “”);
}
@SuppressWarnings(“unchecked”)
public List<Item> getItemByCriteria(String name, String description,
int limit, int page) {
return getItemCriteria(name, description).setMaxResults(limit)
.setFirstResult((page – 1) * limit).list();
}
private Criteria getItemCriteria(String name, String description) {
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(
Item.class);
criteria.add(Restrictions.like(“name”, name, MatchMode.ANYWHERE));
criteria.add(Restrictions.like(“description”, description,
MatchMode.ANYWHERE));
return criteria;
}
private void createItem(Item item) {
User user = getCurrentUser();
LogInformation logInformation = new LogInformation();
logInformation.setCreateBy(user.getId());
logInformation.setCreateDate(getCurretTime());
logInformation.setLastUpdateBy(user.getId());
logInformation.setLastUpdateDate(getCurretTime());
item.setId(null);
item.setLogInformation(logInformation);
persist(item);
}
}
Buat class Category.java dan Item di package org.blueoxygen.mycompany.entity
package org.mycompany.entity;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;
import org.blueoxygen.cimande.commons.DefaultPersistence;
/**
* @author Praditya Eldorado
*
*/
@Entity
@Table(name = “module_category”)
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Category extends DefaultPersistence {
private String code;
private String name;
private String description;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
/**
* Item.java
*
* Created on May 10, 2011 1:54:59 PM
*/
package org.mycompany.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.blueoxygen.cimande.commons.DefaultPersistence;
/**
* @author Surya Putra Haras
*
*/
@Entity
@Table(name = “module_item”)
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Item extends DefaultPersistence {
private String name;
private int price;
private String description;
private Category category;
/**
* @return the name
*/
@Column
public String getName() {
return name;
}
/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the price
*/
@Column
public int getPrice() {
return price;
}
/**
* @param price
* the price to set
*/
public void setPrice(int price) {
this.price = price;
}
/**
* @return the description
*/
public String getDescription() {
return description;
}
/**
* @param description
* the description to set
*/
public void setDescription(String description) {
this.description = description;
}
@ManyToOne
@JoinColumn(name = “category_id”)
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
}
Buat class CategoryService.java dan ItemService di package org.blueoxygen.mycompany.service
package org.mycompany.service;
import java.util.List;
import org.mycompany.entity.Category;
public interface CategoryService {
void saveItemCategory(Category itemCategory);
void deleteItemCategory(Category itemCategory);
Category getItemCategoryById(String id);
long getItemCategoryCount(String name, String description);
List<Category> getItemCategoryList(String name, String description,
int limit, int page);
List<Category> getAllItemCategory();
}
/**
* ItemService.java
*
* Created on May 10, 2011 2:22:25 PM
*/
package org.mycompany.service;
import java.util.List;
import org.mycompany.entity.Item;
/**
* @author Surya Putra Haras
*
*/
public interface ItemService {
void saveItem(Item item);
void deleteItem(Item item);
Item getItemById(String id);
long getItemCount(String name, String description);
List<Item> getItemList(String name, String description,
int limit, int page);
}
Buat class CategoryServiceImpl.java dan ItemServiceImpl di package org.mycompany.service.impl
package org.mycompany.service.impl;
import java.util.List;
import org.mycompany.dao.CategoryDao;
import org.mycompany.entity.Category;
import org.mycompany.entity.Item;
import org.mycompany.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional(readOnly = true)
public class CategoryServiceImpl implements CategoryService {
@Autowired
private CategoryDao itemCategoryDao;
@Transactional
public void saveItemCategory(Category itemCategory) {
itemCategoryDao.saveItemCategory(itemCategory);
}
@Transactional
public void deleteItemCategory(Category itemCategory) {
itemCategoryDao.remove(itemCategory);
}
public Category getItemCategoryById(String id) {
return itemCategoryDao.getById(Category.class, id);
}
public long getItemCategoryCount(String name, String description) {
return itemCategoryDao
.getItemCategoryCountByCriteria(name, description);
}
public List<Category> getItemCategoryList(String name,
String description, int limit, int page) {
return itemCategoryDao.getItemCategoryByCriteria(name, description,
limit, page);
}
public List<Category> getAllItemCategory() {
return itemCategoryDao.findAll(Category.class);
}
}
/**
* ItemServiceImpl.java
*
* Created on May 10, 2011 2:22:16 PM
*/
package org.mycompany.service.impl;
import java.util.List;
import org.mycompany.dao.ItemDao;
import org.mycompany.entity.Item;
import org.mycompany.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* @author Surya Putra Haras
*
*/
@Service
@Transactional(readOnly = true)
public class ItemServiceImpl implements ItemService {
@Autowired
private ItemDao itemDao;
@Transactional
public void saveItem(Item item) {
itemDao.saveItem(item);
}
@Transactional
public void deleteItem(Item item) {
itemDao.remove(item);
}
public Item getItemById(String id) {
return itemDao.getById(Item.class, id);
}
public long getItemCount(String name, String description) {
return itemDao.getItemCountByCriteria(name, description);
}
public List<Item> getItemList(String name, String description, int limit,
int page) {
return itemDao.getItemByCriteria(name, description, limit, page);
}
}
Kemudian restart tomcat:run. Buka web browser dan ketikan url http://localhost:8080/myproject/ . Kemudian login dengan username : inventoryadmin, password : inventoryadmin
maka hasilnya seperti dibawah ini
langsung saja pertama buatkan sebuah database dari project Inventori, setelah itu disini saya punya project tapi belum sepenuhnya berjalan, jika teman-teman menginginkan projectnya bisa langsung hubungi saya di suryaputraharas@gmail.com.
setelah itu lakukan import project inventorynya, pastikan project tersebut sudah di extract di tempat penyimpanan workspace nya. lalu lakukan import project maven dengan cara klik file–>import–>general lalu pilih maven project–> klikm ok.
jika sudah di import lakukan konfigurasi pada project tersebut di file propertis hibernate seperti ini :
- jdbc:mysql://localhost:3306/isikan_database_yang_dibuat?, biarkan tanda ? itu jangan di hapus, jika di hapus maka tidak akan terisi otomatis database yang di buat.
- tambahkan angka 5 di bagian Mysql. yang awalnya hanya MysqlInnoDB menjadi Mysql5InnoDB. contoh gambarnya seperti di bawah ini :
setelah itu konfigurasi lagi di bagian maven build dengan mengisikan perintah di bawah ini :
- Masukkan perintah untuk menggenerate table schema dengan cara klik kanan project → Run As → Maven Build. Isi Goals dengan perintah hibernate3:hbm2ddl
- Untuk mengimport data table kedalam database gunakan perintah
initialize dbunit:operation - jalankan perintah tomcat:run, maka secara otomatis Maven akan mendeploy aplikasi ke dalam server serta menjalankan server.
- Setelah server berhasil dijalankan, proyek dapat dibuka melalui url http://localhost:8080/(namaproject). Masukkan username admin dan password blueoxygen untuk dapat masuk ke halaman wokspace administrator.
setelah itu masukkan koding di setiap package nya, tapi project yang saya sudah buat ini sudah ada kodingnya, tapi jika teman-teman ingin membuat dari awal bisa memasukkan koding di bawah ini tiap package yang ada :
package org.mycompany.controller;
import org.blueoxygen.cimande.commons.CimandeModuleAction;
import org.mycompany.entity.Category;
import org.mycompany.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;
public class CategoryController extends CimandeModuleAction {
@Autowired
private CategoryService itemCategoryService;
private Category itemCategory = new Category();
private Integer limit = 0;
private Integer page = 0;
public CategoryController() {
model.put(“itemCategory”, itemCategory);
}
// POST /module/category/create
// POST /module/category/edit/{id}
@Validations(requiredStrings = { @RequiredStringValidator(fieldName = “itemCategory.name”, message = “Name cannot be empty”, trim = true) })
public String categoryPost() {
itemCategoryService.saveItemCategory(itemCategory);
return SUCCESS;
}
// GET /module/category/create
// GET /module/category/edit/{id}
public String categoryGet() {
model.put(
“itemCategory”,
itemCategoryService.getItemCategoryById(itemCategory.getId()
+ “”));
return INPUT;
}
// DELETE /module/category/edit/{id}
public String categoryDelete() {
itemCategoryService.deleteItemCategory(itemCategoryService
.getItemCategoryById(itemCategory.getId()));
return SUCCESS;
}
// POST/GET /module/category/filter
public String filter() {
try {
limit = model.get(“rows”) == null ? 0 : new Integer(model.get(
“rows”).toString());
page = model.get(“page”) == null ? 0 : new Integer(model
.get(“page”).toString());
} catch (NumberFormatException e) {
e.printStackTrace();
}
if (limit == 0 && page == 0) {
limit = 10;
page = 1;
}
model.put(“rows”, limit);
model.put(“page”, page);
int count = (int) itemCategoryService.getItemCategoryCount(“”, “”);
int total = count / limit;
if (total % limit > 0)
total++;
model.put(“itemCategorys”,
itemCategoryService.getItemCategoryList(“”, “”, limit, page));
if (total == 0)
total++;
model.put(“total”, total);
model.put(“records”, total);
return SUCCESS;
}
}
import org.mycompany.entity.Category;
import org.mycompany.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;
public class CategoryController extends CimandeModuleAction {
@Autowired
private CategoryService itemCategoryService;
private Category itemCategory = new Category();
private Integer limit = 0;
private Integer page = 0;
public CategoryController() {
model.put(“itemCategory”, itemCategory);
}
// POST /module/category/create
// POST /module/category/edit/{id}
@Validations(requiredStrings = { @RequiredStringValidator(fieldName = “itemCategory.name”, message = “Name cannot be empty”, trim = true) })
public String categoryPost() {
itemCategoryService.saveItemCategory(itemCategory);
return SUCCESS;
}
// GET /module/category/create
// GET /module/category/edit/{id}
public String categoryGet() {
model.put(
“itemCategory”,
itemCategoryService.getItemCategoryById(itemCategory.getId()
+ “”));
return INPUT;
}
// DELETE /module/category/edit/{id}
public String categoryDelete() {
itemCategoryService.deleteItemCategory(itemCategoryService
.getItemCategoryById(itemCategory.getId()));
return SUCCESS;
}
// POST/GET /module/category/filter
public String filter() {
try {
limit = model.get(“rows”) == null ? 0 : new Integer(model.get(
“rows”).toString());
page = model.get(“page”) == null ? 0 : new Integer(model
.get(“page”).toString());
} catch (NumberFormatException e) {
e.printStackTrace();
}
if (limit == 0 && page == 0) {
limit = 10;
page = 1;
}
model.put(“rows”, limit);
model.put(“page”, page);
int count = (int) itemCategoryService.getItemCategoryCount(“”, “”);
int total = count / limit;
if (total % limit > 0)
total++;
model.put(“itemCategorys”,
itemCategoryService.getItemCategoryList(“”, “”, limit, page));
if (total == 0)
total++;
model.put(“total”, total);
model.put(“records”, total);
return SUCCESS;
}
}
* ItemController.java
*
* Created on May 10, 2011 2:19:09 PM
*/
package org.mycompany.controller;
import org.blueoxygen.cimande.commons.CimandeModuleAction;
import org.mycompany.entity.Item;
import org.mycompany.service.CategoryService;
import org.mycompany.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import com.opensymphony.xwork2.validator.annotations.IntRangeFieldValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;
/**
* @author Surya Putra Haras
*
*/
public class ItemController extends CimandeModuleAction {
@Autowired
private ItemService itemService;
@Autowired
private CategoryService itemCategoryService;
private Item item = new Item();
private int limit = 0;
private int page = 0;
public ItemController() {
model.put(“item”, item);
}
// POST /module/item/create
// POST /module/item/create/{id}
@Validations(requiredStrings = { @RequiredStringValidator(fieldName = “item.name”, message = “Name cannot be empty”, trim = true) }, intRangeFields = { @IntRangeFieldValidator(fieldName = “item.price”, min = “0”, message = “Must be greater than zero”) })
public String itemPost() {
itemService.saveItem(item);
return SUCCESS;
}
// GET /module/item/create
// GET /module/item/edit/{id}
public String itemGet() {
model.put(“item”, itemService.getItemById(item.getId() + “”));
model.put(“itemCategorys”, itemCategoryService.getAllItemCategory());
return INPUT;
}
// DELETE /module/item/edit/{id}
public String itemDelete() {
itemService.deleteItem(itemService.getItemById(item.getId()));
return SUCCESS;
}
// POST/GET /module/item/filter
public String filter() {
try {
limit = model.get(“rows”) == null ? 0 : new Integer(model.get(
“rows”).toString());
page = model.get(“page”) == null ? 0 : new Integer(model
.get(“page”).toString());
} catch (NumberFormatException e) {
e.printStackTrace();
}
if (limit == 0 && page == 0) {
limit = 10;
page = 1;
}
model.put(“rows”, limit);
model.put(“page”, page);
int count = (int) itemService.getItemCount(“”, “”);
int total = count / limit;
if (total % limit > 0)
total++;
model.put(“items”, itemService.getItemList(“”, “”, limit, page));
if(total==0)
total++;
model.put(“total”, total);
model.put(“records”, total);
return SUCCESS;
}
}
Buat class CategoryDao.java dan item ItemDao di package org.blueoxygen.mycompany.dao package org.mycompany.dao;
import java.util.List;
import org.blueoxygen.cimande.commons.LogInformation;
import org.mycompany.entity.Category;
import org.blueoxygen.cimande.persistence.hibernate.dao.HibernatePersistenceDaoManager;
import org.blueoxygen.cimande.security.User;
import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
@Repository
public class CategoryDao extends
HibernatePersistenceDaoManager<Category> {
public void saveItemCategory(Category itemCategory) {
if (itemCategory == null)
return;
if (itemCategory.getId() == null) {
createItemCategory(itemCategory);
} else if (itemCategory.getId().trim().equalsIgnoreCase(“”)) {
createItemCategory(itemCategory);
} else {
itemCategory.getLogInformation().setLastUpdateBy(
getCurrentUser().getId());
itemCategory.getLogInformation().setLastUpdateDate(getCurretTime());
merge(itemCategory);
}
}
public long getItemCategoryCountByCriteria(String name, String description) {
Criteria criteria = getItemCategoryCriteria(name, description);
criteria.setProjection(Projections.rowCount());
return new Long(criteria.uniqueResult() + “”);
}
@SuppressWarnings(“unchecked”)
public List<Category> getItemCategoryByCriteria(String name,
String description, int limit, int page) {
return getItemCategoryCriteria(name, description).setMaxResults(limit)
.setFirstResult((page – 1) * limit).list();
}
private Criteria getItemCategoryCriteria(String name, String description) {
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(
Category.class);
criteria.add(Restrictions.like(“name”, name, MatchMode.ANYWHERE));
criteria.add(Restrictions.like(“description”, description,
MatchMode.ANYWHERE));
criteria.addOrder(Order.asc(“code”));
return criteria;
}
private void createItemCategory(Category itemCategory) {
User user = getCurrentUser();
LogInformation logInformation = new LogInformation();
logInformation.setCreateBy(user.getId());
logInformation.setCreateDate(getCurretTime());
logInformation.setLastUpdateBy(user.getId());
logInformation.setLastUpdateDate(getCurretTime());
itemCategory.setId(null);
itemCategory.setLogInformation(logInformation);
persist(itemCategory);
}
}
/**
* ItemDao.java
*
* Created on May 10, 2011 2:19:49 PM
*/
package org.mycompany.dao;
import java.util.List;
import org.blueoxygen.cimande.commons.LogInformation;
import org.mycompany.entity.Item;
import org.blueoxygen.cimande.persistence.hibernate.dao.HibernatePersistenceDaoManager;
import org.blueoxygen.cimande.security.User;
import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
/**
* @author Surya Putra Haras
*
*/
@Repository
public class ItemDao extends HibernatePersistenceDaoManager<Item> {
public void saveItem(Item item) {
if (item == null)
return;
if (item.getId() == null) {
createItem(item);
} else if (item.getId().trim().equalsIgnoreCase(“”)) {
createItem(item);
} else {
item.getLogInformation().setLastUpdateBy(getCurrentUser().getId());
item.getLogInformation().setLastUpdateDate(getCurretTime());
merge(item);
}
}
public long getItemCountByCriteria(String name, String description) {
Criteria criteria = getItemCriteria(name, description);
criteria.setProjection(Projections.rowCount());
return new Long(criteria.uniqueResult() + “”);
}
@SuppressWarnings(“unchecked”)
public List<Item> getItemByCriteria(String name, String description,
int limit, int page) {
return getItemCriteria(name, description).setMaxResults(limit)
.setFirstResult((page – 1) * limit).list();
}
private Criteria getItemCriteria(String name, String description) {
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(
Item.class);
criteria.add(Restrictions.like(“name”, name, MatchMode.ANYWHERE));
criteria.add(Restrictions.like(“description”, description,
MatchMode.ANYWHERE));
return criteria;
}
private void createItem(Item item) {
User user = getCurrentUser();
LogInformation logInformation = new LogInformation();
logInformation.setCreateBy(user.getId());
logInformation.setCreateDate(getCurretTime());
logInformation.setLastUpdateBy(user.getId());
logInformation.setLastUpdateDate(getCurretTime());
item.setId(null);
item.setLogInformation(logInformation);
persist(item);
}
}
Buat class Category.java dan Item di package org.blueoxygen.mycompany.entity
package org.mycompany.entity;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;
import org.blueoxygen.cimande.commons.DefaultPersistence;
/**
* @author Praditya Eldorado
*
*/
@Entity
@Table(name = “module_category”)
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Category extends DefaultPersistence {
private String code;
private String name;
private String description;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
/**
* Item.java
*
* Created on May 10, 2011 1:54:59 PM
*/
package org.mycompany.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.blueoxygen.cimande.commons.DefaultPersistence;
/**
* @author Surya Putra Haras
*
*/
@Entity
@Table(name = “module_item”)
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Item extends DefaultPersistence {
private String name;
private int price;
private String description;
private Category category;
/**
* @return the name
*/
@Column
public String getName() {
return name;
}
/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the price
*/
@Column
public int getPrice() {
return price;
}
/**
* @param price
* the price to set
*/
public void setPrice(int price) {
this.price = price;
}
/**
* @return the description
*/
public String getDescription() {
return description;
}
/**
* @param description
* the description to set
*/
public void setDescription(String description) {
this.description = description;
}
@ManyToOne
@JoinColumn(name = “category_id”)
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
}
Buat class CategoryService.java dan ItemService di package org.blueoxygen.mycompany.service
package org.mycompany.service;
import java.util.List;
import org.mycompany.entity.Category;
public interface CategoryService {
void saveItemCategory(Category itemCategory);
void deleteItemCategory(Category itemCategory);
Category getItemCategoryById(String id);
long getItemCategoryCount(String name, String description);
List<Category> getItemCategoryList(String name, String description,
int limit, int page);
List<Category> getAllItemCategory();
}
/**
* ItemService.java
*
* Created on May 10, 2011 2:22:25 PM
*/
package org.mycompany.service;
import java.util.List;
import org.mycompany.entity.Item;
/**
* @author Surya Putra Haras
*
*/
public interface ItemService {
void saveItem(Item item);
void deleteItem(Item item);
Item getItemById(String id);
long getItemCount(String name, String description);
List<Item> getItemList(String name, String description,
int limit, int page);
}
Buat class CategoryServiceImpl.java dan ItemServiceImpl di package org.mycompany.service.impl
package org.mycompany.service.impl;
import java.util.List;
import org.mycompany.dao.CategoryDao;
import org.mycompany.entity.Category;
import org.mycompany.entity.Item;
import org.mycompany.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional(readOnly = true)
public class CategoryServiceImpl implements CategoryService {
@Autowired
private CategoryDao itemCategoryDao;
@Transactional
public void saveItemCategory(Category itemCategory) {
itemCategoryDao.saveItemCategory(itemCategory);
}
@Transactional
public void deleteItemCategory(Category itemCategory) {
itemCategoryDao.remove(itemCategory);
}
public Category getItemCategoryById(String id) {
return itemCategoryDao.getById(Category.class, id);
}
public long getItemCategoryCount(String name, String description) {
return itemCategoryDao
.getItemCategoryCountByCriteria(name, description);
}
public List<Category> getItemCategoryList(String name,
String description, int limit, int page) {
return itemCategoryDao.getItemCategoryByCriteria(name, description,
limit, page);
}
public List<Category> getAllItemCategory() {
return itemCategoryDao.findAll(Category.class);
}
}
/**
* ItemServiceImpl.java
*
* Created on May 10, 2011 2:22:16 PM
*/
package org.mycompany.service.impl;
import java.util.List;
import org.mycompany.dao.ItemDao;
import org.mycompany.entity.Item;
import org.mycompany.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* @author Surya Putra Haras
*
*/
@Service
@Transactional(readOnly = true)
public class ItemServiceImpl implements ItemService {
@Autowired
private ItemDao itemDao;
@Transactional
public void saveItem(Item item) {
itemDao.saveItem(item);
}
@Transactional
public void deleteItem(Item item) {
itemDao.remove(item);
}
public Item getItemById(String id) {
return itemDao.getById(Item.class, id);
}
public long getItemCount(String name, String description) {
return itemDao.getItemCountByCriteria(name, description);
}
public List<Item> getItemList(String name, String description, int limit,
int page) {
return itemDao.getItemByCriteria(name, description, limit, page);
}
}
Kemudian restart tomcat:run. Buka web browser dan ketikan url http://localhost:8080/myproject/ . Kemudian login dengan username : inventoryadmin, password : inventoryadmin
maka hasilnya seperti dibawah ini
di sini teman-teman bisa langsung mengoperasikkan
project tersebut dengan melakukan pengisihan data sesuai kebutuhan
teman-teman. semoga bermanfaat dan samapai berjumpa kembali dan semangat
terus ya teman-teman
Tidak ada komentar:
Posting Komentar