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.util.HashMap;
41 import java.util.List;
44 import javax.servlet.http.HttpServletRequest;
45 import javax.servlet.http.HttpServletResponse;
47 import org.json.JSONObject;
48 import org.onap.portalsdk.core.controller.RestrictedBaseController;
49 import org.onap.portalsdk.core.domain.RoleFunction;
50 import org.onap.portalsdk.core.domain.User;
51 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
52 import org.onap.portalsdk.core.service.RoleService;
53 import org.onap.portalsdk.core.web.support.JsonMessage;
54 import org.onap.portalsdk.core.web.support.UserUtils;
55 import org.springframework.beans.factory.annotation.Autowired;
56 import org.springframework.stereotype.Controller;
57 import org.springframework.web.bind.annotation.RequestBody;
58 import org.springframework.web.bind.annotation.RequestMapping;
59 import org.springframework.web.bind.annotation.RequestMethod;
60 import org.springframework.web.servlet.ModelAndView;
62 import com.fasterxml.jackson.databind.ObjectMapper;
66 public class RoleFunctionListController extends RestrictedBaseController {
68 private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RoleFunctionListController.class);
71 private RoleService service;
73 private String viewName;
75 @RequestMapping(value = {"/role_function_list" }, method = RequestMethod.GET)
76 public ModelAndView welcome(HttpServletRequest request) {
77 Map<String, Object> model = new HashMap<String, Object>();
78 ObjectMapper mapper = new ObjectMapper();
79 User user = UserUtils.getUserSession(request);
83 model.put("availableRoleFunctions", mapper.writeValueAsString(service.getRoleFunctions(user.getOrgUserId())));
84 } catch (Exception e) {
85 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<String, Object>();
94 ObjectMapper mapper = new ObjectMapper();
95 User user = UserUtils.getUserSession(request);
97 model.put("availableRoleFunctions", 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);
107 @RequestMapping(value = {"/role_function_list/saveRoleFunction" }, method = RequestMethod.POST)
108 public void saveRoleFunction(HttpServletRequest request,
109 HttpServletResponse response, @RequestBody String roleFunc) throws Exception {
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 restCallStatus="fail";
125 logger.error(EELFLoggerDelegate.errorLogger, "saveRoleFunction failed", e);
126 throw new Exception("failed while Saving RoleFunction");
128 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(restCallStatus));
129 JSONObject j = new JSONObject(msg);
130 response.getWriter().write(j.toString());
133 @RequestMapping(value = {"/role_function_list/addRoleFunction" }, method = RequestMethod.POST)
134 public void addRoleFunction(HttpServletRequest request,
135 HttpServletResponse response, @RequestBody String roleFunc) throws Exception {
136 ObjectMapper mapper = new ObjectMapper();
137 User user = UserUtils.getUserSession(request);
139 String restCallStatus = "";
140 boolean canSave=true;
142 String data = roleFunc;
143 RoleFunction availableRoleFunction = mapper.readValue(data, RoleFunction.class);
144 String code = availableRoleFunction.getCode();
145 List<RoleFunction> currentRoleFunction = service.getRoleFunctions(user.getOrgUserId());
146 restCallStatus="success";
147 for(RoleFunction roleF:currentRoleFunction){
148 if(roleF.getCode().equals(code)){
149 restCallStatus="code exists";
155 service.saveRoleFunction(user.getOrgUserId(),availableRoleFunction);
156 } catch (Exception e) {
157 restCallStatus="fail";
158 logger.error(EELFLoggerDelegate.errorLogger, "addRoleFunction failed", e);
159 throw new Exception(e.getMessage());
161 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(restCallStatus));
162 JSONObject j = new JSONObject(msg);
163 response.getWriter().write(j.toString());
166 @RequestMapping(value = {"/role_function_list/removeRoleFunction" }, method = RequestMethod.POST)
167 public void removeRoleFunction(HttpServletRequest request,
168 HttpServletResponse response, @RequestBody String roleFunc) throws Exception {
169 ObjectMapper mapper = new ObjectMapper();
170 User user = UserUtils.getUserSession(request);
172 String restCallStatus = "";
174 String data = roleFunc;
176 RoleFunction availableRoleFunction = mapper.readValue(data, RoleFunction.class);
178 RoleFunction domainRoleFunction = service.getRoleFunction(user.getOrgUserId(),availableRoleFunction.getCode());
180 service.deleteRoleFunction(user.getOrgUserId(),domainRoleFunction);
181 logger.info(EELFLoggerDelegate.auditLogger, "Remove role function " + domainRoleFunction.getName());
182 restCallStatus="success";
183 } catch (Exception e) {
184 restCallStatus="fail";
185 logger.error(EELFLoggerDelegate.errorLogger, "removeRoleFunction failed", e);
186 throw new Exception(e.getMessage());
188 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(restCallStatus));
189 JSONObject j = new JSONObject(msg);
190 response.getWriter().write(j.toString());
193 public String getViewName() {
196 public void setViewName(String viewName) {
197 this.viewName = viewName;