Rev 1328 | Rev 1332 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1327 | jmachado | 1 | package pt.estgp.estgweb.services.questionarios.pedagogico; |
2 | |||
1328 | jmachado | 3 | import org.apache.log4j.Logger; |
1327 | jmachado | 4 | import org.json.JSONArray; |
1328 | jmachado | 5 | import org.json.JSONException; |
1327 | jmachado | 6 | import pt.estgp.estgweb.domain.*; |
1328 | jmachado | 7 | import pt.estgp.estgweb.domain.dao.DaoFactory; |
1327 | jmachado | 8 | import pt.estgp.estgweb.services.jobs.JobDeamon; |
9 | import pt.estgp.estgweb.services.jobs.ServiceJob; |
||
10 | import pt.estgp.estgweb.services.logresults.ILogMessages; |
||
11 | import pt.estgp.estgweb.services.logresults.impl.DefaultLogMessages; |
||
1329 | jmachado | 12 | import pt.estgp.estgweb.services.questionarios.QuestionariosService; |
1327 | jmachado | 13 | import pt.estgp.estgweb.web.exceptions.NotAuthorizedException; |
14 | import pt.utl.ist.berserk.logic.serviceManager.IService; |
||
15 | |||
16 | import java.util.HashSet; |
||
1329 | jmachado | 17 | import java.util.List; |
1327 | jmachado | 18 | import java.util.Set; |
19 | |||
1328 | jmachado | 20 | |
1327 | jmachado | 21 | /** |
22 | * Created by jorgemachado on 22/03/16. |
||
23 | */ |
||
24 | public class UpdateCoursesAndUnitsJobService extends ServiceJob implements IService |
||
25 | { |
||
26 | |||
1328 | jmachado | 27 | public 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(UpdateCoursesAndUnitsJobService.class); |
28 | |||
1329 | jmachado | 29 | //return similar questionarios in same year, semestre and history category |
30 | public List<QuestionarioImpl> setAnoSemestre(1.5.0/docs/api/java/lang/String.html">String year, 1.5.0/docs/api/java/lang/String.html">String semestre, long questionarioId, UserSession userSession,QuestionarioImpl questionarioCleared) throws NotAuthorizedException |
||
31 | { |
||
32 | if(questionarioCleared.isClear((UserSessionImpl) userSession,QuestionarioImpl.QuestionarioClearancesOperation.QUESTIONARIO_SET_ANO_SEMESTRE.name())) |
||
33 | { |
||
34 | questionarioCleared.setYear(year); |
||
35 | questionarioCleared.setSemestre(semestre); |
||
36 | questionarioCleared.setSubStatePedagogicoClass(QuestionarioImpl.SubStatePedagogico.YEAR_SEMESTRE_ASSIGNED); |
||
37 | List<QuestionarioImpl> similar = DaoFactory.getQuestionarioDaoImpl().findAnoSemestre(year, semestre, questionarioCleared.getQuestionarioHistoryDrive().getId()); |
||
38 | new QuestionariosService().loadInit(similar); |
||
39 | return similar; |
||
40 | } |
||
41 | else |
||
42 | throw new NotAuthorizedException("Não pode alterar o formulário porque o questionário está num estado avançado"); |
||
43 | } |
||
44 | |||
45 | |||
1328 | jmachado | 46 | public 1.5.0/docs/api/java/lang/String.html">String[] deserializeCoursesList(QuestionarioImpl questionario) |
47 | { |
||
48 | JobServiceTaskImpl job = questionario.getCourseSelectionTaskId() > 0 ? |
||
49 | (JobServiceTaskImpl) DaoFactory.getJobServiceTaskDaoImpl().load(questionario.getCourseSelectionTaskId()) : |
||
50 | null; |
||
51 | if(job == null) |
||
52 | return new 1.5.0/docs/api/java/lang/String.html">String[0]; |
||
53 | |||
54 | 1.5.0/docs/api/java/lang/String.html">String courses = job.getParameter(ServiceJob.JOB_questionario_courseList_KEY).getObject(); |
||
55 | |||
56 | try { |
||
57 | |||
58 | JSONArray jsonArray = new JSONArray(courses); |
||
59 | 1.5.0/docs/api/java/lang/String.html">String[] coursesList = new 1.5.0/docs/api/java/lang/String.html">String[jsonArray.length()]; |
||
60 | for(int i=0;i <jsonArray.length();i++) |
||
61 | { |
||
62 | coursesList[i] = ""+ jsonArray.get(i); |
||
63 | } |
||
64 | return coursesList; |
||
65 | } catch (JSONException e) { |
||
66 | logger.error(e,e); |
||
67 | } |
||
68 | return new 1.5.0/docs/api/java/lang/String.html">String[0]; |
||
69 | } |
||
1327 | jmachado | 70 | //todo falta validar as clearances |
71 | //todo falta meter a tarefa no Questionario |
||
72 | public QuestionarioImpl runJobSelectCourses(long questionarioId,1.5.0/docs/api/java/lang/String.html">String[] coursesList,UserSession session,QuestionarioImpl questionarioCleared) throws NotAuthorizedException { |
||
73 | |||
74 | if(!questionarioCleared.isClear((UserSessionImpl) session,QuestionarioImpl.QuestionarioClearancesOperation.QUESTIONARIO_SCHEDULLE_SELECT_COURSES.name())) |
||
75 | { |
||
76 | throw new NotAuthorizedException("Nao tem autorização para agendar atribuições de cursos"); |
||
77 | } |
||
1329 | jmachado | 78 | ((QuestionarioImpl) questionarioCleared).setSubStatePedagogicoClass(QuestionarioImpl.SubStatePedagogico.ASSIGNING_COURSES); |
1327 | jmachado | 79 | JSONArray jsonArray = new JSONArray(); |
80 | for(1.5.0/docs/api/java/lang/String.html">String sele: coursesList) |
||
81 | { |
||
82 | jsonArray.put(new 1.5.0/docs/api/java/lang/Long.html">Long(sele)); |
||
83 | } |
||
84 | |||
85 | Set<JobServiceTaskParameter> paramsJob = new HashSet<JobServiceTaskParameter>(); |
||
86 | |||
87 | JobServiceTaskParameterImpl paramJobL = DomainObjectFactory.createJobServiceTaskParameterImpl(); |
||
88 | paramJobL.setName(ServiceJob.JOB_questionario_courseList_KEY); |
||
89 | paramJobL.setDescription("lista de cursos a associar"); |
||
90 | paramJobL.setObject(jsonArray.toString()); |
||
91 | paramsJob.add(paramJobL); |
||
92 | |||
93 | |||
94 | JobServiceTaskParameterImpl paramJob = DomainObjectFactory.createJobServiceTaskParameterImpl(); |
||
95 | paramJob.setName(ServiceJob.JOB_questionario_id_KEY); |
||
96 | paramJob.setDescription("ID do questionário"); |
||
97 | paramJob.setObject("" + questionarioId); |
||
98 | paramsJob.add(paramJob); |
||
99 | |||
100 | JobServiceTask job = JobDeamon.createServiceJob(UpdateCoursesAndUnitsJobService.class, session.getUser(), paramsJob, |
||
101 | "Service: Atribuição de Cursos e Unidades a um questionário Pedagógico"); |
||
102 | questionarioCleared.setCourseSelectionTaskId(job.getId()); |
||
103 | return questionarioCleared; |
||
104 | } |
||
105 | |||
106 | @1.5.0/docs/api/java/lang/Override.html">Override |
||
1329 | jmachado | 107 | protected ILogMessages runJobServiceTask() throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable |
108 | { |
||
109 | long questionarioId = 1.5.0/docs/api/java/lang/Long.html">Long.parseLong(getParametersMap().get(ServiceJob.JOB_questionario_id_KEY).getObject()); |
||
110 | QuestionarioImpl q = (QuestionarioImpl) DaoFactory.getQuestionarioDaoImpl().load(questionarioId); |
||
111 | |||
112 | 1.5.0/docs/api/java/lang/String.html">String[] coursesList = deserializeCoursesList((QuestionarioImpl) q); |
||
113 | //Colocamos já aqui o estado para nao se poder mudar o Ano e o Semestre |
||
114 | |||
115 | serviceLogInfo("Starting running service "); |
||
116 | long total = 5000; |
||
1328 | jmachado | 117 | long start = 1.5.0/docs/api/java/lang/System.html">System.currentTimeMillis(); |
118 | //Thread.sleep(20000); |
||
119 | while(true) |
||
120 | { |
||
121 | 1.5.0/docs/api/java/lang/Thread.html">Thread.sleep(5000); |
||
122 | long elapsed = 1.5.0/docs/api/java/lang/System.html">System.currentTimeMillis() - start; |
||
123 | setProgress((int) (((float)elapsed)/((float)total)*100.0f)); |
||
124 | commitPartially(); |
||
1329 | jmachado | 125 | DaoFactory.getQuestionarioDaoImpl().reattach(q); |
1328 | jmachado | 126 | if(1.5.0/docs/api/java/lang/System.html">System.currentTimeMillis() - start > total) |
127 | break; |
||
128 | } |
||
129 | setProgress(100); |
||
130 | |||
1329 | jmachado | 131 | serviceLogInfo("Set courses state as " + QuestionarioImpl.SubStatePedagogico.COURSES_ASSIGNED.name()); |
132 | q.setSubStatePedagogicoClass(QuestionarioImpl.SubStatePedagogico.COURSES_ASSIGNED); |
||
133 | commitPartially(); |
||
1327 | jmachado | 134 | return new DefaultLogMessages(); |
135 | } |
||
136 | } |