person.changeUsername(username, persons);
person.changeUsername(username);
    public InfoMasterDegreeCandidate run(Specialization degreeType, Integer executionDegreeID,            String name, String identificationDocumentNumber, IDDocumentType identificationDocumentType)            throws Exception {        IPersistentMasterDegreeCandidate masterDegreeCandidateDAO = persistentSupport                .getIPersistentMasterDegreeCandidate();        // Read the Execution of this degree in the current execution Year        ExecutionDegree executionDegree = (ExecutionDegree) persistentObject.readByOID(                ExecutionDegree.class, executionDegreeID);        MasterDegreeCandidate masterDegreeCandidateFromDB = masterDegreeCandidateDAO                .readByIdentificationDocNumberAndTypeAndExecutionDegreeAndSpecialization(                        identificationDocumentNumber, identificationDocumentType, executionDegreeID,                        degreeType);        if (masterDegreeCandidateFromDB != null) {            throw new ExistingServiceException();        }        // Set the Candidate's Situation        CandidateSituation candidateSituation = DomainFactory.makeCandidateSituation();        // First candidate situation        candidateSituation.setRemarks("Pr-Candidatura. Pagamento da candidatura por efectuar.");        candidateSituation.setSituation(new SituationName(SituationName.PRE_CANDIDATO));        candidateSituation.setValidation(new State(State.ACTIVE));        Calendar actualDate = Calendar.getInstance();        candidateSituation.setDate(actualDate.getTime());        // Create the Candidate        MasterDegreeCandidate masterDegreeCandidate = DomainFactory.makeMasterDegreeCandidate();        masterDegreeCandidate.addSituations(candidateSituation);        masterDegreeCandidate.setSpecialization(degreeType);        masterDegreeCandidate.setExecutionDegree(executionDegree);        // Generate the Candidate's number        Integer number = persistentSupport.getIPersistentMasterDegreeCandidate().generateCandidateNumber(                executionDegree.getExecutionYear().getYear(),                executionDegree.getDegreeCurricularPlan().getDegree().getSigla(), degreeType);        masterDegreeCandidate.setCandidateNumber(number);        // Check if the person Exists        Person person = persistentSupport.getIPessoaPersistente().lerPessoaPorNumDocIdETipoDocId(                identificationDocumentNumber, identificationDocumentType);        List<Person> persons = Party.readAllPersons();        Role personRole = Role.getRoleByRoleType(RoleType.PERSON);        if (person == null) {                        //Generate Person Username            String username = MasterDegreeCandidate.generateUsernameForNewCandidate(                    masterDegreeCandidate, persons);                        // Create the new Person            person = DomainFactory.makePerson(name, identificationDocumentNumber,                    identificationDocumentType, Gender.MALE, username);                                  // Give the Person Role            person.getPersonRoles().add(personRole);                    } else {            if (person.getUsername().startsWith("INA")) {                // Generate Person Username                String username = MasterDegreeCandidate.generateUsernameForNewCandidate(                        masterDegreeCandidate, persons);                person.changeUsername(username, persons);            }            if (person.getUsername().startsWith("B")) {                // Generate Person Username                person.getPersonRoles().add(personRole);                String username = MasterDegreeCandidate.generateUsernameForNewCandidate(                        masterDegreeCandidate, persons);                person.changeUsername(username, persons);            }        }        if (!person.hasRole(RoleType.PERSON)) {            person.addPersonRoles(personRole);        }        if (!person.hasRole(RoleType.MASTER_DEGREE_CANDIDATE)) {            person.addPersonRoles(Role.getRoleByRoleType(                    RoleType.MASTER_DEGREE_CANDIDATE));        }        masterDegreeCandidate.setPerson(person);        // Return the new Candidate        InfoMasterDegreeCandidate infoMasterDegreeCandidate = InfoMasterDegreeCandidateWithInfoPerson                .newInfoFromDomain(masterDegreeCandidate);        InfoExecutionDegree infoExecutionDegree = InfoExecutionDegreeWithInfoExecutionYearAndDegreeCurricularPlan                .newInfoFromDomain(masterDegreeCandidate.getExecutionDegree());        infoMasterDegreeCandidate.setInfoExecutionDegree(infoExecutionDegree);        return infoMasterDegreeCandidate;    }