Subversion Repositories bacoAlunos

Rev

Rev 1337 | Rev 1341 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
112 jmachado 1
package pt.estgp.estgweb.domain;
2
 
1312 jmachado 3
import com.owlike.genson.Genson;
4
import com.owlike.genson.TransformationException;
5
import org.apache.log4j.Logger;
995 jmachado 6
import pt.estgp.estgweb.Globals;
1313 jmachado 7
import pt.estgp.estgweb.domain.dao.DaoFactory;
489 jmachado 8
import pt.estgp.estgweb.domain.enums.CourseUnitContentsGrants;
1312 jmachado 9
import pt.estgp.estgweb.domain.test.TestCourseUnit;
10
import pt.estgp.estgweb.domain.utils.SimpleClearanceRuledSupport;
11
import pt.utl.ist.berserk.logic.serviceManager.IServiceManager;
12
import pt.utl.ist.berserk.logic.serviceManager.ServiceManager;
489 jmachado 13
 
1312 jmachado 14
import java.io.IOException;
146 jmachado 15
import java.io.Serializable;
1337 jmachado 16
import java.util.*;
146 jmachado 17
 
112 jmachado 18
/**
19
 * Created by IntelliJ IDEA.
20
 * User: Jorge Machado
21
 * Date: 7/Mai/2008
22
 * Time: 20:21:15
23
 * To change this template use File | Settings | File Templates.
24
 */
1312 jmachado 25
public class CourseUnitImpl extends CourseUnit implements ICanManage, ClearanceRuled
146 jmachado 26
{
489 jmachado 27
 
1312 jmachado 28
    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(CourseUnitImpl.class);
29
 
489 jmachado 30
    CourseUnitContentsGrants courseUnitContentsGrants = null;
31
 
146 jmachado 32
    public 1.5.0/docs/api/java/io/Serializable.html">Serializable getSerializable()
33
    {
34
        return getId();
35
    }
204 jmachado 36
 
489 jmachado 37
 
38
    public CourseUnitContentsGrants getCourseUnitContentsGrants() {
39
        if(courseUnitContentsGrants == null)
40
            courseUnitContentsGrants = CourseUnitContentsGrants.parse(super.getContentsGrants());
41
        return courseUnitContentsGrants;
42
    }
43
 
44
    public void setCourseUnitContentsGrants(CourseUnitContentsGrants courseUnitContentsGrants) {
45
        super.setContentsGrants(courseUnitContentsGrants.name());
46
        this.courseUnitContentsGrants = courseUnitContentsGrants;
47
    }
48
 
204 jmachado 49
    public boolean isValid()
50
    {
51
        return true;  //To change body of implemented methods use File | Settings | File Templates.
52
    }
53
 
54
    public boolean isOwnedBy(GenericUser u)
55
    {
208 jmachado 56
        return isOwnedBy(u, false);
204 jmachado 57
    }
58
 
208 jmachado 59
    public boolean isOwnedBy(GenericUser u, boolean transaction)
60
    {
61
        return u != null && u instanceof TeacherImpl && ((TeacherImpl)u).hasCourseUnit(getId(),transaction);
62
    }
63
 
204 jmachado 64
    /**
65
     * Not implemented can be variuous teachers
66
     * @return null
67
     * use isOwnedBy in this class
68
     */
69
    public GenericUser getOwner()
70
    {
71
        return null;
72
    }
73
    /**
74
     * Not implemented
75
     * @return
76
     */
77
    public List<String> getOwnerRoles()
78
    {
79
        return null;
80
    }
81
 
82
    /**
83
     * Not implemented
84
     * @return
85
     */
86
    public 1.5.0/docs/api/java/lang/String.html">String getChoosedOwnerRole()
87
    {
88
        return null;
89
    }
90
 
91
    public boolean isInternal()
92
    {
251 jmachado 93
        return true;
204 jmachado 94
    }
208 jmachado 95
 
96
    public CourseUnitBlogImpl getUnitBlog()
97
    {
98
        if(getBlogs()!=null && getBlogs().size() > 0)
99
            return (CourseUnitBlogImpl) getBlogs().iterator().next();
100
        return null;
101
    }
251 jmachado 102
 
1315 jmachado 103
 
1312 jmachado 104
    public boolean equalsSiges(5+0%2Fdocs%2Fapi+Object">Object anotherInstance,boolean turmasSeparadas)
251 jmachado 105
    {
106
        if(anotherInstance instanceof CourseUnitImpl)
107
        {
108
            CourseUnitImpl anCourseUnit = (CourseUnitImpl) anotherInstance;
1312 jmachado 109
            if(turmasSeparadas)
110
            {
111
                boolean turmaVazia = getCdTurma() == null || getCdTurma().trim().length() == 0;
112
                boolean anotherTurmaVazia = anCourseUnit.getCdTurma() == null || anCourseUnit.getCdTurma().trim().length() == 0;
113
                boolean turmasIguais = (turmaVazia && anotherTurmaVazia) || (!turmaVazia && !anotherTurmaVazia && getCdTurma().equals(anCourseUnit.getCdTurma()));
114
                return equalsIgnoringTurma(anotherInstance)
115
                &&
116
                turmasIguais;
117
            }
118
            else
119
                return equalsIgnoringTurma(anotherInstance);
251 jmachado 120
        }
121
        return false;
122
 
123
    }
489 jmachado 124
 
1204 jmachado 125
    public boolean equalsIgnoringTurma(5+0%2Fdocs%2Fapi+Object">Object anotherInstance)
126
    {
127
        if(anotherInstance instanceof CourseUnitImpl)
128
        {
129
            CourseUnitImpl anCourseUnit = (CourseUnitImpl) anotherInstance;
130
 
131
            return anCourseUnit.getCode() != null && getCode() != null && getCode().equals(anCourseUnit.getCode()) &&
132
                    anCourseUnit.getCourseCode() != null && getCourseCode() != null && getCourseCode().equals(anCourseUnit.getCourseCode()) &&
133
                    anCourseUnit.getSemestre() != null && getSemestre() != null && getSemestre().equals(anCourseUnit.getSemestre()) &&
134
                    anCourseUnit.getImportYear() != null && getImportYear() != null && getImportYear().equals(anCourseUnit.getImportYear());
135
        }
136
        return false;
137
 
138
    }
139
 
973 jmachado 140
    /**
141
     * Means that is teacher os responsable teacher or from course commission
142
     * Only works in open transaction
143
     *
144
     * Needs Course and Responsable Teacher loaded
145
     */
146
    public boolean isFromCourseUnitCommission(UserSession userSession)
147
    {
148
        return userSession.getUser()!= null &&
149
                (((UserImpl) userSession.getUser()).isTeacherOfCourseUnit(getId(), true)
150
                        || (getResponsableTeacher() != null && getResponsableTeacher().getId() == userSession.getUser().getId())
151
                        || userSession.getUser().hasRole(getCourse().getValidationRole())
152
                );
153
    }
489 jmachado 154
 
1312 jmachado 155
    public boolean isFromCourseCommission(UserSession userSession)
156
    {
157
        return userSession.getUser()!= null &&
158
                userSession.getUser().hasRole(getCourse().getValidationRole());
159
    }
973 jmachado 160
 
1312 jmachado 161
 
995 jmachado 162
    public 1.5.0/docs/api/java/lang/String.html">String getSummariosPathIntranet()
163
    {
164
        return Globals.FTP_IONLINE_START_PATH + "/" + getPathIntranet() + "/" + Globals.INTRANET_DTP + "/" + Globals.INTRANET_DTP_Sumarios;
165
    }
973 jmachado 166
 
1148 jmachado 167
    public 1.5.0/docs/api/java/lang/String.html">String getInqueritosPedagogicosPathIntranet()
168
    {
169
        return Globals.FTP_IONLINE_START_PATH + getPathIntranet() + "/" + Globals.INTRANET_DTP + "/" + Globals.INTRANET_DTP_InqueritoPedagogico;
170
    }
171
 
172
 
995 jmachado 173
    public 1.5.0/docs/api/java/lang/String.html">String getSummariosPathFileIntranet()
174
    {
175
        return getSummariosPathIntranet() + "/" + getSummariosFileNameIntranet();
176
    }
177
 
178
    public 1.5.0/docs/api/java/lang/String.html">String getSummariosFileNameIntranet(){
179
        return getCode() + "_" + getNormalizedName() + "_sumarios.pdf";
180
    }
181
 
182
 
996 jmachado 183
    public 1.5.0/docs/api/java/lang/String.html">String getSigesUniqueIdentifiers()
184
    {
185
        return "SIGES:" + getCode() + ", COURSE: " + getCourseCode() +
1139 jmachado 186
                ", " + getSemestre() + " of " + getImportYear() + " - " + getName() + " Turma - " + getCdTurma();
996 jmachado 187
    }
188
 
1312 jmachado 189
 
190
    public boolean hasStudent(long studentId, boolean transaction)
191
    {
192
        Set<StudentImpl> students;
193
        if(transaction)
194
            students = getStudents();
195
        else
196
        {
197
            IServiceManager sm;
198
            try {
199
                sm = ServiceManager.getInstance();
200
                1.5.0/docs/api/java/lang/String.html">String[] names = new 1.5.0/docs/api/java/lang/String.html">String[]{"serializable"};
201
                5+0%2Fdocs%2Fapi+Object">Object[] args = new 5+0%2Fdocs%2Fapi+Object">Object[]{getId()};
202
                students = (Set<StudentImpl>) sm.execute(null, "LoadCourseUnitStudents", args, names);
203
            } catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e) {
204
                logger.error(e,e);
205
                return false;
206
            }
207
        }
208
        if(students == null)
209
            return false;
210
        for(Student s: students)
211
        {
212
            if(s.getId() == studentId)
213
                return true;
214
        }
215
        return false;
216
    }
217
 
218
    public boolean hasTeacher(long teacherId, boolean transaction)
219
    {
220
        Set<TeacherImpl> teachers;
221
        if(transaction)
222
            teachers = getTeachers();
223
        else
224
        {
225
            IServiceManager sm;
226
            try {
227
                sm = ServiceManager.getInstance();
228
                1.5.0/docs/api/java/lang/String.html">String[] names = new 1.5.0/docs/api/java/lang/String.html">String[]{"serializable"};
229
                5+0%2Fdocs%2Fapi+Object">Object[] args = new 5+0%2Fdocs%2Fapi+Object">Object[]{getId()};
230
                teachers = (Set<TeacherImpl>) sm.execute(null, "LoadCourseUnitTeachers", args, names);
231
            } catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e) {
232
                logger.error(e,e);
233
                return false;
234
            }
235
        }
236
        if(teachers == null)
237
            return false;
238
        for(Teacher t: teachers)
239
        {
240
            if(t.getId() == teacherId)
241
                return true;
242
        }
243
        return false;
244
    }
245
 
246
    public boolean isResponsibleTeacher(long teacherId, boolean transaction)
247
    {
248
        Teacher t;
249
        if(transaction)
250
            t = getResponsableTeacher();
251
        else
252
        {
253
            IServiceManager sm;
254
            try {
255
                sm = ServiceManager.getInstance();
256
                1.5.0/docs/api/java/lang/String.html">String[] names = new 1.5.0/docs/api/java/lang/String.html">String[]{"serializable"};
257
                5+0%2Fdocs%2Fapi+Object">Object[] args = new 5+0%2Fdocs%2Fapi+Object">Object[]{getId()};
258
                t = (Teacher) sm.execute(null, "LoadCourseUnitResponsibleTeacher", args, names);
259
            } catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e) {
260
                logger.error(e,e);
261
                return false;
262
            }
263
        }
264
        if(t == null)
265
            return false;
266
        if(t.getId() == teacherId)
267
            return true;
268
        return false;
269
    }
270
 
271
    SimpleClearanceRuledSupport clearanceRuledSupport = null;
272
 
273
    public SimpleClearanceRuledSupport getClearanceRuledSupport() {
274
        return clearanceRuledSupport;
275
    }
276
 
277
    public void setClearanceRuledSupport(SimpleClearanceRuledSupport clearanceRuledSupport) {
278
        this.clearanceRuledSupport = clearanceRuledSupport;
279
    }
280
 
281
    @1.5.0/docs/api/java/lang/Override.html">Override
282
    public boolean isClearanceInitialized(UserSessionImpl u) {
283
        return clearanceRuledSupport == null ? false :
284
         clearanceRuledSupport.isClearanceInitialized(u);
285
    }
286
 
287
    @1.5.0/docs/api/java/lang/Override.html">Override
288
    public boolean isClear(UserSessionImpl u, 1.5.0/docs/api/java/lang/String.html">String OP) {
289
 
290
        if(clearanceRuledSupport == null) return false;
291
        if(OP.startsWith("EVALUATION") && !OP.startsWith("EVALUATION_INFO")
292
                && clearanceRuledSupport.isClear(u,CourseUnitClearancesOperation.EVALUATION_ALL.name()))
293
            return true;
294
        return clearanceRuledSupport.isClear(u, OP);
295
    }
296
 
1326 jmachado 297
    @1.5.0/docs/api/java/lang/Override.html">Override
298
    public void initClearances(UserSessionImpl usession,boolean force)
299
    {
300
        CourseUnitImpl c = this;
301
        1.5.0/docs/api/java/lang/String.html">String activeYear = DaoFactory.getConfigurationDaoImpl().getCourseUnitEvaluationActiveYear();
302
        List<String> activeDegrees = DaoFactory.getConfigurationDaoImpl().getCourseUnitEvaluationActiveDegrees();
303
        List<Long> activeCourseIds = DaoFactory.getConfigurationDaoImpl().getCourseUnitEvaluationActiveCourseIds();
304
 
305
        boolean isActiveYear = activeYear != null && c.getImportYear().equals(activeYear);
306
        boolean isActiveDegree = activeDegrees!=null && activeDegrees.contains(c.getCourse().getDegree());
307
        boolean isActiveCourseId = activeCourseIds!=null && activeCourseIds.contains(c.getCourse().getId());
308
 
309
        boolean activePeriod = (isActiveYear && (isActiveCourseId || isActiveDegree));
310
        boolean active = activePeriod || c.isEvaluationOpenByAdmin();
311
 
312
        boolean isComplete = c.getCourseUnitEvaluation() != null && c.getCourseUnitEvaluation().isTeacherComplete() && !c.getCourseUnitEvaluation().isClosed();
313
        boolean isClosed = c.getCourseUnitEvaluation() != null && c.getCourseUnitEvaluation().isClosed();
314
        boolean isEdit = !isClosed && !isComplete;
315
 
316
 
317
        SimpleClearanceRuledSupport s = new SimpleClearanceRuledSupport();
318
        s.setDirty();
319
 
320
        if(isEdit)
321
            s.addClearance(CourseUnitClearancesOperation.EVALUATION_INFO_STATE_EDIT.name(),usession);
322
        else if(isComplete)
323
            s.addClearance(CourseUnitClearancesOperation.EVALUATION_INFO_STATE_COMPLETE.name(),usession);
324
        else if(isClosed)
325
            s.addClearance(CourseUnitClearancesOperation.EVALUATION_INFO_STATE_CLOSED.name(),usession);
326
 
327
        if(activePeriod)
328
        {
329
            s.addClearance(CourseUnitClearancesOperation.EVALUATION_INFO_ACTIVE.name(),usession);
330
            s.addClearance(CourseUnitClearancesOperation.EVALUATION_INFO_ACTIVE_REGULAR_PERIOD.name(),usession);
331
        }
332
        if(c.isEvaluationOpenByAdmin())
333
        {
334
            s.addClearance(CourseUnitClearancesOperation.EVALUATION_INFO_ACTIVE.name(),usession);
335
            s.addClearance(CourseUnitClearancesOperation.EVALUATION_INFO_ACTIVE_SPECIAL_ADMIN.name(),usession);
336
        }
337
 
338
 
339
        if(usession.getUser() != null && usession.getUser().isSuperuserOrAdmin()
340
                || usession.getUser().hasRole(Globals.SERVICES_PROGRAMS_ROLE))
341
        {
342
            s.addClearance(CourseUnitClearancesOperation.EVALUATION_ALL.name(),usession);
343
        }
344
 
345
        if(usession.getUser() != null && usession.getUser() instanceof Teacher)
346
        {
347
            boolean teacher = ((Teacher)(usession.getUser())).isTeacherOfCourseUnit(c.getId(),true);
348
            boolean responsable = c.getResponsableTeacher()!=null && usession.getUser().getId() == c.getResponsableTeacher().getId();
349
            if(teacher)
350
                s.addClearance(CourseUnitClearancesOperation.EVALUATION_INFO_USER_TEACHER.name(), usession);
351
            if(responsable)
352
                s.addClearance(CourseUnitClearancesOperation.EVALUATION_INFO_USER_RESP_TEACHER.name(), usession);
353
            if(teacher || responsable)
354
            {
355
                //s.addClearance(CourseUnitClearancesOperation.EVALUATION_INFO_USER_TEACHER.name(), usession);
356
                s.addClearance(CourseUnitClearancesOperation.EVALUATION_EDITION.name(),usession);
357
                if(active && isEdit)
358
                {
359
                    s.addClearance(CourseUnitClearancesOperation.EVALUATION_EDITSAVE.name(),usession);
360
                    s.addClearance(CourseUnitClearancesOperation.EVALUATION_PERSIST_TEACHER_PART.name(),usession);
361
                    s.addClearance(CourseUnitClearancesOperation.EVALUATION_TRANSITATE_TO_COMPLETE.name(),usession);
362
                }
363
            }
364
        }
365
 
366
        if(usession.getUser() != null && usession.getUser() instanceof Teacher
367
                && (((usession.getUser())).hasRole(c.getCourse().getValidationRole())
368
                ||
369
                ((usession.getUser()) == c.getCourse().getCoordinator()))
370
                )
371
        {
372
            s.addClearance(CourseUnitClearancesOperation.EVALUATION_INFO_USER_COMMISSION.name(), usession);
373
            s.addClearance(CourseUnitClearancesOperation.EVALUATION_EDITION.name(), usession);
374
            if(active && isEdit)
375
            {
376
                s.addClearance(CourseUnitClearancesOperation.EVALUATION_EDITSAVE.name(),usession);
377
                s.addClearance(CourseUnitClearancesOperation.EVALUATION_PERSIST_TEACHER_PART.name(),usession);
378
                s.addClearance(CourseUnitClearancesOperation.EVALUATION_PERSIST_COMISSION_PART.name(),usession);
379
                s.addClearance(CourseUnitClearancesOperation.EVALUATION_TRANSITATE_TO_COMPLETE.name(),usession);
380
                s.addClearance(CourseUnitClearancesOperation.EVALUATION_TRANSITATE_TO_CLOSE.name(),usession);
381
            }
382
            else if(active && isComplete)
383
            {
384
                s.addClearance(CourseUnitClearancesOperation.EVALUATION_EDITSAVE.name(),usession);
385
                s.addClearance(CourseUnitClearancesOperation.EVALUATION_PERSIST_TEACHER_PART.name(),usession);
386
                s.addClearance(CourseUnitClearancesOperation.EVALUATION_PERSIST_COMISSION_PART.name(),usession);
387
                s.addClearance(CourseUnitClearancesOperation.EVALUATION_TRANSITATE_TO_CLOSE.name(),usession);
388
                s.addClearance(CourseUnitClearancesOperation.EVALUATION_TRANSITATE_TO_EDIT.name(),usession);
389
            }
390
            else if(active && isClosed)
391
            {
392
                s.addClearance(CourseUnitClearancesOperation.EVALUATION_TRANSITATE_TO_EDIT.name(),usession);
393
            }
394
        }
395
        c.setClearanceRuledSupport(s);
396
    }
397
 
1312 jmachado 398
    public static enum CourseUnitClearancesOperation
399
    {
400
        EVALUATION_ALL, //SPECIAL OPERATION FOR DENY AND PERMIT REPLACE FULL CONTROL
401
 
402
        //COURSE UNIT EVALUATION
403
        EVALUATION_PERSIST_TEACHER_PART, //SERVICE PERSIST THIS PART
404
        EVALUATION_PERSIST_COMISSION_PART, //SERVICE PERSIST THIS PART
405
 
406
        EVALUATION_EDITION, //CAN SEE EDITION SCREEN
407
        EVALUATION_EDITSAVE, //CAN PRESS SAVE
408
        EVALUATION_TRANSITATE_TO_COMPLETE, //CAN_TRANSITATE
409
        EVALUATION_TRANSITATE_TO_CLOSE, //CAN CLOSE
410
        EVALUATION_TRANSITATE_TO_EDIT, //CAN REVERT
411
 
412
        EVALUATION_INFO_ACTIVE, //INFO CLEARANCE WHEN ACTIVE
413
        EVALUATION_INFO_ACTIVE_SPECIAL_ADMIN, //INFO CLEARANCE REASON OF BEING ACTIVE
414
        EVALUATION_INFO_ACTIVE_REGULAR_PERIOD, //INFO CLEARANCE REASON OF BEING ACTIVE
415
 
416
        EVALUATION_INFO_STATE_EDIT, //INFO CLEARANCE
417
        EVALUATION_INFO_STATE_COMPLETE, //INFO CLEARANCE
418
        EVALUATION_INFO_STATE_CLOSED, //INFO CLEARANCE
419
 
420
        EVALUATION_INFO_USER_TEACHER, //INFO CLEARANCE
421
        EVALUATION_INFO_USER_RESP_TEACHER, //INFO CLEARANCE
422
        EVALUATION_INFO_USER_COMMISSION, //INFO CLEARANCE
423
 
424
        ;
425
 
426
        public static CourseUnitClearancesOperation parse(1.5.0/docs/api/java/lang/String.html">String clearance)
427
        {
428
            if(clearance == null)
429
                return null;
430
            for(CourseUnitClearancesOperation op: CourseUnitClearancesOperation.values())
431
            {
432
                if(op.name().equals(clearance))
433
                    return op;
434
            }
435
            return null;
436
        }
437
    }
438
 
439
 
440
    public Tipologias getTipologiasClass() throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException
441
    {
442
        return Tipologias.deserialize(getTipologias());
443
    }
444
 
445
    public void setTipologiasClass(Tipologias tipologias) throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException
446
    {
447
        if(tipologias != null)
448
            setTipologias(tipologias.serialize());
449
    }
450
 
1313 jmachado 451
    public TipologiaPersistent loadPersistent(Tipologia myTipo)
452
    {
453
        TipologiaPersistent tipologiaPersistent = new TipologiaPersistent();
454
        tipologiaPersistent.setHorRef(myTipo.isHorRef());
455
        tipologiaPersistent.setExtra(myTipo.isExtra());
456
        tipologiaPersistent.setTeacher(DaoFactory.getTeacherDaoImpl().loadBySigesCode(1.5.0/docs/api/java/lang/Integer.html">Integer.parseInt(myTipo.getCdDocente())));
457
        tipologiaPersistent.setCdTipologia(myTipo.getCdTipologia());
458
        tipologiaPersistent.setOcorrencias(myTipo.getOcorrencias());
1326 jmachado 459
        tipologiaPersistent.setMarkWarning(myTipo.isMarkWarning());
460
        tipologiaPersistent.setUse(myTipo.isUse());
1313 jmachado 461
 
462
        tipologiaPersistent.setCourseUnitTurma(findTurma(myTipo.getCdTurma()));
463
        if(tipologiaPersistent.getCourseUnitTurma() == null || tipologiaPersistent.getTeacher() == null)
464
        {
1338 jmachado 465
            logger.error("Turma ou professor nao encontrados enquanto fazia uma tipologia persistente: turma:" + myTipo.getCdTurma() + " teacher code: " + myTipo.getCdDocente());
1313 jmachado 466
            return null;
467
        }
468
        return tipologiaPersistent;
469
    }
470
 
471
    public CourseUnitTurma findTurma(1.5.0/docs/api/java/lang/String.html">String turma)
472
    {
473
        if(getTurmas() == null)
474
            return null;
475
        for(CourseUnitTurma t: getTurmas())
476
        {
477
            if(t.getTurma().equals(turma))
478
                return t;
479
        }
480
        return null;
481
    }
482
 
483
 
484
    public static class TipologiaPersistent
485
    {
486
        private Teacher teacher;
487
        private CourseUnitTurma courseUnitTurma;
488
        private 1.5.0/docs/api/java/lang/String.html">String cdTipologia;
489
        private 1.5.0/docs/api/java/lang/Integer.html">Integer ocorrencias = 0;
490
        protected boolean extra;
491
        protected boolean horRef;
1326 jmachado 492
        protected boolean use;   //campo auxiliar para marcar se vai ser ou não usada  nas afetacoes especificas
493
        //por omissão apenas as que têm sumários
494
        protected boolean markWarning; //camp
1313 jmachado 495
 
1326 jmachado 496
        public boolean isUse() {
497
            return use;
498
        }
499
 
500
        public void setUse(boolean use) {
501
            this.use = use;
502
        }
503
 
504
        public boolean isMarkWarning() {
505
            return markWarning;
506
        }
507
 
508
        public void setMarkWarning(boolean markWarning) {
509
            this.markWarning = markWarning;
510
        }
511
 
1313 jmachado 512
        public boolean isExtra() {
513
            return extra;
514
        }
515
 
516
        public boolean isHorRef() {
517
            return horRef;
518
        }
519
 
520
        public boolean isSumario() {
521
            return ocorrencias != null && ocorrencias > 0;
522
        }
523
 
524
        public void setExtra(boolean extra) {
525
            this.extra = extra;
526
        }
527
 
528
        public void setHorRef(boolean horRef) {
529
            this.horRef = horRef;
530
        }
531
 
532
        public Teacher getTeacher() {
533
            return teacher;
534
        }
535
 
536
        public void setTeacher(Teacher teacher) {
537
            this.teacher = teacher;
538
        }
539
 
540
        public CourseUnitTurma getCourseUnitTurma() {
541
            return courseUnitTurma;
542
        }
543
 
544
        public void setCourseUnitTurma(CourseUnitTurma courseUnitTurma) {
545
            this.courseUnitTurma = courseUnitTurma;
546
        }
547
 
548
        public 1.5.0/docs/api/java/lang/String.html">String getCdTipologia() {
549
            return cdTipologia;
550
        }
551
 
552
        public void setCdTipologia(1.5.0/docs/api/java/lang/String.html">String cdTipologia) {
553
            this.cdTipologia = cdTipologia;
554
        }
555
 
556
        public 1.5.0/docs/api/java/lang/Integer.html">Integer getOcorrencias() {
1338 jmachado 557
            return ocorrencias == null ? 0:ocorrencias;
1313 jmachado 558
        }
559
 
560
        public void setOcorrencias(1.5.0/docs/api/java/lang/Integer.html">Integer ocorrencias) {
561
            this.ocorrencias = ocorrencias;
562
        }
563
    }
1312 jmachado 564
    /**
565
     * Os codigos de tipologia devem ser limitados a T TP OT etc
566
     */
567
    public static class Tipologia
568
    {
1313 jmachado 569
        protected 1.5.0/docs/api/java/lang/String.html">String cdDocente;
570
        protected 1.5.0/docs/api/java/lang/String.html">String cdTurma;
571
        protected 1.5.0/docs/api/java/lang/String.html">String cdTipologia;
572
        protected 1.5.0/docs/api/java/lang/String.html">String dsTipologia;
1338 jmachado 573
        protected 1.5.0/docs/api/java/lang/Integer.html">Integer ocorrencias = 0; // apenas faz sentido nos sumarios. no horario ref nao.
1312 jmachado 574
 
1325 jmachado 575
        protected boolean use;   //campo auxiliar para marcar se vai ser ou não usada  nas afetacoes especificas
576
                                //por omissão apenas as que têm sumários
577
        protected boolean markWarning; //campo auxiliar para marcar se vai ser ou não usada  nas afetacoes especificas
578
 
1313 jmachado 579
        protected boolean extra;
580
        protected boolean horRef;
581
 
1325 jmachado 582
        public boolean isUse() {
583
            return use;
584
        }
585
 
586
        public void setUse(boolean use) {
587
            this.use = use;
588
        }
589
 
590
        public boolean isMarkWarning() {
591
            return markWarning;
592
        }
593
 
594
        public void setMarkWarning(boolean markWarning) {
595
            this.markWarning = markWarning;
596
        }
597
 
1312 jmachado 598
        public 1.5.0/docs/api/java/lang/Integer.html">Integer getOcorrencias() {
1338 jmachado 599
            return ocorrencias == null ? 0:ocorrencias;
1312 jmachado 600
        }
601
 
1338 jmachado 602
        public boolean hasOcorrencias() {
603
            return ocorrencias != null && ocorrencias > 0;
604
        }
605
 
1312 jmachado 606
        public void setOcorrencias(1.5.0/docs/api/java/lang/Integer.html">Integer ocorrencias) {
607
            this.ocorrencias = ocorrencias;
608
        }
609
 
610
        public 1.5.0/docs/api/java/lang/String.html">String getCdDocente() {
611
            return cdDocente;
612
        }
613
 
614
        public void setCdDocente(1.5.0/docs/api/java/lang/String.html">String cdDocente) {
615
            this.cdDocente = cdDocente;
616
        }
617
 
618
        public 1.5.0/docs/api/java/lang/String.html">String getCdTurma() {
619
            return cdTurma;
620
        }
621
 
622
        public void setCdTurma(1.5.0/docs/api/java/lang/String.html">String cdTurma) {
623
            this.cdTurma = cdTurma;
624
        }
625
 
626
        public 1.5.0/docs/api/java/lang/String.html">String getCdTipologia() {
627
            return cdTipologia;
628
        }
629
 
630
        public void setCdTipologia(1.5.0/docs/api/java/lang/String.html">String cdTipologia) {
631
            this.cdTipologia = cdTipologia;
632
        }
633
 
1313 jmachado 634
        public 1.5.0/docs/api/java/lang/String.html">String getDsTipologia() {
635
            return dsTipologia;
636
        }
637
 
638
        public void setDsTipologia(1.5.0/docs/api/java/lang/String.html">String dsTipologia) {
639
            this.dsTipologia = dsTipologia;
640
        }
641
 
642
        public boolean isExtra() {
643
            return extra;
644
        }
645
 
646
        public boolean isSumario() {
647
            return ocorrencias != null && ocorrencias > 0;
648
        }
649
 
650
        public boolean isHorRef() {
651
            return horRef;
652
        }
653
 
654
 
655
 
1312 jmachado 656
        @1.5.0/docs/api/java/lang/Override.html">Override
657
        public boolean equals(5+0%2Fdocs%2Fapi+Object">Object o)
658
        {
659
            return o instanceof Tipologia &&
660
                    ((((Tipologia)o).getCdTurma() == null && getCdTurma() == null)
661
                            ||
662
                     (((Tipologia)o).getCdTurma().equals(getCdTurma())))
663
                    &&
664
 
665
                    (
666
                     (((Tipologia)o).getCdDocente() == null && getCdDocente() == null)
667
                            ||
668
                     (((Tipologia)o).getCdDocente().equals(getCdDocente())))
669
                    &&
670
 
671
                    (
672
                     (((Tipologia)o).getCdTipologia() == null && getCdTipologia() == null)
673
                                    ||
674
                     (((Tipologia)o).getCdTipologia().equals(getCdTipologia())));
675
        }
1313 jmachado 676
 
677
        public boolean equalsIncDsFields(5+0%2Fdocs%2Fapi+Object">Object o)
678
        {
679
            return o instanceof Tipologia &&
680
                    equals(o)
681
                    &&
682
                    (
683
                            (((Tipologia)o).getDsTipologia() == null && getDsTipologia() == null)
684
                                    ||
685
                                    (((Tipologia)o).getDsTipologia().equals(getDsTipologia())))
686
                    &&
687
 
688
                    (
689
                            (((Tipologia)o).getOcorrencias() == null && getOcorrencias() == null)
690
                                    ||
691
                                    (((Tipologia)o).getOcorrencias().equals(getOcorrencias())))
692
                    ;
693
        }
1312 jmachado 694
    }
695
 
1326 jmachado 696
    public static class MergedTipologias
697
    {
698
        ArrayList<Tipologia> tipologiasWithMarkedAndUsed = new ArrayList<Tipologia>();
699
 
700
        public ArrayList<Tipologia> getTipologiasWithMarkedAndUsed() {
701
            return tipologiasWithMarkedAndUsed;
702
        }
703
 
704
        public void setTipologiasWithMarkedAndUsed(ArrayList<Tipologia> tipologiasWithMarkedAndUsed) {
705
            this.tipologiasWithMarkedAndUsed = tipologiasWithMarkedAndUsed;
706
        }
707
    }
1312 jmachado 708
    public static class Tipologias
709
    {
1313 jmachado 710
        ArrayList<Tipologia> tipologiasHorarioReferencia = new ArrayList<Tipologia>();
711
        ArrayList<Tipologia> tipologiasSumarios = new ArrayList<Tipologia>();
712
        ArrayList<Tipologia> tipologiasExtraAdicionadas = new ArrayList<Tipologia>();
1326 jmachado 713
        ArrayList<Tipologia> tipologiasUsadas = new ArrayList<Tipologia>();
714
        ArrayList<Tipologia> tipologiasMarcadas = new ArrayList<Tipologia>();
1312 jmachado 715
 
716
        public ArrayList<Tipologia> getTipologiasHorarioReferencia() {
1333 jmachado 717
 
718
            return tipologiasHorarioReferencia == null ? new ArrayList<Tipologia>() : tipologiasHorarioReferencia;
719
 
1312 jmachado 720
        }
721
 
722
        public void setTipologiasHorarioReferencia(ArrayList<Tipologia> tipologiasHorarioReferencia) {
723
            this.tipologiasHorarioReferencia = tipologiasHorarioReferencia;
724
        }
725
 
726
        public ArrayList<Tipologia> getTipologiasSumarios() {
1333 jmachado 727
            return tipologiasSumarios == null ? new ArrayList<Tipologia>() : tipologiasSumarios;
1312 jmachado 728
        }
729
 
730
        public void setTipologiasSumarios(ArrayList<Tipologia> tipologiasSumarios) {
731
            this.tipologiasSumarios = tipologiasSumarios;
732
        }
733
 
734
        public ArrayList<Tipologia> getTipologiasExtraAdicionadas() {
1333 jmachado 735
            return tipologiasExtraAdicionadas == null ? new ArrayList<Tipologia>() : tipologiasExtraAdicionadas;
1312 jmachado 736
        }
737
 
738
        public void setTipologiasExtraAdicionadas(ArrayList<Tipologia> tipologiasExtraAdicionadas) {
739
            this.tipologiasExtraAdicionadas = tipologiasExtraAdicionadas;
740
        }
741
 
1333 jmachado 742
        public ArrayList<Tipologia> getTipologiasUsadas() {
743
            return tipologiasUsadas == null ? new ArrayList<Tipologia>() : tipologiasUsadas;
744
        }
1326 jmachado 745
 
746
        public void setTipologiasUsadas(ArrayList<Tipologia> tipologiasUsadas) { this.tipologiasUsadas = tipologiasUsadas; }
747
 
1333 jmachado 748
        public ArrayList<Tipologia> getTipologiasMarcadas() {
749
            return tipologiasMarcadas == null ? new ArrayList<Tipologia>() : tipologiasMarcadas;
750
        }
1326 jmachado 751
 
752
        public void setTipologiasMarcadas(ArrayList<Tipologia> tipologiasMarcadas) { this.tipologiasMarcadas = tipologiasMarcadas; }
753
 
1312 jmachado 754
        private static Genson getGenson(){
755
            Genson genson = new Genson.Builder()
756
                    .exclude(5+0%2Fdocs%2Fapi+Object">Object.class)
757
                    .include("tipologiasExtraAdicionadas", Tipologias.class)
758
                    .include("tipologiasSumarios", Tipologias.class)
759
                    .include("tipologiasHorarioReferencia", Tipologias.class)
1326 jmachado 760
                    .include("tipologiasUsadas", Tipologias.class)
761
                    .include("tipologiasMarcadas", Tipologias.class)
1312 jmachado 762
                    .include("cdTipologia", Tipologia.class)
763
                    .include("cdDocente", Tipologia.class)
764
                    .include("cdTurma", Tipologia.class)
1313 jmachado 765
                    .include("dsTipologia",Tipologia.class)
1312 jmachado 766
                    .include("ocorrencias", Tipologia.class)
767
                    .create();
768
            return genson;
769
        }
770
 
1326 jmachado 771
        private static Genson getGensonMergedTipologiasComplete(){
772
            Genson genson = new Genson.Builder()
773
                    .exclude(5+0%2Fdocs%2Fapi+Object">Object.class)
774
 
775
                    .include("tipologiasWithMarkedAndUsed", MergedTipologias.class)
776
                    .include("cdTipologia", Tipologia.class)
777
                    .include("cdDocente", Tipologia.class)
778
                    .include("cdTurma", Tipologia.class)
779
                    .include("dsTipologia",Tipologia.class)
780
                    .include("ocorrencias", Tipologia.class)
781
                    .include("horRef", Tipologia.class)
782
                    .include("extra", Tipologia.class)
783
                    .include("use", Tipologia.class)
784
                    .include("markWarning", Tipologia.class)
785
                    .create();
786
            return genson;
787
        }
788
 
1312 jmachado 789
        public 1.5.0/docs/api/java/lang/String.html">String serialize() throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException
790
        {
791
            Genson genson = getGenson();
792
            try {
793
                return genson.serialize(this);
794
            } catch (TransformationException e) {
795
                throw e;
796
            } catch (1.5.0/docs/api/java/io/IOException.html">IOException e) {
797
                throw e;
798
            }
799
        }
800
        public static Tipologias deserialize(1.5.0/docs/api/java/lang/String.html">String serial) throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException
801
        {
802
            if(serial == null)
1333 jmachado 803
                return new Tipologias();
1312 jmachado 804
            try {
805
                return getGenson().deserialize(serial, Tipologias.class);
806
            } catch (TransformationException e) {
807
                throw e;
808
            } catch (1.5.0/docs/api/java/io/IOException.html">IOException e) {
809
                throw e;
810
            }
811
        }
812
 
813
        public static void main(1.5.0/docs/api/java/lang/String.html">String [] args)
814
        {
815
            new TestCourseUnit().testTipologias();
816
        }
1313 jmachado 817
 
1326 jmachado 818
        public static 1.5.0/docs/api/java/lang/String.html">String serializeMergedList(ArrayList<Tipologia> tipologias) throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException
1313 jmachado 819
        {
1326 jmachado 820
            MergedTipologias mergedTipologias = new MergedTipologias();
1333 jmachado 821
            if(tipologias != null)
822
            {
823
                mergedTipologias.setTipologiasWithMarkedAndUsed(tipologias);
824
            }
1326 jmachado 825
            Genson genson = getGensonMergedTipologiasComplete();
826
            try {
827
                return genson.serialize(mergedTipologias);
828
            } catch (TransformationException e) {
829
                throw e;
830
            } catch (1.5.0/docs/api/java/io/IOException.html">IOException e) {
831
                throw e;
832
            }
833
        }
834
        public static ArrayList<Tipologia> deserializeMergedTipologias(1.5.0/docs/api/java/lang/String.html">String serial) throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException
835
        {
836
            if(serial == null)
1333 jmachado 837
                return new ArrayList<Tipologia>();
1326 jmachado 838
            try {
839
                MergedTipologias mergedTipologias = getGensonMergedTipologiasComplete().deserialize(serial, MergedTipologias.class);
840
                if(mergedTipologias.getTipologiasWithMarkedAndUsed() == null)
841
                    return new ArrayList<Tipologia>();
842
                return mergedTipologias.getTipologiasWithMarkedAndUsed();
843
            } catch (TransformationException e) {
844
                throw e;
845
            } catch (1.5.0/docs/api/java/io/IOException.html">IOException e) {
846
                throw e;
847
            }
848
        }
1332 jmachado 849
        public 1.5.0/docs/api/java/lang/String.html">String obtainMergeTipologiasSerialized() throws 1.5.0/docs/api/java/io/IOException.html">IOException, TransformationException
850
        {
851
            return serializeMergedList(obtainMergeTipologias());
852
        }
1337 jmachado 853
 
1326 jmachado 854
        public ArrayList<Tipologia> obtainMergeTipologias()
855
        {
856
            ArrayList<Tipologia> merge = new ArrayList<Tipologia>();
1315 jmachado 857
            if(getTipologiasSumarios() != null)
858
                merge.addAll(getTipologiasSumarios());
1313 jmachado 859
 
1315 jmachado 860
            if(getTipologiasHorarioReferencia() != null)
861
                for(Tipologia tipo: getTipologiasHorarioReferencia())
1313 jmachado 862
                {
1315 jmachado 863
                    if(!merge.contains(tipo))
864
                    {
865
                        merge.add(tipo);
866
                    }
1313 jmachado 867
                }
1315 jmachado 868
            if(getTipologiasExtraAdicionadas() != null)
869
                for(Tipologia tipo: getTipologiasExtraAdicionadas())
1313 jmachado 870
                {
1315 jmachado 871
                    if(!merge.contains(tipo))
872
                    {
873
                        merge.add(tipo);
874
                    }
1313 jmachado 875
                }
876
            for(Tipologia tipo: merge)
877
            {
1326 jmachado 878
                tipo.setMarkWarning(false);
879
                tipo.setUse(false);
1315 jmachado 880
                if(getTipologiasHorarioReferencia() != null && getTipologiasHorarioReferencia().contains(tipo))
1313 jmachado 881
                {
882
                    tipo.horRef = true;
883
                }
884
 
1315 jmachado 885
                if(getTipologiasExtraAdicionadas() != null && getTipologiasExtraAdicionadas().contains(tipo))
1313 jmachado 886
                {
887
                    tipo.extra = true;
888
                }
889
            }
1326 jmachado 890
 
891
            if(getTipologiasMarcadas() != null)
892
                for(Tipologia t: getTipologiasMarcadas())
893
                {
894
                    for(Tipologia tMerge: merge)
895
                    {
896
                        if(tMerge.equals(t))
897
                        {
898
                            tMerge.setMarkWarning(true);
899
                            break;
900
                        }
901
                    }
902
                }
903
            if(getTipologiasUsadas() != null)
904
                for(Tipologia t: getTipologiasUsadas())
905
                {
906
                    for(Tipologia tMerge: merge)
907
                    {
908
                        if(tMerge.equals(t))
909
                        {
910
                            tMerge.setUse(true);
911
                            break;
912
                        }
913
                    }
914
                }
1313 jmachado 915
            return merge;
916
        }
917
 
918
 
1337 jmachado 919
        public ArrayList<Tipologia> obtainTipologiasSimples(CourseUnit courseUnit)
920
        {
921
            ArrayList<Tipologia> merge = new ArrayList<Tipologia>();
922
 
923
            Map<String,Map<String,Tipologia>> docenteTurmas = new HashMap<String,Map<String,Tipologia>>();
924
 
925
            fullfillTipologiasSimples(getTipologiasSumarios(), docenteTurmas);
926
            fullfillTipologiasSimples(getTipologiasExtraAdicionadas(), docenteTurmas);
927
            fullfillTipologiasSimples(getTipologiasHorarioReferencia(), docenteTurmas);
928
            fullfillTipologiasSimples(getTipologiasMarcadas(), docenteTurmas);
929
            fullfillTipologiasSimples(getTipologiasUsadas(), docenteTurmas);
930
 
931
 
932
            if(docenteTurmas.size() == 0)
933
            {
934
                //NAO Há sumários vamos associar todos os docentes a todas as turmas
935
                if(courseUnit.getTurmas() != null && courseUnit.getTeachers() != null)
936
                {
937
                    for(CourseUnitTurma turma: courseUnit.getTurmas())
938
                    {
939
                        for(Teacher t : courseUnit.getTeachers())
940
                        {
941
                            Tipologia simples = new Tipologia();
942
                            simples.setMarkWarning(true);
943
                            simples.setCdDocente(""+t.getSigesCode());
944
                            simples.setCdTurma(turma.getTurma());
945
                            simples.setCdTipologia(Globals.BACO_TIPOLOGIA_SIMPLES_VIRTUAL_CODE);
946
                            simples.setDsTipologia(Globals.BACO_TIPOLOGIA_SIMPLES_DS);
947
                            merge.add(simples);
948
                        }
949
                    }
950
                }
951
            }
952
            else
953
            {
954
                for(1.5.0/docs/api/java/util/Map.html">Map.Entry<String,Map<String,Tipologia>> docentes: docenteTurmas.entrySet())
955
                {
956
                    for(Tipologia tipologia: docentes.getValue().values())
957
                    {
958
                        merge.add(tipologia);
959
                    }
960
                }
961
            }
962
            return merge;
963
        }
964
 
965
        private void fullfillTipologiasSimples(ArrayList<Tipologia> tipologias, Map<String, Map<String, Tipologia>> docenteTurmas) {
966
            if(tipologias != null)
967
                for(Tipologia tipo: tipologias)
968
                {
969
                    Map<String,Tipologia> turmasDocente = docenteTurmas.get(tipo.getCdDocente());
970
                    if(turmasDocente == null)
971
                        turmasDocente = new HashMap<String,Tipologia>();
972
                    docenteTurmas.put(tipo.getCdDocente(),turmasDocente);
973
                    Tipologia simples = turmasDocente.get(tipo.getCdTurma());
974
                    if(simples==null)
975
                    {
976
                        simples = new Tipologia();
977
                        simples.setCdDocente(tipo.getCdDocente());
978
                        simples.setCdTurma(tipo.getCdTurma());
979
                        simples.setCdTipologia(Globals.BACO_TIPOLOGIA_SIMPLES_VIRTUAL_CODE);
980
                        simples.setDsTipologia(Globals.BACO_TIPOLOGIA_SIMPLES_DS);
1338 jmachado 981
                        if(tipo.hasOcorrencias())
1337 jmachado 982
                        {
983
                            simples.setOcorrencias(tipo.getOcorrencias());
984
                        }
985
                        turmasDocente.put(tipo.getCdTurma(),simples);
986
                    }
1338 jmachado 987
                    if(tipo.hasOcorrencias())
1337 jmachado 988
                    {
1338 jmachado 989
                        if(simples.getOcorrencias() == null)
990
                            simples.setOcorrencias(0);
1337 jmachado 991
                        simples.setOcorrencias(simples.getOcorrencias()+tipo.getOcorrencias());
992
                    }
993
 
994
                }
995
        }
996
 
997
 
1312 jmachado 998
    }
999
 
1000
 
1001
 
1002
 
1003
 
112 jmachado 1004
}