oldQuestion.delete();
    public List<LabelValueBean> run(Integer executionCourseId, Integer distributedTestId, Integer oldQuestionId, Integer newMetadataId,            Integer studentId, TestQuestionChangesType changesType, Boolean delete, TestQuestionStudentsChangesType studentsType)            throws FenixServiceException, ExcepcaoPersistencia {        List<LabelValueBean> result = new ArrayList<LabelValueBean>();        ISuportePersistente persistentSuport = PersistenceSupportFactory.getDefaultPersistenceSupport();        IQuestion oldQuestion = (IQuestion) persistentSuport.getIPersistentQuestion().readByOID(Question.class, oldQuestionId);        if (oldQuestion == null)            throw new InvalidArgumentsServiceException();        IPersistentMetadata persistentMetadata = persistentSuport.getIPersistentMetadata();        IMetadata metadata = null;        List<IQuestion> availableQuestions = new ArrayList<IQuestion>();        if (newMetadataId != null) {            metadata = (IMetadata) persistentMetadata.readByOID(Metadata.class, newMetadataId);            if (metadata == null)                throw new InvalidArgumentsServiceException();            availableQuestions.addAll(metadata.getVisibleQuestions());        } else {            availableQuestions.addAll(oldQuestion.getMetadata().getVisibleQuestions());            availableQuestions.remove(oldQuestion);        }        IPersistentStudentTestQuestion persistentStudentTestQuestion = persistentSuport.getIPersistentStudentTestQuestion();        List<IDistributedTest> distributedTestList = new ArrayList<IDistributedTest>();        if (studentsType.getType().intValue() == TestQuestionStudentsChangesType.ALL_STUDENTS)            distributedTestList = persistentStudentTestQuestion.readDistributedTestsByTestQuestion(oldQuestion.getIdInternal());        else {            IDistributedTest distributedTest = (IDistributedTest) persistentSuport.getIPersistentDistributedTest().readByOID(DistributedTest.class,                    distributedTestId);            if (distributedTest == null)                throw new InvalidArgumentsServiceException();            distributedTestList.add(distributedTest);        }        boolean canDelete = true;        for (int i = 0; i < distributedTestList.size(); i++) {            List<IStudentTestQuestion> studentsTestQuestionList = new ArrayList<IStudentTestQuestion>();            IDistributedTest distributedTest = distributedTestList.get(i);            if (studentsType.getType().intValue() == TestQuestionStudentsChangesType.THIS_STUDENT) {                IStudent student = (IStudent) persistentSuport.getIPersistentStudent().readByOID(Student.class, studentId);                if (student == null)                    throw new InvalidArgumentsServiceException();                studentsTestQuestionList.add(persistentStudentTestQuestion.readByQuestionAndStudentAndDistributedTest(oldQuestion.getIdInternal(),                        student.getIdInternal(), distributedTest.getIdInternal()));            } else if (studentsType.getType().intValue() == TestQuestionStudentsChangesType.STUDENTS_FROM_TEST) {                IStudent student = (IStudent) persistentSuport.getIPersistentStudent().readByOID(Student.class, studentId);                if (student == null)                    throw new InvalidArgumentsServiceException();                Integer order = persistentStudentTestQuestion.readByQuestionAndStudentAndDistributedTest(oldQuestion.getIdInternal(),                        student.getIdInternal(), distributedTest.getIdInternal()).getTestQuestionOrder();                studentsTestQuestionList = persistentStudentTestQuestion.readByOrderAndDistributedTest(order, distributedTest.getIdInternal());            } else                studentsTestQuestionList = persistentStudentTestQuestion.readByQuestionAndDistributedTest(oldQuestion.getIdInternal(),                        distributedTest.getIdInternal());            List<IPerson> group = new ArrayList<IPerson>();            for (IStudentTestQuestion studentTestQuestion : studentsTestQuestionList) {                if (!compareDates(studentTestQuestion.getDistributedTest().getEndDate(), studentTestQuestion.getDistributedTest().getEndHour())) {                    result.add(new LabelValueBean(studentTestQuestion.getDistributedTest().getTitle().concat(" (Ficha Fechada)"), studentTestQuestion                            .getStudent().getNumber().toString()));                    canDelete = false;                } else {                    result.add(new LabelValueBean(studentTestQuestion.getDistributedTest().getTitle(), studentTestQuestion.getStudent().getNumber()                            .toString()));                    if (availableQuestions.size() == 0)                        availableQuestions.addAll(getNewQuestionList(metadata, oldQuestion));                    IQuestion newQuestion = getNewQuestion(availableQuestions);                    if (newMetadataId == null && (newQuestion == null || newQuestion.equals(oldQuestion)))                        return null;                    else if (newQuestion == null)                        throw new InvalidArgumentsServiceException();                    studentTestQuestion.setQuestion(newQuestion);                    studentTestQuestion.setOldResponse(new Integer(0));                    studentTestQuestion.setResponse(null);                    studentTestQuestion.setOptionShuffle(null);                    availableQuestions.remove(newQuestion);                    double oldMark = studentTestQuestion.getTestQuestionMark().doubleValue();                    studentTestQuestion.setTestQuestionMark(new Double(0));                    if (!group.contains(studentTestQuestion.getStudent().getPerson()))                        group.add(studentTestQuestion.getStudent().getPerson());                    if (studentTestQuestion.getDistributedTest().getTestType().equals(new TestType(TestType.EVALUATION))) {                        IOnlineTest onlineTest = (IOnlineTest) persistentSuport.getIPersistentOnlineTest().readByDistributedTest(                                studentTestQuestion.getDistributedTest().getIdInternal());                        IAttends attend = persistentSuport.getIFrequentaPersistente().readByAlunoAndDisciplinaExecucao(                                studentTestQuestion.getStudent(), (IExecutionCourse) distributedTest.getTestScope().getDomainObject());                        IMark mark = persistentSuport.getIPersistentMark().readBy(onlineTest, attend);                        if (mark != null) {                            mark.setMark(getNewStudentMark(persistentSuport, studentTestQuestion.getDistributedTest(), studentTestQuestion                                    .getStudent(), oldMark));                        }                    }                }            }        }        if (delete.booleanValue()) {            metadata = (IMetadata) persistentMetadata.readByOID(Metadata.class, oldQuestion.getKeyMetadata());            if (metadata == null)                throw new InvalidArgumentsServiceException();            metadata.setNumberOfMembers(new Integer(metadata.getNumberOfMembers().intValue() - 1));            removeOldTestQuestion(persistentSuport, oldQuestion);            List metadataQuestions = metadata.getVisibleQuestions();            if (metadataQuestions != null && metadataQuestions.size() <= 1)                metadata.setVisibility(new Boolean(false));            if (canDelete) {                int metadataNumberOfQuestions = persistentMetadata.getNumberOfQuestions(metadata);                oldQuestion.delete();                if (metadataNumberOfQuestions <= 1) {                    metadata.removeExecutionCourse();                    persistentMetadata.deleteByOID(Metadata.class, metadata.getIdInternal());                }            } else {                oldQuestion.setVisibility(new Boolean(false));            }        }        return result;    }