Skip to content

Instantly share code, notes, and snippets.

@Leward
Last active January 3, 2016 10:49
Show Gist options
  • Save Leward/8452049 to your computer and use it in GitHub Desktop.
Save Leward/8452049 to your computer and use it in GitHub Desktop.
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>SupRails - Add a customer order</title>
</head>
<body>
<p>Trip from ${trip.departureStation.name} to ${trip.arrivalStation.name} for ${trip.price}€</p>
<form method="POST">
<div>
<label for="firstname">First name</label>
<input type="text" id="firstname" name="firstname" />
</div>
<div>
<label for="lastname">Last name</label>
<input type="text" id="lastname" name="lastname" />
</div>
<div>
<label for="email">E-mail</label>
<input type="text" id="email" name="email" />
</div>
<div>
<input type="submit" value="Add" />
</div>
</form>
</body>
</html>
package com.supinfo.suprails.web.servlet;
import com.supinfo.suprails.entity.Customer;
import com.supinfo.suprails.entity.CustomerOrder;
import com.supinfo.suprails.entity.Trip;
import com.supinfo.suprails.service.CustomerOrderService;
import com.supinfo.suprails.service.TripService;
import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name = "AddCustomerOrderServlet", urlPatterns = {"/admin/orders/add"})
public class AddCustomerOrderServlet extends HttpServlet {
@EJB
private CustomerOrderService customerOrderService;
@EJB
private TripService tripService;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Long tripId = new Long(req.getParameter("trip_id"));
Trip trip = tripService.findTripById(tripId);
req.setAttribute("trip", trip);
req.getRequestDispatcher("/jsp/addCustomerOrder.jsp").forward(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Long tripId = new Long(req.getParameter("trip_id"));
Trip trip = tripService.findTripById(tripId);
Customer customer = new Customer();
customer.setEmail(req.getParameter("email"));
customer.setFirstName(req.getParameter("firstname"));
customer.setLastName(req.getParameter("lastname"));
CustomerOrder customerOrder = new CustomerOrder();
customerOrder.setTrip(trip);
customerOrder.setCustomer(customer);
customerOrderService.processCustomerOrder(customerOrder);
resp.sendRedirect(req.getContextPath() + "/trips");
}
}
package com.supinfo.suprails.entity;
import java.io.Serializable;
import javax.persistence.Embeddable;
@Embeddable
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
private String firstName;
private String lastName;
private String email;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
package com.supinfo.suprails.entity;
import java.io.Serializable;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
@Entity
public class CustomerOrder implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Embedded
private Customer customer;
@ManyToOne
@JoinColumn
private Trip trip;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public Trip getTrip() {
return trip;
}
public void setTrip(Trip trip) {
this.trip = trip;
}
}
package com.supinfo.suprails.service;
import java.util.Properties;
import java.util.concurrent.Future;
import javax.ejb.AsyncResult;
import javax.ejb.Asynchronous;
import javax.ejb.Stateless;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
@Stateless
@Asynchronous
public class MailService {
public Future<Void> send(String from, String to, String subject, String body) {
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", "localhost");
Session session = Session.getDefaultInstance(properties);
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
message.setText(body);
Transport.send(message);
return new AsyncResult<Void>(null);
}
catch (MessagingException e) {
e.printStackTrace();
return new AsyncResult<Void>(null);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment