Rev 1500 | 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; |
||
1500 | jmachado | 32 | import java.io.IOException; |
33 | import java.io.InputStream; |
||
34 | import java.io.PrintWriter; |
||
35 | import java.io.StringWriter; |
||
1496 | jmachado | 36 | import java.net.URL; |
37 | import java.util.*; |
||
214 | jmachado | 38 | |
39 | /* |
||
40 | * @author Goncalo Luiz gedl [AT] rnl [DOT] ist [DOT] utl [DOT] pt |
||
41 | * |
||
42 | * |
||
43 | * Created at 17/Out/2003 , 23:45:24 |
||
44 | * |
||
45 | */ |
||
46 | /** |
||
47 | * @author Jorge Machado |
||
48 | * |
||
49 | * |
||
50 | * Created at 17/Out/2003 , 23:45:24 |
||
51 | * |
||
52 | */ |
||
53 | public class CoursesService implements IService |
||
54 | { |
||
55 | 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); |
||
56 | |||
57 | RepositoryService repositoryService = new RepositoryService(); |
||
58 | |||
59 | |||
60 | public CourseView loadCourse(long id, boolean initUnits) throws ServiceException |
||
61 | { |
||
62 | Course c = DaoFactory.getCourseDaoImpl().get(id); |
||
63 | |||
64 | if(c != null) |
||
65 | { |
||
66 | CourseView cV = new CourseView(c,initUnits); |
||
67 | if(c.getStudiesPlan() != null) |
||
68 | { |
||
69 | RepositoryFileImpl repositoryFile = repositoryService.loadView(c.getStudiesPlan()); |
||
70 | cV.setStudiesPlan(repositoryFile); |
||
71 | } |
||
72 | |||
73 | return cV; |
||
74 | } |
||
75 | return null; |
||
76 | } |
||
77 | |||
345 | jmachado | 78 | public List<String> loadImportYears(UserSession userSession) throws ServiceException |
79 | { |
||
80 | List<String> importYears = DaoFactory.getCourseDaoImpl().loadImportYears(); |
||
81 | List<String> imStrings = new ArrayList<String>(); |
||
82 | for(1.5.0/docs/api/java/lang/String.html">String importYear: importYears) |
||
83 | { |
||
84 | imStrings.add(importYear); |
||
85 | } |
||
86 | return imStrings; |
||
87 | } |
||
88 | |||
214 | jmachado | 89 | public CourseView loadCourseByCode(1.5.0/docs/api/java/lang/String.html">String code, boolean initUnits) throws ServiceException |
90 | { |
||
444 | jmachado | 91 | try{ |
92 | Course c = DaoFactory.getCourseDaoImpl().findCourseByCode(code); |
||
93 | if(c != null) |
||
214 | jmachado | 94 | { |
444 | jmachado | 95 | CourseView cV = new CourseView(c,initUnits); |
96 | if(c.getStudiesPlan() != null) |
||
97 | { |
||
98 | RepositoryFileImpl repositoryFile = repositoryService.loadView(c.getStudiesPlan()); |
||
99 | cV.setStudiesPlan(repositoryFile); |
||
100 | } |
||
101 | return cV; |
||
214 | jmachado | 102 | } |
103 | } |
||
444 | jmachado | 104 | catch(1.5.0/docs/api/java/lang/Throwable.html">Throwable e) |
105 | { |
||
106 | logger.error(e + " loading code:" + code,e); |
||
107 | throw new ServiceException("loading code: " + code + " - " + e.toString(),e); |
||
108 | } |
||
214 | jmachado | 109 | return null; |
110 | } |
||
111 | |||
112 | public CourseView submitCourse(CourseView courseView, |
||
113 | 5+0%2Fdocs%2Fapi+InputStream">InputStream stream, |
||
114 | 1.5.0/docs/api/java/lang/String.html">String name, |
||
115 | int size, |
||
116 | 1.5.0/docs/api/java/lang/String.html">String contentType, |
||
1496 | jmachado | 117 | UserSession userSession) throws ServiceException, JAXBException, TransformationException, 1.5.0/docs/api/java/io/IOException.html">IOException { |
214 | jmachado | 118 | Course c; |
119 | if(courseView.getId() > 0) |
||
444 | jmachado | 120 | { |
214 | jmachado | 121 | c = DaoFactory.getCourseDaoImpl().get(courseView.getId()); |
444 | jmachado | 122 | } |
214 | jmachado | 123 | else |
124 | { |
||
444 | jmachado | 125 | c = DaoFactory.getCourseDaoImpl().findCourseByCodeAndYear(courseView.getCode(),courseView.getImportYear()); |
126 | if(c != null) |
||
127 | throw new AlreadyExistsException(AlreadyExistsException.ALREADY_EXISTS_COURSE); |
||
214 | jmachado | 128 | c = DomainObjectFactory.createCourseImpl(); |
129 | DaoFactory.getCourseDaoImpl().save(c); |
||
130 | } |
||
131 | |||
248 | jmachado | 132 | 1.5.0/docs/api/java/lang/String.html">String htmlTrasformationResult = null; |
133 | |||
1496 | jmachado | 134 | htmlTrasformationResult = uploadStudiesPlan(stream, name, size, contentType, userSession, c,false,null); |
135 | courseView.persistViewInObject(c); |
||
136 | CourseView cv = loadCourse(c.getId(),false); |
||
137 | cv.setHtmlResult(htmlTrasformationResult); |
||
138 | |||
139 | /** |
||
140 | * New## generating course json |
||
141 | */ |
||
142 | generateCourseJson(c); |
||
143 | |||
144 | return cv; |
||
145 | } |
||
146 | |||
147 | 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 { |
||
148 | 1.5.0/docs/api/java/lang/String.html">String htmlTrasformationResult = null; |
||
214 | jmachado | 149 | if(stream != null && size > 0) |
150 | { |
||
268 | jmachado | 151 | 1.5.0/docs/api/java/lang/String.html">String extension = FilesUtils.getExtension(name); |
152 | if(c.getStudiesPlan() == null) |
||
153 | { |
||
332 | jmachado | 154 | 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 | 155 | c.setStudiesPlan(identifier); |
156 | } |
||
157 | else |
||
158 | { |
||
159 | repositoryService.updateRepositoryFile(c.getStudiesPlan(), stream, contentType, extension, size, name, "course.studies.plan " + c.getName(), ResourceAccessControlEnum.publicDomain); |
||
160 | } |
||
1496 | jmachado | 161 | htmlTrasformationResult = generateHtmlCache(userSession, c); |
162 | //####New#### Generating XML with JaxB |
||
163 | generateXmlJaxbStudiesPlanVersion(userSession, c,forceUrlFichas,systemUrl); |
||
164 | } |
||
165 | return htmlTrasformationResult; |
||
166 | } |
||
167 | |||
168 | private void generateCourseJson(Course cAux) throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException { |
||
169 | CourseImpl c = (CourseImpl) DaoFactory.getCourseDaoImpl().narrow(cAux); |
||
170 | |||
171 | if(c.getValidationRole() != null && c.getValidationRole().trim().length() > 0) |
||
172 | { |
||
173 | List<Teacher> courseComissionProxys = DaoFactory.getUserDaoImpl().loadRoleTeachers(c.getValidationRole()); |
||
174 | List<Teacher> courseComission = new ArrayList<Teacher>(); |
||
175 | for(Teacher t: courseComissionProxys) |
||
248 | jmachado | 176 | { |
1496 | jmachado | 177 | courseComission.add(DaoFactory.getTeacherDaoImpl().narrow(t)); |
248 | jmachado | 178 | } |
1496 | jmachado | 179 | c.setCourseComission(courseComission); |
180 | } |
||
181 | //Getting Coordinator from proxy |
||
182 | Teacher t = c.getCoordinator(); |
||
1500 | jmachado | 183 | if(t != null) |
184 | t.getName(); |
||
185 | else |
||
186 | { |
||
187 | logger.warn("Course does not have coordinator"); |
||
188 | } |
||
1496 | jmachado | 189 | |
190 | 1.5.0/docs/api/java/lang/String.html">String jsonCourse = getGensonCourse().serialize(c); |
||
191 | c.setJson(jsonCourse); |
||
192 | } |
||
193 | |||
194 | private 1.5.0/docs/api/java/lang/String.html">String generateHtmlCache(UserSession userSession, Course c) { |
||
195 | 1.5.0/docs/api/java/lang/String.html">String htmlTrasformationResult = null; |
||
196 | 5+0%2Fdocs%2Fapi+InputStream">InputStream stream;IRepositoryFile repositoryFile = repositoryService.load(c.getStudiesPlan(),userSession); |
||
197 | stream = repositoryFile.getInput(); |
||
198 | try |
||
199 | { |
||
200 | 5+0%2Fdocs%2Fapi+Document">Document dom = Dom4jUtil.parse(stream); |
||
201 | Map<String,Object> parameters = new HashMap<String,Object>(); |
||
202 | parameters.put("COURSE_SIGES_CODE",c.getCode()); |
||
203 | 1.5.0/docs/api/java/lang/String.html">String html = Dom4jUtil.styleDocument(dom, Globals.TEMPLATE_COURSE_XSL_PATH,parameters); |
||
204 | c.setCacheWebDocument(html); |
||
205 | } |
||
206 | catch (1.5.0/docs/api/java/lang/Exception.html">Exception e) |
||
207 | { |
||
208 | 1.5.0/docs/api/java/io/StringWriter.html">StringWriter writer = new 1.5.0/docs/api/java/io/StringWriter.html">StringWriter(); |
||
209 | 1.5.0/docs/api/java/io/PrintWriter.html">PrintWriter printWriter = new 1.5.0/docs/api/java/io/PrintWriter.html">PrintWriter(writer); |
||
210 | e.printStackTrace(printWriter); |
||
211 | htmlTrasformationResult = "<div class=\"error\"><pre>" + e.toString() + "\n" + printWriter.toString() + "</pre></div>"; |
||
212 | printWriter.close(); |
||
213 | } |
||
214 | try |
||
215 | { |
||
216 | stream.close(); |
||
217 | } |
||
218 | catch (1.5.0/docs/api/java/io/IOException.html">IOException e) |
||
219 | { |
||
220 | logger.error(e,e); |
||
221 | } |
||
222 | return htmlTrasformationResult; |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * ##NEW METHOD### |
||
227 | * Gera o XML normalizado para o JAXB a partir do XML importado do XML do plano XML quese usou no upload |
||
228 | * para garantir que está bem formado |
||
229 | * @param userSession |
||
230 | * @param c |
||
231 | * @return |
||
232 | * @throws JAXBException if XML is not weel formed |
||
233 | */ |
||
1498 | jmachado | 234 | private void generateXmlJaxbStudiesPlanVersion(UserSession userSession, Course c,boolean forceFichaCurricularUrlSet,1.5.0/docs/api/java/lang/String.html">String systemUrl) throws JAXBException, TransformationException |
1496 | jmachado | 235 | { |
236 | CourseStudiesPlan courseStudiesPlan; |
||
1500 | jmachado | 237 | if(c.getStudiesPlan() == null || c.getStudiesPlan().trim().length() == 0) |
238 | { |
||
239 | logger.warn("Course does not have studies plan XML file stream to use in update"); |
||
240 | return; |
||
241 | } |
||
1496 | jmachado | 242 | |
243 | if(c.getStudiesPlans() == null || c.getStudiesPlans().size() == 0) |
||
244 | { |
||
245 | logger.info("Generating first study plan"); |
||
246 | courseStudiesPlan = DomainObjectFactory.createCourseStudiesPlanImpl(); |
||
247 | courseStudiesPlan.setVersion(1); |
||
248 | 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()); |
||
249 | courseStudiesPlan.setCourse(c); |
||
250 | if(c.getStudiesPlans() == null) |
||
251 | c.setStudiesPlans(new HashSet<CourseStudiesPlan>()); |
||
252 | c.getStudiesPlans().add(courseStudiesPlan); |
||
253 | DaoFactory.getCourseStudiesPlanDaoImpl().save(courseStudiesPlan); |
||
254 | } |
||
255 | else |
||
256 | { |
||
257 | courseStudiesPlan = c.getStudiesPlans().iterator().next(); |
||
258 | logger.info("Updating Study Plan version " + courseStudiesPlan.getVersion()); |
||
259 | } |
||
260 | |||
261 | 5+0%2Fdocs%2Fapi+InputStream">InputStream stream; |
||
262 | IRepositoryFile repositoryFile = repositoryService.load(c.getStudiesPlan(),userSession); |
||
263 | stream = repositoryFile.getInput(); |
||
264 | |||
265 | try { |
||
266 | JAXBContext jc = JAXBContext.newInstance(Curso.class); |
||
267 | Unmarshaller unmarshaller = jc.createUnmarshaller(); |
||
268 | Curso curso = (Curso) unmarshaller.unmarshal(stream); |
||
269 | |||
270 | Marshaller marshaller = jc.createMarshaller(); |
||
271 | marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); |
||
272 | 1.5.0/docs/api/java/io/StringWriter.html">StringWriter sw = new 1.5.0/docs/api/java/io/StringWriter.html">StringWriter(); |
||
273 | marshaller.marshal(curso, sw); |
||
274 | //SETTING XML in COURSE STUDIES PLAN |
||
275 | courseStudiesPlan.setXml(sw.toString()); |
||
276 | //##NOVO PARA GERAR LINK SE NAO EXISTIR |
||
1498 | jmachado | 277 | generateAutoUrlFichasCurriculares(curso,systemUrl,forceFichaCurricularUrlSet); |
1496 | jmachado | 278 | 1.5.0/docs/api/java/lang/String.html">String json = getGensonCursoXmlObj().serialize(curso); |
279 | //SETTING JSON in COURSE STUDIES PLAN |
||
280 | courseStudiesPlan.setJson(json); |
||
281 | |||
282 | } catch (JAXBException e) { |
||
283 | logger.error(e,e); |
||
284 | throw e; |
||
285 | } catch (TransformationException e) { |
||
286 | logger.error(e, e); |
||
287 | throw e; |
||
288 | } catch (1.5.0/docs/api/java/io/IOException.html">IOException e) { |
||
289 | e.printStackTrace(); |
||
290 | } |
||
291 | try |
||
292 | { |
||
293 | stream.close(); |
||
294 | } |
||
295 | catch (1.5.0/docs/api/java/io/IOException.html">IOException e) |
||
296 | { |
||
297 | logger.error(e,e); |
||
298 | } |
||
299 | } |
||
300 | |||
301 | private void generateAutoUrlFichasCurriculares(Curso curso,1.5.0/docs/api/java/lang/String.html">String systemUrl,boolean force) |
||
302 | { |
||
303 | for(Curso.Semestre s :curso.getSemestre()) |
||
304 | { |
||
305 | for(Curso.Semestre.Perfil p :s.getPerfil()) |
||
248 | jmachado | 306 | { |
1496 | jmachado | 307 | for(UnidadeType unidadeType : p.getUnidade()) |
308 | { |
||
309 | generateAutoUrlUnidade(unidadeType,systemUrl,curso,s,force); |
||
310 | } |
||
248 | jmachado | 311 | } |
1496 | jmachado | 312 | for(UnidadeType unidadeType : s.getUnidade()) |
214 | jmachado | 313 | { |
1496 | jmachado | 314 | generateAutoUrlUnidade(unidadeType,systemUrl,curso,s,force); |
214 | jmachado | 315 | } |
316 | } |
||
317 | } |
||
318 | |||
1496 | jmachado | 319 | private void generateAutoUrlUnidade(UnidadeType unidadeType,1.5.0/docs/api/java/lang/String.html">String systemUrl,Curso curso,Curso.Semestre semestre,boolean force) |
320 | { |
||
321 | if(force || unidadeType.getUrlFichaCurricular() == null || unidadeType.getUrlFichaCurricular().trim().length()==0) |
||
322 | { |
||
323 | logger.info("GENERATING FICHA CURRICULAR URL For " + unidadeType.getNome()); |
||
324 | 1.5.0/docs/api/java/lang/String.html">String url = systemUrl != null ? systemUrl : ""; |
||
325 | if(!url.endsWith("/;")) |
||
326 | url = url + "/"; |
||
327 | unidadeType.setUrlFichaCurricular(url + "startLoadCourseUnitSiges.do?unitCode=" + unidadeType.getSiges() + "&courseCode=" + curso.getSiges() + "&semestre=" + semestre.getId()); |
||
328 | } |
||
329 | } |
||
330 | |||
331 | |||
332 | private static Genson getGensonCursoXmlObj(){ |
||
333 | Genson genson = new Genson.Builder() |
||
334 | .exclude("class") |
||
335 | .create(); |
||
336 | return genson; |
||
337 | } |
||
338 | |||
339 | |||
340 | |||
341 | private static Genson getGensonCourse(){ |
||
342 | Genson genson = new Genson.Builder() |
||
343 | .exclude(5+0%2Fdocs%2Fapi+Object">Object.class) |
||
344 | .setUseFields(false) |
||
345 | .setUseGettersAndSetters(true) |
||
346 | .setMethodFilter(VisibilityFilter.PACKAGE_PUBLIC) |
||
347 | .exclude("admin") |
||
348 | .exclude("autoBlock") |
||
349 | .exclude("autoBlockMode") |
||
350 | .exclude("manualBlock") |
||
351 | .exclude("newUser") |
||
352 | .exclude("student") |
||
353 | .exclude("superuser") |
||
354 | .exclude("superuserOrAdmin") |
||
355 | .exclude("teacher") |
||
356 | .exclude("unitCheck") |
||
357 | .exclude("id") |
||
358 | |||
359 | /* .exclude(Course.class) |
||
360 | .exclude(CourseImpl.class) |
||
361 | .exclude(GenericUser.class) |
||
362 | .exclude(User.class) |
||
363 | .exclude(UserImpl.class) |
||
364 | .exclude(Teacher.class) |
||
365 | .exclude(TeacherImpl.class) |
||
366 | .exclude(SigesUser.class) |
||
367 | .exclude(SigesUserImpl.class) |
||
368 | .exclude(GenericUser.class) |
||
369 | .exclude(GenericUserImpl.class) |
||
370 | */ |
||
371 | .exclude("id", Course.class) |
||
372 | .exclude("status",Course.class) |
||
373 | .include("degreeForJsonApi", CourseImpl.class) |
||
374 | .include("schoolForJsonApi", CourseImpl.class) |
||
375 | .include("statusForJsonApi", CourseImpl.class) |
||
376 | |||
377 | .include("name", Course.class) |
||
378 | .include("code", Course.class) |
||
1500 | jmachado | 379 | .include("validationRole", Course.class) |
380 | |||
1496 | jmachado | 381 | .include("courseComission", CourseImpl.class) |
382 | |||
383 | .include("name", GenericUser.class) |
||
384 | .include("email", GenericUser.class) |
||
385 | .include("sigesCode", SigesUser.class) |
||
386 | .include("coordinator", Course.class) |
||
387 | .create(); |
||
388 | |||
389 | return genson; |
||
390 | } |
||
391 | |||
392 | |||
393 | |||
214 | jmachado | 394 | public List<CourseView> loadCourses() throws ServiceException |
395 | { |
||
396 | List<Course> courses = DaoFactory.getCourseDaoImpl().findAllOrderByName(); |
||
397 | List<CourseView> courseViews = new ArrayList<CourseView>(); |
||
398 | for(Course c: courses) |
||
399 | { |
||
400 | CourseView courseView = new CourseView(c); |
||
401 | courseViews.add(courseView); |
||
402 | } |
||
403 | return courseViews; |
||
404 | } |
||
405 | |||
376 | jmachado | 406 | 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 |
407 | { |
||
1312 | jmachado | 408 | return loadCoursesImportYearAreaInstitution(importYear, area,null); |
409 | } |
||
410 | |||
411 | 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 |
||
412 | { |
||
413 | List<Course> courses = DaoFactory.getCourseDaoImpl().findAllOrderByName(importYear,area,null,institutionCode); |
||
376 | jmachado | 414 | List<CourseView> courseViews = new ArrayList<CourseView>(); |
415 | for(Course c: courses) |
||
416 | { |
||
417 | CourseView courseView = new CourseView(c); |
||
418 | courseViews.add(courseView); |
||
419 | } |
||
420 | return courseViews; |
||
421 | } |
||
422 | |||
249 | jmachado | 423 | public List<CourseView> loadCoursesImportYear() throws ServiceException |
424 | { |
||
995 | jmachado | 425 | 1.5.0/docs/api/java/lang/String.html">String importYearIntranet = DaoFactory.getConfigurationDaoImpl().getInterfaceImportYear(); |
249 | jmachado | 426 | List<Course> courses = DaoFactory.getCourseDaoImpl().findAllOrderByName(importYearIntranet); |
427 | List<CourseView> courseViews = new ArrayList<CourseView>(); |
||
428 | for(Course c: courses) |
||
429 | { |
||
430 | CourseView courseView = new CourseView(c); |
||
431 | courseViews.add(courseView); |
||
432 | } |
||
433 | return courseViews; |
||
434 | } |
||
417 | jmachado | 435 | public List<CourseView> loadCoursesImportYearByType(1.5.0/docs/api/java/lang/String.html">String type) throws ServiceException |
436 | { |
||
995 | jmachado | 437 | 1.5.0/docs/api/java/lang/String.html">String importYearIntranet = DaoFactory.getConfigurationDaoImpl().getInterfaceImportYear(); |
695 | jmachado | 438 | List<Course> courses = DaoFactory.getCourseDaoImpl().findAllOrderByNameEvenWithoutCourseUnit(importYearIntranet,null,type); |
417 | jmachado | 439 | List<CourseView> courseViews = new ArrayList<CourseView>(); |
440 | for(Course c: courses) |
||
441 | { |
||
442 | CourseView courseView = new CourseView(c); |
||
443 | courseViews.add(courseView); |
||
444 | } |
||
445 | return courseViews; |
||
446 | } |
||
214 | jmachado | 447 | |
790 | jmachado | 448 | public List<CourseView> loadActiveCoursesByType(1.5.0/docs/api/java/lang/String.html">String type) throws ServiceException |
449 | { |
||
995 | jmachado | 450 | 1.5.0/docs/api/java/lang/String.html">String importYearIntranet = DaoFactory.getConfigurationDaoImpl().getInterfaceImportYear(); |
790 | jmachado | 451 | List<Course> courses = DaoFactory.getCourseDaoImpl().findAllActiveOrderByNameEvenWithoutCourseUnit(importYearIntranet,null,type); |
452 | List<CourseView> courseViews = new ArrayList<CourseView>(); |
||
453 | for(Course c: courses) |
||
454 | { |
||
455 | CourseView courseView = new CourseView(c); |
||
456 | courseViews.add(courseView); |
||
457 | } |
||
458 | return courseViews; |
||
459 | } |
||
249 | jmachado | 460 | |
1500 | jmachado | 461 | |
462 | |||
463 | /** JSON API **/ |
||
464 | /** |
||
465 | * @SERVICE@ |
||
466 | * |
||
467 | * @param school |
||
468 | * @param type |
||
469 | * @return |
||
470 | * @throws JSONException |
||
471 | */ |
||
1496 | jmachado | 472 | 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 { |
473 | 1.5.0/docs/api/java/lang/String.html">String institutionalCode = null; |
||
474 | 1.5.0/docs/api/java/lang/String.html">String degree = null; |
||
475 | if(school != null && school.length() > 0) |
||
476 | institutionalCode = ConfigProperties.getProperty("institution.code.prefix.inverse." + school); |
||
249 | jmachado | 477 | |
1496 | jmachado | 478 | if(type != null && type.length() > 0) |
479 | degree = ConfigProperties.getProperty("course.inverse." + type); |
||
790 | jmachado | 480 | |
1496 | jmachado | 481 | List<Course> courses = DaoFactory.getCourseDaoImpl().findAllActiveOrderByNameEvenWithoutCourseUnit(institutionalCode,degree); |
482 | JSONObject coursesResponse = new JSONObject(); |
||
483 | |||
484 | JSONArray coursesArray = new JSONArray(); |
||
485 | for(Course cAux: courses) |
||
486 | { |
||
487 | CourseImpl c = (CourseImpl) DaoFactory.getCourseDaoImpl().narrow(cAux); |
||
488 | JSONObject courseJson = new JSONObject(); |
||
489 | courseJson.put("name",c.getName()); |
||
490 | courseJson.put("code",c.getCode()); |
||
491 | courseJson.put("schoolForJsonApi",c.getSchoolForJsonApi()); |
||
492 | courseJson.put("degreeForJsonApi",c.getDegreeForJsonApi()); |
||
493 | courseJson.put("statusForJsonApi",c.getStatusForJsonApi()); |
||
494 | courseJson.put("getDetailedInfoUrl","/wsjson/api?service=getCourse&code=" + c.getCode()); |
||
495 | coursesArray.put(courseJson); |
||
496 | |||
497 | } |
||
498 | coursesResponse.put("status","ok"); |
||
499 | coursesResponse.put("courses",coursesArray); |
||
500 | |||
501 | return coursesResponse; |
||
502 | } |
||
503 | |||
1500 | jmachado | 504 | /** |
505 | * |
||
506 | * * @SERVICE@ |
||
507 | * |
||
508 | * @param code |
||
509 | * @return |
||
510 | * @throws JSONException |
||
511 | * @throws IOException |
||
512 | * @throws TransformationException |
||
513 | * @throws JAXBException |
||
514 | */ |
||
1498 | jmachado | 515 | 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 | 516 | |
517 | Course course = DaoFactory.getCourseDaoImpl().findCourseByCode(code); |
||
518 | |||
519 | |||
520 | JSONObject coursesResponse = new JSONObject(); |
||
521 | |||
1498 | jmachado | 522 | if(course.getJson() == null) |
523 | { |
||
524 | logger.info("status JSON NOT EXIST FOR STUDIES PLAN IN THIS COURSE, will generate"); |
||
525 | new CoursesService().generateCourseJson(course); |
||
526 | } |
||
527 | |||
1496 | jmachado | 528 | if(course.getJson() != null) |
529 | { |
||
530 | JSONObject courseObj = new JSONObject(course.getJson()); |
||
531 | coursesResponse.put("courseInfo",courseObj); |
||
1500 | jmachado | 532 | if(course.getStudiesPlans() == null || course.getStudiesPlans().size() == 0) |
1498 | jmachado | 533 | { |
534 | logger.info("status JSON NOT EXIST FOR STUDIES PLAN IN THIS COURSE, will generate"); |
||
535 | UserSession userSession = DomainObjectFactory.createUserSessionImpl(); |
||
536 | userSession.setUser(DaoFactory.getUserDaoImpl().load(new 1.5.0/docs/api/java/lang/Long.html">Long(1))); |
||
537 | new CoursesService().generateXmlJaxbStudiesPlanVersion(userSession, course, false, null); |
||
538 | } |
||
539 | |||
1496 | jmachado | 540 | if(course.getStudiesPlans() != null) |
541 | { |
||
542 | CourseStudiesPlan studiesPlan = course.getStudiesPlans().iterator().next(); |
||
543 | JSONObject studiesPlanObj = new JSONObject(studiesPlan.getJson()); |
||
544 | studiesPlanObj.put("version",studiesPlan.getVersion()); |
||
545 | coursesResponse.put("courseStudiesPlan",studiesPlanObj); |
||
546 | } |
||
547 | else |
||
548 | { |
||
549 | coursesResponse.put("status","JSON NOT EXIST FOR STUDIES PLAN IN THIS COURSE"); |
||
550 | } |
||
551 | } |
||
552 | else |
||
553 | { |
||
554 | coursesResponse.put("status","JSON NOT EXIST FOR COURSE"); |
||
555 | } |
||
556 | return coursesResponse; |
||
557 | } |
||
558 | |||
1500 | jmachado | 559 | /** |
560 | * @SERVICE@ |
||
561 | * |
||
562 | * @param code |
||
563 | * @return |
||
564 | * @throws JSONException |
||
565 | */ |
||
1496 | jmachado | 566 | public 1.5.0/docs/api/java/lang/String.html">String getCourseStudiesPlanXml(1.5.0/docs/api/java/lang/String.html">String code) throws JSONException { |
567 | |||
568 | Course course = DaoFactory.getCourseDaoImpl().findCourseByCode(code); |
||
1501 | jmachado | 569 | |
570 | if(course.getStudiesPlans() == null || course.getStudiesPlans().size() == 0) |
||
571 | { |
||
572 | logger.info("status JSON NOT EXIST FOR STUDIES PLAN IN THIS COURSE, will generate"); |
||
573 | UserSession userSession = DomainObjectFactory.createUserSessionImpl(); |
||
574 | userSession.setUser(DaoFactory.getUserDaoImpl().load(new 1.5.0/docs/api/java/lang/Long.html">Long(1))); |
||
575 | try { |
||
576 | generateXmlJaxbStudiesPlanVersion(userSession, course, false, null); |
||
577 | } catch (JAXBException e) { |
||
578 | logger.error(e,e); |
||
579 | return "<error>" + e.toString() + ". see log for details</error>"; |
||
580 | } catch (TransformationException e) { |
||
581 | logger.error(e, e); |
||
582 | return "<error>" + e.toString() + ". see log for details</error>"; |
||
583 | } |
||
584 | } |
||
585 | |||
1496 | jmachado | 586 | if(course.getStudiesPlans() != null && course.getStudiesPlans().size() > 0) |
587 | { |
||
588 | return course.getStudiesPlans().iterator().next().getXml(); |
||
589 | } |
||
590 | return "<error>Does not exixt</error>"; |
||
591 | |||
592 | } |
||
593 | |||
594 | |||
1500 | jmachado | 595 | /** |
596 | * @SERVICE@ |
||
597 | * |
||
598 | * @param systemUrl |
||
599 | * @param setActive |
||
600 | * @return |
||
601 | * @throws IOException |
||
602 | * @throws JSONException |
||
603 | * @throws TransformationException |
||
604 | * @throws JAXBException |
||
605 | */ |
||
1496 | jmachado | 606 | |
1497 | jmachado | 607 | 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) throws 1.5.0/docs/api/java/io/IOException.html">IOException, JSONException, TransformationException, JAXBException { |
1496 | jmachado | 608 | |
1497 | jmachado | 609 | 1.5.0/docs/api/java/lang/StringBuilder.html">StringBuilder log = new 1.5.0/docs/api/java/lang/StringBuilder.html">StringBuilder(); |
610 | 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 | 611 | 5+0%2Fdocs%2Fapi+InputStream">InputStream is = url.openStream(); |
612 | 1.5.0/docs/api/java/lang/String.html">String str = StreamsUtils.readString(is); |
||
613 | JSONObject obj = new JSONObject(str); |
||
614 | JSONArray courses = obj.getJSONArray("courses"); |
||
615 | for(int i = 0; i < courses.length();i++) |
||
616 | { |
||
617 | JSONObject course = courses.getJSONObject(i); |
||
618 | 1.5.0/docs/api/java/lang/String.html">String code = course.getString("code"); |
||
619 | Course c = DaoFactory.getCourseDaoImpl().findCourseByCode(code); |
||
620 | if(c == null) |
||
621 | { |
||
1497 | jmachado | 622 | 1.5.0/docs/api/java/lang/String.html">String msg = "SKIPING - Course " + code + " " + course.getString("name") + " does not exist in this system"; |
623 | log.append("<info>" + msg+"</info>"); |
||
624 | logger.info(msg); |
||
1496 | jmachado | 625 | } |
626 | else |
||
627 | { |
||
1497 | jmachado | 628 | 1.5.0/docs/api/java/lang/String.html">String msg = "UPDATING - Course " + code + " " + course.getString("name") + " exist in this system"; |
629 | log.append("<info>" + msg+"</info>"); |
||
630 | logger.info(msg); |
||
1500 | jmachado | 631 | |
632 | //#############UPDATING STUDIES PLAN |
||
633 | updateStudiesPlanFromRemoteSystem(systemUrl, setActive, log, course, code, c); |
||
634 | |||
635 | //#############UPDATING Course Comission Members |
||
636 | updateCourseComissionMembers(systemUrl, code, c); |
||
637 | } |
||
638 | } |
||
639 | return log.toString(); |
||
640 | |||
641 | } |
||
642 | |||
643 | /** |
||
644 | * Update courseComission Members |
||
645 | * @param systemUrl |
||
646 | * @param code |
||
647 | * @param c |
||
648 | * @throws IOException |
||
649 | * @throws JSONException |
||
650 | */ |
||
651 | 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 { |
||
652 | 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); |
||
653 | 5+0%2Fdocs%2Fapi+InputStream">InputStream isCourseDetails = urlCourseDetails.openStream(); |
||
654 | 1.5.0/docs/api/java/lang/String.html">String strCourseDetails = StreamsUtils.readString(isCourseDetails); |
||
655 | JSONObject objCourseDetails = new JSONObject(strCourseDetails); |
||
656 | 1.5.0/docs/api/java/lang/String.html">String validationRole = objCourseDetails.getJSONObject("courseInfo").getString("validationRole"); |
||
657 | |||
658 | if(validationRole == null) |
||
659 | { |
||
660 | logger.info("validationRole is not defined"); |
||
661 | } |
||
662 | else |
||
663 | { |
||
664 | logger.info("found validationRole: " + validationRole); |
||
665 | c.setValidationRole(validationRole); |
||
666 | |||
667 | JSONObject coordinator = objCourseDetails.getJSONObject("courseInfo").getJSONObject("coordinator"); |
||
668 | JSONArray courseComission = objCourseDetails.getJSONObject("courseInfo").getJSONArray("courseComission"); |
||
669 | |||
670 | Teacher coordinatorPersistent = findPersonFromCourseDetails(coordinator); |
||
671 | if(coordinatorPersistent == null) |
||
672 | { |
||
673 | logger.warn("Coordinator does not exist in this system "); |
||
674 | } |
||
675 | else |
||
676 | { |
||
677 | c.setCoordinator(coordinatorPersistent); |
||
678 | } |
||
679 | for(int j = 0 ; j < courseComission.length(); j++) |
||
680 | { |
||
681 | JSONObject memberComission = courseComission.getJSONObject(j); |
||
682 | Teacher memberPersistent = findPersonFromCourseDetails(memberComission); |
||
683 | if(memberPersistent == null) |
||
1496 | jmachado | 684 | { |
1500 | jmachado | 685 | logger.info("Member does not exist in this system "); |
1496 | jmachado | 686 | } |
687 | else |
||
688 | { |
||
1500 | jmachado | 689 | logger.info("Adding role of course comission member"); |
690 | if(!memberPersistent.hasRole(validationRole)) |
||
1497 | jmachado | 691 | { |
1500 | jmachado | 692 | memberPersistent.addRole(validationRole); |
1497 | jmachado | 693 | } |
1500 | jmachado | 694 | } |
1497 | jmachado | 695 | |
1496 | jmachado | 696 | } |
697 | } |
||
1500 | jmachado | 698 | } |
1497 | jmachado | 699 | |
1500 | jmachado | 700 | private Teacher findPersonFromCourseDetails(JSONObject coordinator) { |
701 | int code; |
||
702 | try { |
||
703 | if(coordinator.has("sigesCode")) |
||
704 | { |
||
705 | code = coordinator.getInt("sigesCode"); |
||
706 | } |
||
707 | else |
||
708 | { |
||
709 | logger.warn("there is no sigesCode for this person " + coordinator.toString()); |
||
710 | return null; |
||
711 | } |
||
712 | } catch (JSONException e){ |
||
713 | return null; |
||
714 | } catch (1.5.0/docs/api/java/lang/NumberFormatException.html">NumberFormatException e){ |
||
715 | return null; |
||
716 | } |
||
717 | return DaoFactory.getTeacherDaoImpl().loadBySigesCode(code); |
||
1496 | jmachado | 718 | } |
719 | |||
1500 | jmachado | 720 | 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 { |
721 | 1.5.0/docs/api/java/lang/String.html">String msg; |
||
722 | 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).openStream(); |
||
723 | 1.5.0/docs/api/java/lang/String.html">String studiesPlan = StreamsUtils.readString(stream); |
||
724 | int len = studiesPlan.length(); |
||
725 | if(studiesPlan == null || studiesPlan.trim().length() == 0 || studiesPlan.contains("<error>")) |
||
726 | { |
||
727 | msg = "Course " + code + " " + course.getString("name") + " dont has studies plan"; |
||
728 | log.append("<warn>" + msg+"</warn>"); |
||
729 | logger.warn(msg); |
||
730 | } |
||
731 | else |
||
732 | { |
||
733 | msg = "Found studies plan for " + code + " " + course.getString("name") + " will update "; |
||
734 | log.append("<info>" + msg+"</info>"); |
||
735 | logger.info(msg); |
||
736 | if(setActive) |
||
737 | { |
||
738 | msg = "Setting course to active"; |
||
739 | log.append("<info>" + msg+"</info>"); |
||
740 | logger.info(msg); |
||
741 | c.setStatus(true); |
||
742 | } |
||
743 | //System.out.println(studiesPlan); |
||
744 | msg = "GENERATING COURSE JSON ...."; |
||
745 | log.append("<info>" + msg+"</info>"); |
||
746 | logger.info(msg); |
||
747 | new CoursesService().generateCourseJson(c); |
||
1496 | jmachado | 748 | |
1500 | jmachado | 749 | msg="GENERATING COURSE STUDIES PLAN JSON ...."; |
750 | log.append("<info>" + msg+"</info>"); |
||
751 | logger.info(msg); |
||
752 | stream.close(); |
||
753 | stream = new 1.5.0/docs/api/java/net/URL.html">URL(systemUrl + "/wsjson/api?service=getStudiesPlanXml&code=" + code).openStream(); |
||
754 | UserSession userSession = DomainObjectFactory.createUserSessionImpl(); |
||
755 | userSession.setUser(DaoFactory.getUserDaoImpl().load(new 1.5.0/docs/api/java/lang/Long.html">Long(1))); |
||
756 | new CoursesService().uploadStudiesPlan(stream, "curso_" + code + ".xml", len,"appication/xml", userSession,c,true,"http://www.estgp.pt"); |
||
757 | } |
||
758 | } |
||
1496 | jmachado | 759 | |
1500 | jmachado | 760 | |
214 | jmachado | 761 | } |