student.setInterruptedStudies(false);
    public InfoCandidateRegistration run(Integer candidateID, Integer branchID, Integer studentNumber,            IUserView userView) throws FenixServiceException, ExcepcaoPersistencia {        ISuportePersistente sp = PersistenceSupportFactory.getDefaultPersistenceSupport();        IStudentCurricularPlan studentCurricularPlanResult = null;        IStudent student = null;                if (studentNumber != null) {            student = sp.getIPersistentStudent().readStudentByNumberAndDegreeType(studentNumber,                    DegreeType.MASTER_DEGREE);        }        IMasterDegreeCandidate masterDegreeCandidate = (IMasterDegreeCandidate) sp                .getIPersistentMasterDegreeCandidate().readByOID(MasterDegreeCandidate.class,                        candidateID);        if (student != null) {            if ((!masterDegreeCandidate.getPerson().getIdInternal().equals(                    student.getPerson().getIdInternal()))) {                throw new ExistingServiceException();            }        }        if (!validSituation(masterDegreeCandidate.getActiveCandidateSituation())) {            throw new InvalidChangeServiceException();        }        // Check if a Master Degree Student Already Exists        if (student == null) {            student = sp.getIPersistentStudent().readByPersonAndDegreeType(                    masterDegreeCandidate.getPerson().getIdInternal(), DegreeType.MASTER_DEGREE);        }        IRole role = (IRole) CollectionUtils.find(masterDegreeCandidate.getPerson().getPersonRoles(),                new Predicate() {                    public boolean evaluate(Object arg0) {                        IRole role = (IRole) arg0;                        return role.getRoleType() == RoleType.MASTER_DEGREE_CANDIDATE;                    }                });        if (role != null) {            masterDegreeCandidate.getPerson().getPersonRoles().remove(role);        }        Integer newStudentNumber = sp.getIPersistentStudent().generateStudentNumber(                DegreeType.MASTER_DEGREE);        if (studentNumber != null && studentNumber.intValue() > newStudentNumber.intValue())            throw new InvalidStudentNumberServiceException();        if (student == null) {            Integer number = ((studentNumber == null) ? newStudentNumber : studentNumber);            IStudentKind studentKind = sp.getIPersistentStudentKind().readByStudentType(                    new StudentType(StudentType.NORMAL));            StudentState state = new StudentState(StudentState.INSCRITO);            student = DomainFactory.makeStudent(masterDegreeCandidate.getPerson(), number, studentKind, state, false,                    false, EntryPhase.FIRST_PHASE_OBJ, DegreeType.MASTER_DEGREE);            List roles = new ArrayList();            Iterator iterator = masterDegreeCandidate.getPerson().getPersonRolesIterator();            while (iterator.hasNext()) {                // roles.add(Cloner.copyIRole2InfoRole((IRole)                // iterator.next()));                roles.add(InfoRole.newInfoFromDomain((IRole) iterator.next()));            }            // Give The Student Role if Necessary            if (!AuthorizationUtils.containsRole(roles, RoleType.STUDENT)) {                role = sp.getIPersistentRole().readByRoleType(RoleType.STUDENT);                masterDegreeCandidate.getPerson().addPersonRoles(role);            }        }        List<IStudentCurricularPlan> studentCurricularPlans = masterDegreeCandidate.getExecutionDegree()                .getDegreeCurricularPlan().getStudentCurricularPlans();        IStudentCurricularPlan existingStudentCurricularPlan = null;        for (IStudentCurricularPlan scp : studentCurricularPlans) {            if (scp.getStudent().getIdInternal().equals(student.getIdInternal())                    && scp.getCurrentState().equals(StudentCurricularPlanState.ACTIVE)) {                existingStudentCurricularPlan = scp;                break;            }        }        if (existingStudentCurricularPlan != null) {            throw new ExistingServiceException();        }        IBranch branch = (IBranch) sp.getIPersistentBranch().readByOID(Branch.class, branchID);        IDegreeCurricularPlan degreecurricularPlan = masterDegreeCandidate.getExecutionDegree()                .getDegreeCurricularPlan();        Date startDate = Calendar.getInstance().getTime();        IStudentCurricularPlan studentCurricularPlan = DomainFactory.makeStudentCurricularPlan(student,                degreecurricularPlan, branch, startDate, StudentCurricularPlanState.ACTIVE,                masterDegreeCandidate.getGivenCredits(), masterDegreeCandidate.getSpecialization());        // Get the Candidate Enrolments        List candidateEnrolments = sp.getIPersistentCandidateEnrolment().readByMDCandidate(                masterDegreeCandidate.getIdInternal());        Iterator iterator = candidateEnrolments.iterator();        while (iterator.hasNext()) {            ICandidateEnrolment candidateEnrolment = (ICandidateEnrolment) iterator.next();            // TODO refactor            IExecutionPeriod executionPeriod = sp.getIPersistentExecutionPeriod()                    .readActualExecutionPeriod();            IEnrolment enrolment = DomainFactory.makeEnrolment();            enrolment.initializeAsNew(studentCurricularPlan, candidateEnrolment.getCurricularCourse(),                    executionPeriod, EnrollmentCondition.FINAL, userView.getUtilizador());            // TODO end refactor        }        // Change the Candidate Situation        ICandidateSituation oldCandidateSituation = (ICandidateSituation) sp                .getIPersistentCandidateSituation().readByOID(CandidateSituation.class,                        masterDegreeCandidate.getActiveCandidateSituation().getIdInternal(), true);        oldCandidateSituation.setValidation(new State(State.INACTIVE));        ICandidateSituation candidateSituation = DomainFactory.makeCandidateSituation();        candidateSituation.setDate(Calendar.getInstance().getTime());        candidateSituation.setMasterDegreeCandidate(masterDegreeCandidate);        candidateSituation.setValidation(new State(State.ACTIVE));        candidateSituation.setSituation(SituationName.ENROLLED_OBJ);        // Copy Qualifications        IQualification qualification = DomainFactory.makeQualification();        if (masterDegreeCandidate.getAverage() != null) {            qualification.setMark(masterDegreeCandidate.getAverage().toString());        }        qualification.setPerson(masterDegreeCandidate.getPerson());        if (masterDegreeCandidate.getMajorDegreeSchool() == null) {            qualification.setSchool("");        } else {            qualification.setSchool(masterDegreeCandidate.getMajorDegreeSchool());        }        qualification.setTitle(masterDegreeCandidate.getMajorDegree());        Calendar calendar = Calendar.getInstance();        if (masterDegreeCandidate.getMajorDegreeYear() == null) {            qualification.setDate(calendar.getTime());        } else {            calendar.set(Calendar.YEAR, masterDegreeCandidate.getMajorDegreeYear().intValue());            qualification.setDate(calendar.getTime());        }        qualification.setDegree(masterDegreeCandidate.getMajorDegree());        // Change the Username If neccessary        changeUsernameIfNeccessary(studentCurricularPlan.getStudent());        studentCurricularPlanResult = studentCurricularPlan;        // Create Gratuity Situations        IGratuityValues gratuityValues = masterDegreeCandidate.getExecutionDegree().getGratuityValues();        if (gratuityValues == null) {            throw new GratuityValuesNotDefinedServiceException(                    "error.exception.masterDegree.gratuity.gratuityValuesNotDefined");        }        IGratuitySituation gratuitySituation = DomainFactory.makeGratuitySituation();        gratuitySituation.setGratuityValues(gratuityValues);        gratuitySituation.setStudentCurricularPlan(studentCurricularPlan);        gratuitySituation.setWhen(Calendar.getInstance().getTime());        Double totalValue = null;        if (studentCurricularPlan.getSpecialization().equals(Specialization.MASTER_DEGREE)) {            totalValue = calculateTotalValueForMasterDegree(gratuityValues);        } else if (studentCurricularPlan.getSpecialization().equals(Specialization.SPECIALIZATION)) {            totalValue = new Double(0);        }        if (totalValue == null) {            throw new GratuityValuesNotDefinedServiceException(                    "error.exception.masterDegree.gratuity.gratuityValuesNotDefined");        }        gratuitySituation.setRemainingValue(totalValue);        gratuitySituation.setTotalValue(totalValue);        InfoCandidateRegistration infoCandidateRegistration = new InfoCandidateRegistration();        infoCandidateRegistration.setInfoMasterDegreeCandidate(InfoMasterDegreeCandidateWithInfoPerson                .newInfoFromDomain(masterDegreeCandidate));        infoCandidateRegistration                .setInfoStudentCurricularPlan(InfoStudentCurricularPlanWithInfoStudentAndInfoBranch                        .newInfoFromDomain(studentCurricularPlanResult));        infoCandidateRegistration.setEnrolments(new ArrayList());        Iterator iteratorSCPs = studentCurricularPlanResult.getEnrolments().iterator();        while (iteratorSCPs.hasNext()) {            IEnrolment enrolment = (IEnrolment) iteratorSCPs.next();            InfoEnrolment infoEnrolment = InfoEnrolmentWithStudentPlanAndCourseAndExecutionPeriod                    .newInfoFromDomain(enrolment);            infoCandidateRegistration.getEnrolments().add(infoEnrolment);        }        return infoCandidateRegistration;    }