Search in sources :

Example 26 with Contributor

use of ai.elimu.model.contributor.Contributor in project webapp by elimu-ai.

the class ApplicationVersionCreateController method handleSubmit.

@RequestMapping(method = RequestMethod.POST)
public String handleSubmit(ApplicationVersion applicationVersion, @RequestParam("bytes") MultipartFile multipartFile, BindingResult result, Model model, HttpSession session) {
    logger.info("handleSubmit");
    if (multipartFile.isEmpty()) {
        result.rejectValue("bytes", "NotNull");
    } else {
        try {
            byte[] bytes = multipartFile.getBytes();
            if (applicationVersion.getBytes() != null) {
                String originalFileName = multipartFile.getOriginalFilename();
                logger.info("originalFileName: " + originalFileName);
                if (!originalFileName.endsWith(".apk")) {
                    result.rejectValue("bytes", "typeMismatch");
                }
                String contentType = multipartFile.getContentType();
                logger.info("contentType: " + contentType);
                applicationVersion.setContentType(contentType);
                applicationVersion.setBytes(bytes);
                Integer fileSizeInKb = bytes.length / 1024;
                logger.info("fileSizeInKb: " + fileSizeInKb + " (" + (fileSizeInKb / 1024) + "MB)");
                applicationVersion.setFileSizeInKb(fileSizeInKb);
                String checksumMd5 = ChecksumHelper.calculateMD5(bytes);
                logger.info("checksumMd5: " + checksumMd5);
                applicationVersion.setChecksumMd5(checksumMd5);
                ByteArrayApkFile byteArrayApkFile = new ByteArrayApkFile(bytes);
                ApkMeta apkMeta = byteArrayApkFile.getApkMeta();
                String packageName = apkMeta.getPackageName();
                logger.info("packageName: " + packageName);
                if (!packageName.equals(applicationVersion.getApplication().getPackageName())) {
                    result.reject("packageName.mismatch");
                }
                Integer versionCode = apkMeta.getVersionCode().intValue();
                logger.info("versionCode: " + versionCode);
                // Verify that the versionCode is higher than previous ones
                List<ApplicationVersion> existingApplicationVersions = applicationVersionDao.readAll(applicationVersion.getApplication());
                for (ApplicationVersion existingApplicationVersion : existingApplicationVersions) {
                    if (existingApplicationVersion.getVersionCode() >= versionCode) {
                        result.rejectValue("versionCode", "TooLow");
                        break;
                    }
                }
                applicationVersion.setVersionCode(versionCode);
                String versionName = apkMeta.getVersionName();
                logger.info("versionName: " + versionName);
                applicationVersion.setVersionName(versionName);
                String label = apkMeta.getLabel();
                logger.info("label: " + label);
                applicationVersion.setLabel(label);
                Integer minSdkVersion = Integer.valueOf(apkMeta.getMinSdkVersion());
                logger.info("minSdkVersion: " + minSdkVersion);
                applicationVersion.setMinSdkVersion(minSdkVersion);
                byte[] icon = byteArrayApkFile.getIconFile().getData();
                logger.info("icon.length: " + (icon.length / 1024) + "kB");
                applicationVersion.setIcon(icon);
            } else {
                result.rejectValue("bytes", "NotNull");
            }
        } catch (IOException ex) {
            logger.error(ex);
        }
    }
    if (result.hasErrors()) {
        model.addAttribute("applicationVersion", applicationVersion);
        return "admin/application-version/create";
    } else {
        Contributor contributor = (Contributor) session.getAttribute("contributor");
        applicationVersion.setContributor(contributor);
        applicationVersion.setTimeUploaded(Calendar.getInstance());
        applicationVersionDao.create(applicationVersion);
        // Update the Application entity to reflect the latest changes
        Application application = applicationVersion.getApplication();
        if (application.getApplicationStatus() == ApplicationStatus.MISSING_APK) {
            // If first APK file, change status of application to "ACTIVE"
            application.setApplicationStatus(ApplicationStatus.ACTIVE);
        }
        applicationDao.update(application);
        DiscordHelper.sendChannelMessage("A new Application version (`.apk`) was uploaded:", application.getPackageName(), "Version: `" + applicationVersion.getVersionName() + "`", null, null);
        return "redirect:/admin/application/edit/" + applicationVersion.getApplication().getId() + "#versions";
    }
}
Also used : ApplicationVersion(ai.elimu.model.admin.ApplicationVersion) Contributor(ai.elimu.model.contributor.Contributor) ByteArrayApkFile(net.dongliu.apk.parser.ByteArrayApkFile) IOException(java.io.IOException) ApkMeta(net.dongliu.apk.parser.bean.ApkMeta) Application(ai.elimu.model.admin.Application) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 27 with Contributor

use of ai.elimu.model.contributor.Contributor in project webapp by elimu-ai.

the class MainContentController method handleRequest.

@RequestMapping(method = RequestMethod.GET)
public String handleRequest(HttpServletRequest request, HttpSession session, Principal principal, Model model) {
    logger.info("handleRequest");
    // Check if the Contributor has not yet provided all required details
    Contributor contributor = (Contributor) session.getAttribute("contributor");
    if (StringUtils.isBlank(contributor.getEmail())) {
        return "redirect:/content/contributor/add-email";
    } else if (StringUtils.isBlank(contributor.getFirstName()) || StringUtils.isBlank(contributor.getLastName())) {
        return "redirect:/content/contributor/edit-name";
    } else if (StringUtils.isBlank(contributor.getMotivation()) && (EnvironmentContextLoaderListener.env != Environment.DEV)) {
        return "redirect:/content/contributor/edit-motivation";
    } else {
        // Redirect to originally requested URL
        DefaultSavedRequest defaultSavedRequest = (DefaultSavedRequest) session.getAttribute("SPRING_SECURITY_SAVED_REQUEST");
        logger.info("defaultSavedRequest: " + defaultSavedRequest);
        if (defaultSavedRequest != null) {
            logger.info("Redirecting to " + defaultSavedRequest.getServletPath());
            return "redirect:" + defaultSavedRequest.getServletPath();
        }
    }
    model.addAttribute("letterCount", letterDao.readCount());
    model.addAttribute("soundCount", soundDao.readCount());
    model.addAttribute("letterSoundCorrespondenceCount", letterSoundCorrespondenceDao.readCount());
    model.addAttribute("numberCount", numberDao.readCount());
    model.addAttribute("syllableCount", syllableDao.readCount());
    model.addAttribute("wordCount", wordDao.readCount());
    model.addAttribute("emojiCount", emojiDao.readCount());
    model.addAttribute("storyBookCount", storyBookDao.readCount());
    model.addAttribute("audioCount", audioDao.readCount());
    model.addAttribute("imageCount", imageDao.readCount());
    model.addAttribute("videoCount", videoDao.readCount());
    List<Contributor> contributorsWithStoryBookContributions = contributorDao.readAllWithStoryBookContributions();
    logger.info("contributorsWithStoryBookContributions.size(): " + contributorsWithStoryBookContributions.size());
    model.addAttribute("contributorsWithStoryBookContributions", contributorsWithStoryBookContributions);
    Map<Long, Long> storyBookContributionsCountMap = new HashMap<>();
    for (Contributor contributorWithContributions : contributorsWithStoryBookContributions) {
        storyBookContributionsCountMap.put(contributorWithContributions.getId(), storyBookContributionEventDao.readCount(contributorWithContributions));
    }
    model.addAttribute("storyBookContributionsCountMap", storyBookContributionsCountMap);
    List<Contributor> contributorsWithAudioContributions = contributorDao.readAllWithAudioContributions();
    logger.info("contributorsWithAudioContributions.size(): " + contributorsWithAudioContributions.size());
    model.addAttribute("contributorsWithAudioContributions", contributorsWithAudioContributions);
    Map<Long, Long> audioContributionsCountMap = new HashMap<>();
    for (Contributor contributorWithContributions : contributorsWithAudioContributions) {
        audioContributionsCountMap.put(contributorWithContributions.getId(), audioContributionEventDao.readCount(contributorWithContributions));
    }
    model.addAttribute("audioContributionsCountMap", audioContributionsCountMap);
    List<Contributor> contributorsWithWordContributions = contributorDao.readAllWithWordContributions();
    logger.info("contributorsWithWordContributions.size(): " + contributorsWithWordContributions.size());
    model.addAttribute("contributorsWithWordContributions", contributorsWithWordContributions);
    Map<Long, Long> wordContributionsCountMap = new HashMap<>();
    for (Contributor contributorWithContributions : contributorsWithWordContributions) {
        wordContributionsCountMap.put(contributorWithContributions.getId(), wordContributionEventDao.readCount(contributorWithContributions));
    }
    model.addAttribute("wordContributionsCountMap", wordContributionsCountMap);
    List<Contributor> contributorsWithNumberContributions = contributorDao.readAllWithNumberContributions();
    logger.info("contributorsWithNumberContributions.size(): " + contributorsWithNumberContributions.size());
    model.addAttribute("contributorsWithNumberContributions", contributorsWithNumberContributions);
    Map<Long, Long> numberContributionsCountMap = new HashMap<>();
    for (Contributor contributorWithContributions : contributorsWithNumberContributions) {
        numberContributionsCountMap.put(contributorWithContributions.getId(), numberContributionEventDao.readCount(contributorWithContributions));
    }
    model.addAttribute("numberContributionsCountMap", numberContributionsCountMap);
    return "content/main";
}
Also used : HashMap(java.util.HashMap) Contributor(ai.elimu.model.contributor.Contributor) DefaultSavedRequest(org.springframework.security.web.savedrequest.DefaultSavedRequest) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 28 with Contributor

use of ai.elimu.model.contributor.Contributor in project webapp by elimu-ai.

the class ContributorController method handleRequest.

@RequestMapping(method = RequestMethod.GET)
public String handleRequest(@PathVariable Long contributorId, Model model) {
    logger.info("handleRequest");
    Contributor contributor = contributorDao.read(contributorId);
    model.addAttribute("contributor2", contributor);
    // For contributor-summarized.jsp
    model.addAttribute("storyBookContributionsCount", storyBookContributionEventDao.readCount(contributor));
    model.addAttribute("storyBookPeerReviewsCount", storyBookPeerReviewEventDao.readCount(contributor));
    model.addAttribute("audioContributionsCount", audioContributionEventDao.readCount(contributor));
    model.addAttribute("audioPeerReviewsCount", audioPeerReviewEventDao.readCount(contributor));
    model.addAttribute("wordContributionsCount", wordContributionEventDao.readCount(contributor));
    model.addAttribute("wordPeerReviewsCount", wordPeerReviewEventDao.readCount(contributor));
    model.addAttribute("numberContributionsCount", numberContributionEventDao.readCount(contributor));
    model.addAttribute("numberPeerReviewsCount", numberPeerReviewEventDao.readCount(contributor));
    // For contributor-storybooks.jsp
    List<StoryBookContributionEvent> storyBookContributionEvents = storyBookContributionEventDao.readAll(contributor);
    model.addAttribute("storyBookContributionEvents", storyBookContributionEvents);
    model.addAttribute("storyBookPeerReviewEvents", storyBookPeerReviewEventDao.readAll(contributor));
    Map<Long, List<StoryBookPeerReviewEvent>> storyBookPeerReviewEventsByContributionMap = new HashMap<>();
    for (StoryBookContributionEvent storyBookContributionEvent : storyBookContributionEvents) {
        storyBookPeerReviewEventsByContributionMap.put(storyBookContributionEvent.getId(), storyBookPeerReviewEventDao.readAll(storyBookContributionEvent));
    }
    model.addAttribute("storyBookPeerReviewEventsByContributionMap", storyBookPeerReviewEventsByContributionMap);
    // For contributor-words.jsp
    List<WordContributionEvent> wordContributionEvents = wordContributionEventDao.readAll(contributor);
    model.addAttribute("wordContributionEvents", wordContributionEvents);
    model.addAttribute("wordPeerReviewEvents", wordPeerReviewEventDao.readAll(contributor));
    Map<Long, List<WordPeerReviewEvent>> wordPeerReviewEventsByContributionMap = new HashMap<>();
    for (WordContributionEvent wordContributionEvent : wordContributionEvents) {
        wordPeerReviewEventsByContributionMap.put(wordContributionEvent.getId(), wordPeerReviewEventDao.readAll(wordContributionEvent));
    }
    model.addAttribute("wordPeerReviewEventsByContributionMap", wordPeerReviewEventsByContributionMap);
    // For contributor-numbers.jsp
    List<NumberContributionEvent> numberContributionEvents = numberContributionEventDao.readAll(contributor);
    model.addAttribute("numberContributionEvents", numberContributionEvents);
    model.addAttribute("numberPeerReviewEvents", numberPeerReviewEventDao.readAll(contributor));
    Map<Long, List<NumberPeerReviewEvent>> numberPeerReviewEventsByContributionMap = new HashMap<>();
    for (NumberContributionEvent numberContributionEvent : numberContributionEvents) {
        numberPeerReviewEventsByContributionMap.put(numberContributionEvent.getId(), numberPeerReviewEventDao.readAll(numberContributionEvent));
    }
    model.addAttribute("numberPeerReviewEventsByContributionMap", numberPeerReviewEventsByContributionMap);
    return "content/contributor/contributor";
}
Also used : HashMap(java.util.HashMap) Contributor(ai.elimu.model.contributor.Contributor) StoryBookContributionEvent(ai.elimu.model.contributor.StoryBookContributionEvent) NumberContributionEvent(ai.elimu.model.contributor.NumberContributionEvent) List(java.util.List) WordContributionEvent(ai.elimu.model.contributor.WordContributionEvent) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 29 with Contributor

use of ai.elimu.model.contributor.Contributor in project webapp by elimu-ai.

the class SignOnController method handleOfflineSignOnRequest.

/**
 * To make it possible to sign on without an active Internet connection,
 * enable sign-on with a test user during offline development.
 */
@RequestMapping("/offline")
public String handleOfflineSignOnRequest(HttpServletRequest request) {
    logger.info("handleOfflineSignOnRequest");
    if (EnvironmentContextLoaderListener.env == Environment.DEV) {
        // Fetch the test user that was created in DbContentImportHelper during application launch
        Contributor contributor = contributorDao.read("dev@elimu.ai");
        // Authenticate
        new CustomAuthenticationManager().authenticateUser(contributor);
        // Add Contributor object to session
        request.getSession().setAttribute("contributor", contributor);
        return "redirect:/content";
    } else {
        return null;
    }
}
Also used : Contributor(ai.elimu.model.contributor.Contributor) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 30 with Contributor

use of ai.elimu.model.contributor.Contributor in project webapp by elimu-ai.

the class SignOnControllerGoogle method handleCallback.

@RequestMapping(value = "/sign-on/google/callback", method = RequestMethod.GET)
public String handleCallback(HttpServletRequest request, Model model) {
    logger.info("handleCallback");
    String state = request.getParameter("state");
    logger.debug("state: " + state);
    if (!secretState.equals(state)) {
        return "redirect:/sign-on?error=state_mismatch";
    } else {
        String code = request.getParameter("code");
        logger.debug("code: " + code);
        String responseBody = null;
        logger.info("Trading the Authorization Code for an Access Token...");
        try {
            OAuth2AccessToken accessToken = oAuth20Service.getAccessToken(code);
            logger.debug("accessToken: " + accessToken);
            logger.info("Got the Access Token!");
            // Access the protected resource
            OAuthRequest oAuthRequest = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);
            oAuth20Service.signRequest(accessToken, oAuthRequest);
            Response response = oAuth20Service.execute(oAuthRequest);
            responseBody = response.getBody();
            logger.info("response.getCode(): " + response.getCode());
            logger.info("response.getBody(): " + response.getBody());
        } catch (IOException | InterruptedException | ExecutionException ex) {
            logger.error(ex);
            return "redirect:/sign-on?error=" + ex.getMessage();
        }
        JSONObject jsonObject = new JSONObject(responseBody);
        logger.info("jsonObject: " + jsonObject);
        Contributor contributor = new Contributor();
        contributor.setEmail(jsonObject.getString("email"));
        contributor.setProviderIdGoogle(jsonObject.getString("sub"));
        contributor.setImageUrl(jsonObject.getString("picture"));
        if (jsonObject.has("given_name")) {
            contributor.setFirstName(jsonObject.getString("given_name"));
        }
        if (jsonObject.has("family_name")) {
            contributor.setLastName(jsonObject.getString("family_name"));
        }
        // Look for existing Contributor with matching e-mail address
        Contributor existingContributor = contributorDao.read(contributor.getEmail());
        logger.info("existingContributor: " + existingContributor);
        if (existingContributor == null) {
            // Store new Contributor in database
            contributor.setRegistrationTime(Calendar.getInstance());
            contributor.setRoles(new HashSet<>(Arrays.asList(Role.CONTRIBUTOR)));
            contributorDao.create(contributor);
            logger.info("Contributor " + contributor.getEmail() + " was created at " + request.getServerName());
        } else {
            // Update existing contributor with latest values fetched from provider
            if (StringUtils.isNotBlank(contributor.getProviderIdGoogle())) {
                existingContributor.setProviderIdGoogle(contributor.getProviderIdGoogle());
            }
            if (StringUtils.isNotBlank(contributor.getImageUrl())) {
                existingContributor.setImageUrl(contributor.getImageUrl());
            }
            if (StringUtils.isNotBlank(contributor.getFirstName())) {
                existingContributor.setFirstName(contributor.getFirstName());
            }
            if (StringUtils.isNotBlank(contributor.getLastName())) {
                existingContributor.setLastName(contributor.getLastName());
            }
            contributorDao.update(existingContributor);
            contributor = existingContributor;
        }
        // Authenticate
        new CustomAuthenticationManager().authenticateUser(contributor);
        // Add Contributor object to session
        request.getSession().setAttribute("contributor", contributor);
        return "redirect:/content";
    }
}
Also used : OAuthRequest(com.github.scribejava.core.model.OAuthRequest) Response(com.github.scribejava.core.model.Response) JSONObject(org.json.JSONObject) OAuth2AccessToken(com.github.scribejava.core.model.OAuth2AccessToken) Contributor(ai.elimu.model.contributor.Contributor) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

Contributor (ai.elimu.model.contributor.Contributor)38 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)32 StoryBookContributionEvent (ai.elimu.model.contributor.StoryBookContributionEvent)10 WordContributionEvent (ai.elimu.model.contributor.WordContributionEvent)8 ArrayList (java.util.ArrayList)8 JSONObject (org.json.JSONObject)8 StoryBook (ai.elimu.model.content.StoryBook)7 Word (ai.elimu.model.content.Word)7 Audio (ai.elimu.model.content.multimedia.Audio)6 AudioContributionEvent (ai.elimu.model.contributor.AudioContributionEvent)6 NumberContributionEvent (ai.elimu.model.contributor.NumberContributionEvent)5 HashMap (java.util.HashMap)5 StoryBookParagraph (ai.elimu.model.content.StoryBookParagraph)4 Gson (com.google.gson.Gson)4 LetterSoundCorrespondence (ai.elimu.model.content.LetterSoundCorrespondence)3 AudioPeerReviewEvent (ai.elimu.model.contributor.AudioPeerReviewEvent)3 LetterSoundCorrespondenceContributionEvent (ai.elimu.model.contributor.LetterSoundCorrespondenceContributionEvent)3 IOException (java.io.IOException)3 Test (org.junit.Test)3 Letter (ai.elimu.model.content.Letter)2