Rev 1505 | Rev 1507 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
214 | jmachado | 1 | package pt.estgp.estgweb.services.courses; |
2 | |||
1496 | jmachado | 3 | import com.owlike.genson.Genson; |
4 | import com.owlike.genson.TransformationException; |
||
5 | import com.owlike.genson.reflect.VisibilityFilter; |
||
417 | jmachado | 6 | import jomm.utils.FilesUtils; |
1496 | jmachado | 7 | import jomm.utils.StreamsUtils; |
417 | jmachado | 8 | import org.apache.log4j.Logger; |
9 | import org.dom4j.Document; |
||
1496 | jmachado | 10 | import org.json.JSONArray; |
11 | import org.json.JSONException; |
||
12 | import org.json.JSONObject; |
||
417 | jmachado | 13 | import pt.estgp.estgweb.Globals; |
1496 | jmachado | 14 | import pt.estgp.estgweb.domain.*; |
417 | jmachado | 15 | import pt.estgp.estgweb.domain.dao.DaoFactory; |
16 | import pt.estgp.estgweb.domain.views.CourseView; |
||
17 | import pt.estgp.estgweb.filters.chains.ResourceAccessControlEnum; |
||
1496 | jmachado | 18 | import pt.estgp.estgweb.services.courses.xsd.Curso; |
19 | import pt.estgp.estgweb.services.courses.xsd.UnidadeType; |
||
417 | jmachado | 20 | import pt.estgp.estgweb.services.data.IRepositoryFile; |
214 | jmachado | 21 | import pt.estgp.estgweb.services.data.RepositoryService; |
444 | jmachado | 22 | import pt.estgp.estgweb.services.expceptions.AlreadyExistsException; |
995 | jmachado | 23 | import pt.estgp.estgweb.services.expceptions.ServiceException; |
1496 | jmachado | 24 | import pt.estgp.estgweb.utils.ConfigProperties; |
248 | jmachado | 25 | import pt.estgp.estgweb.utils.Dom4jUtil; |
417 | jmachado | 26 | import pt.utl.ist.berserk.logic.serviceManager.IService; |
214 | jmachado | 27 | |
1496 | jmachado | 28 | import javax.xml.bind.JAXBContext; |
29 | import javax.xml.bind.JAXBException; |
||
30 | import javax.xml.bind.Marshaller; |
||
31 | import javax.xml.bind.Unmarshaller; |
||
1505 | jmachado | 32 | import java.io.*; |
1496 | jmachado | 33 | import java.net.URL; |
34 | import java.util.*; |
||
214 | jmachado | 35 | |
36 | /* |
||
37 | * @author Goncalo Luiz gedl [AT] rnl [DOT] ist [DOT] utl [DOT] pt |
||
38 | * |
||
39 | * |
||
40 | * Created at 17/Out/2003 , 23:45:24 |
||
41 | * |
||
42 | */ |
||
43 | /** |
||
44 | * @author Jorge Machado |
||
45 | * |
||
46 | * |
||
47 | * Created at 17/Out/2003 , 23:45:24 |
||
48 | * |
||
49 | */ |
||
50 | public class CoursesService implements IService |
||
51 | { |
||
52 | 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(CoursesService.class); |
||
53 | |||
54 | RepositoryService repositoryService = new RepositoryService(); |
||
55 | |||
56 | |||
1505 | jmachado | 57 | /** |
58 | * Servico e subservico para termos acesso as variaveis de controlo |
||
59 | * @param id |
||
60 | * @param initUnits |
||
61 | * @return |
||
62 | * @throws ServiceException |
||
63 | */ |
||
64 | public CourseView loadCourse(long id, boolean initUnits) |
||
65 | throws ServiceException |
||
214 | jmachado | 66 | { |
1505 | jmachado | 67 | return loadCourse(id,initUnits,false); |
68 | } |
||
69 | public CourseView loadCourseAndStudiesPlans(long id, boolean initUnits) |
||
70 | throws ServiceException |
||
71 | { |
||
72 | return loadCourse(id,initUnits,true); |
||
73 | } |
||
74 | |||
75 | |||
76 | private CourseView loadCourse(long id, boolean initUnits,boolean loadStudiesPlans) throws ServiceException |
||
77 | { |
||
214 | jmachado | 78 | Course c = DaoFactory.getCourseDaoImpl().get(id); |
79 | |||
80 | if(c != null) |
||
81 | { |
||
1505 | jmachado | 82 | return getCourseView(initUnits, c,loadStudiesPlans); |
214 | jmachado | 83 | } |
84 | return null; |
||
85 | } |
||
86 | |||
1505 | jmachado | 87 | /** |
88 | * Servico e subservico para termos acesso as variaveis de controlo |
||
89 | * @param code |
||
90 | * @param initUnits |
||
91 | * @return |
||
92 | * @throws ServiceException |
||
93 | */ |
||
94 | |||
95 | public CourseView loadCourseByCode(1.5.0/docs/api/java/lang/String.html">String code, boolean initUnits) throws ServiceException |
||
345 | jmachado | 96 | { |
1505 | jmachado | 97 | return loadCourseByCode(code,initUnits,false); |
345 | jmachado | 98 | } |
1505 | jmachado | 99 | public CourseView loadCourseByCodeAndStudiesPlans(1.5.0/docs/api/java/lang/String.html">String code, boolean initUnits) throws ServiceException |
100 | { |
||
101 | return loadCourseByCode(code,initUnits,true); |
||
102 | } |
||
345 | jmachado | 103 | |
1505 | jmachado | 104 | |
105 | private CourseView loadCourseByCode(1.5.0/docs/api/java/lang/String.html">String code, boolean initUnits,boolean loadStudiesPlans) throws ServiceException |
||
214 | jmachado | 106 | { |
444 | jmachado | 107 | try{ |
108 | Course c = DaoFactory.getCourseDaoImpl().findCourseByCode(code); |
||
109 | if(c != null) |
||
214 | jmachado | 110 | { |
1505 | jmachado | 111 | return getCourseView(initUnits, c, loadStudiesPlans); |
214 | jmachado | 112 | } |
113 | } |
||
444 | jmachado | 114 | catch(1.5.0/docs/api/java/lang/Throwable.html">Throwable e) |
115 | { |
||
116 | logger.error(e + " loading code:" + code,e); |
||
117 | throw new ServiceException("loading code: " + code + " - " + e.toString(),e); |
||
118 | } |
||
214 | jmachado | 119 | return null; |
120 | } |
||
121 | |||
1505 | jmachado | 122 | /** |
123 | * Carrega efetivamente o curso nos servicos load e load by code |
||
124 | * @param initUnits |
||
125 | * @param c |
||
126 | * @return |
||
127 | */ |
||
128 | |||
129 | private CourseView getCourseView(boolean initUnits, Course c,boolean loadStudiesPlans) { |
||
130 | CourseView cV = new CourseView(c,initUnits); |
||
131 | /* |
||
132 | * todo Parte antig antigo XML do plano de estudos para remover futuramente*/ |
||
133 | if(c.getStudiesPlan() != null) |
||
134 | { |
||
135 | RepositoryFileImpl repositoryFile = repositoryService.loadView(c.getStudiesPlan()); |
||
136 | cV.setStudiesPlan(repositoryFile); |
||
137 | } |
||
138 | if(loadStudiesPlans && c.getStudiesPlans() != null && c.getStudiesPlans().size() > 0) |
||
139 | { |
||
140 | for(CourseStudiesPlan sp : c.getStudiesPlans()) |
||
141 | { |
||
142 | sp.getVersion(); |
||
143 | cV.getCourseStudiesPlans().add(sp); |
||
144 | } |
||
145 | } |
||
146 | |||
147 | return cV; |
||
148 | } |
||
149 | |||
150 | public List<String> loadImportYears(UserSession userSession) throws ServiceException |
||
151 | { |
||
152 | List<String> importYears = DaoFactory.getCourseDaoImpl().loadImportYears(); |
||
153 | List<String> imStrings = new ArrayList<String>(); |
||
154 | for(1.5.0/docs/api/java/lang/String.html">String importYear: importYears) |
||
155 | { |
||
156 | imStrings.add(importYear); |
||
157 | } |
||
158 | return imStrings; |
||
159 | } |
||
160 | |||
161 | |||
162 | |||
214 | jmachado | 163 | public CourseView submitCourse(CourseView courseView, |
164 | 5+0%2Fdocs%2Fapi+InputStream">InputStream stream, |
||
165 | 1.5.0/docs/api/java/lang/String.html">String name, |
||
166 | int size, |
||
167 | 1.5.0/docs/api/java/lang/String.html">String contentType, |
||
1496 | jmachado | 168 | UserSession userSession) throws ServiceException, JAXBException, TransformationException, 1.5.0/docs/api/java/io/IOException.html">IOException { |
214 | jmachado | 169 | Course c; |
170 | if(courseView.getId() > 0) |
||
444 | jmachado | 171 | { |
214 | jmachado | 172 | c = DaoFactory.getCourseDaoImpl().get(courseView.getId()); |
444 | jmachado | 173 | } |
214 | jmachado | 174 | else |
175 | { |
||
444 | jmachado | 176 | c = DaoFactory.getCourseDaoImpl().findCourseByCodeAndYear(courseView.getCode(),courseView.getImportYear()); |
177 | if(c != null) |
||
178 | throw new AlreadyExistsException(AlreadyExistsException.ALREADY_EXISTS_COURSE); |
||
214 | jmachado | 179 | c = DomainObjectFactory.createCourseImpl(); |
180 | DaoFactory.getCourseDaoImpl().save(c); |
||
181 | } |
||
182 | |||
248 | jmachado | 183 | 1.5.0/docs/api/java/lang/String.html">String htmlTrasformationResult = null; |
184 | |||
1505 | jmachado | 185 | //Stream que pode vir do upload da UIde Admin de Cursos |
1496 | jmachado | 186 | htmlTrasformationResult = uploadStudiesPlan(stream, name, size, contentType, userSession, c,false,null); |
187 | courseView.persistViewInObject(c); |
||
188 | CourseView cv = loadCourse(c.getId(),false); |
||
189 | cv.setHtmlResult(htmlTrasformationResult); |
||
190 | |||
191 | /** |
||
192 | * New## generating course json |
||
193 | */ |
||
194 | generateCourseJson(c); |
||
195 | |||
196 | return cv; |
||
197 | } |
||
198 | |||
199 | private 1.5.0/docs/api/java/lang/String.html">String uploadStudiesPlan(5+0%2Fdocs%2Fapi+InputStream">InputStream stream, 1.5.0/docs/api/java/lang/String.html">String name, int size, 1.5.0/docs/api/java/lang/String.html">String contentType, UserSession userSession, Course c,boolean forceUrlFichas, 1.5.0/docs/api/java/lang/String.html">String systemUrl) throws JAXBException, TransformationException { |
||
200 | 1.5.0/docs/api/java/lang/String.html">String htmlTrasformationResult = null; |
||
1505 | jmachado | 201 | //APENAS NO CASO DO AMDIN FAZER UPLOAD DE UM XML |
214 | jmachado | 202 | if(stream != null && size > 0) |
203 | { |
||
268 | jmachado | 204 | 1.5.0/docs/api/java/lang/String.html">String extension = FilesUtils.getExtension(name); |
205 | if(c.getStudiesPlan() == null) |
||
206 | { |
||
332 | jmachado | 207 | 1.5.0/docs/api/java/lang/String.html">String identifier = repositoryService.storeRepositoryFile(stream, contentType, extension, size, name, "course.studies.plan " + c.getName(), ResourceAccessControlEnum.publicDomain,userSession); |
268 | jmachado | 208 | c.setStudiesPlan(identifier); |
209 | } |
||
210 | else |
||
211 | { |
||
212 | repositoryService.updateRepositoryFile(c.getStudiesPlan(), stream, contentType, extension, size, name, "course.studies.plan " + c.getName(), ResourceAccessControlEnum.publicDomain); |
||
213 | } |
||
1496 | jmachado | 214 | htmlTrasformationResult = generateHtmlCache(userSession, c); |
215 | //####New#### Generating XML with JaxB |
||
1505 | jmachado | 216 | //ISTO SO É CHAMADO NO CASO DE SE FAZER UPLOAD DE UM NOVO PLANO PELO MECANISMO ANTIGO |
217 | generateXmlJaxbStudiesPlanVersionFromRepositoryOldPlanStream(userSession, c, forceUrlFichas, systemUrl); |
||
1496 | jmachado | 218 | } |
219 | return htmlTrasformationResult; |
||
220 | } |
||
221 | |||
1505 | jmachado | 222 | |
223 | |||
1496 | jmachado | 224 | private void generateCourseJson(Course cAux) throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException { |
225 | CourseImpl c = (CourseImpl) DaoFactory.getCourseDaoImpl().narrow(cAux); |
||
226 | |||
227 | if(c.getValidationRole() != null && c.getValidationRole().trim().length() > 0) |
||
228 | { |
||
229 | List<Teacher> courseComissionProxys = DaoFactory.getUserDaoImpl().loadRoleTeachers(c.getValidationRole()); |
||
230 | List<Teacher> courseComission = new ArrayList<Teacher>(); |
||
231 | for(Teacher t: courseComissionProxys) |
||
248 | jmachado | 232 | { |
1496 | jmachado | 233 | courseComission.add(DaoFactory.getTeacherDaoImpl().narrow(t)); |
248 | jmachado | 234 | } |
1496 | jmachado | 235 | c.setCourseComission(courseComission); |
236 | } |
||
237 | //Getting Coordinator from proxy |
||
238 | Teacher t = c.getCoordinator(); |
||
1500 | jmachado | 239 | if(t != null) |
240 | t.getName(); |
||
241 | else |
||
242 | { |
||
243 | logger.warn("Course does not have coordinator"); |
||
244 | } |
||
1496 | jmachado | 245 | |
246 | 1.5.0/docs/api/java/lang/String.html">String jsonCourse = getGensonCourse().serialize(c); |
||
247 | c.setJson(jsonCourse); |
||
248 | } |
||
249 | |||
250 | private 1.5.0/docs/api/java/lang/String.html">String generateHtmlCache(UserSession userSession, Course c) { |
||
251 | 1.5.0/docs/api/java/lang/String.html">String htmlTrasformationResult = null; |
||
252 | 5+0%2Fdocs%2Fapi+InputStream">InputStream stream;IRepositoryFile repositoryFile = repositoryService.load(c.getStudiesPlan(),userSession); |
||
253 | stream = repositoryFile.getInput(); |
||
254 | try |
||
255 | { |
||
256 | 5+0%2Fdocs%2Fapi+Document">Document dom = Dom4jUtil.parse(stream); |
||
257 | Map<String,Object> parameters = new HashMap<String,Object>(); |
||
258 | parameters.put("COURSE_SIGES_CODE",c.getCode()); |
||
259 | 1.5.0/docs/api/java/lang/String.html">String html = Dom4jUtil.styleDocument(dom, Globals.TEMPLATE_COURSE_XSL_PATH,parameters); |
||
260 | c.setCacheWebDocument(html); |
||
261 | } |
||
262 | catch (1.5.0/docs/api/java/lang/Exception.html">Exception e) |
||
263 | { |
||
264 | 1.5.0/docs/api/java/io/StringWriter.html">StringWriter writer = new 1.5.0/docs/api/java/io/StringWriter.html">StringWriter(); |
||
265 | 1.5.0/docs/api/java/io/PrintWriter.html">PrintWriter printWriter = new 1.5.0/docs/api/java/io/PrintWriter.html">PrintWriter(writer); |
||
266 | e.printStackTrace(printWriter); |
||
267 | htmlTrasformationResult = "<div class=\"error\"><pre>" + e.toString() + "\n" + printWriter.toString() + "</pre></div>"; |
||
268 | printWriter.close(); |
||
269 | } |
||
270 | try |
||
271 | { |
||
272 | stream.close(); |
||
273 | } |
||
274 | catch (1.5.0/docs/api/java/io/IOException.html">IOException e) |
||
275 | { |
||
276 | logger.error(e,e); |
||
277 | } |
||
278 | return htmlTrasformationResult; |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * ##NEW METHOD### |
||
283 | * Gera o XML normalizado para o JAXB a partir do XML importado do XML do plano XML quese usou no upload |
||
284 | * para garantir que está bem formado |
||
285 | * @param userSession |
||
286 | * @param c |
||
287 | * @return |
||
288 | * @throws JAXBException if XML is not weel formed |
||
289 | */ |
||
1505 | jmachado | 290 | private void generateXmlJaxbStudiesPlanVersionFromRepositoryOldPlanStream(UserSession userSession, Course c, boolean forceFichaCurricularUrlSet, 1.5.0/docs/api/java/lang/String.html">String systemUrlForUnitPrograms) throws JAXBException, TransformationException |
1496 | jmachado | 291 | { |
292 | CourseStudiesPlan courseStudiesPlan; |
||
1500 | jmachado | 293 | if(c.getStudiesPlan() == null || c.getStudiesPlan().trim().length() == 0) |
294 | { |
||
1505 | jmachado | 295 | //ESTE É O STREAM DO PLANO DE UPLOAD |
1500 | jmachado | 296 | logger.warn("Course does not have studies plan XML file stream to use in update"); |
297 | return; |
||
298 | } |
||
1496 | jmachado | 299 | |
300 | if(c.getStudiesPlans() == null || c.getStudiesPlans().size() == 0) |
||
301 | { |
||
302 | logger.info("Generating first study plan"); |
||
303 | courseStudiesPlan = DomainObjectFactory.createCourseStudiesPlanImpl(); |
||
304 | courseStudiesPlan.setVersion(1); |
||
305 | courseStudiesPlan.setVersionDescription("Auto gerado durante a importação de um XML com o plano de estudos a " + new 5+0%2Fdocs%2Fapi+Date">Date().toString()); |
||
306 | courseStudiesPlan.setCourse(c); |
||
307 | if(c.getStudiesPlans() == null) |
||
308 | c.setStudiesPlans(new HashSet<CourseStudiesPlan>()); |
||
309 | c.getStudiesPlans().add(courseStudiesPlan); |
||
310 | DaoFactory.getCourseStudiesPlanDaoImpl().save(courseStudiesPlan); |
||
311 | } |
||
312 | else |
||
313 | { |
||
314 | courseStudiesPlan = c.getStudiesPlans().iterator().next(); |
||
315 | logger.info("Updating Study Plan version " + courseStudiesPlan.getVersion()); |
||
316 | } |
||
317 | |||
318 | 5+0%2Fdocs%2Fapi+InputStream">InputStream stream; |
||
319 | IRepositoryFile repositoryFile = repositoryService.load(c.getStudiesPlan(),userSession); |
||
1502 | jmachado | 320 | long lastVersion = repositoryService.loadView(c.getStudiesPlan()).getLastVersion().getId(); |
1496 | jmachado | 321 | stream = repositoryFile.getInput(); |
1503 | jmachado | 322 | //TODO TIRAR |
323 | //JUST FOR DEBUG |
||
324 | try { |
||
325 | 1.5.0/docs/api/java/lang/System.html">System.out.println(StreamsUtils.readString(stream)); |
||
326 | stream.close(); |
||
327 | } catch (1.5.0/docs/api/java/io/IOException.html">IOException e) { |
||
328 | e.printStackTrace(); |
||
329 | } |
||
330 | repositoryFile = repositoryService.load(c.getStudiesPlan(),userSession); |
||
331 | stream = repositoryFile.getInput(); |
||
1496 | jmachado | 332 | |
333 | try { |
||
334 | JAXBContext jc = JAXBContext.newInstance(Curso.class); |
||
335 | Unmarshaller unmarshaller = jc.createUnmarshaller(); |
||
1503 | jmachado | 336 | //Just in case lets update SigesCode |
1496 | jmachado | 337 | Curso curso = (Curso) unmarshaller.unmarshal(stream); |
1503 | jmachado | 338 | curso.setSiges(c.getCode()); |
1505 | jmachado | 339 | curso.setNome(c.getName()); |
340 | curso.setDep(c.getArea()); |
||
1496 | jmachado | 341 | |
1505 | jmachado | 342 | //##NOVO PARA GERAR LINK SE NAO EXISTIR |
343 | generateAutoUrlFichasCurriculares(curso,systemUrlForUnitPrograms,forceFichaCurricularUrlSet); |
||
344 | |||
1496 | jmachado | 345 | Marshaller marshaller = jc.createMarshaller(); |
346 | marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); |
||
347 | 1.5.0/docs/api/java/io/StringWriter.html">StringWriter sw = new 1.5.0/docs/api/java/io/StringWriter.html">StringWriter(); |
||
348 | marshaller.marshal(curso, sw); |
||
349 | //SETTING XML in COURSE STUDIES PLAN |
||
350 | courseStudiesPlan.setXml(sw.toString()); |
||
1505 | jmachado | 351 | |
352 | |||
353 | 1.5.0/docs/api/java/lang/String.html">String json = getGensonPlanoEstudos().serialize(curso); |
||
1496 | jmachado | 354 | //SETTING JSON in COURSE STUDIES PLAN |
355 | courseStudiesPlan.setJson(json); |
||
356 | |||
357 | } catch (JAXBException e) { |
||
358 | logger.error(e,e); |
||
1502 | jmachado | 359 | 1.5.0/docs/api/java/lang/System.html">System.out.print("check XML for possible errors for repositoryStream:" + c.getStudiesPlan() + " file version: " + lastVersion); |
1496 | jmachado | 360 | throw e; |
361 | } catch (TransformationException e) { |
||
362 | logger.error(e, e); |
||
363 | throw e; |
||
364 | } catch (1.5.0/docs/api/java/io/IOException.html">IOException e) { |
||
365 | e.printStackTrace(); |
||
366 | } |
||
367 | try |
||
368 | { |
||
369 | stream.close(); |
||
370 | } |
||
371 | catch (1.5.0/docs/api/java/io/IOException.html">IOException e) |
||
372 | { |
||
373 | logger.error(e,e); |
||
374 | } |
||
375 | } |
||
376 | |||
1505 | jmachado | 377 | |
1506 | jmachado | 378 | /** |
379 | * Apenas é chamado quando se tenta injectar um programa a partir de um upload que foi feito |
||
380 | * pelo user ou pela sincronização remota |
||
381 | * @param curso |
||
382 | * @param systemUrl |
||
383 | * @param force |
||
384 | */ |
||
1496 | jmachado | 385 | private void generateAutoUrlFichasCurriculares(Curso curso,1.5.0/docs/api/java/lang/String.html">String systemUrl,boolean force) |
386 | { |
||
387 | for(Curso.Semestre s :curso.getSemestre()) |
||
388 | { |
||
389 | for(Curso.Semestre.Perfil p :s.getPerfil()) |
||
248 | jmachado | 390 | { |
1496 | jmachado | 391 | for(UnidadeType unidadeType : p.getUnidade()) |
392 | { |
||
393 | generateAutoUrlUnidade(unidadeType,systemUrl,curso,s,force); |
||
394 | } |
||
248 | jmachado | 395 | } |
1496 | jmachado | 396 | for(UnidadeType unidadeType : s.getUnidade()) |
214 | jmachado | 397 | { |
1496 | jmachado | 398 | generateAutoUrlUnidade(unidadeType,systemUrl,curso,s,force); |
214 | jmachado | 399 | } |
400 | } |
||
401 | } |
||
402 | |||
1496 | jmachado | 403 | private void generateAutoUrlUnidade(UnidadeType unidadeType,1.5.0/docs/api/java/lang/String.html">String systemUrl,Curso curso,Curso.Semestre semestre,boolean force) |
404 | { |
||
405 | if(force || unidadeType.getUrlFichaCurricular() == null || unidadeType.getUrlFichaCurricular().trim().length()==0) |
||
406 | { |
||
407 | logger.info("GENERATING FICHA CURRICULAR URL For " + unidadeType.getNome()); |
||
408 | 1.5.0/docs/api/java/lang/String.html">String url = systemUrl != null ? systemUrl : ""; |
||
1504 | jmachado | 409 | if(!url.endsWith("/")) |
1496 | jmachado | 410 | url = url + "/"; |
1504 | jmachado | 411 | |
1505 | jmachado | 412 | //Nao fornece o ano pois o servico irá assumir o ultimo |
1504 | jmachado | 413 | unidadeType.setUrlFichaCurricular(url + "startLoadCourseUnitProgramSiges.do?unitCode=" + unidadeType.getSiges() + "&courseCode=" + curso.getSiges() + "&semestre=" + semestre.getId()); |
414 | unidadeType.setUrlUnidadeCurricular(url + "startLoadCourseUnitSiges.do?unitCode=" + unidadeType.getSiges() + "&courseCode=" + curso.getSiges() + "&semestre=" + semestre.getId()); |
||
1496 | jmachado | 415 | } |
416 | } |
||
417 | |||
418 | |||
1505 | jmachado | 419 | private static Genson getGensonPlanoEstudos(){ |
1496 | jmachado | 420 | Genson genson = new Genson.Builder() |
421 | .exclude("class") |
||
1505 | jmachado | 422 | .exclude("siges") |
423 | .exclude("nome") |
||
424 | .exclude("dep") |
||
1496 | jmachado | 425 | .create(); |
426 | return genson; |
||
427 | } |
||
428 | |||
429 | |||
430 | |||
431 | private static Genson getGensonCourse(){ |
||
432 | Genson genson = new Genson.Builder() |
||
433 | .exclude(5+0%2Fdocs%2Fapi+Object">Object.class) |
||
434 | .setUseFields(false) |
||
435 | .setUseGettersAndSetters(true) |
||
436 | .setMethodFilter(VisibilityFilter.PACKAGE_PUBLIC) |
||
437 | .exclude("admin") |
||
438 | .exclude("autoBlock") |
||
439 | .exclude("autoBlockMode") |
||
440 | .exclude("manualBlock") |
||
441 | .exclude("newUser") |
||
442 | .exclude("student") |
||
443 | .exclude("superuser") |
||
444 | .exclude("superuserOrAdmin") |
||
445 | .exclude("teacher") |
||
446 | .exclude("unitCheck") |
||
447 | .exclude("id") |
||
448 | |||
449 | /* .exclude(Course.class) |
||
450 | .exclude(CourseImpl.class) |
||
451 | .exclude(GenericUser.class) |
||
452 | .exclude(User.class) |
||
453 | .exclude(UserImpl.class) |
||
454 | .exclude(Teacher.class) |
||
455 | .exclude(TeacherImpl.class) |
||
456 | .exclude(SigesUser.class) |
||
457 | .exclude(SigesUserImpl.class) |
||
458 | .exclude(GenericUser.class) |
||
459 | .exclude(GenericUserImpl.class) |
||
460 | */ |
||
461 | .exclude("id", Course.class) |
||
1505 | jmachado | 462 | .exclude("status", Course.class) |
1496 | jmachado | 463 | .include("degreeForJsonApi", CourseImpl.class) |
464 | .include("schoolForJsonApi", CourseImpl.class) |
||
465 | .include("statusForJsonApi", CourseImpl.class) |
||
466 | |||
467 | .include("name", Course.class) |
||
1505 | jmachado | 468 | .include("nameEn", Course.class) |
469 | .include("nameEs", Course.class) |
||
470 | .include("nameFr", Course.class) |
||
471 | .include("department", Course.class) |
||
472 | .exclude("active", CourseDepartment.class) |
||
473 | .include("sigla", CourseDepartment.class) |
||
474 | .include("name", CourseDepartment.class) |
||
475 | .include("nameEn", CourseDepartment.class) |
||
476 | .include("nameEs", CourseDepartment.class) |
||
477 | .include("nameFr", CourseDepartment.class) |
||
1496 | jmachado | 478 | .include("code", Course.class) |
1500 | jmachado | 479 | .include("validationRole", Course.class) |
480 | |||
1496 | jmachado | 481 | .include("courseComission", CourseImpl.class) |
482 | |||
483 | .include("name", GenericUser.class) |
||
484 | .include("email", GenericUser.class) |
||
485 | .include("sigesCode", SigesUser.class) |
||
486 | .include("coordinator", Course.class) |
||
487 | .create(); |
||
488 | |||
489 | return genson; |
||
490 | } |
||
491 | |||
492 | |||
493 | |||
214 | jmachado | 494 | public List<CourseView> loadCourses() throws ServiceException |
495 | { |
||
496 | List<Course> courses = DaoFactory.getCourseDaoImpl().findAllOrderByName(); |
||
497 | List<CourseView> courseViews = new ArrayList<CourseView>(); |
||
498 | for(Course c: courses) |
||
499 | { |
||
500 | CourseView courseView = new CourseView(c); |
||
501 | courseViews.add(courseView); |
||
502 | } |
||
503 | return courseViews; |
||
504 | } |
||
505 | |||
376 | jmachado | 506 | public List<CourseView> loadCoursesImportYearArea(1.5.0/docs/api/java/lang/String.html">String importYear, 1.5.0/docs/api/java/lang/String.html">String area) throws ServiceException |
507 | { |
||
1312 | jmachado | 508 | return loadCoursesImportYearAreaInstitution(importYear, area,null); |
509 | } |
||
510 | |||
511 | public List<CourseView> loadCoursesImportYearAreaInstitution(1.5.0/docs/api/java/lang/String.html">String importYear, 1.5.0/docs/api/java/lang/String.html">String area,1.5.0/docs/api/java/lang/String.html">String institutionCode) throws ServiceException |
||
512 | { |
||
513 | List<Course> courses = DaoFactory.getCourseDaoImpl().findAllOrderByName(importYear,area,null,institutionCode); |
||
376 | jmachado | 514 | List<CourseView> courseViews = new ArrayList<CourseView>(); |
515 | for(Course c: courses) |
||
516 | { |
||
517 | CourseView courseView = new CourseView(c); |
||
518 | courseViews.add(courseView); |
||
519 | } |
||
520 | return courseViews; |
||
521 | } |
||
522 | |||
249 | jmachado | 523 | public List<CourseView> loadCoursesImportYear() throws ServiceException |
524 | { |
||
995 | jmachado | 525 | 1.5.0/docs/api/java/lang/String.html">String importYearIntranet = DaoFactory.getConfigurationDaoImpl().getInterfaceImportYear(); |
249 | jmachado | 526 | List<Course> courses = DaoFactory.getCourseDaoImpl().findAllOrderByName(importYearIntranet); |
527 | List<CourseView> courseViews = new ArrayList<CourseView>(); |
||
528 | for(Course c: courses) |
||
529 | { |
||
530 | CourseView courseView = new CourseView(c); |
||
531 | courseViews.add(courseView); |
||
532 | } |
||
533 | return courseViews; |
||
534 | } |
||
417 | jmachado | 535 | public List<CourseView> loadCoursesImportYearByType(1.5.0/docs/api/java/lang/String.html">String type) throws ServiceException |
536 | { |
||
995 | jmachado | 537 | 1.5.0/docs/api/java/lang/String.html">String importYearIntranet = DaoFactory.getConfigurationDaoImpl().getInterfaceImportYear(); |
695 | jmachado | 538 | List<Course> courses = DaoFactory.getCourseDaoImpl().findAllOrderByNameEvenWithoutCourseUnit(importYearIntranet,null,type); |
417 | jmachado | 539 | List<CourseView> courseViews = new ArrayList<CourseView>(); |
540 | for(Course c: courses) |
||
541 | { |
||
542 | CourseView courseView = new CourseView(c); |
||
543 | courseViews.add(courseView); |
||
544 | } |
||
545 | return courseViews; |
||
546 | } |
||
214 | jmachado | 547 | |
790 | jmachado | 548 | public List<CourseView> loadActiveCoursesByType(1.5.0/docs/api/java/lang/String.html">String type) throws ServiceException |
549 | { |
||
995 | jmachado | 550 | 1.5.0/docs/api/java/lang/String.html">String importYearIntranet = DaoFactory.getConfigurationDaoImpl().getInterfaceImportYear(); |
790 | jmachado | 551 | List<Course> courses = DaoFactory.getCourseDaoImpl().findAllActiveOrderByNameEvenWithoutCourseUnit(importYearIntranet,null,type); |
552 | List<CourseView> courseViews = new ArrayList<CourseView>(); |
||
553 | for(Course c: courses) |
||
554 | { |
||
555 | CourseView courseView = new CourseView(c); |
||
556 | courseViews.add(courseView); |
||
557 | } |
||
558 | return courseViews; |
||
559 | } |
||
249 | jmachado | 560 | |
1500 | jmachado | 561 | |
562 | |||
563 | /** JSON API **/ |
||
564 | /** |
||
565 | * @SERVICE@ |
||
566 | * |
||
567 | * @param school |
||
568 | * @param type |
||
569 | * @return |
||
570 | * @throws JSONException |
||
571 | */ |
||
1496 | jmachado | 572 | public JSONObject getActiveCoursesForJsonApi(1.5.0/docs/api/java/lang/String.html">String school,1.5.0/docs/api/java/lang/String.html">String type) throws JSONException { |
573 | 1.5.0/docs/api/java/lang/String.html">String institutionalCode = null; |
||
574 | 1.5.0/docs/api/java/lang/String.html">String degree = null; |
||
575 | if(school != null && school.length() > 0) |
||
576 | institutionalCode = ConfigProperties.getProperty("institution.code.prefix.inverse." + school); |
||
249 | jmachado | 577 | |
1496 | jmachado | 578 | if(type != null && type.length() > 0) |
579 | degree = ConfigProperties.getProperty("course.inverse." + type); |
||
790 | jmachado | 580 | |
1496 | jmachado | 581 | List<Course> courses = DaoFactory.getCourseDaoImpl().findAllActiveOrderByNameEvenWithoutCourseUnit(institutionalCode,degree); |
582 | JSONObject coursesResponse = new JSONObject(); |
||
583 | |||
584 | JSONArray coursesArray = new JSONArray(); |
||
585 | for(Course cAux: courses) |
||
586 | { |
||
587 | CourseImpl c = (CourseImpl) DaoFactory.getCourseDaoImpl().narrow(cAux); |
||
588 | JSONObject courseJson = new JSONObject(); |
||
589 | courseJson.put("name",c.getName()); |
||
590 | courseJson.put("code",c.getCode()); |
||
591 | courseJson.put("schoolForJsonApi",c.getSchoolForJsonApi()); |
||
592 | courseJson.put("degreeForJsonApi",c.getDegreeForJsonApi()); |
||
593 | courseJson.put("statusForJsonApi",c.getStatusForJsonApi()); |
||
594 | courseJson.put("getDetailedInfoUrl","/wsjson/api?service=getCourse&code=" + c.getCode()); |
||
595 | coursesArray.put(courseJson); |
||
596 | |||
597 | } |
||
598 | coursesResponse.put("status","ok"); |
||
599 | coursesResponse.put("courses",coursesArray); |
||
600 | |||
601 | return coursesResponse; |
||
602 | } |
||
603 | |||
1500 | jmachado | 604 | /** |
605 | * |
||
1505 | jmachado | 606 | * * Serviço invocado para obter o JSON de um curso |
607 | * O JSON tem dois campos o courseInfo e o plano de estudos colocados separadamente |
||
1500 | jmachado | 608 | * |
1505 | jmachado | 609 | * Atenção o plano de estudos usado é o ultimo considerando o seu ID |
610 | * Nota: O plano de Estudos é uma classe persistente que tem apenas versão e descrição |
||
611 | * deverá ter como campo o XML e o JSON já gerados do plano de estudos que comporta |
||
612 | * |
||
1500 | jmachado | 613 | * @param code |
614 | * @return |
||
615 | * @throws JSONException |
||
616 | * @throws IOException |
||
617 | * @throws TransformationException |
||
618 | * @throws JAXBException |
||
619 | */ |
||
1498 | jmachado | 620 | public JSONObject getCourseDetailForJsonApi(1.5.0/docs/api/java/lang/String.html">String code) throws JSONException, 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException, JAXBException { |
1496 | jmachado | 621 | |
622 | Course course = DaoFactory.getCourseDaoImpl().findCourseByCode(code); |
||
623 | |||
624 | |||
625 | JSONObject coursesResponse = new JSONObject(); |
||
626 | |||
1498 | jmachado | 627 | if(course.getJson() == null) |
628 | { |
||
629 | logger.info("status JSON NOT EXIST FOR STUDIES PLAN IN THIS COURSE, will generate"); |
||
630 | new CoursesService().generateCourseJson(course); |
||
631 | } |
||
632 | |||
1496 | jmachado | 633 | if(course.getJson() != null) |
634 | { |
||
635 | JSONObject courseObj = new JSONObject(course.getJson()); |
||
636 | coursesResponse.put("courseInfo",courseObj); |
||
1505 | jmachado | 637 | //Este caso apenas se dá se o plano nunca tiver sido editado ou sincronizado |
638 | //Nesse caso o sistema irá tentar obtê-lo da stream do repositorio |
||
1500 | jmachado | 639 | if(course.getStudiesPlans() == null || course.getStudiesPlans().size() == 0) |
1498 | jmachado | 640 | { |
1505 | jmachado | 641 | logger.info("status JSON NOT EXIST FOR STUDIES PLAN IN THIS COURSE, will try generate from studies plan OLD Stream"); |
1498 | jmachado | 642 | UserSession userSession = DomainObjectFactory.createUserSessionImpl(); |
643 | userSession.setUser(DaoFactory.getUserDaoImpl().load(new 1.5.0/docs/api/java/lang/Long.html">Long(1))); |
||
1505 | jmachado | 644 | new CoursesService().generateXmlJaxbStudiesPlanVersionFromRepositoryOldPlanStream(userSession, course, false, null); |
1498 | jmachado | 645 | } |
646 | |||
1505 | jmachado | 647 | if(course.getStudiesPlans() != null ) |
1496 | jmachado | 648 | { |
649 | CourseStudiesPlan studiesPlan = course.getStudiesPlans().iterator().next(); |
||
1505 | jmachado | 650 | JSONObject studiesPlanObj; |
651 | if(studiesPlan.getJson() != null) |
||
652 | { |
||
653 | studiesPlanObj = new JSONObject(studiesPlan.getJson()); |
||
654 | studiesPlanObj.put("version",studiesPlan.getVersion()); |
||
655 | coursesResponse.put("courseStudiesPlan",studiesPlanObj); |
||
656 | } |
||
657 | else |
||
658 | { |
||
659 | studiesPlanObj = new JSONObject(); |
||
660 | studiesPlanObj.put("fault","Zero contents for this version"); |
||
661 | studiesPlanObj.put("version",studiesPlan.getVersion()); |
||
662 | coursesResponse.put("courseStudiesPlan",studiesPlanObj); |
||
663 | } |
||
1496 | jmachado | 664 | coursesResponse.put("courseStudiesPlan",studiesPlanObj); |
1505 | jmachado | 665 | |
1496 | jmachado | 666 | } |
667 | else |
||
668 | { |
||
669 | coursesResponse.put("status","JSON NOT EXIST FOR STUDIES PLAN IN THIS COURSE"); |
||
670 | } |
||
671 | } |
||
672 | else |
||
673 | { |
||
1505 | jmachado | 674 | coursesResponse.put("status","JSON NOT EXIST FOR COURSE, PLEASE OPEN AND SAVE COURSE IN ADMINISTRATION"); |
1496 | jmachado | 675 | } |
676 | return coursesResponse; |
||
677 | } |
||
678 | |||
1500 | jmachado | 679 | /** |
680 | * @SERVICE@ |
||
681 | * |
||
682 | * @param code |
||
683 | * @return |
||
684 | * @throws JSONException |
||
685 | */ |
||
1502 | jmachado | 686 | public 1.5.0/docs/api/java/lang/String.html">String getCourseStudiesPlanXml(1.5.0/docs/api/java/lang/String.html">String code,1.5.0/docs/api/java/lang/String.html">String renew) throws JSONException { |
1496 | jmachado | 687 | |
688 | Course course = DaoFactory.getCourseDaoImpl().findCourseByCode(code); |
||
1501 | jmachado | 689 | |
1502 | jmachado | 690 | if(renew != null || course.getStudiesPlans() == null || course.getStudiesPlans().size() == 0) |
1501 | jmachado | 691 | { |
692 | logger.info("status JSON NOT EXIST FOR STUDIES PLAN IN THIS COURSE, will generate"); |
||
693 | UserSession userSession = DomainObjectFactory.createUserSessionImpl(); |
||
694 | userSession.setUser(DaoFactory.getUserDaoImpl().load(new 1.5.0/docs/api/java/lang/Long.html">Long(1))); |
||
695 | try { |
||
1505 | jmachado | 696 | generateXmlJaxbStudiesPlanVersionFromRepositoryOldPlanStream(userSession, course, false, null); |
1501 | jmachado | 697 | } catch (JAXBException e) { |
698 | logger.error(e,e); |
||
699 | return "<error>" + e.toString() + ". see log for details</error>"; |
||
700 | } catch (TransformationException e) { |
||
701 | logger.error(e, e); |
||
702 | return "<error>" + e.toString() + ". see log for details</error>"; |
||
703 | } |
||
704 | } |
||
705 | |||
1496 | jmachado | 706 | if(course.getStudiesPlans() != null && course.getStudiesPlans().size() > 0) |
707 | { |
||
708 | return course.getStudiesPlans().iterator().next().getXml(); |
||
709 | } |
||
710 | return "<error>Does not exixt</error>"; |
||
711 | |||
712 | } |
||
713 | |||
714 | |||
1500 | jmachado | 715 | /** |
716 | * @SERVICE@ |
||
717 | * |
||
718 | * @param systemUrl |
||
719 | * @param setActive |
||
720 | * @return |
||
721 | * @throws IOException |
||
722 | * @throws JSONException |
||
723 | * @throws TransformationException |
||
724 | * @throws JAXBException |
||
725 | */ |
||
1496 | jmachado | 726 | |
1502 | jmachado | 727 | public 1.5.0/docs/api/java/lang/String.html">String sincronizeCoursesStudiesPlans(1.5.0/docs/api/java/lang/String.html">String systemUrl,boolean setActive,UserSession sess) throws 1.5.0/docs/api/java/io/IOException.html">IOException, JSONException, TransformationException, JAXBException { |
1496 | jmachado | 728 | |
1497 | jmachado | 729 | 1.5.0/docs/api/java/lang/StringBuilder.html">StringBuilder log = new 1.5.0/docs/api/java/lang/StringBuilder.html">StringBuilder(); |
730 | 1.5.0/docs/api/java/net/URL.html">URL url = new 1.5.0/docs/api/java/net/URL.html">URL(systemUrl + "/wsjson/api?service=listCourses"); |
||
1496 | jmachado | 731 | 5+0%2Fdocs%2Fapi+InputStream">InputStream is = url.openStream(); |
732 | 1.5.0/docs/api/java/lang/String.html">String str = StreamsUtils.readString(is); |
||
733 | JSONObject obj = new JSONObject(str); |
||
734 | JSONArray courses = obj.getJSONArray("courses"); |
||
735 | for(int i = 0; i < courses.length();i++) |
||
736 | { |
||
1502 | jmachado | 737 | 1.5.0/docs/api/java/lang/String.html">String code = ""; |
738 | try{ |
||
739 | JSONObject course = courses.getJSONObject(i); |
||
740 | code = course.getString("code"); |
||
741 | Course c = DaoFactory.getCourseDaoImpl().findCourseByCode(code); |
||
742 | if(c == null) |
||
743 | { |
||
744 | 1.5.0/docs/api/java/lang/String.html">String msg = "SKIPING - Course " + code + " " + course.getString("name") + " does not exist in this system"; |
||
745 | log.append("<info>" + msg+"</info>"); |
||
746 | logger.info(msg); |
||
747 | } |
||
748 | else |
||
749 | { |
||
750 | 1.5.0/docs/api/java/lang/String.html">String msg = "UPDATING - Course " + code + " " + course.getString("name") + " exist in this system"; |
||
751 | log.append("<info>" + msg+"</info>"); |
||
752 | logger.info(msg); |
||
1500 | jmachado | 753 | |
1505 | jmachado | 754 | //#############UPDATING Course Comission Members |
755 | updateCourseComissionMembers(systemUrl, code, c); |
||
756 | |||
1502 | jmachado | 757 | //#############UPDATING STUDIES PLAN |
758 | updateStudiesPlanFromRemoteSystem(systemUrl, setActive, log, course, code, c); |
||
1500 | jmachado | 759 | |
1505 | jmachado | 760 | |
1502 | jmachado | 761 | } |
1500 | jmachado | 762 | } |
1502 | jmachado | 763 | catch(1.5.0/docs/api/java/lang/Throwable.html">Throwable e) |
764 | { |
||
765 | logger.error("UPDATE COURSE: " + i + " code: " + code + " FAILED"); |
||
766 | logger.error(e,e); |
||
767 | } |
||
1500 | jmachado | 768 | } |
769 | return log.toString(); |
||
770 | |||
771 | } |
||
772 | |||
1505 | jmachado | 773 | private void updateStudiesPlanFromRemoteSystem(1.5.0/docs/api/java/lang/String.html">String systemUrl, boolean setActive, 1.5.0/docs/api/java/lang/StringBuilder.html">StringBuilder log, JSONObject course, 1.5.0/docs/api/java/lang/String.html">String code, Course c) throws 1.5.0/docs/api/java/io/IOException.html">IOException, JSONException, TransformationException, JAXBException { |
774 | 1.5.0/docs/api/java/lang/String.html">String msg; |
||
775 | 5+0%2Fdocs%2Fapi+InputStream">InputStream stream = new 1.5.0/docs/api/java/net/URL.html">URL(systemUrl + "/wsjson/api?service=getStudiesPlanXml&code=" + code + "&renew=true").openStream(); |
||
776 | 1.5.0/docs/api/java/lang/String.html">String studiesPlan = StreamsUtils.readString(stream); |
||
777 | int len = studiesPlan.length(); |
||
778 | if(studiesPlan == null || studiesPlan.trim().length() == 0 || studiesPlan.contains("<error>")) |
||
779 | { |
||
780 | msg = "Course " + code + " " + course.getString("name") + " dont has studies plan"; |
||
781 | log.append("<warn>" + msg+"</warn>"); |
||
782 | logger.warn(msg); |
||
783 | } |
||
784 | else |
||
785 | { |
||
786 | msg = "Found studies plan for " + code + " " + course.getString("name") + " will update "; |
||
787 | log.append("<info>" + msg+"</info>"); |
||
788 | logger.info(msg); |
||
789 | if(setActive) |
||
790 | { |
||
791 | msg = "Setting course to active"; |
||
792 | log.append("<info>" + msg+"</info>"); |
||
793 | logger.info(msg); |
||
794 | c.setStatus(true); |
||
795 | } |
||
796 | //System.out.println(studiesPlan); |
||
797 | msg = "GENERATING COURSE JSON ...."; |
||
798 | log.append("<info>" + msg+"</info>"); |
||
799 | logger.info(msg); |
||
800 | new CoursesService().generateCourseJson(c); |
||
801 | |||
802 | msg="GENERATING COURSE STUDIES PLAN JSON ...."; |
||
803 | log.append("<info>" + msg+"</info>"); |
||
804 | logger.info(msg); |
||
805 | stream.close(); |
||
806 | stream = new 1.5.0/docs/api/java/net/URL.html">URL(systemUrl + "/wsjson/api?service=getStudiesPlanXml&code=" + code).openStream(); |
||
807 | UserSession userSession = DomainObjectFactory.createUserSessionImpl(); |
||
808 | userSession.setUser(DaoFactory.getUserDaoImpl().load(new 1.5.0/docs/api/java/lang/Long.html">Long(1))); |
||
809 | new CoursesService().uploadStudiesPlan(stream, "curso_" + code + ".xml", len,"appication/xml", userSession,c,true,systemUrl); |
||
810 | } |
||
811 | } |
||
812 | |||
813 | |||
1500 | jmachado | 814 | /** |
815 | * Update courseComission Members |
||
816 | * @param systemUrl |
||
817 | * @param code |
||
818 | * @param c |
||
819 | * @throws IOException |
||
820 | * @throws JSONException |
||
821 | */ |
||
822 | private void updateCourseComissionMembers(1.5.0/docs/api/java/lang/String.html">String systemUrl, 1.5.0/docs/api/java/lang/String.html">String code, Course c) throws 1.5.0/docs/api/java/io/IOException.html">IOException, JSONException { |
||
823 | 1.5.0/docs/api/java/net/URL.html">URL urlCourseDetails = new 1.5.0/docs/api/java/net/URL.html">URL(systemUrl + "/wsjson/api?service=getCourse&code=" + code); |
||
824 | 5+0%2Fdocs%2Fapi+InputStream">InputStream isCourseDetails = urlCourseDetails.openStream(); |
||
825 | 1.5.0/docs/api/java/lang/String.html">String strCourseDetails = StreamsUtils.readString(isCourseDetails); |
||
826 | JSONObject objCourseDetails = new JSONObject(strCourseDetails); |
||
827 | 1.5.0/docs/api/java/lang/String.html">String validationRole = objCourseDetails.getJSONObject("courseInfo").getString("validationRole"); |
||
828 | |||
829 | if(validationRole == null) |
||
830 | { |
||
831 | logger.info("validationRole is not defined"); |
||
832 | } |
||
833 | else |
||
834 | { |
||
835 | logger.info("found validationRole: " + validationRole); |
||
836 | c.setValidationRole(validationRole); |
||
837 | |||
838 | JSONObject coordinator = objCourseDetails.getJSONObject("courseInfo").getJSONObject("coordinator"); |
||
839 | JSONArray courseComission = objCourseDetails.getJSONObject("courseInfo").getJSONArray("courseComission"); |
||
840 | |||
841 | Teacher coordinatorPersistent = findPersonFromCourseDetails(coordinator); |
||
842 | if(coordinatorPersistent == null) |
||
843 | { |
||
844 | logger.warn("Coordinator does not exist in this system "); |
||
845 | } |
||
846 | else |
||
847 | { |
||
848 | c.setCoordinator(coordinatorPersistent); |
||
849 | } |
||
850 | for(int j = 0 ; j < courseComission.length(); j++) |
||
851 | { |
||
852 | JSONObject memberComission = courseComission.getJSONObject(j); |
||
853 | Teacher memberPersistent = findPersonFromCourseDetails(memberComission); |
||
854 | if(memberPersistent == null) |
||
1496 | jmachado | 855 | { |
1500 | jmachado | 856 | logger.info("Member does not exist in this system "); |
1496 | jmachado | 857 | } |
858 | else |
||
859 | { |
||
1500 | jmachado | 860 | logger.info("Adding role of course comission member"); |
861 | if(!memberPersistent.hasRole(validationRole)) |
||
1497 | jmachado | 862 | { |
1500 | jmachado | 863 | memberPersistent.addRole(validationRole); |
1497 | jmachado | 864 | } |
1500 | jmachado | 865 | } |
1497 | jmachado | 866 | |
1496 | jmachado | 867 | } |
868 | } |
||
1500 | jmachado | 869 | } |
1497 | jmachado | 870 | |
1500 | jmachado | 871 | private Teacher findPersonFromCourseDetails(JSONObject coordinator) { |
872 | int code; |
||
873 | try { |
||
874 | if(coordinator.has("sigesCode")) |
||
875 | { |
||
876 | code = coordinator.getInt("sigesCode"); |
||
877 | } |
||
878 | else |
||
879 | { |
||
880 | logger.warn("there is no sigesCode for this person " + coordinator.toString()); |
||
881 | return null; |
||
882 | } |
||
883 | } catch (JSONException e){ |
||
884 | return null; |
||
885 | } catch (1.5.0/docs/api/java/lang/NumberFormatException.html">NumberFormatException e){ |
||
886 | return null; |
||
887 | } |
||
888 | return DaoFactory.getTeacherDaoImpl().loadBySigesCode(code); |
||
1496 | jmachado | 889 | } |
890 | |||
1505 | jmachado | 891 | |
892 | |||
893 | /* |
||
894 | * Studies Plans Administration Services |
||
895 | * |
||
896 | */ |
||
897 | public void addNewStudiesPlan(long courseId,CourseStudiesPlan studiesPlan,UserSession session) |
||
898 | { |
||
899 | Course c = DaoFactory.getCourseDaoImpl().load(courseId); |
||
900 | studiesPlan.setCourse(c); |
||
901 | c.getStudiesPlans().add(studiesPlan); |
||
902 | DaoFactory.getCourseStudiesPlanDaoImpl().save(studiesPlan); |
||
903 | } |
||
904 | |||
905 | public void cloneVersionFrom(long sourcePlanId, long targetPlanId, long courseId, UserSession session) |
||
906 | { |
||
907 | Course course = DaoFactory.getCourseDaoImpl().load(courseId); |
||
908 | CourseStudiesPlan source = null; |
||
909 | CourseStudiesPlan target = null; |
||
910 | for(CourseStudiesPlan plan: course.getStudiesPlans()) |
||
1500 | jmachado | 911 | { |
1505 | jmachado | 912 | if(plan.getId() == sourcePlanId) |
913 | source = plan; |
||
914 | else if(plan.getId() == targetPlanId) |
||
915 | target = plan; |
||
1500 | jmachado | 916 | } |
1505 | jmachado | 917 | target.setXml(source.getXml()); |
918 | target.setJson(source.getJson()); |
||
919 | } |
||
920 | public Curso loadCursoPlanoFromXml(1.5.0/docs/api/java/lang/String.html">String xml) |
||
921 | { |
||
922 | try { |
||
923 | if(xml != null) |
||
1500 | jmachado | 924 | { |
1505 | jmachado | 925 | JAXBContext jc = JAXBContext.newInstance(Curso.class); |
926 | Unmarshaller unmarshaller = jc.createUnmarshaller(); |
||
927 | Curso curso = (Curso) unmarshaller.unmarshal(new 1.5.0/docs/api/java/io/StringReader.html">StringReader(xml)); |
||
928 | return curso; |
||
1500 | jmachado | 929 | } |
1505 | jmachado | 930 | return null; |
931 | } catch (JAXBException e) { |
||
932 | logger.error(e,e); |
||
933 | return null; |
||
934 | } |
||
935 | } |
||
1496 | jmachado | 936 | |
1505 | jmachado | 937 | /** |
938 | * Persist the edited studies plan |
||
939 | * Updates studiesPlanVersion |
||
940 | * Updates studiesPlanVersionDescription |
||
941 | * Updates PlanoEstudos XML and JSON |
||
942 | * |
||
943 | * @param courseId |
||
944 | * @param coursePlanId |
||
945 | * @param planoEditado |
||
946 | * @param courseStudiesPlanEditado |
||
947 | * @return |
||
948 | */ |
||
949 | public Course savePlanoEstudosEditado(long courseId, long coursePlanId, Curso planoEditado, CourseStudiesPlan courseStudiesPlanEditado,UserSession session) |
||
950 | { |
||
951 | try { |
||
952 | Course course = DaoFactory.getCourseDaoImpl().load(courseId); |
||
953 | |||
954 | for(CourseStudiesPlan courseStudiesPlanPersistente: course.getStudiesPlans()) |
||
955 | { |
||
956 | if(courseStudiesPlanPersistente.getId() == coursePlanId) |
||
957 | { |
||
958 | courseStudiesPlanPersistente.setVersion(courseStudiesPlanEditado.getVersion()); |
||
959 | courseStudiesPlanPersistente.setVersionDescription(courseStudiesPlanEditado.getVersionDescription()); |
||
960 | |||
961 | //Garante-se mas depois não vai para o JSON |
||
962 | planoEditado.setSiges(course.getCode());//GARANTIR QUE O CODIGO SIGEST ESTA CORRECTO |
||
963 | planoEditado.setNome(course.getName()); |
||
964 | planoEditado.setDep(course.getArea()); |
||
965 | //planoEditado.setDepDesc(""); |
||
966 | //planoEditado.setDepDescEn(""); |
||
967 | //planoEditado.setDepDescEs(""); |
||
968 | //planoEditado.setDepDescFr(""); |
||
969 | JAXBContext jc = JAXBContext.newInstance(Curso.class); |
||
970 | Marshaller marshaller = jc.createMarshaller(); |
||
971 | marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); |
||
972 | 1.5.0/docs/api/java/io/StringWriter.html">StringWriter xml = new 1.5.0/docs/api/java/io/StringWriter.html">StringWriter(); |
||
973 | marshaller.marshal(planoEditado,xml); |
||
974 | |||
975 | courseStudiesPlanPersistente.setXml(xml.toString()); |
||
976 | 1.5.0/docs/api/java/lang/String.html">String json = getGensonPlanoEstudos().serialize(planoEditado); |
||
977 | //SETTING JSON in COURSE STUDIES PLAN |
||
978 | courseStudiesPlanPersistente.setJson(json); |
||
979 | break; |
||
980 | } |
||
981 | } |
||
982 | return course; |
||
983 | } catch (JAXBException e) { |
||
984 | logger.error(e,e); |
||
985 | return null; |
||
986 | } catch (TransformationException e) { |
||
987 | logger.error(e,e); |
||
988 | return null; |
||
989 | } catch (1.5.0/docs/api/java/io/IOException.html">IOException e) { |
||
990 | logger.error(e,e); |
||
991 | return null; |
||
1500 | jmachado | 992 | } |
993 | } |
||
1496 | jmachado | 994 | |
1500 | jmachado | 995 | |
1505 | jmachado | 996 | |
997 | public List<CourseDepartment> loadDepartments() |
||
998 | { |
||
999 | List<CourseDepartment> departments = DaoFactory.getCourseDepartmentDaoImpl().findAll(); |
||
1000 | for(CourseDepartment dep: departments) |
||
1001 | dep.getName(); |
||
1002 | return departments; |
||
1003 | } |
||
1004 | |||
1005 | |||
214 | jmachado | 1006 | } |