Authored by jack.xue

1.add controller

2.fix db column name
@@ -31,8 +31,18 @@ @@ -31,8 +31,18 @@
31 <groupId>monitor-service</groupId> 31 <groupId>monitor-service</groupId>
32 <artifactId>monitor-service-model</artifactId> 32 <artifactId>monitor-service-model</artifactId>
33 </dependency> 33 </dependency>
  34 +
  35 +
34 <!--项目内部依赖--> 36 <!--项目内部依赖-->
  37 + <dependency>
  38 + <groupId>org.codehaus.jackson</groupId>
  39 + <artifactId>jackson-mapper-asl</artifactId>
  40 + </dependency>
35 41
  42 + <dependency>
  43 + <groupId>org.codehaus.jackson</groupId>
  44 + <artifactId>jackson-core-asl</artifactId>
  45 + </dependency>
36 </dependencies> 46 </dependencies>
37 47
38 48
  1 +package com.monitor.cmdb.common;
  2 +
  3 +import org.codehaus.jackson.map.ObjectMapper;
  4 +
  5 +/**
  6 + * Created by yoho on 2016/6/15.
  7 + */
  8 +public interface ConObject {
  9 +
  10 + ObjectMapper OBJECT_MAPPER = new ObjectMapper();
  11 +}
  1 +package com.monitor.cmdb.controller;
  2 +
  3 +import com.model.MObjectInfo;
  4 +import com.monitor.cmdb.common.ConObject;
  5 +import com.monitor.cmdb.service.IMObjectInfoService;
  6 +import com.monitor.model.page.PageRequest;
  7 +import com.monitor.model.page.PageResponse;
  8 +import org.apache.commons.lang.StringUtils;
  9 +import org.slf4j.Logger;
  10 +import org.slf4j.LoggerFactory;
  11 +import org.springframework.beans.factory.annotation.Autowired;
  12 +import org.springframework.web.bind.annotation.*;
  13 +
  14 +import java.io.IOException;
  15 +import java.util.ArrayList;
  16 +import java.util.List;
  17 +
  18 +/**
  19 + * Created by yoho on 2016/6/15.
  20 + */
  21 +@RestController
  22 +@RequestMapping(value = "/mobject")
  23 +public class MObjectInfoController {
  24 + public static final Logger DEBUG = LoggerFactory.getLogger(MObjectInfoController.class);
  25 +
  26 + @Autowired
  27 + IMObjectInfoService mobjectService;
  28 +
  29 +
  30 + @RequestMapping(value = "/query", method = RequestMethod.GET)
  31 + public PageResponse queryMObject(@RequestBody String request) {
  32 +
  33 + DEBUG.debug("Query all mobjects...");
  34 +
  35 + List<MObjectInfo> moAllList = null;
  36 +
  37 + moAllList = mobjectService.queryMObjectsInfo();
  38 +
  39 + PageResponse response = null;
  40 +
  41 + if (null != moAllList && StringUtils.isNotBlank(request)) {
  42 +
  43 + response = buildPageResponse(request, moAllList);
  44 + }
  45 +
  46 + return response;
  47 + }
  48 +
  49 + @RequestMapping(value = "/query/type/{typeId}", method = RequestMethod.GET)
  50 + public PageResponse queryMObjectByType(@PathVariable String typeId, @RequestBody String request) {
  51 +
  52 + DEBUG.debug("Query mobjects by typeId: {}", typeId);
  53 +
  54 + List<MObjectInfo> moListByType = null;
  55 +
  56 + if (StringUtils.isNotBlank(typeId)) {
  57 +
  58 + moListByType = mobjectService.queryMObjectsInfoByType(Integer.parseInt(typeId));
  59 + }
  60 +
  61 + PageResponse response = null;
  62 +
  63 + if (null != moListByType && StringUtils.isNotBlank(request)) {
  64 +
  65 + response = buildPageResponse(request, moListByType);
  66 + }
  67 +
  68 + return response;
  69 + }
  70 +
  71 + @RequestMapping(value = "/query/host/{hostId}", method = RequestMethod.GET)
  72 + public PageResponse queryMObjectByHost(@PathVariable String hostId, @RequestBody String request) {
  73 +
  74 + DEBUG.debug("Query mobjects by hostId: {}", hostId);
  75 +
  76 + List<MObjectInfo> moListByHost = null;
  77 +
  78 + if (StringUtils.isNotBlank(hostId)) {
  79 +
  80 + moListByHost = mobjectService.queryMObjectsInfoByHost(Integer.parseInt(hostId));
  81 + }
  82 +
  83 + PageResponse response = null;
  84 +
  85 + if (null != moListByHost && StringUtils.isNotBlank(request)) {
  86 +
  87 + response = buildPageResponse(request, moListByHost);
  88 + }
  89 +
  90 + return response;
  91 + }
  92 +
  93 +
  94 + private PageResponse buildPageResponse(String request, List<MObjectInfo> resourceList) {
  95 +
  96 + PageRequest pageRequest = null;
  97 +
  98 + try {
  99 +
  100 + pageRequest = ConObject.OBJECT_MAPPER.readValue(request, PageRequest.class);
  101 +
  102 + } catch (IOException e) {
  103 +
  104 + DEBUG.error("Failed to parse request: {} ,error: {}", request, e);
  105 +
  106 + return null;
  107 + }
  108 +
  109 + List<MObjectInfo> selectedList = new ArrayList<>();
  110 +
  111 + int start = (pageRequest.getCurrentPage() - 1) * pageRequest.getPageSize();
  112 +
  113 + int end = (pageRequest.getCurrentPage() * pageRequest.getPageSize()) - 1;
  114 +
  115 + int realCount = end < resourceList.size() ? pageRequest.getPageSize() : resourceList.size() - start;
  116 +
  117 + for (int i = 0; i < realCount; i++) {
  118 +
  119 + selectedList.add(resourceList.get(start + i));
  120 + }
  121 +
  122 + PageResponse<MObjectInfo> response = new PageResponse<>();
  123 +
  124 + response.setCurrentPage(pageRequest.getCurrentPage());
  125 +
  126 + response.setTotal(resourceList.size());
  127 +
  128 + response.setPageSize(pageRequest.getCurrentPage());
  129 +
  130 + response.setTotalPage(resourceList.size() / pageRequest.getPageSize() + 1);
  131 +
  132 + response.setRows(selectedList);
  133 +
  134 + return response;
  135 + }
  136 +
  137 +
  138 + @RequestMapping(value = "/add", method = RequestMethod.POST)
  139 + public void addMObject(@RequestBody String request) {
  140 +
  141 + DEBUG.debug("Add mObject: {}", request);
  142 +
  143 + MObjectInfo info = null;
  144 +
  145 + try {
  146 +
  147 + info = ConObject.OBJECT_MAPPER.readValue(request, MObjectInfo.class);
  148 +
  149 + } catch (IOException e) {
  150 +
  151 + DEBUG.error("Failed to parse mObject ,error: {}", e);
  152 + }
  153 +
  154 + if (null != info) {
  155 +
  156 + mobjectService.addMObjectInfo(info);
  157 + }
  158 + }
  159 +
  160 + @RequestMapping(value = "/delete/{moId}", method = RequestMethod.POST)
  161 + public void deleteMObject(@PathVariable String moId) {
  162 +
  163 + DEBUG.debug("Delete mObject by moId: {}", moId);
  164 +
  165 + if (StringUtils.isNotBlank(moId)) {
  166 +
  167 + mobjectService.deleteMObjectInfo(Integer.parseInt(moId));
  168 + }
  169 + }
  170 +
  171 + @RequestMapping(value = "/update", method = RequestMethod.POST)
  172 + public void updateMObject(@RequestBody String request) {
  173 +
  174 + DEBUG.debug("Update mObject: {}", request);
  175 +
  176 + MObjectInfo info = null;
  177 +
  178 + try {
  179 +
  180 + info = ConObject.OBJECT_MAPPER.readValue(request, MObjectInfo.class);
  181 +
  182 + } catch (IOException e) {
  183 +
  184 + DEBUG.error("Failed to parse mObject ,error: {}", e);
  185 + }
  186 +
  187 + if (null != info) {
  188 +
  189 + mobjectService.updateMObjectInfo(info);
  190 + }
  191 + }
  192 +
  193 +}
  1 +package com.monitor.cmdb.controller;
  2 +
  3 +import com.model.TypeInfo;
  4 +import com.monitor.cmdb.common.ConObject;
  5 +import com.monitor.cmdb.service.ITypeInfoService;
  6 +import org.apache.commons.lang.StringUtils;
  7 +import org.slf4j.Logger;
  8 +import org.slf4j.LoggerFactory;
  9 +import org.springframework.beans.factory.annotation.Autowired;
  10 +import org.springframework.web.bind.annotation.*;
  11 +
  12 +import java.io.IOException;
  13 +import java.util.List;
  14 +
  15 +/**
  16 + * Created by yoho on 2016/6/15.
  17 + */
  18 +@RestController
  19 +@RequestMapping(value = "/type")
  20 +public class TypeInfoController {
  21 + public static final Logger DEBUG = LoggerFactory.getLogger(TypeInfoController.class);
  22 +
  23 + @Autowired
  24 + ITypeInfoService typeInfoService;
  25 +
  26 + @RequestMapping(value = "/query/{typeId}", method = RequestMethod.GET)
  27 + public String queryTypeInfo(@PathVariable String typeId) {
  28 +
  29 + DEBUG.debug("Query typeInfo by typeId: {}", typeId);
  30 +
  31 + TypeInfo typeInfo = null;
  32 +
  33 + String info = null;
  34 +
  35 + if (StringUtils.isNotBlank(typeId)) {
  36 +
  37 + typeInfo = typeInfoService.queryTypeInfo(Integer.parseInt(typeId));
  38 + }
  39 +
  40 + if (null != typeInfo) {
  41 +
  42 + try {
  43 +
  44 + info = ConObject.OBJECT_MAPPER.writeValueAsString(typeInfo);
  45 +
  46 + } catch (IOException e) {
  47 +
  48 + DEBUG.error("Failed to parse typeInfo, error: {}", e);
  49 + }
  50 + }
  51 +
  52 + return info;
  53 + }
  54 +
  55 + @RequestMapping(value = "/query/child/{typeId}", method = RequestMethod.GET)
  56 + public String queryChildTypeInfo(@PathVariable String typeId) {
  57 +
  58 + DEBUG.debug("Query children typeInfo by typeId: {}", typeId);
  59 +
  60 + List<TypeInfo> childsTypeInfo = null;
  61 +
  62 + String info = null;
  63 +
  64 + if (StringUtils.isNotBlank(typeId)) {
  65 +
  66 + childsTypeInfo = typeInfoService.queryChildTypesInfo(Integer.parseInt(typeId));
  67 + }
  68 +
  69 + if (null != childsTypeInfo) {
  70 +
  71 + try {
  72 +
  73 + info = ConObject.OBJECT_MAPPER.writeValueAsString(childsTypeInfo);
  74 +
  75 + } catch (IOException e) {
  76 +
  77 + DEBUG.error("Failed to parse typeInfo, error: {}", e);
  78 + }
  79 + }
  80 +
  81 + return info;
  82 + }
  83 +
  84 +
  85 + @RequestMapping(value = "/add", method = RequestMethod.POST)
  86 + public void addTypeInfo(@RequestBody String request) {
  87 +
  88 + DEBUG.debug("Add typeInfo: {}", request);
  89 +
  90 + TypeInfo info = null;
  91 +
  92 + try {
  93 +
  94 + info = ConObject.OBJECT_MAPPER.readValue(request, TypeInfo.class);
  95 +
  96 + } catch (IOException e) {
  97 +
  98 + DEBUG.error("Failed to parse typeInfo ,error: {}", e);
  99 + }
  100 +
  101 + if (null != info) {
  102 +
  103 + typeInfoService.addTypeInfo(info);
  104 + }
  105 + }
  106 +
  107 + @RequestMapping(value = "/delete/{typeId}", method = RequestMethod.POST)
  108 + public void deleteMObject(@PathVariable String typeId) {
  109 +
  110 + DEBUG.debug("Delete typeInfo by moId: {}", typeId);
  111 +
  112 + if (StringUtils.isNotBlank(typeId)) {
  113 +
  114 + typeInfoService.deleteTypeInfo(Integer.parseInt(typeId));
  115 + }
  116 + }
  117 +
  118 + @RequestMapping(value = "/update", method = RequestMethod.POST)
  119 + public void updateMObject(@RequestBody String request) {
  120 +
  121 + DEBUG.debug("Update typeInfo: {}", request);
  122 +
  123 + TypeInfo info = null;
  124 +
  125 + try {
  126 +
  127 + info = ConObject.OBJECT_MAPPER.readValue(request, TypeInfo.class);
  128 +
  129 + } catch (IOException e) {
  130 +
  131 + DEBUG.error("Failed to parse typeInfo ,error: {}", e);
  132 + }
  133 +
  134 + if (null != info) {
  135 +
  136 + typeInfoService.updateTypeInfo(info);
  137 + }
  138 + }
  139 +
  140 +}
@@ -14,7 +14,7 @@ public class TypeInfo implements Serializable{ @@ -14,7 +14,7 @@ public class TypeInfo implements Serializable{
14 14
15 private String typeName; 15 private String typeName;
16 16
17 - private int typeLevel; 17 + private int typeIsLeaf;
18 18
19 private int typeParentId; 19 private int typeParentId;
20 20
@@ -5,16 +5,16 @@ @@ -5,16 +5,16 @@
5 <cache></cache> 5 <cache></cache>
6 6
7 <resultMap id="mobjectInfoMapper" type="com.model.MObjectInfo"> 7 <resultMap id="mobjectInfoMapper" type="com.model.MObjectInfo">
8 - <id property="moId" column="mo_id"></id>  
9 - <id property="moName" column="mo_name"></id>  
10 - <id property="moHostId" column="mo_hostId"></id>  
11 - <id property="moTypeId" column="mo_typeId"></id>  
12 - <id property="moTags" column="mo_tags"></id>  
13 - <id property="moUrl" column="mo_url"></id> 8 + <id property="moId" column="id"></id>
  9 + <id property="moName" column="alias"></id>
  10 + <id property="moHostId" column="host_id"></id>
  11 + <id property="moTypeId" column="type_id"></id>
  12 + <id property="moTags" column="tags"></id>
  13 + <id property="moUrl" column="url"></id>
14 </resultMap> 14 </resultMap>
15 15
16 <select id="getAllTypesInfo" resultType="com.model.MObjectInfo" resultMap="mobjectInfoMapper" useCache="true"> 16 <select id="getAllTypesInfo" resultType="com.model.MObjectInfo" resultMap="mobjectInfoMapper" useCache="true">
17 - SELECT * FROM mobject_info ORDER BY mo_id asc 17 + SELECT * FROM mobject_info ORDER BY id asc
18 </select> 18 </select>
19 19
20 <!-- <select id="getTypeMosInfo" parameterType="int" resultType="com.model.MObjectInfo" resultMap="mobjectInfoMapper"> 20 <!-- <select id="getTypeMosInfo" parameterType="int" resultType="com.model.MObjectInfo" resultMap="mobjectInfoMapper">
@@ -25,16 +25,16 @@ @@ -25,16 +25,16 @@
25 SELECT * FROM mobject_info WHERE mo_hostId = #{moHostId} 25 SELECT * FROM mobject_info WHERE mo_hostId = #{moHostId}
26 </select>--> 26 </select>-->
27 27
28 - <insert id="insertMoInfo" useGeneratedKeys="true" keyProperty="mo_id" parameterType="com.model.MObjectInfo">  
29 - INSERT INTO mobject_info(mo_name,mo_hostId,mo_typeId,mo_tags,m_url) VALUES (#{moName},#{moHostId},#{moTypeId},#{moTags},#{moUrl}) 28 + <insert id="insertMoInfo" useGeneratedKeys="true" keyProperty="id" parameterType="com.model.MObjectInfo">
  29 + INSERT INTO mobject_info(alias,host_id,type_id,tags,url) VALUES (#{moName},#{moHostId},#{moTypeId},#{moTags},#{moUrl})
30 </insert> 30 </insert>
31 31
32 <delete id="deleteMoInfo" parameterType="int"> 32 <delete id="deleteMoInfo" parameterType="int">
33 - DELETE FROM mobject_info WHERE mo_id = #{moId} 33 + DELETE FROM mobject_info WHERE id = #{moId}
34 </delete> 34 </delete>
35 35
36 <update id="updateMoInfo" parameterType="com.model.MObjectInfo"> 36 <update id="updateMoInfo" parameterType="com.model.MObjectInfo">
37 - UPDATE mobject_info SET mo_name=#{moName},mo_hostId=#{moHostId},mo_typeId=#{moTypeId},mo_tags=#{moTags},mo_url=#{moUrl} WHERE mo_id = #{moId} 37 + UPDATE mobject_info SET alias=#{moName},host_id=#{moHostId},type_id=#{moTypeId},tags=#{moTags},url=#{moUrl} WHERE id = #{moId}
38 </update> 38 </update>
39 39
40 40
@@ -6,14 +6,14 @@ @@ -6,14 +6,14 @@
6 <cache></cache> 6 <cache></cache>
7 7
8 <resultMap id="typeInfoMap" type="com.model.TypeInfo"> 8 <resultMap id="typeInfoMap" type="com.model.TypeInfo">
9 - <id property="typeId" column="type_id"></id>  
10 - <id property="typeName" column="type_name"></id>  
11 - <id property="typeLevel" column="type_level"></id>  
12 - <id property="typeParentId" column="type_parentId"></id> 9 + <id property="typeId" column="id"></id>
  10 + <id property="typeName" column="alias"></id>
  11 + <id property="typeIsLeaf" column="isLeaf"></id>
  12 + <id property="typeParentId" column="parent_id"></id>
13 </resultMap> 13 </resultMap>
14 14
15 <select id="getAllTypesInfo" resultType="com.model.TypeInfo" resultMap="typeInfoMap" useCache="true"> 15 <select id="getAllTypesInfo" resultType="com.model.TypeInfo" resultMap="typeInfoMap" useCache="true">
16 - SELECT * FROM type_info ORDER BY type_level asc 16 + SELECT * FROM type_info ORDER BY id asc
17 </select> 17 </select>
18 18
19 <!-- <select id="getChildTypesInfo" parameterType="int" resultType="com.model.TypeInfo" resultMap="typeInfoMap"> 19 <!-- <select id="getChildTypesInfo" parameterType="int" resultType="com.model.TypeInfo" resultMap="typeInfoMap">
@@ -25,15 +25,15 @@ @@ -25,15 +25,15 @@
25 </select>--> 25 </select>-->
26 26
27 <insert id="insertTypeInfo" useGeneratedKeys="true" keyProperty="type_id" parameterType="com.model.TypeInfo"> 27 <insert id="insertTypeInfo" useGeneratedKeys="true" keyProperty="type_id" parameterType="com.model.TypeInfo">
28 - INSERT INTO type_info(type_name,type_level,type_parent_id) VALUES (#{typeName},#{typeLevel},#{typeParentId}) 28 + INSERT INTO type_info(alias,isLeaf,parent_id) VALUES (#{typeName},#{typeIsLeaf},#{typeParentId})
29 </insert> 29 </insert>
30 30
31 <delete id="deleteTypeInfo" parameterType="int"> 31 <delete id="deleteTypeInfo" parameterType="int">
32 - DELETE FROM type_info WHERE type_id = #{typeId} 32 + DELETE FROM type_info WHERE id = #{typeId}
33 </delete> 33 </delete>
34 34
35 <update id="updateTypeInfo" parameterType="com.model.TypeInfo"> 35 <update id="updateTypeInfo" parameterType="com.model.TypeInfo">
36 - UPDATE type_info SET type_name=#{typeName},type_level=#{typeLevel},type_parent_id=#{typeParentId} WHERE type_id=#{typeId} 36 + UPDATE type_info SET alias=#{typeName},isLeaf=#{typeIsLeaf},parent_id=#{typeParentId} WHERE id=#{typeId}
37 </update> 37 </update>
38 38
39 </mapper> 39 </mapper>
@@ -24,6 +24,7 @@ @@ -24,6 +24,7 @@
24 <org.yaml-version>1.9</org.yaml-version> 24 <org.yaml-version>1.9</org.yaml-version>
25 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 25 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
26 <lombok-version>1.16.6</lombok-version> 26 <lombok-version>1.16.6</lombok-version>
  27 + <jackson-version>1.9.13</jackson-version>
27 </properties> 28 </properties>
28 29
29 <dependencyManagement> 30 <dependencyManagement>
@@ -123,6 +124,16 @@ @@ -123,6 +124,16 @@
123 <artifactId>lombok</artifactId> 124 <artifactId>lombok</artifactId>
124 <version>${lombok-version}</version> 125 <version>${lombok-version}</version>
125 </dependency> 126 </dependency>
  127 + <dependency>
  128 + <groupId>org.codehaus.jackson</groupId>
  129 + <artifactId>jackson-core-asl</artifactId>
  130 + <version>${jackson-version}</version>
  131 + </dependency>
  132 + <dependency>
  133 + <groupId>org.codehaus.jackson</groupId>
  134 + <artifactId>jackson-mapper-asl</artifactId>
  135 + <version>${jackson-version}</version>
  136 + </dependency>
126 </dependencies> 137 </dependencies>
127 </dependencyManagement> 138 </dependencyManagement>
128 139