java的树形通讯录

最近用到企业通讯录,虽然以前也开发过,但是用的是jsf,拼成的树形,及其笨重和难维护。后来就想到直接生成json格式字符串,页面上也好展现。

	// 首先取出每个部门的联系人
			for (int i = 0; i < depList.size(); i++) {
				List<Contacts> list = getContactList(depList.get(i), contactList);
				if (!CollectionUtils.isEmpty(list))
					depList.get(i).setContactList(list);
			}
			//做一次嵌套循环,取出部门的所有子部门
			for (int j = 0; j < depList.size(); j++) {
				for (int i = 0; i < depList.size(); i++) {
					List<Department> list = getSubDepartment(depList.get(i).getDepartmentId(), depList);
					if (!CollectionUtils.isEmpty(list))
						depList.get(i).setDepartmentList(list);
				}
			}
			List<Department> list = new ArrayList<Department>();
			// 这里去除非顶级部门
			for (int i = 0; i < depList.size(); i++) {
				Department d = depList.get(i);
				if (depList.get(i).getParentDepartment() == null || depList.get(i).getParentDepartment().intValue() == 0) {
					list.add(d);
				}
			}
			return list;

 

部门实体:

	private Integer departmentId;//部门id
	private Integer type;//类型
	private String name;//名称
	private Integer parentDepartment;//上级部门
	private Integer companyId;//企业代码
	private List<Department> depList;//子部门列表
	private List<Contacts> contactList;//联系人列表

 

联系人实体:

	private Integer contactsId;//联系人代码
	private Integer companyId;//企业代码
	private String name;//联系人名称
	private String email;//邮件
	private String description;//备注
	private String mobile;//邮件
	private String telephone;//电话
	private Integer departmentId;//所属部门代码

 json生成:

		Gson gson=new Gson();
		return gson.toJson(obj);

 

你可能感兴趣的:(java)