Search in sources :

Example 16 with Application

use of ai.elimu.model.admin.Application 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");
    logger.info("applicationVersion.getVersionCode(): " + applicationVersion.getVersionCode());
    if (applicationVersion.getVersionCode() == null) {
        result.rejectValue("versionCode", "NotNull");
    } else {
        // Verify that the versionCode is higher than previous ones
        List<ApplicationVersion> existingApplicationVersions = applicationVersionDao.readAll(applicationVersion.getApplication());
        for (ApplicationVersion existingApplicationVersion : existingApplicationVersions) {
            if (existingApplicationVersion.getVersionCode() >= applicationVersion.getVersionCode()) {
                result.rejectValue("versionCode", "TooLow");
                break;
            }
        }
    }
    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 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);
        // Refresh REST API cache
        jsonService.refreshApplications(application.getLocale());
        boolean isInfrastructureApp = "ai.elimu.appstore".equals(application.getPackageName()) || "ai.elimu.appstore_custom".equals(application.getPackageName()) || "ai.elimu.analytics".equals(application.getPackageName()) || "ai.elimu.launcher".equals(application.getPackageName()) || "ai.elimu.launcher_custom".equals(application.getPackageName());
        if (isInfrastructureApp) {
            jsonService.refreshApplicationsInAppCollection();
        }
        if (EnvironmentContextLoaderListener.env == Environment.PROD) {
            String text = URLEncoder.encode(contributor.getFirstName() + " just uploaded a new APK version:\n" + "• Language: " + applicationVersion.getApplication().getLocale().getLanguage() + "\n" + "• Package name: \"" + applicationVersion.getApplication().getPackageName() + "\"\n" + "• Version: " + applicationVersion.getVersionCode() + "\n" + "• Start command: " + applicationVersion.getStartCommand());
            String iconUrl = contributor.getImageUrl();
            SlackApiHelper.postMessage(SlackApiHelper.getChannelId(Team.DEVELOPMENT), text, iconUrl, null);
        }
        return "redirect:/admin/application/edit/" + applicationVersion.getApplication().getId();
    }
}
Also used : ApplicationVersion(ai.elimu.model.admin.ApplicationVersion) Contributor(ai.elimu.model.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 17 with Application

use of ai.elimu.model.admin.Application in project webapp by elimu-ai.

the class AppEditController method handleRequest.

@RequestMapping(method = RequestMethod.GET)
public String handleRequest(@PathVariable Long projectId, @PathVariable Long appCategoryId, @PathVariable Long appGroupId, @PathVariable Long applicationId, Model model) {
    logger.info("handleRequest");
    Project project = projectDao.read(projectId);
    model.addAttribute("project", project);
    AppCategory appCategory = appCategoryDao.read(appCategoryId);
    model.addAttribute("appCategory", appCategory);
    AppGroup appGroup = appGroupDao.read(appGroupId);
    model.addAttribute("appGroup", appGroup);
    Application application = applicationDao.read(applicationId);
    model.addAttribute("application", application);
    List<ApplicationVersion> applicationVersions = applicationVersionDao.readAll(application);
    model.addAttribute("applicationVersions", applicationVersions);
    model.addAttribute("applicationStatuses", ApplicationStatus.values());
    return "project/app/edit";
}
Also used : Project(ai.elimu.model.project.Project) ApplicationVersion(ai.elimu.model.admin.ApplicationVersion) AppGroup(ai.elimu.model.project.AppGroup) Application(ai.elimu.model.admin.Application) AppCategory(ai.elimu.model.project.AppCategory) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 18 with Application

use of ai.elimu.model.admin.Application in project webapp by elimu-ai.

the class StringToApplicationConverter method convert.

/**
 * Convert Application id to Application entity
 */
public Application convert(String id) {
    if (StringUtils.isBlank(id)) {
        return null;
    } else {
        Long applicationId = Long.parseLong(id);
        Application application = applicationDao.read(applicationId);
        return application;
    }
}
Also used : Application(ai.elimu.model.admin.Application)

Example 19 with Application

use of ai.elimu.model.admin.Application in project webapp by elimu-ai.

the class JsonService method getApplications.

@Cacheable("applications")
public JSONArray getApplications(Locale locale) {
    logger.info("getApplications");
    Date dateStart = new Date();
    JSONArray applications = new JSONArray();
    for (Application application : applicationDao.readAll(locale)) {
        ApplicationGson applicationGson = JavaToGsonConverter.getApplicationGson(application);
        List<ApplicationVersionGson> applicationVersions = new ArrayList<>();
        logger.info("applicationVersionDao.readAll(" + application.getPackageName() + ") - " + new Date());
        for (ApplicationVersion applicationVersion : applicationVersionDao.readAll(application)) {
            logger.info("applicationVersion: " + applicationVersion.getVersionCode() + " - " + new Date());
            ApplicationVersionGson applicationVersionGson = JavaToGsonConverter.getApplicationVersionGson(applicationVersion);
            applicationVersions.add(applicationVersionGson);
        }
        applicationGson.setApplicationVersions(applicationVersions);
        String json = new Gson().toJson(applicationGson);
        applications.put(new JSONObject(json));
    }
    Date dateEnd = new Date();
    logger.info("getApplications duration: " + (dateEnd.getTime() - dateStart.getTime()) + " ms");
    return applications;
}
Also used : ApplicationVersion(ai.elimu.model.admin.ApplicationVersion) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) ApplicationVersionGson(ai.elimu.model.gson.admin.ApplicationVersionGson) ApplicationGson(ai.elimu.model.gson.admin.ApplicationGson) Gson(com.google.gson.Gson) ApplicationVersionGson(ai.elimu.model.gson.admin.ApplicationVersionGson) Application(ai.elimu.model.admin.Application) Date(java.util.Date) ApplicationGson(ai.elimu.model.gson.admin.ApplicationGson) Cacheable(org.springframework.cache.annotation.Cacheable)

Example 20 with Application

use of ai.elimu.model.admin.Application in project webapp by elimu-ai.

the class ApplicationDaoTest method testCacheable.

@Test
public void testCacheable() {
    Locale locale = Locale.values()[(int) (Math.random() * Locale.values().length)];
    logger.info("locale: " + locale);
    List<Application> applications = applicationDao.readAll(locale);
    assertThat(applications.isEmpty(), is(true));
}
Also used : Locale(ai.elimu.model.enums.Locale) Application(ai.elimu.model.admin.Application) Test(org.junit.Test)

Aggregations

Application (ai.elimu.model.admin.Application)20 ApplicationVersion (ai.elimu.model.admin.ApplicationVersion)11 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)11 Contributor (ai.elimu.model.Contributor)4 ApplicationGson (ai.elimu.model.gson.admin.ApplicationGson)4 AppCategory (ai.elimu.model.project.AppCategory)4 AppGroup (ai.elimu.model.project.AppGroup)4 ArrayList (java.util.ArrayList)4 JSONObject (org.json.JSONObject)4 Test (org.junit.Test)4 ApplicationVersionGson (ai.elimu.model.gson.admin.ApplicationVersionGson)3 Project (ai.elimu.model.project.Project)3 Gson (com.google.gson.Gson)3 IOException (java.io.IOException)3 Date (java.util.Date)2 ByteArrayApkFile (net.dongliu.apk.parser.ByteArrayApkFile)2 ApkMeta (net.dongliu.apk.parser.bean.ApkMeta)2 JSONArray (org.json.JSONArray)2 Cacheable (org.springframework.cache.annotation.Cacheable)2 LetterLearningEvent (ai.elimu.model.analytics.LetterLearningEvent)1