Subversion Repositories bacoAlunos

Rev

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

package pt.estgp.estgweb.services.courseunits;

import jomm.utils.BytesUtils;
import jomm.utils.FilesUtils;
import jomm.utils.StringsUtils;
import org.apache.log4j.Logger;
import pt.estgp.estgweb.Globals;
import pt.estgp.estgweb.domain.Course;
import pt.estgp.estgweb.domain.CourseUnit;
import pt.estgp.estgweb.domain.Teacher;
import pt.estgp.estgweb.domain.UserSession;
import pt.estgp.estgweb.domain.dao.DaoFactory;
import pt.estgp.estgweb.services.expceptions.ServiceException;
import pt.estgp.estgweb.utils.DatesUtils;
import pt.utl.ist.berserk.logic.serviceManager.IService;

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

/*
 * @author Goncalo Luiz gedl [AT] rnl [DOT] ist [DOT] utl [DOT] pt
 *
 *
 * Created at 17/Out/2003 , 23:45:24
 *
 */

/**
 * @author Jorge Machado
 *
 *
 * Created at 17/Out/2003 , 23:45:24
 *
 */

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

    public 1.5.0/docs/api/java/lang/String.html">String run(1.5.0/docs/api/java/lang/String.html">String importYear, UserSession userSession) throws ServiceException
    {
        if(importYear == null || importYear.trim().length() == 0)
            importYear = DaoFactory.getConfigurationDaoImpl().getInterfaceImportYear();
        1.5.0/docs/api/java/lang/String.html">String importYearIntranet = DatesUtils.getImportYearIntranet(importYear);
        1.5.0/docs/api/java/lang/String.html">String tmpName =  BytesUtils.generateHexKey();
        1.5.0/docs/api/java/lang/String.html">String tmpDir = Globals.TMP_DIR + "/" + tmpName;
        new 1.5.0/docs/api/java/io/File.html">File(tmpDir).mkdirs();
        //List<Course> courses = DaoFactory.getCourseDaoImpl().findAll(importYear);
        List<Course> courses = DaoFactory.getCourseDaoImpl().findAll();
        try
        {
            1.5.0/docs/api/java/io/FileWriter.html">FileWriter batRunner = new 1.5.0/docs/api/java/io/FileWriter.html">FileWriter(tmpDir + "/create.bat");

            1.5.0/docs/api/java/lang/String.html">String back = "";
            for(Course c: courses)
            {
                //O curso é sempre o mesmo ID mas muda de Ano consoante o ano de importação
                List<CourseUnit> courseUnits = DaoFactory.getCourseUnitDaoImpl().loadByCourse(c.getId(),importYear);
                if(courseUnits == null || courseUnits.size() == 0)
                {
                    logger.info("Skipping course: " + c.getCode() + ":" + c.getName() + " zero units in " + importYear);
                }
                else{
                    1.5.0/docs/api/java/lang/String.html">String couseDirPath = tmpDir + "/" + StringsUtils.normalizeResourceName(c.getName()) + '.' + c.getCode();
                    1.5.0/docs/api/java/io/File.html">File courseDir = new 1.5.0/docs/api/java/io/File.html">File(couseDirPath);
                    courseDir.mkdirs();
                    try
                    {

                        1.5.0/docs/api/java/lang/String.html">String courseImportYearPathDos = couseDirPath + "\\" + importYearIntranet;
                        1.5.0/docs/api/java/lang/String.html">String courseImportYearPath = couseDirPath + "/" + importYearIntranet;
                        1.5.0/docs/api/java/io/File.html">File courseImportYearDir = new 1.5.0/docs/api/java/io/File.html">File(courseImportYearPath);
                        courseImportYearDir.mkdirs();
                        new 1.5.0/docs/api/java/io/File.html">File(courseImportYearPath + "/" + Globals.INTRANET_SEMESTRE_S1).mkdirs();
                        new 1.5.0/docs/api/java/io/File.html">File(courseImportYearPath + "/" + Globals.INTRANET_SEMESTRE_S2).mkdirs();
                        new 1.5.0/docs/api/java/io/File.html">File(courseImportYearPath + "/" + Globals.INTRANET_SEMESTRE_A).mkdirs();
                        new 1.5.0/docs/api/java/io/File.html">File(courseImportYearPath + "/" + Globals.INTRANET_SEMESTRE_T1).mkdirs();
                        new 1.5.0/docs/api/java/io/File.html">File(courseImportYearPath + "/" + Globals.INTRANET_SEMESTRE_T2).mkdirs();
                        new 1.5.0/docs/api/java/io/File.html">File(courseImportYearPath + "/" + Globals.INTRANET_SEMESTRE_T3).mkdirs();
                        new 1.5.0/docs/api/java/io/File.html">File(courseImportYearPath + "/" + Globals.INTRANET_SEMESTRE_T4).mkdirs();
                        FilesUtils.copy(CreateCourseUnitDirPackageService.class.getResourceAsStream(Globals.INTRANET_SETUP_FILE),courseImportYearPath + "/" + Globals.INTRANET_SEMESTRE_S1 + "/" + Globals.INTRANET_SETUP_FILE_NAME);
                        FilesUtils.copy(CreateCourseUnitDirPackageService.class.getResourceAsStream(Globals.INTRANET_SETUP_FILE),courseImportYearPath + "/" + Globals.INTRANET_SEMESTRE_S2 + "/" + Globals.INTRANET_SETUP_FILE_NAME);
                        FilesUtils.copy(CreateCourseUnitDirPackageService.class.getResourceAsStream(Globals.INTRANET_SETUP_FILE),courseImportYearPath + "/" + Globals.INTRANET_SEMESTRE_A + "/" + Globals.INTRANET_SETUP_FILE_NAME);

                        FilesUtils.copy(CreateCourseUnitDirPackageService.class.getResourceAsStream(Globals.INTRANET_SETUP_FILE),courseImportYearPath + "/" + Globals.INTRANET_SEMESTRE_T1 + "/" + Globals.INTRANET_SETUP_FILE_NAME);
                        FilesUtils.copy(CreateCourseUnitDirPackageService.class.getResourceAsStream(Globals.INTRANET_SETUP_FILE),courseImportYearPath + "/" + Globals.INTRANET_SEMESTRE_T2 + "/" + Globals.INTRANET_SETUP_FILE_NAME);
                        FilesUtils.copy(CreateCourseUnitDirPackageService.class.getResourceAsStream(Globals.INTRANET_SETUP_FILE),courseImportYearPath + "/" + Globals.INTRANET_SEMESTRE_T3 + "/" + Globals.INTRANET_SETUP_FILE_NAME);
                        FilesUtils.copy(CreateCourseUnitDirPackageService.class.getResourceAsStream(Globals.INTRANET_SETUP_FILE),courseImportYearPath + "/" + Globals.INTRANET_SEMESTRE_T4 + "/" + Globals.INTRANET_SETUP_FILE_NAME);

                        1.5.0/docs/api/java/lang/String.html">String callPathS1 = (courseImportYearPathDos + "\\" + Globals.INTRANET_SEMESTRE_S1 + "\\").substring(tmpDir.length() + 1);
                        1.5.0/docs/api/java/lang/String.html">String callPathS2 = (courseImportYearPathDos + "\\" + Globals.INTRANET_SEMESTRE_S2 + "\\").substring(tmpDir.length() + 1);
                        1.5.0/docs/api/java/lang/String.html">String callPathA = (courseImportYearPathDos + "\\" + Globals.INTRANET_SEMESTRE_A + "\\").substring(tmpDir.length() + 1);
                        1.5.0/docs/api/java/lang/String.html">String callPathT1 = (courseImportYearPathDos + "\\" + Globals.INTRANET_SEMESTRE_T1 + "\\").substring(tmpDir.length() + 1);
                        1.5.0/docs/api/java/lang/String.html">String callPathT2 = (courseImportYearPathDos + "\\" + Globals.INTRANET_SEMESTRE_T2 + "\\").substring(tmpDir.length() + 1);
                        1.5.0/docs/api/java/lang/String.html">String callPathT3 = (courseImportYearPathDos + "\\" + Globals.INTRANET_SEMESTRE_T3 + "\\").substring(tmpDir.length() + 1);
                        1.5.0/docs/api/java/lang/String.html">String callPathT4 = (courseImportYearPathDos + "\\" + Globals.INTRANET_SEMESTRE_T4 + "\\").substring(tmpDir.length() + 1);
                        batRunner.write("cd " + back + callPathS1);
                        back = "..\\..\\..\\";
                        batRunner.write("\n");
                        batRunner.write(Globals.INTRANET_SETUP_FILE_NAME + " " + Globals.INTRANET_USERS_SETUP_FILE_NAME);
                        batRunner.write("\n");
                        batRunner.write("cd " + back + callPathS2);
                        batRunner.write("\n");
                        batRunner.write(Globals.INTRANET_SETUP_FILE_NAME + " " + Globals.INTRANET_USERS_SETUP_FILE_NAME);
                        batRunner.write("\n");
                        batRunner.write("cd " + back + callPathA);
                        batRunner.write("\n");
                        batRunner.write(Globals.INTRANET_SETUP_FILE_NAME + " " + Globals.INTRANET_USERS_SETUP_FILE_NAME);
                        batRunner.write("\n");

                        batRunner.write("cd " + back + callPathT1);
                        batRunner.write("\n");
                        batRunner.write(Globals.INTRANET_SETUP_FILE_NAME + " " + Globals.INTRANET_USERS_SETUP_FILE_NAME);
                        batRunner.write("\n");

                        batRunner.write("cd " + back + callPathT2);
                        batRunner.write("\n");
                        batRunner.write(Globals.INTRANET_SETUP_FILE_NAME + " " + Globals.INTRANET_USERS_SETUP_FILE_NAME);
                        batRunner.write("\n");

                        batRunner.write("cd " + back + callPathT3);
                        batRunner.write("\n");
                        batRunner.write(Globals.INTRANET_SETUP_FILE_NAME + " " + Globals.INTRANET_USERS_SETUP_FILE_NAME);
                        batRunner.write("\n");

                        batRunner.write("cd " + back + callPathT4);
                        batRunner.write("\n");
                        batRunner.write(Globals.INTRANET_SETUP_FILE_NAME + " " + Globals.INTRANET_USERS_SETUP_FILE_NAME);
                        batRunner.write("\n");
                        1.5.0/docs/api/java/io/FileWriter.html">FileWriter confWriterSemestre1 = new 1.5.0/docs/api/java/io/FileWriter.html">FileWriter(courseImportYearPath + "/" + Globals.INTRANET_SEMESTRE_S1 + "/" + Globals.INTRANET_USERS_SETUP_FILE_NAME);
                        1.5.0/docs/api/java/io/FileWriter.html">FileWriter confWriterSemestre2 = new 1.5.0/docs/api/java/io/FileWriter.html">FileWriter(courseImportYearPath + "/" + Globals.INTRANET_SEMESTRE_S2 + "/" + Globals.INTRANET_USERS_SETUP_FILE_NAME);
                        1.5.0/docs/api/java/io/FileWriter.html">FileWriter confWriterA = new 1.5.0/docs/api/java/io/FileWriter.html">FileWriter(courseImportYearPath + "/" + Globals.INTRANET_SEMESTRE_A + "/" + Globals.INTRANET_USERS_SETUP_FILE_NAME);

                        1.5.0/docs/api/java/io/FileWriter.html">FileWriter confWriterTrimestre1 = new 1.5.0/docs/api/java/io/FileWriter.html">FileWriter(courseImportYearPath + "/" + Globals.INTRANET_SEMESTRE_T1 + "/" + Globals.INTRANET_USERS_SETUP_FILE_NAME);
                        1.5.0/docs/api/java/io/FileWriter.html">FileWriter confWriterTrimestre2 = new 1.5.0/docs/api/java/io/FileWriter.html">FileWriter(courseImportYearPath + "/" + Globals.INTRANET_SEMESTRE_T2 + "/" + Globals.INTRANET_USERS_SETUP_FILE_NAME);
                        1.5.0/docs/api/java/io/FileWriter.html">FileWriter confWriterTrimestre3 = new 1.5.0/docs/api/java/io/FileWriter.html">FileWriter(courseImportYearPath + "/" + Globals.INTRANET_SEMESTRE_T3 + "/" + Globals.INTRANET_USERS_SETUP_FILE_NAME);
                        1.5.0/docs/api/java/io/FileWriter.html">FileWriter confWriterTrimestre4 = new 1.5.0/docs/api/java/io/FileWriter.html">FileWriter(courseImportYearPath + "/" + Globals.INTRANET_SEMESTRE_T4 + "/" + Globals.INTRANET_USERS_SETUP_FILE_NAME);


//                    List<CourseUnit> courseUnits = DaoFactory.getCourseUnitDaoImpl().loadByCourse(c.getId(),importYear);
                        for(CourseUnit courseUnit: courseUnits)
                        {
                            1.5.0/docs/api/java/io/FileWriter.html">FileWriter nowWriter;
                            1.5.0/docs/api/java/lang/String.html">String semestre;
                            if(courseUnit.getSemestre().equals("S1"))
                            {
                                nowWriter = confWriterSemestre1;
                                semestre = Globals.INTRANET_SEMESTRE_S1;
                            }
                            else if(courseUnit.getSemestre().equals("S2"))
                            {
                                nowWriter = confWriterSemestre2;
                                semestre = Globals.INTRANET_SEMESTRE_S2;
                            }
                            else if(courseUnit.getSemestre().equals("T1"))
                            {
                                nowWriter = confWriterTrimestre1;
                                semestre = Globals.INTRANET_SEMESTRE_T1;
                            }
                            else if(courseUnit.getSemestre().equals("T2"))
                            {
                                nowWriter = confWriterTrimestre2;
                                semestre = Globals.INTRANET_SEMESTRE_T2;
                            }
                            else if(courseUnit.getSemestre().equals("T3"))
                            {
                                nowWriter = confWriterTrimestre3;
                                semestre = Globals.INTRANET_SEMESTRE_T3;
                            }
                            else if(courseUnit.getSemestre().equals("T4"))
                            {
                                nowWriter = confWriterTrimestre4;
                                semestre = Globals.INTRANET_SEMESTRE_T4;
                            }

                            else
                            {
                                nowWriter = confWriterA;
                                semestre = Globals.INTRANET_SEMESTRE_A;
                            }

                            1.5.0/docs/api/java/lang/String.html">String normalizedName = StringsUtils.normalizeResourceName(courseUnit.getName());
                            1.5.0/docs/api/java/lang/String.html">String unitPath = courseImportYearPath + "/" + semestre + "/" + normalizedName;
                            courseUnit.setNormalizedName(normalizedName);
                            courseUnit.setPathIntranet(unitPath.substring(tmpDir.length()));
                            nowWriter.write(normalizedName);
                            1.5.0/docs/api/java/lang/System.html">System.out.println("GENERATING LINE: " + normalizedName);

                            if(courseUnit.getTeachers() != null)
                            {
                                1.5.0/docs/api/java/lang/String.html">String users = "";
                                int counter = 0;
                                for(Teacher t: courseUnit.getTeachers())
                                {
                                    if(t.getUsername() != null)
                                    {
                                        users += " " + t.getUsername();
                                        counter++;
                                    }
                                }
                                nowWriter.write(" " + counter + users);
                                nowWriter.write("\n");
                            }
                        }
                        confWriterSemestre1.flush();
                        confWriterSemestre2.flush();
                        confWriterTrimestre1.flush();
                        confWriterTrimestre2.flush();
                        confWriterTrimestre3.flush();
                        confWriterTrimestre4.flush();
                        confWriterA.flush();
                        confWriterSemestre1.close();
                        confWriterSemestre2.close();
                        confWriterTrimestre1.close();
                        confWriterTrimestre2.close();
                        confWriterTrimestre3.close();
                        confWriterTrimestre4.close();
                        confWriterA.close();
                    }
                    catch (1.5.0/docs/api/java/io/IOException.html">IOException e)
                    {
                        logger.error(e,e);
                    }
                }
            }
            batRunner.write("cd " + back);
            batRunner.close();
            FilesUtils.zipFolder(tmpDir,tmpDir + ".zip");
            FilesUtils.delete(tmpDir);
            return tmpName + ".zip";
        }
        catch (1.5.0/docs/api/java/io/IOException.html">IOException e)
        {
            logger.error(e,e);
        }
        return null;
    }

    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 teste = "Sistemas Térmicos, Hidráulicos e Pneumáticos";
        1.5.0/docs/api/java/lang/System.html">System.out.println(StringsUtils.normalizeResourceName(teste));
//        AbstractDao.getCurrentSession().beginTransaction();
//        new CreateCourseUnitDirPackageService().run(null,null);
//        AbstractDao.getCurrentSession().getTransaction().commit();
    }


}