0014d0a5e1733c454081df49c56d527bd66ff026
[portal/sdk.git] /
1 /*-
2  * ================================================================================
3  * ECOMP Portal SDK
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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  * ================================================================================
19  */
20 package org.openecomp.portalapp.controller.core;
21
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28
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;
43
44 import com.fasterxml.jackson.databind.ObjectMapper;
45
46 @Controller
47 @RequestMapping("/")
48 public class RoleFunctionListController extends RestrictedBaseController {
49         
50         
51         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RoleFunctionListController.class);
52
53         @Autowired
54         RoleService service;
55         
56         private String viewName;
57
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);
63
64                 
65                 try {
66                         model.put("availableRoleFunctions", mapper.writeValueAsString(service.getRoleFunctions(user.getOrgUserId())));
67                 } catch (Exception e) {
68                         logger.error(EELFLoggerDelegate.errorLogger, "welcome failed", e);
69                 }
70                 
71                 return new ModelAndView(getViewName(),model);           
72         }
73         
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);
79
80                 
81                 try {
82                         model.put("availableRoleFunctions", mapper.writeValueAsString(service.getRoleFunctions(user.getOrgUserId())));
83                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
84                         JSONObject j = new JSONObject(msg);
85                         response.getWriter().write(j.toString());       
86                 } catch (Exception e) {
87                         logger.error(EELFLoggerDelegate.errorLogger, "getROleFunctionList failed", e);
88                 }
89                 
90         }
91         
92         @RequestMapping(value = {"/role_function_list/saveRoleFunction" }, method = RequestMethod.POST)
93         public void saveRoleFunction(HttpServletRequest request, 
94                         HttpServletResponse response, @RequestBody String roleFunc) throws Exception {
95                 ObjectMapper mapper = new ObjectMapper();
96                 User user = UserUtils.getUserSession(request);
97
98                 String restCallStatus = "";
99                 try {
100                         String data = roleFunc;
101                         RoleFunction availableRoleFunction = mapper.readValue(data, RoleFunction.class);                
102                         String code = availableRoleFunction.getCode();
103                         RoleFunction domainRoleFunction = service.getRoleFunction(user.getOrgUserId(),code);
104                         domainRoleFunction.setName(availableRoleFunction.getName());
105                         domainRoleFunction.setCode(code); 
106                         restCallStatus="success";
107                         service.saveRoleFunction(user.getOrgUserId(),domainRoleFunction);
108                 } catch (Exception e) {
109                         restCallStatus="fail";
110                         logger.error(EELFLoggerDelegate.errorLogger, "saveRoleFunction failed", e);
111                 }
112                 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(restCallStatus));
113                 JSONObject j = new JSONObject(msg);
114                 response.getWriter().write(j.toString());
115         }
116         
117         @RequestMapping(value = {"/role_function_list/addRoleFunction" }, method = RequestMethod.POST)
118         public void addRoleFunction(HttpServletRequest request, 
119                         HttpServletResponse response, @RequestBody String roleFunc) throws Exception {
120                 ObjectMapper mapper = new ObjectMapper();
121                 User user = UserUtils.getUserSession(request);
122
123                 String restCallStatus = "";
124                 boolean canSave=true;
125                 try {
126                         String data = roleFunc;
127                         RoleFunction availableRoleFunction = mapper.readValue(data, RoleFunction.class);                
128                         String code = availableRoleFunction.getCode();
129                         RoleFunction domainRoleFunction = service.getRoleFunction(user.getOrgUserId(),code);
130                         domainRoleFunction.setName(availableRoleFunction.getName());
131                         domainRoleFunction.setCode(code); 
132                         List<RoleFunction> currentRoleFunction = service.getRoleFunctions(user.getOrgUserId());
133                         restCallStatus="success";
134                         for(RoleFunction roleF:currentRoleFunction){
135                                 if(roleF.getCode().equals(code)){
136                                         restCallStatus="code exists";
137                                         canSave=false;
138                                         break;
139                                 }
140                         }
141                         if(canSave)
142                                 service.saveRoleFunction(user.getOrgUserId(),domainRoleFunction);
143                 } catch (Exception e) {
144                         restCallStatus="fail";
145                         logger.error(EELFLoggerDelegate.errorLogger, "addRoleFunction failed", e);
146                 }
147                 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(restCallStatus));
148                 JSONObject j = new JSONObject(msg);
149                 response.getWriter().write(j.toString());
150         }
151
152         @RequestMapping(value = {"/role_function_list/removeRoleFunction" }, method = RequestMethod.POST)
153         public void removeRoleFunction(HttpServletRequest request, 
154                         HttpServletResponse response, @RequestBody String roleFunc) throws Exception {
155                 ObjectMapper mapper = new ObjectMapper();
156                 User user = UserUtils.getUserSession(request);
157
158                 String restCallStatus = "";
159                 try {
160                         String data = roleFunc;
161                 
162                         RoleFunction availableRoleFunction = mapper.readValue(data, RoleFunction.class);
163
164                         RoleFunction domainRoleFunction = service.getRoleFunction(user.getOrgUserId(),availableRoleFunction.getCode());
165                         
166                         service.deleteRoleFunction(user.getOrgUserId(),domainRoleFunction);
167                         logger.info(EELFLoggerDelegate.auditLogger, "Remove role function " + domainRoleFunction.getName());
168                         restCallStatus="success";
169                 } catch (Exception e) {
170                         restCallStatus="fail";
171                         logger.error(EELFLoggerDelegate.errorLogger, "removeRoleFunction failed", e);
172                 }
173                 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(restCallStatus));
174                 JSONObject j = new JSONObject(msg);
175                 response.getWriter().write(j.toString());
176         }
177
178         public String getViewName() {
179                 return viewName;
180         }
181         public void setViewName(String viewName) {
182                 this.viewName = viewName;
183         }
184         
185         
186 }