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);
}
}
}
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());
}
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;
}
}
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());
}
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;
}
Aggregations