Inventory barang dengan Blueoxygen

1. buat project baru dengan Maven Project
ket:
Untuk langkah-langkah membuat projectnya seperti yang saya telah buat sebelumya, ikuti langkah-langkahnya.
2. setelah membuat project, kita buat package baru dengan nama sebagai berikut:
1
3. buat class CategoryController.java di package org.mycompany.controller
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;
}
}
4. buat class ItemController di package org.mycompany.controller
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 Dian Aditya
*
*/
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;
}
}
5.  buat class CategoryDao.java di package org.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);
}
}
6. buat class ItemDao.java di package org.mycompany.dao
/**
* 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 Dian Aditya
*
*/
@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);
}
}
7. buat class Category.java di package org.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;
}
}
8. buat class Item.java di package org.mycompany.entity
/**
* 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 Dian Aditya
*
*/
@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;
}
}
9. buat class CategoryService.java dipackage org.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();
}
10. buat class ItemService.java dipackage org.mycompany.service
/**
* 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 Dian Aditya
*
*/
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);
}
11. buat class CategoryServiceImpl.java dipackage 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);
}
}
12. buat class ItemServiceImpl.java dipackage org.mycompany.service.impl
/**
* 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 Dian Aditya
*
*/
@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);
}
}
13. Jalankan Project dengan server tomcat gunakan:
user : inventoryadmin
pass : inventoryadmin
2
berikut hasilnya setelah berhasil login
capture
SHARE

Milan Tomic

Hi. I’m Designer of Blog Magic. I’m CEO/Founder of ThemeXpose. I’m Creative Art Director, Web Designer, UI/UX Designer, Interaction Designer, Industrial Designer, Web Developer, Business Enthusiast, StartUp Enthusiast, Speaker, Writer and Photographer. Inspired to make things looks better.

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 komentar:

Posting Komentar