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.domain.User;
33 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
34 import org.openecomp.portalsdk.core.service.RoleService;
35 import org.openecomp.portalsdk.core.web.support.JsonMessage;
36 import org.openecomp.portalsdk.core.web.support.UserUtils;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.stereotype.Controller;
39 import org.springframework.web.bind.annotation.RequestBody;
40 import org.springframework.web.bind.annotation.RequestMapping;
41 import org.springframework.web.bind.annotation.RequestMethod;
42 import org.springframework.web.servlet.ModelAndView;
44 import com.fasterxml.jackson.databind.ObjectMapper;
48 public class RoleFunctionListController extends RestrictedBaseController {
51 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RoleFunctionListController.class);
56 private String viewName;
58 @RequestMapping(value = {"/role_function_list" }, method = RequestMethod.GET)
59 public ModelAndView welcome(HttpServletRequest request) {
60 Map<String, Object> model = new HashMap<String, Object>();
61 ObjectMapper mapper = new ObjectMapper();
62 User user = UserUtils.getUserSession(request);
66 model.put("availableRoleFunctions", mapper.writeValueAsString(service.getRoleFunctions(user.getOrgUserId())));
67 } catch (Exception e) {
68 logger.error(EELFLoggerDelegate.errorLogger, "welcome failed", e);
71 return new ModelAndView(getViewName(),model);
74 @RequestMapping(value = {"/get_role_functions" }, method = RequestMethod.GET)
75 public void getRoleFunctionList(HttpServletRequest request,HttpServletResponse response) {
76 Map<String, Object> model = new HashMap<String, Object>();
77 ObjectMapper mapper = new ObjectMapper();
78 User user = UserUtils.getUserSession(request);
80 model.put("availableRoleFunctions", mapper.writeValueAsString(service.getRoleFunctions(user.getOrgUserId())));
81 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
82 JSONObject j = new JSONObject(msg);
83 response.getWriter().write(j.toString());
84 } catch (Exception e) {
85 logger.error(EELFLoggerDelegate.errorLogger, "getROleFunctionList failed", e);
90 @RequestMapping(value = {"/role_function_list/saveRoleFunction" }, method = RequestMethod.POST)
91 public void saveRoleFunction(HttpServletRequest request,
92 HttpServletResponse response, @RequestBody String roleFunc) throws Exception {
93 ObjectMapper mapper = new ObjectMapper();
94 User user = UserUtils.getUserSession(request);
96 String restCallStatus = "";
98 String data = roleFunc;
99 RoleFunction availableRoleFunction = mapper.readValue(data, RoleFunction.class);
100 String code = availableRoleFunction.getCode();
101 RoleFunction domainRoleFunction = service.getRoleFunction(user.getOrgUserId(),code);
102 domainRoleFunction.setName(availableRoleFunction.getName());
103 domainRoleFunction.setCode(code);
104 restCallStatus="success";
105 service.saveRoleFunction(user.getOrgUserId(),domainRoleFunction);
106 } catch (Exception e) {
107 restCallStatus="fail";
108 logger.error(EELFLoggerDelegate.errorLogger, "saveRoleFunction failed", e);
109 throw new Exception("failed while Saving RoleFunction");
111 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(restCallStatus));
112 JSONObject j = new JSONObject(msg);
113 response.getWriter().write(j.toString());
116 @RequestMapping(value = {"/role_function_list/addRoleFunction" }, method = RequestMethod.POST)
117 public void addRoleFunction(HttpServletRequest request,
118 HttpServletResponse response, @RequestBody String roleFunc) throws Exception {
119 ObjectMapper mapper = new ObjectMapper();
120 User user = UserUtils.getUserSession(request);
122 String restCallStatus = "";
123 boolean canSave=true;
125 String data = roleFunc;
126 RoleFunction availableRoleFunction = mapper.readValue(data, RoleFunction.class);
127 String code = availableRoleFunction.getCode();
128 List<RoleFunction> currentRoleFunction = service.getRoleFunctions(user.getOrgUserId());
129 restCallStatus="success";
130 for(RoleFunction roleF:currentRoleFunction){
131 if(roleF.getCode().equals(code)){
132 restCallStatus="code exists";
138 service.saveRoleFunction(user.getOrgUserId(),availableRoleFunction);
139 } catch (Exception e) {
140 restCallStatus="fail";
141 logger.error(EELFLoggerDelegate.errorLogger, "addRoleFunction failed", e);
142 throw new Exception(e.getMessage());
144 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(restCallStatus));
145 JSONObject j = new JSONObject(msg);
146 response.getWriter().write(j.toString());
149 @RequestMapping(value = {"/role_function_list/removeRoleFunction" }, method = RequestMethod.POST)
150 public void removeRoleFunction(HttpServletRequest request,
151 HttpServletResponse response, @RequestBody String roleFunc) throws Exception {
152 ObjectMapper mapper = new ObjectMapper();
153 User user = UserUtils.getUserSession(request);
155 String restCallStatus = "";
157 String data = roleFunc;
159 RoleFunction availableRoleFunction = mapper.readValue(data, RoleFunction.class);
161 RoleFunction domainRoleFunction = service.getRoleFunction(user.getOrgUserId(),availableRoleFunction.getCode());
163 service.deleteRoleFunction(user.getOrgUserId(),domainRoleFunction);
164 logger.info(EELFLoggerDelegate.auditLogger, "Remove role function " + domainRoleFunction.getName());
165 restCallStatus="success";
166 } catch (Exception e) {
167 restCallStatus="fail";
168 logger.error(EELFLoggerDelegate.errorLogger, "removeRoleFunction failed", e);
169 throw new Exception(e.getMessage());
171 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(restCallStatus));
172 JSONObject j = new JSONObject(msg);
173 response.getWriter().write(j.toString());
176 public String getViewName() {
179 public void setViewName(String viewName) {
180 this.viewName = viewName;