TavernerPOS - Source Code

Clone: 

git clone http://www.librarysmith.co.uk/tavernerPOS

 

/*
 * UserForm2.java
 *
 * Created on February 8, 2008, 6:08 PM
 */

package com.floreantpos.ui.forms;

import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JCheckBox;
import javax.swing.JLabel;

import net.miginfocom.swing.MigLayout;

import com.floreantpos.Messages;
import com.floreantpos.PosException;
import com.floreantpos.model.User;
import com.floreantpos.model.UserType;
import com.floreantpos.model.dao.UserDAO;
import com.floreantpos.model.dao.UserTypeDAO;
import com.floreantpos.model.util.IllegalModelStateException;
import com.floreantpos.swing.DoubleTextField;
import com.floreantpos.swing.FixedLengthDocument;
import com.floreantpos.swing.FixedLengthTextField;
import com.floreantpos.ui.BeanEditor;
import com.floreantpos.ui.dialog.POSMessageDialog;
import com.floreantpos.util.POSUtil;

/**
 *
 * @author rodaya
 */
public class UserForm extends BeanEditor {

	/** Creates new form UserForm2 */
	public UserForm() {
		initComponents();

		UserTypeDAO dao = new UserTypeDAO();
		List<UserType> userTypes = dao.findAll();

		cbUserType.setModel(new DefaultComboBoxModel(userTypes.toArray()));

		chkDriver = new JCheckBox(Messages.getString("UserForm.0")); //$NON-NLS-1$
		add(chkDriver, "cell 1 9"); //$NON-NLS-1$
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed" desc="Generated
	// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {
		jLabel1 = new javax.swing.JLabel();
		jLabel2 = new javax.swing.JLabel();
		jLabel3 = new javax.swing.JLabel();
		jLabel4 = new javax.swing.JLabel();
		jLabel9 = new javax.swing.JLabel();
		jLabel10 = new javax.swing.JLabel();
		tfPassword1 = new javax.swing.JPasswordField(new FixedLengthDocument(4), "", 10); //$NON-NLS-1$
		tfPassword1.setColumns(16);
		tfPassword2 = new javax.swing.JPasswordField(new FixedLengthDocument(4), "", 10); //$NON-NLS-1$
		tfPassword2.setColumns(16);
		tfId = new FixedLengthTextField();
		tfSsn = new FixedLengthTextField();
		tfSsn.setLength(30);
		tfSsn.setColumns(30);
		tfFirstName = new FixedLengthTextField();
		tfFirstName.setColumns(30);
		tfFirstName.setLength(30);
		tfLastName = new FixedLengthTextField();
		tfLastName.setLength(30);
		tfLastName.setColumns(30);
		jLabel5 = new javax.swing.JLabel();
		tfCostPerHour = new DoubleTextField();
		jLabel6 = new javax.swing.JLabel();
		cbUserType = new javax.swing.JComboBox();
		setLayout(new MigLayout("", "[134px][204px,grow]", "[19px][][19px][19px][19px][19px][19px][19px][24px][]")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

		jLabel1.setText(Messages.getString("UserForm.7")); //$NON-NLS-1$
		add(jLabel1, "cell 0 0,alignx trailing,aligny center"); //$NON-NLS-1$

		lblPhone = new JLabel(Messages.getString("UserForm.9")); //$NON-NLS-1$
		add(lblPhone, "cell 0 1,alignx trailing"); //$NON-NLS-1$

		tfPhone = new FixedLengthTextField();
		tfPhone.setLength(20);
		tfPhone.setColumns(20);
		add(tfPhone, "cell 1 1,growx"); //$NON-NLS-1$

		jLabel2.setText("SSN"); //$NON-NLS-1$
		add(jLabel2, "cell 0 2,alignx trailing,aligny center"); //$NON-NLS-1$

		jLabel3.setText(Messages.getString("UserForm.14")); //$NON-NLS-1$
		add(jLabel3, "cell 0 3,alignx trailing,aligny center"); //$NON-NLS-1$

		jLabel4.setText(Messages.getString("UserForm.16")); //$NON-NLS-1$
		add(jLabel4, "cell 0 4,alignx trailing,aligny center"); //$NON-NLS-1$

		jLabel9.setText(Messages.getString("UserForm.18")); //$NON-NLS-1$
		add(jLabel9, "cell 0 5,alignx trailing,aligny center"); //$NON-NLS-1$

		jLabel10.setText(Messages.getString("UserForm.20")); //$NON-NLS-1$
		add(jLabel10, "cell 0 6,alignx trailing,aligny center"); //$NON-NLS-1$
		add(tfPassword1, "cell 1 5,growx,aligny center"); //$NON-NLS-1$
		add(tfPassword2, "cell 1 6,growx,aligny center"); //$NON-NLS-1$
		add(tfId, "cell 1 0,growx,aligny center"); //$NON-NLS-1$
		add(tfSsn, "cell 1 2,aligny center"); //$NON-NLS-1$
		add(tfFirstName, "cell 1 3,growx,aligny center"); //$NON-NLS-1$
		add(tfLastName, "cell 1 4,growx,aligny center"); //$NON-NLS-1$

		jLabel5.setText(Messages.getString("UserForm.28")); //$NON-NLS-1$
		add(jLabel5, "cell 0 7,alignx trailing,aligny center"); //$NON-NLS-1$
		add(tfCostPerHour, "cell 1 7,growx,aligny center"); //$NON-NLS-1$

		jLabel6.setText(Messages.getString("UserForm.31")); //$NON-NLS-1$
		add(jLabel6, "cell 0 8,alignx trailing,aligny center"); //$NON-NLS-1$

		cbUserType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { Messages.getString("UserForm.33"), Messages.getString("UserForm.34"), Messages.getString("UserForm.35") })); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
		add(cbUserType, "cell 1 8,growx,aligny center"); //$NON-NLS-1$
	}// </editor-fold>//GEN-END:initComponents

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JComboBox cbUserType;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel10;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JLabel jLabel6;
	private javax.swing.JLabel jLabel9;
	private DoubleTextField tfCostPerHour;
	private FixedLengthTextField tfFirstName;
	private FixedLengthTextField tfId;
	private FixedLengthTextField tfLastName;
	private javax.swing.JPasswordField tfPassword1;
	private javax.swing.JPasswordField tfPassword2;
	private FixedLengthTextField tfSsn;

	// End of variables declaration//GEN-END:variables

	@Override
	public String getDisplayText() {
		if (isEditMode())
			return Messages.getString("UserForm.37"); //$NON-NLS-1$

		return Messages.getString("UserForm.38"); //$NON-NLS-1$
	}

	private boolean editMode;
	private JLabel lblPhone;
	private FixedLengthTextField tfPhone;
	private JCheckBox chkDriver;

	@Override
	public boolean save() {
		try {
			updateModel();
		} catch (IllegalModelStateException e) {
			POSMessageDialog.showError(this, e.getMessage());
			return false;
		}

		User user = (User) getBean();
		UserDAO userDAO = UserDAO.getInstance();

		if (!editMode) {
			if (userDAO.isUserExist(user.getUserId())) {
				POSMessageDialog.showError(this, Messages.getString("UserForm.39") + user.getUserId() + " " + Messages.getString("UserForm.1")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
				return false;
			}
		}

		try {
			userDAO.saveOrUpdate(user, editMode);
		} catch (PosException x) {
			POSMessageDialog.showError(this, x.getMessage(), x);
			x.printStackTrace();
			return false;
		} catch (Exception x) {
			POSMessageDialog.showError(this, Messages.getString("UserForm.41"), x); //$NON-NLS-1$
			x.printStackTrace();
			return false;
		}

		return true;
	}

	@Override
	protected boolean updateModel() throws IllegalModelStateException {
		User user = null;
		if (!(getBean() instanceof User)) {
			user = new User();
		}
		else {
			user = (User) getBean();
		}

		int id = 1000;
		try {
			id = Integer.parseInt(tfId.getText());
		} catch (Exception x) {
			throw new IllegalModelStateException(Messages.getString("UserForm.42")); //$NON-NLS-1$
		}

		String ssn = tfSsn.getText();
		String firstName = tfFirstName.getText();
		String lastName = tfLastName.getText();
		String secretKey1 = new String(tfPassword1.getPassword());
		String secretKey2 = new String(tfPassword2.getPassword());

		if (POSUtil.isBlankOrNull(firstName)) {
			throw new IllegalModelStateException(Messages.getString("UserForm.43")); //$NON-NLS-1$
		}
		if (POSUtil.isBlankOrNull(lastName)) {
			throw new IllegalModelStateException(Messages.getString("UserForm.44")); //$NON-NLS-1$
		}
		if (POSUtil.isBlankOrNull(secretKey1)) {
			throw new IllegalModelStateException(Messages.getString("UserForm.45")); //$NON-NLS-1$
		}
		if (POSUtil.isBlankOrNull(secretKey2)) {
			throw new IllegalModelStateException(Messages.getString("UserForm.46")); //$NON-NLS-1$
		}
		if (!secretKey1.equals(secretKey2)) {
			throw new IllegalModelStateException(Messages.getString("UserForm.47")); //$NON-NLS-1$
		}

		if (!isEditMode()) {
			User userBySecretKey = UserDAO.getInstance().findUserBySecretKey(secretKey1);
			if (userBySecretKey != null) {
				throw new IllegalModelStateException(Messages.getString("UserForm.48")); //$NON-NLS-1$
			}
		}

		double cost = 0;

		try {
			cost = Double.parseDouble(tfCostPerHour.getText());
		} catch (Exception x) {
			throw new IllegalModelStateException(Messages.getString("UserForm.49") + firstName + " " + lastName + " " + Messages.getString("UserForm.2")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
		}

		user.setType((UserType) cbUserType.getSelectedItem());
		user.setCostPerHour(cost);

		user.setSsn(ssn);
		user.setUserId(id);
		user.setFirstName(firstName);
		user.setLastName(lastName);
		user.setPhoneNo(tfPhone.getText());
		user.setPassword(secretKey1);
		user.setDriver(chkDriver.isSelected());

		setBean(user);
		return true;
	}

	@Override
	protected void updateView() {
		if (!(getBean() instanceof User)) {
			return;
		}
		User user = (User) getBean();
		setData(user);
	}

	private void setData(User data) {
		if (data.getUserId() != null) {
			tfId.setText(String.valueOf(data.getUserId()));
		}
		else {
			tfId.setText(""); //$NON-NLS-1$
		}
		if (data.getSsn() != null) {
			tfSsn.setText(data.getSsn());
		}
		else {
			tfSsn.setText(""); //$NON-NLS-1$
		}
		tfFirstName.setText(data.getFirstName());
		tfLastName.setText(data.getLastName());
		tfPassword1.setText(data.getPassword());
		tfPassword2.setText(data.getPassword());
		tfPhone.setText(data.getPhoneNo());
		cbUserType.setSelectedItem(data.getType());

		Double costPerHour = data.getCostPerHour();
		if(costPerHour == null) {
			costPerHour = 0.0;
		}

		tfCostPerHour.setText(String.valueOf(costPerHour));
		chkDriver.setSelected(data.isDriver());
	}

	public boolean isEditMode() {
		return editMode;
	}

	public void setEditMode(boolean editMode) {
		this.editMode = editMode;
		if (editMode) {
			tfId.setEditable(false);
		}
		else {
			tfId.setEditable(true);
		}
	}

	public void setId(Integer id) {
		if (id != null) {
			tfId.setText(String.valueOf(id.intValue()));
		}
	}
}

Add comment
These comments are moderated so so won't be published until reviewed.