e1e3e1329f37a6c16048db71138af8207f556e55
[portal/sdk.git] /
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
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
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
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
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
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.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.onap.portalapp.controller.core;
39
40 import java.util.HashMap;
41 import java.util.List;
42 import java.util.Map;
43
44 import javax.servlet.http.HttpServletRequest;
45 import javax.servlet.http.HttpServletResponse;
46
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;
61
62 import com.fasterxml.jackson.databind.ObjectMapper;
63
64 @Controller
65 @RequestMapping("/")
66 public class RoleFunctionListController extends RestrictedBaseController {
67         
68         private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RoleFunctionListController.class);
69
70         @Autowired
71         private RoleService service;
72         
73         private String viewName;
74
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);
80
81                 
82                 try {
83                         model.put("availableRoleFunctions", mapper.writeValueAsString(service.getRoleFunctions(user.getOrgUserId())));
84                 } catch (Exception e) {
85                         logger.error(EELFLoggerDelegate.errorLogger, "welcome failed", e);
86                 }
87                 
88                 return new ModelAndView(getViewName(),model);           
89         }
90         
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);
96                 try {
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);
103                 }
104                 
105         }
106         
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);
112
113                 String restCallStatus = "";
114                 try {
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");
127                 }
128                 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(restCallStatus));
129                 JSONObject j = new JSONObject(msg);
130                 response.getWriter().write(j.toString());
131         }
132         
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);
138
139                 String restCallStatus = "";
140                 boolean canSave=true;
141                 try {
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";
150                                         canSave=false;
151                                         break;
152                                 }
153                         }
154                         if(canSave)
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());
160                 }
161                 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(restCallStatus));
162                 JSONObject j = new JSONObject(msg);
163                 response.getWriter().write(j.toString());
164         }
165
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);
171
172                 String restCallStatus = "";
173                 try {
174                         String data = roleFunc;
175                 
176                         RoleFunction availableRoleFunction = mapper.readValue(data, RoleFunction.class);
177
178                         RoleFunction domainRoleFunction = service.getRoleFunction(user.getOrgUserId(),availableRoleFunction.getCode());
179                         
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());
187                 }
188                 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(restCallStatus));
189                 JSONObject j = new JSONObject(msg);
190                 response.getWriter().write(j.toString());
191         }
192
193         public String getViewName() {
194                 return viewName;
195         }
196         public void setViewName(String viewName) {
197                 this.viewName = viewName;
198         }
199         
200         
201 }