Java tutorial
/* * Copyright (C) 2010 {Apertum}Projects. web: www.apertum.ru email: info@apertum.ru * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package ru.apertum.qsystem.reports.formirovators; import java.io.UnsupportedEncodingException; import java.sql.Connection; import java.sql.Statement; import java.sql.ResultSet; import java.sql.DriverManager; import java.sql.SQLException; import java.text.ParseException; import java.util.Date; import java.util.HashMap; import java.util.Map; import org.apache.http.HttpRequest; import ru.apertum.qsystem.common.Uses; import ru.apertum.qsystem.common.QLog; import ru.apertum.qsystem.common.exceptions.ReportException; import ru.apertum.qsystem.reports.common.Response; /** * * @author Igor Savin */ public class DistributionJobDayUsers extends AFormirovator { /** * ? ? . . ?? . ? , ? * ? . * * @param driverClassName * @param url * @param username * @param password * @param request * @return */ @Override public Map getParameters(String driverClassName, String url, String username, String password, HttpRequest request) { return paramMap; } /** * ? */ final private HashMap<String, Object> paramMap = new HashMap<>(); /** * ? ? ??? . ? ??? , , null. * * @param driverClassName * @param url * @param username * @param password * @param request * @return ?? null. */ @Override public Connection getConnection(String driverClassName, String url, String username, String password, HttpRequest request) { final Connection connection; try { Class.forName(driverClassName); connection = DriverManager.getConnection(url, username, password); } catch (SQLException | ClassNotFoundException ex) { throw new ReportException(StatisticServices.class.getName() + " " + ex); } return connection; } /* @Override public byte[] preparation(String driverClassName, String url, String username, String password, HttpRequest request) { // ? ? ??? , // null. final String data = NetUtil.getEntityContent(request); final String tmp = NetUtil.getUrl(request); QLog.l().logger().trace("? \"" + data + "\"."); QLog.l().logger().trace("subject \"" + tmp + "\"."); // ? boolean flag = false; String mess = ""; if ("".equals(data)) { flag = true; } else { // ? final String[] ss = data.split("&"); if (ss.length == 3) { final String[] ss0 = ss[0].split("="); final String[] ss1 = ss[1].split("="); final String[] ss2 = ss[2].split("="); Date date = null; String sdate = null; int user_id = -1; String user = null; try { date = Uses.format_dd_MM_yyyy.parse(ss0[1]); sdate = (new java.text.SimpleDateFormat("yyyy-MM-dd")).format(date); user_id = Integer.parseInt(ss1[1]); user = ss2[1]; } catch (Exception ex) { mess = "<br> ! ? ? (..)."; flag = true; } if (!flag) { paramMap.put("sdate", sdate); paramMap.put(ss0[0], date); paramMap.put(ss1[0], new Integer(user_id)); paramMap.put(ss2[0], user); } } else { mess = "<br> !"; flag = true; } } if (flag) { // ? ?? // . // ? ? ?? ? ? null, // getDataSource ? . // ? ? ? . ? , ? . final InputStream inStream = getClass().getResourceAsStream("/ru/apertum/qsystem/reports/web/get_date_distribution_users.html"); String result = null; String users_select = ""; try { result = new String(Uses.readInputStream(inStream), "UTF-8"); } catch (IOException ex) { throw new Uses.ReportException(" ? ?? ? ??. " + ex); } try { Connection conn = getConnection(driverClassName, url, username, password, request); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT id, name FROM users ORDER BY name"); int id; String usrname; users_select = ""; while (rs.next()) { id = rs.getInt(1); usrname = rs.getString(2); users_select += "<option value=" + id + ">" + usrname + "\n"; } } catch (SQLException ex) { throw new Uses.ReportException(" ? ? ? ?. " + ex); } result = result.replaceFirst(Uses.ANCHOR_DATA_FOR_REPORT, request.getRequestLine().getUri()).replaceFirst(Uses.ANCHOR_ERROR_INPUT_DATA, mess).replaceFirst("#DATA_FOR_TITLE#", "? ? ? ?:").replaceFirst("#DATA_FOR_USERS#", users_select); try { return result.getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { return result.getBytes(); } } else { return null; } } */ @Override public Response getDialog(String driverClassName, String url, String username, String password, HttpRequest request, String errorMessage) { Response result = getDialog("/ru/apertum/qsystem/reports/web/get_date_distribution_users.html", request, errorMessage); final StringBuilder users_select = new StringBuilder(); try (final Connection conn = getConnection(driverClassName, url, username, password, request); final Statement stmt = conn.createStatement(); final ResultSet rs = stmt.executeQuery("SELECT id, name FROM users ORDER BY name")) { while (rs.next()) { users_select.append("<option value=").append(rs.getLong(1)).append(">").append(rs.getString(2)) .append("\n"); } } catch (SQLException ex) { users_select.setLength(0); throw new ReportException( " ? ? ? ?. " + ex); } try { result.setData(new String(result.getData(), "UTF-8").replaceFirst("#DATA_FOR_TITLE#", "? ? ? ?:") .replaceFirst("#DATA_FOR_USERS#", users_select.toString()).getBytes("UTF-8")); } catch (UnsupportedEncodingException ex) { } users_select.setLength(0); return result; } @Override public String validate(String driverClassName, String url, String username, String password, HttpRequest request, HashMap<String, String> params) { // ? QLog.l().logger().trace("? \"" + params.toString() + "\"."); if (params.size() == 3) { // date/user_id/user Date date; String sdate, user; long user_id; try { date = Uses.format_dd_MM_yyyy.parse(params.get("date")); sdate = (new java.text.SimpleDateFormat("yyyy-MM-dd")).format(date); user_id = Long.parseLong(params.get("user_id")); user = params.get("user"); } catch (ParseException | NumberFormatException ex) { return "<br> ! ? ? (..)."; } paramMap.put("sdate", sdate); paramMap.put("date", date); paramMap.put("user_id", user_id); paramMap.put("user", user); } else { return "<br> !"; } return null; } }