4ac5f37a133541ed4ecf15aa241caa03a54dc820
[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  * 
37  */
38 package org.onap.portalapp.controller.core;
39
40 import java.io.IOException;
41 import java.util.HashMap;
42 import java.util.List;
43 import java.util.Map;
44
45 import javax.servlet.http.HttpServletRequest;
46 import javax.servlet.http.HttpServletResponse;
47
48 import org.json.JSONObject;
49 import org.onap.portalsdk.core.controller.RestrictedBaseController;
50 import org.onap.portalsdk.core.domain.RoleFunction;
51 import org.onap.portalsdk.core.domain.User;
52 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
53 import org.onap.portalsdk.core.service.RoleService;
54 import org.onap.portalsdk.core.web.support.JsonMessage;
55 import org.onap.portalsdk.core.web.support.UserUtils;
56 import org.springframework.beans.factory.annotation.Autowired;
57 import org.springframework.stereotype.Controller;
58 import org.springframework.web.bind.annotation.RequestBody;
59 import org.springframework.web.bind.annotation.RequestMapping;
60 import org.springframework.web.bind.annotation.RequestMethod;
61 import org.springframework.web.servlet.ModelAndView;
62
63 import com.fasterxml.jackson.databind.ObjectMapper;
64
65 @Controller
66 @RequestMapping("/")
67 public class RoleFunctionListController extends RestrictedBaseController {
68
69         private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RoleFunctionListController.class);
70         private static final String SUCCESS = "SUCCESS";
71
72         @Autowired
73         private RoleService service;
74
75         private String viewName;
76
77         @RequestMapping(value = { "/role_function_list" }, method = RequestMethod.GET)
78         public ModelAndView welcome(HttpServletRequest request) {
79                 Map<String, Object> model = new HashMap<>();
80                 ObjectMapper mapper = new ObjectMapper();
81                 User user = UserUtils.getUserSession(request);
82                 try {
83                         model.put("availableRoleFunctions",
84                                         mapper.writeValueAsString(service.getRoleFunctions(user.getOrgUserId())));
85                 } catch (Exception e) {
86                         logger.error(EELFLoggerDelegate.errorLogger, "welcome failed", e);
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<>();
94                 ObjectMapper mapper = new ObjectMapper();
95                 User user = UserUtils.getUserSession(request);
96                 try {
97                         model.put("availableRoleFunctions",
98                                         mapper.writeValueAsString(service.getRoleFunctions(user.getOrgUserId())));
99                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
100                         JSONObject j = new JSONObject(msg);
101                         response.getWriter().write(j.toString());
102                 } catch (Exception e) {
103                         logger.error(EELFLoggerDelegate.errorLogger, "getROleFunctionList failed", e);
104                 }
105         }
106
107         @RequestMapping(value = { "/role_function_list/saveRoleFunction" }, method = RequestMethod.POST)
108         public void saveRoleFunction(HttpServletRequest request, HttpServletResponse response, @RequestBody String roleFunc)
109                         throws IOException {
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                         logger.error(EELFLoggerDelegate.errorLogger, "saveRoleFunction failed", e);
125                         throw new IOException(e);
126                 }
127                 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(restCallStatus));
128                 JSONObject j = new JSONObject(msg);
129                 response.getWriter().write(j.toString());
130         }
131
132         @RequestMapping(value = { "/role_function_list/addRoleFunction" }, method = RequestMethod.POST)
133         public void addRoleFunction(HttpServletRequest request, HttpServletResponse response, @RequestBody String roleFunc)
134                         throws IOException {
135                 ObjectMapper mapper = new ObjectMapper();
136                 User user = UserUtils.getUserSession(request);
137
138                 String restCallStatus = "";
139                 boolean canSave = true;
140                 try {
141                         String data = roleFunc;
142                         RoleFunction availableRoleFunction = mapper.readValue(data, RoleFunction.class);
143                         String code = availableRoleFunction.getCode();
144                         List<RoleFunction> currentRoleFunction = service.getRoleFunctions(user.getOrgUserId());
145                         restCallStatus = SUCCESS;
146                         for (RoleFunction roleF : currentRoleFunction) {
147                                 if (roleF.getCode().equals(code)) {
148                                         restCallStatus = "code exists";
149                                         canSave = false;
150                                         break;
151                                 }
152                         }
153                         if (canSave)
154                                 service.saveRoleFunction(user.getOrgUserId(), availableRoleFunction);
155                 } catch (Exception e) {
156                         logger.error(EELFLoggerDelegate.errorLogger, "addRoleFunction failed", e);
157                         throw new IOException(e);
158                 }
159                 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(restCallStatus));
160                 JSONObject j = new JSONObject(msg);
161                 response.getWriter().write(j.toString());
162         }
163
164         @RequestMapping(value = { "/role_function_list/removeRoleFunction" }, method = RequestMethod.POST)
165         public void removeRoleFunction(HttpServletRequest request, HttpServletResponse response,
166                         @RequestBody String roleFunc) throws IOException {
167                 ObjectMapper mapper = new ObjectMapper();
168                 User user = UserUtils.getUserSession(request);
169
170                 String restCallStatus = "";
171                 try {
172                         String data = roleFunc;
173
174                         RoleFunction availableRoleFunction = mapper.readValue(data, RoleFunction.class);
175
176                         RoleFunction domainRoleFunction = service.getRoleFunction(user.getOrgUserId(),
177                                         availableRoleFunction.getCode());
178
179                         service.deleteRoleFunction(user.getOrgUserId(), domainRoleFunction);
180                         logger.info(EELFLoggerDelegate.auditLogger, "Remove role function " + domainRoleFunction.getName());
181                         restCallStatus = SUCCESS;
182                 } catch (Exception e) {
183                         logger.error(EELFLoggerDelegate.errorLogger, "removeRoleFunction failed", e);
184                         throw new IOException(e);
185                 }
186                 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(restCallStatus));
187                 JSONObject j = new JSONObject(msg);
188                 response.getWriter().write(j.toString());
189         }
190
191         @Override
192         public String getViewName() {
193                 return viewName;
194         }
195
196         @Override
197         public void setViewName(String viewName) {
198                 this.viewName = viewName;
199         }
200
201 }