2 * ================================================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ================================================================================
20 package org.openecomp.portalapp.controller.core;
22 import java.util.HashMap;
23 import java.util.List;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
29 import org.json.JSONObject;
30 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
31 import org.openecomp.portalsdk.core.domain.RoleFunction;
32 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
33 import org.openecomp.portalsdk.core.service.RoleService;
34 import org.openecomp.portalsdk.core.web.support.JsonMessage;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.stereotype.Controller;
37 import org.springframework.web.bind.annotation.RequestBody;
38 import org.springframework.web.bind.annotation.RequestMapping;
39 import org.springframework.web.bind.annotation.RequestMethod;
40 import org.springframework.web.servlet.ModelAndView;
42 import com.fasterxml.jackson.databind.ObjectMapper;
46 public class RoleFunctionListController extends RestrictedBaseController {
47 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RoleFunctionListController.class);
52 private String viewName;
54 @RequestMapping(value = {"/role_function_list" }, method = RequestMethod.GET)
55 public ModelAndView welcome(HttpServletRequest request) {
56 Map<String, Object> model = new HashMap<String, Object>();
57 ObjectMapper mapper = new ObjectMapper();
60 model.put("availableRoleFunctions", mapper.writeValueAsString(service.getRoleFunctions()));
61 } catch (Exception e) {
62 logger.error(EELFLoggerDelegate.errorLogger, "welcome failed", e);
65 return new ModelAndView(getViewName(),model);
68 @RequestMapping(value = {"/get_role_functions" }, method = RequestMethod.GET)
69 public void getRoleFunctionList(HttpServletRequest request,HttpServletResponse response) {
70 Map<String, Object> model = new HashMap<String, Object>();
71 ObjectMapper mapper = new ObjectMapper();
74 model.put("availableRoleFunctions", mapper.writeValueAsString(service.getRoleFunctions()));
75 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
76 JSONObject j = new JSONObject(msg);
77 response.getWriter().write(j.toString());
78 } catch (Exception e) {
79 logger.error(EELFLoggerDelegate.errorLogger, "getROleFunctionList failed", e);
84 @RequestMapping(value = {"/role_function_list/saveRoleFunction" }, method = RequestMethod.POST)
85 public void saveRoleFunction(HttpServletRequest request,
86 HttpServletResponse response, @RequestBody String roleFunc) throws Exception {
87 ObjectMapper mapper = new ObjectMapper();
88 String restCallStatus = "";
90 String data = roleFunc;
91 RoleFunction availableRoleFunction = mapper.readValue(data, RoleFunction.class);
92 String code = availableRoleFunction.getCode();
93 RoleFunction domainRoleFunction = service.getRoleFunction(code);
94 domainRoleFunction.setName(availableRoleFunction.getName());
95 domainRoleFunction.setCode(code);
96 restCallStatus="success";
97 service.saveRoleFunction(domainRoleFunction);
98 } catch (Exception e) {
99 restCallStatus="fail";
100 logger.error(EELFLoggerDelegate.errorLogger, "saveRoleFunction failed", e);
102 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(restCallStatus));
103 JSONObject j = new JSONObject(msg);
104 response.getWriter().write(j.toString());
107 @RequestMapping(value = {"/role_function_list/addRoleFunction" }, method = RequestMethod.POST)
108 public void addRoleFunction(HttpServletRequest request,
109 HttpServletResponse response, @RequestBody String roleFunc) throws Exception {
110 ObjectMapper mapper = new ObjectMapper();
111 String restCallStatus = "";
112 boolean canSave=true;
114 String data = roleFunc;
115 RoleFunction availableRoleFunction = mapper.readValue(data, RoleFunction.class);
116 String code = availableRoleFunction.getCode();
117 RoleFunction domainRoleFunction = service.getRoleFunction(code);
118 domainRoleFunction.setName(availableRoleFunction.getName());
119 domainRoleFunction.setCode(code);
120 List<RoleFunction> currentRoleFunction = service.getRoleFunctions();
121 restCallStatus="success";
122 for(RoleFunction roleF:currentRoleFunction){
123 if(roleF.getCode().equals(code)){
124 restCallStatus="code exists";
130 service.saveRoleFunction(domainRoleFunction);
131 } catch (Exception e) {
132 restCallStatus="fail";
133 logger.error(EELFLoggerDelegate.errorLogger, "addRoleFunction failed", e);
135 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(restCallStatus));
136 JSONObject j = new JSONObject(msg);
137 response.getWriter().write(j.toString());
140 @RequestMapping(value = {"/role_function_list/removeRoleFunction" }, method = RequestMethod.POST)
141 public void removeRoleFunction(HttpServletRequest request,
142 HttpServletResponse response, @RequestBody String roleFunc) throws Exception {
143 ObjectMapper mapper = new ObjectMapper();
144 String restCallStatus = "";
146 String data = roleFunc;
148 RoleFunction availableRoleFunction = mapper.readValue(data, RoleFunction.class);
150 RoleFunction domainRoleFunction = service.getRoleFunction(availableRoleFunction.getCode());
152 service.deleteRoleFunction(domainRoleFunction);
153 logger.info(EELFLoggerDelegate.auditLogger, "Remove role function " + domainRoleFunction.getName());
154 restCallStatus="success";
155 } catch (Exception e) {
156 restCallStatus="fail";
157 logger.error(EELFLoggerDelegate.errorLogger, "removeRoleFunction failed", e);
159 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(restCallStatus));
160 JSONObject j = new JSONObject(msg);
161 response.getWriter().write(j.toString());
164 public String getViewName() {
167 public void setViewName(String viewName) {
168 this.viewName = viewName;