<commit_before>package com.jukusoft.erp.core.module.base.service.menu;

import com.jukusoft.data.repository.MenuRepository;
import com.jukusoft.erp.lib.database.InjectRepository;
import com.jukusoft.erp.lib.message.ResponseType;
import com.jukusoft.erp.lib.message.request.ApiRequest;
import com.jukusoft.erp.lib.message.response.ApiResponse;
import com.jukusoft.erp.lib.route.Route;
import com.jukusoft.erp.lib.service.AbstractService;
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.eventbus.Message;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;

public class MenuService extends AbstractService {

    @InjectRepository
    protected MenuRepository menuRepository;

    @Route(routes = "/list-menus")
    public void listMenus (Message<ApiRequest> event, ApiRequest req, ApiResponse response, Handler<AsyncResult<ApiResponse>> handler) {
        //first, check if request contains menuID
        if (!req.getData().has("menuID")) {
            response.setStatusCode(ResponseType.BAD_REQUEST);

            //log
            getLogger().warn(req.getMessageID(), "list_menus", "request doesnt contains menuID: " + req.toString());

            //call handler
            handler.handle(Future.succeededFuture(response));

            return;
        }

        //get menuID
        int menuID = req.getData().getInt("menuID");

        //list menus from database
        menuRepository.listMenusByMenuID(menuID, res -> {
            if (!res.succeeded()) {
                response.setStatusCode(ResponseType.INTERNAL_SERVER_ERROR);
                handler.handle(Future.succeededFuture(response));

                getLogger().warn(req.getMessageID(), "list_menus_api", "Couldnt get menu list by repository: " + res.cause().getMessage());

                return;
            }

            //get database rows
            JsonArray rows = res.result();

            //create new menu array, which is sended to client
            JsonArray menuArray = new JsonArray();

            //first, get all entries with parentID = -1
            for (int i = 0; i < rows.size(); i++) {
                //get row
                JsonObject row = rows.getJsonObject(i);

                int parentID = row.getInteger("parentID");

                if (parentID == -1) {
                    //it isnt an sub menu

                    //check login permissions
                    if (!checkLogin(req.getUserID(), row.getInteger("login_required"))) {
                        //login is required and user isnt logged in, so dont show this menu
                        continue;
                    }

                    //check permissions
                    if (!checkPermissions(req.getUserID(), row.getString("permissions"))) {
                        //user doesnt have permissions, so dont show this menu
                        continue;
                    }

                    //show menu
                    JsonObject json = new JsonObject();
                    json.put("title", row.getString("title"));
                    json.put("event", row.getString("event_name"));
                    json.put("shortcut", row.getString("shortcut"));
                    json.put("order", row.getInteger("order"));

                    //get id
                    int id = row.getInteger("id");

                    //add sub menus
                    this.addSubMenusToJson(json, id, req.getUserID(), rows);

                    //add menu to array
                    menuArray.add(json);
                }
            }

            response.setStatusCode(ResponseType.OK);
            response.getData().put("menu", menuArray);
            handler.handle(Future.succeededFuture(response));
        });
    }

    protected void addSubMenusToJson (JsonObject json, int id, long userID, JsonArray rows) {
        JsonArray subMenus = new JsonArray();

        //search for menu entries with parentID = id
        for (int i = 0; i < rows.size(); i++) {
            //get row
            JsonObject row = rows.getJsonObject(i);

            //get parent id
            int parentID = row.getInteger("parentID");

            if (parentID != id) {
                //menu doesnt belongs to this parent menu
                continue;
            }
<commit_msg>changed menu service, added logs for better debugging.<commit_after>package com.jukusoft.erp.core.module.base.service.menu;

import com.jukusoft.data.repository.MenuRepository;
import com.jukusoft.erp.lib.database.InjectRepository;
import com.jukusoft.erp.lib.message.ResponseType;
import com.jukusoft.erp.lib.message.request.ApiRequest;
import com.jukusoft.erp.lib.message.response.ApiResponse;
import com.jukusoft.erp.lib.route.Route;
import com.jukusoft.erp.lib.service.AbstractService;
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.eventbus.Message;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;

public class MenuService extends AbstractService {

    @InjectRepository
    protected MenuRepository menuRepository;

    @Route(routes = "/list-menus")
    public void listMenus (Message<ApiRequest> event, ApiRequest req, ApiResponse response, Handler<AsyncResult<ApiResponse>> handler) {
        //first, check if request contains menuID
        if (!req.getData().has("menuID")) {
            response.setStatusCode(ResponseType.BAD_REQUEST);

            //log
            getLogger().warn(req.getMessageID(), "list_menus", "request doesnt contains menuID: " + req.toString());

            //call handler
            handler.handle(Future.succeededFuture(response));

            return;
        }

        //get menuID
        int menuID = req.getData().getInt("menuID");

        //list menus from database
        menuRepository.listMenusByMenuID(menuID, res -> {
            if (!res.succeeded()) {
                response.setStatusCode(ResponseType.INTERNAL_SERVER_ERROR);
                handler.handle(Future.succeededFuture(response));

                getLogger().warn(req.getMessageID(), "list_menus_api", "Couldnt get menu list by repository: " + res.cause().getMessage());

                return;
            }

            //get database rows
            JsonArray rows = res.result();

            //create new menu array, which is sended to client
            JsonArray menuArray = new JsonArray();

            //first, get all entries with parentID = -1
            for (int i = 0; i < rows.size(); i++) {
                //get row
                JsonObject row = rows.getJsonObject(i);

                int parentID = row.getInteger("parentID");

                if (parentID == -1) {
                    //it isnt an sub menu

                    System.out.println("check menu: " + row.getString("title"));

                    //check login permissions
                    if (!checkLogin(req.getUserID(), row.getInteger("login_required"))) {
                        getLogger().debug(req.getMessageID(), "list_menus_api", "Dont show menu, because user isnt logged in: " + row.getString("title"));

                        //login is required and user isnt logged in, so dont show this menu
                        continue;
                    }

                    //check permissions
                    if (!checkPermissions(req.getUserID(), row.getString("permissions"))) {
                        getLogger().debug(req.getMessageID(), "list_menus_api", "Dont show menu, because user doesnt have permission: " + row.getString("title"));

                        //user doesnt have permissions, so dont show this menu
                        continue;
                    }

                    //show menu
                    JsonObject json = new JsonObject();
                    json.put("title", row.getString("title"));
                    json.put("event", row.getString("event_name"));
                    json.put("shortcut", row.getString("shortcut"));
                    json.put("order", row.getInteger("order"));

                    //get id
                    int id = row.getInteger("id");

                    //add sub menus
                    this.addSubMenusToJson(json, id, req.getUserID(), rows);

                    //add menu to array
                    menuArray.add(json);
                }
            }

            response.setStatusCode(ResponseType.OK);
            response.getData().put("menu", menuArray);
            handler.handle(Future.succeededFuture(response));
        });
    }

    protected void addSubMenusToJson (JsonObject json, int id, long userID, JsonArray rows) {
        JsonArray subMenus = new JsonArray();

        //search for menu entries with parentID = id
        for (int i = 0; i < rows.size(); i++) {
            //get row
            JsonObject row = rows.getJsonObject(i);

            //get parent id
            int parentID = row.getInteger("parentID");

            if (parentID != id) {
                //menu doesnt belongs to this parent menu
                continue;
            }

            System.out.println("check menu: " + row.getString("title"));

{'fraction_non_alphanumeric': 0.11175002712379299, 'fraction_numerical': 0.0008679613757187805, 'mean_word_length': 1.8389282414536494, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, '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': '7395821', 'n_tokens_mistral': 2475, 'n_tokens_neox': 2333, 'n_words': 567}