Guest User

Untitled

a guest
Mar 2nd, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.05 KB | None | 0 0
  1. package com.ryc.dao;
  2.  
  3. import javax.persistence.EntityManager;
  4. import javax.persistence.EntityManagerFactory;
  5. import javax.persistence.EntityTransaction;
  6. import javax.persistence.NoResultException;
  7. import javax.persistence.Persistence;
  8. import javax.persistence.TypedQuery;
  9.  
  10. import com.ryc.entities.User;
  11.  
  12. public class UserDAO {
  13.  
  14. private static final String PERSISTENCE_UNIT_NAME = "TestPersistence";
  15. private static EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
  16. private static EntityManager entityMgrObj = entityManagerFactory.createEntityManager();
  17. private static EntityTransaction entityTransaction = entityMgrObj.getTransaction();
  18.  
  19. public static User login(User user){
  20. User userFound = null;
  21.  
  22. try{
  23. TypedQuery<User> query = entityMgrObj.createQuery("SELECT u FROM User u WHERE u.userName = :userName and u.password = :password", User.class);
  24. query.setParameter("userName", user.getUserName());
  25. query.setParameter("password", user.getPassword());
  26. userFound = (User)query.getSingleResult();
  27.  
  28. }catch(NoResultException e){
  29.  
  30. }
  31.  
  32.  
  33. return userFound;
  34.  
  35. }
  36.  
  37. }
  38.  
  39. import java.io.IOException;
  40.  
  41. import javax.faces.bean.ManagedBean;
  42. import javax.faces.context.ExternalContext;
  43. import javax.faces.context.FacesContext;
  44. import javax.faces.view.ViewScoped;
  45.  
  46. import com.ryc.dao.UserDAO;
  47. import com.ryc.entities.User;
  48.  
  49.  
  50. @ManagedBean(name = "user")
  51. @ViewScoped
  52. public class UserController {
  53.  
  54. boolean isUsernameValid = false;
  55. boolean validationComplete = false;
  56. boolean isPasswordValid = false;
  57. String userName;
  58. String password;
  59. UserDAO userDAO;
  60.  
  61. public void login() {
  62. User userFound = null;
  63. User userRequest = new User();
  64. userDAO = new UserDAO();
  65. ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
  66.  
  67.  
  68. userRequest.setUserName(userName);
  69. userRequest.setPassword(password);
  70.  
  71. userFound = UserDAO.login(userRequest);
  72.  
  73. if(userFound != null){
  74. try {
  75. ec.redirect(ec.getRequestContextPath() + "/success.xhtml");
  76. } catch (IOException e) {
  77. // TODO Auto-generated catch block
  78. e.printStackTrace();
  79. }
  80. } else {
  81. try {
  82. ec.redirect(ec.getRequestContextPath() + "/login.xhtml");
  83. } catch (IOException e) {
  84. // TODO Auto-generated catch block
  85. e.printStackTrace();
  86. }
  87. }
  88.  
  89.  
  90. }
  91.  
  92. /** * @return the isUsernameValid */
  93. public boolean getIsUsernameValid() {
  94. return isUsernameValid;
  95. }
  96.  
  97. /** * @paramisUsernameValid the isUsernameValid to set */
  98. public void setUsernameValid(boolean isUsernameValid) {
  99. this.isUsernameValid = isUsernameValid;
  100. }
  101.  
  102. /** * @return the isPasswordValid */
  103. public boolean getIsPasswordValid() {
  104. return isPasswordValid;
  105. }
  106.  
  107. /** * @paramisPasswordValid the isPasswordValid to set */
  108. public void setPasswordValid(boolean isPasswordValid) {
  109. this.isPasswordValid = isPasswordValid;
  110. }
  111.  
  112. /** * @return the validationComplete */
  113. public boolean getValidationComplete() {
  114. return validationComplete;
  115. }
  116.  
  117. /** * @paramvalidationComplete the validationComplete to set */
  118. public void setValidationComplete(boolean validationComplete) {
  119. this.validationComplete = validationComplete;
  120. }
  121.  
  122. public String getUserName() {
  123. return userName;
  124. }
  125.  
  126. public void setUserName(String userName) {
  127. this.userName = userName;
  128. }
  129.  
  130. public String getPassword() {
  131. return password;
  132. }
  133.  
  134. public void setPassword(String password) {
  135. this.password = password;
  136. }
  137.  
  138. }
  139.  
  140. package com.ryc.entities;
  141.  
  142.  
  143. import java.io.Serializable;
  144. import java.util.Date;
  145.  
  146. import javax.persistence.Entity;
  147. import javax.persistence.GeneratedValue;
  148. import javax.persistence.GenerationType;
  149. import javax.persistence.Id;
  150. import javax.persistence.Table;
  151.  
  152. @Entity
  153. @Table(name="user")
  154. public class User implements Serializable{
  155.  
  156. private static final long serialVersionUID = 1L;
  157. @Id
  158. @GeneratedValue(strategy = GenerationType.AUTO)
  159. private int id;
  160. private String userName;
  161. private String email;
  162. private String password;
  163. private String passwordSalt;
  164. private Date creationDate;
  165.  
  166. public String getUserName() {
  167. return userName;
  168. }
  169. public void setUserName(String userName) {
  170. this.userName = userName;
  171. }
  172. public String getPassword() {
  173. return password;
  174. }
  175. public void setPassword(String password) {
  176. this.password = password;
  177. }
  178. public String getEmail() {
  179. return email;
  180. }
  181. public void setEmail(String email) {
  182. this.email = email;
  183. }
  184. public String getPasswordSalt() {
  185. return passwordSalt;
  186. }
  187. public void setPasswordSalt(String passwordSalt) {
  188. this.passwordSalt = passwordSalt;
  189. }
  190. public Date getCreationDate() {
  191. return creationDate;
  192. }
  193. public void setCreationDate(Date creationDate) {
  194. this.creationDate = creationDate;
  195. }
  196.  
  197. @Override
  198. public String toString() {
  199. StringBuilder builder = new StringBuilder();
  200. builder.append("User [id=");
  201. builder.append(id);
  202. builder.append(", userName=");
  203. builder.append(userName);
  204. builder.append(", email=");
  205. builder.append(email);
  206. builder.append(", password=");
  207. builder.append(password);
  208. builder.append(", passwordSalt=");
  209. builder.append(passwordSalt);
  210. builder.append(", creationDate=");
  211. builder.append(creationDate);
  212. builder.append("]");
  213. return builder.toString();
  214. }
  215.  
  216.  
  217. }
  218.  
  219. <?xml version="1.0" encoding="UTF-8"?>
  220. <persistence version="2.1"
  221. xmlns="http://xmlns.jcp.org/xml/ns/persistence"
  222. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  223. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
  224. http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  225. <persistence-unit name="TestPersistence"
  226. transaction-type="RESOURCE_LOCAL">
  227.  
  228. <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
  229. <class>com.ryc.entities.User</class>
  230.  
  231. <properties>
  232. <property name="javax.persistence.jdbc.driver"
  233. value="com.mysql.jdbc.Driver" />
  234. <property name="javax.persistence.jdbc.url"
  235. value="jdbc:mysql://localhost:3306/jsftest" />
  236. <property name="javax.persistence.jdbc.user" value="root" />
  237. <property name="javax.persistence.jdbc.password" value="" />
  238. </properties>
  239. </persistence-unit>
  240. </persistence>
Add Comment
Please, Sign In to add comment