Created
May 22, 2012 14:06
-
-
Save euch/2769304 to your computer and use it in GitHub Desktop.
TableGenerator (ejb → html)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * To change this template, choose Tools | Templates | |
| * and open the template in the editor. | |
| */ | |
| package com.mycompany.taskmanager.servlet; | |
| import com.mycompany.taskmanager.ejb.RelationFacadeLocal; | |
| import com.mycompany.taskmanager.ejb.Task; | |
| import com.mycompany.taskmanager.ejb.TaskFacadeLocal; | |
| import com.mycompany.taskmanager.ejb.Time; | |
| import com.mycompany.taskmanager.ejb.TimeFacadeLocal; | |
| import java.io.IOException; | |
| import java.io.PrintWriter; | |
| import java.io.Serializable; | |
| import java.io.StringWriter; | |
| import java.text.DateFormat; | |
| import java.text.SimpleDateFormat; | |
| import java.util.List; | |
| import java.util.TimeZone; | |
| import java.util.Vector; | |
| import javax.ejb.EJB; | |
| import javax.servlet.ServletException; | |
| import javax.servlet.http.HttpServlet; | |
| import javax.servlet.http.HttpServletRequest; | |
| import javax.servlet.http.HttpServletResponse; | |
| /** | |
| * | |
| * @author eugene | |
| */ | |
| public class EjbTasksTableGenerator extends HttpServlet { | |
| private static final long serialVersionUID = 1L; | |
| @EJB | |
| private TimeFacadeLocal timeFacade; | |
| @EJB | |
| private RelationFacadeLocal relationFacade; | |
| @EJB | |
| private TaskFacadeLocal taskFacade; | |
| /** | |
| * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods. | |
| * @param request servlet request | |
| * @param response servlet response | |
| * @throws ServletException if a servlet-specific error occurs | |
| * @throws IOException if an I/O error occurs | |
| */ | |
| protected void processRequest(HttpServletRequest request, HttpServletResponse response) | |
| throws ServletException, IOException { | |
| response.setContentType("text/html;charset=UTF-8"); | |
| PrintWriter out = response.getWriter(); | |
| try { | |
| response.setContentType("text/html"); | |
| Vector<Serializable> av = getVector(); | |
| printTableHeader(out); | |
| out.println(getTableContents(av, 6)); // 6 is a columns count | |
| printTableFooter(out); | |
| } finally { | |
| //out.close(); // It caused an error | |
| } | |
| } | |
| private Vector<Serializable> getVector() { | |
| Vector<Serializable> av = new Vector<Serializable>(); | |
| List<Task> tasks = taskFacade.findAll(); | |
| int count = taskFacade.count(); | |
| for (int i = 0; i < count; i++) { | |
| Task task = tasks.get(i); | |
| String id = task.getId(); | |
| int relationsWithChilds = relationFacade.findByParentId(id).size(); | |
| int relationsWithParents = relationFacade.findByChildId(id).size(); | |
| Time time = timeFacade.find(id); | |
| // 1 column | |
| av.addElement("<a href=showTask.jsp?id=" + id + " target=\"_blank\">" | |
| + task.getName() + "</a>"); | |
| // 2 column | |
| av.addElement(task.getDescription()); | |
| // 3 column | |
| av.addElement(task.getContacts()); | |
| // 4 column | |
| av.addElement("<a href=showTask.jsp?id=" + id + " target=\"_blank\">" | |
| + relationsWithParents + "/" + relationsWithChilds + "</a>"); | |
| DateFormat df = new SimpleDateFormat("dd.MM.yy HH:mm"); | |
| //Warning! Hard time zone selection because of wrong tzdata | |
| df.setTimeZone(TimeZone.getTimeZone("GMT+04:00")); | |
| // 5 column | |
| if (time == null) { | |
| av.addElement("-"); | |
| } else { | |
| av.addElement(df.format(time.getTimefrom())); | |
| } | |
| // 6 column | |
| if (time == null) { | |
| av.addElement("-"); | |
| } else { | |
| av.addElement(df.format(time.getTimeuntil())); | |
| } | |
| } | |
| return av; | |
| } | |
| private void printTableHeader(PrintWriter out) { | |
| out.println("\n<table " | |
| + "id=\"t1\" " | |
| + "class=\"example table-autosort table-autofilter table-stripeclass:alternate table-page-number:t1page table-autopage:50 table-page-count:t1pages table-filtered-rowcount:t1filtercount table-rowcount:t1allcount\">"); | |
| out.println("<thead><tr style=\"height:35px\">" | |
| + " <th class=\"table-filterable table-sortable:default\">Short Name</th>\n" | |
| + " <th class=\"table-filterable table-sortable:default\">Description</th>\n" | |
| + " <th class=\"table-filterable table-sortable:default\">Contacts</th>\n" | |
| + " <th class=\"table-sortable:default\">P/C</th>\n" | |
| + " <th class=\"table-sortable:date\">Start time</th>\n" | |
| + " <th class=\"table-sortable:date\">End time</th>\n" | |
| + " </tr>" + "</thead>"); | |
| out.println("<tbody>"); | |
| } | |
| private void printTableFooter(PrintWriter out) { | |
| out.println("\n</tbody>"); | |
| out.println("<tfoot>" | |
| + "<tr>" | |
| + " <td class=\"table-page:previous\" style=\"cursor:pointer;\">< < Previous</td>" | |
| + " <td colspan=\"3\" style=\"text-align:center;\">Page <span id=\"t1page\"></span> of <span id=\"t1pages\"></span></td>" | |
| + " <td class=\"table-page:next\" style=\"cursor:pointer;\">Next > ></td>" | |
| + "</tr>" | |
| + "<tr>" | |
| + " <td colspan=\"6\"><span id=\"t1filtercount\"></span> of <span id=\"t1allcount\"></span> rows match filter(s)</td>" | |
| + "</tfoot>"); | |
| out.println("\n</table>"); | |
| } | |
| private String getTableContents(Vector<Serializable> values, | |
| int elementCounter) throws IOException { | |
| StringWriter Cells = new StringWriter(); | |
| String contents; | |
| int vsize = values.size(); | |
| Cells.write("\n<tr>\n"); | |
| for (int i = 0; i < vsize; i++) { | |
| String value = values.elementAt(i).toString(); | |
| if (i != 0) { | |
| if (i >= elementCounter) { | |
| if (i % elementCounter == 0) { | |
| Cells.write("</td>\n\n<tr>\n"); | |
| } | |
| } | |
| } | |
| Cells.write(" <td>" + value + "</td> \n"); | |
| } | |
| Cells.write("</tr>"); | |
| contents = Cells.toString(); | |
| Cells.flush(); | |
| Cells.close(); | |
| return contents; | |
| } | |
| // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> | |
| /** | |
| * Handles the HTTP <code>GET</code> method. | |
| * @param request servlet request | |
| * @param response servlet response | |
| * @throws ServletException if a servlet-specific error occurs | |
| * @throws IOException if an I/O error occurs | |
| */ | |
| @Override | |
| protected void doGet(HttpServletRequest request, HttpServletResponse response) | |
| throws ServletException, IOException { | |
| processRequest(request, response); | |
| } | |
| /** | |
| * Handles the HTTP <code>POST</code> method. | |
| * @param request servlet request | |
| * @param response servlet response | |
| * @throws ServletException if a servlet-specific error occurs | |
| * @throws IOException if an I/O error occurs | |
| */ | |
| @Override | |
| protected void doPost(HttpServletRequest request, HttpServletResponse response) | |
| throws ServletException, IOException { | |
| processRequest(request, response); | |
| } | |
| /** | |
| * Returns a short description of the servlet. | |
| * @return a String containing servlet description | |
| */ | |
| @Override | |
| public String getServletInfo() { | |
| return "Short description"; | |
| }// </editor-fold> | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment