Subversion Repositories bacoAlunos

Rev

Rev 1332 | Rev 1335 | 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
 
1333 jmachado 3
import com.owlike.genson.TransformationException;
1328 jmachado 4
import org.apache.log4j.Logger;
1327 jmachado 5
import org.json.JSONArray;
1328 jmachado 6
import org.json.JSONException;
1327 jmachado 7
import pt.estgp.estgweb.domain.*;
1328 jmachado 8
import pt.estgp.estgweb.domain.dao.DaoFactory;
1333 jmachado 9
import pt.estgp.estgweb.domain.dao.impl.CourseUnitDaoImpl;
1327 jmachado 10
import pt.estgp.estgweb.services.jobs.JobDeamon;
11
import pt.estgp.estgweb.services.jobs.ServiceJob;
12
import pt.estgp.estgweb.services.logresults.ILogMessages;
1332 jmachado 13
import pt.estgp.estgweb.services.logresults.LogMessageTypeEnum;
14
import pt.estgp.estgweb.services.logresults.impl.DefaultLogMessage;
1327 jmachado 15
import pt.estgp.estgweb.services.logresults.impl.DefaultLogMessages;
1329 jmachado 16
import pt.estgp.estgweb.services.questionarios.QuestionariosService;
1327 jmachado 17
import pt.estgp.estgweb.web.exceptions.NotAuthorizedException;
18
import pt.utl.ist.berserk.logic.serviceManager.IService;
19
 
1333 jmachado 20
import java.io.IOException;
21
import java.util.*;
1327 jmachado 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
    }
1333 jmachado 96
 
1327 jmachado 97
    public QuestionarioImpl runJobSelectCourses(long questionarioId,1.5.0/docs/api/java/lang/String.html">String[] coursesList,UserSession session,QuestionarioImpl questionarioCleared) throws NotAuthorizedException {
98
 
99
        if(!questionarioCleared.isClear((UserSessionImpl) session,QuestionarioImpl.QuestionarioClearancesOperation.QUESTIONARIO_SCHEDULLE_SELECT_COURSES.name()))
100
        {
101
            throw new NotAuthorizedException("Nao tem autorização para agendar atribuições de cursos");
102
        }
1333 jmachado 103
        ( questionarioCleared).setSubStatePedagogicoClass(QuestionarioImpl.SubStatePedagogico.ASSIGNING_COURSES);
1327 jmachado 104
        JSONArray jsonArray = new JSONArray();
105
        for(1.5.0/docs/api/java/lang/String.html">String sele: coursesList)
106
        {
107
            jsonArray.put(new 1.5.0/docs/api/java/lang/Long.html">Long(sele));
108
        }
109
 
110
        Set<JobServiceTaskParameter> paramsJob = new HashSet<JobServiceTaskParameter>();
111
 
112
        JobServiceTaskParameterImpl paramJobL = DomainObjectFactory.createJobServiceTaskParameterImpl();
113
        paramJobL.setName(ServiceJob.JOB_questionario_courseList_KEY);
114
        paramJobL.setDescription("lista de cursos a associar");
115
        paramJobL.setObject(jsonArray.toString());
116
        paramsJob.add(paramJobL);
117
 
118
 
119
        JobServiceTaskParameterImpl paramJob = DomainObjectFactory.createJobServiceTaskParameterImpl();
120
        paramJob.setName(ServiceJob.JOB_questionario_id_KEY);
121
        paramJob.setDescription("ID do questionário");
122
        paramJob.setObject("" + questionarioId);
123
        paramsJob.add(paramJob);
124
 
125
        JobServiceTask job = JobDeamon.createServiceJob(UpdateCoursesAndUnitsJobService.class, session.getUser(), paramsJob,
126
                "Service: Atribuição de Cursos e Unidades a um questionário Pedagógico");
127
        questionarioCleared.setCourseSelectionTaskId(job.getId());
128
        return questionarioCleared;
129
    }
130
 
1332 jmachado 131
 
1333 jmachado 132
    /**
133
     *
134
     * @return
135
     * @throws Throwable
136
     */
1327 jmachado 137
    @1.5.0/docs/api/java/lang/Override.html">Override
1329 jmachado 138
    protected ILogMessages runJobServiceTask() throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable
139
    {
1333 jmachado 140
 
1332 jmachado 141
        DefaultLogMessages logMessages = new DefaultLogMessages();
1329 jmachado 142
        long questionarioId = 1.5.0/docs/api/java/lang/Long.html">Long.parseLong(getParametersMap().get(ServiceJob.JOB_questionario_id_KEY).getObject());
143
        QuestionarioImpl q = (QuestionarioImpl) DaoFactory.getQuestionarioDaoImpl().load(questionarioId);
144
 
1332 jmachado 145
        1.5.0/docs/api/java/lang/String.html">String[] coursesList = deserializeCoursesList(q);
1329 jmachado 146
        //Colocamos já aqui o estado para nao se poder mudar o Ano e o Semestre
147
 
1333 jmachado 148
        /**logging**/
149
        1.5.0/docs/api/java/lang/String.html">String msg = "Starting running service for questionario: " + q.getId() + " - " + q.getiAnswersProcessorClass().getTitle();
150
        serviceLogInfo(msg);
151
        logger.info(msg);
152
        /**logging**/
1332 jmachado 153
 
1333 jmachado 154
        List<String> newCourseCodesList = new ArrayList<String>();
1332 jmachado 155
        int count = 0;
156
        int totalCourses = coursesList.length;
157
        for(1.5.0/docs/api/java/lang/String.html">String courseStrId: coursesList)
1328 jmachado 158
        {
1332 jmachado 159
            count++;
160
            try{
161
                1.5.0/docs/api/java/lang/Long.html">Long courseLong = 1.5.0/docs/api/java/lang/Long.html">Long.parseLong(courseStrId);
162
                /** logging **/
1333 jmachado 163
                msg = "Loading Course BACO ID: " + courseStrId;
1332 jmachado 164
                serviceLogInfo(msg);
165
                logger.info(msg);
166
                /** logging **/
167
 
168
                Course c = DaoFactory.getCourseDaoImpl().load(courseLong);
1333 jmachado 169
                newCourseCodesList.add(c.getCode());
1332 jmachado 170
                /** logging **/
171
                msg = "(" + count + " of " + totalCourses + ")-> Course loaded: " + c.getName() + "(" + c.getCode() + ")";
172
                serviceLogInfo(msg);
173
                logger.info(msg);
174
                /** logging **/
175
 
1333 jmachado 176
                createCursoAfeto(q, c, logMessages);
1332 jmachado 177
 
1333 jmachado 178
                /** COMMIT OPERATION **/
1332 jmachado 179
                setProgress((int)((((float)count)/((float)totalCourses))*100.0f));
180
                commitPartially();
1333 jmachado 181
                DaoFactory.getQuestionarioDaoImpl().update(q);
182
                /** COMMIT OPERATION **/
1332 jmachado 183
            }
184
            catch(1.5.0/docs/api/java/lang/Throwable.html">Throwable t)
185
            {
186
                logger.error(t,t);
187
                serviceLogError(t.toString(),t);
188
                logMessages.addMessage(new DefaultLogMessage("questionario.courses.selection", LogMessageTypeEnum.ERROR,t.toString()));
189
            }
190
 
1328 jmachado 191
        }
1333 jmachado 192
 
193
        removeUnselectedCourses(q, newCourseCodesList);
194
 
195
        /** COMMIT OPERATION **/
1328 jmachado 196
        setProgress(100);
1332 jmachado 197
        commitPartially();
1333 jmachado 198
        DaoFactory.getQuestionarioDaoImpl().update(q);
199
        /** COMMIT OPERATION **/
1328 jmachado 200
 
1333 jmachado 201
 
202
        msg = "Set courses state as " + QuestionarioImpl.SubStatePedagogico.COURSES_ASSIGNED.name();
1332 jmachado 203
        serviceLogInfo(msg);
204
        logger.info(msg);
1329 jmachado 205
        q.setSubStatePedagogicoClass(QuestionarioImpl.SubStatePedagogico.COURSES_ASSIGNED);
206
        commitPartially();
1332 jmachado 207
 
208
        return logMessages;
1327 jmachado 209
    }
1333 jmachado 210
 
211
    /**
212
     *
213
     * @param q
214
     * @param newCourseCodesList
215
     */
216
    private void removeUnselectedCourses(QuestionarioImpl q, List<String> newCourseCodesList) {
217
        /** logging **/
218
        1.5.0/docs/api/java/lang/String.html">String msg = "Removing unselected courses ";
219
        serviceLogInfo(msg);
220
        logger.info(msg);
221
        /** logging **/
222
        List<QuestionarioPedagogicoCursoAfeto> allCoursesInQuestionario =  DaoFactory.getQuestionarioPedagogicoCursoAfetoDaoImpl().find(q.getId());
223
        for(QuestionarioPedagogicoCursoAfeto cursoAfeto: allCoursesInQuestionario)
224
        {
225
            if(!newCourseCodesList.contains(cursoAfeto.getCodigoCurso()))
226
            {
227
                removeCursoAfeto(cursoAfeto);
228
            }
229
        }
230
    }
231
 
232
    private void removeCursoAfeto(QuestionarioPedagogicoCursoAfeto cursoAfeto) {
233
        1.5.0/docs/api/java/lang/String.html">String msg; /** logging **/
234
        msg = "Deleting course: " + cursoAfeto.getCodigoCurso();
235
        serviceLogInfo(msg);
236
        logger.info(msg);
237
        /** logging **/
238
        for(QuestionarioPedagogicoUnidadeCurricularAfeta unidade: cursoAfeto.getUnidadesAfetas())
239
        {
240
            /** logging **/
241
            msg = "--->Deleting unit from course: " + unidade.getCodigoUnidade();
242
            serviceLogInfo(msg);
243
            logger.info(msg);
244
            /** logging **/
245
            DaoFactory.getQuestionarioPedagogicoUnidadeCurricularAfetaDaoImpl().delete(unidade);
246
        }
247
        cursoAfeto.setUnidadesAfetas(null);
248
        DaoFactory.getQuestionarioPedagogicoCursoAfetoDaoImpl().delete(cursoAfeto);
249
    }
250
 
251
    private void removeUnselectedUnits(QuestionarioPedagogicoCursoAfeto cursoAfetado, List<String> newUnidadesCodesList) {
252
        /** logging **/
253
        1.5.0/docs/api/java/lang/String.html">String msg = "Removing unselected units from course: " + cursoAfetado.getCodigoCurso();
254
        serviceLogInfo(msg);
255
        logger.info(msg);
256
        /** logging **/
257
        Set<QuestionarioPedagogicoUnidadeCurricularAfeta> allUnidades =  cursoAfetado.getUnidadesAfetas();
258
        for(QuestionarioPedagogicoUnidadeCurricularAfeta unidadeAfeta: allUnidades)
259
        {
260
            if(!newUnidadesCodesList.contains(unidadeAfeta.getCodigoUnidade()))
261
            {
262
                /** logging **/
263
                msg = "--->Deleting unidade: " + unidadeAfeta.getCodigoUnidade();
264
                serviceLogInfo(msg);
265
                logger.info(msg);
266
                /** logging **/
267
                DaoFactory.getQuestionarioPedagogicoUnidadeCurricularAfetaDaoImpl().delete(unidadeAfeta);
268
            }
269
        }
270
    }
271
 
272
 
273
    /**
274
     *
275
     * @param q
276
     * @param c
277
     * @throws IOException
278
     * @throws TransformationException
279
     */
280
    private void createCursoAfeto(QuestionarioImpl q, Course c, DefaultLogMessages logMessages) throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException
281
    {
282
        1.5.0/docs/api/java/lang/String.html">String msg;
283
        QuestionarioPedagogicoCursoAfetoImpl cursoAfeto = null;
284
        List<QuestionarioPedagogicoCursoAfeto> cursosFound = DaoFactory.getQuestionarioPedagogicoCursoAfetoDaoImpl().find(q.getId(),c.getCode());
285
        if(cursosFound != null && cursosFound.size() > 1)
286
        {
287
            msg = "Encontrados mais que um curso nos mesmos códigos:" + c.getCode() + " apagando um";
288
            serviceLogWarn(msg);
289
            logger.warn(msg);
290
            logMessages.addMessage(new DefaultLogMessage("questionario.courses.selection", LogMessageTypeEnum.WARNING,msg));
291
            cursoAfeto = (QuestionarioPedagogicoCursoAfetoImpl) cursosFound.get(0);
292
            cursosFound.remove(0);
293
            for(QuestionarioPedagogicoCursoAfeto ca: cursosFound)
294
                removeCursoAfeto(ca);
295
        }
296
        else if(cursosFound != null && cursosFound.size() == 1)
297
        {
298
            msg = "Curso " + c.getCode() + " encontrado, vai atualizar";
299
            serviceLogInfo(msg);
300
            logger.info(msg);
301
            cursoAfeto = (QuestionarioPedagogicoCursoAfetoImpl) cursosFound.get(0);
302
        }
303
        else
304
        {
305
            msg = "Criando novo curso para o código " + c.getCode();
306
            serviceLogInfo(msg);
307
            logger.info(msg);
308
            cursoAfeto = DomainObjectFactory.createQuestionarioPedagogicoCursoAfeto();
309
            DaoFactory.getQuestionarioPedagogicoCursoAfetoDaoImpl().save(cursoAfeto);
310
        }
311
 
312
        cursoAfeto.resetStats();
313
 
314
        cursoAfeto.setCourse(c);
315
        cursoAfeto.setAno(q.getYear());
316
        cursoAfeto.setCodigoCurso(c.getCode());
317
        cursoAfeto.setNome(c.getName());
318
        cursoAfeto.setQuestionario(q);
319
        cursoAfeto.setSemestre(q.getSemestre());
320
        cursoAfeto.setUnidadesAfetas(new HashSet<QuestionarioPedagogicoUnidadeCurricularAfeta>());
321
 
322
 
323
 
324
        List<String> newUnitCodesList = new ArrayList<String>();
325
        int countUnits = 0;
326
        List<CourseUnitDaoImpl.CourseUnitResult> results = DaoFactory.getCourseUnitDaoImpl().loadCourseUnits(c.getId(), q.getSemestre(), q.getYear());
327
        int totalUnits =  results.size();
328
        for(CourseUnitDaoImpl.CourseUnitResult cur : results)
329
        {
330
            countUnits++;
331
            CourseUnit cu = DaoFactory.getCourseUnitDaoImpl().load(cur.id);
332
            newUnitCodesList.add(cu.getCode());
333
            /** logging **/
334
            msg = "---->(" + countUnits + " of " + totalUnits  +") CourseUnit loaded: " + cu.getName() + "(" + cu.getCode() + ")";
335
            serviceLogInfo(msg);
336
            logger.info(msg);
337
            /** logging **/
338
            createUnidadeAfeta(cursoAfeto, cur,cu, logMessages);
339
            DaoFactory.getCourseUnitDaoImpl().evict(cu);
340
        }
341
 
342
        removeUnselectedUnits(cursoAfeto,newUnitCodesList);
343
    }
344
 
345
    /**
346
     *
347
     * @param cursoAfeto
348
     * @param cur
349
     * @throws IOException
350
     * @throws TransformationException
351
     */
352
    private void createUnidadeAfeta(QuestionarioPedagogicoCursoAfetoImpl cursoAfeto, CourseUnitDaoImpl.CourseUnitResult cur, CourseUnit cu, DefaultLogMessages logMessages) throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException {
353
 
354
        1.5.0/docs/api/java/lang/String.html">String msg;
355
        QuestionarioPedagogicoUnidadeCurricularAfetaImpl unidadeAfeta = null;
356
        List<QuestionarioPedagogicoUnidadeCurricularAfeta> unidadesFound = DaoFactory.getQuestionarioPedagogicoUnidadeCurricularAfetaDaoImpl().find(cursoAfeto.getQuestionario().getId(), cursoAfeto.getCodigoCurso(), cu.getCode());
357
        if(unidadesFound != null && unidadesFound.size() > 1)
358
        {
359
            msg = "Encontradas mais que uma unidade nos mesmos códigos: curso:" +cursoAfeto.getCodigoCurso() + ", unidade " + cu.getCode() + " apagando um";
360
            serviceLogWarn(msg);
361
            logger.warn(msg);
362
            logMessages.addMessage(new DefaultLogMessage("questionario.courses.selection", LogMessageTypeEnum.WARNING,msg));
363
            unidadeAfeta = (QuestionarioPedagogicoUnidadeCurricularAfetaImpl) unidadesFound.get(0);
364
            unidadesFound.remove(0);
365
            DaoFactory.getQuestionarioPedagogicoUnidadeCurricularAfetaDaoImpl().delete(unidadesFound);
366
        }
367
        else if(unidadesFound != null && unidadesFound.size() == 1)
368
        {
369
            msg = "Unidade " +  cu.getCode() + " encontrada, vai atualizar";
370
            serviceLogInfo(msg);
371
            logger.info(msg);
372
            unidadeAfeta = (QuestionarioPedagogicoUnidadeCurricularAfetaImpl) unidadesFound.get(0);
373
        }
374
        else
375
        {
376
            msg = "Criando nova unidade para o código " + cu.getCode();
377
            serviceLogInfo(msg);
378
            logger.info(msg);
379
            unidadeAfeta = DomainObjectFactory.createQuestionarioPedagogicoUnidadeCurricularAfeta();
380
            DaoFactory.getQuestionarioPedagogicoUnidadeCurricularAfetaDaoImpl().save(unidadeAfeta);
381
        }
382
 
383
        unidadeAfeta.resetStats();
384
        unidadeAfeta.setCursoAfeto(cursoAfeto);//so metemos de um dos lados
385
        unidadeAfeta.setCourseUnit(cu);
386
        unidadeAfeta.setCodigoUnidade(cu.getCode());
387
        unidadeAfeta.setObs("");
388
 
389
        unidadeAfeta.setMarked(false);
390
 
391
        ArrayList<CourseUnitImpl.Tipologia> mergedTipologias = copiarTipologias(cu, unidadeAfeta);
392
 
393
 
394
        checkTurmaAndProfs(cu, unidadeAfeta, mergedTipologias);
395
        checkProfs(cur, unidadeAfeta);
396
        checkTurmas(cur, unidadeAfeta);
397
        checkStudents(cur, unidadeAfeta);
398
 
399
        if(unidadeAfeta.isMarked())
400
            cursoAfeto.incMarcadas();
401
 
402
 
403
    }
404
 
405
    /**
406
     *
407
     * @param cur
408
     * @param unidadeAfeta
409
     */
410
    private void checkTurmas(CourseUnitDaoImpl.CourseUnitResult cur, QuestionarioPedagogicoUnidadeCurricularAfetaImpl unidadeAfeta) {
411
        if(cur.turmas == 0)
412
        {
413
            unidadeAfeta.setTurmas(0);
414
            unidadeAfeta.setMarked(true); //WARNING
415
            unidadeAfeta.addObs("Não tem turmas");
416
        }
417
        else
418
            unidadeAfeta.setTurmas(cur.turmas);
419
    }
420
 
421
    /**
422
     *
423
     * @param cur
424
     * @param unidadeAfeta
425
     */
426
    private void checkProfs(CourseUnitDaoImpl.CourseUnitResult cur, QuestionarioPedagogicoUnidadeCurricularAfetaImpl unidadeAfeta) {
427
        if(cur.teachers == 0)
428
        {
429
            unidadeAfeta.setProfs(0);
430
            unidadeAfeta.setMarked(true); //WARNING
431
            unidadeAfeta.addObs("Não tem professores");
432
        }
433
        else
434
            unidadeAfeta.setProfs(cur.teachers);
435
    }
436
 
437
    /**
438
     *
439
     * @param cur
440
     * @param unidadeAfeta
441
     */
442
    private void checkStudents(CourseUnitDaoImpl.CourseUnitResult cur, QuestionarioPedagogicoUnidadeCurricularAfetaImpl unidadeAfeta) {
443
        if(cur.students == 0)
444
        {
445
            unidadeAfeta.setStudents(0);
446
            unidadeAfeta.setMarked(true); //WARNING
447
            unidadeAfeta.addObs("Não tem estudantes");
448
        }
449
        else
450
            unidadeAfeta.setStudents(cur.students);
451
    }
452
 
453
    /**
454
     *
455
     * @param cu
456
     * @param unidadeAfeta
457
     * @return
458
     * @throws IOException
459
     * @throws TransformationException
460
     */
461
    private ArrayList<CourseUnitImpl.Tipologia> copiarTipologias(CourseUnit cu, QuestionarioPedagogicoUnidadeCurricularAfetaImpl unidadeAfeta) throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException {
462
        try
463
        {
464
            CourseUnitImpl.Tipologias tipologias = ((CourseUnitImpl)cu).getTipologiasClass();
465
            ArrayList<CourseUnitImpl.Tipologia> mergedTipologias = tipologias.obtainMergeTipologias();
466
            unidadeAfeta.setTipologias(mergedTipologias.size());
467
            unidadeAfeta.setTipologiasJson(CourseUnitImpl.Tipologias.serializeMergedList(mergedTipologias));
468
            unidadeAfeta.setAddedTipologias(tipologias.getTipologiasExtraAdicionadas().size());
469
            return mergedTipologias;
470
        }
471
        catch(1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
472
        {
473
            ArrayList<CourseUnitImpl.Tipologia> tipologias = new ArrayList<CourseUnitImpl.Tipologia>();
474
            unidadeAfeta.setMarked(true);
475
            unidadeAfeta.addObs("Erro a descodificar tipologias");
476
            unidadeAfeta.setTipologiasJson(CourseUnitImpl.Tipologias.serializeMergedList(tipologias));
477
            return tipologias;
478
        }
479
    }
480
 
481
    /**
482
     *
483
     * @param cu
484
     * @param unidadeAfeta
485
     * @param mergedTipologias
486
     */
487
    private void checkTurmaAndProfs(CourseUnit cu, QuestionarioPedagogicoUnidadeCurricularAfetaImpl unidadeAfeta, ArrayList<CourseUnitImpl.Tipologia> mergedTipologias) {
488
        if(cu.getTeachers() != null)
489
        {
490
            HashMap<String,Boolean> turmaWithProfs = new HashMap<String, Boolean>();
491
            HashMap<String,Boolean> profsWithTurma = new HashMap<String, Boolean>();
492
            for(CourseUnitImpl.Tipologia t: mergedTipologias)
493
            {
494
                turmaWithProfs.put(t.getCdTurma(),true);
495
                profsWithTurma.put(t.getCdDocente(),true);
496
            }
497
            for(Teacher t: cu.getTeachers())
498
            {
499
                if(profsWithTurma.get(""+t.getSigesCode())==null)
500
                {
501
                    unidadeAfeta.setProfsWithoutTurma(unidadeAfeta.getProfsWithoutTurma()+1);
502
                    unidadeAfeta.setMarked(true);
503
                }
504
                DaoFactory.getTeacherDaoImpl().evict(t);
505
            }
506
 
507
            for(CourseUnitTurma t: cu.getTurmas())
508
            {
509
                if(turmaWithProfs.get(t.getTurma())==null)
510
                {
511
                    unidadeAfeta.setTurmaWithoutProf(unidadeAfeta.getTurmaWithoutProf() + 1);
512
                    unidadeAfeta.setMarked(true);
513
                }
514
                DaoFactory.getCourseUnitTurmaDaoImpl().evict(t);
515
            }
516
        }
517
    }
1327 jmachado 518
}