Rev 1814 | Rev 1823 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
995 | jmachado | 1 | package pt.estgp.estgweb.services.sigesimports; |
2 | |||
3 | import jomm.dao.impl.AbstractDao; |
||
4 | import jomm.utils.FilesUtils; |
||
5 | import jomm.utils.PdfUtils; |
||
6 | import org.apache.commons.net.ftp.FTP; |
||
7 | import org.apache.commons.net.ftp.FTPClient; |
||
8 | import org.apache.fop.apps.FOPException; |
||
9 | import org.apache.log4j.Logger; |
||
10 | import org.dom4j.Document; |
||
11 | import org.json.JSONArray; |
||
12 | import org.json.JSONException; |
||
13 | import org.json.JSONObject; |
||
1814 | jmachado | 14 | import pt.estgp.estgweb.utils.Globals; |
995 | jmachado | 15 | import pt.estgp.estgweb.domain.*; |
16 | import pt.estgp.estgweb.domain.dao.DaoFactory; |
||
17 | import pt.estgp.estgweb.domain.enums.LangEnum; |
||
1353 | jmachado | 18 | import pt.estgp.estgweb.domain.views.CourseUnitSummaryView; |
19 | import pt.estgp.estgweb.domain.views.CourseUnitView; |
||
995 | jmachado | 20 | import pt.estgp.estgweb.services.courseunits.CreateCourseUnitXML; |
21 | import pt.estgp.estgweb.services.email.EMAILJob; |
||
22 | import pt.estgp.estgweb.services.email.SendEmailService; |
||
23 | import pt.estgp.estgweb.services.expceptions.ServiceException; |
||
24 | import pt.estgp.estgweb.services.jobs.JobScheduleService; |
||
25 | import pt.estgp.estgweb.services.jobs.ServiceJob; |
||
26 | import pt.estgp.estgweb.services.logresults.ILogMessages; |
||
27 | import pt.estgp.estgweb.services.logresults.LogMessageTypeEnum; |
||
28 | import pt.estgp.estgweb.services.logresults.impl.DefaultLogMessage; |
||
29 | import pt.estgp.estgweb.services.logresults.impl.DefaultLogMessages; |
||
1235 | jmachado | 30 | import pt.estgp.estgweb.services.sigesimports.oracle.dao.AulaDao; |
1267 | jmachado | 31 | import pt.estgp.estgweb.services.sigesimports.oracle.dao.connection.ConnectionManager; |
1312 | jmachado | 32 | import pt.estgp.estgweb.utils.ConfigProperties; |
995 | jmachado | 33 | import pt.estgp.estgweb.utils.Dom4jUtil; |
1822 | jmachado | 34 | import pt.estgp.estgweb.utils.HtmlStringUtils; |
995 | jmachado | 35 | import pt.estgp.estgweb.web.FtpServer; |
36 | import pt.utl.ist.berserk.logic.serviceManager.IService; |
||
37 | |||
38 | import javax.xml.transform.TransformerException; |
||
39 | import javax.xml.transform.dom.DOMSource; |
||
40 | import java.io.File; |
||
41 | import java.io.FileInputStream; |
||
42 | import java.io.FileOutputStream; |
||
43 | import java.io.IOException; |
||
1267 | jmachado | 44 | import java.sql.Connection; |
1353 | jmachado | 45 | import java.util.*; |
995 | jmachado | 46 | |
47 | /** |
||
48 | * @author Jorge Machado |
||
49 | * @date 11/May/2008 |
||
50 | * @time 12:51:32 |
||
51 | * @see pt.estgp.estgweb |
||
1353 | jmachado | 52 | * |
53 | * |
||
54 | * @NOTAS IMPORTANTES DESTE SERVIÇO |
||
55 | * |
||
56 | * @DEPENDS TODO DEPENDENCIAS FORTES |
||
57 | * Este serviço gera toda a informação acerca de sumários e sumários em FALTA |
||
58 | * Existe uma forte depêndencia da CourseUniView que recebe daqui a lógica de decisão daquilo que são ou não sumários |
||
59 | * atrasados |
||
995 | jmachado | 60 | */ |
61 | public class ImportSumariesJson extends ServiceJob implements IService |
||
62 | { |
||
63 | |||
64 | 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); |
||
65 | |||
1030 | jmachado | 66 | private static final int MAX_COMMIT = 10; |
995 | jmachado | 67 | |
68 | |||
1312 | jmachado | 69 | public ILogMessages run(1.5.0/docs/api/java/lang/String.html">String year,int institutionCode,1.5.0/docs/api/java/lang/String.html">String semestre) throws ServiceException |
995 | jmachado | 70 | { |
1312 | jmachado | 71 | return run(year,institutionCode,semestre,true,true); |
995 | jmachado | 72 | } |
73 | |||
1312 | jmachado | 74 | public ILogMessages run(1.5.0/docs/api/java/lang/String.html">String year,int institutionCode,1.5.0/docs/api/java/lang/String.html">String semestre,boolean sendIONLINE,boolean sendEMAIL) throws ServiceException |
995 | jmachado | 75 | { |
76 | 1.5.0/docs/api/java/lang/String.html">String msgS = "STARTING SUMARIES IMPORT SERVICE FOR year: " + year + " semestre: " + semestre + " sendIONLINE: " + sendIONLINE + " sendEMAIL: " + sendEMAIL; |
||
77 | serviceLogInfo(msgS); |
||
78 | logger.info(msgS); |
||
79 | |||
80 | DefaultLogMessages logMessages = new DefaultLogMessages(); |
||
1312 | jmachado | 81 | logMessages.addMessage(new DefaultLogMessage("import.summaries", LogMessageTypeEnum.INFO, "instituicao " + institutionCode)); |
995 | jmachado | 82 | serviceLogInfo(logMessages.getLastMessage()); |
83 | |||
84 | |||
85 | int countUnits = 0; |
||
86 | int countUnitsOk = 0; |
||
87 | int countUnitsZeroSummaries = 0; |
||
1081 | jmachado | 88 | int countUnitsZeroValidSummaries = 0; |
995 | jmachado | 89 | |
90 | List<String> unitsZeroSummaries = new ArrayList<String>(); |
||
91 | List<String> unitsFailed = new ArrayList<String>(); |
||
92 | try |
||
93 | { |
||
94 | |||
1298 | jmachado | 95 | /* SiGesWEB service; |
1017 | jmachado | 96 | String WSDL = DaoFactory.getConfigurationDaoImpl().getSigesWebServicesWsdl(); |
995 | jmachado | 97 | try |
98 | { |
||
1017 | jmachado | 99 | serviceLogInfo("STARTING WEB SERVICE AT " + WSDL); |
100 | service = new SiGesWEB(new URL(WSDL), new QName(Globals.SIGES_WEBSERVICE_TARGET_NAMESPACE, "SiGesWEB")); |
||
995 | jmachado | 101 | } |
102 | catch (Throwable e) |
||
103 | { |
||
104 | logMessages.addMessage(new DefaultLogMessage("import.error",e.toString(),"erro na configuracao do WEB Service", LogMessageTypeEnum.INFO)); |
||
105 | serviceLogInfo(logMessages.getLastMessage()); |
||
106 | logger.fatal(e, e); |
||
107 | serviceLogError(e.toString(), e); |
||
108 | return logMessages; |
||
1298 | jmachado | 109 | }*/ |
995 | jmachado | 110 | |
111 | int i = 0; |
||
112 | //List<Long> cUs = DaoFactory.getCourseUnitDaoImpl().loadIds(year,DatesUtils.getSemestre()); |
||
113 | List<Long> cUs = DaoFactory.getCourseUnitDaoImpl().loadIds(year,semestre); |
||
114 | 1.5.0/docs/api/java/lang/String.html">String nowCode = ""; |
||
1267 | jmachado | 115 | 1.5.0/docs/api/java/sql/Connection.html">Connection conn = ConnectionManager.openConnection(); |
116 | boolean connError = false; |
||
995 | jmachado | 117 | for(1.5.0/docs/api/java/lang/Long.html">Long cId: cUs) |
118 | { |
||
1559 | jmachado | 119 | if(cId == 1096) |
120 | 1.5.0/docs/api/java/lang/System.html">System.currentTimeMillis(); |
||
1267 | jmachado | 121 | if(connError) |
122 | break; |
||
1144 | jmachado | 123 | nowCode = "" + cId; |
995 | jmachado | 124 | countUnits++; |
125 | try |
||
126 | { |
||
127 | if (i++ > MAX_COMMIT) |
||
128 | { |
||
129 | i = 0; |
||
996 | jmachado | 130 | setProgress((int) (((float)countUnits)/((float)cUs.size())*100.0f)); |
1312 | jmachado | 131 | conn.close(); |
1076 | jmachado | 132 | commitPartially(); |
1312 | jmachado | 133 | conn = ConnectionManager.openConnection(); |
995 | jmachado | 134 | } |
135 | CourseUnit c = DaoFactory.getCourseUnitDaoImpl().load(cId); |
||
1558 | jmachado | 136 | |
1559 | jmachado | 137 | boolean separateTurmas = ImportCourseService.loadCourseYearTurmasPolicy(c.getCourse().getCode(), year, logMessages, this); |
1558 | jmachado | 138 | //c.getCourse().getCourseYears() |
139 | |||
995 | jmachado | 140 | nowCode = c.getCode(); |
141 | logMessages.addMessage(new DefaultLogMessage("import.info","Importing Summaries to CourseUnit: " + c.getCode() + ": course: " + c.getCourse().getCode(),"", LogMessageTypeEnum.INFO)); |
||
142 | logger.info("Importing Summaries to CourseUnit: " + c.getName() + " (" + c.getCode() + ") course " + c.getCourse().getName() + " (" + c.getCourse().getCode() + ")"); |
||
143 | serviceLogInfo(logMessages.getLastMessage()); |
||
144 | |||
145 | int tries = 3; |
||
1235 | jmachado | 146 | //ArrayOfAula arrayOfAulas = null; |
147 | ArrayList<pt.estgp.estgweb.services.sigesimports.oracle.domain.Aula> arrayOfAulas = null; |
||
995 | jmachado | 148 | while(tries > 0) |
149 | { |
||
150 | //comentario de teste |
||
151 | try |
||
152 | { |
||
1267 | jmachado | 153 | |
1814 | jmachado | 154 | //if(c.getSum) |
1235 | jmachado | 155 | //arrayOfAulas = service.getSiGesWEBSoap().getAulas(new BigDecimal(DaoFactory.getConfigurationDaoImpl().getSigesInstitutionCode()), year,c.getCode(),semestre,c.getCourse().getCode()); |
1814 | jmachado | 156 | 1.5.0/docs/api/java/lang/String.html">String semestreEmQueForamCriadosOsSumarios = semestre; |
157 | if(c.getSumariesSemestre() != null && c.getSumariesSemestre().length() > 0) |
||
158 | { |
||
159 | semestreEmQueForamCriadosOsSumarios = c.getSumariesSemestre(); |
||
160 | 1.5.0/docs/api/java/lang/String.html">String msg = "courseUnit:" + cId + " ( " + c.getName() + " ) com sumarios num semestre diferente, atribuido pela administracao do baco: " + semestreEmQueForamCriadosOsSumarios; |
||
161 | logger.info(msg); |
||
162 | serviceLogInfo(msg); |
||
163 | } |
||
164 | arrayOfAulas = AulaDao.getInstance().loadAulas(institutionCode,year,c.getCode(),semestreEmQueForamCriadosOsSumarios,c.getCourse().getCode(),conn); |
||
1267 | jmachado | 165 | |
995 | jmachado | 166 | break; |
167 | } |
||
168 | catch(1.5.0/docs/api/java/lang/Throwable.html">Throwable t) |
||
169 | { |
||
170 | if(tries-- == 0) |
||
171 | { |
||
172 | logger.error("Giving up of courseUnit:" + cId + " ( " + c.getName() + " ) ",t); |
||
173 | serviceLogError("Giving up of courseUnit:" + cId + " ( " + c.getName() + " ) ",t); |
||
1267 | jmachado | 174 | connError = true; |
995 | jmachado | 175 | break; |
176 | } |
||
177 | |||
1144 | jmachado | 178 | |
1267 | jmachado | 179 | logger.error("WebService Fail, trying " + tries + " times sleeping a while",t); |
1144 | jmachado | 180 | serviceLogError("WebService Fail, trying " + tries + " times sleeping a while " + t.toString()); |
181 | serviceLogError("WebService Fail, trying " + tries + " times sleeping a while " + t.getCause()); |
||
995 | jmachado | 182 | 1.5.0/docs/api/java/lang/Thread.html">Thread.sleep(5000); |
183 | } |
||
184 | } |
||
185 | |||
1235 | jmachado | 186 | if(arrayOfAulas != null && arrayOfAulas.size() > 0) |
995 | jmachado | 187 | { |
188 | List<CourseUnitSummary> courseUnitSummaries = new ArrayList<CourseUnitSummary>(); |
||
189 | |||
190 | //HashMap<String, CourseUnitSummary> map = new HashMap<String, CourseUnitSummary>(); |
||
191 | /*if(c.getSummaries() != null && c.getSummaries().size() > 0) |
||
192 | for(CourseUnitSummary s: c.getSummaries()) |
||
193 | map.put(s.getCodigoAula() + ":" + s.getCodigoSumario(),s); |
||
194 | else |
||
195 | c.setSummaries(new HashSet<CourseUnitSummary>());*/ |
||
196 | JSONObject summariesJson = new JSONObject(); |
||
197 | JSONArray summaries = new JSONArray(); |
||
198 | summariesJson.put("summary",summaries); |
||
1081 | jmachado | 199 | int summariesValid = 0; |
1235 | jmachado | 200 | for(pt.estgp.estgweb.services.sigesimports.oracle.domain.Aula aula: arrayOfAulas) |
995 | jmachado | 201 | { |
202 | |||
1558 | jmachado | 203 | |
204 | //Quando isto era feito as cadeiras não eram separadas em turmas agora como já são |
||
205 | //vai-se ignorar sumarios de outras turmas |
||
206 | if(separateTurmas) |
||
207 | { |
||
208 | if(c.getCdTurma() != null && aula.getAula_codigoTurma()!=null |
||
209 | && c.getCdTurma().trim().length() > 0 |
||
210 | && aula.getAula_codigoTurma().trim().length() > 0 |
||
211 | && !aula.getAula_codigoTurma().equals(c.getCdTurma())) |
||
212 | { |
||
213 | //Código de Turma diferente |
||
214 | 1.5.0/docs/api/java/lang/System.html">System.out.println("Ignoring summary code: " + aula.getAula_codigoTurma() + " for unit " + c.getName() + "/" + c.getCode() + " T:" + c.getCdTurma()); |
||
215 | continue; |
||
216 | } |
||
217 | } |
||
1235 | jmachado | 218 | //todo cdTurma Rollback aqui não retirar o sumário |
219 | //Mas pensar em separar os sumários na página, podemos |
||
220 | //separar tendo em conta quem entra, se for aluno só vê os |
||
221 | //seus sumários, se for prof vê os sumários das suas turmas |
||
222 | |||
1248 | jmachado | 223 | /* |
1235 | jmachado | 224 | if(c.getCdTurma() != null && aula.getAula_codigoTurma()!=null |
1133 | jmachado | 225 | && c.getCdTurma().trim().length() > 0 |
1235 | jmachado | 226 | && aula.getAula_codigoTurma().trim().length() > 0 |
227 | && !aula.getAula_codigoTurma().equals(c.getCdTurma())) |
||
1133 | jmachado | 228 | { |
229 | //Código de Turma diferente |
||
1235 | jmachado | 230 | System.out.println("Ignoring summary code: " + aula.getAula_codigoTurma() + " for unit " + c.getName() + "/" + c.getCode() + " T:" + c.getCdTurma()); |
1133 | jmachado | 231 | continue; |
1248 | jmachado | 232 | }*/ |
1133 | jmachado | 233 | |
995 | jmachado | 234 | //CourseUnitSummary s = map.get(aula.getSumNumeroAula() + ":" + aula.getCodigoSumario()); |
235 | //if(s == null) |
||
236 | //{ |
||
237 | CourseUnitSummaryImpl s = DomainObjectFactory.createCourseUnitSummaryImpl(); |
||
238 | s.setCourseUnit(c); |
||
239 | //c.getSummaries().add(s); |
||
240 | //DaoFactory.getCourseUnitSummaryDaoImpl().save(s); |
||
241 | //summaries.add(); |
||
242 | //} |
||
1235 | jmachado | 243 | s.setCdTipoOcupacao(aula.getTipoOcupacao().intValue()); |
244 | s.setNrAula(aula.getNumeroAula().intValue()); |
||
995 | jmachado | 245 | s.setCodigoSumario(""+aula.getCodigoSumario().intValue()); |
1235 | jmachado | 246 | s.setCodigoAula("" + aula.getSum_numeroAula().intValue()); |
247 | s.setNumeroAula(aula.getSum_numeroAula().intValue()); |
||
248 | s.setDataAula(aula.getData()); |
||
249 | s.setTipoAula(aula.getSum_tipoAula()); |
||
250 | s.setCodigoSalaAula("" + aula.getAula_codigoSalaAula().intValue()); |
||
251 | s.setAlunosPresentes(aula.getSum_numeroPresencas().intValue()); |
||
252 | s.setTitulo(aula.getSum_titulo()); |
||
253 | s.setCodigoDocente(aula.getAula_codigoDocente()+ ""); |
||
1558 | jmachado | 254 | s.setCdResponsavel(aula.getCodigoResponsavel()+ ""); |
1822 | jmachado | 255 | if(aula.getSum_descricao()!= null) |
256 | s.setDescricao(HtmlStringUtils.unescapeHtml3(aula.getSum_descricao())); |
||
1235 | jmachado | 257 | s.setcEstado(""+aula.getSum_cdEstado().intValue()); |
995 | jmachado | 258 | s.setNumeroHoras("" + aula.getNumeroCelulas().intValue()); |
1558 | jmachado | 259 | s.setCdTurma(aula.getAula_codigoTurma()); |
1780 | jmachado | 260 | summaries.put(s.toJsonObject()); |
995 | jmachado | 261 | courseUnitSummaries.add(s); |
1081 | jmachado | 262 | if(s.getNumeroAula() > 0) |
263 | summariesValid++; |
||
995 | jmachado | 264 | } |
265 | if(courseUnitSummaries.size() == 0) |
||
266 | { |
||
267 | countUnitsZeroSummaries++; |
||
268 | unitsZeroSummaries.add(c.getCode()); |
||
1081 | jmachado | 269 | logger.info("ZERO SUMARIES unit:" + c.getCode() + " course: " + c.getCourseCode()); |
270 | serviceLogInfo("ZERO SUMARIES unit:" + c.getCode() + " course: " + c.getCourseCode()); |
||
995 | jmachado | 271 | } |
1081 | jmachado | 272 | else if(summariesValid == 0) |
273 | { |
||
1144 | jmachado | 274 | //NAO METE OK NEM ZERO |
1081 | jmachado | 275 | countUnitsZeroValidSummaries++; |
276 | unitsZeroSummaries.add(c.getCode()); |
||
277 | logger.info("ZERO VALID SUMARIES unit:" + c.getCode() + " course: " + c.getCourseCode()); |
||
278 | serviceLogInfo("ZERO VALID SUMARIES unit:" + c.getCode() + " course: " + c.getCourseCode()); |
||
279 | } |
||
995 | jmachado | 280 | else |
281 | { |
||
282 | countUnitsOk++; |
||
283 | } |
||
284 | c.setSummariesJson(summariesJson.toString()); |
||
285 | |||
286 | if(sendIONLINE) |
||
287 | { |
||
288 | 1.5.0/docs/api/java/lang/String.html">String path = generatePdf(c); |
||
289 | sendPdfFtpIonline(c,new ArrayList<String>(),null,path,null); |
||
1268 | jmachado | 290 | try{ |
291 | FilesUtils.delete(path); |
||
292 | }catch(1.5.0/docs/api/java/lang/Throwable.html">Throwable e) |
||
293 | { |
||
294 | logger.error("trying to delete " + path,e); |
||
295 | } |
||
995 | jmachado | 296 | } |
297 | if(sendEMAIL) |
||
298 | { |
||
299 | sendEmail(null, courseUnitSummaries,true); |
||
300 | } |
||
301 | } |
||
302 | else |
||
303 | { |
||
1144 | jmachado | 304 | //NAO METE OK MAS METE ZERO |
995 | jmachado | 305 | countUnitsZeroSummaries++; |
306 | unitsZeroSummaries.add(c.getCode()); |
||
307 | logger.error("Summaries come null in unit:" + c.getCode()); |
||
308 | serviceLogError("Summaries come null in unit:" + c.getCode()); |
||
309 | } |
||
310 | } |
||
311 | catch(1.5.0/docs/api/java/lang/Throwable.html">Throwable e) |
||
312 | { |
||
1144 | jmachado | 313 | //NAO METE OK |
995 | jmachado | 314 | unitsFailed.add(nowCode); |
315 | logger.error(e,e); |
||
316 | serviceLogError(e.toString(),e); |
||
317 | } |
||
318 | } |
||
1267 | jmachado | 319 | conn.close(); |
995 | jmachado | 320 | |
321 | setProgress(100); |
||
322 | serviceLogError("Finish commiting"); |
||
323 | |||
324 | } |
||
325 | catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e) |
||
326 | { |
||
327 | logger.error(e, e); |
||
328 | logMessages.addMessage(new DefaultLogMessage("import.error", e.toString(), "see log for details", LogMessageTypeEnum.ERROR)); |
||
329 | serviceLogError(logMessages.getLastMessage(),e); |
||
330 | throw new ServiceException(e.toString(), e); |
||
331 | } |
||
332 | |||
333 | logMessages.addMessage(new DefaultLogMessage("import.summaries.terminating", LogMessageTypeEnum.INFO)); |
||
334 | logger.info("terminating summaries import"); |
||
335 | serviceLogInfo(logMessages.getLastMessage()); |
||
336 | |||
1144 | jmachado | 337 | 1.5.0/docs/api/java/lang/StringBuilder.html">StringBuilder failedUnitsExc = new 1.5.0/docs/api/java/lang/StringBuilder.html">StringBuilder(); |
338 | for(1.5.0/docs/api/java/lang/String.html">String cId: unitsFailed) |
||
339 | failedUnitsExc.append(cId).append(";"); |
||
1145 | jmachado | 340 | |
995 | jmachado | 341 | serviceLogInfo("#######################################"); |
342 | serviceLogInfo("#######################################"); |
||
343 | serviceLogInfo("UNITS PROCESSED: " + countUnits); |
||
1144 | jmachado | 344 | serviceLogInfo("UNITS FAILED: " + (countUnits - countUnitsZeroSummaries - countUnitsOk - countUnitsZeroValidSummaries)); |
345 | serviceLogInfo("UNITS FAILED EXCEPTION PARSING XML WEB SERVICE: " + unitsFailed.size()); |
||
995 | jmachado | 346 | serviceLogInfo("UNITS IMPORT OK: " + countUnitsOk); |
347 | serviceLogInfo("UNITS ZERO SUMARIES: " + countUnitsZeroSummaries); |
||
1081 | jmachado | 348 | serviceLogInfo("UNITS ZERO VALID SUMARIES: " + countUnitsZeroValidSummaries); |
995 | jmachado | 349 | serviceLogInfo("#######################################"); |
1144 | jmachado | 350 | serviceLogWarn("####FAILED Exception (Check exceptions in log): Units Baco Id Code : " + failedUnitsExc.toString()); |
1025 | jmachado | 351 | |
352 | logger.info("#######################################"); |
||
353 | logger.info("#######################################"); |
||
354 | logger.info("UNITS PROCESSED: " + countUnits); |
||
1144 | jmachado | 355 | logger.info("UNITS FAILED: " + (countUnits - countUnitsZeroSummaries - countUnitsOk - countUnitsZeroValidSummaries)); |
356 | logger.info("UNITS FAILED EXCEPTION PARSING XML WEB SERVICE: " + unitsFailed.size()); |
||
1025 | jmachado | 357 | logger.info("UNITS IMPORT OK: " + countUnitsOk); |
358 | logger.info("UNITS ZERO SUMARIES: " + countUnitsZeroSummaries); |
||
1081 | jmachado | 359 | logger.info("UNITS ZERO VALID SUMARIES: " + countUnitsZeroValidSummaries); |
1025 | jmachado | 360 | logger.info("#######################################"); |
1144 | jmachado | 361 | logger.warn("####FAILED Exception (Check exceptions in log): Units Baco Id Code : " + failedUnitsExc.toString()); |
362 | |||
363 | |||
995 | jmachado | 364 | return logMessages; |
365 | } |
||
366 | |||
367 | |||
368 | public static SendEmailService sendEmailService = new SendEmailService(); |
||
369 | |||
370 | private void sendEmail(UserSession userSession,List<CourseUnitSummary> summaries,boolean sendEmail) |
||
371 | { |
||
372 | if(Globals.EMAIL_SUMMARIES && summaries.size() > 0 && sendEmail) |
||
373 | { |
||
374 | // for(CourseUnitSummary summary: summaries) |
||
375 | // { |
||
376 | |||
377 | List<String> emails = DaoFactory.getCourseUnitSummaryDaoImpl().getStudentsEmails(summaries.get(0).getId()); |
||
378 | if(emails == null) |
||
379 | logger.warn("No students emails in unit: code:" + summaries.get(0).getCourseUnit().getCode() + " " + summaries.get(0).getCourseUnit().getName()); |
||
380 | EMAILJob emailJob = new EMAILJob(sendEmailService.getSummariesEmail(emails, summaries, summaries.get(0).getCourseUnit(), LangEnum.PORTUGUESE)); |
||
381 | JobScheduleService.getInstance().scheduleNow(emailJob,"New Summary EMAIL from: " + summaries.get(0).getCourseUnit().getName() + " aula: " +summaries.get(summaries.size()-1).getNumeroAula() ,userSession); |
||
382 | // } |
||
383 | } |
||
384 | } |
||
385 | |||
386 | |||
387 | |||
388 | |||
389 | |||
390 | |||
391 | 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 { |
||
392 | 5+0%2Fdocs%2Fapi+Document">Document dom = new CreateCourseUnitXML().run(c,null); |
||
393 | org.w3c.dom.5+0%2Fdocs%2Fapi+Document">Document dd = Dom4jUtil.toW3c(dom); |
||
394 | 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"; |
||
395 | 1.5.0/docs/api/java/io/FileOutputStream.html">FileOutputStream out = new 1.5.0/docs/api/java/io/FileOutputStream.html">FileOutputStream(path); |
||
396 | Map<String,Object> params = new HashMap<String,Object>(); |
||
397 | params.put("site", Globals.SITE_URL); |
||
398 | 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); |
||
399 | out.flush(); |
||
400 | out.close(); |
||
401 | return path; |
||
402 | } |
||
403 | |||
1006 | jmachado | 404 | 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 { |
995 | jmachado | 405 | |
1312 | jmachado | 406 | if(ConfigProperties.getBooleanProperty("test.control.var")) |
407 | { |
||
408 | 1.5.0/docs/api/java/lang/String.html">String warn = "System in TEST Environement - will NOT send file to ionline test.control.var.DontSendIonlineFiles = true"; |
||
409 | logger.warn(warn); |
||
410 | return; |
||
411 | } |
||
995 | jmachado | 412 | FtpServer server = null; |
413 | if(client == null) |
||
414 | { |
||
415 | server = FtpServer.getNewServer(Globals.FTP_IONLINE_URL,Globals.FTP_IONLINE_USER,Globals.FTP_IONLINE_PASS); |
||
416 | client = server.getClient(); |
||
1005 | jmachado | 417 | if(client == null) |
418 | { |
||
419 | logger.warn("###################"); |
||
1009 | jmachado | 420 | logger.warn("################### > CANT CONNECT FTP - could be FTPTimeout: " + Globals.FTP_TIMEOUT_SECONDS); |
421 | errors.add("################### > CANT CONNECT FTP to send summaries " + ((CourseUnitImpl)cu).getSummariosPathIntranet()); |
||
1005 | jmachado | 422 | return; |
423 | } |
||
424 | |||
995 | jmachado | 425 | } |
426 | client.setFileType(FTP.BINARY_FILE_TYPE); |
||
427 | |||
428 | |||
429 | 1.5.0/docs/api/java/lang/String.html">String pastaFichaCurricular = ((CourseUnitImpl)cu).getSummariosPathIntranet(); |
||
430 | if(!client.changeWorkingDirectory(pastaFichaCurricular)) |
||
431 | { |
||
432 | logger.error("FTP CANT CHANGE TO PATH: " + pastaFichaCurricular); |
||
433 | } |
||
434 | else |
||
435 | { |
||
1009 | jmachado | 436 | if(!client.storeFile(((CourseUnitImpl)cu).getSummariosFileNameIntranet(),new 1.5.0/docs/api/java/io/FileInputStream.html">FileInputStream(pdfPath))) |
437 | { |
||
1010 | jmachado | 438 | 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; |
439 | logger.error(msgS); |
||
440 | serviceLogError(msgS); |
||
1009 | jmachado | 441 | } |
995 | jmachado | 442 | if(server != null) |
443 | { |
||
444 | client.quit(); |
||
445 | client.disconnect(); |
||
446 | } |
||
447 | } |
||
448 | // |
||
449 | } |
||
450 | /** |
||
451 | * Testar por aqui poi requer Super Role e assim e' autmatico |
||
452 | * |
||
453 | * |
||
454 | * @param args of main |
||
455 | * @throws pt.estgp.estgweb.services.expceptions.ServiceException on error |
||
456 | */ |
||
457 | public static void main(1.5.0/docs/api/java/lang/String.html">String[] args) throws ServiceException, JSONException { |
||
458 | |||
997 | jmachado | 459 | 1.5.0/docs/api/java/lang/System.html">System.out.println("Syntax:"); |
460 | 1.5.0/docs/api/java/lang/System.html">System.out.println("year, semestre, sendIonline, sendEmail"); |
||
461 | 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"); |
||
462 | |||
1353 | jmachado | 463 | 1.5.0/docs/api/java/lang/String.html">String year = "201516";DaoFactory.getConfigurationDaoImpl().getImportsDefaultImportYearCreateTransaction(); |
1559 | jmachado | 464 | 1.5.0/docs/api/java/lang/String.html">String semestre = "A"; |
1133 | jmachado | 465 | boolean sendIonline = false; |
466 | boolean sendEmail = false; |
||
1312 | jmachado | 467 | int institutionCode = 1; |
995 | jmachado | 468 | if(args != null && args.length > 0) |
469 | year = args[0]; |
||
997 | jmachado | 470 | |
995 | jmachado | 471 | if(args != null && args.length > 1) |
472 | semestre = args[1]; |
||
997 | jmachado | 473 | |
995 | jmachado | 474 | if(args != null && args.length > 2) |
475 | sendIonline = 1.5.0/docs/api/java/lang/Boolean.html">Boolean.parseBoolean(args[2]); |
||
997 | jmachado | 476 | |
995 | jmachado | 477 | if(args != null && args.length > 3) |
478 | sendEmail = 1.5.0/docs/api/java/lang/Boolean.html">Boolean.parseBoolean(args[3]); |
||
997 | jmachado | 479 | |
1312 | jmachado | 480 | if(args != null && args.length > 4) |
481 | institutionCode = 1.5.0/docs/api/java/lang/Integer.html">Integer.parseInt(args[4]); |
||
482 | |||
995 | jmachado | 483 | AbstractDao.getCurrentSession().beginTransaction(); |
484 | if(semestre != null) |
||
1312 | jmachado | 485 | new ImportSumariesJson().run(year,institutionCode,semestre,sendIonline,sendEmail); |
995 | jmachado | 486 | else |
487 | { |
||
1312 | jmachado | 488 | new ImportSumariesJson().run(year,institutionCode,"S1",sendIonline,sendEmail); |
489 | new ImportSumariesJson().run(year,institutionCode,"S2",sendIonline,sendEmail); |
||
490 | new ImportSumariesJson().run(year,institutionCode,"A",sendIonline,sendEmail); |
||
995 | jmachado | 491 | } |
492 | AbstractDao.getCurrentSession().getTransaction().commit(); |
||
493 | } |
||
494 | |||
495 | |||
496 | @1.5.0/docs/api/java/lang/Override.html">Override |
||
1070 | jmachado | 497 | protected ILogMessages runJobServiceTask() throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable { |
995 | jmachado | 498 | |
1070 | jmachado | 499 | 1.5.0/docs/api/java/lang/String.html">String importYear = getParametersMap().get(JOB_importYear_KEY).getObject(); |
500 | 1.5.0/docs/api/java/lang/String.html">String semestre = getParametersMap().get(JOB_semestre_KEY).getObject(); |
||
501 | 1.5.0/docs/api/java/lang/String.html">String sendIonlineStr = getParametersMap().get(JOB_sendIonline_KEY).getObject(); |
||
502 | 1.5.0/docs/api/java/lang/String.html">String sendEmailStr = getParametersMap().get(JOB_sendEmail_KEY).getObject(); |
||
1312 | jmachado | 503 | 1.5.0/docs/api/java/lang/String.html">String institutionCode = getParametersMap().get(JOB_institution_KEY).getObject(); |
1070 | jmachado | 504 | boolean sendIonline = sendIonlineStr != null && 1.5.0/docs/api/java/lang/Boolean.html">Boolean.parseBoolean(sendIonlineStr); |
505 | boolean sendEmail = sendEmailStr != null && 1.5.0/docs/api/java/lang/Boolean.html">Boolean.parseBoolean(sendEmailStr); |
||
1312 | jmachado | 506 | return run(importYear,1.5.0/docs/api/java/lang/Integer.html">Integer.parseInt(institutionCode),semestre,sendIonline,sendEmail); |
995 | jmachado | 507 | } |
508 | |||
1353 | jmachado | 509 | |
510 | /** |
||
511 | * METODO DE HELP PARA A VISTA DA UNIDADE CURRICULAR |
||
512 | */ |
||
513 | |||
514 | public static void setSummariesFromJson(CourseUnitView cuview) { |
||
515 | |||
516 | int lateSummaries = 0; |
||
517 | int prelancadosSummaries = 0; |
||
518 | List<CourseUnitSummaryView> summariesFromJson = new ArrayList<CourseUnitSummaryView>(); |
||
519 | 1.5.0/docs/api/java/lang/String.html">String summariesJson = cuview.getSummariesJson(); |
||
520 | ArrayList<String> lateSummariesDates = new ArrayList<String>(); |
||
521 | ArrayList<String> prelancadosSummariesDates = new ArrayList<String>(); |
||
522 | |||
523 | if(summariesJson != null && summariesJson.length() > 0) |
||
524 | { |
||
525 | try { |
||
526 | JSONObject jsonObject = new JSONObject(summariesJson); |
||
527 | JSONArray summaries = jsonObject.getJSONArray("summary"); |
||
528 | for(int i = 0; i < summaries.length();i++) |
||
529 | { |
||
530 | JSONObject summary = summaries.getJSONObject(i); |
||
531 | CourseUnitSummaryImpl sum = DomainObjectFactory.createCourseUnitSummaryImpl(); |
||
532 | |||
533 | if(sum.updateWithJson(summary)){ |
||
534 | CourseUnitSummaryView view = new CourseUnitSummaryView(sum); |
||
535 | summariesFromJson.add(view); |
||
536 | } |
||
537 | else |
||
538 | // so verifica atrasados para unidades do ano corrente a titulo informativo |
||
539 | { |
||
540 | CourseUnitSummaryView view = new CourseUnitSummaryView(sum); |
||
541 | if(view.getNumber() < 0 && view.getOriginalDate().getTime() + 48*60*60*1000 < 1.5.0/docs/api/java/lang/System.html">System.currentTimeMillis()) |
||
542 | { |
||
543 | |||
544 | //System.out.println(view.getNumber() + " " + view.getDate() + " " + view.getOriginalDate()); |
||
545 | lateSummaries++; |
||
546 | lateSummariesDates.add(view.getDate()); |
||
547 | } |
||
548 | else if(sum.getcEstado().equals(""+Globals.SIGES_SUMARIO_PRELANCADO_CODIGO)) |
||
549 | { |
||
550 | prelancadosSummaries++; |
||
551 | prelancadosSummariesDates.add(view.getDate()); |
||
552 | } |
||
553 | } |
||
554 | } |
||
555 | } catch (JSONException e) { |
||
556 | logger.error(e,e); |
||
557 | } |
||
558 | 1.5.0/docs/api/java/util/Collections.html">Collections.sort(summariesFromJson, new Comparator<CourseUnitSummaryView>() { |
||
559 | @1.5.0/docs/api/java/lang/Override.html">Override |
||
560 | public int compare(CourseUnitSummaryView courseUnitSummaryView, CourseUnitSummaryView courseUnitSummaryView2) { |
||
561 | |||
562 | if (courseUnitSummaryView.getNumber() > courseUnitSummaryView2.getNumber()) |
||
563 | return -1; |
||
564 | else if (courseUnitSummaryView.getNumber() < courseUnitSummaryView2.getNumber()) |
||
565 | return 1; |
||
566 | return 0; |
||
567 | } |
||
568 | }); |
||
569 | cuview.setSummariesFromJson(summariesFromJson); |
||
570 | cuview.setLateSummaries(lateSummaries); |
||
571 | cuview.setLateSummariesDates(lateSummariesDates); |
||
572 | cuview.setPrelancadosSummaries(prelancadosSummaries); |
||
573 | cuview.setPrelancadosSummariesDates(prelancadosSummariesDates); |
||
574 | } |
||
575 | } |
||
995 | jmachado | 576 | } |