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