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