<commit_before>package org.openforis.ceo;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import spark.ModelAndView;
import spark.Request;
import spark.Response;

public class Views {

    private static final String OF_USERS_API_URL = CeoConfig.ofUsersApiUrl;

    private static Map<String, Object> getBaseModel(Request req, String navlink) {
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("root", CeoConfig.documentRoot);
        model.put("navlink", navlink);
        model.put("userid", req.session().attribute("userid"));
        model.put("username", req.session().attribute("username"));
        model.put("role", req.session().attribute("role"));
        if (req.session().attribute("flash_messages") != null) {
            model.put("flash_messages", req.session().attribute("flash_messages"));
            req.session().removeAttribute("flash_messages");
        } else {
            model.put("flash_messages", new String[]{});
        }
        return model;
    }

    private static void authenticateOrRedirect(Request req, Response res) {
        if (req.session().attribute("userid") == null) {
            res.redirect(CeoConfig.documentRoot + "/home");
        }
    }

    public static ModelAndView home(Request req, Response res) {
        return new ModelAndView(getBaseModel(req, "Home"), "home.ftl");
    }

    public static ModelAndView about(Request req, Response res) {
        return new ModelAndView(getBaseModel(req, "About"), "about.ftl");
    }

    public static ModelAndView support(Request req, Response res) {
        return new ModelAndView(getBaseModel(req, "Support"), "support.ftl");
    }

    public static ModelAndView account(Request req, Response res) {
        authenticateOrRedirect(req, res);
        Map<String, Object> model = getBaseModel(req, "Account");
        model.put("account_id", req.params(":id"));
        return new ModelAndView(model, "account.ftl");
    }

    public static ModelAndView institution(Request req, Response res) {
        if (req.params(":id").equals("0")) {
            authenticateOrRedirect(req, res);
        }
        Map<String, Object> model = getBaseModel(req, "Institution");
        model.put("of_users_api_url", OF_USERS_API_URL);
        model.put("institution_id", req.params(":id"));
        return new ModelAndView(model, "institution.ftl");
    }

    public static ModelAndView collection(Request req, Response res) {
        Map<String, Object> model = getBaseModel(req, "Collection");
        model.put("project_id", req.params(":id"));
        return new ModelAndView(model, "collection.ftl");
    }

    public static ModelAndView project(Request req, Response res) {
        authenticateOrRedirect(req, res);
        Map<String, Object> model = getBaseModel(req, "Project");
        model.put("project_id", req.params(":id"));
        if (req.params(":id").equals("0")) {
            model.put("institution_id", req.queryParams("institution"));
        }
        return new ModelAndView(model, "project.ftl");
    }

    public static ModelAndView login(Request req, Response res) {
        //String inputReturnURL = req.queryParams("returnurl");
        Map<String, Object> model = getBaseModel(req, "Login");
        String inputReturnURL = req.queryParams("returnurl");
        String returnURL = "home";
        if(inputReturnURL != null && !inputReturnURL.isEmpty()) {
            returnURL = inputReturnURL;
        }
        String psssQuery = req.queryString();
        if(psssQuery == null || psssQuery.isEmpty()) {
            psssQuery = "empty";
        }
        model.put("returnurl", returnURL );
        model.put("querystring", psssQuery );
        return new ModelAndView(model, "login.ftl");
    }

    public static ModelAndView register(Request req, Response res) {
        return new ModelAndView(getBaseModel(req, "Register"), "register.ftl");
    }

    public static ModelAndView password(Request req, Response res) {
        return new ModelAndView(getBaseModel(req, "Password"), "password.ftl");
    }

    public static ModelAndView passwordReset(Request req, Response res) {
        Map<String, Object> model = getBaseModel(req, "Password-Reset");
        model.put("email", req.queryParams("email"));
        model.put("password_reset_key", req.queryParams("password-reset-key"));
        return new ModelAndView(model, "password-reset.ftl");
    }

    public static ModelAndView geodash(Request req, Response res) {
        Map<String, Object> model = getBaseModel(req, "Geo-Dash");
        model.put("editable", req.queryParams("editable"));
        return new ModelAndView(model, "geo-dash.ftl");<commit_msg>Cleaning up Views.login.<commit_after>package org.openforis.ceo;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import spark.ModelAndView;
import spark.Request;
import spark.Response;

public class Views {

    private static final String OF_USERS_API_URL = CeoConfig.ofUsersApiUrl;

    private static Map<String, Object> getBaseModel(Request req, String navlink) {
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("root", CeoConfig.documentRoot);
        model.put("navlink", navlink);
        model.put("userid", req.session().attribute("userid"));
        model.put("username", req.session().attribute("username"));
        model.put("role", req.session().attribute("role"));
        if (req.session().attribute("flash_messages") != null) {
            model.put("flash_messages", req.session().attribute("flash_messages"));
            req.session().removeAttribute("flash_messages");
        } else {
            model.put("flash_messages", new String[]{});
        }
        return model;
    }

    private static void authenticateOrRedirect(Request req, Response res) {
        if (req.session().attribute("userid") == null) {
            res.redirect(CeoConfig.documentRoot + "/home");
        }
    }

    public static ModelAndView home(Request req, Response res) {
        return new ModelAndView(getBaseModel(req, "Home"), "home.ftl");
    }

    public static ModelAndView about(Request req, Response res) {
        return new ModelAndView(getBaseModel(req, "About"), "about.ftl");
    }

    public static ModelAndView support(Request req, Response res) {
        return new ModelAndView(getBaseModel(req, "Support"), "support.ftl");
    }

    public static ModelAndView account(Request req, Response res) {
        authenticateOrRedirect(req, res);
        Map<String, Object> model = getBaseModel(req, "Account");
        model.put("account_id", req.params(":id"));
        return new ModelAndView(model, "account.ftl");
    }

    public static ModelAndView institution(Request req, Response res) {
        if (req.params(":id").equals("0")) {
            authenticateOrRedirect(req, res);
        }
        Map<String, Object> model = getBaseModel(req, "Institution");
        model.put("of_users_api_url", OF_USERS_API_URL);
        model.put("institution_id", req.params(":id"));
        return new ModelAndView(model, "institution.ftl");
    }

    public static ModelAndView collection(Request req, Response res) {
        Map<String, Object> model = getBaseModel(req, "Collection");
        model.put("project_id", req.params(":id"));
        return new ModelAndView(model, "collection.ftl");
    }

    public static ModelAndView project(Request req, Response res) {
        authenticateOrRedirect(req, res);
        Map<String, Object> model = getBaseModel(req, "Project");
        model.put("project_id", req.params(":id"));
        if (req.params(":id").equals("0")) {
            model.put("institution_id", req.queryParams("institution"));
        }
        return new ModelAndView(model, "project.ftl");
    }

    public static ModelAndView login(Request req, Response res) {
        String inputReturnURL = req.queryParams("returnurl");
        String psssQuery = req.queryString();
        Map<String, Object> model = getBaseModel(req, "Login");
        model.put("returnurl", (inputReturnURL == null || inputReturnURL.isEmpty()) ? "home" : inputReturnURL);
        model.put("querystring", (psssQuery == null || psssQuery.isEmpty()) ? "empty" : psssQuery);
        return new ModelAndView(model, "login.ftl");
    }

    public static ModelAndView register(Request req, Response res) {
        return new ModelAndView(getBaseModel(req, "Register"), "register.ftl");
    }

    public static ModelAndView password(Request req, Response res) {
        return new ModelAndView(getBaseModel(req, "Password"), "password.ftl");
    }

    public static ModelAndView passwordReset(Request req, Response res) {
        Map<String, Object> model = getBaseModel(req, "Password-Reset");
        model.put("email", req.queryParams("email"));
        model.put("password_reset_key", req.queryParams("password-reset-key"));
        return new ModelAndView(model, "password-reset.ftl");
    }

    public static ModelAndView geodash(Request req, Response res) {
        Map<String, Object> model = getBaseModel(req, "Geo-Dash");
        model.put("editable", req.queryParams("editable"));
        return new ModelAndView(model, "geo-dash.ftl");
{'fraction_non_alphanumeric': 0.13790863823338384, 'fraction_numerical': 0.00043299415457891317, 'mean_word_length': 3.317289719626168, 'pattern_counts': {'":': 12, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7475962', 'n_tokens_mistral': 2649, 'n_tokens_neox': 2608, 'n_words': 587}