Friday, December 17, 2004

Prevayler 2-minute Tutorial

Concise
tutorial on Prevayler.

Here is some sample code based on that tutorial:

/*
* Created on Dec 16, 2004
* @author BXParanj
*/
package registration;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import persistence.UserDAO;

public class RegistrationParser {
private String userName;
private String password;

public RegistrationParser(String fileName) {
String index = findIndex();
System.out.println("Index found : " + index);
try
{
// load the driver into memory
Class.forName("org.relique.jdbc.csv.CsvDriver");
// create a connection. The first command line parameter is assumed to
// be the directory in which the .csv files are held
Connection conn = DriverManager.getConnection("jdbc:relique:csv:" + fileName );
// create a Statement object to execute the query with
Statement stmt = conn.createStatement();
// Select the EBK_USERNAME and EBK_PASSWORD columns from lead.csv file
String sqlQuery = "SELECT EBK_USERNAME, EBK_PASSWORD FROM lead WHERE EBK_USERNAME =" + index ;
ResultSet results = stmt.executeQuery(sqlQuery);

while (results.next())
{
userName = results.getString("EBK_USERNAME");
password = results.getString("EBK_PASSWORD");
System.out.println("EBK_USERNAME= " + userName + " EBK_PASSWORD= " + password);
}
// clean up
results.close();
stmt.close();
conn.close();
}
catch(Exception e)
{
System.out.println("Oops-> " + e);
}
}

public static void main(String[] args) {

RegistrationParser rp = new RegistrationParser(args[0]);
System.out.println("Username - " + rp.getUserName() + " Password - " + rp.getPassword());
}

private String findIndex() {
UserDAO userDAO = new UserDAO();
userDAO.list();
String index = userDAO.getNext();
userDAO.delete(index);
System.out.println("AFTER UPDATE - HERE IS THE LIST : ");
userDAO.list();

return index;
}

public String getPassword() {
return password;
}

public String getUserName() {
return userName;
}
}

/*
* Created on Dec 17, 2004
*/
package persistence;

import java.io.Serializable;

import org.apache.commons.collections.MapIterator;
import org.apache.commons.collections.map.ListOrderedMap;

/**
* @author BXParanj
*/
public class UserList implements Serializable {
private ListOrderedMap users = new ListOrderedMap();

public void save(ListOrderedMap users) {
this.users = users;
}

public void remove(String userName) {
users.remove(userName);
}


public void print() {
MapIterator it = users.mapIterator();

while (it.hasNext()) {
Object key = it.next();
Object value = it.getValue();
System.out.println("KEY " + key + " VALUE : " + value);//it.setValue("newValue");
}
// System.out.println("Checking indexing at 99: " + users.get(99));
}

public ListOrderedMap getUsers() {
return users;
}
}

/*
* Created on Dec 17, 2004
*/
package persistence;

import java.util.Date;

import org.apache.commons.collections.map.ListOrderedMap;
import org.prevayler.TransactionWithQuery;

/**
* @author BXParanj
*/

public class SaveUserTransaction implements TransactionWithQuery {

private final ListOrderedMap users ;

public SaveUserTransaction(ListOrderedMap users) {
this.users = users;
}

public Object executeAndQuery(Object prevalentSystem, Date executionTime) throws Exception {

UserList system = (UserList) prevalentSystem;
system.save(users);

return users;
}
}

package persistence;

import java.io.IOException;

import org.apache.commons.collections.map.ListOrderedMap;
import org.prevayler.Prevayler;
import org.prevayler.PrevaylerFactory;

/**
* @author BXParanj
* Created on Dec 17, 2004
*/
public class UserDAO {
private Prevayler prevayler;

public UserDAO() {
// Create a new prevayler. /userlist-base is the tx-journal directory.
try {
prevayler = PrevaylerFactory.createPrevayler(new UserList(), "/userlist-base");
UserList list = (UserList) prevayler.prevalentSystem();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}

public void save(ListOrderedMap users ) {
try {
prevayler.execute(new SaveUserTransaction(users));
} catch (Exception e) {
e.printStackTrace();
}
}

public void delete(String userName) {
try {
prevayler.execute(new DeleteUserTransaction(userName));
} catch (Exception e) {
e.printStackTrace();
}
}

public void list() {
UserList list = (UserList) prevayler.prevalentSystem();
list.print();
System.out.println("Listing generated through DAO");
}

public String getNext() {
UserList list = (UserList) prevayler.prevalentSystem();
ListOrderedMap listOrderedMap = list.getUsers();
if(!listOrderedMap.isEmpty())
return (String)listOrderedMap.get(0);

return null;
}

public ListOrderedMap retrieve() {
UserList list = (UserList) prevayler.prevalentSystem();
return list.getUsers();
}
}

package persistence;

import java.util.Date;

import org.prevayler.TransactionWithQuery;

public class ListUserTransaction implements TransactionWithQuery {

public ListUserTransaction() {
}

public Object executeAndQuery(Object prevalentSystem, Date executionTime) throws Exception {

UserList system = (UserList) prevalentSystem;
system.print();

return system;
}
}

/*
* Created on Dec 17, 2004
*/
package persistence;

import java.util.Date;

import org.prevayler.Transaction;

/**
* @author BXParanj
*/

public class DeleteUserTransaction implements Transaction {

private final String userName ;

public DeleteUserTransaction(String userName) {
this.userName = userName;
}

public void executeOn(Object prevalentSystem, Date executionTime) {
UserList system = (UserList) prevalentSystem;
system.remove(userName);
}
}

/*
* Created on Dec 16, 2004
* @author BXParanj
*/
package registration;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import org.apache.commons.collections.map.ListOrderedMap;

import persistence.UserDAO;

public class IndexUsers {

public IndexUsers() {
}

public void run(String fileName) {
try
{
// load the driver into memory
Class.forName("org.relique.jdbc.csv.CsvDriver");
// create a connection. The first command line parameter is assumed to
// be the directory in which the .csv files are held
Connection conn = DriverManager.getConnection("jdbc:relique:csv:" + fileName );
// create a Statement object to execute the query with
Statement stmt = conn.createStatement();
// Select the EBK_USERNAME columns from lead.csv file
String sqlQuery = "SELECT EBK_USERNAME FROM lead " ;
ResultSet results = stmt.executeQuery(sqlQuery);

save(results);
// clean up
results.close();
stmt.close();
conn.close();
}
catch(Exception e)
{
System.out.println("Oops-> " + e);
}
}

private void save(ResultSet results) throws Exception {
String userName;
ListOrderedMap userList = new ListOrderedMap();

while (results.next())
{
userName = results.getString("EBK_USERNAME"); System.out.println("EBK_USERNAME= " + userName );
userList.put(userName, "Y");
}
UserDAO userDAO = new UserDAO();
userDAO.save(userList);
}

public static void main(String[] args) {
IndexUsers indexUsers = new IndexUsers();
indexUsers.run(args[0]);
}
}

Tools used :
Jakarta Commons Collections 3.1
Prevayler 2.02.005
and of course cvsjdbc.

Nice thing about this is that I am using DAO to hide the persistence mechanism. I have not complicated the code by making it too generic to accommodate new persistence mechanism.

I have used the Commons Collections API so that I can retain the ordering of the list. I initially tried to use the Properties file instead of Prevayler, problem is that Properties file does not maintain the order.

No comments:

Post a Comment