[jboss-cvs] jboss-seam/examples/wiki/src/org/jboss/seam/wiki/core/dao ...
Christian Bauer
christian.bauer at jboss.com
Wed Feb 21 11:24:09 EST 2007
User: cbauer
Date: 07/02/21 11:24:09
Added: examples/wiki/src/org/jboss/seam/wiki/core/dao
WikiRoot.java NodeDAO.java UserDAO.java
Log:
User registration/login and some security
JBSEAM-870
JBSEAM-871
JBSEAM-874
Revision Changes Path
1.1 date: 2007/02/21 16:24:09; author: cbauer; state: Exp;jboss-seam/examples/wiki/src/org/jboss/seam/wiki/core/dao/WikiRoot.java
Index: WikiRoot.java
===================================================================
package org.jboss.seam.wiki.core.dao;
import org.jboss.seam.annotations.*;
import org.jboss.seam.ScopeType;
import org.jboss.seam.wiki.core.node.Directory;
import javax.persistence.EntityManager;
@Name("wikiRoot")
@Scope(ScopeType.CONVERSATION)
public class WikiRoot {
@In
protected EntityManager entityManager;
protected Directory wikiRoot;
@Unwrap
public Directory getWikiRoot() {
if (wikiRoot == null) loadWikiRoot();
return wikiRoot;
}
@Transactional
private void loadWikiRoot() {
entityManager.joinTransaction();
try {
wikiRoot =(Directory)entityManager
.createQuery("select d from Directory d where d.parent is null")
.getSingleResult();
} catch (RuntimeException ex) {
throw new RuntimeException("You need to INSERT at least one parentless directory into the database", ex);
}
}
}
1.1 date: 2007/02/21 16:24:09; author: cbauer; state: Exp;jboss-seam/examples/wiki/src/org/jboss/seam/wiki/core/dao/NodeDAO.java
Index: NodeDAO.java
===================================================================
package org.jboss.seam.wiki.core.dao;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Transactional;
import org.jboss.seam.wiki.core.node.Node;
import org.jboss.seam.wiki.core.node.Directory;
import org.jboss.seam.wiki.core.node.Document;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.NoResultException;
/**
* DAO for Nodes.
* <p>
* The primary reason why this DAO exists is the broken JPA specification. There is no reason
* why query.getSingleResult() should throw any exception if the query result is empty. It should
* just return <tt>null</tt>, like Hibernates query.uniqueResult() method. So instead of using
* the EntityManager directly, must users, like me, will outsource this exception wrapping into
* a DAO. Hey, this sounds like a job for Spring? Or maybe we should fix the specification...
*
* @author Christian Bauer
*
*/
@Name("nodeDAO")
public class NodeDAO {
@In
protected EntityManager entityManager;
@In(create = true)
Directory wikiRoot;
@Transactional
public Node findNode(Long nodeId) {
entityManager.joinTransaction();
try {
return entityManager.find(Node.class, nodeId);
} catch (EntityNotFoundException ex) {
}
return null;
}
@Transactional
public Node findNodeInArea(Long areaNumber, String wikiname) {
entityManager.joinTransaction();
try {
return (Node) entityManager
.createQuery("select n from Node n where n.areaNumber = :areaNumber and n.wikiname = :wikiname")
.setParameter("areaNumber", areaNumber)
.setParameter("wikiname", wikiname)
.getSingleResult();
} catch (EntityNotFoundException ex) {
} catch (NoResultException ex) {
}
return null;
}
@Transactional
public Document findDocumentInArea(Long areaNumber, String wikiname) {
entityManager.joinTransaction();
try {
return (Document) entityManager
.createQuery("select d from Document d where d.areaNumber = :areaNumber and d.wikiname = :wikiname")
.setParameter("areaNumber", areaNumber)
.setParameter("wikiname", wikiname)
.getSingleResult();
} catch (EntityNotFoundException ex) {
} catch (NoResultException ex) {
}
return null;
}
@Transactional
public Directory findDirectoryInArea(Long areaNumber, String wikiname) {
entityManager.joinTransaction();
try {
return (Directory) entityManager
.createQuery("select d from Directory d where d.areaNumber = :areaNumber and d.wikiname = :wikiname")
.setParameter("areaNumber", areaNumber)
.setParameter("wikiname", wikiname)
.getSingleResult();
} catch (EntityNotFoundException ex) {
} catch (NoResultException ex) {
}
return null;
}
@Transactional
public Directory findArea(String wikiname) {
entityManager.joinTransaction();
try {
return (Directory) entityManager
.createQuery("select d from Directory d where d.parent = :root and d.wikiname = :wikiname")
.setParameter("root", wikiRoot)
.setParameter("wikiname", wikiname)
.getSingleResult();
} catch (EntityNotFoundException ex) {
} catch (NoResultException ex) {
}
return null;
}
// I need these methods because find() is broken, e.g. find(Document,1) would return a Directory if the
// persistence context contains a directory with id 1... even more annoying, I need to catch NoResultException,
// so there really is no easy and correct way to look for the existence of a row.
// TODO: A new Hibernate version should fix find()/get() - the old JBoss AS 4.0.5 version is broken
// ... or is it not: http://opensource.atlassian.com/projects/hibernate/browse/HHH-2352
@Transactional
public Document findDocument(Long documentId) {
entityManager.joinTransaction();
try {
return (Document) entityManager
.createQuery("select d from Document d where d.id = :id")
.setParameter("id", documentId)
.getSingleResult();
} catch (EntityNotFoundException ex) {
} catch (NoResultException ex) {
}
return null;
}
@Transactional
public Directory findDirectory(Long directoryId) {
entityManager.joinTransaction();
try {
return (Directory) entityManager
.createQuery("select d from Directory d where d.id = :id")
.setParameter("id", directoryId)
.getSingleResult();
} catch (EntityNotFoundException ex) {
} catch (NoResultException ex) {
}
return null;
}
}
1.1 date: 2007/02/21 16:24:09; author: cbauer; state: Exp;jboss-seam/examples/wiki/src/org/jboss/seam/wiki/core/dao/UserDAO.java
Index: UserDAO.java
===================================================================
package org.jboss.seam.wiki.core.dao;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Transactional;
import org.jboss.seam.wiki.core.users.User;
import org.jboss.seam.wiki.core.users.Role;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.NoResultException;
import javax.persistence.Query;
@Name("userDAO")
public class UserDAO {
@In
protected EntityManager entityManager;
@Transactional
public User findUser(String username, boolean onlyActivated) {
entityManager.joinTransaction();
StringBuffer query = new StringBuffer("select u from User u where u.username = :username");
if (onlyActivated) query.append(" and u.activated = true");
try {
return (User) entityManager
.createQuery(query.toString())
.setParameter("username", username)
.getSingleResult();
} catch (EntityNotFoundException ex) {
} catch (NoResultException ex) {
}
return null;
}
@Transactional
public User findUserWithActivationCode(String activationCode) {
entityManager.joinTransaction();
StringBuffer query = new StringBuffer("select u from User u where u.activationCode = :activationCode");
try {
return (User) entityManager
.createQuery(query.toString())
.setParameter("activationCode", activationCode)
.getSingleResult();
} catch (EntityNotFoundException ex) {
} catch (NoResultException ex) {
}
return null;
}
@Transactional
public Role findRole(String rolename) {
entityManager.joinTransaction();
try {
return (Role) entityManager
.createQuery("select r from Role r where r.name = :name")
.setParameter("name", rolename)
.getSingleResult();
} catch (EntityNotFoundException ex) {
} catch (NoResultException ex) {
}
return null;
}
}
More information about the jboss-cvs-commits
mailing list