|
|
package com.monitor.cmdb.controller;
|
|
|
|
|
|
import com.model.MObjectInfo;
|
|
|
import com.monitor.cmdb.common.ConObject;
|
|
|
import com.monitor.cmdb.service.IMObjectInfoService;
|
|
|
import com.monitor.model.page.PageRequest;
|
|
|
import com.monitor.model.page.PageResponse;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
|
|
|
/**
|
|
|
* Created by yoho on 2016/6/15.
|
|
|
*/
|
|
|
@RestController
|
|
|
@RequestMapping(value = "/mobject")
|
|
|
public class MObjectInfoController {
|
|
|
public static final Logger DEBUG = LoggerFactory.getLogger(MObjectInfoController.class);
|
|
|
|
|
|
@Autowired
|
|
|
IMObjectInfoService mobjectService;
|
|
|
|
|
|
|
|
|
@RequestMapping(value = "/query", method = RequestMethod.GET)
|
|
|
public PageResponse queryMObject(@RequestBody String request) {
|
|
|
|
|
|
DEBUG.debug("Query all mobjects...");
|
|
|
|
|
|
List<MObjectInfo> moAllList = null;
|
|
|
|
|
|
moAllList = mobjectService.queryMObjectsInfo();
|
|
|
|
|
|
PageResponse response = null;
|
|
|
|
|
|
if (null != moAllList && StringUtils.isNotBlank(request)) {
|
|
|
|
|
|
response = buildPageResponse(request, moAllList);
|
|
|
}
|
|
|
|
|
|
return response;
|
|
|
}
|
|
|
|
|
|
@RequestMapping(value = "/query/type/{typeId}", method = RequestMethod.GET)
|
|
|
public PageResponse queryMObjectByType(@PathVariable String typeId, @RequestBody String request) {
|
|
|
|
|
|
DEBUG.debug("Query mobjects by typeId: {}", typeId);
|
|
|
|
|
|
List<MObjectInfo> moListByType = null;
|
|
|
|
|
|
if (StringUtils.isNotBlank(typeId)) {
|
|
|
|
|
|
moListByType = mobjectService.queryMObjectsInfoByType(Integer.parseInt(typeId));
|
|
|
}
|
|
|
|
|
|
PageResponse response = null;
|
|
|
|
|
|
if (null != moListByType && StringUtils.isNotBlank(request)) {
|
|
|
|
|
|
response = buildPageResponse(request, moListByType);
|
|
|
}
|
|
|
|
|
|
return response;
|
|
|
}
|
|
|
|
|
|
@RequestMapping(value = "/query/host/{hostId}", method = RequestMethod.GET)
|
|
|
public PageResponse queryMObjectByHost(@PathVariable String hostId, @RequestBody String request) {
|
|
|
|
|
|
DEBUG.debug("Query mobjects by hostId: {}", hostId);
|
|
|
|
|
|
List<MObjectInfo> moListByHost = null;
|
|
|
|
|
|
if (StringUtils.isNotBlank(hostId)) {
|
|
|
|
|
|
moListByHost = mobjectService.queryMObjectsInfoByHost(Integer.parseInt(hostId));
|
|
|
}
|
|
|
|
|
|
PageResponse response = null;
|
|
|
|
|
|
if (null != moListByHost && StringUtils.isNotBlank(request)) {
|
|
|
|
|
|
response = buildPageResponse(request, moListByHost);
|
|
|
}
|
|
|
|
|
|
return response;
|
|
|
}
|
|
|
|
|
|
|
|
|
private PageResponse buildPageResponse(String request, List<MObjectInfo> resourceList) {
|
|
|
|
|
|
PageRequest pageRequest = null;
|
|
|
|
|
|
try {
|
|
|
|
|
|
pageRequest = ConObject.OBJECT_MAPPER.readValue(request, PageRequest.class);
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
DEBUG.error("Failed to parse request: {} ,error: {}", request, e);
|
|
|
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
List<MObjectInfo> selectedList = new ArrayList<>();
|
|
|
|
|
|
int start = (pageRequest.getCurrentPage() - 1) * pageRequest.getPageSize();
|
|
|
|
|
|
int end = (pageRequest.getCurrentPage() * pageRequest.getPageSize()) - 1;
|
|
|
|
|
|
int realCount = end < resourceList.size() ? pageRequest.getPageSize() : resourceList.size() - start;
|
|
|
|
|
|
for (int i = 0; i < realCount; i++) {
|
|
|
|
|
|
selectedList.add(resourceList.get(start + i));
|
|
|
}
|
|
|
|
|
|
PageResponse<MObjectInfo> response = new PageResponse<>();
|
|
|
|
|
|
response.setCurrentPage(pageRequest.getCurrentPage());
|
|
|
|
|
|
response.setTotal(resourceList.size());
|
|
|
|
|
|
response.setPageSize(pageRequest.getCurrentPage());
|
|
|
|
|
|
response.setTotalPage(resourceList.size() / pageRequest.getPageSize() + 1);
|
|
|
|
|
|
response.setRows(selectedList);
|
|
|
|
|
|
return response;
|
|
|
}
|
|
|
|
|
|
|
|
|
@RequestMapping(value = "/add", method = RequestMethod.POST)
|
|
|
public void addMObject(@RequestBody String request) {
|
|
|
|
|
|
DEBUG.debug("Add mObject: {}", request);
|
|
|
|
|
|
MObjectInfo info = null;
|
|
|
|
|
|
try {
|
|
|
|
|
|
info = ConObject.OBJECT_MAPPER.readValue(request, MObjectInfo.class);
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
DEBUG.error("Failed to parse mObject ,error: {}", e);
|
|
|
}
|
|
|
|
|
|
if (null != info) {
|
|
|
|
|
|
mobjectService.addMObjectInfo(info);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@RequestMapping(value = "/delete/{moId}", method = RequestMethod.POST)
|
|
|
public void deleteMObject(@PathVariable String moId) {
|
|
|
|
|
|
DEBUG.debug("Delete mObject by moId: {}", moId);
|
|
|
|
|
|
if (StringUtils.isNotBlank(moId)) {
|
|
|
|
|
|
mobjectService.deleteMObjectInfo(Integer.parseInt(moId));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@RequestMapping(value = "/update", method = RequestMethod.POST)
|
|
|
public void updateMObject(@RequestBody String request) {
|
|
|
|
|
|
DEBUG.debug("Update mObject: {}", request);
|
|
|
|
|
|
MObjectInfo info = null;
|
|
|
|
|
|
try {
|
|
|
|
|
|
info = ConObject.OBJECT_MAPPER.readValue(request, MObjectInfo.class);
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
DEBUG.error("Failed to parse mObject ,error: {}", e);
|
|
|
}
|
|
|
|
|
|
if (null != info) {
|
|
|
|
|
|
mobjectService.updateMObjectInfo(info);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|