20 September 2007

Step 08 org.example.TSA.value - SessionData

Step 08
SessionData
is an object to store userName and sessionExists flag between pages, so if a session doesn't exist then user will be redirected t Login page.

package org.example.TSA.value;

public class SessionData {
private String _userName;
private boolean sessionExists;

/**
* @return the sessionExists
*/
public boolean isSessionExists() {
return sessionExists;
}
/**
* @param exists the sessionExists to set
*/
public void setSessionExists(boolean exists) {
sessionExists = exists;
}
/**
* @return the userName
*/
public String getUserName() {
return _userName;
}
/**
* @param userName the userName to set
*/
public void setUserName(String userName) {
this._userName = userName;
}
}

Step 08 , org.example.TSA.pages.spages - LoginProtected

Step 08
Base (super) page class for pages which require checking whether user has logged in or not.


package org.example.TSA.pages.spages;


import org.apache.tapestry.annotations.ApplicationState;
import org.example.TSA.value.SessionData;
public abstract class LoginProtected {
@ApplicationState
private SessionData _session;

Object onActivate() {
if(!_session.isSessionExists()){
return "Login";
}
return null;
}

/**
* @return the _session
*/
public SessionData get_session() {
return _session;
}
}

21 July 2007

TSA504.pages.UpdateExisting - Step 06

Main changes are in green colour:


package org.example.TSA504.pages;


import org.apache.tapestry.annotations.ApplicationState;
import org.apache.tapestry.annotations.Inject;
import org.example.TSA504.beans.TUser;
import org.example.TSA504.services.interfaces.UserAuthenticator;
/* BeanEditForm usage example from the ScreenCast#4 - http://tapestry.apache.org/tapestry5/screencast.html
* To get more details - http://elozovan.blogspot.com
* */

public class UpdateExisting {

@ApplicationState
private TUser _tUser; //TUser bean.
private String _tUserName;
@Inject
private UserAuthenticator _authenticator;
/* @Component //for future...
private BeanEditForm _form;*/

String onSuccess() {
if (_tUser==null){ return null;}

if (!(_authenticator.addNewTUser(_tUser))){
//_form.recordError("Sorry, a error has occured upon adding the user"); // For some reason BeanEditForm
hasn't recordError() (inspite of JavaDoc).
return null;
}
_tUser = null;
return "Start";
}
/*See for theory: http://tapestry.apache.org/tapestry5/tapestry-core/guide/pagenav.html
*onActivate()+ onPassivate() are used for getting TUser fields values upon selecting on the Home page. */
void onActivate(String userName)
{
_tUserName = userName;
_tUser = _authenticator.getTUserByUserName(_tUserName);
}
String onPassivate() { return _tUserName; }
/**
* @return the (TUser)_tUser object.
*/
public TUser getTUser() {
return _tUser;
}
}

Home.html, Step XY - BeanEditForm and Grid

<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
<head>
<title>Home</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>

<style type="text/css">
#spisok {
position: absolute;
left: 40%;
top: 30%;
margin-right: auto;
margin-left: auto;
}
</style>
<body>
[<a t:type="PageLink" page="Start">Start</a>]
<br/>
[<a t:type="PageLink" page="AddNew">Add New/a>]
<br/>
[<a t:type="PageLink" page="Registration">Registration</a>]
<div id="spisok" title="Рыба" align = "justify">
<table t:type="grid" rowsPerPage="5" source="usersList">
<t:parameter name="passwordCell">
*****
</t:parameter>
</table>
</div>

</body>
</html>

TSA504.pages.Registration, Step XY - BeanEditForm and Grid

package org.example.TSA504.pages;

import org.apache.tapestry.annotations.ApplicationState;
import org.apache.tapestry.annotations.Inject;
import org.example.TSA504.beans.TUser;
import org.example.TSA504.services.interfaces.UserAuthenticator;
/* BeanEditForm usage example from the ScreenCast#4 - http://tapestry.apache.org/tapestry5/screencast.html*/

public class Registration {

@ApplicationState
private TUser _addnew;
@Inject
private UserAuthenticator _authenticator;
/* @Component //for future...
private BeanEditForm _form;*/

String onSuccess() {
if (_addnew==null){ return null;}

if (!(_authenticator.addNewTUser(_addnew))){
//_form.recordError("Sorry, a error has occured upon adding the user"); // For some reason BeanEditForm hasn't recordError() (inspite of JavaDoc).
return null;
}
_addnew = null; //Destroying user data from "session"
return "Start";
}
/**
* @return the _addnewPage
*/
public TUser getAddnew() {
return _addnew;
}
}

Registration.html, Step XY - BeanEditForm and Grid

<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
<head>
<title>Registration</title>
</head>
<body>
<h1>Enter your registration information below</h1>

<form t:type="BeanEditForm" object="addnew" submitLabel="Create"/>
[<a t:type="PageLink" page="Start">Start</a>]
<br/>
[<a type="PageLink" page="Login">Login</a>]
</body>
</html>

11 June 2007

TSA504.pages.Home -Step 06

Refers to ...

Only last modifications are shown:

public class Home {
@Inject
private BeanModelSource _beanModelSource;
@Inject
private ComponentResources _resources;
@Retain
private BeanModel _model;
...

//Inspired by http://tapestry.apache.org/tapestry5/tapestry-core/guide/beaneditform.html
void pageLoaded()
{
_model = _beanModelSource.create(TUser.class, true, _resources);
_model.remove("password"); //Just for "password" column not to be displayed on the Home page, in the grid. But is's displayed on BeanEditFromPage (UpdateExisting).
}

...
}

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;
}
}