ca.qc.cegepoutaouais.tge.pige.server.report.ReportDataProvider.java Source code

Java tutorial

Introduction

Here is the source code for ca.qc.cegepoutaouais.tge.pige.server.report.ReportDataProvider.java

Source

/*
 * Copyright 2010, 2011 Renaud Brub
 *
 * This file is part of PIGE.
 *
 * PIGE 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.
 *
 * PIGE 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 PIGE.  If not, see <http://www.gnu.org/licenses/>.
 */
package ca.qc.cegepoutaouais.tge.pige.server.report;

import ca.qc.cegepoutaouais.tge.pige.client.LoanStatus;
import ca.qc.cegepoutaouais.tge.pige.dao.pojos.Loan;
import ca.qc.cegepoutaouais.tge.pige.dao.pojos.User;
import ca.qc.cegepoutaouais.tge.pige.server.LogHelper;
import ca.qc.cegepoutaouais.tge.pige.server.PigeHibernateUtil;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

/**
 * Cette classe permet d'obtenir les informations provenant de la base de donnes
 * afin de les insrer dans les rapports.
 *
 * @author Renaud Brub
 */
public class ReportDataProvider {

    private static final Logger logger = LogHelper.getLogger(ReportDataProvider.class);

    public ReportDataProvider() {
    }

    /**
     * Retourne une liste des usagers ayant des emprunts en retards.
     *
     * @return liste des usagers
     */
    public List<User> getAllUserWithLateLoan() {

        Transaction tx = null;
        List<User> users = null;
        Session session = null;

        try {
            session = PigeHibernateUtil.openSession();
            tx = session.beginTransaction();

            Criteria userCriteria = session.createCriteria(User.class);
            Criteria loanCriteria = userCriteria.createCriteria(User.LOAN_COLLECTION_REF, "ln", Criteria.LEFT_JOIN);
            loanCriteria.add(Restrictions.eq(Loan.STATUS_REF, LoanStatus.STATUS_LATE));
            users = (List) loanCriteria.list();

            tx.commit();

        } catch (Exception hex) {
            logger.error(hex);
            if (tx != null) {
                tx.rollback();
            }
        } finally {
            if (session != null) {
                session.close();
            }
        }

        if (users == null) {
            users = new ArrayList();
        }

        // Le hashset permet de retirer les doublons provenant de la requte
        // SQL.
        return new ArrayList(new HashSet(users));

    }

    /**
     * Permet d'obtenir des donnes de la base de donnes afin de gnrer
     * des statistiques utiles pour les rapports.
     *
     * @param stats map contenant les statistiques gnres
     */
    public void calculateLoanStatistics(Map stats) {

        Transaction tx = null;
        Session session = null;

        try {
            session = PigeHibernateUtil.openSession();
            tx = session.beginTransaction();

            Integer totalCount = (Integer) session.createCriteria(Loan.class).setProjection(Projections.rowCount())
                    .uniqueResult();

            Integer lentCount = (Integer) session.createCriteria(Loan.class)
                    .add(Restrictions.eq(Loan.STATUS_REF, LoanStatus.STATUS_LENT))
                    .setProjection(Projections.rowCount()).uniqueResult();

            Integer lateCount = (Integer) session.createCriteria(Loan.class)
                    .add(Restrictions.eq(Loan.STATUS_REF, LoanStatus.STATUS_LATE))
                    .setProjection(Projections.rowCount()).uniqueResult();

            tx.commit();

            stats.put("total-count", totalCount);
            stats.put("lent-count", lentCount + lateCount);
            stats.put("late-count", lateCount);

            Float tc = new Float(totalCount);
            Float lc = new Float(lentCount + lateCount);
            Float percent = lc / tc * 100.0f;

            stats.put("percent-lent", percent);

            Float lec = new Float(lentCount);
            Float lac = new Float(lateCount);
            percent = lac / (lec + lac) * 100.0f;

            stats.put("percent-late", percent);

        } catch (Exception hex) {
            logger.error(hex);
            if (tx != null) {
                tx.rollback();
            }
        } finally {
            if (session != null) {
                session.close();
            }
        }

    }

}