Ejemplo n.º 1
0
  /**
   * 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.setCharacterEncoding("UTF-8");
    response.setHeader("Expires", "-1");
    PrintWriter out = response.getWriter();
    try {
      Long langId = Long.parseLong(request.getParameter("langId"));
      Long dataId = Long.parseLong(request.getParameter("dataId"));
      long type = dataId; // type of value (like 4096)
      if ((type & 0xC000) != 0xC000) {
        dataId = (type & 0xFFF); // remove type to get an index 4096&0xFFF -> 0
      } else {
        dataId = (type & 0xFFFF);
      }

      DataDao dataDao = DbImplDecider.use(DaoType.MYSQL).getDao(DataDao.class);
      Data translate = dataDao.getById(dataId, langId);
      out.print("<html>");
      out.print("<message>");
      if (translate.getId() != null) {
        out.print(translate.getUnicodeLabel());
      } else {
        out.print("Translation does not exist");
      }
      out.println("</message>");
      out.print("</html>");
    } catch (Exception ex) {
      out.print("<message>");
      out.print(
          "Error occur during execution the query. \n"
              + "Perhaps translation for this data type does not exist .");
      out.println("</message>");
    }
  }
  /**
   * 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");

    OutputStream out = response.getOutputStream();

    try {
      if (!CheckUserInSession.isUserInSession(request)) {
        logger.error("Unauthorized access!");
        response.sendRedirect("./login.jsp");
      } else {
        long flockId = Long.parseLong(request.getParameter("flockId"));
        FromDayToDay growDayRangeParam =
            new FromDayToDay(request.getParameter("fromDay"), request.getParameter("toDay"));
        try {
          FlockHistoryService flockHistoryService = new FlockHistoryServiceImpl();
          Map<Integer, String> historyByGrowDay =
              flockHistoryService.getFlockPerDayNotParsedReportsWithinRange(
                  flockId, growDayRangeParam);

          List<Map<Integer, Data>> dataHistoryList = new ArrayList<Map<Integer, Data>>();
          List<String> axisTitles = new ArrayList<String>();
          Locale currLocale = (Locale) request.getSession().getAttribute("currLocale");
          String lang = currLocale.toString().substring(0, 2);
          LanguageDao languageDao = DbImplDecider.use(DaoType.MYSQL).getDao(LanguageDao.class);
          long langId = languageDao.getLanguageId(lang);

          DataDao dataDao = DbImplDecider.use(DaoType.MYSQL).getDao(DataDao.class);
          Data data1 = dataDao.getById(PerGrowDayHistoryDataType.DAY_MORTALITY.getId());
          axisTitles.add(data1.getLabel());
          dataHistoryList.add(DataGraphCreator.createHistoryDataByGrowDay(historyByGrowDay, data1));

          Data data2 = dataDao.getById(PerGrowDayHistoryDataType.DAY_MORTALITY_MALE.getId());
          axisTitles.add(data2.getLabel());
          dataHistoryList.add(DataGraphCreator.createHistoryDataByGrowDay(historyByGrowDay, data2));

          Data data3 = dataDao.getById(PerGrowDayHistoryDataType.DAY_MORTALITY_FEMALE.getId());
          axisTitles.add(data3.getLabel());
          dataHistoryList.add(DataGraphCreator.createHistoryDataByGrowDay(historyByGrowDay, data3));

          HashMap<String, String> dictinary = createDictionary(currLocale);
          String title = dictinary.get("graph.mrt.title"); // "Daily Mortality";
          String xAxisTitle = dictinary.get("graph.mrt.axis.growday"); // "Grow Day[Day]";
          String yAxisTitle = dictinary.get("graph.mrt.axis.birds"); // "Birds";
          HistoryGraph mortalityGraph = new HistoryGraph();

          mortalityGraph.setDataHistoryList(dataHistoryList);
          mortalityGraph.createChart(title, xAxisTitle, yAxisTitle);
          request.setAttribute("fromDay", growDayRangeParam.getFromDay());
          request.setAttribute("toDay", growDayRangeParam.getToDay());
          ChartUtilities.writeChartAsPNG(out, mortalityGraph.getChart(), 800, 600);
          out.flush();
          out.close();
        } catch (Exception ex) {
          ex.printStackTrace();
          logger.error("Unknown error.", ex);
          EmptyGraph graph = new EmptyGraph();
          ChartUtilities.writeChartAsPNG(out, graph.getChart(), 600, 300);
          out.flush();
          out.close();
        }
      }
    } finally {
      out.close();
    }
  }