Rev 1504 | 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 | |
378 | |||
1496 | jmachado | 379 | private void generateAutoUrlFichasCurriculares(Curso curso,1.5.0/docs/api/java/lang/String.html">String systemUrl,boolean force) |
380 | { |
||
381 | for(Curso.Semestre s :curso.getSemestre()) |
||
382 | { |
||
383 | for(Curso.Semestre.Perfil p :s.getPerfil()) |
||
248 | jmachado | 384 | { |
1496 | jmachado | 385 | for(UnidadeType unidadeType : p.getUnidade()) |
386 | { |
||
387 | generateAutoUrlUnidade(unidadeType,systemUrl,curso,s,force); |
||
388 | } |
||
248 | jmachado | 389 | } |
1496 | jmachado | 390 | for(UnidadeType unidadeType : s.getUnidade()) |
214 | jmachado | 391 | { |
1496 | jmachado | 392 | generateAutoUrlUnidade(unidadeType,systemUrl,curso,s,force); |
214 | jmachado | 393 | } |
394 | } |
||
395 | } |
||
396 | |||
1496 | jmachado | 397 | private void generateAutoUrlUnidade(UnidadeType unidadeType,1.5.0/docs/api/java/lang/String.html">String systemUrl,Curso curso,Curso.Semestre semestre,boolean force) |
398 | { |
||
399 | if(force || unidadeType.getUrlFichaCurricular() == null || unidadeType.getUrlFichaCurricular().trim().length()==0) |
||
400 | { |
||
401 | logger.info("GENERATING FICHA CURRICULAR URL For " + unidadeType.getNome()); |
||
402 | 1.5.0/docs/api/java/lang/String.html">String url = systemUrl != null ? systemUrl : ""; |
||
1504 | jmachado | 403 | if(!url.endsWith("/")) |
1496 | jmachado | 404 | url = url + "/"; |
1504 | jmachado | 405 | |
1505 | jmachado | 406 | //Nao fornece o ano pois o servico irá assumir o ultimo |
1504 | jmachado | 407 | unidadeType.setUrlFichaCurricular(url + "startLoadCourseUnitProgramSiges.do?unitCode=" + unidadeType.getSiges() + "&courseCode=" + curso.getSiges() + "&semestre=" + semestre.getId()); |
408 | unidadeType.setUrlUnidadeCurricular(url + "startLoadCourseUnitSiges.do?unitCode=" + unidadeType.getSiges() + "&courseCode=" + curso.getSiges() + "&semestre=" + semestre.getId()); |
||
1496 | jmachado | 409 | } |
410 | } |
||
411 | |||
412 | |||
1505 | jmachado | 413 | private static Genson getGensonPlanoEstudos(){ |
1496 | jmachado | 414 | Genson genson = new Genson.Builder() |
415 | .exclude("class") |
||
1505 | jmachado | 416 | .exclude("siges") |
417 | .exclude("nome") |
||
418 | .exclude("dep") |
||
1496 | jmachado | 419 | .create(); |
420 | return genson; |
||
421 | } |
||
422 | |||
423 | |||
424 | |||
425 | private static Genson getGensonCourse(){ |
||
426 | Genson genson = new Genson.Builder() |
||
427 | .exclude(5+0%2Fdocs%2Fapi+Object">Object.class) |
||
428 | .setUseFields(false) |
||
429 | .setUseGettersAndSetters(true) |
||
430 | .setMethodFilter(VisibilityFilter.PACKAGE_PUBLIC) |
||
431 | .exclude("admin") |
||
432 | .exclude("autoBlock") |
||
433 | .exclude("autoBlockMode") |
||
434 | .exclude("manualBlock") |
||
435 | .exclude("newUser") |
||
436 | .exclude("student") |
||
437 | .exclude("superuser") |
||
438 | .exclude("superuserOrAdmin") |
||
439 | .exclude("teacher") |
||
440 | .exclude("unitCheck") |
||
441 | .exclude("id") |
||
442 | |||
443 | /* .exclude(Course.class) |
||
444 | .exclude(CourseImpl.class) |
||
445 | .exclude(GenericUser.class) |
||
446 | .exclude(User.class) |
||
447 | .exclude(UserImpl.class) |
||
448 | .exclude(Teacher.class) |
||
449 | .exclude(TeacherImpl.class) |
||
450 | .exclude(SigesUser.class) |
||
451 | .exclude(SigesUserImpl.class) |
||
452 | .exclude(GenericUser.class) |
||
453 | .exclude(GenericUserImpl.class) |
||
454 | */ |
||
455 | .exclude("id", Course.class) |
||
1505 | jmachado | 456 | .exclude("status", Course.class) |
1496 | jmachado | 457 | .include("degreeForJsonApi", CourseImpl.class) |
458 | .include("schoolForJsonApi", CourseImpl.class) |
||
459 | .include("statusForJsonApi", CourseImpl.class) |
||
460 | |||
461 | .include("name", Course.class) |
||
1505 | jmachado | 462 | .include("nameEn", Course.class) |
463 | .include("nameEs", Course.class) |
||
464 | .include("nameFr", Course.class) |
||
465 | .include("department", Course.class) |
||
466 | .exclude("active", CourseDepartment.class) |
||
467 | .include("sigla", CourseDepartment.class) |
||
468 | .include("name", CourseDepartment.class) |
||
469 | .include("nameEn", CourseDepartment.class) |
||
470 | .include("nameEs", CourseDepartment.class) |
||
471 | .include("nameFr", CourseDepartment.class) |
||
1496 | jmachado | 472 | .include("code", Course.class) |
1500 | jmachado | 473 | .include("validationRole", Course.class) |
474 | |||
1496 | jmachado | 475 | .include("courseComission", CourseImpl.class) |
476 | |||
477 | .include("name", GenericUser.class) |
||
478 | .include("email", GenericUser.class) |
||
479 | .include("sigesCode", SigesUser.class) |
||
480 | .include("coordinator", Course.class) |
||
481 | .create(); |
||
482 | |||
483 | return genson; |
||
484 | } |
||
485 | |||
486 | |||
487 | |||
214 | jmachado | 488 | public List<CourseView> loadCourses() throws ServiceException |
489 | { |
||
490 | List<Course> courses = DaoFactory.getCourseDaoImpl().findAllOrderByName(); |
||
491 | List<CourseView> courseViews = new ArrayList<CourseView>(); |
||
492 | for(Course c: courses) |
||
493 | { |
||
494 | CourseView courseView = new CourseView(c); |
||
495 | courseViews.add(courseView); |
||
496 | } |
||
497 | return courseViews; |
||
498 | } |
||
499 | |||
376 | jmachado | 500 | 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 |
501 | { |
||
1312 | jmachado | 502 | return loadCoursesImportYearAreaInstitution(importYear, area,null); |
503 | } |
||
504 | |||
505 | 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 |
||
506 | { |
||
507 | List<Course> courses = DaoFactory.getCourseDaoImpl().findAllOrderByName(importYear,area,null,institutionCode); |
||
376 | jmachado | 508 | List<CourseView> courseViews = new ArrayList<CourseView>(); |
509 | for(Course c: courses) |
||
510 | { |
||
511 | CourseView courseView = new CourseView(c); |
||
512 | courseViews.add(courseView); |
||
513 | } |
||
514 | return courseViews; |
||
515 | } |
||
516 | |||
249 | jmachado | 517 | public List<CourseView> loadCoursesImportYear() throws ServiceException |
518 | { |
||
995 | jmachado | 519 | 1.5.0/docs/api/java/lang/String.html">String importYearIntranet = DaoFactory.getConfigurationDaoImpl().getInterfaceImportYear(); |
249 | jmachado | 520 | List<Course> courses = DaoFactory.getCourseDaoImpl().findAllOrderByName(importYearIntranet); |
521 | List<CourseView> courseViews = new ArrayList<CourseView>(); |
||
522 | for(Course c: courses) |
||
523 | { |
||
524 | CourseView courseView = new CourseView(c); |
||
525 | courseViews.add(courseView); |
||
526 | } |
||
527 | return courseViews; |
||
528 | } |
||
417 | jmachado | 529 | public List<CourseView> loadCoursesImportYearByType(1.5.0/docs/api/java/lang/String.html">String type) throws ServiceException |
530 | { |
||
995 | jmachado | 531 | 1.5.0/docs/api/java/lang/String.html">String importYearIntranet = DaoFactory.getConfigurationDaoImpl().getInterfaceImportYear(); |
695 | jmachado | 532 | List<Course> courses = DaoFactory.getCourseDaoImpl().findAllOrderByNameEvenWithoutCourseUnit(importYearIntranet,null,type); |
417 | jmachado | 533 | List<CourseView> courseViews = new ArrayList<CourseView>(); |
534 | for(Course c: courses) |
||
535 | { |
||
536 | CourseView courseView = new CourseView(c); |
||
537 | courseViews.add(courseView); |
||
538 | } |
||
539 | return courseViews; |
||
540 | } |
||
214 | jmachado | 541 | |
790 | jmachado | 542 | public List<CourseView> loadActiveCoursesByType(1.5.0/docs/api/java/lang/String.html">String type) throws ServiceException |
543 | { |
||
995 | jmachado | 544 | 1.5.0/docs/api/java/lang/String.html">String importYearIntranet = DaoFactory.getConfigurationDaoImpl().getInterfaceImportYear(); |
790 | jmachado | 545 | List<Course> courses = DaoFactory.getCourseDaoImpl().findAllActiveOrderByNameEvenWithoutCourseUnit(importYearIntranet,null,type); |
546 | List<CourseView> courseViews = new ArrayList<CourseView>(); |
||
547 | for(Course c: courses) |
||
548 | { |
||
549 | CourseView courseView = new CourseView(c); |
||
550 | courseViews.add(courseView); |
||
551 | } |
||
552 | return courseViews; |
||
553 | } |
||
249 | jmachado | 554 | |
1500 | jmachado | 555 | |
556 | |||
557 | /** JSON API **/ |
||
558 | /** |
||
559 | * @SERVICE@ |
||
560 | * |
||
561 | * @param school |
||
562 | * @param type |
||
563 | * @return |
||
564 | * @throws JSONException |
||
565 | */ |
||
1496 | jmachado | 566 | 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 { |
567 | 1.5.0/docs/api/java/lang/String.html">String institutionalCode = null; |
||
568 | 1.5.0/docs/api/java/lang/String.html">String degree = null; |
||
569 | if(school != null && school.length() > 0) |
||
570 | institutionalCode = ConfigProperties.getProperty("institution.code.prefix.inverse." + school); |
||
249 | jmachado | 571 | |
1496 | jmachado | 572 | if(type != null && type.length() > 0) |
573 | degree = ConfigProperties.getProperty("course.inverse." + type); |
||
790 | jmachado | 574 | |
1496 | jmachado | 575 | List<Course> courses = DaoFactory.getCourseDaoImpl().findAllActiveOrderByNameEvenWithoutCourseUnit(institutionalCode,degree); |
576 | JSONObject coursesResponse = new JSONObject(); |
||
577 | |||
578 | JSONArray coursesArray = new JSONArray(); |
||
579 | for(Course cAux: courses) |
||
580 | { |
||
581 | CourseImpl c = (CourseImpl) DaoFactory.getCourseDaoImpl().narrow(cAux); |
||
582 | JSONObject courseJson = new JSONObject(); |
||
583 | courseJson.put("name",c.getName()); |
||
584 | courseJson.put("code",c.getCode()); |
||
585 | courseJson.put("schoolForJsonApi",c.getSchoolForJsonApi()); |
||
586 | courseJson.put("degreeForJsonApi",c.getDegreeForJsonApi()); |
||
587 | courseJson.put("statusForJsonApi",c.getStatusForJsonApi()); |
||
588 | courseJson.put("getDetailedInfoUrl","/wsjson/api?service=getCourse&code=" + c.getCode()); |
||
589 | coursesArray.put(courseJson); |
||
590 | |||
591 | } |
||
592 | coursesResponse.put("status","ok"); |
||
593 | coursesResponse.put("courses",coursesArray); |
||
594 | |||
595 | return coursesResponse; |
||
596 | } |
||
597 | |||
1500 | jmachado | 598 | /** |
599 | * |
||
1505 | jmachado | 600 | * * Serviço invocado para obter o JSON de um curso |
601 | * O JSON tem dois campos o courseInfo e o plano de estudos colocados separadamente |
||
1500 | jmachado | 602 | * |
1505 | jmachado | 603 | * Atenção o plano de estudos usado é o ultimo considerando o seu ID |
604 | * Nota: O plano de Estudos é uma classe persistente que tem apenas versão e descrição |
||
605 | * deverá ter como campo o XML e o JSON já gerados do plano de estudos que comporta |
||
606 | * |
||
1500 | jmachado | 607 | * @param code |
608 | * @return |
||
609 | * @throws JSONException |
||
610 | * @throws IOException |
||
611 | * @throws TransformationException |
||
612 | * @throws JAXBException |
||
613 | */ |
||
1498 | jmachado | 614 | 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 | 615 | |
616 | Course course = DaoFactory.getCourseDaoImpl().findCourseByCode(code); |
||
617 | |||
618 | |||
619 | JSONObject coursesResponse = new JSONObject(); |
||
620 | |||
1498 | jmachado | 621 | if(course.getJson() == null) |
622 | { |
||
623 | logger.info("status JSON NOT EXIST FOR STUDIES PLAN IN THIS COURSE, will generate"); |
||
624 | new CoursesService().generateCourseJson(course); |
||
625 | } |
||
626 | |||
1496 | jmachado | 627 | if(course.getJson() != null) |
628 | { |
||
629 | JSONObject courseObj = new JSONObject(course.getJson()); |
||
630 | coursesResponse.put("courseInfo",courseObj); |
||
1505 | jmachado | 631 | //Este caso apenas se dá se o plano nunca tiver sido editado ou sincronizado |
632 | //Nesse caso o sistema irá tentar obtê-lo da stream do repositorio |
||
1500 | jmachado | 633 | if(course.getStudiesPlans() == null || course.getStudiesPlans().size() == 0) |
1498 | jmachado | 634 | { |
1505 | jmachado | 635 | logger.info("status JSON NOT EXIST FOR STUDIES PLAN IN THIS COURSE, will try generate from studies plan OLD Stream"); |
1498 | jmachado | 636 | UserSession userSession = DomainObjectFactory.createUserSessionImpl(); |
637 | userSession.setUser(DaoFactory.getUserDaoImpl().load(new 1.5.0/docs/api/java/lang/Long.html">Long(1))); |
||
1505 | jmachado | 638 | new CoursesService().generateXmlJaxbStudiesPlanVersionFromRepositoryOldPlanStream(userSession, course, false, null); |
1498 | jmachado | 639 | } |
640 | |||
1505 | jmachado | 641 | if(course.getStudiesPlans() != null ) |
1496 | jmachado | 642 | { |
643 | CourseStudiesPlan studiesPlan = course.getStudiesPlans().iterator().next(); |
||
1505 | jmachado | 644 | JSONObject studiesPlanObj; |
645 | if(studiesPlan.getJson() != null) |
||
646 | { |
||
647 | studiesPlanObj = new JSONObject(studiesPlan.getJson()); |
||
648 | studiesPlanObj.put("version",studiesPlan.getVersion()); |
||
649 | coursesResponse.put("courseStudiesPlan",studiesPlanObj); |
||
650 | } |
||
651 | else |
||
652 | { |
||
653 | studiesPlanObj = new JSONObject(); |
||
654 | studiesPlanObj.put("fault","Zero contents for this version"); |
||
655 | studiesPlanObj.put("version",studiesPlan.getVersion()); |
||
656 | coursesResponse.put("courseStudiesPlan",studiesPlanObj); |
||
657 | } |
||
1496 | jmachado | 658 | coursesResponse.put("courseStudiesPlan",studiesPlanObj); |
1505 | jmachado | 659 | |
1496 | jmachado | 660 | } |
661 | else |
||
662 | { |
||
663 | coursesResponse.put("status","JSON NOT EXIST FOR STUDIES PLAN IN THIS COURSE"); |
||
664 | } |
||
665 | } |
||
666 | else |
||
667 | { |
||
1505 | jmachado | 668 | coursesResponse.put("status","JSON NOT EXIST FOR COURSE, PLEASE OPEN AND SAVE COURSE IN ADMINISTRATION"); |
1496 | jmachado | 669 | } |
670 | return coursesResponse; |
||
671 | } |
||
672 | |||
1500 | jmachado | 673 | /** |
674 | * @SERVICE@ |
||
675 | * |
||
676 | * @param code |
||
677 | * @return |
||
678 | * @throws JSONException |
||
679 | */ |
||
1502 | jmachado | 680 | 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 | 681 | |
682 | Course course = DaoFactory.getCourseDaoImpl().findCourseByCode(code); |
||
1501 | jmachado | 683 | |
1502 | jmachado | 684 | if(renew != null || course.getStudiesPlans() == null || course.getStudiesPlans().size() == 0) |
1501 | jmachado | 685 | { |
686 | logger.info("status JSON NOT EXIST FOR STUDIES PLAN IN THIS COURSE, will generate"); |
||
687 | UserSession userSession = DomainObjectFactory.createUserSessionImpl(); |
||
688 | userSession.setUser(DaoFactory.getUserDaoImpl().load(new 1.5.0/docs/api/java/lang/Long.html">Long(1))); |
||
689 | try { |
||
1505 | jmachado | 690 | generateXmlJaxbStudiesPlanVersionFromRepositoryOldPlanStream(userSession, course, false, null); |
1501 | jmachado | 691 | } catch (JAXBException e) { |
692 | logger.error(e,e); |
||
693 | return "<error>" + e.toString() + ". see log for details</error>"; |
||
694 | } catch (TransformationException e) { |
||
695 | logger.error(e, e); |
||
696 | return "<error>" + e.toString() + ". see log for details</error>"; |
||
697 | } |
||
698 | } |
||
699 | |||
1496 | jmachado | 700 | if(course.getStudiesPlans() != null && course.getStudiesPlans().size() > 0) |
701 | { |
||
702 | return course.getStudiesPlans().iterator().next().getXml(); |
||
703 | } |
||
704 | return "<error>Does not exixt</error>"; |
||
705 | |||
706 | } |
||
707 | |||
708 | |||
1500 | jmachado | 709 | /** |
710 | * @SERVICE@ |
||
711 | * |
||
712 | * @param systemUrl |
||
713 | * @param setActive |
||
714 | * @return |
||
715 | * @throws IOException |
||
716 | * @throws JSONException |
||
717 | * @throws TransformationException |
||
718 | * @throws JAXBException |
||
719 | */ |
||
1496 | jmachado | 720 | |
1502 | jmachado | 721 | 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 | 722 | |
1497 | jmachado | 723 | 1.5.0/docs/api/java/lang/StringBuilder.html">StringBuilder log = new 1.5.0/docs/api/java/lang/StringBuilder.html">StringBuilder(); |
724 | 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 | 725 | 5+0%2Fdocs%2Fapi+InputStream">InputStream is = url.openStream(); |
726 | 1.5.0/docs/api/java/lang/String.html">String str = StreamsUtils.readString(is); |
||
727 | JSONObject obj = new JSONObject(str); |
||
728 | JSONArray courses = obj.getJSONArray("courses"); |
||
729 | for(int i = 0; i < courses.length();i++) |
||
730 | { |
||
1502 | jmachado | 731 | 1.5.0/docs/api/java/lang/String.html">String code = ""; |
732 | try{ |
||
733 | JSONObject course = courses.getJSONObject(i); |
||
734 | code = course.getString("code"); |
||
735 | Course c = DaoFactory.getCourseDaoImpl().findCourseByCode(code); |
||
736 | if(c == null) |
||
737 | { |
||
738 | 1.5.0/docs/api/java/lang/String.html">String msg = "SKIPING - Course " + code + " " + course.getString("name") + " does not exist in this system"; |
||
739 | log.append("<info>" + msg+"</info>"); |
||
740 | logger.info(msg); |
||
741 | } |
||
742 | else |
||
743 | { |
||
744 | 1.5.0/docs/api/java/lang/String.html">String msg = "UPDATING - Course " + code + " " + course.getString("name") + " exist in this system"; |
||
745 | log.append("<info>" + msg+"</info>"); |
||
746 | logger.info(msg); |
||
1500 | jmachado | 747 | |
1505 | jmachado | 748 | //#############UPDATING Course Comission Members |
749 | updateCourseComissionMembers(systemUrl, code, c); |
||
750 | |||
1502 | jmachado | 751 | //#############UPDATING STUDIES PLAN |
752 | updateStudiesPlanFromRemoteSystem(systemUrl, setActive, log, course, code, c); |
||
1500 | jmachado | 753 | |
1505 | jmachado | 754 | |
1502 | jmachado | 755 | } |
1500 | jmachado | 756 | } |
1502 | jmachado | 757 | catch(1.5.0/docs/api/java/lang/Throwable.html">Throwable e) |
758 | { |
||
759 | logger.error("UPDATE COURSE: " + i + " code: " + code + " FAILED"); |
||
760 | logger.error(e,e); |
||
761 | } |
||
1500 | jmachado | 762 | } |
763 | return log.toString(); |
||
764 | |||
765 | } |
||
766 | |||
1505 | jmachado | 767 | 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 { |
768 | 1.5.0/docs/api/java/lang/String.html">String msg; |
||
769 | 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(); |
||
770 | 1.5.0/docs/api/java/lang/String.html">String studiesPlan = StreamsUtils.readString(stream); |
||
771 | int len = studiesPlan.length(); |
||
772 | if(studiesPlan == null || studiesPlan.trim().length() == 0 || studiesPlan.contains("<error>")) |
||
773 | { |
||
774 | msg = "Course " + code + " " + course.getString("name") + " dont has studies plan"; |
||
775 | log.append("<warn>" + msg+"</warn>"); |
||
776 | logger.warn(msg); |
||
777 | } |
||
778 | else |
||
779 | { |
||
780 | msg = "Found studies plan for " + code + " " + course.getString("name") + " will update "; |
||
781 | log.append("<info>" + msg+"</info>"); |
||
782 | logger.info(msg); |
||
783 | if(setActive) |
||
784 | { |
||
785 | msg = "Setting course to active"; |
||
786 | log.append("<info>" + msg+"</info>"); |
||
787 | logger.info(msg); |
||
788 | c.setStatus(true); |
||
789 | } |
||
790 | //System.out.println(studiesPlan); |
||
791 | msg = "GENERATING COURSE JSON ...."; |
||
792 | log.append("<info>" + msg+"</info>"); |
||
793 | logger.info(msg); |
||
794 | new CoursesService().generateCourseJson(c); |
||
795 | |||
796 | msg="GENERATING COURSE STUDIES PLAN JSON ...."; |
||
797 | log.append("<info>" + msg+"</info>"); |
||
798 | logger.info(msg); |
||
799 | stream.close(); |
||
800 | stream = new 1.5.0/docs/api/java/net/URL.html">URL(systemUrl + "/wsjson/api?service=getStudiesPlanXml&code=" + code).openStream(); |
||
801 | UserSession userSession = DomainObjectFactory.createUserSessionImpl(); |
||
802 | userSession.setUser(DaoFactory.getUserDaoImpl().load(new 1.5.0/docs/api/java/lang/Long.html">Long(1))); |
||
803 | new CoursesService().uploadStudiesPlan(stream, "curso_" + code + ".xml", len,"appication/xml", userSession,c,true,systemUrl); |
||
804 | } |
||
805 | } |
||
806 | |||
807 | |||
1500 | jmachado | 808 | /** |
809 | * Update courseComission Members |
||
810 | * @param systemUrl |
||
811 | * @param code |
||
812 | * @param c |
||
813 | * @throws IOException |
||
814 | * @throws JSONException |
||
815 | */ |
||
816 | 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 { |
||
817 | 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); |
||
818 | 5+0%2Fdocs%2Fapi+InputStream">InputStream isCourseDetails = urlCourseDetails.openStream(); |
||
819 | 1.5.0/docs/api/java/lang/String.html">String strCourseDetails = StreamsUtils.readString(isCourseDetails); |
||
820 | JSONObject objCourseDetails = new JSONObject(strCourseDetails); |
||
821 | 1.5.0/docs/api/java/lang/String.html">String validationRole = objCourseDetails.getJSONObject("courseInfo").getString("validationRole"); |
||
822 | |||
823 | if(validationRole == null) |
||
824 | { |
||
825 | logger.info("validationRole is not defined"); |
||
826 | } |
||
827 | else |
||
828 | { |
||
829 | logger.info("found validationRole: " + validationRole); |
||
830 | c.setValidationRole(validationRole); |
||
831 | |||
832 | JSONObject coordinator = objCourseDetails.getJSONObject("courseInfo").getJSONObject("coordinator"); |
||
833 | JSONArray courseComission = objCourseDetails.getJSONObject("courseInfo").getJSONArray("courseComission"); |
||
834 | |||
835 | Teacher coordinatorPersistent = findPersonFromCourseDetails(coordinator); |
||
836 | if(coordinatorPersistent == null) |
||
837 | { |
||
838 | logger.warn("Coordinator does not exist in this system "); |
||
839 | } |
||
840 | else |
||
841 | { |
||
842 | c.setCoordinator(coordinatorPersistent); |
||
843 | } |
||
844 | for(int j = 0 ; j < courseComission.length(); j++) |
||
845 | { |
||
846 | JSONObject memberComission = courseComission.getJSONObject(j); |
||
847 | Teacher memberPersistent = findPersonFromCourseDetails(memberComission); |
||
848 | if(memberPersistent == null) |
||
1496 | jmachado | 849 | { |
1500 | jmachado | 850 | logger.info("Member does not exist in this system "); |
1496 | jmachado | 851 | } |
852 | else |
||
853 | { |
||
1500 | jmachado | 854 | logger.info("Adding role of course comission member"); |
855 | if(!memberPersistent.hasRole(validationRole)) |
||
1497 | jmachado | 856 | { |
1500 | jmachado | 857 | memberPersistent.addRole(validationRole); |
1497 | jmachado | 858 | } |
1500 | jmachado | 859 | } |
1497 | jmachado | 860 | |
1496 | jmachado | 861 | } |
862 | } |
||
1500 | jmachado | 863 | } |
1497 | jmachado | 864 | |
1500 | jmachado | 865 | private Teacher findPersonFromCourseDetails(JSONObject coordinator) { |
866 | int code; |
||
867 | try { |
||
868 | if(coordinator.has("sigesCode")) |
||
869 | { |
||
870 | code = coordinator.getInt("sigesCode"); |
||
871 | } |
||
872 | else |
||
873 | { |
||
874 | logger.warn("there is no sigesCode for this person " + coordinator.toString()); |
||
875 | return null; |
||
876 | } |
||
877 | } catch (JSONException e){ |
||
878 | return null; |
||
879 | } catch (1.5.0/docs/api/java/lang/NumberFormatException.html">NumberFormatException e){ |
||
880 | return null; |
||
881 | } |
||
882 | return DaoFactory.getTeacherDaoImpl().loadBySigesCode(code); |
||
1496 | jmachado | 883 | } |
884 | |||
1505 | jmachado | 885 | |
886 | |||
887 | /* |
||
888 | * Studies Plans Administration Services |
||
889 | * |
||
890 | */ |
||
891 | public void addNewStudiesPlan(long courseId,CourseStudiesPlan studiesPlan,UserSession session) |
||
892 | { |
||
893 | Course c = DaoFactory.getCourseDaoImpl().load(courseId); |
||
894 | studiesPlan.setCourse(c); |
||
895 | c.getStudiesPlans().add(studiesPlan); |
||
896 | DaoFactory.getCourseStudiesPlanDaoImpl().save(studiesPlan); |
||
897 | } |
||
898 | |||
899 | public void cloneVersionFrom(long sourcePlanId, long targetPlanId, long courseId, UserSession session) |
||
900 | { |
||
901 | Course course = DaoFactory.getCourseDaoImpl().load(courseId); |
||
902 | CourseStudiesPlan source = null; |
||
903 | CourseStudiesPlan target = null; |
||
904 | for(CourseStudiesPlan plan: course.getStudiesPlans()) |
||
1500 | jmachado | 905 | { |
1505 | jmachado | 906 | if(plan.getId() == sourcePlanId) |
907 | source = plan; |
||
908 | else if(plan.getId() == targetPlanId) |
||
909 | target = plan; |
||
1500 | jmachado | 910 | } |
1505 | jmachado | 911 | target.setXml(source.getXml()); |
912 | target.setJson(source.getJson()); |
||
913 | } |
||
914 | public Curso loadCursoPlanoFromXml(1.5.0/docs/api/java/lang/String.html">String xml) |
||
915 | { |
||
916 | try { |
||
917 | if(xml != null) |
||
1500 | jmachado | 918 | { |
1505 | jmachado | 919 | JAXBContext jc = JAXBContext.newInstance(Curso.class); |
920 | Unmarshaller unmarshaller = jc.createUnmarshaller(); |
||
921 | Curso curso = (Curso) unmarshaller.unmarshal(new 1.5.0/docs/api/java/io/StringReader.html">StringReader(xml)); |
||
922 | return curso; |
||
1500 | jmachado | 923 | } |
1505 | jmachado | 924 | return null; |
925 | } catch (JAXBException e) { |
||
926 | logger.error(e,e); |
||
927 | return null; |
||
928 | } |
||
929 | } |
||
1496 | jmachado | 930 | |
1505 | jmachado | 931 | /** |
932 | * Persist the edited studies plan |
||
933 | * Updates studiesPlanVersion |
||
934 | * Updates studiesPlanVersionDescription |
||
935 | * Updates PlanoEstudos XML and JSON |
||
936 | * |
||
937 | * @param courseId |
||
938 | * @param coursePlanId |
||
939 | * @param planoEditado |
||
940 | * @param courseStudiesPlanEditado |
||
941 | * @return |
||
942 | */ |
||
943 | public Course savePlanoEstudosEditado(long courseId, long coursePlanId, Curso planoEditado, CourseStudiesPlan courseStudiesPlanEditado,UserSession session) |
||
944 | { |
||
945 | try { |
||
946 | Course course = DaoFactory.getCourseDaoImpl().load(courseId); |
||
947 | |||
948 | for(CourseStudiesPlan courseStudiesPlanPersistente: course.getStudiesPlans()) |
||
949 | { |
||
950 | if(courseStudiesPlanPersistente.getId() == coursePlanId) |
||
951 | { |
||
952 | courseStudiesPlanPersistente.setVersion(courseStudiesPlanEditado.getVersion()); |
||
953 | courseStudiesPlanPersistente.setVersionDescription(courseStudiesPlanEditado.getVersionDescription()); |
||
954 | |||
955 | //Garante-se mas depois não vai para o JSON |
||
956 | planoEditado.setSiges(course.getCode());//GARANTIR QUE O CODIGO SIGEST ESTA CORRECTO |
||
957 | planoEditado.setNome(course.getName()); |
||
958 | planoEditado.setDep(course.getArea()); |
||
959 | //planoEditado.setDepDesc(""); |
||
960 | //planoEditado.setDepDescEn(""); |
||
961 | //planoEditado.setDepDescEs(""); |
||
962 | //planoEditado.setDepDescFr(""); |
||
963 | JAXBContext jc = JAXBContext.newInstance(Curso.class); |
||
964 | Marshaller marshaller = jc.createMarshaller(); |
||
965 | marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); |
||
966 | 1.5.0/docs/api/java/io/StringWriter.html">StringWriter xml = new 1.5.0/docs/api/java/io/StringWriter.html">StringWriter(); |
||
967 | marshaller.marshal(planoEditado,xml); |
||
968 | |||
969 | courseStudiesPlanPersistente.setXml(xml.toString()); |
||
970 | 1.5.0/docs/api/java/lang/String.html">String json = getGensonPlanoEstudos().serialize(planoEditado); |
||
971 | //SETTING JSON in COURSE STUDIES PLAN |
||
972 | courseStudiesPlanPersistente.setJson(json); |
||
973 | break; |
||
974 | } |
||
975 | } |
||
976 | return course; |
||
977 | } catch (JAXBException e) { |
||
978 | logger.error(e,e); |
||
979 | return null; |
||
980 | } catch (TransformationException e) { |
||
981 | logger.error(e,e); |
||
982 | return null; |
||
983 | } catch (1.5.0/docs/api/java/io/IOException.html">IOException e) { |
||
984 | logger.error(e,e); |
||
985 | return null; |
||
1500 | jmachado | 986 | } |
987 | } |
||
1496 | jmachado | 988 | |
1500 | jmachado | 989 | |
1505 | jmachado | 990 | |
991 | public List<CourseDepartment> loadDepartments() |
||
992 | { |
||
993 | List<CourseDepartment> departments = DaoFactory.getCourseDepartmentDaoImpl().findAll(); |
||
994 | for(CourseDepartment dep: departments) |
||
995 | dep.getName(); |
||
996 | return departments; |
||
997 | } |
||
998 | |||
999 | |||
214 | jmachado | 1000 | } |