Rev 1329 | Rev 1333 | 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; |
||
1332 | jmachado | 11 | import pt.estgp.estgweb.services.logresults.LogMessageTypeEnum; |
12 | import pt.estgp.estgweb.services.logresults.impl.DefaultLogMessage; |
||
1327 | jmachado | 13 | import pt.estgp.estgweb.services.logresults.impl.DefaultLogMessages; |
1329 | jmachado | 14 | import pt.estgp.estgweb.services.questionarios.QuestionariosService; |
1327 | jmachado | 15 | import pt.estgp.estgweb.web.exceptions.NotAuthorizedException; |
16 | import pt.utl.ist.berserk.logic.serviceManager.IService; |
||
17 | |||
1332 | jmachado | 18 | import java.util.ArrayList; |
1327 | jmachado | 19 | import java.util.HashSet; |
1329 | jmachado | 20 | import java.util.List; |
1327 | jmachado | 21 | import java.util.Set; |
22 | |||
1328 | jmachado | 23 | |
1327 | jmachado | 24 | /** |
25 | * Created by jorgemachado on 22/03/16. |
||
26 | */ |
||
27 | public class UpdateCoursesAndUnitsJobService extends ServiceJob implements IService |
||
28 | { |
||
29 | |||
1328 | jmachado | 30 | 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); |
31 | |||
1329 | jmachado | 32 | //return similar questionarios in same year, semestre and history category |
33 | 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 |
||
34 | { |
||
35 | if(questionarioCleared.isClear((UserSessionImpl) userSession,QuestionarioImpl.QuestionarioClearancesOperation.QUESTIONARIO_SET_ANO_SEMESTRE.name())) |
||
36 | { |
||
37 | questionarioCleared.setYear(year); |
||
38 | questionarioCleared.setSemestre(semestre); |
||
39 | questionarioCleared.setSubStatePedagogicoClass(QuestionarioImpl.SubStatePedagogico.YEAR_SEMESTRE_ASSIGNED); |
||
40 | List<QuestionarioImpl> similar = DaoFactory.getQuestionarioDaoImpl().findAnoSemestre(year, semestre, questionarioCleared.getQuestionarioHistoryDrive().getId()); |
||
41 | new QuestionariosService().loadInit(similar); |
||
42 | return similar; |
||
43 | } |
||
44 | else |
||
45 | throw new NotAuthorizedException("Não pode alterar o formulário porque o questionário está num estado avançado"); |
||
46 | } |
||
47 | |||
48 | |||
1328 | jmachado | 49 | public 1.5.0/docs/api/java/lang/String.html">String[] deserializeCoursesList(QuestionarioImpl questionario) |
50 | { |
||
51 | JobServiceTaskImpl job = questionario.getCourseSelectionTaskId() > 0 ? |
||
52 | (JobServiceTaskImpl) DaoFactory.getJobServiceTaskDaoImpl().load(questionario.getCourseSelectionTaskId()) : |
||
53 | null; |
||
54 | if(job == null) |
||
55 | return new 1.5.0/docs/api/java/lang/String.html">String[0]; |
||
56 | |||
57 | 1.5.0/docs/api/java/lang/String.html">String courses = job.getParameter(ServiceJob.JOB_questionario_courseList_KEY).getObject(); |
||
1332 | jmachado | 58 | return deserializeCoursesList(courses); |
59 | } |
||
1328 | jmachado | 60 | |
1332 | jmachado | 61 | public List<Course> getCoursesList(1.5.0/docs/api/java/lang/String.html">String coursesListJson) |
62 | { |
||
63 | List<Course> courses = new ArrayList<Course>(); |
||
64 | 1.5.0/docs/api/java/lang/String.html">String[] courseIds = deserializeCoursesList(coursesListJson); |
||
65 | for(1.5.0/docs/api/java/lang/String.html">String courseId: courseIds) |
||
66 | { |
||
67 | try |
||
68 | { |
||
69 | Course c = DaoFactory.getCourseDaoImpl().load(new 1.5.0/docs/api/java/lang/Long.html">Long(courseId)); |
||
70 | courses.add(c); |
||
71 | } |
||
72 | catch(1.5.0/docs/api/java/lang/Throwable.html">Throwable e) |
||
73 | { |
||
74 | logger.info("Course not found in course list with id:" + courseId); |
||
75 | } |
||
76 | } |
||
77 | return courses; |
||
78 | } |
||
79 | |||
80 | public 1.5.0/docs/api/java/lang/String.html">String[] deserializeCoursesList(1.5.0/docs/api/java/lang/String.html">String json) |
||
81 | { |
||
1328 | jmachado | 82 | try { |
83 | |||
1332 | jmachado | 84 | JSONArray jsonArray = new JSONArray(json); |
1328 | jmachado | 85 | 1.5.0/docs/api/java/lang/String.html">String[] coursesList = new 1.5.0/docs/api/java/lang/String.html">String[jsonArray.length()]; |
86 | for(int i=0;i <jsonArray.length();i++) |
||
87 | { |
||
88 | coursesList[i] = ""+ jsonArray.get(i); |
||
89 | } |
||
90 | return coursesList; |
||
91 | } catch (JSONException e) { |
||
92 | logger.error(e,e); |
||
93 | } |
||
94 | return new 1.5.0/docs/api/java/lang/String.html">String[0]; |
||
95 | } |
||
1327 | jmachado | 96 | //todo falta validar as clearances |
97 | //todo falta meter a tarefa no Questionario |
||
98 | public QuestionarioImpl runJobSelectCourses(long questionarioId,1.5.0/docs/api/java/lang/String.html">String[] coursesList,UserSession session,QuestionarioImpl questionarioCleared) throws NotAuthorizedException { |
||
99 | |||
100 | if(!questionarioCleared.isClear((UserSessionImpl) session,QuestionarioImpl.QuestionarioClearancesOperation.QUESTIONARIO_SCHEDULLE_SELECT_COURSES.name())) |
||
101 | { |
||
102 | throw new NotAuthorizedException("Nao tem autorização para agendar atribuições de cursos"); |
||
103 | } |
||
1329 | jmachado | 104 | ((QuestionarioImpl) questionarioCleared).setSubStatePedagogicoClass(QuestionarioImpl.SubStatePedagogico.ASSIGNING_COURSES); |
1327 | jmachado | 105 | JSONArray jsonArray = new JSONArray(); |
106 | for(1.5.0/docs/api/java/lang/String.html">String sele: coursesList) |
||
107 | { |
||
108 | jsonArray.put(new 1.5.0/docs/api/java/lang/Long.html">Long(sele)); |
||
109 | } |
||
110 | |||
111 | Set<JobServiceTaskParameter> paramsJob = new HashSet<JobServiceTaskParameter>(); |
||
112 | |||
113 | JobServiceTaskParameterImpl paramJobL = DomainObjectFactory.createJobServiceTaskParameterImpl(); |
||
114 | paramJobL.setName(ServiceJob.JOB_questionario_courseList_KEY); |
||
115 | paramJobL.setDescription("lista de cursos a associar"); |
||
116 | paramJobL.setObject(jsonArray.toString()); |
||
117 | paramsJob.add(paramJobL); |
||
118 | |||
119 | |||
120 | JobServiceTaskParameterImpl paramJob = DomainObjectFactory.createJobServiceTaskParameterImpl(); |
||
121 | paramJob.setName(ServiceJob.JOB_questionario_id_KEY); |
||
122 | paramJob.setDescription("ID do questionário"); |
||
123 | paramJob.setObject("" + questionarioId); |
||
124 | paramsJob.add(paramJob); |
||
125 | |||
126 | JobServiceTask job = JobDeamon.createServiceJob(UpdateCoursesAndUnitsJobService.class, session.getUser(), paramsJob, |
||
127 | "Service: Atribuição de Cursos e Unidades a um questionário Pedagógico"); |
||
128 | questionarioCleared.setCourseSelectionTaskId(job.getId()); |
||
129 | return questionarioCleared; |
||
130 | } |
||
131 | |||
1332 | jmachado | 132 | |
1327 | jmachado | 133 | @1.5.0/docs/api/java/lang/Override.html">Override |
1329 | jmachado | 134 | protected ILogMessages runJobServiceTask() throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable |
135 | { |
||
1332 | jmachado | 136 | DefaultLogMessages logMessages = new DefaultLogMessages(); |
1329 | jmachado | 137 | long questionarioId = 1.5.0/docs/api/java/lang/Long.html">Long.parseLong(getParametersMap().get(ServiceJob.JOB_questionario_id_KEY).getObject()); |
138 | QuestionarioImpl q = (QuestionarioImpl) DaoFactory.getQuestionarioDaoImpl().load(questionarioId); |
||
139 | |||
1332 | jmachado | 140 | 1.5.0/docs/api/java/lang/String.html">String[] coursesList = deserializeCoursesList(q); |
1329 | jmachado | 141 | //Colocamos já aqui o estado para nao se poder mudar o Ano e o Semestre |
142 | |||
1332 | jmachado | 143 | serviceLogInfo("Starting running service "); |
144 | logger.info("Starting running service"); |
||
145 | |||
146 | int count = 0; |
||
147 | int totalCourses = coursesList.length; |
||
148 | for(1.5.0/docs/api/java/lang/String.html">String courseStrId: coursesList) |
||
1328 | jmachado | 149 | { |
1332 | jmachado | 150 | count++; |
151 | try{ |
||
152 | 1.5.0/docs/api/java/lang/Long.html">Long courseLong = 1.5.0/docs/api/java/lang/Long.html">Long.parseLong(courseStrId); |
||
153 | /** logging **/ |
||
154 | |||
155 | 1.5.0/docs/api/java/lang/String.html">String msg = "Loading Course BACO ID: " + courseStrId; |
||
156 | serviceLogInfo(msg); |
||
157 | logger.info(msg); |
||
158 | /** logging **/ |
||
159 | |||
160 | Course c = DaoFactory.getCourseDaoImpl().load(courseLong); |
||
161 | |||
162 | QuestionarioPedagogicoCursoAfeto cursoAfeto = DomainObjectFactory.createQuestionarioPedagogicoCursoAfeto(); |
||
163 | cursoAfeto.setCourse(c); |
||
164 | cursoAfeto.setAno(q.getYear()); |
||
165 | cursoAfeto.setCodigoCurso(c.getCode()); |
||
166 | cursoAfeto.setNome(c.getName()); |
||
167 | cursoAfeto.setQuestionario(q); |
||
168 | cursoAfeto.setSemestre(q.getSemestre()); |
||
169 | cursoAfeto.setUnidadesAfetas(new HashSet<QuestionarioPedagogicoUnidadeCurricularAfeta>()); |
||
170 | |||
171 | DaoFactory.getQuestionarioPedagogicoCursoAfetoDaoImpl().save(cursoAfeto); |
||
172 | |||
173 | /** logging **/ |
||
174 | msg = "(" + count + " of " + totalCourses + ")-> Course loaded: " + c.getName() + "(" + c.getCode() + ")"; |
||
175 | serviceLogInfo(msg); |
||
176 | logger.info(msg); |
||
177 | /** logging **/ |
||
178 | |||
179 | int countUnits = 0; |
||
180 | int totalUnits = c.getCourseUnits().size(); |
||
181 | for(CourseUnit cu : c.getCourseUnits()) |
||
182 | { |
||
183 | countUnits++; |
||
184 | /** logging **/ |
||
185 | msg = "---->(" + countUnits + " of " + totalUnits +") CourseUnit loaded: " + cu.getName() + "(" + cu.getCode() + ")"; |
||
186 | serviceLogInfo(msg); |
||
187 | logger.info(msg); |
||
188 | /** logging **/ |
||
189 | |||
190 | |||
191 | QuestionarioPedagogicoUnidadeCurricularAfetaImpl unidadeAfeta = DomainObjectFactory.createQuestionarioPedagogicoUnidadeCurricularAfeta(); |
||
192 | unidadeAfeta.setCourseUnit(cu); |
||
193 | unidadeAfeta.setObs(""); |
||
194 | |||
195 | unidadeAfeta.setCodigoUnidade(cu.getCode()); |
||
196 | cursoAfeto.getUnidadesAfetas().add(unidadeAfeta);//so metemos de um dos lados |
||
197 | |||
198 | unidadeAfeta.setMarked(false); |
||
199 | if(cu.getTeachers() == null || cu.getTeachers().size() == 0) |
||
200 | { |
||
201 | unidadeAfeta.setProfs(0); |
||
202 | unidadeAfeta.setMarked(true); //WARNING |
||
203 | unidadeAfeta.addObs("Não tem professores"); |
||
204 | } |
||
205 | else |
||
206 | unidadeAfeta.setProfs(cu.getTeachers().size()); |
||
207 | |||
208 | //todo falta as turmas verificacao de numero de profs |
||
209 | // testar se ja tem notas acrescentar |
||
210 | try{ |
||
211 | unidadeAfeta.setTipologiasJson(((CourseUnitImpl)cu).getTipologiasClass().obtainMergeTipologiasSerialized()); |
||
212 | } |
||
213 | catch(1.5.0/docs/api/java/lang/Throwable.html">Throwable e) |
||
214 | { |
||
215 | unidadeAfeta.setMarked(true); |
||
216 | unidadeAfeta.setTipologiasJson(CourseUnitImpl.Tipologias.serializeMergedList(new ArrayList<CourseUnitImpl.Tipologia>())); |
||
217 | cursoAfeto.setMarcadas(cursoAfeto.getMarcadas()+1); |
||
218 | //todo colocar as observacoes auqi e na unidade marcada |
||
219 | } |
||
220 | |||
221 | |||
222 | cursoAfeto.setCourse(c); |
||
223 | cursoAfeto.setAno(q.getYear()); |
||
224 | cursoAfeto.setCodigoCurso(c.getCode()); |
||
225 | cursoAfeto.setNome(c.getName()); |
||
226 | cursoAfeto.setQuestionario(q); |
||
227 | cursoAfeto.setSemestre(q.getSemestre()); |
||
228 | cursoAfeto.setUnidadesAfetas(new HashSet<QuestionarioPedagogicoUnidadeCurricularAfeta>()); |
||
229 | |||
230 | DaoFactory.getQuestionarioPedagogicoCursoAfetoDaoImpl().save(cursoAfeto); |
||
231 | |||
232 | |||
233 | } |
||
234 | setProgress((int)((((float)count)/((float)totalCourses))*100.0f)); |
||
235 | commitPartially(); |
||
236 | } |
||
237 | catch(1.5.0/docs/api/java/lang/Throwable.html">Throwable t) |
||
238 | { |
||
239 | logger.error(t,t); |
||
240 | serviceLogError(t.toString(),t); |
||
241 | logMessages.addMessage(new DefaultLogMessage("questionario.courses.selection", LogMessageTypeEnum.ERROR,t.toString())); |
||
242 | } |
||
243 | |||
1328 | jmachado | 244 | } |
245 | setProgress(100); |
||
1332 | jmachado | 246 | commitPartially(); |
1328 | jmachado | 247 | |
1332 | jmachado | 248 | 1.5.0/docs/api/java/lang/String.html">String msg = "Set courses state as " + QuestionarioImpl.SubStatePedagogico.COURSES_ASSIGNED.name(); |
249 | serviceLogInfo(msg); |
||
250 | logger.info(msg); |
||
1329 | jmachado | 251 | q.setSubStatePedagogicoClass(QuestionarioImpl.SubStatePedagogico.COURSES_ASSIGNED); |
252 | commitPartially(); |
||
1332 | jmachado | 253 | |
254 | return logMessages; |
||
1327 | jmachado | 255 | } |
256 | } |