2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the "License");
10 * you may not use this software except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
38 package org.onap.portalapp.controller.core;
40 import java.io.IOException;
41 import java.util.HashMap;
42 import java.util.List;
45 import javax.servlet.http.HttpServletRequest;
46 import javax.servlet.http.HttpServletResponse;
48 import org.json.JSONObject;
49 import org.onap.portalsdk.core.controller.RestrictedBaseController;
50 import org.onap.portalsdk.core.domain.RoleFunction;
51 import org.onap.portalsdk.core.domain.User;
52 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
53 import org.onap.portalsdk.core.service.RoleService;
54 import org.onap.portalsdk.core.web.support.JsonMessage;
55 import org.onap.portalsdk.core.web.support.UserUtils;
56 import org.springframework.beans.factory.annotation.Autowired;
57 import org.springframework.stereotype.Controller;
58 import org.springframework.web.bind.annotation.RequestBody;
59 import org.springframework.web.bind.annotation.RequestMapping;
60 import org.springframework.web.bind.annotation.RequestMethod;
61 import org.springframework.web.servlet.ModelAndView;
63 import com.fasterxml.jackson.databind.ObjectMapper;
67 public class RoleFunctionListController extends RestrictedBaseController {
69 private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RoleFunctionListController.class);
70 private static final String SUCCESS = "SUCCESS";
73 private RoleService service;
75 private String viewName;
77 @RequestMapping(value = { "/role_function_list" }, method = RequestMethod.GET)
78 public ModelAndView welcome(HttpServletRequest request) {
79 Map<String, Object> model = new HashMap<>();
80 ObjectMapper mapper = new ObjectMapper();
81 User user = UserUtils.getUserSession(request);
83 model.put("availableRoleFunctions",
84 mapper.writeValueAsString(service.getRoleFunctions(user.getOrgUserId())));
85 } catch (Exception e) {
86 logger.error(EELFLoggerDelegate.errorLogger, "welcome failed", e);
88 return new ModelAndView(getViewName(), model);
91 @RequestMapping(value = { "/get_role_functions" }, method = RequestMethod.GET)
92 public void getRoleFunctionList(HttpServletRequest request, HttpServletResponse response) {
93 Map<String, Object> model = new HashMap<>();
94 ObjectMapper mapper = new ObjectMapper();
95 User user = UserUtils.getUserSession(request);
97 model.put("availableRoleFunctions",
98 mapper.writeValueAsString(service.getRoleFunctions(user.getOrgUserId())));
99 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
100 JSONObject j = new JSONObject(msg);
101 response.getWriter().write(j.toString());
102 } catch (Exception e) {
103 logger.error(EELFLoggerDelegate.errorLogger, "getROleFunctionList failed", e);
107 @RequestMapping(value = { "/role_function_list/saveRoleFunction" }, method = RequestMethod.POST)
108 public void saveRoleFunction(HttpServletRequest request, HttpServletResponse response, @RequestBody String roleFunc)
110 ObjectMapper mapper = new ObjectMapper();
111 User user = UserUtils.getUserSession(request);
113 String restCallStatus = "";
115 String data = roleFunc;
116 RoleFunction availableRoleFunction = mapper.readValue(data, RoleFunction.class);
117 String code = availableRoleFunction.getCode();
118 RoleFunction domainRoleFunction = service.getRoleFunction(user.getOrgUserId(), code);
119 domainRoleFunction.setName(availableRoleFunction.getName());
120 domainRoleFunction.setCode(code);
121 restCallStatus = SUCCESS;
122 service.saveRoleFunction(user.getOrgUserId(), domainRoleFunction);
123 } catch (Exception e) {
124 logger.error(EELFLoggerDelegate.errorLogger, "saveRoleFunction failed", e);
125 throw new IOException(e);
127 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(restCallStatus));
128 JSONObject j = new JSONObject(msg);
129 response.getWriter().write(j.toString());
132 @RequestMapping(value = { "/role_function_list/addRoleFunction" }, method = RequestMethod.POST)
133 public void addRoleFunction(HttpServletRequest request, HttpServletResponse response, @RequestBody String roleFunc)
135 ObjectMapper mapper = new ObjectMapper();
136 User user = UserUtils.getUserSession(request);
138 String restCallStatus = "";
139 boolean canSave = true;
141 String data = roleFunc;
142 RoleFunction availableRoleFunction = mapper.readValue(data, RoleFunction.class);
143 String code = availableRoleFunction.getCode();
144 List<RoleFunction> currentRoleFunction = service.getRoleFunctions(user.getOrgUserId());
145 restCallStatus = SUCCESS;
146 for (RoleFunction roleF : currentRoleFunction) {
147 if (roleF.getCode().equals(code)) {
148 restCallStatus = "code exists";
154 service.saveRoleFunction(user.getOrgUserId(), availableRoleFunction);
155 } catch (Exception e) {
156 logger.error(EELFLoggerDelegate.errorLogger, "addRoleFunction failed", e);
157 throw new IOException(e);
159 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(restCallStatus));
160 JSONObject j = new JSONObject(msg);
161 response.getWriter().write(j.toString());
164 @RequestMapping(value = { "/role_function_list/removeRoleFunction" }, method = RequestMethod.POST)
165 public void removeRoleFunction(HttpServletRequest request, HttpServletResponse response,
166 @RequestBody String roleFunc) throws IOException {
167 ObjectMapper mapper = new ObjectMapper();
168 User user = UserUtils.getUserSession(request);
170 String restCallStatus = "";
172 String data = roleFunc;
174 RoleFunction availableRoleFunction = mapper.readValue(data, RoleFunction.class);
176 RoleFunction domainRoleFunction = service.getRoleFunction(user.getOrgUserId(),
177 availableRoleFunction.getCode());
179 service.deleteRoleFunction(user.getOrgUserId(), domainRoleFunction);
180 logger.info(EELFLoggerDelegate.auditLogger, "Remove role function " + domainRoleFunction.getName());
181 restCallStatus = SUCCESS;
182 } catch (Exception e) {
183 logger.error(EELFLoggerDelegate.errorLogger, "removeRoleFunction failed", e);
184 throw new IOException(e);
186 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(restCallStatus));
187 JSONObject j = new JSONObject(msg);
188 response.getWriter().write(j.toString());
192 public String getViewName() {
197 public void setViewName(String viewName) {
198 this.viewName = viewName;