public static IMenu menuInstance() { try { return fact_._newInstance(); } catch (Exception e) { throw new RuntimeException(e); } }
private void load_() { startTx_(); // to init JPA em if needed IMenu m = menuInstance(); if (0 < m._count()) { if ("yml".equalsIgnoreCase(Play.configuration.getProperty("menu.loadFrom", "db"))) { Logger.info(msg_("Force loading menus from yaml file. clean up menu database ...")); // clean db m._purge(); } else { // reuse the data in database commitTx_(); return; } } commitTx_(); // close the previous Tx if it's started String fileName = Play.configuration.getProperty("menu.yamlFile", "_menu.yml"); VirtualFile yamlFile = virtualFile_(fileName); if (yamlFile == null) { Logger.warn(msg_("Couldn't find menu plugin initial file: %s", fileName)); return; } load_(yamlFile); }
public static String url(IMenu menu) { String s = menu.getUrl(); if (null == s) return null; s = s.trim(); Matcher m = p1_.matcher(s); if (m.find()) { s = m.group(1); } m = p2_.matcher(s); if (m.find()) { s = m.group(1); return Router.reverse(s).url; } else { return s; } }
private static boolean isJPAModel_() { return JPAMenu.class.equals(fact_.getClass()); }
public static void _purge() { fact_._purge(); }
public static List<IMenu> _topLevelMenusByLabel(String label) { if (cacheEnabled_()) return cache_._topLevelMenusByLabel(label); return fact_._topLevelMenusByLabel(label); }
public static List<IMenu> _topLevelMenus() { if (cacheEnabled_()) return cache_._topLevelMenus(); return fact_._topLevelMenus(); }
public static IMenu _find(String id) { if (cacheEnabled_()) return cache_._find(id); return fact_._findById(id); }
public static List<IMenu> getSubMenusByLabel(IMenu parentMenu, String label) { if (cacheEnabled_()) return cache_.getSubMenusByLabel(parentMenu, label); return parentMenu.getSubMenusByLabel(label); }
public static List<IMenu> getSubMenus(IMenu parentMenu) { if (cacheEnabled_()) return cache_.getSubMenus(parentMenu); return parentMenu.getSubMenus(); }
static List<IMenu> allMenus() { return fact_._all(); }
private static void load_(VirtualFile yamlFile) { Logger.info(msg_("loading menu from yaml file: %s", yamlFile.relativePath())); String renderedYaml = TemplateLoader.load(yamlFile).render(); Yaml yaml = new Yaml(); try { startTx_(); Object o = yaml.load(renderedYaml); if (o instanceof LinkedHashMap<?, ?>) { Map<String, IMenu> all = new HashMap<String, IMenu>(); @SuppressWarnings("unchecked") Map<Object, Map<?, ?>> objects = (Map<Object, Map<?, ?>>) o; Map<Object, String> parents = new HashMap<Object, String>(); for (Object key : objects.keySet()) { String id = key.toString().trim(); Matcher matcher = P_K.matcher(id); if (matcher.matches()) { id = matcher.group(2); } Map<?, ?> mm = objects.get(key); String name = (String) mm.get("name"); String url = (String) mm.get("url"); String cssClass = (String) mm.get("cssClass"); String title = (String) mm.get("title"); List<String> labels = (List<String>) mm.get("labels"); String parent = (String) mm.get("parent"); IMenu menu = menuInstance(); menu.setName(name); menu.setUrl(url); menu.setCssClass(cssClass); menu.setTitle(title); if (null != labels) { menu.setLabels(labels); } menu._save(); if (null != parent) parents.put(menu._getId(), parent); all.put(id, menu); } for (IMenu menu : all.values()) { String parent = (String) parents.get(menu._getId()); if (null != parent) { if (all.containsKey(parent)) { menu.setParent(all.get(parent)); menu._save(); } else { throw new RuntimeException( "cannot find parent[" + parent + "] in men yaml file : " + yamlFile.relativePath()); } } } } else { throw new RuntimeException("menu yml format not reconized: " + yamlFile.relativePath()); } commitTx_(); } catch (Exception e) { rollbackTx_(); throw new RuntimeException(e); } }