0b8fb8c8e4439e4de09c2c3ed8b7dedf940b00b2
[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                 try {
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);
86                 }
87                 
88         }
89         
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);
95
96                 String restCallStatus = "";
97                 try {
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");
110                 }
111                 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(restCallStatus));
112                 JSONObject j = new JSONObject(msg);
113                 response.getWriter().write(j.toString());
114         }
115         
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);
121
122                 String restCallStatus = "";
123                 boolean canSave=true;
124                 try {
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";
133                                         canSave=false;
134                                         break;
135                                 }
136                         }
137                         if(canSave)
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());
143                 }
144                 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(restCallStatus));
145                 JSONObject j = new JSONObject(msg);
146                 response.getWriter().write(j.toString());
147         }
148
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);
154
155                 String restCallStatus = "";
156                 try {
157                         String data = roleFunc;
158                 
159                         RoleFunction availableRoleFunction = mapper.readValue(data, RoleFunction.class);
160
161                         RoleFunction domainRoleFunction = service.getRoleFunction(user.getOrgUserId(),availableRoleFunction.getCode());
162                         
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());
170                 }
171                 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(restCallStatus));
172                 JSONObject j = new JSONObject(msg);
173                 response.getWriter().write(j.toString());
174         }
175
176         public String getViewName() {
177                 return viewName;
178         }
179         public void setViewName(String viewName) {
180                 this.viewName = viewName;
181         }
182         
183         
184 }