Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Enhance Create Ticket Functionality: After logging in. If you click create the c

ID: 3851142 • Letter: E

Question

Enhance Create Ticket Functionality:

After logging in. If you click create the create ticket link on the left side. You see an option to include Subject , body and attachment option for creating a ticket. Add a drop down box to add the named status with options open, in progress and closed. As of now once you click the submit button, if you look into the files. Its getting stored in java objects. If you restart the server, these information would be lost. So add modifications to store these information, creating a new table ticket in mysql database. Also add a foreign key relation to a table to store the userid who created the ticket. Make sure viewticket and list ticket functionalities work after this change.

Related Files :

-ticketForm.jsp, TicketServlet.java

package com.wrox;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.Instant;
import java.util.LinkedHashMap;
import java.util.Map;

@WebServlet(
name = "ticketServlet",
urlPatterns = {"/tickets"},
loadOnStartup = 1
)
@MultipartConfig(
fileSizeThreshold = 5_242_880, //5MB
maxFileSize = 20_971_520L, //20MB
maxRequestSize = 41_943_040L //40MB
)
public class TicketServlet extends HttpServlet
{
private volatile int TICKET_ID_SEQUENCE = 1;

private Map<Integer, Ticket> ticketDatabase = new LinkedHashMap<>();

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String action = request.getParameter("action");
if(action == null)
action = "list";
switch(action)
{
case "create":
this.showTicketForm(request, response);
break;
case "view":
this.viewTicket(request, response);
break;
case "download":
this.downloadAttachment(request, response);
break;
case "list":
default:
this.listTickets(request, response);
break;
}
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String action = request.getParameter("action");
if(action == null)
action = "list";
switch(action)
{
case "create":
this.createTicket(request, response);
break;
case "list":
default:
response.sendRedirect("tickets");
break;
}
}

private void showTicketForm(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
request.getRequestDispatcher("/WEB-INF/jsp/view/ticketForm.jsp")
.forward(request, response);
}

private void viewTicket(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
String idString = request.getParameter("ticketId");
Ticket ticket = this.getTicket(idString, response);
if(ticket == null)
return;

request.setAttribute("ticketId", idString);
request.setAttribute("ticket", ticket);

request.getRequestDispatcher("/WEB-INF/jsp/view/viewTicket.jsp")
.forward(request, response);
}

private void downloadAttachment(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
String idString = request.getParameter("ticketId");
Ticket ticket = this.getTicket(idString, response);
if(ticket == null)
return;

String name = request.getParameter("attachment");
if(name == null)
{
response.sendRedirect("tickets?action=view&ticketId=" + idString);
return;
}

Attachment attachment = ticket.getAttachment(name);
if(attachment == null)
{
response.sendRedirect("tickets?action=view&ticketId=" + idString);
return;
}

response.setHeader("Content-Disposition",
"attachment; filename=" + attachment.getName());
response.setContentType("application/octet-stream");

ServletOutputStream stream = response.getOutputStream();
stream.write(attachment.getContents());
}

private void listTickets(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
request.setAttribute("ticketDatabase", this.ticketDatabase);

request.getRequestDispatcher("/WEB-INF/jsp/view/listTickets.jsp")
.forward(request, response);
}

private void createTicket(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
Ticket ticket = new Ticket();
ticket.setCustomerName(
(String)request.getSession().getAttribute("username")
);
ticket.setSubject(request.getParameter("subject"));
ticket.setBody(request.getParameter("body"));
ticket.setDateCreated(Instant.now());

Part filePart = request.getPart("file1");
if(filePart != null && filePart.getSize() > 0)
{
Attachment attachment = this.processAttachment(filePart);
if(attachment != null)
ticket.addAttachment(attachment);
}

int id;
synchronized(this)
{
id = this.TICKET_ID_SEQUENCE++;
this.ticketDatabase.put(id, ticket);
}

response.sendRedirect("tickets?action=view&ticketId=" + id);
}

private Attachment processAttachment(Part filePart)
throws IOException
{
InputStream inputStream = filePart.getInputStream();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

int read;
final byte[] bytes = new byte[1024];

while((read = inputStream.read(bytes)) != -1)
{
outputStream.write(bytes, 0, read);
}

Attachment attachment = new Attachment();
attachment.setName(filePart.getSubmittedFileName());
attachment.setContents(outputStream.toByteArray());

return attachment;
}

private Ticket getTicket(String idString, HttpServletResponse response)
throws ServletException, IOException
{
if(idString == null || idString.length() == 0)
{
response.sendRedirect("tickets");
return null;
}

try
{
Ticket ticket = this.ticketDatabase.get(Integer.parseInt(idString));
if(ticket == null)
{
response.sendRedirect("tickets");
return null;
}
return ticket;
}
catch(Exception e)
{
response.sendRedirect("tickets");
return null;
}
}
}

ticketform.jsp

<template:basic htmlTitle="Create a Ticket" bodyTitle="Create a Ticket">
<form method="POST" action="tickets" enctype="multipart/form-data">
<input type="hidden" name="action" value="create"/>
Subject<br />
<input type="text" name="subject"><br /><br />
Body<br />
<textarea name="body" rows="5" cols="30"></textarea><br /><br />
<b>Attachments</b><br />
<input type="file" name="file1"/><br /><br />
<input type="submit" value="Submit"/>
</form>
</template:basic>

Explanation / Answer

public class TicketServlet extends HttpServlet {

   private static Logger logger = Logger.getLogger(TicketServlet.class.getName());
  
   private BookingService impl;

   public TicketServlet() {
       logger.info("Build Servlet:" + this.getClass());
         
       List<Event> events = new ArrayList<Event>();
       events.add(new Event(0, "Metallica", 100, 1000));
       events.add(new Event(1, "Chicago Musical", 140, 880));
       events.add(new Event(2, "ABBA Live performance", 70, 2000));
       events.add(new Event(3, "Beyoncé Show", 100, 1000));
       events.add(new Event(4, "San Francisco Symphonic recital", 100, 1000));

       Customer customer = new Customer(1, "rpelisse", "password");
       List<Customer> customers = new ArrayList<Customer>();
       customers.add(customer);
       customers.add(new Customer(2, "other", "otherPassword"));
       impl = new BookingServicesDefaultImpl(events, customers);
       logger.info("Instance ready to use");
   }

   public void doGet(HttpServletRequest request, HttpServletResponse response) {
       response.setContentType("text/plain");

       String eventLabel = request.getParameter("eventLabel");
       if ( eventLabel == null || "".equals(eventLabel) ) {
           print(response, "No event label provided");
           return ;
       }

       String username = request.getParameter("username");
       if ( username == null || "".equals(username) ) {
           print(response, "No username provided");
           return ;
       }

       print(response, impl.book(new Event(eventLabel), new Customer(username, "")).toString());
   }

   private static void print(HttpServletResponse response, String message) {
       try {
           response.getWriter().print(message);
       } catch ( IOException e) {
           throw new IllegalStateException(e);
       }

   }
}