HostGroupCtrl.java
6.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package com.ui.ctrl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ui.cloud.model.AutoScalingGroup;
import com.ui.common.TagTypeEnum;
import com.ui.contants.HttpUriContants;
import com.ui.http.HttpRestClient;
import com.ui.model.BaseResponse;
import com.ui.model.HostGroup;
import com.ui.model.req.PageRequest;
import com.ui.model.req.TagReq;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import java.util.*;
/**
* Created by yoho on 2016/6/14.
* 查询机器信息
*/
@Controller
@RequestMapping("hostGroup")
public class HostGroupCtrl {
Logger log = LoggerFactory.getLogger(HostGroupCtrl.class);
@Autowired
HttpRestClient httpRestClient;
@RequestMapping("/toHostGroup")
public ModelAndView getHostGroups(Map model) {
model.put("tagTypeList",TagTypeEnum.getAllType());
model.put("tagTypeMapJson", JSON.toJSONString(TagTypeEnum.getAllTypeMap()));
return new ModelAndView("host/hostGroupList",model);
}
@RequestMapping("/getHostGroups")
@ResponseBody
public BaseResponse getHostGroups(TagReq req) {
BaseResponse response=httpRestClient.defaultPost(HttpUriContants.HOST_GROUP_GETALL_WITH_PAGE, req, BaseResponse.class);
return response;
}
@RequestMapping("/getHostGroupsWithTreegrid")
@ResponseBody
public BaseResponse getHostGroupsWithTreegrid(TagReq req) {
//"?tagTypeContent=" + req.getTagTypeContent()
BaseResponse<List<HostGroup>> response = httpRestClient.exchangeForpost(HttpUriContants.HOST_ALL_GROUPS , new ParameterizedTypeReference<BaseResponse<List<HostGroup>>>() {
}, req);
List<HostGroup> allHostGroups= (List<HostGroup>) response.getData();
// ls.add(t1);ls.add(t11);ls.add(t12);ls.add(t111);ls.add(t2);
return new BaseResponse(turnToTree(allHostGroups));
}
@RequestMapping("/getAllTagsGroupByType")
@ResponseBody
public BaseResponse getAllTagsGroupByType() {
try{
BaseResponse response=httpRestClient.defaultPost(HttpUriContants.HOST_ALL_TAGS_GROUP_BY_TYPE, null, BaseResponse.class);
return response;
}catch (Exception e){
log.error("getAllTagsGroupByType error",e);
return null;
}
}
@RequestMapping("/getAllTagsGroupTreegridByType")
@ResponseBody
public BaseResponse getAllTagsGroupTreegridByType() {
try{
BaseResponse<List<HostGroup>> response = httpRestClient.exchangeForpost(HttpUriContants.HOST_ALL_GROUPS , new ParameterizedTypeReference<BaseResponse<List<HostGroup>>>() {
}, new TagReq());
List<HostGroup> allHostGroups= (List<HostGroup>) response.getData();
List<HostGroup> allHostGroupTreeList=turnToTree(allHostGroups);
Map<String,List<HostGroup>> map= new HashMap<String,List<HostGroup>>();
if(allHostGroupTreeList!=null&&allHostGroupTreeList.size()>0){
for(HostGroup tag:allHostGroupTreeList){
if(map.keySet().contains(tag.getTagType())){
map.get(tag.getTagType()).add(tag);
}else{
List<HostGroup> ls=new ArrayList<HostGroup>();
ls.add(tag);
map.put(tag.getTagType(),ls);
}
}
}
return new BaseResponse(map);
}catch (Exception e){
log.error("getAllTagsGroupByType error",e);
return null;
}
}
private List<HostGroup> turnToTree(List<HostGroup> allHostGroups ){
if(allHostGroups==null||allHostGroups.size()<=0){
return new ArrayList<>();
}
Map<Integer,HostGroup> map=new HashMap<>();
for(HostGroup group:allHostGroups){
map.put(group.getId(),group);
}
//选择根节点
List<HostGroup> rootHostGroup=new ArrayList<>();
for(HostGroup group:allHostGroups){
if(!map.keySet().contains(group.getPid())){
rootHostGroup.add(group);
}
}
//递归寻找子节点
resuriveTreeChildren(rootHostGroup,map);
return rootHostGroup;
}
private void resuriveTreeChildren(List<HostGroup> rootHostGroup,Map<Integer,HostGroup> allMap){
if(rootHostGroup!=null&&rootHostGroup.size()>0){
for(HostGroup group:rootHostGroup){
for(Integer key:allMap.keySet()){
HostGroup thisNode= allMap.get(key);
if(thisNode.getPid()==group.getId()){
if(group.getChildren()==null){
List<HostGroup> child=new ArrayList<>();
child.add(thisNode);
group.setChildren(child);
}else{
group.getChildren().add(thisNode);
}
}
}
resuriveTreeChildren(group.getChildren(), allMap);
}
}
}
@RequestMapping("/saveHostGroup")
@ResponseBody
public BaseResponse saveHostGroup(int id,String groupName,String descr,String tagType,int pid) {
Map map = new HashMap<>();
map.put("id",id);
map.put("groupName",groupName);
map.put("descr",descr);
map.put("tagType",tagType);
map.put("pid",pid);
BaseResponse response = httpRestClient.defaultPost(HttpUriContants.HOST_GROUP_SAVE, map, BaseResponse.class);
return response;
}
@RequestMapping("/deleteHostGroup")
@ResponseBody
public BaseResponse deleteHostGroup(int id) {
Map map = new HashMap<>();
map.put("id",id);
BaseResponse response = httpRestClient.defaultGet(HttpUriContants.HOST_GROUP_DELETE, BaseResponse.class, map);
return response;
}
@RequestMapping("/getAllGroups")
@ResponseBody
public BaseResponse getAllGroups() {
try{
TagReq req=new TagReq();
BaseResponse response=httpRestClient.defaultPost(HttpUriContants.HOST_ALL_GROUPS, req, BaseResponse.class);
return response;
}catch (Exception e){
log.error("getAllGroups error",e);
return null;
}
}
}