Subversion Repositories bacoAlunos

Rev

Rev 1306 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
214 jmachado 1
package pt.estgp.estgweb.services.courseunits;
2
 
728 jmachado 3
import jomm.utils.BytesUtils;
4
import jomm.utils.FilesUtils;
5
import jomm.utils.StringsUtils;
6
import org.apache.log4j.Logger;
7
import pt.estgp.estgweb.Globals;
8
import pt.estgp.estgweb.domain.Course;
9
import pt.estgp.estgweb.domain.CourseUnit;
10
import pt.estgp.estgweb.domain.Teacher;
11
import pt.estgp.estgweb.domain.UserSession;
12
import pt.estgp.estgweb.domain.dao.DaoFactory;
214 jmachado 13
import pt.estgp.estgweb.services.expceptions.ServiceException;
14
import pt.estgp.estgweb.utils.DatesUtils;
728 jmachado 15
import pt.utl.ist.berserk.logic.serviceManager.IService;
214 jmachado 16
 
17
import java.io.File;
18
import java.io.FileWriter;
19
import java.io.IOException;
728 jmachado 20
import java.util.List;
214 jmachado 21
 
22
/*
23
 * @author Goncalo Luiz gedl [AT] rnl [DOT] ist [DOT] utl [DOT] pt
24
 *
25
 *
26
 * Created at 17/Out/2003 , 23:45:24
27
 *
28
 */
29
/**
30
 * @author Jorge Machado
31
 *
32
 *
33
 * Created at 17/Out/2003 , 23:45:24
34
 *
35
 */
36
public class CreateCourseUnitDirPackageService implements IService
37
{
38
    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);
39
 
215 jmachado 40
    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
214 jmachado 41
    {
215 jmachado 42
        if(importYear == null || importYear.trim().length() == 0)
995 jmachado 43
            importYear = DaoFactory.getConfigurationDaoImpl().getInterfaceImportYear();
799 jmachado 44
        1.5.0/docs/api/java/lang/String.html">String importYearIntranet = DatesUtils.getImportYearIntranet(importYear);
214 jmachado 45
        1.5.0/docs/api/java/lang/String.html">String tmpName =  BytesUtils.generateHexKey();
46
        1.5.0/docs/api/java/lang/String.html">String tmpDir = Globals.TMP_DIR + "/" + tmpName;
47
        new 1.5.0/docs/api/java/io/File.html">File(tmpDir).mkdirs();
799 jmachado 48
        //List<Course> courses = DaoFactory.getCourseDaoImpl().findAll(importYear);
49
        List<Course> courses = DaoFactory.getCourseDaoImpl().findAll();
214 jmachado 50
        try
51
        {
52
            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");
53
 
54
            1.5.0/docs/api/java/lang/String.html">String back = "";
55
            for(Course c: courses)
56
            {
799 jmachado 57
                //O curso é sempre o mesmo ID mas muda de Ano consoante o ano de importação
58
                List<CourseUnit> courseUnits = DaoFactory.getCourseUnitDaoImpl().loadByCourse(c.getId(),importYear);
59
                if(courseUnits == null || courseUnits.size() == 0)
214 jmachado 60
                {
799 jmachado 61
                    logger.info("Skipping course: " + c.getCode() + ":" + c.getName() + " zero units in " + importYear);
62
                }
63
                else{
64
                    1.5.0/docs/api/java/lang/String.html">String couseDirPath = tmpDir + "/" + StringsUtils.normalizeResourceName(c.getName()) + '.' + c.getCode();
65
                    1.5.0/docs/api/java/io/File.html">File courseDir = new 1.5.0/docs/api/java/io/File.html">File(couseDirPath);
66
                    courseDir.mkdirs();
67
                    try
68
                    {
214 jmachado 69
 
799 jmachado 70
                        1.5.0/docs/api/java/lang/String.html">String courseImportYearPathDos = couseDirPath + "\\" + importYearIntranet;
71
                        1.5.0/docs/api/java/lang/String.html">String courseImportYearPath = couseDirPath + "/" + importYearIntranet;
72
                        1.5.0/docs/api/java/io/File.html">File courseImportYearDir = new 1.5.0/docs/api/java/io/File.html">File(courseImportYearPath);
73
                        courseImportYearDir.mkdirs();
74
                        new 1.5.0/docs/api/java/io/File.html">File(courseImportYearPath + "/" + Globals.INTRANET_SEMESTRE_S1).mkdirs();
75
                        new 1.5.0/docs/api/java/io/File.html">File(courseImportYearPath + "/" + Globals.INTRANET_SEMESTRE_S2).mkdirs();
76
                        new 1.5.0/docs/api/java/io/File.html">File(courseImportYearPath + "/" + Globals.INTRANET_SEMESTRE_A).mkdirs();
1040 jmachado 77
                        new 1.5.0/docs/api/java/io/File.html">File(courseImportYearPath + "/" + Globals.INTRANET_SEMESTRE_T1).mkdirs();
78
                        new 1.5.0/docs/api/java/io/File.html">File(courseImportYearPath + "/" + Globals.INTRANET_SEMESTRE_T2).mkdirs();
79
                        new 1.5.0/docs/api/java/io/File.html">File(courseImportYearPath + "/" + Globals.INTRANET_SEMESTRE_T3).mkdirs();
80
                        new 1.5.0/docs/api/java/io/File.html">File(courseImportYearPath + "/" + Globals.INTRANET_SEMESTRE_T4).mkdirs();
799 jmachado 81
                        FilesUtils.copy(CreateCourseUnitDirPackageService.class.getResourceAsStream(Globals.INTRANET_SETUP_FILE),courseImportYearPath + "/" + Globals.INTRANET_SEMESTRE_S1 + "/" + Globals.INTRANET_SETUP_FILE_NAME);
82
                        FilesUtils.copy(CreateCourseUnitDirPackageService.class.getResourceAsStream(Globals.INTRANET_SETUP_FILE),courseImportYearPath + "/" + Globals.INTRANET_SEMESTRE_S2 + "/" + Globals.INTRANET_SETUP_FILE_NAME);
83
                        FilesUtils.copy(CreateCourseUnitDirPackageService.class.getResourceAsStream(Globals.INTRANET_SETUP_FILE),courseImportYearPath + "/" + Globals.INTRANET_SEMESTRE_A + "/" + Globals.INTRANET_SETUP_FILE_NAME);
214 jmachado 84
 
1040 jmachado 85
                        FilesUtils.copy(CreateCourseUnitDirPackageService.class.getResourceAsStream(Globals.INTRANET_SETUP_FILE),courseImportYearPath + "/" + Globals.INTRANET_SEMESTRE_T1 + "/" + Globals.INTRANET_SETUP_FILE_NAME);
86
                        FilesUtils.copy(CreateCourseUnitDirPackageService.class.getResourceAsStream(Globals.INTRANET_SETUP_FILE),courseImportYearPath + "/" + Globals.INTRANET_SEMESTRE_T2 + "/" + Globals.INTRANET_SETUP_FILE_NAME);
87
                        FilesUtils.copy(CreateCourseUnitDirPackageService.class.getResourceAsStream(Globals.INTRANET_SETUP_FILE),courseImportYearPath + "/" + Globals.INTRANET_SEMESTRE_T3 + "/" + Globals.INTRANET_SETUP_FILE_NAME);
88
                        FilesUtils.copy(CreateCourseUnitDirPackageService.class.getResourceAsStream(Globals.INTRANET_SETUP_FILE),courseImportYearPath + "/" + Globals.INTRANET_SEMESTRE_T4 + "/" + Globals.INTRANET_SETUP_FILE_NAME);
89
 
799 jmachado 90
                        1.5.0/docs/api/java/lang/String.html">String callPathS1 = (courseImportYearPathDos + "\\" + Globals.INTRANET_SEMESTRE_S1 + "\\").substring(tmpDir.length() + 1);
91
                        1.5.0/docs/api/java/lang/String.html">String callPathS2 = (courseImportYearPathDos + "\\" + Globals.INTRANET_SEMESTRE_S2 + "\\").substring(tmpDir.length() + 1);
92
                        1.5.0/docs/api/java/lang/String.html">String callPathA = (courseImportYearPathDos + "\\" + Globals.INTRANET_SEMESTRE_A + "\\").substring(tmpDir.length() + 1);
1040 jmachado 93
                        1.5.0/docs/api/java/lang/String.html">String callPathT1 = (courseImportYearPathDos + "\\" + Globals.INTRANET_SEMESTRE_T1 + "\\").substring(tmpDir.length() + 1);
94
                        1.5.0/docs/api/java/lang/String.html">String callPathT2 = (courseImportYearPathDos + "\\" + Globals.INTRANET_SEMESTRE_T2 + "\\").substring(tmpDir.length() + 1);
95
                        1.5.0/docs/api/java/lang/String.html">String callPathT3 = (courseImportYearPathDos + "\\" + Globals.INTRANET_SEMESTRE_T3 + "\\").substring(tmpDir.length() + 1);
96
                        1.5.0/docs/api/java/lang/String.html">String callPathT4 = (courseImportYearPathDos + "\\" + Globals.INTRANET_SEMESTRE_T4 + "\\").substring(tmpDir.length() + 1);
799 jmachado 97
                        batRunner.write("cd " + back + callPathS1);
98
                        back = "..\\..\\..\\";
99
                        batRunner.write("\n");
100
                        batRunner.write(Globals.INTRANET_SETUP_FILE_NAME + " " + Globals.INTRANET_USERS_SETUP_FILE_NAME);
101
                        batRunner.write("\n");
102
                        batRunner.write("cd " + back + callPathS2);
103
                        batRunner.write("\n");
104
                        batRunner.write(Globals.INTRANET_SETUP_FILE_NAME + " " + Globals.INTRANET_USERS_SETUP_FILE_NAME);
105
                        batRunner.write("\n");
106
                        batRunner.write("cd " + back + callPathA);
107
                        batRunner.write("\n");
108
                        batRunner.write(Globals.INTRANET_SETUP_FILE_NAME + " " + Globals.INTRANET_USERS_SETUP_FILE_NAME);
109
                        batRunner.write("\n");
1040 jmachado 110
 
111
                        batRunner.write("cd " + back + callPathT1);
112
                        batRunner.write("\n");
113
                        batRunner.write(Globals.INTRANET_SETUP_FILE_NAME + " " + Globals.INTRANET_USERS_SETUP_FILE_NAME);
114
                        batRunner.write("\n");
115
 
116
                        batRunner.write("cd " + back + callPathT2);
117
                        batRunner.write("\n");
118
                        batRunner.write(Globals.INTRANET_SETUP_FILE_NAME + " " + Globals.INTRANET_USERS_SETUP_FILE_NAME);
119
                        batRunner.write("\n");
120
 
121
                        batRunner.write("cd " + back + callPathT3);
122
                        batRunner.write("\n");
123
                        batRunner.write(Globals.INTRANET_SETUP_FILE_NAME + " " + Globals.INTRANET_USERS_SETUP_FILE_NAME);
124
                        batRunner.write("\n");
125
 
126
                        batRunner.write("cd " + back + callPathT4);
127
                        batRunner.write("\n");
128
                        batRunner.write(Globals.INTRANET_SETUP_FILE_NAME + " " + Globals.INTRANET_USERS_SETUP_FILE_NAME);
129
                        batRunner.write("\n");
799 jmachado 130
                        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);
131
                        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);
132
                        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);
214 jmachado 133
 
1040 jmachado 134
                        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);
135
                        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);
136
                        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);
137
                        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);
799 jmachado 138
 
1040 jmachado 139
 
799 jmachado 140
//                    List<CourseUnit> courseUnits = DaoFactory.getCourseUnitDaoImpl().loadByCourse(c.getId(),importYear);
141
                        for(CourseUnit courseUnit: courseUnits)
214 jmachado 142
                        {
799 jmachado 143
                            1.5.0/docs/api/java/io/FileWriter.html">FileWriter nowWriter;
144
                            1.5.0/docs/api/java/lang/String.html">String semestre;
145
                            if(courseUnit.getSemestre().equals("S1"))
146
                            {
147
                                nowWriter = confWriterSemestre1;
148
                                semestre = Globals.INTRANET_SEMESTRE_S1;
149
                            }
150
                            else if(courseUnit.getSemestre().equals("S2"))
151
                            {
152
                                nowWriter = confWriterSemestre2;
153
                                semestre = Globals.INTRANET_SEMESTRE_S2;
154
                            }
1040 jmachado 155
                            else if(courseUnit.getSemestre().equals("T1"))
156
                            {
157
                                nowWriter = confWriterTrimestre1;
158
                                semestre = Globals.INTRANET_SEMESTRE_T1;
159
                            }
160
                            else if(courseUnit.getSemestre().equals("T2"))
161
                            {
162
                                nowWriter = confWriterTrimestre2;
163
                                semestre = Globals.INTRANET_SEMESTRE_T2;
164
                            }
165
                            else if(courseUnit.getSemestre().equals("T3"))
166
                            {
167
                                nowWriter = confWriterTrimestre3;
168
                                semestre = Globals.INTRANET_SEMESTRE_T3;
169
                            }
170
                            else if(courseUnit.getSemestre().equals("T4"))
171
                            {
172
                                nowWriter = confWriterTrimestre4;
173
                                semestre = Globals.INTRANET_SEMESTRE_T4;
174
                            }
175
 
799 jmachado 176
                            else
177
                            {
178
                                nowWriter = confWriterA;
179
                                semestre = Globals.INTRANET_SEMESTRE_A;
180
                            }
214 jmachado 181
 
799 jmachado 182
                            1.5.0/docs/api/java/lang/String.html">String normalizedName = StringsUtils.normalizeResourceName(courseUnit.getName());
183
                            1.5.0/docs/api/java/lang/String.html">String unitPath = courseImportYearPath + "/" + semestre + "/" + normalizedName;
184
                            courseUnit.setNormalizedName(normalizedName);
185
                            courseUnit.setPathIntranet(unitPath.substring(tmpDir.length()));
186
                            nowWriter.write(normalizedName);
187
                            1.5.0/docs/api/java/lang/System.html">System.out.println("GENERATING LINE: " + normalizedName);
214 jmachado 188
 
799 jmachado 189
                            if(courseUnit.getTeachers() != null)
214 jmachado 190
                            {
799 jmachado 191
                                1.5.0/docs/api/java/lang/String.html">String users = "";
192
                                int counter = 0;
193
                                for(Teacher t: courseUnit.getTeachers())
214 jmachado 194
                                {
799 jmachado 195
                                    if(t.getUsername() != null)
196
                                    {
197
                                        users += " " + t.getUsername();
198
                                        counter++;
199
                                    }
214 jmachado 200
                                }
799 jmachado 201
                                nowWriter.write(" " + counter + users);
202
                                nowWriter.write("\n");
214 jmachado 203
                            }
204
                        }
799 jmachado 205
                        confWriterSemestre1.flush();
206
                        confWriterSemestre2.flush();
1040 jmachado 207
                        confWriterTrimestre1.flush();
208
                        confWriterTrimestre2.flush();
209
                        confWriterTrimestre3.flush();
210
                        confWriterTrimestre4.flush();
799 jmachado 211
                        confWriterA.flush();
212
                        confWriterSemestre1.close();
213
                        confWriterSemestre2.close();
1040 jmachado 214
                        confWriterTrimestre1.close();
215
                        confWriterTrimestre2.close();
216
                        confWriterTrimestre3.close();
217
                        confWriterTrimestre4.close();
799 jmachado 218
                        confWriterA.close();
214 jmachado 219
                    }
799 jmachado 220
                    catch (1.5.0/docs/api/java/io/IOException.html">IOException e)
221
                    {
222
                        logger.error(e,e);
223
                    }
214 jmachado 224
                }
225
            }
226
            batRunner.write("cd " + back);
227
            batRunner.close();
228
            FilesUtils.zipFolder(tmpDir,tmpDir + ".zip");
229
            FilesUtils.delete(tmpDir);
230
            return tmpName + ".zip";
231
        }
232
        catch (1.5.0/docs/api/java/io/IOException.html">IOException e)
233
        {
234
            logger.error(e,e);
235
        }
236
        return null;
237
    }
238
 
239
    public static void main(1.5.0/docs/api/java/lang/String.html">String[] args) throws ServiceException
240
    {
799 jmachado 241
        1.5.0/docs/api/java/lang/String.html">String teste = "Sistemas Térmicos, Hidráulicos e Pneumáticos";
242
        1.5.0/docs/api/java/lang/System.html">System.out.println(StringsUtils.normalizeResourceName(teste));
271 jmachado 243
//        AbstractDao.getCurrentSession().beginTransaction();
244
//        new CreateCourseUnitDirPackageService().run(null,null);
245
//        AbstractDao.getCurrentSession().getTransaction().commit();
214 jmachado 246
    }
247
 
248
 
249
}