Java tutorial
/* * Copyright 2014 ardikapras. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.ardikapras.controller.product; import com.ardikapras.common.CommonUtil; import com.ardikapras.common.HibernateHelper; import com.ardikapras.common.enumeration.CommonID; import com.ardikapras.model.Product; import com.ardikapras.security.UserBean; import java.util.Date; import javax.swing.JOptionPane; import org.hibernate.Session; /** * * @author ardikapras */ public class ProductFormCtrl { private Product product; private Boolean isNew = false; public ProductFormCtrl() { product = new Product(); isNew = true; } public ProductFormCtrl(String idProduct) { boolean closeAfter = false; try { closeAfter = HibernateHelper.beginTx(); Session session = HibernateHelper.getSession(); product = (Product) session.get(Product.class, idProduct); isNew = false; HibernateHelper.commitTx(closeAfter); } catch (Exception e) { HibernateHelper.rollbackTx(closeAfter); JOptionPane.showMessageDialog(null, e); } } public Boolean save() { boolean closeAfter = false; boolean isSuccess = false; try { closeAfter = HibernateHelper.beginTx(); Session session = HibernateHelper.getSession(); product.setIsActive('1'); if (getIsNew()) { product.setId(CommonUtil.generateId(CommonID.Supplier.getVal())); product.setUserCreated(UserBean.getInstance().getIdUser()); product.setTimeCreated(new Date()); session.save(product); } else { product.setUserUpdated(UserBean.getInstance().getIdUser()); product.setTimeUpdated(new Date()); session.merge(product); } session.flush(); isSuccess = true; HibernateHelper.commitTx(closeAfter); } catch (Exception e) { HibernateHelper.rollbackTx(closeAfter); JOptionPane.showMessageDialog(null, e); } return isSuccess; } public Boolean delete(String idProduct) { boolean closeAfter = false; boolean isSuccess = false; try { closeAfter = HibernateHelper.beginTx(); Session session = HibernateHelper.getSession(); if (product.getIsActive() == '1') { product.setIsActive('0'); } else { product.setIsActive('1'); } product.setUserUpdated(UserBean.getInstance().getIdUser()); product.setTimeUpdated(new Date()); session.merge(product); session.flush(); isSuccess = true; HibernateHelper.commitTx(closeAfter); } catch (Exception e) { HibernateHelper.rollbackTx(closeAfter); JOptionPane.showMessageDialog(null, e); } return isSuccess; } public Product getProduct() { return product; } public void setProduct(Product product) { this.product = product; } /** * @return the isNew */ public Boolean getIsNew() { return isNew; } /** * @param isNew the isNew to set */ public void setIsNew(Boolean isNew) { this.isNew = isNew; } }