<file_sep>package com.dh.spt.db;

import java.util.Date;
import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.dh.spt.db.model.ComplaintTypes;
import com.dh.spt.db.model.Complaints;

/**
 * Utilities class to interact with Complaints table.
 * 
 * @author pavan
 *
 */
public class ComplaintsUtil {

	/**
	 * Inserts a new record into Complaint table and returns the auto generated
	 * complaint id
	 * 
	 * @param complaintType
	 * @param incidentLocation
	 * @param incidentDate
	 * @param incdientSummary
	 * @param reporterName
	 * @param reporterContactNo
	 * @param reporterEmailAddress
	 * @param reportedLocation
	 * @return
	 */
	public String createNewComplaint(String complaintType,
			String incidentLocation, Date incidentDate, String incdientSummary,
			String reporterName, String reporterContactNo,
			String reporterEmailAddress, String reportedLocation) {
		// Create a new session with Hibernate
		Session session = HibernateUtil.getSession();
		// Initiate a transaction
		Transaction transaction = session.beginTransaction();

		// Create a new complaint object to store
		Complaints newComplaint = new Complaints();
		newComplaint.setComplaintTypes((ComplaintTypes) session.get(
				ComplaintTypes.class, complaintType));

		newComplaint.setIncidentLocataion(incidentLocation);
		newComplaint.setIncidentTime(incidentDate);
		newComplaint.setReporterContactNo(reporterContactNo);
		newComplaint.setReporterEmailAddress(reporterEmailAddress);
		newComplaint.setReporterName(reporterName);
		newComplaint.setSummary(incdientSummary);
		newComplaint.setReportedLocation(reportedLocation);

		// Save the complaint object
		session.save(newComplaint);

		// Commit and close the session
		transaction.commit();
		session.flush();
		session.close();

		// Return the auto generated complaint id to the server
		return newComplaint.getIncidentId();
	}

	/**
	 * Returns the complaint object by querying based on the incident id
	 * provided. Creates a Hibernate session internally
	 * 
	 * @param incidentid
	 * @return
	 */
	public Complaints getComplaintByComplaintId(String incidentid) {

		// Initiate a Hibernate session.
		Session session = HibernateUtil.getSession();

		// Read complaint object
		Complaints complaint = getComplaintByComplaintId(incidentid, session);

		// Read the complaint images for the complaint. This is needed as in
		// hibernate configuration, lazy loading is enabled for this class
		Hibernate.initialize(complaint.getComplaintImageses());
		// close the session
		session.flush();
		session.close();

		// Return the complaint object
		return complaint;
	}

	/**
	 * 
	 * @param incidentId
	 * @param session
	 * @return
	 */
	public Complaints getComplaintByComplaintId(String incidentId,
			Session session) {
		// Read the query to be fired from named queries
		Query query = session.getNamedQuery("HQL_GET_COMPLAINT_BY_INCIDENT");

		// Add the parameter
		query.setString("IncidentId", incidentId);

		// Read the query
		return (Complaints) query.uniqueResult();

	}

	/**
	 * Get latest complaints logged.
	 * 
	 * @return List of complaints
	 */
	@SuppressWarnings("unchecked")
	public List<Complaints> getLatestComplaints() {

		// Initiate a Hibernate session.
		Session session = HibernateUtil.getSession();

		// Read the query to be fired from named queries
		Query query = session.getNamedQuery("HQL_GET_LATEST_COMPLAINTS");

		// Fire the query to read the result
		List<Complaints> complaints = query.list();

		// close session.
		session.flush();
		session.close();

		// Return the complaints read from database.
		return complaints;
	}
}
<file_sep>package com.dh.spt.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dh.spt.db.ComplaintsUtil;

/**
 * Servlet used to register a new complaint. Response from the servlet is a json
 * object consisting the incident id generated while registering.
 * 
 * @author pavan
 *
 */
@WebServlet("/register-complaint")
public class RegisterComplaintServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public RegisterComplaintServlet() {
		super();
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		// Reading all the input parameters.
		String complaintType = request.getParameter("complaintType");
		String incidentLocation = request.getParameter("incidentLocation");
		String incidentDateStr = request.getParameter("incidentDate");
		String reportedBy = request.getParameter("reportedBy");
		String contactNo = request.getParameter("contactNo");
		String emailAddress = request.getParameter("emailAddress");
		String incidentSummary = request.getParameter("summary");
		String reportedLocation = request.getParameter("reportedLocation");

		// Date formatter for formatting the date passed.
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm");

		// Validations on the date. If the date is passed as a future date,
		// exception will be thrown back.
		Date incidentDate;
		try {
			incidentDate = formatter.parse(incidentDateStr);
		} catch (ParseException e) {
			throw new ServletException("Invalid Date Format.");
		}

		if (incidentDate.after(new Date()))
			throw new ServletException(
					"Incident Date cannot be greater than today.");

		// Creating a new complaint
		ComplaintsUtil complaintsUtil = new ComplaintsUtil();
		String incidentId = complaintsUtil.createNewComplaint(complaintType,
				incidentLocation, incidentDate, incidentSummary, reportedBy,
				contactNo, emailAddress, reportedLocation);

		// Constructing the response json in string format.
		String respStr = "{\"incidentid\":\"" + incidentId + "\"}";

		// Setting the content type to JSON. If this is not set, the response
		// will be treated as string instead of a json object.
		response.setContentType("application/json");

		//Writing the response back.
		response.getOutputStream().write(respStr.getBytes());
	}

}
<file_sep>package com.dh.spt.db;

import java.util.List;

import org.hibernate.Session;

import com.dh.spt.db.model.ComplaintTypes;

/**
 * DAO Class to handle all executions related to Complaints table.
 * 
 * @author pavan
 *
 */
public class ComplaintTypesUtil {

	/**
	 * Returns all complaint types in the table
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<ComplaintTypes> getAllComplaintTypes() {
		// Create a session
		Session session = HibernateUtil.getSession();

		// Read all complaint types available
		List<ComplaintTypes> allComplaintTypes = session.createCriteria(
				ComplaintTypes.class).list();

		// Close session
		session.flush();
		session.close();

		// Return complaint types
		return allComplaintTypes;
	}
}
<file_sep>package com.dh.spt.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dh.spt.db.ComplaintImagesUtil;

/**
 * Servlet class to return image for a complaint based on the image id. The
 * image is read from database and is written to output stream.
 */
@WebServlet("/complaint-image")
public class ComplaintImageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public ComplaintImageServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// Read the request parameter
		String imageId = request.getParameter("id");
		ComplaintImagesUtil ciUtil = new ComplaintImagesUtil();

		// Read the image data from database
		byte[] imageData = ciUtil.getImageData(Integer.parseInt(imageId));

		// If image data is found, write the same. Else, blank page is returned.
		if (imageData != null) {
			// Setting the content type to image.
			response.setContentType("image/gif");
			response.getOutputStream().write(imageData);
		}

	}

}
<file_sep>package com.dh.spt.db.model;

// Generated 22 Nov, 2014 6:09:24 PM by Hibernate Tools 3.4.0.CR1

import java.util.HashSet;
import java.util.Set;

/**
 * ComplaintTypes generated by hbm2java
 */
public class ComplaintTypes implements java.io.Serializable {

	private String complaintTypeCode;
	private String complaintTypeName;
	private Set<Complaints> complaintses = new HashSet<Complaints>(0);

	public ComplaintTypes() {
	}

	public ComplaintTypes(String complaintTypeCode) {
		this.complaintTypeCode = complaintTypeCode;
	}

	public ComplaintTypes(String complaintTypeCode, String complaintTypeName,
			Set<Complaints> complaintses) {
		this.complaintTypeCode = complaintTypeCode;
		this.complaintTypeName = complaintTypeName;
		this.complaintses = complaintses;
	}

	public String getComplaintTypeCode() {
		return this.complaintTypeCode;
	}

	public void setComplaintTypeCode(String complaintTypeCode) {
		this.complaintTypeCode = complaintTypeCode;
	}

	public String getComplaintTypeName() {
		return this.complaintTypeName;
	}

	public void setComplaintTypeName(String complaintTypeName) {
		this.complaintTypeName = complaintTypeName;
	}

	public Set<Complaints> getComplaintses() {
		return this.complaintses;
	}

	public void setComplaintses(Set<Complaints> complaintses) {
		this.complaintses = complaintses;
	}

}
