com.ardikapras.controller.supplier.SupplierFormCtrl.java Source code

Java tutorial

Introduction

Here is the source code for com.ardikapras.controller.supplier.SupplierFormCtrl.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.supplier;

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

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

    private Supplier supplier;
    private Boolean isNew = false;

    public SupplierFormCtrl() {
        supplier = new Supplier();
        isNew = true;
    }

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

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

    public Supplier getSupplier() {
        return supplier;
    }

    public void setSupplier(Supplier supplier) {
        this.supplier = supplier;
    }

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

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