Skip to content
Snippets Groups Projects
Commit 7f9c55b0 authored by AnneliKlamas's avatar AnneliKlamas
Browse files

#46 added getting info from dropdown

parent c3f64cf0
No related branches found
No related tags found
1 merge request!48feat: Add support for the template dropdowns #46
......@@ -5,6 +5,7 @@ import com.quiz.converter.models.QuestionState;
import com.quiz.converter.models.enums.QuestionType;
import com.quiz.converter.models.enums.QuestionWarningType;
import java.util.List;
import java.util.Optional;
public class QuestionHandler {
......@@ -14,7 +15,7 @@ public class QuestionHandler {
this.state = state;
}
public Optional<Question> handleQuestion(String text) {
public Optional<Question> handleQuestion(String text, List<String> dropdownValues) {
Optional<Question> question = Optional.empty();
if (!state.getAnswerOptions().isEmpty()) {
var validationHandler = new QuestionValidationHandler(state);
......@@ -22,22 +23,22 @@ public class QuestionHandler {
question = Optional.of(state.createQuestion());
state.cleanState();
}
addQuestionType(text);
addQuestionType(text, dropdownValues);
addQuestionName(text);
addQuestionOptions(text);
addQuestionOptions(text, dropdownValues);
return question;
}
private void addQuestionOptions(String text) {
private void addQuestionOptions(String text, List<String> dropdownValues) {
var lowerCaseText = text.toLowerCase();
if (lowerCaseText.matches(".*(no)\s*(shuffle).*")) {
if (matchesRegex(dropdownValues, lowerCaseText, ".*(no)\s*(shuffle).*")) {
state.setShuffle(false);
} else if (lowerCaseText.matches(".*(shuffle).*")) {
} else if (matchesRegex(dropdownValues, lowerCaseText, ".*(shuffle).*")) {
state.setShuffle(true);
}
if (lowerCaseText.matches(".*(no)\s*(partial)\s*(credit).*")) {
if (matchesRegex(dropdownValues, lowerCaseText, ".*(no)\s*(partial)\s*(credit).*")) {
state.setPartialCredit(false);
} else if (lowerCaseText.matches(".*(partial)\s*(credit).*")) {
} else if (matchesRegex(dropdownValues, lowerCaseText, ".*(partial)\s*(credit).*")) {
state.setPartialCredit(true);
}
}
......@@ -52,18 +53,22 @@ public class QuestionHandler {
}
}
private void addQuestionType(String text) {
private void addQuestionType(String text, List<String> dropdownValues) {
var lowerCaseText = text.toLowerCase();
if (lowerCaseText.matches(".*(single)\s*(choice).*")) {
if (matchesRegex(dropdownValues, lowerCaseText, ".*(single)\s*(choice).*")) {
state.setType(QuestionType.SINGLE_CHOICE);
} else if (lowerCaseText.matches(".*(multiple)\s*(choice).*") || lowerCaseText.contains("checkbox")) {
} else if (matchesRegex(dropdownValues, lowerCaseText, ".*(multiple)\s*(choice).*|.*(checkbox).*")) {
state.setType(QuestionType.MULTIPLE_CHOICE);
} else if (lowerCaseText.matches(".*(text)\s*(match).*") || lowerCaseText.matches(".*(short)\s*(answer).*")) {
} else if (matchesRegex(dropdownValues, lowerCaseText, ".*(text)\s*(match).*|.*(short)\s*(answer).*")) {
state.setType(QuestionType.TEXT_MATCH);
} else if (lowerCaseText.matches(".*(regular)\s*(expression).*")) {
} else if (matchesRegex(dropdownValues, lowerCaseText, ".*(regular)\s*(expression).*")) {
state.setType(QuestionType.REGULAR_EXPRESSION);
} else {
state.setType(QuestionType.UNKNOWN);
}
}
private boolean matchesRegex(List<String> dropdownValues, String lowerCaseText, String regex) {
return lowerCaseText.matches(regex) || dropdownValues.stream().anyMatch(s -> s.matches(regex));
}
}
......@@ -8,6 +8,7 @@ import com.quiz.converter.models.enums.ParagraphType;
import com.quiz.converter.models.enums.QuestionWarningType;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFSDT;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
......@@ -36,13 +37,14 @@ public class FileUploadService {
private void handleParagraph(QuestionState state, ArrayList<Question> questions, XWPFParagraph paragraph) {
var text = paragraph.getParagraphText();
var paragraphPictures = handleParagraphPictures(paragraph);
var dropdownValues = handleDropdownLists(paragraph);
var paragraphType = getParagraphType(text, state);
switch (paragraphType) {
case EMPTY_TEXT -> handleEmptyTextParagraph(paragraphPictures, state);
case QUESTION_DETAILS -> {
var questionHandler = new QuestionHandler(state);
questionHandler.handleQuestion(text).ifPresent(questions::add);
questionHandler.handleQuestion(text, dropdownValues).ifPresent(questions::add);
state.setPreviousParagraphType(paragraphType);
}
case QUESTION_DESCRIPTION -> {
......@@ -64,6 +66,16 @@ public class FileUploadService {
}
}
private List<String> handleDropdownLists(XWPFParagraph paragraph) {
var dropdownValues = new ArrayList<String>();
for (var run : paragraph.getIRuns()) {
if (run instanceof XWPFSDT) {
dropdownValues.add(((XWPFSDT) run).getContent().getText());
}
}
return dropdownValues;
}
private static void handleEmptyTextParagraph(List<Picture> paragraphPictures, QuestionState state) {
if (!paragraphPictures.isEmpty()) {
if (state.getPreviousParagraphType().equals(ParagraphType.ANSWER_OPTION)) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment