Subversion Repositories bacoAlunos

Rev

Rev 1298 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package pt.estgp.estgweb.services.sigesimports;

import jomm.dao.impl.AbstractDao;
import jomm.utils.FilesUtils;
import jomm.utils.PdfUtils;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.fop.apps.FOPException;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import pt.estgp.estgweb.Globals;
import pt.estgp.estgweb.domain.*;
import pt.estgp.estgweb.domain.dao.DaoFactory;
import pt.estgp.estgweb.domain.enums.LangEnum;
import pt.estgp.estgweb.services.courseunits.CreateCourseUnitXML;
import pt.estgp.estgweb.services.email.EMAILJob;
import pt.estgp.estgweb.services.email.SendEmailService;
import pt.estgp.estgweb.services.expceptions.ServiceException;
import pt.estgp.estgweb.services.jobs.JobScheduleService;
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.estgp.estgweb.services.sigesimports.oracle.dao.AulaDao;
import pt.estgp.estgweb.services.sigesimports.oracle.dao.connection.ConnectionManager;
import pt.estgp.estgweb.utils.Dom4jUtil;
import pt.estgp.estgweb.web.FtpServer;
import pt.utl.ist.berserk.logic.serviceManager.IService;

import javax.xml.transform.TransformerException;
import javax.xml.transform.dom.DOMSource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Jorge Machado
 * @date 11/May/2008
 * @time 12:51:32
 * @see pt.estgp.estgweb
 */

public class ImportSumariesJson extends ServiceJob implements IService
{

    private 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(ImportSumariesJson.class);

    private static final int MAX_COMMIT = 10;


    public ILogMessages run(1.5.0/docs/api/java/lang/String.html">String year,1.5.0/docs/api/java/lang/String.html">String semestre) throws ServiceException
    {
        return run(year,semestre,true,true);
    }

    public ILogMessages run(1.5.0/docs/api/java/lang/String.html">String year,1.5.0/docs/api/java/lang/String.html">String semestre,boolean sendIONLINE,boolean sendEMAIL) throws ServiceException
    {
        1.5.0/docs/api/java/lang/String.html">String msgS = "STARTING SUMARIES IMPORT SERVICE FOR year: " + year + " semestre: " + semestre + " sendIONLINE: " + sendIONLINE + " sendEMAIL: " + sendEMAIL;
        serviceLogInfo(msgS);
        logger.info(msgS);

        DefaultLogMessages logMessages = new DefaultLogMessages();
        logMessages.addMessage(new DefaultLogMessage("import.summaries", LogMessageTypeEnum.INFO, "instituicao " + DaoFactory.getConfigurationDaoImpl().getSigesInstitutionCode()));
        serviceLogInfo(logMessages.getLastMessage());


        int countUnits = 0;
        int countUnitsOk = 0;
        int countUnitsZeroSummaries = 0;
        int countUnitsZeroValidSummaries = 0;

        List<String> unitsZeroSummaries = new ArrayList<String>();
        List<String> unitsFailed = new ArrayList<String>();
        try
        {

           /* SiGesWEB service;
            String WSDL = DaoFactory.getConfigurationDaoImpl().getSigesWebServicesWsdl();
            try
            {
                serviceLogInfo("STARTING WEB SERVICE AT " + WSDL);
                service = new SiGesWEB(new URL(WSDL), new QName(Globals.SIGES_WEBSERVICE_TARGET_NAMESPACE, "SiGesWEB"));
            }
            catch (Throwable e)
            {
                logMessages.addMessage(new DefaultLogMessage("import.error",e.toString(),"erro na configuracao do WEB Service", LogMessageTypeEnum.INFO));
                serviceLogInfo(logMessages.getLastMessage());
                logger.fatal(e, e);
                serviceLogError(e.toString(), e);
                return logMessages;
            }*/


            int i = 0;
            //List<Long> cUs = DaoFactory.getCourseUnitDaoImpl().loadIds(year,DatesUtils.getSemestre());
            List<Long> cUs = DaoFactory.getCourseUnitDaoImpl().loadIds(year,semestre);
            1.5.0/docs/api/java/lang/String.html">String nowCode = "";
            1.5.0/docs/api/java/sql/Connection.html">Connection conn = ConnectionManager.openConnection();
            boolean connError = false;
            for(1.5.0/docs/api/java/lang/Long.html">Long cId: cUs)
            {
                if(connError)
                    break;
                nowCode = "" + cId;
                countUnits++;
                try
                {
                    if (i++ > MAX_COMMIT)
                    {
                        i = 0;
                        setProgress((int) (((float)countUnits)/((float)cUs.size())*100.0f));
                        commitPartially();
                    }
                    CourseUnit c = DaoFactory.getCourseUnitDaoImpl().load(cId);
                    nowCode = c.getCode();
                    logMessages.addMessage(new DefaultLogMessage("import.info","Importing Summaries to CourseUnit: " + c.getCode() + ": course: " + c.getCourse().getCode(),"", LogMessageTypeEnum.INFO));
                    logger.info("Importing Summaries to CourseUnit: " + c.getName() + " (" + c.getCode() + ") course " + c.getCourse().getName() + " (" + c.getCourse().getCode() + ")");
                    serviceLogInfo(logMessages.getLastMessage());

                    int tries = 3;
                    //ArrayOfAula arrayOfAulas = null;
                    ArrayList<pt.estgp.estgweb.services.sigesimports.oracle.domain.Aula> arrayOfAulas = null;
                    while(tries > 0)
                    {
                    //comentario de teste
                        try
                        {

                            //arrayOfAulas = service.getSiGesWEBSoap().getAulas(new BigDecimal(DaoFactory.getConfigurationDaoImpl().getSigesInstitutionCode()), year,c.getCode(),semestre,c.getCourse().getCode());
                            arrayOfAulas = AulaDao.getInstance().loadAulas(DaoFactory.getConfigurationDaoImpl().getSigesInstitutionCode(),year,c.getCode(),semestre,c.getCourse().getCode(),conn);

                            break;
                        }
                        catch(1.5.0/docs/api/java/lang/Throwable.html">Throwable t)
                        {
                            if(tries-- == 0)
                            {
                                logger.error("Giving up of courseUnit:" + cId + " ( " + c.getName() + " ) ",t);
                                serviceLogError("Giving up of courseUnit:" + cId + " ( " + c.getName() + " ) ",t);
                                connError = true;
                                break;
                            }


                            logger.error("WebService Fail, trying " + tries + " times sleeping a while",t);
                            serviceLogError("WebService Fail, trying " + tries + " times sleeping a while " + t.toString());
                            serviceLogError("WebService Fail, trying " + tries + " times sleeping a while " + t.getCause());
                            1.5.0/docs/api/java/lang/Thread.html">Thread.sleep(5000);
                        }
                    }

                    if(arrayOfAulas != null && arrayOfAulas.size() > 0)
                    {
                        List<CourseUnitSummary> courseUnitSummaries = new ArrayList<CourseUnitSummary>();

                        //HashMap<String, CourseUnitSummary> map = new HashMap<String, CourseUnitSummary>();
                        /*if(c.getSummaries() != null && c.getSummaries().size() > 0)
                            for(CourseUnitSummary s: c.getSummaries())
                                map.put(s.getCodigoAula() + ":" + s.getCodigoSumario(),s);
                        else
                            c.setSummaries(new HashSet<CourseUnitSummary>());*/

                        JSONObject summariesJson = new JSONObject();
                        JSONArray summaries = new JSONArray();
                        summariesJson.put("summary",summaries);
                        int summariesValid = 0;
                        for(pt.estgp.estgweb.services.sigesimports.oracle.domain.Aula aula: arrayOfAulas)
                        {

                            //todo cdTurma Rollback aqui não retirar o sumário
                            //Mas pensar em separar os sumários na página, podemos
                            //separar tendo em conta quem entra, se for aluno só vê os
                            //seus sumários, se for prof vê os sumários das suas turmas

                            /*
                            if(c.getCdTurma() != null && aula.getAula_codigoTurma()!=null
                                    && c.getCdTurma().trim().length() > 0
                                    && aula.getAula_codigoTurma().trim().length() > 0
                                    && !aula.getAula_codigoTurma().equals(c.getCdTurma()))
                            {
                                //Código de Turma diferente
                                System.out.println("Ignoring summary code: " + aula.getAula_codigoTurma() + " for unit " + c.getName() + "/" + c.getCode() + " T:" + c.getCdTurma());
                                continue;
                            }*/


                            //CourseUnitSummary s = map.get(aula.getSumNumeroAula() + ":" + aula.getCodigoSumario());
                            //if(s == null)
                            //{
                            CourseUnitSummaryImpl s = DomainObjectFactory.createCourseUnitSummaryImpl();
                            s.setCourseUnit(c);
                                //c.getSummaries().add(s);
                                //DaoFactory.getCourseUnitSummaryDaoImpl().save(s);
                            //summaries.add();
                            //}
                            s.setCdTipoOcupacao(aula.getTipoOcupacao().intValue());
                            s.setNrAula(aula.getNumeroAula().intValue());
                            s.setCodigoSumario(""+aula.getCodigoSumario().intValue());
                            s.setCodigoAula("" + aula.getSum_numeroAula().intValue());
                            s.setNumeroAula(aula.getSum_numeroAula().intValue());
                            s.setDataAula(aula.getData());
                            s.setTipoAula(aula.getSum_tipoAula());
                            s.setCodigoSalaAula("" + aula.getAula_codigoSalaAula().intValue());
                            s.setAlunosPresentes(aula.getSum_numeroPresencas().intValue());
                            s.setTitulo(aula.getSum_titulo());
                            s.setCodigoDocente(aula.getAula_codigoDocente()+ "");
                            s.setDescricao(aula.getSum_descricao());
                            s.setcEstado(""+aula.getSum_cdEstado().intValue());
                            s.setNumeroHoras("" + aula.getNumeroCelulas().intValue());
                            summaries.put(s.getJson());
                            courseUnitSummaries.add(s);
                            if(s.getNumeroAula() > 0)
                                summariesValid++;
                        }
                        if(courseUnitSummaries.size() == 0)
                        {
                            countUnitsZeroSummaries++;
                            unitsZeroSummaries.add(c.getCode());
                            logger.info("ZERO SUMARIES unit:" + c.getCode() + " course: " + c.getCourseCode());
                            serviceLogInfo("ZERO SUMARIES unit:" + c.getCode() + " course: " + c.getCourseCode());
                        }
                        else if(summariesValid == 0)
                        {
                            //NAO METE OK NEM ZERO
                            countUnitsZeroValidSummaries++;
                            unitsZeroSummaries.add(c.getCode());
                            logger.info("ZERO VALID SUMARIES unit:" + c.getCode() + " course: " + c.getCourseCode());
                            serviceLogInfo("ZERO VALID SUMARIES unit:" + c.getCode() + " course: " + c.getCourseCode());
                        }
                        else
                        {
                            countUnitsOk++;
                        }
                        c.setSummariesJson(summariesJson.toString());

                        if(sendIONLINE)
                        {
                            1.5.0/docs/api/java/lang/String.html">String path = generatePdf(c);
                            sendPdfFtpIonline(c,new ArrayList<String>(),null,path,null);
                            try{
                                FilesUtils.delete(path);
                            }catch(1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
                            {
                                logger.error("trying to delete " + path,e);
                            }
                        }
                        if(sendEMAIL)
                        {
                            sendEmail(null, courseUnitSummaries,true);
                        }
                    }
                    else
                    {
                        //NAO METE OK MAS METE ZERO
                        countUnitsZeroSummaries++;
                        unitsZeroSummaries.add(c.getCode());
                        logger.error("Summaries come null in unit:" + c.getCode());
                        serviceLogError("Summaries come null in unit:" + c.getCode());
                    }
                }
                catch(1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
                {
                    //NAO METE OK
                    unitsFailed.add(nowCode);
                    logger.error(e,e);
                    serviceLogError(e.toString(),e);
                }
            }
            conn.close();

            setProgress(100);
            serviceLogError("Finish commiting");

        }
        catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
        {
            logger.error(e, e);
            logMessages.addMessage(new DefaultLogMessage("import.error", e.toString(), "see log for details", LogMessageTypeEnum.ERROR));
            serviceLogError(logMessages.getLastMessage(),e);
            throw new ServiceException(e.toString(), e);
        }

        logMessages.addMessage(new DefaultLogMessage("import.summaries.terminating", LogMessageTypeEnum.INFO));
        logger.info("terminating summaries import");
        serviceLogInfo(logMessages.getLastMessage());

        1.5.0/docs/api/java/lang/StringBuilder.html">StringBuilder failedUnitsExc = new 1.5.0/docs/api/java/lang/StringBuilder.html">StringBuilder();
        for(1.5.0/docs/api/java/lang/String.html">String cId: unitsFailed)
            failedUnitsExc.append(cId).append(";");

        serviceLogInfo("#######################################");
        serviceLogInfo("#######################################");
        serviceLogInfo("UNITS PROCESSED: " + countUnits);
        serviceLogInfo("UNITS FAILED: " + (countUnits - countUnitsZeroSummaries - countUnitsOk - countUnitsZeroValidSummaries));
        serviceLogInfo("UNITS FAILED EXCEPTION PARSING XML WEB SERVICE: " + unitsFailed.size());
        serviceLogInfo("UNITS IMPORT OK: " + countUnitsOk);
        serviceLogInfo("UNITS ZERO SUMARIES: " + countUnitsZeroSummaries);
        serviceLogInfo("UNITS ZERO VALID SUMARIES: " + countUnitsZeroValidSummaries);
        serviceLogInfo("#######################################");
        serviceLogWarn("####FAILED Exception (Check exceptions in log): Units Baco Id Code : " + failedUnitsExc.toString());

        logger.info("#######################################");
        logger.info("#######################################");
        logger.info("UNITS PROCESSED: " + countUnits);
        logger.info("UNITS FAILED: " + (countUnits - countUnitsZeroSummaries - countUnitsOk - countUnitsZeroValidSummaries));
        logger.info("UNITS FAILED EXCEPTION PARSING XML WEB SERVICE: " + unitsFailed.size());
        logger.info("UNITS IMPORT OK: " + countUnitsOk);
        logger.info("UNITS ZERO SUMARIES: " + countUnitsZeroSummaries);
        logger.info("UNITS ZERO VALID SUMARIES: " + countUnitsZeroValidSummaries);
        logger.info("#######################################");
        logger.warn("####FAILED Exception (Check exceptions in log): Units Baco Id Code : " + failedUnitsExc.toString());


        return logMessages;
    }


    public static SendEmailService sendEmailService = new SendEmailService();

    private void sendEmail(UserSession userSession,List<CourseUnitSummary> summaries,boolean sendEmail)
    {
        if(Globals.EMAIL_SUMMARIES && summaries.size() > 0 && sendEmail)
        {
           // for(CourseUnitSummary summary: summaries)
           // {

                List<String> emails = DaoFactory.getCourseUnitSummaryDaoImpl().getStudentsEmails(summaries.get(0).getId());
                if(emails == null)
                    logger.warn("No students emails in unit: code:" + summaries.get(0).getCourseUnit().getCode() + " " + summaries.get(0).getCourseUnit().getName());
                EMAILJob emailJob = new EMAILJob(sendEmailService.getSummariesEmail(emails, summaries, summaries.get(0).getCourseUnit(), LangEnum.PORTUGUESE));
                JobScheduleService.getInstance().scheduleNow(emailJob,"New Summary EMAIL from: " + summaries.get(0).getCourseUnit().getName() + " aula: " +summaries.get(summaries.size()-1).getNumeroAula() ,userSession);
           // }
        }
    }






    public static 1.5.0/docs/api/java/lang/String.html">String generatePdf(CourseUnit c) throws 1.5.0/docs/api/java/io/IOException.html">IOException, 1.5.0/docs/api/javax/xml/transform/TransformerException.html">TransformerException, FOPException {
        5+0%2Fdocs%2Fapi+Document">Document dom = new CreateCourseUnitXML().run(c,null);
        org.w3c.dom.5+0%2Fdocs%2Fapi+Document">Document dd = Dom4jUtil.toW3c(dom);
        1.5.0/docs/api/java/lang/String.html">String path = Globals.TMP_DIR + 1.5.0/docs/api/java/io/File.html">File.separator + c.getCode() + "-" + c.getNormalizedName() + "-sumarios.pdf";
        1.5.0/docs/api/java/io/FileOutputStream.html">FileOutputStream out = new 1.5.0/docs/api/java/io/FileOutputStream.html">FileOutputStream(path);
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("site", Globals.SITE_URL);
        PdfUtils.createPdfFromXml(new 1.5.0/docs/api/javax/xml/transform/dom/DOMSource.html">DOMSource(dd), "pt/estgp/estgweb/services/courseunits/courseunitsummaries.fo.xsl", out, params);
        out.flush();
        out.close();
        return path;
    }

    private void sendPdfFtpIonline(CourseUnit cu, List<String> errors, UserSession u,1.5.0/docs/api/java/lang/String.html">String pdfPath,FTPClient client) throws 1.5.0/docs/api/java/io/IOException.html">IOException, 1.5.0/docs/api/javax/xml/transform/TransformerException.html">TransformerException, FOPException {

        FtpServer server = null;
        if(client == null)
        {
            server = FtpServer.getNewServer(Globals.FTP_IONLINE_URL,Globals.FTP_IONLINE_USER,Globals.FTP_IONLINE_PASS);
            client = server.getClient();
            if(client == null)
            {
                logger.warn("###################");
                logger.warn("################### > CANT CONNECT FTP - could be FTPTimeout: " + Globals.FTP_TIMEOUT_SECONDS);
                errors.add("################### > CANT CONNECT FTP to send summaries " + ((CourseUnitImpl)cu).getSummariosPathIntranet());
                return;
            }

        }
        client.setFileType(FTP.BINARY_FILE_TYPE);


        1.5.0/docs/api/java/lang/String.html">String pastaFichaCurricular = ((CourseUnitImpl)cu).getSummariosPathIntranet();
        if(!client.changeWorkingDirectory(pastaFichaCurricular))
        {
            logger.error("FTP CANT CHANGE TO PATH: " + pastaFichaCurricular);
        }
        else
        {
            if(!client.storeFile(((CourseUnitImpl)cu).getSummariosFileNameIntranet(),new 1.5.0/docs/api/java/io/FileInputStream.html">FileInputStream(pdfPath)))
            {
                1.5.0/docs/api/java/lang/String.html">String msgS = "CANT store file in given time, probably timeout at " + Globals.FTP_DATA_STORE_TIMEOUT_SECONDS;
                logger.error(msgS);
                serviceLogError(msgS);
            }
            if(server != null)
            {
                client.quit();
                client.disconnect();
            }
        }
        //
    }
    /**
     * Testar por aqui poi requer Super Role e assim e' autmatico
     *
     *
     * @param args of main
     * @throws pt.estgp.estgweb.services.expceptions.ServiceException on error
     */

    public static void main(1.5.0/docs/api/java/lang/String.html">String[] args) throws ServiceException, JSONException {

        1.5.0/docs/api/java/lang/System.html">System.out.println("Syntax:");
        1.5.0/docs/api/java/lang/System.html">System.out.println("year, semestre, sendIonline, sendEmail");
        1.5.0/docs/api/java/lang/System.html">System.out.println("If no arguments wil user actual year import config in all semestres and true in email and ionline");

        1.5.0/docs/api/java/lang/String.html">String year = DaoFactory.getConfigurationDaoImpl().getImportsDefaultImportYearCreateTransaction();
        1.5.0/docs/api/java/lang/String.html">String semestre = null;
        boolean sendIonline = false;
        boolean sendEmail = false;
        if(args != null && args.length > 0)
            year = args[0];

        if(args != null && args.length > 1)
            semestre = args[1];

        if(args != null && args.length > 2)
            sendIonline = 1.5.0/docs/api/java/lang/Boolean.html">Boolean.parseBoolean(args[2]);

        if(args != null && args.length > 3)
            sendEmail = 1.5.0/docs/api/java/lang/Boolean.html">Boolean.parseBoolean(args[3]);

        AbstractDao.getCurrentSession().beginTransaction();
        if(semestre != null)
            new ImportSumariesJson().run(year,semestre,sendIonline,sendEmail);
        else
        {
            new ImportSumariesJson().run(year,"S1",sendIonline,sendEmail);
            new ImportSumariesJson().run(year,"S2",sendIonline,sendEmail);
            new ImportSumariesJson().run(year,"A",sendIonline,sendEmail);
        }
        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 semestre =  getParametersMap().get(JOB_semestre_KEY).getObject();
        1.5.0/docs/api/java/lang/String.html">String sendIonlineStr = getParametersMap().get(JOB_sendIonline_KEY).getObject();
        1.5.0/docs/api/java/lang/String.html">String sendEmailStr =  getParametersMap().get(JOB_sendEmail_KEY).getObject();
        boolean sendIonline = sendIonlineStr != null && 1.5.0/docs/api/java/lang/Boolean.html">Boolean.parseBoolean(sendIonlineStr);
        boolean sendEmail = sendEmailStr != null && 1.5.0/docs/api/java/lang/Boolean.html">Boolean.parseBoolean(sendEmailStr);
        return run(importYear,semestre,sendIonline,sendEmail);
    }

}