Rev 1327 | Rev 1329 | 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; |
||
12 | import pt.estgp.estgweb.web.exceptions.NotAuthorizedException; |
||
13 | import pt.utl.ist.berserk.logic.serviceManager.IService; |
||
14 | |||
15 | import java.util.HashSet; |
||
16 | import java.util.Set; |
||
17 | |||
1328 | jmachado | 18 | |
1327 | jmachado | 19 | /** |
20 | * Created by jorgemachado on 22/03/16. |
||
21 | */ |
||
22 | public class UpdateCoursesAndUnitsJobService extends ServiceJob implements IService |
||
23 | { |
||
24 | |||
1328 | jmachado | 25 | 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); |
26 | |||
27 | public 1.5.0/docs/api/java/lang/String.html">String[] deserializeCoursesList(QuestionarioImpl questionario) |
||
28 | { |
||
29 | JobServiceTaskImpl job = questionario.getCourseSelectionTaskId() > 0 ? |
||
30 | (JobServiceTaskImpl) DaoFactory.getJobServiceTaskDaoImpl().load(questionario.getCourseSelectionTaskId()) : |
||
31 | null; |
||
32 | if(job == null) |
||
33 | return new 1.5.0/docs/api/java/lang/String.html">String[0]; |
||
34 | |||
35 | 1.5.0/docs/api/java/lang/String.html">String courses = job.getParameter(ServiceJob.JOB_questionario_courseList_KEY).getObject(); |
||
36 | |||
37 | try { |
||
38 | |||
39 | JSONArray jsonArray = new JSONArray(courses); |
||
40 | 1.5.0/docs/api/java/lang/String.html">String[] coursesList = new 1.5.0/docs/api/java/lang/String.html">String[jsonArray.length()]; |
||
41 | for(int i=0;i <jsonArray.length();i++) |
||
42 | { |
||
43 | coursesList[i] = ""+ jsonArray.get(i); |
||
44 | } |
||
45 | return coursesList; |
||
46 | } catch (JSONException e) { |
||
47 | logger.error(e,e); |
||
48 | } |
||
49 | return new 1.5.0/docs/api/java/lang/String.html">String[0]; |
||
50 | } |
||
1327 | jmachado | 51 | //todo falta validar as clearances |
52 | //todo falta meter a tarefa no Questionario |
||
53 | public QuestionarioImpl runJobSelectCourses(long questionarioId,1.5.0/docs/api/java/lang/String.html">String[] coursesList,UserSession session,QuestionarioImpl questionarioCleared) throws NotAuthorizedException { |
||
54 | |||
55 | if(!questionarioCleared.isClear((UserSessionImpl) session,QuestionarioImpl.QuestionarioClearancesOperation.QUESTIONARIO_SCHEDULLE_SELECT_COURSES.name())) |
||
56 | { |
||
57 | throw new NotAuthorizedException("Nao tem autorização para agendar atribuições de cursos"); |
||
58 | } |
||
59 | JSONArray jsonArray = new JSONArray(); |
||
60 | for(1.5.0/docs/api/java/lang/String.html">String sele: coursesList) |
||
61 | { |
||
62 | jsonArray.put(new 1.5.0/docs/api/java/lang/Long.html">Long(sele)); |
||
63 | } |
||
64 | |||
65 | Set<JobServiceTaskParameter> paramsJob = new HashSet<JobServiceTaskParameter>(); |
||
66 | |||
67 | JobServiceTaskParameterImpl paramJobL = DomainObjectFactory.createJobServiceTaskParameterImpl(); |
||
68 | paramJobL.setName(ServiceJob.JOB_questionario_courseList_KEY); |
||
69 | paramJobL.setDescription("lista de cursos a associar"); |
||
70 | paramJobL.setObject(jsonArray.toString()); |
||
71 | paramsJob.add(paramJobL); |
||
72 | |||
73 | |||
74 | JobServiceTaskParameterImpl paramJob = DomainObjectFactory.createJobServiceTaskParameterImpl(); |
||
75 | paramJob.setName(ServiceJob.JOB_questionario_id_KEY); |
||
76 | paramJob.setDescription("ID do questionário"); |
||
77 | paramJob.setObject("" + questionarioId); |
||
78 | paramsJob.add(paramJob); |
||
79 | |||
80 | JobServiceTask job = JobDeamon.createServiceJob(UpdateCoursesAndUnitsJobService.class, session.getUser(), paramsJob, |
||
81 | "Service: Atribuição de Cursos e Unidades a um questionário Pedagógico"); |
||
82 | questionarioCleared.setCourseSelectionTaskId(job.getId()); |
||
83 | return questionarioCleared; |
||
84 | } |
||
85 | |||
86 | @1.5.0/docs/api/java/lang/Override.html">Override |
||
87 | protected ILogMessages runJobServiceTask() throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable { |
||
88 | 1.5.0/docs/api/java/lang/System.html">System.out.println("RUNNING DEMO"); |
||
1328 | jmachado | 89 | long total = 60000; |
90 | long start = 1.5.0/docs/api/java/lang/System.html">System.currentTimeMillis(); |
||
91 | //Thread.sleep(20000); |
||
92 | while(true) |
||
93 | { |
||
94 | 1.5.0/docs/api/java/lang/Thread.html">Thread.sleep(5000); |
||
95 | long elapsed = 1.5.0/docs/api/java/lang/System.html">System.currentTimeMillis() - start; |
||
96 | setProgress((int) (((float)elapsed)/((float)total)*100.0f)); |
||
97 | commitPartially(); |
||
98 | if(1.5.0/docs/api/java/lang/System.html">System.currentTimeMillis() - start > total) |
||
99 | break; |
||
100 | } |
||
101 | setProgress(100); |
||
102 | |||
1327 | jmachado | 103 | return new DefaultLogMessages(); |
104 | } |
||
105 | } |