ru.apertum.qsystem.reports.formirovators.DistributionMedDayServices.java Source code

Java tutorial

Introduction

Here is the source code for ru.apertum.qsystem.reports.formirovators.DistributionMedDayServices.java

Source

package ru.apertum.qsystem.reports.formirovators;

/*
 *  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/>.
 */
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 DistributionMedDayServices 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 Response getDialog(String driverClassName, String url, String username, String password,
            HttpRequest request, String errorMessage) {
        final Response result = getDialog("/ru/apertum/qsystem/reports/web/get_date_distribution.html", request,
                errorMessage);
        final StringBuilder services_select = new StringBuilder();

        try {
            result.setData(new String(result.getData(), "UTF-8").replaceFirst("#DATA_FOR_TITLE#",
                    "   ?   :")
                    .replaceFirst("#DATA_FOR_SERVICES#", services_select.toString()).getBytes("UTF-8"));
        } catch (UnsupportedEncodingException ex) {
        }
        services_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() == 1) {
            // date/service_id/service
            Date date;
            String sdate;
            try {
                date = Uses.format_dd_MM_yyyy.parse(params.get("date"));
                sdate = (new java.text.SimpleDateFormat("yyyy-MM-dd")).format(date);
            } catch (NumberFormatException | ParseException ex) {
                return "<br>  ! ? ?    (..).";
            }
            paramMap.put("sdate", sdate);
            paramMap.put("date", date);
        } else {
            return "<br>  !";
        }
        return null;
    }
}