private int calculateVoteTotalRate(Vote vote) { BibliographicPollingTemplate template = (BibliographicPollingTemplate) vote.getPollingTemplate(); template = (BibliographicPollingTemplate) voteDao.getById(AbstractPollingTemplate.class, template.getId()); List<AbstractTemplateQuestion> templateQuestions = template.getQuestionList(); int totalWeight = 0; for (int i = 0; i < templateQuestions.size(); i++) { totalWeight += templateQuestions.get(i).getWeight(); } float totalRate = 0; List<AbstractQuestionAnswer> questionAnswers = vote.getQuestionAnswer(); for (int i = 0; i < questionAnswers.size(); i++) { int questionWeight = 0; int questionTotalRate = 0; int answerTotalRate = 0; AbstractQuestionAnswer questionAnswer = questionAnswers.get(i); questionWeight = questionAnswer.getQuestion().getWeight(); if (questionAnswer instanceof MultipleChoiceQuestionAnswer) { questionTotalRate = calculateOptionsTotalRate( (MultipleChoicePollingQuestion) questionAnswer.getQuestion().getQuestion()); answerTotalRate = 0; List<PollingAnswerOption> optionList = ((MultipleChoiceQuestionAnswer) questionAnswers.get(i)).getOptionList(); for (int j = 0; j < optionList.size(); j++) { PollingAnswerOption pollingAnswerOption = optionList.get(j); answerTotalRate += pollingAnswerOption.getPollingOption().getWeight(); } } else if (questionAnswer instanceof RatingQuestionAnswer) { questionTotalRate = ((RatingTemplateQuestion) ((RatingQuestionAnswer) questionAnswer).getQuestion()) .getMaxValue(); answerTotalRate = ((RatingQuestionAnswer) questionAnswer).getValue(); } totalRate += ((new Integer(answerTotalRate)).floatValue() / (new Integer(questionTotalRate)).floatValue()) * questionWeight; } return Math.round(((totalRate / totalWeight) * 100)); }
public MultipleChoiceQuestionReport getMultipleChoiceQuestionTotalRateOnRecord( VoteReportFilter voteReportFilter, MultipleChoicePollingQuestion question) { MultipleChoiceQuestionReport multipleChoiceQuestionReport = new MultipleChoiceQuestionReport(voteDao.findOptions(question)); multipleChoiceQuestionReport.setQuestion(question); List<AbstractQuestionAnswer> questionAnswers = voteDao.findVoteQuestionAnswers(voteReportFilter, question); multipleChoiceQuestionReport.setCount(questionAnswers.size()); float questionTotalWeight = calculateOptionsTotalRate(question); float sumOfTotalRates = questionAnswers.size() * questionTotalWeight; float sumOfRates = 0; for (int i = 0; i < questionAnswers.size(); i++) { MultipleChoiceQuestionAnswer multipleChoiceQuestionAnswer = (MultipleChoiceQuestionAnswer) questionAnswers.get(i); List<PollingAnswerOption> optionList = multipleChoiceQuestionAnswer.getOptionList(); for (int j = 0; j < optionList.size(); j++) { PollingAnswerOption pollingAnswerOption = optionList.get(j); MultipleChoiceQuestionReport.OptionInfo optionInfo = findOptionCount( multipleChoiceQuestionReport.getOptionCounts(), pollingAnswerOption.getPollingOption()); optionInfo.setCount(optionInfo.getCount() + 1); float percent = (new Float(optionInfo.getCount()) / new Float(multipleChoiceQuestionReport.getCount())); optionInfo.setPercent(Math.round(percent * 10000) / 100); sumOfRates += pollingAnswerOption.getPollingOption().getWeight(); } } // multipleChoiceQuestionReport.setOp tionCounts(optionCounts); if (sumOfTotalRates != 0) { multipleChoiceQuestionReport.setTotalRate(sumOfRates / sumOfTotalRates); } return multipleChoiceQuestionReport; }