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

Java tutorial

Introduction

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

Source

/*
 *  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.sql.Connection;
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.commons.lang.time.DateUtils;
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 Evgeniy Egorov
 */
public class RatioServices 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, Date> 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(RatioServices.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);
    QLog.l().logger().trace("?  \"" + data + "\".");
    //  ? 
    boolean flag = false;
    String mess = "";
    if ("".equals(data)) {
        flag = true;
    } else {
        //sd=20.01.2009&ed=28.01.2009
        //   ?  
        final String[] ss = data.split("&");
        if (ss.length == 2) {
            final String[] ss0 = ss[0].split("=");
            final String[] ss1 = ss[1].split("=");
            Date sd = null;
            Date fd = null;
            Date fd1 = null;
            flag = !(ss0.length == 2 && ss1.length == 2);
            if (!flag) {
        
                try {
                    sd = Uses.format_dd_MM_yyyy.parse(ss0[1]);
                    fd = Uses.format_dd_MM_yyyy.parse(ss1[1]);
                    fd1 = DateUtils.addDays(Uses.format_dd_MM_yyyy.parse(ss1[1]), 1);
        
                } catch (ParseException ex) {
                    mess = "<br>  ! ? ?   (..).";
                    flag = true;
                }
            } else {
                mess = "<br>  ! ? ?   (..).";
            }
            if (!flag) {
                if (!sd.after(fd)) {
                    paramMap.put(ss0[0], sd);
                    paramMap.put(ss1[0], fd);
                    paramMap.put("ed1", fd1);
                } else {
                    mess = "<br>  !     ?.";
                    flag = true;
                }
            }
        
        } else {
            mess = "<br>  !";
            flag = true;
        }
    }
    if (flag) {
        // ?  ??     
        //      .
        // ? ? ??   ?    ?   null,
        //      getDataSource ?  .
        // ? ?  ?        . ?    ,  ?  .
        // get_period_for_statistic_services.html
        final InputStream inStream = getClass().getResourceAsStream("/ru/apertum/qsystem/reports/web/get_period_for_ratio_services.html");
        byte[] result = null;
        try {
            result = Uses.readInputStream(inStream);
        } catch (IOException ex) {
            throw new Uses.ReportException(" ? ?? ?   . " + ex);
        }
        result = new String(result).replaceFirst(Uses.ANCHOR_DATA_FOR_REPORT, request.getRequestLine().getUri()).replaceFirst(Uses.ANCHOR_ERROR_INPUT_DATA, mess).getBytes();
        return result;
    } else {
        return null;
    }
        }
        
    */
    @Override
    public Response getDialog(String driverClassName, String url, String username, String password,
            HttpRequest request, String errorMessage) {
        return getDialog("/ru/apertum/qsystem/reports/web/get_period_for_statistic_services.html", request,
                errorMessage);
    }

    @Override
    public String validate(String driverClassName, String url, String username, String password,
            HttpRequest request, HashMap<String, String> params) {
        //sd=20.01.2009&ed=28.01.2009
        //   ?  
        QLog.l().logger().trace("?  \"" + params.toString() + "\".");
        if (params.size() == 2) {
            Date sd;
            Date fd;
            Date fd1;
            try {
                sd = Uses.format_dd_MM_yyyy.parse(params.get("sd"));
                fd = Uses.format_dd_MM_yyyy.parse(params.get("ed"));
                fd1 = DateUtils.addDays(Uses.format_dd_MM_yyyy.parse(params.get("ed")), 1);
            } catch (ParseException ex) {
                return "<br>  ! ? ?   (..).";
            }
            if (!sd.after(fd)) {
                paramMap.put("sd", sd);
                paramMap.put("ed", fd);
                paramMap.put("ed1", fd1);
            } else {
                return "<br>  !     ?.";
            }

        } else {
            return "<br>  !";
        }
        return null;// ? 
    }
}