package com.mobius.controllers;
import com.mobius.model.MyAuthenticationToken;
import com.mobius.model.MyEntityManagerFactory;
import com.mobius.model.ResponseData;
import com.mobius.model.ResponseEFormTemplate;
import com.mobius.model.entity.TblDocTypeE;
import com.mobius.model.entity.TblEFormTemplateHistoryE;
import com.mobius.model.entity.TblEFromTemplateE;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
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.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* Created with IntelliJ IDEA.
* User: Pawel
* Date: 07.01.14
* Time: 12:07
* To change this template use File | Settings | File Templates.
*/
@Controller
@RequestMapping("/template")
@Scope("session")
public class TemplateController {
@RequestMapping(method = RequestMethod.GET, produces = "application/json", value = "/getall")
public
@ResponseBody
ResponseData getall(HttpServletRequest request) {
ResponseData responseData = new ResponseData();
try {
EntityManager em = MyEntityManagerFactory.getEm(request.getSession());
Query query = em.createQuery("SELECT t FROM TblEFromTemplateE t", TblEFromTemplateE.class);
List<TblEFromTemplateE> list = query.getResultList();
List<ResponseEFormTemplate> results = new ArrayList<ResponseEFormTemplate>();
for (TblEFromTemplateE template : list) {
ResponseEFormTemplate template1 = new ResponseEFormTemplate(template);
results.add(template1);
}
responseData.setSuccess(true);
responseData.setMax(results.size());
responseData.setData(results);
return responseData;
} catch (Exception exc) {
exc.printStackTrace();
return ResponseData.error(exc);
}
}
@RequestMapping(method = RequestMethod.GET, produces = "application/json", value = "/searchbyname")
public
@ResponseBody
ResponseData searchByName(@RequestParam("name") String name, HttpServletRequest request) {
ResponseData responseData = new ResponseData();
try {
EntityManager em = MyEntityManagerFactory.getEm(request.getSession());
Query query = em.createQuery("SELECT t.name,t.displayName,t.description FROM TblEFromTemplateE t where t.name LIKE :name OR t.displayName LIKE :displayName" +
" OR t.description LIKE :description");
query.setParameter("name","%"+ name + "%");
query.setParameter("displayName","%"+ name + "%");
query.setParameter("description","%"+ name + "%");
List<Object[]> list = query.getResultList();
List<ResponseEFormTemplate> results = new ArrayList<ResponseEFormTemplate>();
for (Object[] template : list) {
ResponseEFormTemplate template1 = new ResponseEFormTemplate();
template1.setName((String)template[0]);
template1.setDisplayName((String)template[1]);
template1.setDescription((String)template[2]);
results.add(template1);
}
responseData.setSuccess(true);
responseData.setMax(results.size());
responseData.setData(results);
return responseData;
} catch (Exception exc) {
exc.printStackTrace();
return ResponseData.error(exc);
}
}
@RequestMapping(method = RequestMethod.GET, produces = "application/json", value = "/getbyname")
public
@ResponseBody
ResponseData getByName(@RequestParam("name") String name, HttpServletRequest request) {
ResponseData responseData = new ResponseData();
try {
EntityManager em = MyEntityManagerFactory.getEm(request.getSession());
Query query = em.createQuery("SELECT t FROM TblEFromTemplateE t where t.name=:name", TblEFromTemplateE.class);
query.setParameter("name", name);
List<TblEFromTemplateE> list = query.getResultList();
List<ResponseEFormTemplate> results = new ArrayList<ResponseEFormTemplate>();
for (TblEFromTemplateE template : list) {
ResponseEFormTemplate template1 = new ResponseEFormTemplate(template);
results.add(template1);
}
responseData.setSuccess(true);
responseData.setMax(results.size());
responseData.setData(results);
return responseData;
} catch (Exception exc) {
exc.printStackTrace();
return ResponseData.error(exc);
}
}
@RequestMapping(method = RequestMethod.GET, produces = "application/json", value = "/doctypes")
public
@ResponseBody
ResponseData doctypes(HttpServletRequest request) {
try {
EntityManager em = MyEntityManagerFactory.getEm(request.getSession());
Query query = em.createQuery("SELECT t FROM TblDocTypeE t where t.method='E'", TblDocTypeE.class);
List<TblDocTypeE> list = query.getResultList();
ResponseData responseData = new ResponseData();
responseData.setSuccess(Boolean.TRUE);
responseData.setMax(list.size());
responseData.setData(list);
return responseData;
} catch (Exception exc) {
exc.printStackTrace();
return ResponseData.error(exc);
}
}
@RequestMapping(method = RequestMethod.GET, produces = "application/json", value = "/doctypessearch")
public
@ResponseBody
ResponseData doctypesSearch(@RequestParam("search") String search,HttpServletRequest request) {
try {
EntityManager em = MyEntityManagerFactory.getEm(request.getSession());
Query query = em.createQuery("SELECT t FROM TblDocTypeE t where t.name LIKE :name", TblDocTypeE.class);
query.setParameter("name","%"+search+"%");
List<TblDocTypeE> list = query.getResultList();
ResponseData responseData = new ResponseData();
responseData.setSuccess(Boolean.TRUE);
responseData.setMax(list.size());
responseData.setData(list);
return responseData;
} catch (Exception exc) {
exc.printStackTrace();
return ResponseData.error(exc);
}
}
@RequestMapping(method = RequestMethod.POST, produces = "application/json", value = "/update")
public
@ResponseBody
ResponseData update(@RequestBody ResponseEFormTemplate rtemplates, BindingResult result, HttpServletRequest request) {
try {
ResponseEFormTemplate rtemplate = rtemplates;//.get(0);
EntityManager em = MyEntityManagerFactory.getEm(request.getSession());
em.getTransaction().begin();
Query query = em.createQuery("SELECT t FROM TblEFromTemplateE t where t.name=:name", TblEFromTemplateE.class);
query.setParameter("name", rtemplate.getName());
List<TblEFromTemplateE> list = query.getResultList();
TblEFromTemplateE template = null;
if (list.size() > 0) {
template = list.get(0);
TblEFormTemplateHistoryE history = new TblEFormTemplateHistoryE();
history.setName(template.getName());
history.setOryId(template.getId());
history.setModifiedBy(template.getModifiedBy());
history.setModifiedOn(template.getModifiedOn());
history.setTemplateData(template.getTemplateData());
history.setDisplayName(template.getDisplayName());
history.setDescription(template.getDescription());
history.setVersion(template.getVersion());
em.persist(history);
template.setVersion(rtemplate.getVersion());
template.setTemplateData(rtemplate.getJson());
template.setModifiedOn(new Timestamp(new Date().getTime()));
template.setDisplayName(rtemplate.getDisplayName());
template.setDescription(rtemplate.getDescription());
SecurityContext context = SecurityContextHolder.getContext();
Authentication authentication = context.getAuthentication();
String username = "";
if (authentication instanceof MyAuthenticationToken) {
MyAuthenticationToken authenticationToken = (MyAuthenticationToken) authentication;
username = authenticationToken.getMyUser().getDomainName();
}
template.setModifiedBy(username);
em.persist(template);
em.getTransaction().commit();
} else {
template = new TblEFromTemplateE();
template.setName(rtemplate.getName());
template.setVersion(rtemplate.getVersion());
template.setTemplateData(rtemplate.getJson());
template.setModifiedOn(new Timestamp(new Date().getTime()));
template.setDisplayName(rtemplate.getDisplayName());
template.setDescription(rtemplate.getDescription());
SecurityContext context = SecurityContextHolder.getContext();
Authentication authentication = context.getAuthentication();
String username = "";
if (authentication instanceof MyAuthenticationToken) {
MyAuthenticationToken authenticationToken = (MyAuthenticationToken) authentication;
username = authenticationToken.getMyUser().getDomainName();
}
template.setModifiedBy(username);
em.persist(template);
em.getTransaction().commit();
em.refresh(template);
}
em.close();
ResponseData responseData = new ResponseData();
List<ResponseEFormTemplate> results = new ArrayList<ResponseEFormTemplate>();
ResponseEFormTemplate template1 = new ResponseEFormTemplate(template);
results.add(template1);
responseData.setSuccess(true);
responseData.setMax(1);
responseData.setData(results);
return responseData;
} catch (Exception exc) {
exc.printStackTrace();
return ResponseData.error(exc);
}
}
}