24 May 2007

TSA504.pages.AddNew - Step 04

Refers to... .

package org.example.TSA504.pages;

import org.apache.tapestry.annotations.Component;
import org.apache.tapestry.annotations.Inject;
import org.apache.tapestry.annotations.Persist;
import org.apache.tapestry.corelib.components.Form;
import org.apache.tapestry.corelib.components.PasswordField;
import org.apache.tapestry.corelib.components.TextField;
import org.example.TSA504.beans.TUser;
import org.example.TSA504.services.interfaces.UserAuthenticator;

public class AddNew
{
@Persist
private String _newUserName;

private String _newFirstUserName;
private String _newLastUserName;
private String _newMI;
private String _newEmail;
private String _newPassword;
private String _confirmNewPassword;

@Component(id = "newUserName")
private TextField _newUserNameField;

@Component(id = "confirmNewPassword")
private PasswordField _confirmNewPasswordField;

@Component
private Form _regform;

@Inject
private UserAuthenticator _authenticator;

String onSuccess() {
if (!_confirmNewPassword.equals(_newPassword))
{
_regform.recordError(_confirmNewPasswordField, "Passwords don't match.");
return null;
}
if (!(_authenticator.addNewTUser(new TUser (_newUserName, _newFirstUserName,_newLastUserName,_newMI,_newEmail,_newPassword)))){
_regform.recordError(_newUserNameField, "Sorry, a error has occured upon adding the user");
return null;
}
return "Start";
}
/**
* @return the _confirmpassword
*/
public String getConfirmNewPassword() {
return _confirmNewPassword;
}
/**
* @param _confirmpassword the _confirmpassword to set
*/
public void setConfirmNewPassword(String _confirmpassword) {
this._confirmNewPassword = _confirmpassword;
}

/**
* @return the _confirmNewPasswordField
*/
public PasswordField getConfirmNewPasswordField() {
return _confirmNewPasswordField;
}

/**
* @param field the _confirmNewPasswordField to set
*/
public void setConfirmNewPasswordField(PasswordField field) {
_confirmNewPasswordField = field;
}

/**
* @return the _newPassword
*/
public String getNewPassword() {
return _newPassword;
}

/**
* @param _newPassword the _newPassword to set
*/
public void setNewPassword(String _password) {
this._newPassword = _password;
}

/**
* @return the _regform
*/
public Form getRegform() {
return _regform;
}

/**
* @param _regform the _regform to set
*/
public void setRegform(Form _regform) {
this._regform = _regform;
}

/**
* @return the _newUserName
*/
public String getNewUserName() {
return _newUserName;
}

/**
* @param name the _newUserName to set
*/
public void setNewUserName(String name) {
_newUserName = name;
}

/**
* @return the _newEmail
*/
public String get_newEmail() {
return _newEmail;
}

/**
* @return the _newFirstUserName
*/
public String get_newFirstUserName() {
return _newFirstUserName;
}

/**
* @return the _newLastUserName
*/
public String get_newLastUserName() {
return _newLastUserName;
}

/**
* @return the _newMI
*/
public String get_newMI() {
return _newMI;
}

/**
* @param email the _newEmail to set
*/
public void set_newEmail(String email) {
_newEmail = email;
}

/**
* @param firstUserName the _newFirstUserName to set
*/
public void set_newFirstUserName(String firstUserName) {
_newFirstUserName = firstUserName;
}

/**
* @param lastUserName the _newLastUserName to set
*/
public void set_newLastUserName(String lastUserName) {
_newLastUserName = lastUserName;
}

/**
* @param _newmi the _newMI to set
*/
public void set_newMI(String _newmi) {
_newMI = _newmi;
}

/**
* @return the _newUserNameField
*/
public TextField get_newUserNameField() {
return _newUserNameField;
}

/**
* @param userNameField the _newUserNameField to set
*/
public void set_newUserNameField(TextField userNameField) {
_newUserNameField = userNameField;
}


}

package org.example.TSA504.services.UserAuthenticatorImpl - Step 04 Adding some basic Hibernate features into the project.

Refers to... .

package org.example.TSA504.services;

import java.util.List;

import org.example.TSA504.services.interfaces.UserAuthenticator;
import org.example.TSA504.beans.TUser;
import org.example.TSA504.hibernate.HibernateUtil;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;

public class UserAuthenticatorImpl implements UserAuthenticator {

public boolean isValid(String userName, String pwd) {
TUser tu = null;
tu = getTUserByUserName(userName);
if (tu!=null){
if ((tu.getUserName().equals(userName))&&(tu.getPassword().equals(pwd))){
return true;
}
return false;
}
return false;
}
public TUser getTUserByUserName(String userName) {
TUser res = null;
Session session = HibernateUtil.getSession();
Criteria cr = session.createCriteria(TUser.class);
cr.add(Expression.eq("userName", userName));
List result = cr.list();
if (result!=null && result.size()>0) {
res = (TUser)result.get(0); // don't forget about lazy-loading, it may become a pain.
}
session.flush();
//session.close();
return res;
}

public boolean addNewTUser(TUser tUser){
if (tUser!=null){
TUser tu = getTUserByUserName(tUser.getUserName());
if (tu==null){
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.save(tUser);
session.getTransaction().commit();
return true;
}
return false;
}
return false;
}
}

17 May 2007

TSA504.beans.TUser - Step 03, user bean.

package org.example.TSA504.beans;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;


import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;



@Entity
@Table(name = "TUSER") // TUSER is a DB's table name
public class TUser implements Serializable {
private static final long serialVersionUID = 7769115993193444629L;
protected long id; // required
protected String userName; // required
protected String firstName; // required
protected String MI;
protected String lastName; // required
protected String email; // required
protected String password; // required

public TUser() {}

public TUser(String userName) {
this.userName = userName;
}
/**
* @param userName
* @param firstName
* @param mi
* @param lastName
* @param email
* @param password
*/
public TUser(String userName, String firstName, String mi, String lastName, String email, String password) {
super();
this.userName = userName;
this.firstName = firstName;
this.MI = mi;
this.lastName = lastName;
this.email = email;
this.password = password;
}
/**
* @return the id
*/
@Id
@Column (name="id") // the Id column in the TUSER DB table, the pimary key column
@GeneratedValue(strategy = GenerationType.AUTO) //There are several variants can be here, consult the documentation.
public long getId() {
return id;
}
/**
* @return the userName
*/
@Column (name="USERNAME", nullable=false, length = 128 ) //USERNAME column, has to be filled in.
public String getUserName() {
return userName;
}
/**
* @return the firstName
*/
@Column (name="FIRSTNAME", nullable=false, length = 128 )//FIRSTNAME column, has to be filled in.
public String getFirstName() {
return firstName;
}
/**
* @return the mI
*/
@Column (name="MI", nullable=true, length = 16 )//MI column, has not to be filled in.
public String getMI() {
return MI;
}

/**
* @return the lastName
*/
@Column (name="LASTNAME", nullable=false, length = 128 )//LASTNAME column, has to be filled in.
public String getLastName() {
return lastName;
}
/**
* @return the email
*/
@Column (name="EMAIL", nullable=false, length = 256)//EMAIL column, has to be filled in.
public String getEmail() {
return email;
}

/**
* @return the password
*/
@Column (name="PASSWORD", nullable=false, length = 128 )//PASSWORD column, has to be filled in.
public String getPassword() {
return password;
}
//---- set of setProperty() methods:
/**
* @param email the email to set
*/
public void setEmail(String email) {
this.email = email;
}

/**
* @param firstName the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}

/**
* @param id the id to set
*/

public void setId(long id) {
this.id = id;
}

/**
* @param lastName the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}

/**
* @param mi the mI to set
*/
public void setMI(String mi) {
MI = mi;
}

/**
* @param password the password to set
*/

public void setPassword(String password) {
this.password = password;
}

/**
* @param userName the userName to set
*/
public void setUserName(String userName) {
this.userName = userName;
}
//----Implementation of the Serializable interface:
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final TUser other = (TUser) obj;
if (MI == null) {
if (other.MI != null)
return false;
} else if (!MI.equals(other.MI))
return false;
if (email == null) {
if (other.email != null)
return false;
} else if (!email.equals(other.email))
return false;
if (firstName == null) {
if (other.firstName != null)
return false;
} else if (!firstName.equals(other.firstName))
return false;
if (id != other.id)
return false;
if (lastName == null) {
if (other.lastName != null)
return false;
} else if (!lastName.equals(other.lastName))
return false;
if (password == null) {
if (other.password != null)
return false;
} else if (!password.equals(other.password))
return false;
if (userName == null) {
if (other.userName != null)
return false;
} else if (!userName.equals(other.userName))
return false;
return true;
}

/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int PRIME = 31;
int result = super.hashCode();
result = PRIME * result + ((MI == null) ? 0 : MI.hashCode());
result = PRIME * result + ((email == null) ? 0 : email.hashCode());
result = PRIME * result + ((firstName == null) ? 0 : firstName.hashCode());
result = PRIME * result + (int) (id ^ (id >>> 32));
result = PRIME * result + ((lastName == null) ? 0 : lastName.hashCode());
result = PRIME * result + ((password == null) ? 0 : password.hashCode());
result = PRIME * result + ((userName == null) ? 0 : userName.hashCode());
return result;
}
//-- toStrting() implementation, uses some of org.apache.commons.lang.builder classes
public String toString() {
ToStringBuilder sb = new ToStringBuilder(this,
ToStringStyle.DEFAULT_STYLE).append("id", this.id)
.append("username", this.userName)
.append("FN", this.firstName)
.append("MI",this.MI)
.append("LN",this.lastName)
.append("email",this.email);

return sb.toString();
}
}

9 May 2007

TSA503.pages.Login - Step 02

Login.java - Step 02, adding a service

package org.example.TSA503.pages;
import org.apache.tapestry.annotations.*;
import org.apache.tapestry.corelib.components.*;
import org.example.TSA503.services.interfaces.UserAuthenticator;
/**
* Original code was taken from http://tapestry.apache.org/tapestry5/tapestry-core/guide/validation.html
* Modifications:
* - Added getters/setters for all variables
* - Commented UserAuthenticator injection and usage.
*/

public class Login
{
@Persist
private String _userName;
private String _password;
@Inject // was commented for Step 01.
private UserAuthenticator _authenticator; // was commented for Step 01.
@Component(id = "password")
private PasswordField _passwordField;
@Component
private Form _form;
String onSuccess()
{
if (!_authenticator.isValid(_userName, _password)) // was commented for Step 01.
{
_form.recordError(_passwordField, "Invalid user name or password.");
return null; //returns to the same page, i.e. "Login", something like "Try again".
}
return "Start"; //if authentication is valid, navigation will go to the "Start" page.
}
/**
* @return the _password
*/
public String getPassword()
{
return _password;
}
/**
* @param password to set
*/
public void setPassword(String password)
{
_password = password;
}
/**
* @return the _userName
*/
public String getUserName()
{
return _userName;
}
/**
* @param userName to set
*/
public void setUserName(String userName)
{
_userName = userName;
}
/**
* @return the _form
*/
public Form getForm() {
return _form;
}
/**
* @param _form the _form to set
*/
public void setForm(Form _form) {
this._form = _form;
}
/**
* @return the _passwordField
*/
public PasswordField getPasswordField() {
return _passwordField;
}
/**
* @param field the _passwordField to set
*/
public void setPasswordField(PasswordField field) {
_passwordField = field;
}
}

8 May 2007

TSA503.pages.Login - Step 01

Step 01 Login.java

package org.example.TSA503.pages;
import org.apache.tapestry.annotations.*;
import org.apache.tapestry.corelib.components.*;
/**
* Original code was taken from http://tapestry.apache.org/tapestry5/tapestry-core/guide/validation.html
* Modifications:
* - Added getters/setters for all variables
* - Commented UserAuthenticator injection and usage.
*/

public class Login
{
@Persist
private String _userName;
private String _password;
// @Inject
// private UserAuthenticator _authenticator; // Don't need this for now.
@Component(id = "password")
private PasswordField _passwordField;
@Component
private Form _form;
String onSuccess()
{
// if (!_authenticator.isValid(_userName, _password)) // Don't need this for now.
if (!_userName.equals(_password))
{
_form.recordError(_passwordField, "Invalid user name or password.");
return null; //returns to the same page, i.e. "Login", something like "Try again".
}
return "Start"; //if authentication is valid, navigation will go to the "Start" page.
}
/**
* @return the _password
*/
public String getPassword()
{
return _password;
}
/**
* @param password to set
*/
public void setPassword(String password)
{
_password = password;
}
/**
* @return the _userName
*/
public String getUserName()
{
return _userName;
}
/**
* @param userName to set
*/
public void setUserName(String userName)
{
_userName = userName;
}
/**
* @return the _form
*/
public Form getForm() {
return _form;
}
/**
* @param _form the _form to set
*/
public void setForm(Form _form) {
this._form = _form;
}
/**
* @return the _passwordField
*/
public PasswordField getPasswordField() {
return _passwordField;
}
/**
* @param field the _passwordField to set
*/
public void setPasswordField(PasswordField field) {
_passwordField = field;
}
}