Subversion Repositories bacoAlunos

Rev

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

package pt.estgp.estgweb.services.sigesimports;

import pt.utl.ist.berserk.logic.serviceManager.IService;
import pt.estgp.estgweb.services.expceptions.ServiceException;
import pt.estgp.estgweb.services.logresults.impl.DefaultLogMessages;
import pt.estgp.estgweb.services.logresults.impl.DefaultLogMessage;
import pt.estgp.estgweb.services.logresults.LogMessageTypeEnum;
import pt.estgp.estgweb.services.logresults.ILogMessages;
import pt.estgp.estgweb.Globals;
import pt.estgp.estgweb.domain.dao.DaoFactory;
import pt.estgp.estgweb.domain.Teacher;
import pt.estgp.estgweb.domain.DomainObjectFactory;
import pt.estgp.estgweb.domain.CourseUnit;
import pt.ipportalegre.siges.web.services.*;
import org.apache.log4j.Logger;

import java.util.*;
import java.math.BigDecimal;
import java.net.URL;
import java.net.MalformedURLException;

import jomm.dao.impl.AbstractDao;

import javax.xml.namespace.QName;

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

public class ImportTeachersService 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(ImportTeachersService.class);

    public ILogMessages run(1.5.0/docs/api/java/lang/String.html">String year) throws ServiceException
    {
        DefaultLogMessages logMessages = new DefaultLogMessages();
        logMessages.addMessage(new DefaultLogMessage("import.teachers", LogMessageTypeEnum.INFO, "instituicao " + Globals.SIGES_INSTITUTION_CODE));
        logger.info("starting teacher import");
        try
        {
            SiGesWEB service;
            try
            {
                service = new SiGesWEB(new 1.5.0/docs/api/java/net/URL.html">URL(Globals.SIGES_WEBSERVICE_WSDL), new 1.5.0/docs/api/javax/xml/namespace/QName.html">QName(Globals.SIGES_WEBSERVICE_TARGET_NAMESPACE, "SiGesWEB"));
            }
            catch (1.5.0/docs/api/java/net/MalformedURLException.html">MalformedURLException e)
            {
                logMessages.addMessage(new DefaultLogMessage("import.error",e.toString(),"erro na configuracao do WEB Service", LogMessageTypeEnum.INFO));
                logger.fatal(e, e);
                return logMessages;
            }
            ArrayOfDecimal codigosDocentes = service.getSiGesWEBSoap().getCodigosDocentesInscritosDaInstituicao(Globals.SIGES_INSTITUTION_CODE, year);
            List<BigDecimal> codigos = codigosDocentes.getDecimal();
            for (1.5.0/docs/api/java/math/BigDecimal.html">BigDecimal c : codigos)
            {
                Docente d = service.getSiGesWEBSoap().getDocente(c, Globals.SIGES_INSTITUTION_CODE, year);
                Teacher t = DaoFactory.getTeacherDaoImpl().loadBySigesCode(d.getCodigoFuncionario().intValue());
                if (t == null)
                {
                    t = DomainObjectFactory.createTeacherImpl();
                    DaoFactory.getTeacherDaoImpl().save(t);
                }
                persist(d, t);
            }
        }
        catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
        {
            logMessages.addMessage(new DefaultLogMessage("import.error", e.toString(), "see log for details", LogMessageTypeEnum.ERROR));
            logger.error(e, e);
            throw new ServiceException(e.toString(), e);
        }
        logMessages.addMessage(new DefaultLogMessage("import.teachers.terminating", LogMessageTypeEnum.INFO));
        logger.info("terminating teacher import");
        return logMessages;
    }

    /**
     * Nao esta testado
     * jm
     *
     * @param d docente
     * @param t teacher
     */

    private void persist(Docente d, Teacher t)
    {
        t.setName(d.getNomeFuncionarioInt());
        t.setEmail(d.getEmail());
        t.setSigesCode(d.getCodigoFuncionario().intValue());
        t.setAddress(d.getMorada());
        t.setZip("" + d.getCodigoPostal().intValue());
        t.setBi(d.getNumeroBi());
        t.setEmployerName(d.getNomeFuncionario());
        t.setAcademicName(d.getNomeAcademico());
        t.setBirthDate(d.getDataNascimento().toGregorianCalendar().getTime());
        //Desta forma as relacoes antigas sao ignoradas cria-se uma lista nova e atribui-se ao Teacher, o Hibernate faz resto e apaga as chaves estrangeiras antigas
        if (d.getDisciplinas() == null || d.getDisciplinas().getDecimal() == null || d.getDisciplinas().getDecimal().size() == 0)
            logger.warn("ATENTION TEACHER WITH ZERO UNITS: codigoFuncionario " + d.getCodigoFuncionario());
        else
        {
            Set<CourseUnit> units = new HashSet<CourseUnit>();
            for (1.5.0/docs/api/java/math/BigDecimal.html">BigDecimal unitCode : d.getDisciplinas().getDecimal())
            {
                List<CourseUnit> l = DaoFactory.getCourseUnitDaoImpl().loadBySigesCode("" + unitCode.intValue());
                if (l != null)
                    units.addAll(l);
                else
                    logger.warn("ATENTION SIGES COURSE UNIT NOT FOUND IN BACO STORAGE: curseUnit " + unitCode.intValue());
            }
            t.setTeachedUnits(units);
        }
    }

    /**
     * Testar por aqui poi requer Super Role e assim e' autmatico
     *
     * @param args of main
     * @throws ServiceException on error
     */

    public static void main(1.5.0/docs/api/java/lang/String.html">String[] args) throws ServiceException
    {
        AbstractDao.getCurrentSession().beginTransaction();
        new ImportTeachersService().run("200708");
        AbstractDao.getCurrentSession().getTransaction().commit();
    }


}