Subversion Repositories bacoAlunos

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package pt.estgp.estgweb.services.courses;

import jomm.dao.impl.AbstractDao;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.json.JSONArray;
import pt.estgp.estgweb.domain.CourseUnit;
import pt.estgp.estgweb.domain.CourseUnitImpl;
import pt.estgp.estgweb.domain.dao.DaoFactory;
import pt.estgp.estgweb.domain.views.CourseUnitView;
import pt.estgp.estgweb.services.expceptions.ServiceException;
import pt.estgp.estgweb.services.jobs.ServiceJob;
import pt.estgp.estgweb.services.logresults.ILogMessages;
import pt.estgp.estgweb.services.logresults.LogMessageTypeEnum;
import pt.estgp.estgweb.services.logresults.impl.DefaultLogMessage;
import pt.estgp.estgweb.services.logresults.impl.DefaultLogMessages;
import pt.utl.ist.berserk.logic.serviceManager.IService;

import java.io.IOException;
import java.util.List;


/**
 * Created by jorgemachado on 22/03/16.
 */

public class CourseUnitsStatisticsJobService extends ServiceJob implements IService
{

    public static final 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(CourseUnitsStatisticsJobService.class);

    private static final int MAX_COMMIT = 20;

    public ILogMessages run(1.5.0/docs/api/java/lang/String.html">String year, int institutionCode) throws ServiceException
    {
        DefaultLogMessages logMessages = new DefaultLogMessages();
        logMessages.addMessage(new DefaultLogMessage("courseunits.statistics", LogMessageTypeEnum.INFO, "instituicao " + institutionCode + " ano : "  + year));
        logger.info(logMessages.getLastMessage());
        serviceLogInfo(logMessages.getLastMessage());

        int foundTotal = 0;


        1.5.0/docs/api/javax/management/Query.html">Query q = AbstractDao.getCurrentSession().createQuery("select count(c.id) from c in class " + CourseUnit.class.getName() + " where c.institutionCode = " + institutionCode + " and c.importYear = '" + year + "'");
        1.5.0/docs/api/java/lang/Long.html">Long count = (1.5.0/docs/api/java/lang/Long.html">Long) q.list().get(0);

        q = AbstractDao.getCurrentSession().createQuery("select c.id from c in class " + CourseUnit.class.getName() + " where c.institutionCode = " + institutionCode + " and c.importYear = '" + year + "'");

        IFilesServiceFacade filesServiceFacade = null;
        try {
            filesServiceFacade = CourseUnitFilesFacadeFactory.createCourseUnitFilesFacade();
        } catch (1.5.0/docs/api/java/io/IOException.html">IOException e) {
            logger.error(e, e);
            //AbstractDao.getCurrentSession().getTransaction().rollback();
            logMessages.addMessage(new DefaultLogMessage("courseunits.statistics.error", e.toString(), "see log for details", LogMessageTypeEnum.ERROR));
            serviceLogError(logMessages.getLastMessage());
        }
        List<Long> ids = q.list();
        int i = 0;
        for(1.5.0/docs/api/java/lang/Long.html">Long id: ids)
        {
            foundTotal++;
            if (i++ > MAX_COMMIT)
            {
                i = 0;
                setProgress((int) (((float)foundTotal)/((float)count)*100.0f));
                commitPartially();
            }
            try
            {
                CourseUnitImpl cu = (CourseUnitImpl) DaoFactory.getCourseUnitDaoImpl().load(id);
                1.5.0/docs/api/java/lang/String.html">String msg = "Unit: " + cu.getCode() + " " + cu.getName() + " " + cu.getCourseName() + " (" + cu.getCourseCode() + ")";
                logger.info(msg);
                serviceLogInfo(msg);

                if(cu.getAnnouncements() != null)
                    cu.setStatanuncios(cu.getAnnouncements().size());
                if(cu.getBlogs()!=null && cu.getBlogs().size() > 0)
                    cu.setStatblogPosts(cu.getBlogs().iterator().next().getPosts().size());
                if(cu.getStudents() != null)
                    cu.setStatstudentsCount(cu.getStudents().size());
                if(cu.getTeachers() != null)
                    cu.setStatteachersCount(cu.getTeachers().size());
                if(cu.getTeachers() != null)
                    cu.setStatteachersCount(cu.getTeachers().size());
                if(cu.getAssignements() != null)
                    cu.setStatworks(cu.getAssignements().size());

                CourseUnitView courseUnitView = new CourseUnitView(cu);
                if(courseUnitView.getSummariesFromJson() != null)
                {
                    cu.setStatdtpSumaries(courseUnitView.getSummariesFromJson().size());
                    cu.setStatdtpSumariesMissing(courseUnitView.getLateSummaries());
                    cu.setStatdtpSumariesPrelancados(courseUnitView.getPrelancadosSummaries());
                }

                cu.setStatdtpFichaCurricularValid(cu.isValidProgram());
                if(cu.getCourseUnitEvaluation() == null)
                    cu.setStatdtpEvaluationReportState("INEXISTENTE");
                else if(cu.getCourseUnitEvaluation().isClosed())
                    cu.setStatdtpEvaluationReportState("VALIDO");
                else if(cu.getCourseUnitEvaluation().isTeacherComplete())
                    cu.setStatdtpEvaluationReportState("ENTREGUE");
                else if(cu.getEvaluationStreamId() != null)
                    cu.setStatdtpEvaluationReportState("INICIADO");
                else
                    cu.setStatdtpEvaluationReportState("INEXISTENTE");


                if(filesServiceFacade != null)
                {
                    filesServiceFacade.reset(cu);
                    try
                    {
                        cu.setStatcontentsFiles((int) filesServiceFacade.getContentsNumberOfFiles());
                        cu.setStatcontentsFolders((int) filesServiceFacade.getContentsNumberOfFolders());
                        cu.setStatcontentsLastUpdate(filesServiceFacade.getContentsLastUpdate());
                        cu.setStatcontentsSize(filesServiceFacade.getContentsSize());

                        cu.setStatdtpPlaneamentoFiles((int) filesServiceFacade.getDtpPlaneamentoNumberOfFiles());
                        cu.setStatdtpPlaneamentoLastUpdate(filesServiceFacade.getDtpPlaneamentoLastUpdate());


                        cu.setStatdtpFichaCurricularLastUpdate(filesServiceFacade.getDtpFichaCurricularLastUpdate());
                        cu.setStatdtpEvaluationReportLastUpdate(filesServiceFacade.getDtpRelatorioAvaliacaoLastUpdate());


                        cu.setStatdtpInqueritoPedagogicoFiles((int) filesServiceFacade.getDtpInqueritoPedagogicoNumberOfFiles());
                        cu.setStatdtpInqueritoPedagogicoFilesLastUpdate(filesServiceFacade.getDtpInqueritoPedagogicoLastUpdate());

                        cu.setStatdtpAvaliacaoEnunciadosFiles((int) filesServiceFacade.getDtpAvaliacaoEnunciadosNumberOfFiles());
                        cu.setStatdtpAvaliacaoEnunciadosLastUpdate(filesServiceFacade.getDtpAvaliacaoEnunciadosLastUpdate());

                        cu.setStatdtpAvaliacaoPautasFiles((int) filesServiceFacade.getDtpAvaliacaoNotasNumberOfFiles());
                        cu.setStatdtpAvaliacaoPautasLastUpdate(filesServiceFacade.getDtpAvaliacaoNotasLastUpdate());

                        JSONArray files = filesServiceFacade.getAllFilesJsonFields();
                        for(int j = 0; j < files.length();j++ )
                        {
                            files.getJSONObject(j).put("id","baco/courseunit/" + cu.getId() + "/" + files.getJSONObject(j).getString("file"));
                            files.getJSONObject(j).put("obj_sub_type","baco/courseunit:" + cu.getId());
                            files.getJSONObject(j).put("obj_type","baco/courseunit:file");
                        }
                        cu.setFilesJson(files.toString());
                    }
                    catch(1.5.0/docs/api/java/lang/Exception.html">Exception e)
                    {   logger.warn(e, e);
                        //AbstractDao.getCurrentSession().getTransaction().rollback();
                        logMessages.addMessage(new DefaultLogMessage("courseunits.statistics.warning", e.toString(), "see log for details", LogMessageTypeEnum.ERROR));
                        serviceLogWarn(logMessages.getLastMessage());
                    }
                 }




            }
            catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
            {
                logger.error(e, e);
                //AbstractDao.getCurrentSession().getTransaction().rollback();
                logMessages.addMessage(new DefaultLogMessage("courseunits.statistics.error", e.toString(), "see log for details", LogMessageTypeEnum.ERROR));
                serviceLogError(logMessages.getLastMessage());
            }
        }

        if(filesServiceFacade != null)
            try {
                filesServiceFacade.close();
            } catch (1.5.0/docs/api/java/io/IOException.html">IOException e) {
                logger.error(e, e);
                //AbstractDao.getCurrentSession().getTransaction().rollback();
                logMessages.addMessage(new DefaultLogMessage("courseunits.statistics.error", e.toString(), "see log for details", LogMessageTypeEnum.ERROR));
                serviceLogError(logMessages.getLastMessage());
            }

        setProgress(100);
        logger.info("######################################");
        serviceLogInfo("######################################");
        logger.info("######################################");
        serviceLogInfo("######################################");
        logger.info("STATS " + year);
        serviceLogInfo("STATS " + year);
        logger.info("Total Notas: " + foundTotal);
        serviceLogInfo("Total Notas: " + foundTotal);


        logMessages.addMessage(new DefaultLogMessage("courseunits.statistics.terminating", LogMessageTypeEnum.INFO));
        logger.info(logMessages.getLastMessage());
        serviceLogInfo(logMessages.getLastMessage());
        return logMessages;
    }

    public static void main(1.5.0/docs/api/java/lang/String.html">String[] args) throws ServiceException
    {
        1.5.0/docs/api/java/lang/String.html">String year = DaoFactory.getConfigurationDaoImpl().getImportsDefaultImportYearCreateTransaction();;
        1.5.0/docs/api/java/lang/String.html">String institutionCode = "1";
        if(args != null && args.length > 0)
            year = args[0];
        if(args != null && args.length > 1)
            institutionCode = args[1];
        AbstractDao.getCurrentSession().beginTransaction();
        new CourseUnitsStatisticsJobService().run(year,1.5.0/docs/api/java/lang/Integer.html">Integer.parseInt(institutionCode));
        AbstractDao.getCurrentSession().getTransaction().commit();
    }

    @1.5.0/docs/api/java/lang/Override.html">Override
    protected ILogMessages runJobServiceTask() throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable {
        1.5.0/docs/api/java/lang/String.html">String importYear =  getParametersMap().get(JOB_importYear_KEY).getObject();
        1.5.0/docs/api/java/lang/String.html">String institutionCode = getParametersMap().get(JOB_institution_KEY).getObject();
        return run(importYear,1.5.0/docs/api/java/lang/Integer.html">Integer.parseInt(institutionCode));
    }
}