/*
 * Copyright 2009 Lari Natri <lari.natri@gmail.com>
 *
 *  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 compid.report;

import net.aavalla.utils.jobpool.JobCategory;
import compid.data.DAOException;
import compid.data.DAOFactory;
import compid.data.QueryHandler;
import compid.report.ManualQueryReporter.ManualQueryReporterParams;
import net.aavalla.utils.jobpool.parameters.FieldAnnotations;
import net.aavalla.utils.jobpool.parameters.validators.StringValidator;

/**
 *
 * @author Lari Natri
 */
public class ManualQueryReporter extends Reporter<ManualQueryReporterParams> {
    
    public final class ManualQueryReporterParams extends ReporterParams {
        @FieldAnnotations(
            name="Report Title",
            validator=StringValidator.class,
            validatorSettings={"1","64",FieldAnnotations.NULL})
        private String reportTitle = "Manual Query Results";
        
        @FieldAnnotations(
            name="SQL Query",
            lines=8)
            //validator=StringValidator.class,
            //validatorSettings={"0","4096",FieldAnnotations.NULL})
            // "[Ss][Ee][Ll][Ee][Cc][Tt] [.\\n\\r\\f\\u0085\\u2028\\u2029]*" doesn't work :)
        private String query = "SELECT "; //NOI18N

        @Override
        public synchronized String validateFieldRelations() {
            if (query.trim().toUpperCase().startsWith("SELECT")) { // NOI18N
                return null;
            } else {
                return "Query must start with SELECT";
            }
        }

        public ManualQueryReporterParams() {
            this.setDescription("Executes a manual query. See documentation for help.");
            this.setHelpID("manquery");
        }
    }
    
    public ManualQueryReporter() {
        super();
        this.addCategory(JobCategory.MEMORY);
        this.addCategory(JobCategory.DB_READ);
        this.addConflictingCategory(JobCategory.DB_WRITE);
        this.setName("Manual Query");
    }

    @Override
    protected ReportModel createReport(ManualQueryReporterParams p) throws DAOException {
        QueryHandler qh = DAOFactory.getInstance().getQueryHandler();
        ReportPageModel page = new ReportPageModel(p.reportTitle,"Results of Manual Query");
        ReportModel report = new ReportModel(p.reportTitle,"Results of Manual Query", p);
        qh.executeManualQuery(p.query, page);
        page.setPageIsFinal();
        report.addPage(page);
        return report;
    }

    @Override
    protected ManualQueryReporterParams getNonfinalEditableParameters() {
        return new ManualQueryReporterParams();
    }


}
