package com.mobius.controllers;
import com.mobius.model.MyEntityManagerFactory;
import com.mobius.model.ResponseData;
import com.mobius.model.ResponseEFormTemplate;
import com.mobius.model.ResponsePatient;
import com.mobius.model.entity.TblEFromTemplateE;
import com.mobius.model.entity.TblPatientE;
import org.eclipse.persistence.jpa.jpql.parser.DateTime;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* Created with IntelliJ IDEA.
* User: Pawel
* Date: 30.12.13
* Time: 10:09
* To change this template use File | Settings | File Templates.
*/
@Controller
@RequestMapping("/patient")
@Scope("session")
public class PatientController {
@RequestMapping(method = RequestMethod.POST, produces = "application/json", value = "/patients")
public
@ResponseBody
ResponseData patients(@RequestBody List<Integer> idlist, BindingResult result, HttpServletRequest request) {
try {
EntityManager em = MyEntityManagerFactory.getEm(request.getSession());
Query query = em.createQuery("SELECT t FROM TblPatientE t where t.id IN :ids", TblPatientE.class);
query.setParameter("ids", idlist);
List<TblPatientE> list = query.getResultList();
List<ResponsePatient> patientList = new ArrayList<ResponsePatient>();
for(TblPatientE patient : list) {
patientList.add(new ResponsePatient(patient));
}
ResponseData responseData = new ResponseData();
responseData.setMax(list.size());
responseData.setSuccess(true);
responseData.setData(list);
return responseData;
} catch (Exception exc) {
exc.printStackTrace();
return ResponseData.error(exc);
}
}
@RequestMapping(method = RequestMethod.GET, produces = "application/json", value = "/search")
public
@ResponseBody
ResponseData search(@RequestParam("searchString") String searchString, HttpServletRequest request) {
List<TblPatientE> list = null;
try {
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
EntityManager em = MyEntityManagerFactory.getEm(request.getSession());
Date dobDate = null;
try {
dobDate = sdf.parse(searchString);
}catch (Exception e) {}
String queryString = "SELECT t FROM TblPatientE t WHERE t.familyName LIKE :familyName OR t.givenName LIKE :givenName " +
"OR t.hospitalNumber LIKE :hospitalNumber OR t.nhsNumber LIKE :nhsNumber OR t.middleNames LIKE :middleNames";
if(dobDate!=null) {
queryString = queryString + " OR t.dob = :dob";
}
Query query = em.createQuery(queryString, TblPatientE.class);
String likeSearch = "%"+searchString+"%";
query.setParameter("familyName",likeSearch);
query.setParameter("givenName",likeSearch);
query.setParameter("hospitalNumber",likeSearch);
query.setParameter("nhsNumber",likeSearch);
query.setParameter("middleNames",likeSearch);
if(dobDate!=null) {
query.setParameter("dob",dobDate);
}
list = query.getResultList();
} catch (Exception exc) {
ResponseData responseData = new ResponseData();
responseData.setMax(0);
responseData.setSuccess(false);
responseData.setMessage(exc.getMessage());
exc.printStackTrace();
return responseData;
}
ResponseData responseData = new ResponseData();
responseData.setMax(list.size());
responseData.setSuccess(true);
responseData.setData(list);
return responseData;
}
@RequestMapping(method = RequestMethod.GET, produces = "application/json", value = "/find")
public
@ResponseBody
ResponseData find(@RequestParam("searchString") String searchString, HttpServletRequest request) {
List<TblPatientE> list = null;
try {
EntityManager em = MyEntityManagerFactory.getEm(request.getSession());
Query query = em.createQuery("SELECT t FROM TblPatientE t WHERE t.familyName LIKE '%"
+ searchString + "%' OR t.givenName LIKE '%"
+ searchString + "%' OR t.hospitalNumber='" + searchString + "'", TblPatientE.class);
list = query.getResultList();
} catch (Exception exc) {
ResponseData responseData = new ResponseData();
responseData.setMax(0);
responseData.setSuccess(false);
responseData.setMessage(exc.getMessage());
exc.printStackTrace();
return responseData;
}
ResponseData responseData = new ResponseData();
responseData.setMax(list.size());
responseData.setSuccess(true);
List<ResponsePatient> patientList = new ArrayList<ResponsePatient>();
for(TblPatientE patientE : list) {
patientList.add(new ResponsePatient(patientE));
}
responseData.setData(patientList);
return responseData;
}
@RequestMapping(method = RequestMethod.GET, produces = "application/json", value = "/get")
public
@ResponseBody
ResponseData get(@RequestParam("id") Integer id, HttpServletRequest request) {
List<TblPatientE> list = null;
try {
EntityManager em = MyEntityManagerFactory.getEm(request.getSession());
list = new ArrayList<TblPatientE>();
list.add(em.find(TblPatientE.class, id));
} catch (Exception exc) {
ResponseData responseData = new ResponseData();
responseData.setMax(0);
responseData.setSuccess(false);
responseData.setMessage(exc.getMessage());
exc.printStackTrace();
return responseData;
}
ResponseData responseData = new ResponseData();
responseData.setMax(list.size());
responseData.setSuccess(true);
List<ResponsePatient> patientList = new ArrayList<ResponsePatient>();
for(TblPatientE patientE : list) {
patientList.add(new ResponsePatient(patientE));
}
responseData.setData(patientList);
return responseData;
}
}