Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.ostfalia.entity;
- import java.sql.Date;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.Id;
- @SuppressWarnings("javadoc")
- @Entity
- public class Customer {
- @Id
- @GeneratedValue
- @Column(name = "id")
- private int id;
- private String firstname;
- private String lastname;
- private Date birthday;
- private String email;
- // private Credentials login;
- // private String password;
- public Customer() {
- // TODO Auto-generated constructor stub
- }
- public String getFirstname() {
- return firstname;
- }
- public void setFirstname(final String firstname) {
- this.firstname = firstname;
- }
- public String getLastname() {
- return lastname;
- }
- public void setLastname(final String lastname) {
- this.lastname = lastname;
- }
- public String getEmail() {
- return email;
- }
- public void setEmail(final String email) {
- this.email = email;
- }
- public Date getBirthday() {
- return birthday;
- }
- public void setBirthday(final Date birthday) {
- this.birthday = birthday;
- }
- }
- package de.ostfalia.manager.impl;
- import java.io.Serializable;
- import java.util.List;
- import java.util.logging.Logger;
- import javax.annotation.PostConstruct;
- import javax.enterprise.context.RequestScoped;
- import javax.enterprise.inject.Produces;
- import javax.inject.Inject;
- import javax.inject.Named;
- import javax.persistence.EntityManager;
- import javax.transaction.UserTransaction;
- import de.ostfalia.entity.Customer;
- import de.ostfalia.manager.ICustomerManager;
- @SuppressWarnings("javadoc")
- @Named
- @RequestScoped
- public class CustomerManager implements ICustomerManager, Serializable {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- @Inject
- private transient Logger logger;
- @Inject
- private EntityManager entityManager;
- @Inject
- private UserTransaction utx;
- private Customer customer;
- @PostConstruct
- public void init() {
- this.customer = new Customer();
- }
- @SuppressWarnings("unchecked")
- @Override
- @Produces
- @Named
- public List<Customer> getCustomers() throws Exception {
- try {
- try {
- utx.begin();
- return this.entityManager.createQuery("select c from Customer c").getResultList();
- } finally {
- utx.commit();
- }
- } catch (final Exception e) {
- utx.rollback();
- throw e;
- }
- }
- @Override
- public String saveCustomer() throws Exception {
- try {
- try {
- utx.begin();
- entityManager.persist(this.customer);
- logger.info("Added " + customer);
- } finally {
- utx.commit();
- }
- } catch (final Exception e) {
- utx.rollback();
- throw e;
- }
- return "customer added!";
- }
- @Override
- public Customer findCustomer(final String username, final String password) throws Exception {
- try {
- try {
- utx.begin();
- @SuppressWarnings("unchecked")
- final
- List<Customer> results = entityManager
- .createQuery(
- "select u from User u where u.username=:username and u.password=:password")
- .setParameter("username", username)
- .setParameter("password", password).getResultList();
- if (results.isEmpty()) {
- return null;
- } else if (results.size() > 1) {
- throw new IllegalStateException(
- "Cannot have more than one user with the same username!");
- } else {
- return results.get(0);
- }
- } finally {
- utx.commit();
- }
- } catch (final Exception e) {
- utx.rollback();
- throw e;
- }
- }
- @Override
- @Produces
- @Named
- public Customer getCustomer() {
- // TODO Auto-generated method stub
- return this.customer;
- }
- }
- ...
- <h:form id="form">
- <h:head>
- <h1 align="center">JavaEE - Master</h1>
- </h:head>
- <h:body>
- <h3 align="center">Bitte legen Sie einen neuen Kunden an</h3>
- <h:panelGrid id="panel" columns="4" border="3">
- <h:outputText value="Firstname " />
- <h:inputText id="firstname" value="#{customerManager.customer.setFirstname}" required="true"/>
- <h:outputText value="Lastname " />
- <h:inputText id="lastname" value="#{customerManager.customer.setLastname}" required="true"/>
- <h:outputText value="Birthday " />
- <h:inputText id="birthday" value="#{customerManager.customer.setBirthday}" />
- <h:outputText value="e-mail " />
- <h:inputText id="email" value="#{customerManager.customer.setEmail}" />
- </h:panelGrid>
- <h:commandButton id="button" value="save" action="#{customerManager.saveCustomer}"/>
- </h:body>
- </h:form>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement