@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;