本段代碼主要是一個工具。費話不多說,
注:代碼中的一些常量值分別爲:
public static final String NUM_ZERO = "0";
public static final String NUM_ONE = "1";
public static final String NUM_UNONE = "-1";
public static final String SYMB_MH = ":";
上代碼
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;
/**
* 使用說明:將List<Map>轉換爲Tree<List<Map>>
* @author yshlin
*
*/
public class ListMap2TreeUtils {
private String id; //主鍵
private String text; //名稱
private String iconCls; //圖標
private boolean leaf; //是否葉子
private boolean expanded; //是否展開
private int sort; //序號
private boolean checked; //是否選中
private Map<String, Object> attributes;
private List<ListMap2TreeUtils> children; //子節點
public ListMap2TreeUtils() {
super();
}
/*
public ListMap2TreeUtils(String id, String text, String iconCls, boolean leaf,
boolean expanded, Map<String, Object> attributes, List<ListMap2TreeUtils> children) {
super();
this.id = id;
this.text = text;
this.iconCls = iconCls;
this.leaf = leaf;
this.expanded = expanded;
this.children = children;
this.attributes = attributes;
}
*/
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getIconCls() {
return iconCls;
}
public void setIconCls(String iconCls) {
this.iconCls = iconCls;
}
public boolean isLeaf() {
return leaf;
}
public void setLeaf(boolean leaf) {
this.leaf = leaf;
}
public boolean isExpanded() {
return expanded;
}
public void setExpanded(boolean expanded) {
this.expanded = expanded;
}
public List<ListMap2TreeUtils> getChildren() {
return children;
}
public void setChildren(List<ListMap2TreeUtils> children) {
this.children = children;
}
public int getSort() {
return sort;
}
public void setSort(int sort) {
this.sort = sort;
}
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
public Map<String, Object> getAttributes() {
return attributes;
}
public void setAttributes(Map<String, Object> attributes) {
this.attributes = attributes;
}
/**
* 帶屬性的List轉JSON樹
* [id, text, sjid, checked, iconCls, attributes{v1:v2:v3}, othermc...]
* 創建時間
* 創建人 yshlin
* @param m2tList
* @param column
* @param rootName
* @return
*
*/
public static String getTreeJsonStringFull(final List<Map<String, Object>> m2tList,
final String[] column, final String rootName) {
if(column.length < 6){
return "";
}
Map<String, List<ListMap2TreeUtils>> arrayListMap = getArrayListMapFull(m2tList,column,rootName);
return listToString(arrayListMap);
}
/**
* List轉樹的方法
* column = [id, mc, parentid]
* 創建時間
* 創建人 yshlin
*
* @param m2tList
* @param column
* @return
*/
public static String getTreeJsonString(final List<Map<String, Object>> m2tList,
final String[] column, final String rootName) {
if(column.length < 4){
return "";
}
Map<String, List<ListMap2TreeUtils>> arrayListMap = getArrayListMap(m2tList,column,rootName);
return listToString(arrayListMap);
}
/**
* 生成層次形式的JSON
*
* 創建時間
* 創建人 yshlin
* @param arrayListMap
* @return
*
*/
private static String listToString(Map<String, List<ListMap2TreeUtils>> arrayListMap){
for (Map.Entry<String, List<ListMap2TreeUtils>> entry : arrayListMap.entrySet()) {
List<ListMap2TreeUtils> smallTreeList = new ArrayList<ListMap2TreeUtils>();
smallTreeList = entry.getValue();
int nodeListSize = smallTreeList.size();
for (int i = 0; i < nodeListSize; i++) {
String findID = smallTreeList.get(i).getId();
List<ListMap2TreeUtils> findList = arrayListMap.get(findID);
smallTreeList.get(i).setChildren(findList);
}
}
List<ListMap2TreeUtils> rootNodeList = arrayListMap.get(ConstantUtil.NUM_UNONE);
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);//自動爲我排除circle。
JSONArray jsonArray = JSONArray.fromObject(rootNodeList, jsonConfig);
return jsonArray.toString();
}
/**
* List轉map的方法
* column = [id, text, sjid, checked, othermc....]
* 創建時間
* 創建人 yshlin
*
* @param m2tList
* @param column
* @return
*/
private static Map<String, List<ListMap2TreeUtils>> getArrayListMap(final List<Map<String, Object>> m2tList,
final String[] column, final String rootName) {
Map<String, List<ListMap2TreeUtils>> arrayListMap = new HashMap<String, List<ListMap2TreeUtils>>();
List<ListMap2TreeUtils> rootlist = new ArrayList<ListMap2TreeUtils>();
ListMap2TreeUtils root = new ListMap2TreeUtils();
root.setId(ConstantUtil.NUM_ZERO);
root.setText(rootName);
rootlist.add(root);
arrayListMap.put(ConstantUtil.NUM_UNONE, rootlist);
for (int i=m2tList.size()-1;i>=0;i--) {
Map<String, Object> e = m2tList.get(i);
ListMap2TreeUtils e2t = new ListMap2TreeUtils();
e2t.setId(e2t.getString(e, column[0]));
String text = e2t.getString(e, column[1]);
String check = column[3].equals("") ? ConstantUtil.NUM_ZERO : e2t.getString(e, column[3]);
e2t.setChecked(check.equals("") || check.equals(ConstantUtil.NUM_ZERO) ? false : true);
String textTmp = "";
if(column.length > 4){
textTmp = e2t.getOtherText(e, column, 4);
}
e2t.setText(text + (textTmp.equals("") ? "" : "[" + textTmp + "]"));
String fatherId = e2t.getString(e, column[2]);
if (arrayListMap.get(fatherId) == null) {
List<ListMap2TreeUtils> list = new ArrayList<ListMap2TreeUtils>();
list.add(e2t);
arrayListMap.put(fatherId, list);
} else {
List<ListMap2TreeUtils> valueList = arrayListMap.get(fatherId);
valueList.add(e2t);
arrayListMap.put(fatherId, valueList);
}
}
return arrayListMap;
}
/**
* List轉map的方法
* column = [id, mc, parentid]
* 創建時間
* 創建人 yshlin
*
* @param m2tList
* @param column
* @return
*/
private static Map<String, List<ListMap2TreeUtils>> getArrayListMapFull(final List<Map<String, Object>> m2tList,
final String[] column, final String rootName) {
Map<String, List<ListMap2TreeUtils>> arrayListMap = new HashMap<String, List<ListMap2TreeUtils>>();
List<ListMap2TreeUtils> rootlist = new ArrayList<ListMap2TreeUtils>();
ListMap2TreeUtils root = new ListMap2TreeUtils();
root.setId(ConstantUtil.NUM_ZERO);
root.setText(rootName);
rootlist.add(root);
arrayListMap.put(ConstantUtil.NUM_UNONE, rootlist);
for (int i=m2tList.size()-1;i>=0;i--) {
Map<String, Object> e = m2tList.get(i);
ListMap2TreeUtils e2t = new ListMap2TreeUtils();
e2t.setId(e2t.getString(e, column[0]));//id
String text = e2t.getString(e, column[1]);//text
String check = column[3].equals("") ? ConstantUtil.NUM_ZERO : e2t.getString(e, column[3]);//checked
e2t.setChecked(check.equals("") || check.equals(ConstantUtil.NUM_ZERO) ? false : true);
//other text
String textTmp = "";
if(column.length > 6){
textTmp = e2t.getOtherText(e, column, 6);
}
e2t.setText(text + (textTmp.equals("") ? "" : "[" + textTmp + "]"));
e2t.setAttributes(e2t.getColumnAttributes(e, column[5]));
String fatherId = e2t.getString(e, column[2]);
if (arrayListMap.get(fatherId) == null) {
List<ListMap2TreeUtils> list = new ArrayList<ListMap2TreeUtils>();
list.add(e2t);
arrayListMap.put(fatherId, list);
} else {
List<ListMap2TreeUtils> valueList = arrayListMap.get(fatherId);
valueList.add(e2t);
arrayListMap.put(fatherId, valueList);
}
}
return arrayListMap;
}
private Map<String, Object> getColumnAttributes(Map<String, Object> m, String attr){
Map<String, Object> map = new HashMap<String, Object>();
if(PatternUtil.isNull(attr).equals("")){
return map;
}
String[] val = attr.split(ConstantUtil.SYMB_MH);
for(int i=0;i<val.length;i++){
map.put(val[i], getString(m, val[i]));
}
return map;
}
/**
* 附加顯示的信息
*
* 創建時間
* 創建人 yshlin
* @param m
* @param column
* @param start
* @return
*
*/
private String getOtherText(Map<String, Object> m, String[] column, int start){
String textTmp = "";
for(int il=start;il<column.length;il++){
String tmp = getString(m, column[il]);
textTmp += textTmp.equals("") ? tmp : ConstantUtil.SYMB_MH + tmp;
}
return textTmp;
}
private String getString(Map<String, Object> m, String key){
String tmp = String.valueOf(m.get(key));
return PatternUtil.isNull(tmp);
}
}