Package com.javaeye.jert.domain

Source Code of com.javaeye.jert.domain.ReportInstance

package com.javaeye.jert.domain;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.javaeye.jert.domain.query.Query;
import com.javaeye.jert.domain.query.QueryParameter;

/**
* @author Quake Wang
* @since 2004-7-30
* @version $Revision: 1.9 $
*/
public class ReportInstance {
    public static int DEFAULT_COUNT_ON_EACH_PAGE = 10;
    private ReportDefinition reportDefinition;
    private ResultSetOutput resultSetOutput;
    private int totalCount;
    private int startIndex;
    private int countOnEachPage;

    public ReportInstance() {
        this(DEFAULT_COUNT_ON_EACH_PAGE);
    }

    public ReportInstance(int countOnEachPage) {
        startIndex = 0;
        if (countOnEachPage < 1) {
            throw new IllegalArgumentException("Count should be greater than zero!");
        } else {
            this.countOnEachPage = countOnEachPage;
        }
    }

    public void generateItems(QueryParameter[] parameters) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet rs = null;
        Query query = reportDefinition.getQuery();

        try {
            connection = reportDefinition.getDatabase().getConnection();
            statement = connection.prepareStatement(query.getSql(parameters), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            if (parameters != null) {
                int index = 0;
                for (int i = 0; i < parameters.length; i++) {
                    if (parameters[i].isNeedToSet()) {
                        index++;
                        statement.setObject(index, parameters[i].getQueryValue());
                    }
                }
            }
            rs = statement.executeQuery();
            resultSetOutput = new ResultSetOutput(rs, getStartIndex(), getCountOnEachPage());

            rs.last();
            totalCount = rs.getRow();
        } catch (SQLException sqle) {
            //TODO: change to a customize execption
            throw new RuntimeException("Generate report error", sqle);
        } finally {
            try {
                rs.close();
            } catch (Exception e) {
            }
            try {
                statement.close();
            } catch (Exception e) {
            }
            try {
                connection.close();
            } catch (Exception e) {
            }
        }
    }

    public void setStartIndex(int startIndex) {
        this.startIndex = startIndex;
    }

    public void setCountOnEachPage(int countOnEachPage) {
        this.countOnEachPage = countOnEachPage;
    }

    public ResultSetOutput getResultSetOutput() {
        return resultSetOutput;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public int getEndIndex() {
        int endIndex = getStartIndex() + countOnEachPage;
        if (endIndex > totalCount)
            return totalCount;
        else
            return endIndex;
    }

    public int getStartIndex() {
        return startIndex;
    }

    public int getNextIndex() {
        return getNextStartIndexes()[0];
    }

    public int getPreviousIndex() {
        int previousIndexes[] = getPreviousStartIndexes();
        return previousIndexes[previousIndexes.length - 1];
    }

    public int[] getNextStartIndexes() {
        int index = getEndIndex();
        if (index == totalCount)
            return null;
        int count = (totalCount - index) / countOnEachPage;
        if ((totalCount - index) % countOnEachPage > 0)
            count++;
        int result[] = new int[count];
        for (int i = 0; i < count; i++) {
            result[i] = index;
            index += countOnEachPage;
        }

        return result;
    }

    public int[] getPreviousStartIndexes() {
        int index = getStartIndex();
        if (index == 0)
            return null;
        int count = index / countOnEachPage;
        if (index % countOnEachPage > 0)
            count++;
        int result[] = new int[count];
        for (int i = count - 1; i > 0; i--) {
            index -= countOnEachPage;
            result[i] = index;
        }

        return result;
    }

    public int getCountOnEachPage() {
        return countOnEachPage;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public ReportDefinition getReportDefinition() {
        return reportDefinition;
    }

    public void setReportDefinition(ReportDefinition reportDefinition) {
        this.reportDefinition = reportDefinition;
    }
}
TOP

Related Classes of com.javaeye.jert.domain.ReportInstance

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.