Java recursive menu trees are converted into pojo objects

  • 2020-04-01 02:09:06
  • OfStack


  package com.cjonline.foundation.authority.pojo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.apache.log4j.Logger;
import com.cjonline.foundation.util.CheckNullEmpty;

public class ModelList implements java.io.Serializable {
    private static final Logger logger = Logger.getLogger(ModelList.class);
    private static final long serialVersionUID = 6384598893693849820L;
    
    private SysModel model;
    
    private List<ModelList> subList = new ArrayList<ModelList>();
    
    private int flag = 0;
    public SysModel getModel() {
        return model;
    }
    public void setModel(SysModel model) {
        this.model = model;
    }
    public List<ModelList> getSubList() {
        return subList;
    }
    public void setSubList(List<ModelList> subList) {
        this.subList = subList;
    }
    public void setFlag(int flag) {
        this.flag = flag;
    }
    public int getFlag() {
        return flag;
    }
    
    @SuppressWarnings("unchecked")
    public ModelList createTree2(List<SysModel> ms) {
        //
        ModelList node = new ModelList();
        ArrayList<SysModel> fu = new ArrayList<SysModel>();//It is used to store the parentId which is empty.
        ArrayList<SysModel> childs = new ArrayList<SysModel>();//Used to store modules that are not systems
        //Separate the system and module menus
        for (Iterator<SysModel> it = ms.iterator(); it.hasNext();) {
            SysModel mode = (SysModel) it.next();
            String parentId = mode.getParentId();
            if (parentId == null || parentId.equals("")) {
                fu.add(mode);
            } else {
                childs.add(mode);
            }
        }
        //Since there are multiple subsystems, the first step is to find out how many subsystems there are
        for (SysModel model : fu) {
            ModelList node1 = new ModelList();
            node1.setFlag(0);
            node1.setModel(model);
            node.subList.add(node1);
            appendChild(node1, childs);
        }
        return node;
    }
    
    public void appendChild(ModelList node, List<SysModel> childs) {
        if (node != null) {
            String systemId = node.getModel().getSystemId();
            String smid = node.getModel().getSysModuleId();
            int flag = node.getFlag();
            if (childs != null && childs.size() > 0) {
                for (SysModel model : childs) {
                    String systemId2 = model.getSystemId();
                    String parentId2 = model.getParentId();
                    if (systemId.equals(systemId2)) {
                        if (parentId2.equals(smid)) {
                            ModelList child = new ModelList();
                            child.setModel(model);
                            child.setFlag(flag + 1);
                            node.getSubList().add(child);
                            appendChild(child, childs);
                        }
                    }
                }
            }
        }
    }
}
  

Related articles: