Search in sources :

Example 6 with Application

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

the class VersionRestController method read.

@RequestMapping("/read")
public String read(HttpServletRequest request, @RequestParam Locale locale, @RequestParam String applicationId, @RequestParam Integer appVersionCode, @RequestParam String osVersion) {
    logger.info("read");
    logger.info("request.getQueryString(): " + request.getQueryString());
    logger.info("request.getRemoteAddr(): " + request.getRemoteAddr());
    Application appStoreApplication = applicationDao.readByPackageName(locale, "ai.elimu.appstore");
    if (appStoreApplication != null) {
    // TODO: fetch dynamically from Application/ApplicationVersion
    }
    // TODO: fetch version of ContentProvider
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("result", "success");
    jsonObject.put("newestVersion", NEWEST_VERSION_CONTENT_PROVIDER);
    jsonObject.put("minOsVersion", MINIMUM_OS_VERSION);
    logger.info("jsonObject: " + jsonObject);
    return jsonObject.toString();
}
Also used : JSONObject(org.json.JSONObject) Application(ai.elimu.model.admin.Application) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 7 with Application

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

the class JsonService method addInfrastructureApps.

/**
 * As AppCollections in Custom Projects do not include all the Applications from the open source
 * version, some apps are required to form the basic infrastructure.
 */
private void addInfrastructureApps(JSONArray applications) {
    logger.info("addInfrastructureApps");
    // Appstore
    Application applicationAppstore = applicationDao.readByPackageName(Locale.EN, "ai.elimu.appstore_custom");
    if (applicationAppstore != null) {
        ApplicationGson applicationGson = JavaToGsonConverter.getApplicationGson(applicationAppstore);
        // Fetch the Application's ApplicationVersions
        List<ApplicationVersionGson> applicationVersions = new ArrayList<>();
        for (ApplicationVersion applicationVersion : applicationVersionDao.readAll(applicationAppstore)) {
            ApplicationVersionGson applicationVersionGson = JavaToGsonConverter.getApplicationVersionGson(applicationVersion);
            applicationVersions.add(applicationVersionGson);
        }
        applicationGson.setApplicationVersions(applicationVersions);
        String json = new Gson().toJson(applicationGson);
        applications.put(new JSONObject(json));
    }
    // Analytics
    Application applicationAnalytics = applicationDao.readByPackageName(Locale.EN, "ai.elimu.analytics");
    if (applicationAnalytics != null) {
        ApplicationGson applicationGson = JavaToGsonConverter.getApplicationGson(applicationAnalytics);
        // Fetch the Application's ApplicationVersions
        List<ApplicationVersionGson> applicationVersions = new ArrayList<>();
        for (ApplicationVersion applicationVersion : applicationVersionDao.readAll(applicationAnalytics)) {
            ApplicationVersionGson applicationVersionGson = JavaToGsonConverter.getApplicationVersionGson(applicationVersion);
            applicationVersions.add(applicationVersionGson);
        }
        applicationGson.setApplicationVersions(applicationVersions);
        String json = new Gson().toJson(applicationGson);
        applications.put(new JSONObject(json));
    }
    // Custom Launcher
    Application applicationLauncher = applicationDao.readByPackageName(Locale.EN, "ai.elimu.launcher_custom");
    if (applicationLauncher != null) {
        ApplicationGson applicationGson = JavaToGsonConverter.getApplicationGson(applicationLauncher);
        // Fetch the Application's ApplicationVersions
        List<ApplicationVersionGson> applicationVersions = new ArrayList<>();
        for (ApplicationVersion applicationVersion : applicationVersionDao.readAll(applicationLauncher)) {
            ApplicationVersionGson applicationVersionGson = JavaToGsonConverter.getApplicationVersionGson(applicationVersion);
            applicationVersions.add(applicationVersionGson);
        }
        applicationGson.setApplicationVersions(applicationVersions);
        String json = new Gson().toJson(applicationGson);
        applications.put(new JSONObject(json));
    }
}
Also used : ApplicationVersion(ai.elimu.model.admin.ApplicationVersion) JSONObject(org.json.JSONObject) 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) ApplicationGson(ai.elimu.model.gson.admin.ApplicationGson)

Example 8 with Application

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

the class JsonService method getApplications.

/**
 * Returns app collection for custom project.
 */
@Cacheable(value = "applicationsInAppCollection", key = "#appCollection.id")
public JSONArray getApplications(AppCollection appCollection) {
    logger.info("getApplications_appCollection");
    Date dateStart = new Date();
    JSONArray applications = new JSONArray();
    addInfrastructureApps(applications);
    for (AppCategory appCategory : appCollection.getAppCategories()) {
        for (AppGroup appGroup : appCategory.getAppGroups()) {
            for (Application application : appGroup.getApplications()) {
                ApplicationGson applicationGson = JavaToGsonConverter.getApplicationGson(application);
                // Fetch the Application's ApplicationVersions
                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("getApplicationVersions duration: " + (dateEnd.getTime() - dateStart.getTime()) + " ms");
    return applications;
}
Also used : ApplicationVersion(ai.elimu.model.admin.ApplicationVersion) 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) Date(java.util.Date) ApplicationGson(ai.elimu.model.gson.admin.ApplicationGson) JSONObject(org.json.JSONObject) AppGroup(ai.elimu.model.project.AppGroup) Application(ai.elimu.model.admin.Application) AppCategory(ai.elimu.model.project.AppCategory) Cacheable(org.springframework.cache.annotation.Cacheable)

Example 9 with Application

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

the class AppCreateController method handleRequest.

@RequestMapping(method = RequestMethod.GET)
public String handleRequest(@PathVariable Long projectId, @PathVariable Long appCategoryId, @PathVariable Long appGroupId, @RequestParam(required = false) 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);
    ApplicationVersion applicationVersion = new ApplicationVersion();
    logger.info("applicationId: " + applicationId);
    if (applicationId != null) {
        Application application = applicationDao.read(applicationId);
        applicationVersion.setApplication(application);
    }
    model.addAttribute("applicationVersion", applicationVersion);
    return "project/app/create";
}
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 10 with Application

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

the class AppCreateController method handleSubmit.

@RequestMapping(method = RequestMethod.POST)
public String handleSubmit(ApplicationVersion applicationVersion, @RequestParam("bytes") MultipartFile multipartFile, BindingResult result, Model model, HttpSession session, @PathVariable Long appGroupId, @PathVariable Long projectId, @PathVariable Long appCategoryId) {
    logger.info("handleSubmit");
    Project project = projectDao.read(projectId);
    AppCategory appCategory = appCategoryDao.read(appCategoryId);
    AppGroup appGroup = appGroupDao.read(appGroupId);
    boolean isUpdateOfExistingApplication = applicationVersion.getApplication() != null;
    logger.info("isUpdateOfExistingApplication: " + isUpdateOfExistingApplication);
    Contributor contributor = (Contributor) session.getAttribute("contributor");
    String packageName = null;
    if (multipartFile.isEmpty()) {
        result.rejectValue("bytes", "NotNull");
    } else {
        try {
            byte[] bytes = multipartFile.getBytes();
            Integer fileSizeInKb = bytes.length / 1024;
            logger.info("fileSizeInKb: " + fileSizeInKb + " (" + (fileSizeInKb / 1024) + "MB)");
            String contentType = multipartFile.getContentType();
            logger.info("contentType: " + contentType);
            // Auto-detect applicationId, versionCode, etc.
            ByteArrayApkFile byteArrayApkFile = new ByteArrayApkFile(bytes);
            ApkMeta apkMeta = byteArrayApkFile.getApkMeta();
            packageName = apkMeta.getPackageName();
            logger.info("packageName: " + packageName);
            String label = apkMeta.getLabel();
            logger.info("label: " + label);
            byte[] icon = byteArrayApkFile.getIconFile().getData();
            logger.info("icon.length: " + (icon.length / 1024) + "kB");
            Integer versionCode = apkMeta.getVersionCode().intValue();
            logger.info("versionCode: " + versionCode);
            String versionName = apkMeta.getVersionName();
            logger.info("versionName: " + versionName);
            Integer minSdkVersion = Integer.valueOf(apkMeta.getMinSdkVersion());
            logger.info("minSdkVersion: " + minSdkVersion);
            // Check if Application already exists in the same AppCategory
            // TODO
            applicationVersion.setBytes(bytes);
            applicationVersion.setFileSizeInKb(fileSizeInKb);
            applicationVersion.setChecksumMd5(ChecksumHelper.calculateMD5(bytes));
            applicationVersion.setContentType(contentType);
            applicationVersion.setVersionCode(versionCode);
            applicationVersion.setVersionName(versionName);
            applicationVersion.setLabel(label);
            applicationVersion.setMinSdkVersion(minSdkVersion);
            applicationVersion.setIcon(icon);
            applicationVersion.setTimeUploaded(Calendar.getInstance());
            applicationVersion.setContributor(contributor);
            if (isUpdateOfExistingApplication) {
                // Verify that the packageName of the APK update matches that of the Application
                if (!applicationVersion.getApplication().getPackageName().equals(packageName)) {
                    result.rejectValue("application", "NonUnique");
                }
            }
            if (!isUpdateOfExistingApplication) {
                // Verify that an Application with identical packageName has not already been uploaded withing the same Project
                Application existingApplication = applicationDao.readByPackageName(project, packageName);
                if (existingApplication != null) {
                    result.rejectValue("application", "NonUnique", new String[] { "application" }, null);
                }
            }
            if (isUpdateOfExistingApplication) {
                // 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;
                    }
                }
            }
        } catch (IOException ex) {
            logger.error(ex);
        }
    }
    if (result.hasErrors()) {
        model.addAttribute("project", project);
        model.addAttribute("appCategory", appCategory);
        model.addAttribute("appGroup", appGroup);
        return "project/app/create";
    } else {
        Application application = applicationVersion.getApplication();
        logger.info("application: " + application);
        if (application == null) {
            // First-time upload for packageName
            // Create new Application
            application = new Application();
            // TODO: Add Locale to each Project?
            application.setLocale(contributor.getLocale());
            application.setPackageName(packageName);
            application.setApplicationStatus(ApplicationStatus.MISSING_APK);
            application.setContributor(contributor);
            application.setProject(project);
            applicationDao.create(application);
            appGroup.getApplications().add(application);
            appGroupDao.update(appGroup);
            applicationVersion.setApplication(application);
            applicationVersionDao.create(applicationVersion);
            application.setApplicationStatus(ApplicationStatus.ACTIVE);
            applicationDao.update(application);
        } else {
            // Update of existing packageName previously uploaded
            // Create new ApplicationVersion for the existing Application
            applicationVersionDao.create(applicationVersion);
        }
        // Refresh REST API cache
        // jsonService.refreshApplicationsInAppCollection(appCollection);
        jsonService.refreshApplicationsInAppCollection();
        if (EnvironmentContextLoaderListener.env == Environment.PROD) {
            String applicationDescription = !isUpdateOfExistingApplication ? "Application" : "APK version";
            String text = URLEncoder.encode(contributor.getFirstName() + " just uploaded a new " + applicationDescription + ":\n" + "• Project: \"" + project.getName() + "\"\n" + "• App Category: \"" + appCategory.getName() + "\"\n" + "• Package name: \"" + application.getPackageName() + "\"\n" + "• Version code: " + applicationVersion.getVersionCode() + "\n" + "See ") + "http://elimu.ai/project/" + project.getId() + "/app-category/" + appCategory.getId() + "/app-group/" + appGroup.getId() + "/app/" + application.getId() + "/edit";
            SlackApiHelper.postMessage("G6UR7UH2S", text, null, null);
        }
        if (!isUpdateOfExistingApplication) {
            return "redirect:/project/{projectId}/app-category/{appCategoryId}/app-group/{appGroupId}/app/list#" + application.getId();
        } else {
            return "redirect:/project/{projectId}/app-category/{appCategoryId}/app-group/{appGroupId}/app/" + application.getId() + "/edit";
        }
    }
}
Also used : ApplicationVersion(ai.elimu.model.admin.ApplicationVersion) Contributor(ai.elimu.model.Contributor) IOException(java.io.IOException) Project(ai.elimu.model.project.Project) ByteArrayApkFile(net.dongliu.apk.parser.ByteArrayApkFile) AppGroup(ai.elimu.model.project.AppGroup) ApkMeta(net.dongliu.apk.parser.bean.ApkMeta) Application(ai.elimu.model.admin.Application) AppCategory(ai.elimu.model.project.AppCategory) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

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