Search in sources :

Example 1 with EventSourceDto

use of org.mifos.platform.questionnaire.service.dtos.EventSourceDto in project head by mifos.

the class QuestionnaireServiceFacadeImpl method saveResponses.

@Override
public void saveResponses(QuestionGroupDetails questionGroupDetails) {
    questionnaireService.saveResponses(questionGroupDetails);
    if (auditLogService != null) {
        int creatorId = questionGroupDetails.getCreatorId();
        int entityId = questionGroupDetails.getEntityId();
        for (QuestionGroupDetail questionGroupDetail : questionGroupDetails.getDetails()) {
            EventSourceDto eventSourceDto = questionnaireService.getEventSource(questionGroupDetails.getEventSourceId());
            String source = eventSourceDto.getSource();
            String event = eventSourceDto.getEvent();
            QuestionGroupDetail secondLastQuestionGroupDetail = null;
            int max = 0;
            int secondMax = 0;
            List<QuestionGroupInstanceDetail> oldQuestionGroupDetails = questionnaireService.getQuestionGroupInstances(entityId, eventSourceDto, false, false);
            for (QuestionGroupInstanceDetail oldQuestionGroupDetail : oldQuestionGroupDetails) {
                // find previously entered responses that belong to the same question group as one being saved
                if (oldQuestionGroupDetail.getQuestionGroupDetail().getId().equals(questionGroupDetail.getId())) {
                    if (oldQuestionGroupDetail.getId() >= max) {
                        secondMax = max;
                        max = oldQuestionGroupDetail.getId();
                    } else if (oldQuestionGroupDetail.getId() > secondMax) {
                        secondMax = oldQuestionGroupDetail.getId();
                    }
                }
            }
            if (secondMax != 0) {
                secondLastQuestionGroupDetail = questionnaireService.getQuestionGroupInstance(secondMax).getQuestionGroupDetail();
            }
            auditLogService.addAuditLogRegistry(questionGroupDetail, secondLastQuestionGroupDetail, creatorId, entityId, source, event);
        }
    }
}
Also used : EventSourceDto(org.mifos.platform.questionnaire.service.dtos.EventSourceDto)

Example 2 with EventSourceDto

use of org.mifos.platform.questionnaire.service.dtos.EventSourceDto in project head by mifos.

the class QuestionnaireServiceFacadeTest method testSaveResponsesTwoQuestionGroups.

@Test
public void testSaveResponsesTwoQuestionGroups() {
    questionnaireServiceFacade = new QuestionnaireServiceFacadeImpl(questionnaireService, auditLogService, null);
    // setup test data
    Integer creatorId = new Integer(1);
    Integer entityId = new Integer(2);
    Integer eventSourceId = new Integer(3);
    EventSourceDto eventSourceDto = new EventSourceDto("Create", "Savings", "Test");
    // new responses. these will be saved when saveResponses() is called
    List<SectionDetail> sectionDetails = new ArrayList<SectionDetail>();
    List<QuestionGroupDetail> questionGroups = new ArrayList<QuestionGroupDetail>();
    questionGroups.add(new QuestionGroupDetail(1, "Title 1", sectionDetails));
    questionGroups.add(new QuestionGroupDetail(2, "Title 2", sectionDetails));
    QuestionGroupDetails newQuestionGroupDetails = new QuestionGroupDetails(creatorId, entityId, eventSourceId, questionGroups);
    // persisted responses. when saveResponse() is called, it will store responses in the database. this is why
    // question persistedQuestionGroupInstanceDetailsX uses responses from above.
    QuestionGroupInstanceDetail persistedQuestionGroupInstanceDetail1 = new QuestionGroupInstanceDetail();
    persistedQuestionGroupInstanceDetail1.setId(111);
    persistedQuestionGroupInstanceDetail1.setQuestionGroupDetail(questionGroups.get(0));
    QuestionGroupInstanceDetail persistedQuestionGroupInstanceDetail2 = new QuestionGroupInstanceDetail();
    persistedQuestionGroupInstanceDetail2.setId(222);
    persistedQuestionGroupInstanceDetail2.setQuestionGroupDetail(questionGroups.get(1));
    List<QuestionGroupInstanceDetail> persistedQuestionGroupInstanceDetails = new ArrayList<QuestionGroupInstanceDetail>();
    persistedQuestionGroupInstanceDetails.add(persistedQuestionGroupInstanceDetail1);
    persistedQuestionGroupInstanceDetails.add(persistedQuestionGroupInstanceDetail2);
    when(questionnaireService.getEventSource(eventSourceId)).thenReturn(eventSourceDto);
    when(questionnaireService.getQuestionGroupInstances(entityId, eventSourceDto, false, false)).thenReturn(persistedQuestionGroupInstanceDetails);
    when(questionnaireService.getQuestionGroupInstance(111)).thenReturn(persistedQuestionGroupInstanceDetail1);
    when(questionnaireService.getQuestionGroupInstance(222)).thenReturn(persistedQuestionGroupInstanceDetail2);
    // test
    questionnaireServiceFacade.saveResponses(newQuestionGroupDetails);
    // verify expectations. the key thing to note is second parameter is expected to be NULL
    verify(auditLogService).addAuditLogRegistry(questionGroups.get(0), null, creatorId, entityId, eventSourceDto.getSource(), eventSourceDto.getEvent());
    verify(auditLogService).addAuditLogRegistry(questionGroups.get(1), null, creatorId, entityId, eventSourceDto.getSource(), eventSourceDto.getEvent());
}
Also used : ArrayList(java.util.ArrayList) EventSourceDto(org.mifos.platform.questionnaire.service.dtos.EventSourceDto) Test(org.junit.Test)

Example 3 with EventSourceDto

use of org.mifos.platform.questionnaire.service.dtos.EventSourceDto in project head by mifos.

the class QuestionnaireValidatorImpl method validateForDefineQuestionGroup.

@Override
public void validateForDefineQuestionGroup(QuestionGroupDto questionGroupDto, boolean withDuplicateQuestionTextCheck) {
    ValidationException parentException = new ValidationException(GENERIC_VALIDATION);
    validateQuestionGroupTitle(questionGroupDto, parentException);
    List<EventSourceDto> eventSourceDtos = questionGroupDto.getEventSourceDtos();
    if (eventSourceDtos == null || eventSourceDtos.size() == 0) {
        throw new SystemException(INVALID_EVENT_SOURCE);
    } else {
        for (EventSourceDto eventSourceDto : eventSourceDtos) {
            validateEventSource(eventSourceDto, parentException);
        }
    }
    validateSections(questionGroupDto.getSections(), parentException, withDuplicateQuestionTextCheck);
    if (parentException.hasChildExceptions()) {
        throw parentException;
    }
}
Also used : ValidationException(org.mifos.platform.validations.ValidationException) SystemException(org.mifos.framework.exceptions.SystemException) EventSourceDto(org.mifos.platform.questionnaire.service.dtos.EventSourceDto)

Example 4 with EventSourceDto

use of org.mifos.platform.questionnaire.service.dtos.EventSourceDto in project head by mifos.

the class QuestionnaireServiceTest method shouldSaveResponses.

@Test
public void shouldSaveResponses() {
    List<QuestionDetail> questionDetails = asList(new QuestionDetail(12, "Question 1", QuestionType.FREETEXT, true, true));
    List<SectionDetail> sectionDetails = asList(getSectionDetailWithQuestions("Sec1", questionDetails, "value", false));
    QuestionGroupDetail questionGroupDetail = new QuestionGroupDetail(1, "QG1", Arrays.asList(new EventSourceDto("Create", "Client", null)), sectionDetails, true);
    QuestionGroupInstance questionGroupInstance = new QuestionGroupInstance();
    questionGroupInstance.setVersionNum(0);
    when(questionGroupInstanceDao.retrieveLatestQuestionGroupInstanceByQuestionGroupAndEntity(1, 1)).thenReturn(asList(questionGroupInstance));
    questionnaireService.saveResponses(new QuestionGroupDetails(1, 1, 1, asList(questionGroupDetail)));
    verify(questionGroupInstanceDao).retrieveLatestQuestionGroupInstanceByQuestionGroupAndEntity(1, 1);
    verify(questionnaireValidator, times(1)).validateForQuestionGroupResponses(asList(questionGroupDetail));
    // TODO: Verify the contents using a custom matcher
    verify(questionGroupInstanceDao, times(1)).saveOrUpdateAll(Matchers.<List<QuestionGroupInstance>>any());
}
Also used : QuestionGroupDetail(org.mifos.platform.questionnaire.service.QuestionGroupDetail) QuestionGroupDetails(org.mifos.platform.questionnaire.service.QuestionGroupDetails) SectionDetail(org.mifos.platform.questionnaire.service.SectionDetail) QuestionDetail(org.mifos.platform.questionnaire.service.QuestionDetail) SectionQuestionDetail(org.mifos.platform.questionnaire.service.SectionQuestionDetail) EventSourceDto(org.mifos.platform.questionnaire.service.dtos.EventSourceDto) Test(org.junit.Test)

Example 5 with EventSourceDto

use of org.mifos.platform.questionnaire.service.dtos.EventSourceDto in project head by mifos.

the class QuestionGroupController method groupByEventSource.

private Map<String, List<QuestionGroupDetail>> groupByEventSource(List<QuestionGroupDetail> questionGroups) {
    Map<String, List<QuestionGroupDetail>> questionGroupsCategoriesSplit = new HashMap<String, List<QuestionGroupDetail>>();
    for (QuestionGroupDetail questionGroup : questionGroups) {
        for (EventSourceDto eventSourceDto : questionGroup.getEventSources()) {
            String eventSource = eventSourceDto.toString();
            if (!questionGroupsCategoriesSplit.containsKey(eventSource)) {
                questionGroupsCategoriesSplit.put(eventSource, new ArrayList<QuestionGroupDetail>());
            }
            questionGroupsCategoriesSplit.get(eventSource).add(questionGroup);
        }
    }
    // MIFOS-4164: sort by event source
    Map<String, List<QuestionGroupDetail>> orderedQGCategories = new LinkedHashMap<String, List<QuestionGroupDetail>>();
    for (EventSourceDto eventSourceDto : questionnaireServiceFacade.getAllEventSources()) {
        String eventSource = eventSourceDto.toString();
        List<QuestionGroupDetail> questionGroupsList = questionGroupsCategoriesSplit.get(eventSource);
        if (questionGroupsList != null) {
            orderedQGCategories.put(eventSource, questionGroupsList);
        }
    }
    return orderedQGCategories;
}
Also used : QuestionGroupDetail(org.mifos.platform.questionnaire.service.QuestionGroupDetail) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) List(java.util.List) EventSourceDto(org.mifos.platform.questionnaire.service.dtos.EventSourceDto) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

EventSourceDto (org.mifos.platform.questionnaire.service.dtos.EventSourceDto)39 Test (org.junit.Test)28 QuestionGroupDetail (org.mifos.platform.questionnaire.service.QuestionGroupDetail)15 SectionDetail (org.mifos.platform.questionnaire.service.SectionDetail)12 ArrayList (java.util.ArrayList)10 Transactional (org.springframework.transaction.annotation.Transactional)7 QuestionGroup (org.mifos.platform.questionnaire.domain.QuestionGroup)6 EventSourceEntity (org.mifos.platform.questionnaire.domain.EventSourceEntity)5 SectionQuestionDetail (org.mifos.platform.questionnaire.service.SectionQuestionDetail)5 SystemException (org.mifos.framework.exceptions.SystemException)4 EventSourcesMatcher (org.mifos.platform.questionnaire.matchers.EventSourcesMatcher)3 QuestionDetail (org.mifos.platform.questionnaire.service.QuestionDetail)3 QuestionGroupInstanceDetail (org.mifos.platform.questionnaire.service.QuestionGroupInstanceDetail)3 ValidationException (org.mifos.platform.validations.ValidationException)3 LinkedHashMap (java.util.LinkedHashMap)2 QuestionEntity (org.mifos.platform.questionnaire.domain.QuestionEntity)2 Section (org.mifos.platform.questionnaire.domain.Section)2 SectionQuestion (org.mifos.platform.questionnaire.domain.SectionQuestion)2 MandatoryAnswerNotFoundException (org.mifos.platform.questionnaire.exceptions.MandatoryAnswerNotFoundException)2 QuestionGroupDetails (org.mifos.platform.questionnaire.service.QuestionGroupDetails)2