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