Search in sources :

Example 1 with WordRevisionEvent

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();
    }
}
Also used : Word(ai.elimu.model.content.Word) Allophone(ai.elimu.model.content.Allophone) Contributor(ai.elimu.model.Contributor) WordRevisionEvent(ai.elimu.model.contributor.WordRevisionEvent) Image(ai.elimu.model.content.multimedia.Image) Syllable(ai.elimu.model.content.Syllable) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with WordRevisionEvent

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();
    }
}
Also used : Word(ai.elimu.model.content.Word) Allophone(ai.elimu.model.content.Allophone) Contributor(ai.elimu.model.Contributor) WordRevisionEvent(ai.elimu.model.contributor.WordRevisionEvent) Audio(ai.elimu.model.content.multimedia.Audio) Syllable(ai.elimu.model.content.Syllable) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

Contributor (ai.elimu.model.Contributor)2 Allophone (ai.elimu.model.content.Allophone)2 Syllable (ai.elimu.model.content.Syllable)2 Word (ai.elimu.model.content.Word)2 WordRevisionEvent (ai.elimu.model.contributor.WordRevisionEvent)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 Audio (ai.elimu.model.content.multimedia.Audio)1 Image (ai.elimu.model.content.multimedia.Image)1