package com.paibo.police.eventalarm.dao.impl;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Resource;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import com.paibo.framework.tools.date.DateUtils;
public class TestDao {
public static List<Entity> getResult(List<Entity> src){
List<Entity> parents = new ArrayList<Entity>();
for(Entity ent : src){
if(ent.isCheckGroup()){
Entity result = ent;
result.setChildren(new ArrayList<Entity>());
parents.add(result);
}
}
List<Entity> last = new ArrayList<Entity>();
for(Entity ent : src){
if(!ent.isCheckGroup()) {
last.add(ent);
}
}
buildTree(parents, last);
return parents;
}
private static void buildTree(List<Entity> parents, List<Entity> others){
List<Entity> record = new ArrayList<Entity>();
for(Iterator<Entity> it = parents.iterator(); it.hasNext();){
Entity vi = it.next();
if(vi.getId() != null) {
for(Iterator<Entity> otherIt = others.iterator(); otherIt.hasNext();){
Entity inVi = otherIt.next();
if(vi.getId().equals(inVi.getPid())){
if(null == vi.getChildren()){
vi.setChildren(new ArrayList<Entity>());
}
vi.getChildren().add(inVi);
record.add(inVi);
otherIt.remove();
}
}
}
}
if(others.size() == 0){
return;
}else{
buildTree(record, others);
}
}
public static void main(String[] args) throws Exception {
List<Entity> src = new ArrayList<Entity>();
Entity ent0 = new Entity("0", "全世界", "", true);
Entity ent1 = new Entity("1", "中国", "0" , false);
Entity ent2 = new Entity("11", "上海", "1" , false);
Entity ent3 = new Entity("111", "普陀区", "11", false);
Entity ent6 = new Entity("1111", "天地软件园", "111", false);
Entity ent4 = new Entity("2", "美国", "0" , false);
Entity ent5 = new Entity("21", "华盛顿", "2" , false);
src.add(ent0);
src.add(ent1);
src.add(ent2);
src.add(ent3);
src.add(ent4);
src.add(ent5);
src.add(ent6);
List<Entity> result = getResult(src);
readTree(result,0);
}
private static void readTree(List<Entity> result,int level){
String preStr = "";
for (int i = 0; i < level; i++) {
preStr += "*****";
}
for (Entity entity : result) {
System.out.println(preStr+entity.getGroupName());
if(entity.getChildren() != null){
readTree(entity.getChildren(),level+1);
}
}
}
}
class Entity {
private String id;
private String groupName;
private String pid;
private boolean checkGroup;
private List<Entity> children;
public Entity(String id, String groupName, String pid, boolean checkGroup) {
super();
this.id = id;
this.groupName = groupName;
this.pid = pid;
this.checkGroup = checkGroup;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
public boolean isCheckGroup() {
return checkGroup;
}
public void setCheckGroup(boolean checkGroup) {
this.checkGroup = checkGroup;
}
public List<Entity> getChildren() {
return children;
}
public void setChildren(List<Entity> children) {
this.children = children;
}
}