Pada tulisan blog kali ini saya akan menuliskan hasil praktikum pembuatan pemesanan Hotel menggunakan Sping Source Tools.
Pembuatanya sama seperti tulian sebelumnya ya itu pembuatan absen, bisa di lihat pada tulisan blog sebelumnya.
kita langsung masuk pada mspring source toolsnya dan jangan lupa untuk mengaktifkan Tomcatnya.
untuk membuat project baru
kita pilih File– New–Java Project
kemuduan ketikan pemesanan hotel.
selanjutnya kita buka yang sudah saya beri warna merah.
klik kanan pilih new package
disini saya akan membuat 5 package.
kemudian pada masing -masing package kita buat class baru
pada package org.mycompany.controller kita buat 2 class
}
/**
* 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 Isnawaty Ibrahim
*
*/
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;
}
}
selanjutnya kita masuk pada package org.mycompany.dao buat 2 class juga sama seperti sebelumnya.
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);
}
}
sekarang kita masuk pada DAO. pada package org.mycompany.dao kita buat dua class.
isikan source di bawah ini pada CategoryDao.java
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 sama seperti sebelumnya kita masukan source code nya
/**
* 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 isnawaty ibrahim
*
*/
@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);
}
}
sekarang buat dua class pada org.mycompany.entity
Category. java
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 Isnawaty Ibrahim
*
*/
@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
/**
* 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 isnawaty Ibrahim
*
*/
@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;
}
}
kemudian buat class pada org.mycompany.service
CategoryService.java
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
/**
* 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 Isnawaty Ibrahim
*
*/
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);
}
dan terakhir kita buat dua class pada org.mycompany.service.impl
CategoryServiceImpl.java
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
/**
* 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 Isnawaty
*
*/
@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);
}
}
kita masuk pada pengisian
Pembuatanya sama seperti tulian sebelumnya ya itu pembuatan absen, bisa di lihat pada tulisan blog sebelumnya.
kita langsung masuk pada mspring source toolsnya dan jangan lupa untuk mengaktifkan Tomcatnya.
untuk membuat project baru
kita pilih File– New–Java Project
kemuduan ketikan pemesanan hotel.
selanjutnya kita buka yang sudah saya beri warna merah.
klik kanan pilih new package
disini saya akan membuat 5 package.
- org.mycompany.controller
- org.mycompany.dao
- org.mycompany.entity
- org.mycompany.service
- org.mycompany.service.impl
kemudian pada masing -masing package kita buat class baru
pada package org.mycompany.controller kita buat 2 class
- CategoryController dan masukan source kode berikut ini.
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;
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 CategoryService itemCategoryService;
private Category itemCategory = new Category();
private Integer limit = 0;
private Integer page = 0;
private Integer limit = 0;
private Integer page = 0;
public CategoryController() {
model.put(“itemCategory”, itemCategory);
}
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;
}
// 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;
}
// 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()));
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());
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();
}
e.printStackTrace();
}
if (limit == 0 && page == 0) {
limit = 10;
page = 1;
limit = 10;
page = 1;
}
model.put(“rows”, limit);
model.put(“page”, page);
model.put(“rows”, limit);
model.put(“page”, page);
int count = (int) itemCategoryService.getItemCategoryCount(“”, “”);
int total = count / limit;
if (total % limit > 0)
total++;
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;itemCategoryService.getItemCategoryList(“”, “”, limit, page));
if (total == 0)
total++;
model.put(“total”, total);
model.put(“records”, total);
}
}
kemudian class ke 2 yaitu ItemController kita masukan source berikut ini/**
* 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 Isnawaty Ibrahim
*
*/
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;
}
}
selanjutnya kita masuk pada package org.mycompany.dao buat 2 class juga sama seperti sebelumnya.
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);
}
}
sekarang kita masuk pada DAO. pada package org.mycompany.dao kita buat dua class.
isikan source di bawah ini pada CategoryDao.java
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 sama seperti sebelumnya kita masukan source code nya
/**
* 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 isnawaty ibrahim
*
*/
@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);
}
}
sekarang buat dua class pada org.mycompany.entity
Category. java
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 Isnawaty Ibrahim
*
*/
@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
/**
* 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 isnawaty Ibrahim
*
*/
@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;
}
}
kemudian buat class pada org.mycompany.service
CategoryService.java
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
/**
* 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 Isnawaty Ibrahim
*
*/
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);
}
dan terakhir kita buat dua class pada org.mycompany.service.impl
CategoryServiceImpl.java
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
/**
* 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 Isnawaty
*
*/
@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);
}
}
kita masuk pada pengisian
Tidak ada komentar:
Posting Komentar