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============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
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);
72 private RoleService service;
74 private String viewName;
76 @RequestMapping(value = { "/role_function_list" }, method = RequestMethod.GET)
77 public ModelAndView welcome(HttpServletRequest request) {
78 Map<String, Object> model = new HashMap<>();
79 ObjectMapper mapper = new ObjectMapper();
80 User user = UserUtils.getUserSession(request);
82 model.put("availableRoleFunctions",
83 mapper.writeValueAsString(service.getRoleFunctions(user.getOrgUserId())));
84 } catch (Exception e) {
85 logger.error(EELFLoggerDelegate.errorLogger, "welcome failed", e);
87 return new ModelAndView(getViewName(), model);
90 @RequestMapping(value = { "/get_role_functions" }, method = RequestMethod.GET)
91 public void getRoleFunctionList(HttpServletRequest request, HttpServletResponse response) {
92 Map<String, Object> model = new HashMap<>();
93 ObjectMapper mapper = new ObjectMapper();
94 User user = UserUtils.getUserSession(request);
96 model.put("availableRoleFunctions",
97 mapper.writeValueAsString(service.getRoleFunctions(user.getOrgUserId())));
98 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
99 JSONObject j = new JSONObject(msg);
100 response.getWriter().write(j.toString());
101 } catch (Exception e) {
102 logger.error(EELFLoggerDelegate.errorLogger, "getROleFunctionList failed", e);
106 @RequestMapping(value = { "/role_function_list/saveRoleFunction" }, method = RequestMethod.POST)
107 public void saveRoleFunction(HttpServletRequest request, HttpServletResponse response, @RequestBody String roleFunc)
109 ObjectMapper mapper = new ObjectMapper();
110 User user = UserUtils.getUserSession(request);
112 String restCallStatus = "";
114 String data = roleFunc;
115 RoleFunction availableRoleFunction = mapper.readValue(data, RoleFunction.class);
116 String code = availableRoleFunction.getCode();
117 RoleFunction domainRoleFunction = service.getRoleFunction(user.getOrgUserId(), code);
118 domainRoleFunction.setName(availableRoleFunction.getName());
119 domainRoleFunction.setCode(code);
120 restCallStatus = "success";
121 service.saveRoleFunction(user.getOrgUserId(), domainRoleFunction);
122 } catch (Exception e) {
123 logger.error(EELFLoggerDelegate.errorLogger, "saveRoleFunction failed", e);
124 throw new IOException(e);
126 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(restCallStatus));
127 JSONObject j = new JSONObject(msg);
128 response.getWriter().write(j.toString());
131 @RequestMapping(value = { "/role_function_list/addRoleFunction" }, method = RequestMethod.POST)
132 public void addRoleFunction(HttpServletRequest request, HttpServletResponse response, @RequestBody String roleFunc)
134 ObjectMapper mapper = new ObjectMapper();
135 User user = UserUtils.getUserSession(request);
137 String restCallStatus = "";
138 boolean canSave = true;
140 String data = roleFunc;
141 RoleFunction availableRoleFunction = mapper.readValue(data, RoleFunction.class);
142 String code = availableRoleFunction.getCode();
143 List<RoleFunction> currentRoleFunction = service.getRoleFunctions(user.getOrgUserId());
144 restCallStatus = "success";
145 for (RoleFunction roleF : currentRoleFunction) {
146 if (roleF.getCode().equals(code)) {
147 restCallStatus = "code exists";
153 service.saveRoleFunction(user.getOrgUserId(), availableRoleFunction);
154 } catch (Exception e) {
155 logger.error(EELFLoggerDelegate.errorLogger, "addRoleFunction failed", e);
156 throw new IOException(e);
158 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(restCallStatus));
159 JSONObject j = new JSONObject(msg);
160 response.getWriter().write(j.toString());
163 @RequestMapping(value = { "/role_function_list/removeRoleFunction" }, method = RequestMethod.POST)
164 public void removeRoleFunction(HttpServletRequest request, HttpServletResponse response,
165 @RequestBody String roleFunc) throws IOException {
166 ObjectMapper mapper = new ObjectMapper();
167 User user = UserUtils.getUserSession(request);
169 String restCallStatus = "";
171 String data = roleFunc;
173 RoleFunction availableRoleFunction = mapper.readValue(data, RoleFunction.class);
175 RoleFunction domainRoleFunction = service.getRoleFunction(user.getOrgUserId(),
176 availableRoleFunction.getCode());
178 service.deleteRoleFunction(user.getOrgUserId(), domainRoleFunction);
179 logger.info(EELFLoggerDelegate.auditLogger, "Remove role function " + domainRoleFunction.getName());
180 restCallStatus = "success";
181 } catch (Exception e) {
182 logger.error(EELFLoggerDelegate.errorLogger, "removeRoleFunction failed", e);
183 throw new IOException(e);
185 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(restCallStatus));
186 JSONObject j = new JSONObject(msg);
187 response.getWriter().write(j.toString());
191 public String getViewName() {
196 public void setViewName(String viewName) {
197 this.viewName = viewName;