public GroupNode getUserDataModel(int unitId){
return constrution(unitId);
}
public synchronized GroupNode constrution(int unitId){
if (unitId<0) {
log.info("avalid unitId: "+unitId);
return null;
}
List groups=groupService.getAllUnitGroup(unitId);
List users=userService.getUnitUsers(unitId);
if (groups==null || groups.size()==0) {
log.info("no group in this unit: "+unitId);
return null;
}
if (users==null || users.size()==0) {
log.info("no user in this unit: "+unitId);
return null;
}
GroupNode root=getGroupTree(groups);
root=dealUsers(root,users);
return root;
}
private GroupNode getGroupTree(List groups){
GroupNode root=new GroupNode("G0","用户列表树根");
root.setPos("000");
for (Iterator iter = groups.iterator(); iter.hasNext(); ) {
GroupDto item = (GroupDto)iter.next();
GroupNode gnode=new GroupNode(item);
root.addChild(gnode);
}
if (root.getChildren().size()<=0) {
log.info("no groups in this system!");
return root;
}
for (Iterator iter = groups.iterator(); iter.hasNext(); ) {
GroupDto item = (GroupDto)iter.next();
String selfKey="G"+String.valueOf(item.getId());
String key="G"+String.valueOf(item.getParentId());
GroupNode gnode=(GroupNode)root.getChildren().get(selfKey);
if (item.getParentId()<1) {
//root.addChild(gnode);
continue;
}
GroupNode parentNode=getGroupNode(root,key);
if (parentNode==null) {
log.info("no parent group to finded, id is :"+key);
continue;
}
root.getChildren().remove(selfKey);
parentNode.addChild(gnode);
}
return root;
}
private GroupNode getGroupNode(GroupNode root,String key){
if (root==null || key ==null ){
log.info("root is null or key is null!");
return null;
}
Map groups=root.getChildren();
GroupNode reNode=null;
if (groups.get(key)!=null )
return (GroupNode)groups.get(key);
else {
for (Iterator iter = groups.values().iterator(); iter.hasNext(); ) {
BaseNode bNode = (BaseNode) iter.next();
if (bNode instanceof GroupNode) {
GroupNode item = (GroupNode)bNode;
reNode = getGroupNode(item, key);
if (reNode != null)
return reNode;
}
}
}
return null;
}
private GroupNode dealUsers(GroupNode root,List users){
if (root==null || users==null ){
log.info("no group or user ");
}
for (Iterator iter = users.iterator(); iter.hasNext(); ) {
UserDto item = (UserDto)iter.next();
if (item.getUserType()!=2) continue;
UserNode unode=new UserNode(item);
int groupId=item.getGroupId();
if (groupId<1) {
root.addChild(unode);
continue;
}
String key="G"+String.valueOf(item.getGroupId());
GroupNode gnode=getGroupNode(root,key);
if (gnode==null) {
log.info("no groupnode for this id: "+key);
continue;
}
gnode.addChild(unode);
}
return root;
}