Project

General

Profile

Statistics
| Revision:

root / trunk / src / java / org / lidar / LogServlet.java @ 10

History | View | Annotate | Download (3.82 KB)

1
/*
2
 * To change this template, choose Tools | Templates
3
 * and open the template in the editor.
4
 */
5

    
6
package org.lidar;
7

    
8
import com.google.gson.Gson;
9
import java.io.IOException;
10
import java.io.PrintWriter;
11
import java.util.ArrayList;
12
import javax.servlet.ServletException;
13
import javax.servlet.http.HttpServlet;
14
import javax.servlet.http.HttpServletRequest;
15
import javax.servlet.http.HttpServletResponse;
16
import org.hibernate.Query;
17
import org.hibernate.Session;
18
import org.hibernate.Transaction;
19
import org.lidar.HibernateUtil;
20

    
21
/**
22
 *
23
 * @author Andrej Cimpersek
24
 */
25
public class LogServlet extends HttpServlet {
26
   
27
    /** 
28
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
29
     * @param request servlet request
30
     * @param response servlet response
31
     * @throws ServletException if a servlet-specific error occurs
32
     * @throws IOException if an I/O error occurs
33
     */
34
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
35
    throws ServletException, IOException {
36
        response.setContentType("application/json;charset=UTF-8");
37
        PrintWriter out = response.getWriter();
38
        Gson gson = new Gson();
39
        try {
40
            int start = 0;
41
            int limit = 15;
42
            if(request.getParameter("limit") != null){
43
                limit = Integer.parseInt(request.getParameter("limit"));
44
            }
45
            if(request.getParameter("start") != null){
46
                start = Integer.parseInt(request.getParameter("start"));
47
            }
48

    
49
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
50
            Transaction tx = session.beginTransaction();
51

    
52
            // FIX: use COUNT instead
53
            int total = session.createQuery("from Log as log").list().size();
54
            Query query = session.createQuery("from Log as log order by log.date desc");
55
            query.setFirstResult(start);
56
            query.setMaxResults(limit);
57
            JSONResponse resp = new JSONResponse(true, total, (ArrayList)query.list());
58
            out.write(gson.toJson(resp));
59
            tx.commit();
60
        } catch (Exception e) {
61
            response.setContentType("text/plain;charset=UTF-8");
62
            out.write(e.getMessage());
63
        } finally {
64
            out.close();
65
        }
66
    }
67

    
68
    class JSONResponse {
69
        private boolean success = true;
70
        private int total;
71
        private ArrayList rows;
72

    
73
        public JSONResponse(boolean success, int total, ArrayList rows){
74
            this.success = success;
75
            this.total = total;
76
            this.rows = rows;
77
        }
78
    }
79

    
80
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
81
    /** 
82
     * Handles the HTTP <code>GET</code> method.
83
     * @param request servlet request
84
     * @param response servlet response
85
     * @throws ServletException if a servlet-specific error occurs
86
     * @throws IOException if an I/O error occurs
87
     */
88
    @Override
89
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
90
    throws ServletException, IOException {
91
        processRequest(request, response);
92
    } 
93

    
94
    /** 
95
     * Handles the HTTP <code>POST</code> method.
96
     * @param request servlet request
97
     * @param response servlet response
98
     * @throws ServletException if a servlet-specific error occurs
99
     * @throws IOException if an I/O error occurs
100
     */
101
    @Override
102
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
103
    throws ServletException, IOException {
104
        processRequest(request, response);
105
    }
106

    
107
    /** 
108
     * Returns a short description of the servlet.
109
     * @return a String containing servlet description
110
     */
111
    @Override
112
    public String getServletInfo() {
113
        return "Short description";
114
    }// </editor-fold>
115

    
116
}