use of ai.elimu.model.contributor.WordRevisionEvent in project webapp by elimu-ai.
the class WordCreateController method handleSubmit.
@RequestMapping(method = RequestMethod.POST)
public String handleSubmit(HttpSession session, @Valid Word word, BindingResult result, Model model) {
logger.info("handleSubmit");
Word existingWord = wordDao.readByText(word.getLocale(), word.getText());
if (existingWord != null) {
result.rejectValue("text", "NonUnique");
}
Contributor contributor = (Contributor) session.getAttribute("contributor");
List<Allophone> allophones = allophoneDao.readAllOrderedByUsage(contributor.getLocale());
// Verify that only valid Allophones are used
String allAllophonesCombined = "";
for (Allophone allophone : allophones) {
allAllophonesCombined += allophone.getValueIpa();
}
if (StringUtils.isNotBlank(word.getPhonetics())) {
for (char allophoneCharacter : word.getPhonetics().toCharArray()) {
String allophone = String.valueOf(allophoneCharacter);
if (!allAllophonesCombined.contains(allophone)) {
result.rejectValue("phonetics", "Invalid");
break;
}
}
}
if (result.hasErrors()) {
model.addAttribute("word", word);
model.addAttribute("allophones", allophones);
model.addAttribute("wordTypes", WordType.values());
model.addAttribute("spellingConsistencies", SpellingConsistency.values());
return "content/word/create";
} else {
if (!"I".equals(word.getText())) {
word.setText(word.getText().toLowerCase());
}
word.setTimeLastUpdate(Calendar.getInstance());
wordDao.create(word);
WordRevisionEvent wordRevisionEvent = new WordRevisionEvent();
wordRevisionEvent.setContributor(contributor);
wordRevisionEvent.setCalendar(Calendar.getInstance());
wordRevisionEvent.setWord(word);
wordRevisionEvent.setText(word.getText());
wordRevisionEvent.setPhonetics(word.getPhonetics());
wordRevisionEventDao.create(wordRevisionEvent);
// Label Image with Word of matching title
Image matchingImage = imageDao.read(word.getText(), word.getLocale());
if (matchingImage != null) {
Set<Word> labeledWords = matchingImage.getWords();
if (!labeledWords.contains(word)) {
labeledWords.add(word);
matchingImage.setWords(labeledWords);
imageDao.update(matchingImage);
}
}
// Delete syllables that are actual words
Syllable syllable = syllableDao.readByText(contributor.getLocale(), word.getText());
if (syllable != null) {
syllableDao.delete(syllable);
}
if (EnvironmentContextLoaderListener.env == Environment.PROD) {
String text = URLEncoder.encode(contributor.getFirstName() + " just added a new Word:\n" + "• Language: \"" + word.getLocale().getLanguage() + "\"\n" + "• Text: \"" + word.getText() + "\"\n" + "• Phonetics (IPA): /" + word.getPhonetics() + "/\n" + "• Word type: " + word.getWordType() + "\n" + "• Spelling consistency: " + word.getSpellingConsistency() + "\n" + "See ") + "http://elimu.ai/content/word/edit/" + word.getId();
String iconUrl = contributor.getImageUrl();
SlackApiHelper.postMessage(SlackApiHelper.getChannelId(Team.CONTENT_CREATION), text, iconUrl, null);
}
return "redirect:/content/word/list#" + word.getId();
}
}
use of ai.elimu.model.contributor.WordRevisionEvent in project webapp by elimu-ai.
the class WordEditController method handleSubmit.
@RequestMapping(value = "/{id}", method = RequestMethod.POST)
public String handleSubmit(HttpSession session, @Valid Word word, BindingResult result, Model model, HttpServletRequest request) {
logger.info("handleSubmit");
Word existingWord = wordDao.readByText(word.getLocale(), word.getText());
if ((existingWord != null) && !existingWord.getId().equals(word.getId())) {
result.rejectValue("text", "NonUnique");
}
Contributor contributor = (Contributor) session.getAttribute("contributor");
List<Allophone> allophones = allophoneDao.readAllOrderedByUsage(contributor.getLocale());
// Verify that only valid Allophones are used
String allAllophonesCombined = "";
for (Allophone allophone : allophones) {
allAllophonesCombined += allophone.getValueIpa();
}
if (StringUtils.isNotBlank(word.getPhonetics())) {
for (char allophoneCharacter : word.getPhonetics().toCharArray()) {
String allophone = String.valueOf(allophoneCharacter);
if (!allAllophonesCombined.contains(allophone)) {
result.rejectValue("phonetics", "Invalid");
break;
}
}
}
if (result.hasErrors()) {
model.addAttribute("word", word);
model.addAttribute("allophones", allophones);
model.addAttribute("wordTypes", WordType.values());
model.addAttribute("spellingConsistencies", SpellingConsistency.values());
model.addAttribute("wordRevisionEvents", wordRevisionEventDao.readAll(word));
Audio audio = audioDao.read(word.getText(), contributor.getLocale());
model.addAttribute("audio", audio);
return "content/word/edit";
} else {
if (!"I".equals(word.getText())) {
word.setText(word.getText().toLowerCase());
}
word.setTimeLastUpdate(Calendar.getInstance());
word.setRevisionNumber(word.getRevisionNumber() + 1);
wordDao.update(word);
WordRevisionEvent wordRevisionEvent = new WordRevisionEvent();
wordRevisionEvent.setContributor(contributor);
wordRevisionEvent.setCalendar(Calendar.getInstance());
wordRevisionEvent.setWord(word);
wordRevisionEvent.setText(word.getText());
wordRevisionEvent.setPhonetics(word.getPhonetics());
if (StringUtils.isNotBlank(request.getParameter("comment"))) {
wordRevisionEvent.setComment(request.getParameter("comment"));
}
wordRevisionEventDao.create(wordRevisionEvent);
// Delete syllables that are actual words
Syllable syllable = syllableDao.readByText(contributor.getLocale(), word.getText());
if (syllable != null) {
syllableDao.delete(syllable);
}
if (EnvironmentContextLoaderListener.env == Environment.PROD) {
String text = URLEncoder.encode(contributor.getFirstName() + " just updated a Word:\n" + "• Language: \"" + word.getLocale().getLanguage() + "\"\n" + "• Text: \"" + word.getText() + "\"\n" + "• Phonetics (IPA): /" + word.getPhonetics() + "/\n" + "• Word type: " + word.getWordType() + "\n" + "• Spelling consistency: " + word.getSpellingConsistency() + "\n" + "• Comment: \"" + wordRevisionEvent.getComment() + "\"\n" + "See ") + "http://elimu.ai/content/word/edit/" + word.getId();
String iconUrl = contributor.getImageUrl();
SlackApiHelper.postMessage(SlackApiHelper.getChannelId(Team.CONTENT_CREATION), text, iconUrl, null);
}
return "redirect:/content/word/list#" + word.getId();
}
}
Aggregations