Search in sources :

Example 1 with Sound

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

the class LetterSoundCorrespondenceCreateController method handleRequest.

@RequestMapping(method = RequestMethod.GET)
public String handleRequest(Model model) {
    logger.info("handleRequest");
    LetterSoundCorrespondence letterSoundCorrespondence = new LetterSoundCorrespondence();
    model.addAttribute("letterSoundCorrespondence", letterSoundCorrespondence);
    List<Letter> letters = letterDao.readAllOrdered();
    model.addAttribute("letters", letters);
    List<Sound> sounds = soundDao.readAllOrdered();
    model.addAttribute("sounds", sounds);
    model.addAttribute("timeStart", System.currentTimeMillis());
    return "content/letter-sound-correspondence/create";
}
Also used : Letter(ai.elimu.model.content.Letter) Sound(ai.elimu.model.content.Sound) LetterSoundCorrespondence(ai.elimu.model.content.LetterSoundCorrespondence) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with Sound

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

the class LetterSoundCorrespondenceCreateController method handleSubmit.

@RequestMapping(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) {
        result.rejectValue("letters", "NonUnique");
    }
    if (result.hasErrors()) {
        model.addAttribute("letterSoundCorrespondence", letterSoundCorrespondence);
        List<Letter> letters = letterDao.readAllOrdered();
        model.addAttribute("letters", letters);
        List<Sound> sounds = soundDao.readAllOrdered();
        model.addAttribute("sounds", sounds);
        model.addAttribute("timeStart", System.currentTimeMillis());
        return "content/letter-sound-correspondence/create";
    } else {
        letterSoundCorrespondence.setTimeLastUpdate(Calendar.getInstance());
        letterSoundCorrespondenceDao.create(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 created: " + 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 3 with Sound

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

the class SoundsRestController method handleGetRequest.

@RequestMapping(method = RequestMethod.GET)
public String handleGetRequest() {
    logger.info("handleGetRequest");
    JSONArray soundsJsonArray = new JSONArray();
    for (Sound sound : soundDao.readAllOrdered()) {
        SoundGson soundGson = JpaToGsonConverter.getSoundGson(sound);
        String json = new Gson().toJson(soundGson);
        soundsJsonArray.put(new JSONObject(json));
    }
    String jsonResponse = soundsJsonArray.toString();
    logger.info("jsonResponse: " + jsonResponse);
    return jsonResponse;
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) SoundGson(ai.elimu.model.v2.gson.content.SoundGson) Gson(com.google.gson.Gson) SoundGson(ai.elimu.model.v2.gson.content.SoundGson) Sound(ai.elimu.model.content.Sound) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with Sound

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

the class JpaToGsonConverter method getLetterSoundCorrespondenceGson.

public static LetterSoundCorrespondenceGson getLetterSoundCorrespondenceGson(LetterSoundCorrespondence letterSoundCorrespondence) {
    if (letterSoundCorrespondence == null) {
        return null;
    } else {
        LetterSoundCorrespondenceGson letterSoundCorrespondenceGson = new LetterSoundCorrespondenceGson();
        // BaseEntity
        letterSoundCorrespondenceGson.setId(letterSoundCorrespondence.getId());
        // LetterSoundCorrespondence
        List<LetterGson> letters = new ArrayList<>();
        for (Letter letter : letterSoundCorrespondence.getLetters()) {
            LetterGson letterGson = getLetterGson(letter);
            letters.add(letterGson);
        }
        letterSoundCorrespondenceGson.setLetters(letters);
        List<SoundGson> sounds = new ArrayList<>();
        for (Sound sound : letterSoundCorrespondence.getSounds()) {
            SoundGson soundGson = getSoundGson(sound);
            sounds.add(soundGson);
        }
        letterSoundCorrespondenceGson.setSounds(sounds);
        letterSoundCorrespondenceGson.setUsageCount(letterSoundCorrespondence.getUsageCount());
        return letterSoundCorrespondenceGson;
    }
}
Also used : Letter(ai.elimu.model.content.Letter) ArrayList(java.util.ArrayList) SoundGson(ai.elimu.model.v2.gson.content.SoundGson) Sound(ai.elimu.model.content.Sound) LetterGson(ai.elimu.model.v2.gson.content.LetterGson) LetterSoundCorrespondenceGson(ai.elimu.model.v2.gson.content.LetterSoundCorrespondenceGson)

Example 5 with Sound

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

the class SoundUsageCountScheduler method execute.

// At 06:30 every day
@Scheduled(cron = "00 30 06 * * *")
public synchronized void execute() {
    logger.info("execute");
    logger.info("Calculating usage count of Sounds");
    // Long = Sound ID
    // Integer = Usage count
    Map<Long, Integer> soundFrequencyMap = new HashMap<>();
    // Summarize the usage count of each Word's Sounds based on the LetterSoundCorrespondence's
    // usage count (see LetterSoundCorrespondenceUsageCountScheduler).
    List<Word> words = wordDao.readAllOrdered();
    logger.info("words.size(): " + words.size());
    for (Word word : words) {
        for (LetterSoundCorrespondence letterSoundCorrespondence : word.getLetterSoundCorrespondences()) {
            for (Sound sound : letterSoundCorrespondence.getSounds()) {
                soundFrequencyMap.put(sound.getId(), soundFrequencyMap.getOrDefault(sound.getId(), 0) + letterSoundCorrespondence.getUsageCount());
            }
        }
    }
    // Update each Sound's usage count in the database
    for (Long soundId : soundFrequencyMap.keySet()) {
        Sound sound = soundDao.read(soundId);
        sound.setUsageCount(soundFrequencyMap.get(soundId));
        soundDao.update(sound);
    }
    logger.info("execute complete");
}
Also used : Word(ai.elimu.model.content.Word) HashMap(java.util.HashMap) Sound(ai.elimu.model.content.Sound) LetterSoundCorrespondence(ai.elimu.model.content.LetterSoundCorrespondence) Scheduled(org.springframework.scheduling.annotation.Scheduled)

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