989a9f45b29a784342391f0d71ead37bbb6c58d6
[dcaegen2/services.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : DataLake
4  * ================================================================================
5  * Copyright 2019 China Mobile
6  *=================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.datalake.feeder.controller;
22
23 import org.onap.datalake.feeder.controller.domain.PostReturnBody;
24 import org.onap.datalake.feeder.domain.PortalDesign;
25 import org.onap.datalake.feeder.dto.PortalDesignConfig;
26 import org.onap.datalake.feeder.repository.PortalDesignRepository;
27 import org.onap.datalake.feeder.service.PortalDesignService;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.http.MediaType;
32 import org.springframework.validation.BindingResult;
33 import org.springframework.web.bind.annotation.*;
34
35 import io.swagger.annotations.ApiOperation;
36
37 import java.io.IOException;
38 import java.util.List;
39
40 import javax.servlet.http.HttpServletResponse;
41
42
43 /**
44  * This controller manages portalDesign settings
45  *
46  * @author guochunmeng
47  */
48 @RestController
49 @RequestMapping(value = "/portalDesigns", produces = MediaType.APPLICATION_JSON_VALUE)
50 public class PortalDesignController {
51
52     private final Logger log = LoggerFactory.getLogger(this.getClass());
53
54     @Autowired
55     private PortalDesignRepository portalDesignRepository;
56     
57     @Autowired
58     private PortalDesignService portalDesignService;
59
60         @PostMapping("")
61         @ResponseBody
62         @ApiOperation(value="Create a portalDesign.")
63     public PostReturnBody<PortalDesignConfig> createPortalDesign(@RequestBody PortalDesignConfig portalDesignConfig, BindingResult result, HttpServletResponse response) throws IOException {
64
65                 if (result.hasErrors()) {
66                         sendError(response, 400, "Error parsing PortalDesignConfig: "+result.toString());
67                         return null;
68                 }
69
70                 PortalDesign portalDesign = null;
71                 try {
72                         portalDesign = portalDesignService.fillPortalDesignConfiguration(portalDesignConfig);
73                 } catch (Exception e) {
74                         log.debug("FillPortalDesignConfiguration failed", e.getMessage());
75                         sendError(response, 400, "Error FillPortalDesignConfiguration: "+e.getMessage());
76                         return null;
77                 }
78                 portalDesignRepository.save(portalDesign);
79                 log.info("PortalDesign save successed");
80                 return mkPostReturnBody(200, portalDesign);
81     }
82
83
84         @PutMapping("{id}")
85         @ResponseBody
86         @ApiOperation(value="Update a portalDesign.")
87         public PostReturnBody<PortalDesignConfig> updatePortalDesign(@RequestBody PortalDesignConfig portalDesignConfig, BindingResult result, @PathVariable Integer id, HttpServletResponse response) throws IOException {
88
89                 if (result.hasErrors()) {
90                         sendError(response, 400, "Error parsing PortalDesignConfig: "+result.toString());
91                         return null;
92                 }
93
94                 PortalDesign portalDesign = portalDesignService.getPortalDesign(id);
95                 if (portalDesign != null) {
96                         try {
97                                 portalDesignService.fillPortalDesignConfiguration(portalDesignConfig, portalDesign);
98                         } catch (Exception e) {
99                                 log.debug("FillPortalDesignConfiguration failed", e.getMessage());
100                                 sendError(response, 400, "Error FillPortalDesignConfiguration: "+e.getMessage());
101                                 return null;
102                         }
103                         portalDesignRepository.save(portalDesign);
104                         log.info("PortalDesign update successed");
105                         return mkPostReturnBody(200, portalDesign);
106                 } else {
107                         sendError(response, 400, "PortalDesign not found: "+id);
108                         return null;
109                 }
110
111         }
112
113
114         @DeleteMapping("/{id}")
115         @ResponseBody
116         @ApiOperation(value="delete a portalDesign.")
117     public void deletePortalDesign(@PathVariable("id") Integer id, HttpServletResponse response) throws IOException{
118                 
119                 PortalDesign oldPortalDesign= portalDesignService.getPortalDesign(id);
120                 if (oldPortalDesign == null) {
121                         sendError(response, 400, "portalDesign not found "+id);
122                 } else {
123                         portalDesignRepository.delete(oldPortalDesign);
124                         response.setStatus(204);
125                 }
126     }
127
128
129         @GetMapping("")
130         @ResponseBody
131         @ApiOperation(value="List all PortalDesigns")
132     public List<PortalDesignConfig> queryAllPortalDesign(){
133                 return portalDesignService.queryAllPortalDesign();
134     }
135
136
137         @PostMapping("/deploy/{id}")
138         @ResponseBody
139         @ApiOperation(value="PortalDesign deploy")
140         public void deployPortalDesign(@PathVariable Integer id, HttpServletResponse response) throws IOException {
141
142                 PortalDesign portalDesign = null;
143                 try {
144                         portalDesign = portalDesignRepository.findById(id).get();
145                         boolean flag;
146                         try {
147                                 flag = portalDesignService.deploy(portalDesign);
148                                 if (flag) {
149                                         portalDesign.setSubmitted(true);
150                                         portalDesignRepository.save(portalDesign);
151                                         response.setStatus(204);
152                                 } else {
153                                         sendError(response, 400, "DeployPortalDesign failed, id: "+id);
154                                 }
155                         } catch (Exception e) {
156                                 log.debug("The request failed", e.getMessage());
157                                 sendError(response, 400, "The request failed : "+e.getMessage());
158                         }
159                 } catch (Exception e) {
160                         log.debug("PortalDesign is null", e.getMessage());
161                         sendError(response, 400, "PortalDesign not found, id: "+id);
162                 }
163
164         }
165
166
167         private PostReturnBody<PortalDesignConfig> mkPostReturnBody(int statusCode, PortalDesign portalDesign) {
168                 PostReturnBody<PortalDesignConfig> retBody = new PostReturnBody<>();
169         retBody.setStatusCode(statusCode);
170         retBody.setReturnBody(portalDesign.getPortalDesignConfig());
171         return retBody;
172         }
173     
174         private void sendError(HttpServletResponse response, int sc, String msg) throws IOException {
175                 log.info(msg);
176                 response.sendError(sc, msg);            
177         }
178     
179 }