Subversion Repositories bacoAlunos

Rev

Rev 997 | Rev 1006 | 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;
18
import pt.estgp.estgweb.services.courseunits.CreateCourseUnitXML;
19
import pt.estgp.estgweb.services.email.EMAILJob;
20
import pt.estgp.estgweb.services.email.SendEmailService;
21
import pt.estgp.estgweb.services.expceptions.ServiceException;
22
import pt.estgp.estgweb.services.jobs.JobScheduleService;
23
import pt.estgp.estgweb.services.jobs.ServiceJob;
24
import pt.estgp.estgweb.services.logresults.ILogMessages;
25
import pt.estgp.estgweb.services.logresults.LogMessageTypeEnum;
26
import pt.estgp.estgweb.services.logresults.impl.DefaultLogMessage;
27
import pt.estgp.estgweb.services.logresults.impl.DefaultLogMessages;
28
import pt.estgp.estgweb.utils.Dom4jUtil;
29
import pt.estgp.estgweb.web.FtpServer;
30
import pt.ipportalegre.siges.web.services.ArrayOfAula;
31
import pt.ipportalegre.siges.web.services.Aula;
32
import pt.ipportalegre.siges.web.services.SiGesWEB;
33
import pt.utl.ist.berserk.logic.serviceManager.IService;
34
 
35
import javax.xml.namespace.QName;
36
import javax.xml.transform.TransformerException;
37
import javax.xml.transform.dom.DOMSource;
38
import java.io.File;
39
import java.io.FileInputStream;
40
import java.io.FileOutputStream;
41
import java.io.IOException;
42
import java.net.URL;
43
import java.util.ArrayList;
44
import java.util.HashMap;
45
import java.util.List;
46
import java.util.Map;
47
 
48
/**
49
 * @author Jorge Machado
50
 * @date 11/May/2008
51
 * @time 12:51:32
52
 * @see pt.estgp.estgweb
53
 */
54
public class ImportSumariesJson extends ServiceJob implements IService
55
{
56
 
57
    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);
58
 
59
    private static final int MAX_COMMIT = 100;
60
 
61
 
62
    public ILogMessages run(1.5.0/docs/api/java/lang/String.html">String year,1.5.0/docs/api/java/lang/String.html">String semestre) throws ServiceException
63
    {
64
        return run(year,semestre,true,true);
65
    }
66
 
67
    public ILogMessages run(1.5.0/docs/api/java/lang/String.html">String year,1.5.0/docs/api/java/lang/String.html">String semestre,boolean sendIONLINE,boolean sendEMAIL) throws ServiceException
68
    {
69
        1.5.0/docs/api/java/lang/String.html">String msgS = "STARTING SUMARIES IMPORT SERVICE FOR year: " + year + " semestre: " + semestre + " sendIONLINE: " + sendIONLINE + " sendEMAIL: " + sendEMAIL;
70
        serviceLogInfo(msgS);
71
        logger.info(msgS);
72
 
73
        DefaultLogMessages logMessages = new DefaultLogMessages();
74
        logMessages.addMessage(new DefaultLogMessage("import.summaries", LogMessageTypeEnum.INFO, "instituicao " + Globals.SIGES_INSTITUTION_CODE));
75
        serviceLogInfo(logMessages.getLastMessage());
76
 
77
 
78
        int countUnits = 0;
79
        int countUnitsOk = 0;
80
        int countUnitsZeroSummaries = 0;
81
 
82
        List<String> unitsZeroSummaries = new ArrayList<String>();
83
        List<String> unitsFailed = new ArrayList<String>();
84
        try
85
        {
86
 
87
            SiGesWEB service;
88
            try
89
            {
90
                serviceLogInfo("STARTING WEB SERVICE AT " + Globals.SIGES_WEBSERVICE_WSDL);
91
                service = new SiGesWEB(new 1.5.0/docs/api/java/net/URL.html">URL(Globals.SIGES_WEBSERVICE_WSDL), new 1.5.0/docs/api/javax/xml/namespace/QName.html">QName(Globals.SIGES_WEBSERVICE_TARGET_NAMESPACE, "SiGesWEB"));
92
            }
93
            catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
94
            {
95
                logMessages.addMessage(new DefaultLogMessage("import.error",e.toString(),"erro na configuracao do WEB Service", LogMessageTypeEnum.INFO));
96
                serviceLogInfo(logMessages.getLastMessage());
97
                logger.fatal(e, e);
98
                serviceLogError(e.toString(), e);
99
                return logMessages;
100
            }
101
 
102
            int i = 0;
103
            //List<Long> cUs = DaoFactory.getCourseUnitDaoImpl().loadIds(year,DatesUtils.getSemestre());
104
            List<Long> cUs = DaoFactory.getCourseUnitDaoImpl().loadIds(year,semestre);
105
            1.5.0/docs/api/java/lang/String.html">String nowCode = "";
106
            for(1.5.0/docs/api/java/lang/Long.html">Long cId: cUs)
107
            {
108
                nowCode = "BACO_ONLY_CODE" + cId;
109
                countUnits++;
110
                try
111
                {
112
                    if (i++ > MAX_COMMIT)
113
                    {
114
                        i = 0;
996 jmachado 115
                        setProgress((int) (((float)countUnits)/((float)cUs.size())*100.0f));
995 jmachado 116
                        AbstractDao.getCurrentSession().getTransaction().commit();
117
                        AbstractDao.getCurrentSession().beginTransaction();
118
                        serviceLogInfo("Commiting database");
119
                    }
120
                    CourseUnit c = DaoFactory.getCourseUnitDaoImpl().load(cId);
121
                    nowCode = c.getCode();
122
                    logMessages.addMessage(new DefaultLogMessage("import.info","Importing Summaries to CourseUnit: " + c.getCode() + ": course: " + c.getCourse().getCode(),"", LogMessageTypeEnum.INFO));
123
                    logger.info("Importing Summaries to CourseUnit: " + c.getName() + " (" + c.getCode() + ") course " + c.getCourse().getName() + " (" + c.getCourse().getCode() + ")");
124
                    serviceLogInfo(logMessages.getLastMessage());
125
 
126
                    int tries = 3;
127
                    ArrayOfAula arrayOfAulas = null;
128
                    while(tries > 0)
129
                    {
130
                    //comentario de teste
131
                        try
132
                        {
133
                            arrayOfAulas = service.getSiGesWEBSoap().getAulas(Globals.SIGES_INSTITUTION_CODE, year,c.getCode(),semestre);
134
                            break;
135
                        }
136
                        catch(1.5.0/docs/api/java/lang/Throwable.html">Throwable t)
137
                        {
138
                            if(tries-- == 0)
139
                            {
140
                                logger.error("Giving up of courseUnit:" + cId + " ( " + c.getName() + " ) ",t);
141
                                serviceLogError("Giving up of courseUnit:" + cId + " ( " + c.getName() + " ) ",t);
142
                                break;
143
                            }
144
 
145
                            logger.error("WebService Fail, trying " + tries + " times sleeping a while");
146
                            serviceLogError("WebService Fail, trying " + tries + " times sleeping a while");
147
                            1.5.0/docs/api/java/lang/Thread.html">Thread.sleep(5000);
148
                        }
149
                    }
150
 
151
                    if(arrayOfAulas != null && arrayOfAulas.getAula() != null)
152
                    {
153
                        List<CourseUnitSummary> courseUnitSummaries = new ArrayList<CourseUnitSummary>();
154
 
155
                        //HashMap<String, CourseUnitSummary> map = new HashMap<String, CourseUnitSummary>();
156
                        /*if(c.getSummaries() != null && c.getSummaries().size() > 0)
157
                            for(CourseUnitSummary s: c.getSummaries())
158
                                map.put(s.getCodigoAula() + ":" + s.getCodigoSumario(),s);
159
                        else
160
                            c.setSummaries(new HashSet<CourseUnitSummary>());*/
161
                        JSONObject summariesJson = new JSONObject();
162
                        JSONArray summaries = new JSONArray();
163
                        summariesJson.put("summary",summaries);
164
                        for(Aula aula: arrayOfAulas.getAula())
165
                        {
166
 
167
                            //CourseUnitSummary s = map.get(aula.getSumNumeroAula() + ":" + aula.getCodigoSumario());
168
                            //if(s == null)
169
                            //{
170
                            CourseUnitSummaryImpl s = DomainObjectFactory.createCourseUnitSummaryImpl();
171
                            s.setCourseUnit(c);
172
                                //c.getSummaries().add(s);
173
                                //DaoFactory.getCourseUnitSummaryDaoImpl().save(s);
174
                            //summaries.add();
175
                            //}
176
                            s.setCodigoSumario(""+aula.getCodigoSumario().intValue());
177
                            s.setCodigoAula(""+aula.getSumNumeroAula().intValue());
178
                            s.setNumeroAula(aula.getSumNumeroAula().intValue());
179
                            s.setDataAula(aula.getData().toGregorianCalendar().getTime());
180
                            s.setTipoAula(aula.getSumTipoAula());
181
                            s.setCodigoSalaAula("" + aula.getAulaCodigoSalaAula().intValue());
182
                            s.setAlunosPresentes(aula.getSumNumeroAlunos().intValue());
183
                            s.setTitulo(aula.getSumTitulo());
184
                            s.setDescricao(aula.getSumDescricao());
185
                            s.setcEstado(""+aula.getSumCdEstado().intValue());
186
                            s.setNumeroHoras("" + aula.getNumeroCelulas().intValue());
187
                            summaries.put(s.getJson());
188
                            courseUnitSummaries.add(s);
189
                        }
190
                        if(courseUnitSummaries.size() == 0)
191
                        {
192
                            countUnitsZeroSummaries++;
193
                            unitsZeroSummaries.add(c.getCode());
194
                        }
195
                        else
196
                        {
197
                            countUnitsOk++;
198
                        }
199
                        c.setSummariesJson(summariesJson.toString());
200
 
201
                        if(sendIONLINE)
202
                        {
203
                            1.5.0/docs/api/java/lang/String.html">String path = generatePdf(c);
204
                            sendPdfFtpIonline(c,new ArrayList<String>(),null,path,null);
205
                            FilesUtils.delete(path);
206
                        }
207
                        if(sendEMAIL)
208
                        {
209
                            sendEmail(null, courseUnitSummaries,true);
210
                        }
211
                    }
212
                    else
213
                    {
214
                        countUnitsZeroSummaries++;
215
                        unitsZeroSummaries.add(c.getCode());
216
                        logger.error("Summaries come null in unit:" + c.getCode());
217
                        serviceLogError("Summaries come null in unit:" + c.getCode());
218
                    }
219
                }
220
                catch(1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
221
                {
222
                    unitsFailed.add(nowCode);
223
                    logger.error(e,e);
224
                    serviceLogError(e.toString(),e);
225
                }
226
            }
227
 
228
            setProgress(100);
229
            serviceLogError("Finish commiting");
230
 
231
        }
232
        catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
233
        {
234
            logger.error(e, e);
235
            logMessages.addMessage(new DefaultLogMessage("import.error", e.toString(), "see log for details", LogMessageTypeEnum.ERROR));
236
            serviceLogError(logMessages.getLastMessage(),e);
237
            throw new ServiceException(e.toString(), e);
238
        }
239
 
240
        logMessages.addMessage(new DefaultLogMessage("import.summaries.terminating", LogMessageTypeEnum.INFO));
241
        logger.info("terminating summaries import");
242
        serviceLogInfo(logMessages.getLastMessage());
243
 
244
        serviceLogInfo("#######################################");
245
        serviceLogInfo("#######################################");
246
        serviceLogInfo("UNITS PROCESSED: " + countUnits);
247
        serviceLogInfo("UNITS FAILED: " + (countUnits - countUnitsZeroSummaries - countUnitsOk));
248
        serviceLogInfo("UNITS IMPORT OK: " + countUnitsOk);
249
        serviceLogInfo("UNITS ZERO SUMARIES: " + countUnitsZeroSummaries);
250
        serviceLogInfo("#######################################");
251
        return logMessages;
252
    }
253
 
254
 
255
    public static SendEmailService sendEmailService = new SendEmailService();
256
 
257
    private void sendEmail(UserSession userSession,List<CourseUnitSummary> summaries,boolean sendEmail)
258
    {
259
        if(Globals.EMAIL_SUMMARIES && summaries.size() > 0 && sendEmail)
260
        {
261
           // for(CourseUnitSummary summary: summaries)
262
           // {
263
 
264
                List<String> emails = DaoFactory.getCourseUnitSummaryDaoImpl().getStudentsEmails(summaries.get(0).getId());
265
                if(emails == null)
266
                    logger.warn("No students emails in unit: code:" + summaries.get(0).getCourseUnit().getCode() + " " + summaries.get(0).getCourseUnit().getName());
267
                EMAILJob emailJob = new EMAILJob(sendEmailService.getSummariesEmail(emails, summaries, summaries.get(0).getCourseUnit(), LangEnum.PORTUGUESE));
268
                JobScheduleService.getInstance().scheduleNow(emailJob,"New Summary EMAIL from: " + summaries.get(0).getCourseUnit().getName() + " aula: " +summaries.get(summaries.size()-1).getNumeroAula() ,userSession);
269
           // }
270
        }
271
    }
272
 
273
 
274
 
275
 
276
 
277
 
278
    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 {
279
        5+0%2Fdocs%2Fapi+Document">Document dom = new CreateCourseUnitXML().run(c,null);
280
        org.w3c.dom.5+0%2Fdocs%2Fapi+Document">Document dd = Dom4jUtil.toW3c(dom);
281
        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";
282
        1.5.0/docs/api/java/io/FileOutputStream.html">FileOutputStream out = new 1.5.0/docs/api/java/io/FileOutputStream.html">FileOutputStream(path);
283
        Map<String,Object> params = new HashMap<String,Object>();
284
        params.put("site", Globals.SITE_URL);
285
        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);
286
        out.flush();
287
        out.close();
288
        return path;
289
    }
290
 
291
    private static 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 {
292
 
293
        FtpServer server = null;
294
        if(client == null)
295
        {
296
            server = FtpServer.getNewServer(Globals.FTP_IONLINE_URL,Globals.FTP_IONLINE_USER,Globals.FTP_IONLINE_PASS);
297
            client = server.getClient();
1005 jmachado 298
            if(client == null)
299
            {
300
                logger.warn("###################");
301
                logger.warn("################### > CANT CONNECT FTP");
302
                return;
303
            }
304
 
995 jmachado 305
        }
306
        client.setFileType(FTP.BINARY_FILE_TYPE);
307
 
308
 
309
        1.5.0/docs/api/java/lang/String.html">String pastaFichaCurricular = ((CourseUnitImpl)cu).getSummariosPathIntranet();
310
        if(!client.changeWorkingDirectory(pastaFichaCurricular))
311
        {
312
            logger.error("FTP CANT CHANGE TO PATH: " + pastaFichaCurricular);
313
        }
314
        else
315
        {
316
            client.storeFile(((CourseUnitImpl)cu).getSummariosFileNameIntranet(),new 1.5.0/docs/api/java/io/FileInputStream.html">FileInputStream(pdfPath));
317
 
318
            if(server != null)
319
            {
320
                client.quit();
321
                client.disconnect();
322
            }
323
        }
324
        //
325
    }
326
    /**
327
     * Testar por aqui poi requer Super Role e assim e' autmatico
328
     *
329
     *
330
     * @param args of main
331
     * @throws pt.estgp.estgweb.services.expceptions.ServiceException on error
332
     */
333
    public static void main(1.5.0/docs/api/java/lang/String.html">String[] args) throws ServiceException, JSONException {
334
 
997 jmachado 335
        1.5.0/docs/api/java/lang/System.html">System.out.println("Syntax:");
336
        1.5.0/docs/api/java/lang/System.html">System.out.println("year, semestre, sendIonline, sendEmail");
337
        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");
338
 
995 jmachado 339
        1.5.0/docs/api/java/lang/String.html">String year = DaoFactory.getConfigurationDaoImpl().getImportsDefaultImportYearCreateTransaction();
340
        1.5.0/docs/api/java/lang/String.html">String semestre = null;
341
        boolean sendIonline = true;
342
        boolean sendEmail = true;
343
        if(args != null && args.length > 0)
344
            year = args[0];
997 jmachado 345
 
995 jmachado 346
        if(args != null && args.length > 1)
347
            semestre = args[1];
997 jmachado 348
 
995 jmachado 349
        if(args != null && args.length > 2)
350
            sendIonline = 1.5.0/docs/api/java/lang/Boolean.html">Boolean.parseBoolean(args[2]);
997 jmachado 351
 
995 jmachado 352
        if(args != null && args.length > 3)
353
            sendEmail = 1.5.0/docs/api/java/lang/Boolean.html">Boolean.parseBoolean(args[3]);
997 jmachado 354
 
995 jmachado 355
        AbstractDao.getCurrentSession().beginTransaction();
356
        if(semestre != null)
357
            new ImportSumariesJson().run(year,semestre,sendIonline,sendEmail);
358
        else
359
        {
360
            new ImportSumariesJson().run(year,"S1",sendIonline,sendEmail);
361
            new ImportSumariesJson().run(year,"S2",sendIonline,sendEmail);
362
            new ImportSumariesJson().run(year,"A",sendIonline,sendEmail);
363
        }
364
        AbstractDao.getCurrentSession().getTransaction().commit();
365
    }
366
 
367
 
368
    public static final 1.5.0/docs/api/java/lang/String.html">String JOB_importYear_KEY = "JOB_importYear_KEY";
369
    public static final 1.5.0/docs/api/java/lang/String.html">String JOB_semestre_KEY = "JOB_semestre_KEY";
370
    public static final 1.5.0/docs/api/java/lang/String.html">String JOB_sendIonline_KEY = "JOB_sendIonline_KEY";
371
    public static final 1.5.0/docs/api/java/lang/String.html">String JOB_sendEmail_KEY = "JOB_sendEmail_KEY";
372
 
373
    @1.5.0/docs/api/java/lang/Override.html">Override
374
    protected void runJobServiceTask() throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable {
375
 
376
        1.5.0/docs/api/java/lang/String.html">String importYear = (1.5.0/docs/api/java/lang/String.html">String) getParametersMap().get(JOB_importYear_KEY).getObject();
377
        1.5.0/docs/api/java/lang/String.html">String semestre = (1.5.0/docs/api/java/lang/String.html">String) getParametersMap().get(JOB_semestre_KEY).getObject();
378
        1.5.0/docs/api/java/lang/Boolean.html">Boolean sendIonline = (1.5.0/docs/api/java/lang/Boolean.html">Boolean) getParametersMap().get(JOB_sendIonline_KEY).getObject();
379
        1.5.0/docs/api/java/lang/Boolean.html">Boolean sendEmail = (1.5.0/docs/api/java/lang/Boolean.html">Boolean) getParametersMap().get(JOB_sendEmail_KEY).getObject();
380
        run(importYear,semestre,sendIonline,sendEmail);
381
 
382
    }
383
 
384
}