Advertisement
Guest User

Untitled

a guest
Mar 28th, 2016
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.24 KB | None | 0 0
  1. package de.ostfalia.entity;
  2.  
  3. import java.sql.Date;
  4.  
  5. import javax.persistence.Column;
  6. import javax.persistence.Entity;
  7. import javax.persistence.GeneratedValue;
  8. import javax.persistence.Id;
  9.  
  10. @SuppressWarnings("javadoc")
  11. @Entity
  12. public class Customer {
  13. @Id
  14. @GeneratedValue
  15. @Column(name = "id")
  16. private int id;
  17. private String firstname;
  18. private String lastname;
  19. private Date birthday;
  20. private String email;
  21.  
  22. // private Credentials login;
  23. // private String password;
  24.  
  25. public Customer() {
  26. // TODO Auto-generated constructor stub
  27. }
  28.  
  29. public String getFirstname() {
  30. return firstname;
  31. }
  32.  
  33. public void setFirstname(final String firstname) {
  34. this.firstname = firstname;
  35. }
  36.  
  37. public String getLastname() {
  38. return lastname;
  39. }
  40.  
  41. public void setLastname(final String lastname) {
  42. this.lastname = lastname;
  43. }
  44.  
  45. public String getEmail() {
  46. return email;
  47. }
  48.  
  49. public void setEmail(final String email) {
  50. this.email = email;
  51. }
  52.  
  53. public Date getBirthday() {
  54. return birthday;
  55. }
  56.  
  57. public void setBirthday(final Date birthday) {
  58. this.birthday = birthday;
  59. }
  60.  
  61. }
  62.  
  63. package de.ostfalia.manager.impl;
  64.  
  65. import java.io.Serializable;
  66. import java.util.List;
  67. import java.util.logging.Logger;
  68.  
  69. import javax.annotation.PostConstruct;
  70. import javax.enterprise.context.RequestScoped;
  71. import javax.enterprise.inject.Produces;
  72. import javax.inject.Inject;
  73. import javax.inject.Named;
  74. import javax.persistence.EntityManager;
  75. import javax.transaction.UserTransaction;
  76.  
  77. import de.ostfalia.entity.Customer;
  78. import de.ostfalia.manager.ICustomerManager;
  79.  
  80. @SuppressWarnings("javadoc")
  81. @Named
  82. @RequestScoped
  83. public class CustomerManager implements ICustomerManager, Serializable {
  84. /**
  85. *
  86. */
  87. private static final long serialVersionUID = 1L;
  88.  
  89. @Inject
  90. private transient Logger logger;
  91. @Inject
  92. private EntityManager entityManager;
  93. @Inject
  94. private UserTransaction utx;
  95. private Customer customer;
  96.  
  97. @PostConstruct
  98. public void init() {
  99. this.customer = new Customer();
  100. }
  101.  
  102. @SuppressWarnings("unchecked")
  103. @Override
  104. @Produces
  105. @Named
  106. public List<Customer> getCustomers() throws Exception {
  107. try {
  108. try {
  109. utx.begin();
  110. return this.entityManager.createQuery("select c from Customer c").getResultList();
  111. } finally {
  112. utx.commit();
  113. }
  114. } catch (final Exception e) {
  115. utx.rollback();
  116. throw e;
  117. }
  118. }
  119.  
  120. @Override
  121. public String saveCustomer() throws Exception {
  122. try {
  123. try {
  124. utx.begin();
  125. entityManager.persist(this.customer);
  126. logger.info("Added " + customer);
  127. } finally {
  128. utx.commit();
  129. }
  130. } catch (final Exception e) {
  131. utx.rollback();
  132. throw e;
  133. }
  134. return "customer added!";
  135. }
  136.  
  137. @Override
  138. public Customer findCustomer(final String username, final String password) throws Exception {
  139. try {
  140. try {
  141.  
  142. utx.begin();
  143.  
  144. @SuppressWarnings("unchecked")
  145. final
  146.  
  147. List<Customer> results = entityManager
  148. .createQuery(
  149. "select u from User u where u.username=:username and u.password=:password")
  150. .setParameter("username", username)
  151. .setParameter("password", password).getResultList();
  152.  
  153. if (results.isEmpty()) {
  154. return null;
  155.  
  156. } else if (results.size() > 1) {
  157. throw new IllegalStateException(
  158. "Cannot have more than one user with the same username!");
  159. } else {
  160. return results.get(0);
  161. }
  162. } finally {
  163. utx.commit();
  164. }
  165. } catch (final Exception e) {
  166. utx.rollback();
  167. throw e;
  168. }
  169. }
  170.  
  171. @Override
  172. @Produces
  173. @Named
  174. public Customer getCustomer() {
  175. // TODO Auto-generated method stub
  176. return this.customer;
  177. }
  178. }
  179.  
  180. ...
  181. <h:form id="form">
  182. <h:head>
  183. <h1 align="center">JavaEE - Master</h1>
  184. </h:head>
  185.  
  186. <h:body>
  187. <h3 align="center">Bitte legen Sie einen neuen Kunden an</h3>
  188. <h:panelGrid id="panel" columns="4" border="3">
  189. <h:outputText value="Firstname " />
  190. <h:inputText id="firstname" value="#{customerManager.customer.setFirstname}" required="true"/>
  191.  
  192. <h:outputText value="Lastname " />
  193. <h:inputText id="lastname" value="#{customerManager.customer.setLastname}" required="true"/>
  194.  
  195. <h:outputText value="Birthday " />
  196. <h:inputText id="birthday" value="#{customerManager.customer.setBirthday}" />
  197.  
  198. <h:outputText value="e-mail " />
  199. <h:inputText id="email" value="#{customerManager.customer.setEmail}" />
  200. </h:panelGrid>
  201. <h:commandButton id="button" value="save" action="#{customerManager.saveCustomer}"/>
  202. </h:body>
  203. </h:form>
  204. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement