com.kalai.controller.AjaxCallController.java Source code

Java tutorial

Introduction

Here is the source code for com.kalai.controller.AjaxCallController.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kalai.controller;

import com.kalai.Employee;
import com.kalai.HibernateUtil;
import com.kalai.loginusers;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author kalaiselvan.a
 */
@Controller
public class AjaxCallController {

    private Employee emp;
    private HibernateUtil h;
    private Session session;

    @RequestMapping("/ajaxcall")
    public String ajax(HttpSession logsession) {
        String username = "";
        username = (String) logsession.getAttribute("username");
        if (username != null && username.trim().length() != 0) {
            return "ajaxcall";
        } else {
            return "index";
        }
    }

    @RequestMapping(value = "/ajaxcalldetails")
    public @ResponseBody String ajaxmethod(@ModelAttribute("ajaxcalldetails") loginusers users,
            HttpSession logsession) {
        String retrunstring = "", username = "";
        String user = users.getEmail();
        String pass = users.getPassword();
        try {
            username = (String) logsession.getAttribute("username");
            if (username != null && username.trim().length() != 0) {
                emp = new Employee(users.getEmail());

                session = HibernateUtil.getSessionFactory().getCurrentSession();
                session.getTransaction().begin();

                String hql = "from Employee where role='" + users.getEmail() + "' ORDER BY name DESC";
                Query qr = session.createQuery(hql);
                List list = qr.list();
                for (Object list1 : list) {
                    Employee em;
                    em = (Employee) list1;
                    retrunstring += em.getName() + " - " + em.getRole() + "~($#$)~";
                    //    System.out.println(em.getName() + " - " + em.getRole() + "~($#$)~");
                }
                System.out.println(retrunstring);
                session.getTransaction().commit();
            } else {
                return "index";
            }
        } catch (Exception e) {
            return e.getMessage();
        } finally {
            if (session.isOpen()) {
                session.close();
            }
        }
        return retrunstring;
    }

    //    public static void main(String[] args) {
    //        AjaxCallController acc = new AjaxCallController();
    //        loginusers user;
    //        user = new loginusers("J-Executive", "kalai");
    //        acc.ajaxmethod(user);
    //    }
}