ccdab9f05dad65c2a90ed1935f3a4213a46b9ce2
[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.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
71         @Autowired
72         private RoleService service;
73
74         private String viewName;
75
76         @RequestMapping(value = { "/role_function_list" }, method = RequestMethod.GET)
77         public ModelAndView welcome(HttpServletRequest request) {
78                 Map<String, Object> model = new HashMap<>();
79                 ObjectMapper mapper = new ObjectMapper();
80                 User user = UserUtils.getUserSession(request);
81                 try {
82                         model.put("availableRoleFunctions",
83                                         mapper.writeValueAsString(service.getRoleFunctions(user.getOrgUserId())));
84                 } catch (Exception e) {
85                         logger.error(EELFLoggerDelegate.errorLogger, "welcome failed", e);
86                 }
87                 return new ModelAndView(getViewName(), model);
88         }
89
90         @RequestMapping(value = { "/get_role_functions" }, method = RequestMethod.GET)
91         public void getRoleFunctionList(HttpServletRequest request, HttpServletResponse response) {
92                 Map<String, Object> model = new HashMap<>();
93                 ObjectMapper mapper = new ObjectMapper();
94                 User user = UserUtils.getUserSession(request);
95                 try {
96                         model.put("availableRoleFunctions",
97                                         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         @RequestMapping(value = { "/role_function_list/saveRoleFunction" }, method = RequestMethod.POST)
107         public void saveRoleFunction(HttpServletRequest request, HttpServletResponse response, @RequestBody String roleFunc)
108                         throws IOException {
109                 ObjectMapper mapper = new ObjectMapper();
110                 User user = UserUtils.getUserSession(request);
111
112                 String restCallStatus = "";
113                 try {
114                         String data = roleFunc;
115                         RoleFunction availableRoleFunction = mapper.readValue(data, RoleFunction.class);
116                         String code = availableRoleFunction.getCode();
117                         RoleFunction domainRoleFunction = service.getRoleFunction(user.getOrgUserId(), code);
118                         domainRoleFunction.setName(availableRoleFunction.getName());
119                         domainRoleFunction.setCode(code);
120                         restCallStatus = "success";
121                         service.saveRoleFunction(user.getOrgUserId(), domainRoleFunction);
122                 } catch (Exception e) {
123                         logger.error(EELFLoggerDelegate.errorLogger, "saveRoleFunction failed", e);
124                         throw new IOException(e);
125                 }
126                 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(restCallStatus));
127                 JSONObject j = new JSONObject(msg);
128                 response.getWriter().write(j.toString());
129         }
130
131         @RequestMapping(value = { "/role_function_list/addRoleFunction" }, method = RequestMethod.POST)
132         public void addRoleFunction(HttpServletRequest request, HttpServletResponse response, @RequestBody String roleFunc)
133                         throws IOException {
134                 ObjectMapper mapper = new ObjectMapper();
135                 User user = UserUtils.getUserSession(request);
136
137                 String restCallStatus = "";
138                 boolean canSave = true;
139                 try {
140                         String data = roleFunc;
141                         RoleFunction availableRoleFunction = mapper.readValue(data, RoleFunction.class);
142                         String code = availableRoleFunction.getCode();
143                         List<RoleFunction> currentRoleFunction = service.getRoleFunctions(user.getOrgUserId());
144                         restCallStatus = "success";
145                         for (RoleFunction roleF : currentRoleFunction) {
146                                 if (roleF.getCode().equals(code)) {
147                                         restCallStatus = "code exists";
148                                         canSave = false;
149                                         break;
150                                 }
151                         }
152                         if (canSave)
153                                 service.saveRoleFunction(user.getOrgUserId(), availableRoleFunction);
154                 } catch (Exception e) {
155                         logger.error(EELFLoggerDelegate.errorLogger, "addRoleFunction failed", e);
156                         throw new IOException(e);
157                 }
158                 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(restCallStatus));
159                 JSONObject j = new JSONObject(msg);
160                 response.getWriter().write(j.toString());
161         }
162
163         @RequestMapping(value = { "/role_function_list/removeRoleFunction" }, method = RequestMethod.POST)
164         public void removeRoleFunction(HttpServletRequest request, HttpServletResponse response,
165                         @RequestBody String roleFunc) throws IOException {
166                 ObjectMapper mapper = new ObjectMapper();
167                 User user = UserUtils.getUserSession(request);
168
169                 String restCallStatus = "";
170                 try {
171                         String data = roleFunc;
172
173                         RoleFunction availableRoleFunction = mapper.readValue(data, RoleFunction.class);
174
175                         RoleFunction domainRoleFunction = service.getRoleFunction(user.getOrgUserId(),
176                                         availableRoleFunction.getCode());
177
178                         service.deleteRoleFunction(user.getOrgUserId(), domainRoleFunction);
179                         logger.info(EELFLoggerDelegate.auditLogger, "Remove role function " + domainRoleFunction.getName());
180                         restCallStatus = "success";
181                 } catch (Exception e) {
182                         logger.error(EELFLoggerDelegate.errorLogger, "removeRoleFunction failed", e);
183                         throw new IOException(e);
184                 }
185                 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(restCallStatus));
186                 JSONObject j = new JSONObject(msg);
187                 response.getWriter().write(j.toString());
188         }
189
190         @Override
191         public String getViewName() {
192                 return viewName;
193         }
194
195         @Override
196         public void setViewName(String viewName) {
197                 this.viewName = viewName;
198         }
199
200 }