Search in sources :

Example 16 with Sound

use of ai.elimu.model.content.Sound in project webapp by elimu-ai.

the class LetterSoundCorrespondenceEditController method handleSubmit.

@RequestMapping(value = "/{id}", method = RequestMethod.POST)
public String handleSubmit(HttpServletRequest request, HttpSession session, @Valid LetterSoundCorrespondence letterSoundCorrespondence, BindingResult result, Model model) {
    logger.info("handleSubmit");
    // Check if the LetterSoundCorrespondence already exists
    LetterSoundCorrespondence existingLetterSoundCorrespondence = letterSoundCorrespondenceDao.read(letterSoundCorrespondence.getLetters(), letterSoundCorrespondence.getSounds());
    if ((existingLetterSoundCorrespondence != null) && !existingLetterSoundCorrespondence.getId().equals(letterSoundCorrespondence.getId())) {
        result.rejectValue("letters", "NonUnique");
    }
    if (result.hasErrors()) {
        model.addAttribute("letterSoundCorrespondence", letterSoundCorrespondence);
        model.addAttribute("timeStart", System.currentTimeMillis());
        List<Letter> letters = letterDao.readAllOrdered();
        model.addAttribute("letters", letters);
        List<Sound> sounds = soundDao.readAllOrdered();
        model.addAttribute("sounds", sounds);
        model.addAttribute("letterSoundCorrespondenceContributionEvents", letterSoundCorrespondenceContributionEventDao.readAll(letterSoundCorrespondence));
        model.addAttribute("letterSoundCorrespondencePeerReviewEvents", letterSoundCorrespondencePeerReviewEventDao.readAll(letterSoundCorrespondence));
        return "content/letter-sound-correspondence/edit";
    } else {
        letterSoundCorrespondence.setTimeLastUpdate(Calendar.getInstance());
        letterSoundCorrespondence.setRevisionNumber(letterSoundCorrespondence.getRevisionNumber() + 1);
        letterSoundCorrespondenceDao.update(letterSoundCorrespondence);
        LetterSoundCorrespondenceContributionEvent letterSoundCorrespondenceContributionEvent = new LetterSoundCorrespondenceContributionEvent();
        letterSoundCorrespondenceContributionEvent.setContributor((Contributor) session.getAttribute("contributor"));
        letterSoundCorrespondenceContributionEvent.setTime(Calendar.getInstance());
        letterSoundCorrespondenceContributionEvent.setLetterSoundCorrespondence(letterSoundCorrespondence);
        letterSoundCorrespondenceContributionEvent.setRevisionNumber(letterSoundCorrespondence.getRevisionNumber());
        letterSoundCorrespondenceContributionEvent.setComment(StringUtils.abbreviate(request.getParameter("contributionComment"), 1000));
        letterSoundCorrespondenceContributionEvent.setTimeSpentMs(System.currentTimeMillis() - Long.valueOf(request.getParameter("timeStart")));
        letterSoundCorrespondenceContributionEvent.setPlatform(Platform.WEBAPP);
        letterSoundCorrespondenceContributionEventDao.create(letterSoundCorrespondenceContributionEvent);
        String contentUrl = "http://" + EnvironmentContextLoaderListener.PROPERTIES.getProperty("content.language").toLowerCase() + ".elimu.ai/content/letter-sound-correspondence/edit/" + letterSoundCorrespondence.getId();
        DiscordHelper.sendChannelMessage("Letter-sound correspondence edited: " + contentUrl, "\"" + letterSoundCorrespondence.getLetters().stream().map(Letter::getText).collect(Collectors.joining()) + "\"", "Comment: \"" + letterSoundCorrespondenceContributionEvent.getComment() + "\"", null, null);
        return "redirect:/content/letter-sound-correspondence/list#" + letterSoundCorrespondence.getId();
    }
}
Also used : Letter(ai.elimu.model.content.Letter) LetterSoundCorrespondenceContributionEvent(ai.elimu.model.contributor.LetterSoundCorrespondenceContributionEvent) Sound(ai.elimu.model.content.Sound) LetterSoundCorrespondence(ai.elimu.model.content.LetterSoundCorrespondence) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 17 with Sound

use of ai.elimu.model.content.Sound in project webapp by elimu-ai.

the class LetterSoundCorrespondenceEditController method handleRequest.

@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public String handleRequest(Model model, @PathVariable Long id) {
    logger.info("handleRequest");
    LetterSoundCorrespondence letterSoundCorrespondence = letterSoundCorrespondenceDao.read(id);
    model.addAttribute("letterSoundCorrespondence", letterSoundCorrespondence);
    model.addAttribute("timeStart", System.currentTimeMillis());
    List<Letter> letters = letterDao.readAllOrdered();
    model.addAttribute("letters", letters);
    List<Sound> sounds = soundDao.readAllOrdered();
    model.addAttribute("sounds", sounds);
    model.addAttribute("letterSoundCorrespondenceContributionEvents", letterSoundCorrespondenceContributionEventDao.readAll(letterSoundCorrespondence));
    model.addAttribute("letterSoundCorrespondencePeerReviewEvents", letterSoundCorrespondencePeerReviewEventDao.readAll(letterSoundCorrespondence));
    List<Word> words = wordDao.readAllOrderedByUsage();
    model.addAttribute("words", words);
    return "content/letter-sound-correspondence/edit";
}
Also used : Letter(ai.elimu.model.content.Letter) Word(ai.elimu.model.content.Word) Sound(ai.elimu.model.content.Sound) LetterSoundCorrespondence(ai.elimu.model.content.LetterSoundCorrespondence) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 18 with Sound

use of ai.elimu.model.content.Sound in project webapp by elimu-ai.

the class SoundCsvExportController method handleRequest.

@RequestMapping(value = "/sounds.csv", method = RequestMethod.GET)
public void handleRequest(HttpServletResponse response, OutputStream outputStream) throws IOException {
    logger.info("handleRequest");
    List<Sound> sounds = soundDao.readAllOrderedByUsage();
    logger.info("sounds.size(): " + sounds.size());
    CSVFormat csvFormat = CSVFormat.DEFAULT.withHeader("id", "value_ipa", "value_sampa", "audio_id", "diacritic", "sound_type", "usage_count");
    StringWriter stringWriter = new StringWriter();
    CSVPrinter csvPrinter = new CSVPrinter(stringWriter, csvFormat);
    for (Sound sound : sounds) {
        Long audioId = null;
        if (sound.getAudio() != null) {
            audioId = sound.getAudio().getId();
        }
        csvPrinter.printRecord(sound.getId(), sound.getValueIpa(), sound.getValueSampa(), audioId, sound.isDiacritic(), sound.getSoundType(), sound.getUsageCount());
        csvPrinter.flush();
    }
    String csvFileContent = stringWriter.toString();
    response.setContentType("text/csv");
    byte[] bytes = csvFileContent.getBytes();
    response.setContentLength(bytes.length);
    try {
        outputStream.write(bytes);
        outputStream.flush();
        outputStream.close();
    } catch (IOException ex) {
        logger.error(ex);
    }
}
Also used : CSVPrinter(org.apache.commons.csv.CSVPrinter) StringWriter(java.io.StringWriter) Sound(ai.elimu.model.content.Sound) CSVFormat(org.apache.commons.csv.CSVFormat) IOException(java.io.IOException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 19 with Sound

use of ai.elimu.model.content.Sound in project webapp by elimu-ai.

the class SoundCreateController method handleSubmit.

@RequestMapping(method = RequestMethod.POST)
public String handleSubmit(HttpServletRequest request, HttpSession session, @Valid Sound sound, BindingResult result, Model model) {
    logger.info("handleSubmit");
    if (StringUtils.isNotBlank(sound.getValueIpa())) {
        Sound existingSound = soundDao.readByValueIpa(sound.getValueIpa());
        if (existingSound != null) {
            result.rejectValue("valueIpa", "NonUnique");
        }
    }
    if (StringUtils.isNotBlank(sound.getValueSampa())) {
        Sound existingSound = soundDao.readByValueSampa(sound.getValueSampa());
        if (existingSound != null) {
            result.rejectValue("valueSampa", "NonUnique");
        }
    }
    if (result.hasErrors()) {
        model.addAttribute("sound", sound);
        model.addAttribute("timeStart", System.currentTimeMillis());
        model.addAttribute("soundTypes", SoundType.values());
        return "content/sound/create";
    } else {
        sound.setTimeLastUpdate(Calendar.getInstance());
        soundDao.create(sound);
        SoundContributionEvent soundContributionEvent = new SoundContributionEvent();
        soundContributionEvent.setContributor((Contributor) session.getAttribute("contributor"));
        soundContributionEvent.setTime(Calendar.getInstance());
        soundContributionEvent.setSound(sound);
        soundContributionEvent.setRevisionNumber(sound.getRevisionNumber());
        soundContributionEvent.setComment(StringUtils.abbreviate(request.getParameter("contributionComment"), 1000));
        soundContributionEvent.setTimeSpentMs(System.currentTimeMillis() - Long.valueOf(request.getParameter("timeStart")));
        soundContributionEvent.setPlatform(Platform.WEBAPP);
        soundContributionEventDao.create(soundContributionEvent);
        String contentUrl = "http://" + EnvironmentContextLoaderListener.PROPERTIES.getProperty("content.language").toLowerCase() + ".elimu.ai/content/sound/edit/" + sound.getId();
        DiscordHelper.sendChannelMessage("Sound created: " + contentUrl, "/" + soundContributionEvent.getSound().getValueIpa() + "/", "Comment: \"" + soundContributionEvent.getComment() + "\"", null, null);
        return "redirect:/content/sound/list#" + sound.getId();
    }
}
Also used : SoundContributionEvent(ai.elimu.model.contributor.SoundContributionEvent) Sound(ai.elimu.model.content.Sound) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 20 with Sound

use of ai.elimu.model.content.Sound in project webapp by elimu-ai.

the class SoundEditController method handleSubmit.

@RequestMapping(value = "/{id}", method = RequestMethod.POST)
public String handleSubmit(HttpServletRequest request, HttpSession session, @PathVariable Long id, @Valid Sound sound, BindingResult result, Model model) {
    logger.info("handleSubmit");
    if (StringUtils.isNotBlank(sound.getValueIpa())) {
        Sound existingSound = soundDao.readByValueIpa(sound.getValueIpa());
        if ((existingSound != null) && !existingSound.getId().equals(sound.getId())) {
            result.rejectValue("valueIpa", "NonUnique");
        }
    }
    if (StringUtils.isNotBlank(sound.getValueSampa())) {
        Sound existingSound = soundDao.readByValueSampa(sound.getValueSampa());
        if ((existingSound != null) && !existingSound.getId().equals(sound.getId())) {
            result.rejectValue("valueSampa", "NonUnique");
        }
    }
    if (result.hasErrors()) {
        model.addAttribute("sound", sound);
        model.addAttribute("timeStart", System.currentTimeMillis());
        model.addAttribute("soundTypes", SoundType.values());
        model.addAttribute("soundContributionEvents", soundContributionEventDao.readAll(sound));
        return "content/sound/edit";
    } else {
        sound.setTimeLastUpdate(Calendar.getInstance());
        sound.setRevisionNumber(sound.getRevisionNumber() + 1);
        soundDao.update(sound);
        SoundContributionEvent soundContributionEvent = new SoundContributionEvent();
        soundContributionEvent.setContributor((Contributor) session.getAttribute("contributor"));
        soundContributionEvent.setTime(Calendar.getInstance());
        soundContributionEvent.setSound(sound);
        soundContributionEvent.setRevisionNumber(sound.getRevisionNumber());
        soundContributionEvent.setComment(StringUtils.abbreviate(request.getParameter("contributionComment"), 1000));
        soundContributionEvent.setTimeSpentMs(System.currentTimeMillis() - Long.valueOf(request.getParameter("timeStart")));
        soundContributionEvent.setPlatform(Platform.WEBAPP);
        soundContributionEventDao.create(soundContributionEvent);
        String contentUrl = "http://" + EnvironmentContextLoaderListener.PROPERTIES.getProperty("content.language").toLowerCase() + ".elimu.ai/content/sound/edit/" + sound.getId();
        DiscordHelper.sendChannelMessage("Sound edited: " + contentUrl, "/" + soundContributionEvent.getSound().getValueIpa() + "/", "Comment: \"" + soundContributionEvent.getComment() + "\"", null, null);
        return "redirect:/content/sound/list#" + sound.getId();
    }
}
Also used : SoundContributionEvent(ai.elimu.model.contributor.SoundContributionEvent) Sound(ai.elimu.model.content.Sound) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

Sound (ai.elimu.model.content.Sound)23 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)13 Letter (ai.elimu.model.content.Letter)10 LetterSoundCorrespondence (ai.elimu.model.content.LetterSoundCorrespondence)10 IOException (java.io.IOException)6 Word (ai.elimu.model.content.Word)5 ArrayList (java.util.ArrayList)5 CSVFormat (org.apache.commons.csv.CSVFormat)5 JSONArray (org.json.JSONArray)4 LetterSoundCorrespondenceContributionEvent (ai.elimu.model.contributor.LetterSoundCorrespondenceContributionEvent)3 Reader (java.io.Reader)3 Path (java.nio.file.Path)3 CSVParser (org.apache.commons.csv.CSVParser)3 CSVRecord (org.apache.commons.csv.CSVRecord)3 SoundContributionEvent (ai.elimu.model.contributor.SoundContributionEvent)2 SoundGson (ai.elimu.model.v2.gson.content.SoundGson)2 StringWriter (java.io.StringWriter)2 HashMap (java.util.HashMap)2 CSVPrinter (org.apache.commons.csv.CSVPrinter)2 JSONObject (org.json.JSONObject)2