Authored by jack.xue

1.add controller

2.fix db column name
... ... @@ -31,8 +31,18 @@
<groupId>monitor-service</groupId>
<artifactId>monitor-service-model</artifactId>
</dependency>
<!--项目内部依赖-->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
</dependency>
</dependencies>
... ...
package com.monitor.cmdb.common;
import org.codehaus.jackson.map.ObjectMapper;
/**
* Created by yoho on 2016/6/15.
*/
public interface ConObject {
ObjectMapper OBJECT_MAPPER = new ObjectMapper();
}
... ...
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);
}
}
}
... ...
package com.monitor.cmdb.controller;
import com.model.TypeInfo;
import com.monitor.cmdb.common.ConObject;
import com.monitor.cmdb.service.ITypeInfoService;
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.List;
/**
* Created by yoho on 2016/6/15.
*/
@RestController
@RequestMapping(value = "/type")
public class TypeInfoController {
public static final Logger DEBUG = LoggerFactory.getLogger(TypeInfoController.class);
@Autowired
ITypeInfoService typeInfoService;
@RequestMapping(value = "/query/{typeId}", method = RequestMethod.GET)
public String queryTypeInfo(@PathVariable String typeId) {
DEBUG.debug("Query typeInfo by typeId: {}", typeId);
TypeInfo typeInfo = null;
String info = null;
if (StringUtils.isNotBlank(typeId)) {
typeInfo = typeInfoService.queryTypeInfo(Integer.parseInt(typeId));
}
if (null != typeInfo) {
try {
info = ConObject.OBJECT_MAPPER.writeValueAsString(typeInfo);
} catch (IOException e) {
DEBUG.error("Failed to parse typeInfo, error: {}", e);
}
}
return info;
}
@RequestMapping(value = "/query/child/{typeId}", method = RequestMethod.GET)
public String queryChildTypeInfo(@PathVariable String typeId) {
DEBUG.debug("Query children typeInfo by typeId: {}", typeId);
List<TypeInfo> childsTypeInfo = null;
String info = null;
if (StringUtils.isNotBlank(typeId)) {
childsTypeInfo = typeInfoService.queryChildTypesInfo(Integer.parseInt(typeId));
}
if (null != childsTypeInfo) {
try {
info = ConObject.OBJECT_MAPPER.writeValueAsString(childsTypeInfo);
} catch (IOException e) {
DEBUG.error("Failed to parse typeInfo, error: {}", e);
}
}
return info;
}
@RequestMapping(value = "/add", method = RequestMethod.POST)
public void addTypeInfo(@RequestBody String request) {
DEBUG.debug("Add typeInfo: {}", request);
TypeInfo info = null;
try {
info = ConObject.OBJECT_MAPPER.readValue(request, TypeInfo.class);
} catch (IOException e) {
DEBUG.error("Failed to parse typeInfo ,error: {}", e);
}
if (null != info) {
typeInfoService.addTypeInfo(info);
}
}
@RequestMapping(value = "/delete/{typeId}", method = RequestMethod.POST)
public void deleteMObject(@PathVariable String typeId) {
DEBUG.debug("Delete typeInfo by moId: {}", typeId);
if (StringUtils.isNotBlank(typeId)) {
typeInfoService.deleteTypeInfo(Integer.parseInt(typeId));
}
}
@RequestMapping(value = "/update", method = RequestMethod.POST)
public void updateMObject(@RequestBody String request) {
DEBUG.debug("Update typeInfo: {}", request);
TypeInfo info = null;
try {
info = ConObject.OBJECT_MAPPER.readValue(request, TypeInfo.class);
} catch (IOException e) {
DEBUG.error("Failed to parse typeInfo ,error: {}", e);
}
if (null != info) {
typeInfoService.updateTypeInfo(info);
}
}
}
... ...
... ... @@ -14,7 +14,7 @@ public class TypeInfo implements Serializable{
private String typeName;
private int typeLevel;
private int typeIsLeaf;
private int typeParentId;
... ...
... ... @@ -5,16 +5,16 @@
<cache></cache>
<resultMap id="mobjectInfoMapper" type="com.model.MObjectInfo">
<id property="moId" column="mo_id"></id>
<id property="moName" column="mo_name"></id>
<id property="moHostId" column="mo_hostId"></id>
<id property="moTypeId" column="mo_typeId"></id>
<id property="moTags" column="mo_tags"></id>
<id property="moUrl" column="mo_url"></id>
<id property="moId" column="id"></id>
<id property="moName" column="alias"></id>
<id property="moHostId" column="host_id"></id>
<id property="moTypeId" column="type_id"></id>
<id property="moTags" column="tags"></id>
<id property="moUrl" column="url"></id>
</resultMap>
<select id="getAllTypesInfo" resultType="com.model.MObjectInfo" resultMap="mobjectInfoMapper" useCache="true">
SELECT * FROM mobject_info ORDER BY mo_id asc
SELECT * FROM mobject_info ORDER BY id asc
</select>
<!-- <select id="getTypeMosInfo" parameterType="int" resultType="com.model.MObjectInfo" resultMap="mobjectInfoMapper">
... ... @@ -25,16 +25,16 @@
SELECT * FROM mobject_info WHERE mo_hostId = #{moHostId}
</select>-->
<insert id="insertMoInfo" useGeneratedKeys="true" keyProperty="mo_id" parameterType="com.model.MObjectInfo">
INSERT INTO mobject_info(mo_name,mo_hostId,mo_typeId,mo_tags,m_url) VALUES (#{moName},#{moHostId},#{moTypeId},#{moTags},#{moUrl})
<insert id="insertMoInfo" useGeneratedKeys="true" keyProperty="id" parameterType="com.model.MObjectInfo">
INSERT INTO mobject_info(alias,host_id,type_id,tags,url) VALUES (#{moName},#{moHostId},#{moTypeId},#{moTags},#{moUrl})
</insert>
<delete id="deleteMoInfo" parameterType="int">
DELETE FROM mobject_info WHERE mo_id = #{moId}
DELETE FROM mobject_info WHERE id = #{moId}
</delete>
<update id="updateMoInfo" parameterType="com.model.MObjectInfo">
UPDATE mobject_info SET mo_name=#{moName},mo_hostId=#{moHostId},mo_typeId=#{moTypeId},mo_tags=#{moTags},mo_url=#{moUrl} WHERE mo_id = #{moId}
UPDATE mobject_info SET alias=#{moName},host_id=#{moHostId},type_id=#{moTypeId},tags=#{moTags},url=#{moUrl} WHERE id = #{moId}
</update>
... ...
... ... @@ -6,14 +6,14 @@
<cache></cache>
<resultMap id="typeInfoMap" type="com.model.TypeInfo">
<id property="typeId" column="type_id"></id>
<id property="typeName" column="type_name"></id>
<id property="typeLevel" column="type_level"></id>
<id property="typeParentId" column="type_parentId"></id>
<id property="typeId" column="id"></id>
<id property="typeName" column="alias"></id>
<id property="typeIsLeaf" column="isLeaf"></id>
<id property="typeParentId" column="parent_id"></id>
</resultMap>
<select id="getAllTypesInfo" resultType="com.model.TypeInfo" resultMap="typeInfoMap" useCache="true">
SELECT * FROM type_info ORDER BY type_level asc
SELECT * FROM type_info ORDER BY id asc
</select>
<!-- <select id="getChildTypesInfo" parameterType="int" resultType="com.model.TypeInfo" resultMap="typeInfoMap">
... ... @@ -25,15 +25,15 @@
</select>-->
<insert id="insertTypeInfo" useGeneratedKeys="true" keyProperty="type_id" parameterType="com.model.TypeInfo">
INSERT INTO type_info(type_name,type_level,type_parent_id) VALUES (#{typeName},#{typeLevel},#{typeParentId})
INSERT INTO type_info(alias,isLeaf,parent_id) VALUES (#{typeName},#{typeIsLeaf},#{typeParentId})
</insert>
<delete id="deleteTypeInfo" parameterType="int">
DELETE FROM type_info WHERE type_id = #{typeId}
DELETE FROM type_info WHERE id = #{typeId}
</delete>
<update id="updateTypeInfo" parameterType="com.model.TypeInfo">
UPDATE type_info SET type_name=#{typeName},type_level=#{typeLevel},type_parent_id=#{typeParentId} WHERE type_id=#{typeId}
UPDATE type_info SET alias=#{typeName},isLeaf=#{typeIsLeaf},parent_id=#{typeParentId} WHERE id=#{typeId}
</update>
</mapper>
\ No newline at end of file
... ...
... ... @@ -24,6 +24,7 @@
<org.yaml-version>1.9</org.yaml-version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<lombok-version>1.16.6</lombok-version>
<jackson-version>1.9.13</jackson-version>
</properties>
<dependencyManagement>
... ... @@ -123,6 +124,16 @@
<artifactId>lombok</artifactId>
<version>${lombok-version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>${jackson-version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>${jackson-version}</version>
</dependency>
</dependencies>
</dependencyManagement>
... ...