<commit_before>package org.orienteer.core.web;

import com.orientechnologies.orient.core.record.impl.ODocument;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.head.IHeaderResponse;
import org.apache.wicket.markup.head.OnDomReadyHeaderItem;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.ExternalLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.EmptyPanel;
import org.apache.wicket.markup.html.panel.GenericPanel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.request.cycle.RequestCycle;
import org.apache.wicket.util.visit.IVisit;
import org.apache.wicket.util.visit.IVisitor;
import org.orienteer.core.component.FAIcon;
import org.orienteer.core.model.ODocumentNameModel;
import org.orienteer.core.module.PerspectivesModule;

import ru.ydn.wicket.wicketorientdb.model.ODocumentPropertyModel;

import java.util.Collections;
import java.util.List;

/**
 * Panel to display recursive tree menu.
 */
public class RecursiveMenuPanel extends GenericPanel<ODocument> {
	
	private int level = -1;

    @Override
    public void renderHead(IHeaderResponse response) {
		if(level<=1) {
            response.render(OnDomReadyHeaderItem.forScript(
                    "var cur = $(\"#"+getMarkupId()+" li.active\");" +
                    "cur.parents('ul').collapse('show');" +
                    "cur.parents('li').addClass(\"active\");"));
		}
    }

    public RecursiveMenuPanel(String id, IModel<ODocument> item) {
        super(id, item);
        setOutputMarkupId(true);
        add(new ListView<ODocument>("items", new PropertyModel<List<ODocument>>(this, "items")) {

            @Override
            protected void populateItem(ListItem<ODocument> item) {
                IModel<ODocument> itemModel = item.getModel();
                ODocumentPropertyModel<String> urlModel = new ODocumentPropertyModel<String>(itemModel, "url");
                ODocumentPropertyModel<List<ODocument>> subItems = new ODocumentPropertyModel<List<ODocument>>(itemModel, "subItems");
                final boolean hasSubItems = subItems.getObject() != null && !subItems.getObject().isEmpty();
                ExternalLink link = new ExternalLink("link", urlModel)
                        .setContextRelative(true);
                link.add(new FAIcon("icon", new ODocumentPropertyModel<String>(itemModel, "icon")),
                        new Label("name", new ODocumentNameModel(item.getModel())).setRenderBodyOnly(true),
                        new WebMarkupContainer("menuLevelGlyph").setVisibilityAllowed(hasSubItems));
                item.add(link);
                if (isActiveItem(urlModel)) {
                    item.add(new AttributeModifier("class", "active"));
                }
                item.add(new RecursiveMenuPanel("subItems", itemModel)); 
            }
        });
    }
    
    public List<ODocument> getItems() {
    	return getItems(getModelObject());
    }
    
    public List<ODocument> getItems(ODocument doc) {
    	if(doc.getSchemaClass().isSubClassOf(PerspectivesModule.OCLASS_PERSPECTIVE)) {
    		return (List<ODocument>)doc.field("menu");
    	} else if(doc.getSchemaClass().isSubClassOf(PerspectivesModule.OCLASS_ITEM)) {
    		return (List<ODocument>)doc.field("subItems");
    	} else {
    		return null;
    	}
    }
    
    @Override
    protected void onConfigure() {
    	super.onConfigure();
    	List<ODocument> subItems = getItems();
    	setVisible(subItems!=null && !subItems.isEmpty());
    }
    
    @Override
    protected void onComponentTag(ComponentTag tag) {
    	super.onComponentTag(tag);
    	String addClass = level==1?"nav-first-level":
    						(level==2?"nav-second-level":
    							(level==3?"nav-third-level":"nav-"+level+"-level"));<commit_msg>Fix NPE in case if perspective contain deleted menu items<commit_after>package org.orienteer.core.web;

import com.orientechnologies.orient.core.record.impl.ODocument;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.head.IHeaderResponse;
import org.apache.wicket.markup.head.OnDomReadyHeaderItem;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.ExternalLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.EmptyPanel;
import org.apache.wicket.markup.html.panel.GenericPanel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.request.cycle.RequestCycle;
import org.apache.wicket.util.visit.IVisit;
import org.apache.wicket.util.visit.IVisitor;
import org.orienteer.core.component.FAIcon;
import org.orienteer.core.model.ODocumentNameModel;
import org.orienteer.core.module.PerspectivesModule;

import ru.ydn.wicket.wicketorientdb.model.ODocumentPropertyModel;

import java.util.Collections;
import java.util.List;

/**
 * Panel to display recursive tree menu.
 */
public class RecursiveMenuPanel extends GenericPanel<ODocument> {
	
	private int level = -1;

    @Override
    public void renderHead(IHeaderResponse response) {
		if(level<=1) {
            response.render(OnDomReadyHeaderItem.forScript(
                    "var cur = $(\"#"+getMarkupId()+" li.active\");" +
                    "cur.parents('ul').collapse('show');" +
                    "cur.parents('li').addClass(\"active\");"));
		}
    }

    public RecursiveMenuPanel(String id, IModel<ODocument> itemModel) {
        super(id, itemModel);
        setOutputMarkupId(true);
        add(new ListView<ODocument>("items", new PropertyModel<List<ODocument>>(this, "items")) {

            @Override
            protected void populateItem(ListItem<ODocument> item) {
                IModel<ODocument> itemModel = item.getModel();
                ODocumentPropertyModel<String> urlModel = new ODocumentPropertyModel<String>(itemModel, "url");
                ODocumentPropertyModel<List<ODocument>> subItems = new ODocumentPropertyModel<List<ODocument>>(itemModel, "subItems");
                final boolean hasSubItems = subItems.getObject() != null && !subItems.getObject().isEmpty();
                ExternalLink link = new ExternalLink("link", urlModel)
                        .setContextRelative(true);
                link.add(new FAIcon("icon", new ODocumentPropertyModel<String>(itemModel, "icon")),
                        new Label("name", new ODocumentNameModel(item.getModel())).setRenderBodyOnly(true),
                        new WebMarkupContainer("menuLevelGlyph").setVisibilityAllowed(hasSubItems));
                item.add(link);
                if (isActiveItem(urlModel)) {
                    item.add(new AttributeModifier("class", "active"));
                }
                item.add(new RecursiveMenuPanel("subItems", itemModel)); 
            }
        });
    }
    
    public List<ODocument> getItems() {
    	return getItems(getModelObject());
    }
    
    public List<ODocument> getItems(ODocument doc) {
    	List<ODocument> items = null;
    	if(doc.getSchemaClass().isSubClassOf(PerspectivesModule.OCLASS_PERSPECTIVE)) {
    		items = (List<ODocument>)doc.field("menu");
    	} else if(doc.getSchemaClass().isSubClassOf(PerspectivesModule.OCLASS_ITEM)) {
    		items = (List<ODocument>)doc.field("subItems");
    	}
    	if(items!=null) items.remove(null); //Remove deleted records
    	return items;
    }
    
    @Override
    protected void onConfigure() {
    	super.onConfigure();
    	List<ODocument> subItems = getItems();
    	setVisible(subItems!=null && !subItems.isEmpty());
    }
    
    @Override
    protected void onComponentTag(ComponentTag tag) {
    	super.onComponentTag(tag);
    	String addClass = level==1?"nav-first-level":
    						(level==2?"nav-second-level":
    							(level==3?"nav-third-level":"nav-"+level+"-level"));
{'fraction_non_alphanumeric': 0.12810869828945773, 'fraction_numerical': 0.001213150551983501, 'mean_word_length': 3.269290523045054, 'pattern_counts': {'":': 6, '<': 44, '<?xml version=': 0, '>': 42, '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': '6543487', 'n_tokens_mistral': 2513, 'n_tokens_neox': 2436, 'n_words': 373}