Rev 1048 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
358 | jmachado | 1 | package pt.estgp.estgweb.services.sigesimports; |
2 | |||
3 | import jomm.dao.impl.AbstractDao; |
||
4 | import org.apache.log4j.Logger; |
||
5 | import pt.estgp.estgweb.Globals; |
||
6 | import pt.estgp.estgweb.domain.CourseUnit; |
||
7 | import pt.estgp.estgweb.domain.CourseUnitSummary; |
||
8 | import pt.estgp.estgweb.domain.DomainObjectFactory; |
||
9 | import pt.estgp.estgweb.domain.UserSession; |
||
10 | import pt.estgp.estgweb.domain.dao.DaoFactory; |
||
11 | import pt.estgp.estgweb.domain.enums.LangEnum; |
||
12 | import pt.estgp.estgweb.services.email.EMAILJob; |
||
13 | import pt.estgp.estgweb.services.email.SendEmailService; |
||
14 | import pt.estgp.estgweb.services.expceptions.ServiceException; |
||
15 | import pt.estgp.estgweb.services.jobs.JobScheduleService; |
||
16 | import pt.estgp.estgweb.services.logresults.ILogMessages; |
||
17 | import pt.estgp.estgweb.services.logresults.LogMessageTypeEnum; |
||
18 | import pt.estgp.estgweb.services.logresults.impl.DefaultLogMessage; |
||
19 | import pt.estgp.estgweb.services.logresults.impl.DefaultLogMessages; |
||
20 | import pt.estgp.estgweb.utils.DatesUtils; |
||
21 | import pt.ipportalegre.siges.web.services.ArrayOfAula; |
||
22 | import pt.ipportalegre.siges.web.services.Aula; |
||
23 | import pt.ipportalegre.siges.web.services.SiGesWEB; |
||
24 | import pt.utl.ist.berserk.logic.serviceManager.IService; |
||
25 | |||
26 | import javax.xml.namespace.QName; |
||
1017 | jmachado | 27 | import java.math.BigDecimal; |
358 | jmachado | 28 | import java.net.MalformedURLException; |
29 | import java.net.URL; |
||
30 | import java.util.ArrayList; |
||
31 | import java.util.HashMap; |
||
32 | import java.util.HashSet; |
||
33 | import java.util.List; |
||
34 | |||
35 | /** |
||
598 | jmachado | 36 | * @author Jorge Machado |
358 | jmachado | 37 | * @date 11/May/2008 |
38 | * @time 12:51:32 |
||
39 | * @see pt.estgp.estgweb |
||
40 | */ |
||
41 | public class ImportSumaries implements IService |
||
42 | { |
||
43 | |||
44 | 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(ImportSumaries.class); |
||
45 | |||
46 | private static final int MAX_COMMIT = 100; |
||
47 | |||
48 | private List<Long> newSummaries = new ArrayList<Long>(); |
||
49 | |||
50 | public ILogMessages run(1.5.0/docs/api/java/lang/String.html">String year) throws ServiceException |
||
51 | { |
||
52 | DefaultLogMessages logMessages = new DefaultLogMessages(); |
||
1017 | jmachado | 53 | logMessages.addMessage(new DefaultLogMessage("import.summaries", LogMessageTypeEnum.INFO, "instituicao " + DaoFactory.getConfigurationDaoImpl().getSigesInstitutionCode())); |
358 | jmachado | 54 | logger.info("starting summaries import"); |
55 | try |
||
56 | { |
||
57 | AbstractDao.getCurrentSession().beginTransaction(); |
||
1017 | jmachado | 58 | 1.5.0/docs/api/java/lang/String.html">String WSDL = DaoFactory.getConfigurationDaoImpl().getSigesWebServicesWsdl(); |
358 | jmachado | 59 | SiGesWEB service; |
60 | try |
||
61 | { |
||
1017 | jmachado | 62 | service = new SiGesWEB(new 1.5.0/docs/api/java/net/URL.html">URL(WSDL), new 1.5.0/docs/api/javax/xml/namespace/QName.html">QName(Globals.SIGES_WEBSERVICE_TARGET_NAMESPACE, "SiGesWEB")); |
358 | jmachado | 63 | } |
64 | catch (1.5.0/docs/api/java/net/MalformedURLException.html">MalformedURLException e) |
||
65 | { |
||
66 | logMessages.addMessage(new DefaultLogMessage("import.error",e.toString(),"erro na configuracao do WEB Service", LogMessageTypeEnum.INFO)); |
||
67 | logger.fatal(e, e); |
||
68 | return logMessages; |
||
69 | } |
||
70 | |||
71 | int i = 0; |
||
72 | List<Long> cUs = DaoFactory.getCourseUnitDaoImpl().loadIds(year,DatesUtils.getSemestre()); |
||
73 | for(1.5.0/docs/api/java/lang/Long.html">Long cId: cUs) |
||
74 | { |
||
75 | if (i++ > MAX_COMMIT) |
||
76 | { |
||
77 | i = 0; |
||
78 | AbstractDao.getCurrentSession().getTransaction().commit(); |
||
79 | AbstractDao.getCurrentSession().beginTransaction(); |
||
80 | } |
||
81 | CourseUnit c = DaoFactory.getCourseUnitDaoImpl().load(cId); |
||
593 | jmachado | 82 | logMessages.addMessage(new DefaultLogMessage("import.info","","Importing Summaries to CourseUnit: " + c.getCode() + ": course: " + c.getCourse().getCode(), LogMessageTypeEnum.INFO)); |
83 | logger.info("Importing Summaries to CourseUnit: " + c.getName() + " (" + c.getCode() + ") course " + c.getCourse().getName() + " (" + c.getCourse().getCode() + ")"); |
||
358 | jmachado | 84 | |
85 | int tries = 3; |
||
86 | ArrayOfAula arrayOfAulas = null; |
||
87 | while(tries > 0) |
||
88 | { |
||
594 | jmachado | 89 | //comentario de teste |
358 | jmachado | 90 | try |
91 | { |
||
1048 | jmachado | 92 | arrayOfAulas = service.getSiGesWEBSoap().getAulas(new 1.5.0/docs/api/java/math/BigDecimal.html">BigDecimal(DaoFactory.getConfigurationDaoImpl().getSigesInstitutionCode()), year,c.getCode(),DatesUtils.getSemestre(),null); |
358 | jmachado | 93 | break; |
94 | } |
||
95 | catch(1.5.0/docs/api/java/lang/Throwable.html">Throwable t) |
||
96 | { |
||
97 | if(tries-- == 0) |
||
98 | { |
||
99 | logger.error("Giving up of courseUnit:" + cId + " ( " + c.getName() + " ) ",t); |
||
100 | break; |
||
101 | } |
||
102 | |||
103 | logger.error("WebService Fail, trying " + tries + " times sleeping a while"); |
||
104 | 1.5.0/docs/api/java/lang/Thread.html">Thread.sleep(5000); |
||
105 | } |
||
106 | } |
||
107 | if(arrayOfAulas != null && arrayOfAulas.getAula() != null) |
||
108 | { |
||
109 | HashMap<String, CourseUnitSummary> map = new HashMap<String, CourseUnitSummary>(); |
||
110 | if(c.getSummaries() != null && c.getSummaries().size() > 0) |
||
111 | for(CourseUnitSummary s: c.getSummaries()) |
||
112 | map.put(s.getCodigoAula() + ":" + s.getCodigoSumario(),s); |
||
113 | else |
||
114 | c.setSummaries(new HashSet<CourseUnitSummary>()); |
||
115 | for(Aula aula: arrayOfAulas.getAula()) |
||
116 | { |
||
117 | CourseUnitSummary s = map.get(aula.getSumNumeroAula() + ":" + aula.getCodigoSumario()); |
||
118 | if(s == null) |
||
119 | { |
||
120 | s = DomainObjectFactory.createCourseUnitSummaryImpl(); |
||
121 | s.setCourseUnit(c); |
||
122 | c.getSummaries().add(s); |
||
123 | DaoFactory.getCourseUnitSummaryDaoImpl().save(s); |
||
124 | newSummaries.add(s.getId()); |
||
125 | } |
||
126 | s.setCodigoSumario(""+aula.getCodigoSumario().intValue()); |
||
127 | s.setCodigoAula(""+aula.getSumNumeroAula().intValue()); |
||
128 | s.setNumeroAula(aula.getSumNumeroAula().intValue()); |
||
129 | s.setDataAula(aula.getData().toGregorianCalendar().getTime()); |
||
130 | s.setTipoAula(aula.getSumTipoAula()); |
||
131 | s.setCodigoSalaAula("" + aula.getAulaCodigoSalaAula().intValue()); |
||
132 | s.setAlunosPresentes(aula.getSumNumeroAlunos().intValue()); |
||
133 | s.setTitulo(aula.getSumTitulo()); |
||
134 | s.setDescricao(aula.getSumDescricao()); |
||
135 | s.setcEstado(""+aula.getSumCdEstado().intValue()); |
||
995 | jmachado | 136 | s.setNumeroHoras("" + aula.getNumeroCelulas().intValue()); |
358 | jmachado | 137 | } |
138 | } |
||
139 | else |
||
140 | logger.error("Summaries come null in unit:" + c.getCode()); |
||
141 | } |
||
142 | sendEmail(null); |
||
143 | AbstractDao.getCurrentSession().getTransaction().commit(); |
||
144 | } |
||
145 | catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e) |
||
146 | { |
||
147 | logger.error(e, e); |
||
148 | AbstractDao.getCurrentSession().getTransaction().rollback(); |
||
149 | logMessages.addMessage(new DefaultLogMessage("import.error", e.toString(), "see log for details", LogMessageTypeEnum.ERROR)); |
||
150 | throw new ServiceException(e.toString(), e); |
||
151 | } |
||
152 | |||
153 | logMessages.addMessage(new DefaultLogMessage("import.summaries.terminating", LogMessageTypeEnum.INFO)); |
||
154 | logger.info("terminating summaries import"); |
||
155 | return logMessages; |
||
156 | } |
||
157 | |||
158 | |||
159 | public static SendEmailService sendEmailService = new SendEmailService(); |
||
160 | |||
161 | private void sendEmail(UserSession userSession) |
||
162 | { |
||
163 | if(Globals.EMAIL_SUMMARIES && newSummaries.size() > 0) |
||
164 | { |
||
165 | for(long id: newSummaries) |
||
166 | { |
||
167 | CourseUnitSummary summary = DaoFactory.getCourseUnitSummaryDaoImpl().load(id); |
||
168 | List<String> emails = DaoFactory.getCourseUnitSummaryDaoImpl().getStudentsEmails(summary.getId()); |
||
169 | if(emails == null) |
||
170 | logger.warn("No students emails in unit: code:" + summary.getCourseUnit().getCode() + " " + summary.getCourseUnit().getName()); |
||
171 | EMAILJob emailJob = new EMAILJob(sendEmailService.getSummaryEmail(emails,summary,summary.getCourseUnit(), LangEnum.PORTUGUESE)); |
||
172 | JobScheduleService.getInstance().scheduleNow(emailJob,"New Summary EMAIL from: " + summary.getCourseUnit().getName() + " aula: " +summary.getNumeroAula() ,userSession); |
||
173 | } |
||
174 | } |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Testar por aqui poi requer Super Role e assim e' autmatico |
||
179 | * |
||
595 | jmachado | 180 | * |
358 | jmachado | 181 | * @param args of main |
182 | * @throws pt.estgp.estgweb.services.expceptions.ServiceException on error |
||
183 | */ |
||
184 | public static void main(1.5.0/docs/api/java/lang/String.html">String[] args) throws ServiceException |
||
185 | { |
||
995 | jmachado | 186 | 1.5.0/docs/api/java/lang/String.html">String year = DaoFactory.getConfigurationDaoImpl().getImportsDefaultImportYearCreateTransaction(); |
491 | jmachado | 187 | if(args != null && args.length > 0) |
188 | year = args[0]; |
||
189 | new ImportSumaries().run(year); |
||
358 | jmachado | 190 | } |
191 | |||
192 | |||
193 | } |