Author: pete.muir(a)jboss.org
Date: 2010-03-02 14:39:11 -0500 (Tue, 02 Mar 2010)
New Revision: 5987
Added:
examples/trunk/jsf/login/src/main/java/org/jboss/weld/examples/login/Users.java
examples/trunk/jsf/login/src/main/resources/
examples/trunk/jsf/login/src/main/resources/META-INF/
examples/trunk/jsf/login/src/main/resources/META-INF/persistence.xml
examples/trunk/jsf/login/src/main/resources/import.sql
Modified:
examples/trunk/jsf/login/pom.xml
examples/trunk/jsf/login/src/main/java/org/jboss/weld/examples/login/Credentials.java
examples/trunk/jsf/login/src/main/java/org/jboss/weld/examples/login/Login.java
examples/trunk/jsf/login/src/main/java/org/jboss/weld/examples/login/User.java
Log:
start to add persistence to the example
Modified: examples/trunk/jsf/login/pom.xml
===================================================================
--- examples/trunk/jsf/login/pom.xml 2010-03-02 18:41:14 UTC (rev 5986)
+++ examples/trunk/jsf/login/pom.xml 2010-03-02 19:39:11 UTC (rev 5987)
@@ -1,5 +1,6 @@
<?xml version="1.0"?>
-<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
+<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
@@ -43,12 +44,11 @@
<scope>provided</scope>
</dependency>
- <!--
- <dependency> <groupId>org.jboss.el</groupId>
- <artifactId>jboss-el</artifactId> <exclusions>
<exclusion>
- <groupId>javax.el</groupId>
<artifactId>el-api</artifactId>
- </exclusion> </exclusions> </dependency>
- -->
+ <dependency>
+ <groupId>javax.persistence</groupId>
+ <artifactId>persistence-api</artifactId>
+ <scope>provided</scope>
+ </dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
@@ -164,7 +164,7 @@
<
connection>scm:svn:http://anonsvn.jboss.org/repos/weld/examples/trunk/...
<
developerConnection>scm:svn:https://svn.jboss.org/repos/weld/examples/...
<
url>http://fisheye.jboss.org/browse/weld/examples/trunk/jsf/login</...
- </scm>
+ </scm>
</project>
Modified:
examples/trunk/jsf/login/src/main/java/org/jboss/weld/examples/login/Credentials.java
===================================================================
---
examples/trunk/jsf/login/src/main/java/org/jboss/weld/examples/login/Credentials.java 2010-03-02
18:41:14 UTC (rev 5986)
+++
examples/trunk/jsf/login/src/main/java/org/jboss/weld/examples/login/Credentials.java 2010-03-02
19:39:11 UTC (rev 5987)
@@ -1,21 +1,36 @@
package org.jboss.weld.examples.login;
-import java.io.Serializable;
-
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Default;
import javax.inject.Named;
-@RequestScoped @Named @Default
-public class Credentials implements Serializable {
-
- private String username;
- private String password;
-
- public String getUsername() { return username; }
- public void setUsername(String username) { this.username = username; }
-
- public String getPassword() { return password; }
- public void setPassword(String password) { this.password = password; }
-
+@RequestScoped
+@Named
+@Default
+public class Credentials
+{
+
+ private String username;
+ private String password;
+
+ public String getUsername()
+ {
+ return username;
+ }
+
+ public void setUsername(String username)
+ {
+ this.username = username;
+ }
+
+ public String getPassword()
+ {
+ return password;
+ }
+
+ public void setPassword(String password)
+ {
+ this.password = password;
+ }
+
}
\ No newline at end of file
Modified: examples/trunk/jsf/login/src/main/java/org/jboss/weld/examples/login/Login.java
===================================================================
---
examples/trunk/jsf/login/src/main/java/org/jboss/weld/examples/login/Login.java 2010-03-02
18:41:14 UTC (rev 5986)
+++
examples/trunk/jsf/login/src/main/java/org/jboss/weld/examples/login/Login.java 2010-03-02
19:39:11 UTC (rev 5987)
@@ -1,7 +1,6 @@
package org.jboss.weld.examples.login;
import java.io.Serializable;
-import java.util.Arrays;
import java.util.List;
import javax.enterprise.context.SessionScoped;
@@ -10,46 +9,54 @@
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
-@SessionScoped @Named
-public class Login implements Serializable {
+@SessionScoped
+@Named
+public class Login implements Serializable
+{
- @Inject Credentials credentials;
- //@PersistenceContext EntityManager userDatabase;
+ private static final long serialVersionUID = 7965455427888195913L;
- private User user;
-
- public void login() {
-
- List<User> results = /*userDatabase.createQuery(
- "select u from User u where u.username=:username and
u.password=:password")
- .setParameter("username", credentials.getUsername())
- .setParameter("password", credentials.getPassword())
- .getResultList();*/
-
- Arrays.asList( new User(credentials.getUsername(), "Your Name",
credentials.getPassword()) );
-
- if ( !results.isEmpty() ) {
- user = results.get(0);
- FacesContext.getCurrentInstance()
- .addMessage(null, new FacesMessage("Welcome, " +
user.getName()));
- }
-
- }
-
- public void logout() {
- FacesContext.getCurrentInstance()
- .addMessage(null, new FacesMessage("Goodbye, " + user.getName()));
- user = null;
- }
-
- public boolean isLoggedIn() {
- return user!=null;
- }
-
- @Produces @LoggedIn
- public User getCurrentUser() {
- return user;
- }
+ @Inject
+ private Credentials credentials;
+
+ @PersistenceContext
+ private EntityManager userDatabase;
+ private User currentUser;
+
+ @SuppressWarnings("unchecked")
+ public void login()
+ {
+
+ List<User> results = userDatabase.createQuery("select u from User u
where u.username=:username and
u.password=:password").setParameter("username",
credentials.getUsername()).setParameter("password",
credentials.getPassword()).getResultList();
+
+ if (!results.isEmpty())
+ {
+ currentUser = results.get(0);
+ FacesContext.getCurrentInstance().addMessage(null, new
FacesMessage("Welcome, " + currentUser.getName()));
+ }
+
+ }
+
+ public void logout()
+ {
+ FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Goodbye,
" + currentUser.getName()));
+ currentUser = null;
+ }
+
+ public boolean isLoggedIn()
+ {
+ return currentUser != null;
+ }
+
+ @Produces
+ @LoggedIn
+ public User getCurrentUser()
+ {
+ return currentUser;
+ }
+
}
\ No newline at end of file
Modified: examples/trunk/jsf/login/src/main/java/org/jboss/weld/examples/login/User.java
===================================================================
---
examples/trunk/jsf/login/src/main/java/org/jboss/weld/examples/login/User.java 2010-03-02
18:41:14 UTC (rev 5986)
+++
examples/trunk/jsf/login/src/main/java/org/jboss/weld/examples/login/User.java 2010-03-02
19:39:11 UTC (rev 5987)
@@ -1,27 +1,47 @@
package org.jboss.weld.examples.login;
-public class User {
- private String username;
- private String name;
- private String password;
-
- public User(String username, String name, String password) {
- super();
- this.username = username;
- this.name = name;
- this.password = password;
- }
-
- public String getUsername() {
- return username;
- }
-
- public String getName() {
- return name;
- }
-
- public void setPassword(String password) {
- this.password = password;
- }
-
+import javax.persistence.Entity;
+import javax.persistence.Id;
+
+@Entity
+public class User
+{
+ @Id
+ private String username;
+ private String name;
+ @SuppressWarnings("unused")
+ private String password;
+
+ public User() {}
+
+ public String getUsername()
+ {
+ return username;
+ }
+
+ public void setName(String name)
+ {
+ this.name = name;
+ }
+
+ public void setUsername(String username)
+ {
+ this.username = username;
+ }
+
+ public String getName()
+ {
+ return name;
+ }
+
+ public void setPassword(String password)
+ {
+ this.password = password;
+ }
+
+ public String getPassword()
+ {
+ return password;
+ }
+
}
Added: examples/trunk/jsf/login/src/main/java/org/jboss/weld/examples/login/Users.java
===================================================================
--- examples/trunk/jsf/login/src/main/java/org/jboss/weld/examples/login/Users.java
(rev 0)
+++
examples/trunk/jsf/login/src/main/java/org/jboss/weld/examples/login/Users.java 2010-03-02
19:39:11 UTC (rev 5987)
@@ -0,0 +1,24 @@
+package org.jboss.weld.examples.login;
+
+import java.util.List;
+
+import javax.enterprise.context.RequestScoped;
+import javax.enterprise.inject.Produces;
+import javax.inject.Named;
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+
+public class Users
+{
+
+ @PersistenceContext
+ private EntityManager userDatabase;
+
+ @SuppressWarnings("unchecked")
+ @Produces @Named @RequestScoped
+ public List<User> getUsers()
+ {
+ return userDatabase.createQuery("select u from User u").getResultList();
+ }
+
+}
Property changes on:
examples/trunk/jsf/login/src/main/java/org/jboss/weld/examples/login/Users.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Added: examples/trunk/jsf/login/src/main/resources/META-INF/persistence.xml
===================================================================
--- examples/trunk/jsf/login/src/main/resources/META-INF/persistence.xml
(rev 0)
+++ examples/trunk/jsf/login/src/main/resources/META-INF/persistence.xml 2010-03-02
19:39:11 UTC (rev 5987)
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<persistence
xmlns="http://java.sun.com/xml/ns/persistence"
+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
+ version="1.0">
+ <persistence-unit name="loginDatabase">
+ <provider>org.hibernate.ejb.HibernatePersistence</provider>
+ <jta-data-source>java:/DefaultDS</jta-data-source>
+ <properties>
+ <property name="hibernate.hbm2ddl.auto"
value="create-drop"/>
+ <property name="hibernate.show_sql" value="true"/>
+ </properties>
+ </persistence-unit>
+</persistence>
Property changes on: examples/trunk/jsf/login/src/main/resources/META-INF/persistence.xml
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added: examples/trunk/jsf/login/src/main/resources/import.sql
===================================================================
--- examples/trunk/jsf/login/src/main/resources/import.sql (rev
0)
+++ examples/trunk/jsf/login/src/main/resources/import.sql 2010-03-02 19:39:11 UTC (rev
5987)
@@ -0,0 +1 @@
+insert into User (username, name, password) values ('demo', 'Demo User',
'demo')
\ No newline at end of file