<commit_before>package org.skyve.wildcat.web.faces.beans;

import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

import org.primefaces.model.menu.DefaultMenuItem;
import org.primefaces.model.menu.DefaultMenuModel;
import org.primefaces.model.menu.DefaultSubMenu;
import org.primefaces.model.menu.MenuModel;
import org.primefaces.model.menu.Submenu;
import org.skyve.CORE;
import org.skyve.metadata.MetaDataException;
import org.skyve.metadata.customer.Customer;
import org.skyve.metadata.module.Module;
import org.skyve.metadata.module.menu.MenuGroup;
import org.skyve.metadata.module.menu.MenuItem;
import org.skyve.metadata.router.UxUiSelector;
import org.skyve.util.Util;
import org.skyve.web.WebAction;
import org.skyve.wildcat.metadata.module.menu.CalendarItem;
import org.skyve.wildcat.metadata.module.menu.EditItem;
import org.skyve.wildcat.metadata.module.menu.GridItem;
import org.skyve.wildcat.metadata.module.menu.MapItem;
import org.skyve.wildcat.metadata.module.menu.TreeItem;
import org.skyve.wildcat.metadata.repository.router.Router;
import org.skyve.wildcat.persistence.AbstractPersistence;
import org.skyve.wildcat.web.faces.FacesAction;

@ManagedBean
@ViewScoped
public class Menu extends Harness {
	private static final long serialVersionUID = -7523306130675202901L;

	// The modules menu on the LHS
	private MenuModel menu;
	public MenuModel getMenu() {
		return menu;
	}

	public void preRender() {
		new FacesAction<Void>() {
			@Override
			@SuppressWarnings("synthetic-access")
			public Void callback() throws Exception {
				FacesContext fc = FacesContext.getCurrentInstance();
				if (! fc.isPostback()) {
					AbstractPersistence persistence = AbstractPersistence.get();
					org.skyve.wildcat.metadata.user.User internalUser = (org.skyve.wildcat.metadata.user.User) persistence.getUser();
					Customer customer = internalUser.getCustomer();

					initialise(customer, internalUser);
					
					Router router = CORE.getRepository().getRouter();
					String uxui = ((UxUiSelector) router.getUxuiSelector()).select((HttpServletRequest) fc.getExternalContext().getRequest());

					menu = createMenuModel(getBizModuleParameter(), uxui);
				}
				
				return null;
			}
		}.execute();
	}
	
	private static MenuModel createMenuModel(String bizModule, String uxui) throws MetaDataException {
		MenuModel result = new DefaultMenuModel();

		org.skyve.wildcat.metadata.user.User user = (org.skyve.wildcat.metadata.user.User) CORE.getUser();
		Customer customer = user.getCustomer();

		// determine if the first menu should be open - ie no default
		org.skyve.metadata.module.menu.Menu moduleMenu = user.getModuleMenu(bizModule);
		boolean setFirstModuleOpen = (moduleMenu == null) || moduleMenu.getItems().isEmpty();

		// render each module menu
		List<Module> modules = customer.getModules();
		for (int i = 0, l = modules.size(); i < l; i++) {
			Module thisModule = modules.get(i);
			String thisModuleName = thisModule.getName();

			moduleMenu = user.getModuleMenu(thisModuleName);
			if (moduleMenu.isApplicable(uxui)) {
				Submenu moduleSub = new DefaultSubMenu(thisModule.getTitle());
				result.addElement(moduleSub);

				for (MenuItem item : moduleMenu.getItems()) {
					if (item.isApplicable(uxui)) {
						processItem(item, moduleSub, customer, thisModule);
					}
				}
/*				
				if (setFirstModuleOpen) {
					result.append(",open:");
					result.append(i == 0);
				} 
				else {
					result.append(",open:");
					result.append(thisModuleName.equals(moduleName));
				}
*/
			}
		}
		
		return result;
	}

	private static void processItem(MenuItem item,
										Submenu menu,
										Customer customer,
										Module module)
	throws MetaDataException {
		if (item instanceof MenuGroup) {
			menu.getElements().add(createSubMenu((MenuGroup) item, customer, module));
		}
		else {
			menu.getElements().add(createMenuItem(item, customer, module));
		}
	}

	private static org.primefaces.model.menu.MenuItem createMenuItem(MenuItem item,
																		Customer customer,
																		Module module)
	throws MetaDataException {
		String url = createMenuItemUrl(customer, module, item);
		DefaultMenuItem result = new DefaultMenuItem(item.getName(), null, url);
		result.setAjax(false);
		result.setHref(url);
		result.setTarget("content");
		return result;
	}

	private static Submenu createSubMenu(MenuGroup group,
											Customer customer, 
											Module module)
	throws MetaDataException {
		Submenu result = new DefaultSubMenu(group.getName());
		for (MenuItem subItem : group.getItems()) {
			processItem(subItem, result, customer, module);
		}<commit_msg>Fix uxui applicability in menu items.<commit_after>package org.skyve.wildcat.web.faces.beans;

import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

import org.primefaces.model.menu.DefaultMenuItem;
import org.primefaces.model.menu.DefaultMenuModel;
import org.primefaces.model.menu.DefaultSubMenu;
import org.primefaces.model.menu.MenuModel;
import org.primefaces.model.menu.Submenu;
import org.skyve.CORE;
import org.skyve.metadata.MetaDataException;
import org.skyve.metadata.customer.Customer;
import org.skyve.metadata.module.Module;
import org.skyve.metadata.module.menu.MenuGroup;
import org.skyve.metadata.module.menu.MenuItem;
import org.skyve.metadata.router.UxUiSelector;
import org.skyve.util.Util;
import org.skyve.web.WebAction;
import org.skyve.wildcat.metadata.module.menu.CalendarItem;
import org.skyve.wildcat.metadata.module.menu.EditItem;
import org.skyve.wildcat.metadata.module.menu.GridItem;
import org.skyve.wildcat.metadata.module.menu.MapItem;
import org.skyve.wildcat.metadata.module.menu.TreeItem;
import org.skyve.wildcat.metadata.repository.router.Router;
import org.skyve.wildcat.persistence.AbstractPersistence;
import org.skyve.wildcat.web.faces.FacesAction;

@ManagedBean
@ViewScoped
public class Menu extends Harness {
	private static final long serialVersionUID = -7523306130675202901L;

	// The modules menu on the LHS
	private MenuModel menu;
	public MenuModel getMenu() {
		return menu;
	}

	public void preRender() {
		new FacesAction<Void>() {
			@Override
			@SuppressWarnings("synthetic-access")
			public Void callback() throws Exception {
				FacesContext fc = FacesContext.getCurrentInstance();
				if (! fc.isPostback()) {
					AbstractPersistence persistence = AbstractPersistence.get();
					org.skyve.wildcat.metadata.user.User internalUser = (org.skyve.wildcat.metadata.user.User) persistence.getUser();
					Customer customer = internalUser.getCustomer();

					initialise(customer, internalUser);
					
					Router router = CORE.getRepository().getRouter();
					String uxui = ((UxUiSelector) router.getUxuiSelector()).select((HttpServletRequest) fc.getExternalContext().getRequest());

					menu = createMenuModel(getBizModuleParameter(), uxui);
				}
				
				return null;
			}
		}.execute();
	}
	
	private static MenuModel createMenuModel(String bizModule, String uxui) throws MetaDataException {
		MenuModel result = new DefaultMenuModel();

		org.skyve.wildcat.metadata.user.User user = (org.skyve.wildcat.metadata.user.User) CORE.getUser();
		Customer customer = user.getCustomer();

		// determine if the first menu should be open - ie no default
		org.skyve.metadata.module.menu.Menu moduleMenu = user.getModuleMenu(bizModule);
		boolean setFirstModuleOpen = (moduleMenu == null) || moduleMenu.getItems().isEmpty();

		// render each module menu
		List<Module> modules = customer.getModules();
		for (int i = 0, l = modules.size(); i < l; i++) {
			Module thisModule = modules.get(i);
			String thisModuleName = thisModule.getName();

			moduleMenu = user.getModuleMenu(thisModuleName);
			if (moduleMenu.isApplicable(uxui)) {
				Submenu moduleSub = new DefaultSubMenu(thisModule.getTitle());
				result.addElement(moduleSub);

				for (MenuItem item : moduleMenu.getItems()) {
					processItem(item, moduleSub, customer, thisModule, uxui);
				}
/*				
				if (setFirstModuleOpen) {
					result.append(",open:");
					result.append(i == 0);
				} 
				else {
					result.append(",open:");
					result.append(thisModuleName.equals(moduleName));
				}
*/
			}
		}
		
		return result;
	}

	private static void processItem(MenuItem item,
										Submenu menu,
										Customer customer,
										Module module,
										String uxui)
	throws MetaDataException {
		if (item.isApplicable(uxui)) {
			if (item instanceof MenuGroup) {
				menu.getElements().add(createSubMenu((MenuGroup) item, customer, module, uxui));
			}
			else {
				menu.getElements().add(createMenuItem(item, customer, module));
			}
		}
	}

	private static org.primefaces.model.menu.MenuItem createMenuItem(MenuItem item,
																		Customer customer,
																		Module module)
	throws MetaDataException {
		String url = createMenuItemUrl(customer, module, item);
		DefaultMenuItem result = new DefaultMenuItem(item.getName(), null, url);
		result.setAjax(false);
		result.setHref(url);
		result.setTarget("content");
		return result;
	}

	private static Submenu createSubMenu(MenuGroup group,
											Customer customer, 
											Module module,
											String uxui)
	throws MetaDataException {
		Submenu result = new DefaultSubMenu(group.getName());
		for (MenuItem subItem : group.getItems()) {
			processItem(subItem, result, customer, module, uxui);
		}
{'fraction_non_alphanumeric': 0.11435117683815872, 'fraction_numerical': 0.0043740887315142675, 'mean_word_length': 4.168460710441335, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, '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': '2766848', 'n_tokens_mistral': 3437, 'n_tokens_neox': 2883, 'n_words': 594}