com.ardikapras.controller.customer.CustomerFormCtrl.java Source code

Java tutorial

Introduction

Here is the source code for com.ardikapras.controller.customer.CustomerFormCtrl.java

Source

/*
 * 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.customer;

import com.ardikapras.common.CommonUtil;
import com.ardikapras.common.HibernateHelper;
import com.ardikapras.common.enumeration.CommonID;
import com.ardikapras.model.Customer;
import com.ardikapras.security.UserBean;
import java.util.Date;
import javax.swing.JOptionPane;
import org.hibernate.Session;

/**
 *
 * @author ardikapras
 */
public class CustomerFormCtrl {

    private Customer customer;
    private Boolean isNew = false;

    public CustomerFormCtrl() {
        customer = new Customer();
        isNew = true;
    }

    public CustomerFormCtrl(String idCustomer) {
        boolean closeAfter = false;
        try {
            closeAfter = HibernateHelper.beginTx();
            Session session = HibernateHelper.getSession();
            customer = (Customer) session.get(Customer.class, idCustomer);
            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();
            customer.setIsActive('1');
            if (getIsNew()) {
                customer.setId(CommonUtil.generateId(CommonID.Customer.getVal()));
                customer.setUserCreated(UserBean.getInstance().getIdUser());
                customer.setTimeCreated(new Date());
                session.save(customer);
            } else {
                customer.setUserUpdated(UserBean.getInstance().getIdUser());
                customer.setTimeUpdated(new Date());
                session.merge(customer);
            }
            session.flush();
            isSuccess = true;
            HibernateHelper.commitTx(closeAfter);
        } catch (Exception e) {
            HibernateHelper.rollbackTx(closeAfter);
            JOptionPane.showMessageDialog(null, e);
        }
        return isSuccess;
    }

    public Boolean delete(String idCustomer) {
        boolean closeAfter = false;
        boolean isSuccess = false;
        try {
            closeAfter = HibernateHelper.beginTx();
            Session session = HibernateHelper.getSession();
            if (customer.getIsActive() == '1') {
                customer.setIsActive('0');
            } else {
                customer.setIsActive('1');
            }
            customer.setUserUpdated(UserBean.getInstance().getIdUser());
            customer.setTimeUpdated(new Date());
            session.merge(customer);
            session.flush();
            isSuccess = true;
            HibernateHelper.commitTx(closeAfter);
        } catch (Exception e) {
            HibernateHelper.rollbackTx(closeAfter);
            JOptionPane.showMessageDialog(null, e);
        }
        return isSuccess;
    }

    /**
     * @return the customer
     */
    public Customer getCustomer() {
        return customer;
    }

    /**
     * @param customer the customer to set
     */
    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    /**
     * @return the isNew
     */
    public Boolean getIsNew() {
        return isNew;
    }

    /**
     * @param isNew the isNew to set
     */
    public void setIsNew(Boolean isNew) {
        this.isNew = isNew;
    }
}