13b3a7c8f2315d47dbf6814af39d1a04158fb90d
[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.logging.logic.EELFLoggerDelegate;
33 import org.openecomp.portalsdk.core.service.RoleService;
34 import org.openecomp.portalsdk.core.web.support.JsonMessage;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.stereotype.Controller;
37 import org.springframework.web.bind.annotation.RequestBody;
38 import org.springframework.web.bind.annotation.RequestMapping;
39 import org.springframework.web.bind.annotation.RequestMethod;
40 import org.springframework.web.servlet.ModelAndView;
41
42 import com.fasterxml.jackson.databind.ObjectMapper;
43
44 @Controller
45 @RequestMapping("/")
46 public class RoleFunctionListController extends RestrictedBaseController {
47         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RoleFunctionListController.class);
48
49         @Autowired
50         RoleService service;
51         
52         private String viewName;
53
54         @RequestMapping(value = {"/role_function_list" }, method = RequestMethod.GET)
55         public ModelAndView welcome(HttpServletRequest request) {
56                 Map<String, Object> model = new HashMap<String, Object>();
57                 ObjectMapper mapper = new ObjectMapper();       
58                 
59                 try {
60                         model.put("availableRoleFunctions", mapper.writeValueAsString(service.getRoleFunctions()));
61                 } catch (Exception e) {
62                         logger.error(EELFLoggerDelegate.errorLogger, "welcome failed", e);
63                 }
64                 
65                 return new ModelAndView(getViewName(),model);           
66         }
67         
68         @RequestMapping(value = {"/get_role_functions" }, method = RequestMethod.GET)
69         public void getRoleFunctionList(HttpServletRequest request,HttpServletResponse response) {
70                 Map<String, Object> model = new HashMap<String, Object>();
71                 ObjectMapper mapper = new ObjectMapper();       
72                 
73                 try {
74                         model.put("availableRoleFunctions", mapper.writeValueAsString(service.getRoleFunctions()));
75                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
76                         JSONObject j = new JSONObject(msg);
77                         response.getWriter().write(j.toString());       
78                 } catch (Exception e) {
79                         logger.error(EELFLoggerDelegate.errorLogger, "getROleFunctionList failed", e);
80                 }
81                 
82         }
83         
84         @RequestMapping(value = {"/role_function_list/saveRoleFunction" }, method = RequestMethod.POST)
85         public void saveRoleFunction(HttpServletRequest request, 
86                         HttpServletResponse response, @RequestBody String roleFunc) throws Exception {
87                 ObjectMapper mapper = new ObjectMapper();
88                 String restCallStatus = "";
89                 try {
90                         String data = roleFunc;
91                         RoleFunction availableRoleFunction = mapper.readValue(data, RoleFunction.class);                
92                         String code = availableRoleFunction.getCode();
93                         RoleFunction domainRoleFunction = service.getRoleFunction(code);
94                         domainRoleFunction.setName(availableRoleFunction.getName());
95                         domainRoleFunction.setCode(code); 
96                         restCallStatus="success";
97                         service.saveRoleFunction(domainRoleFunction);
98                 } catch (Exception e) {
99                         restCallStatus="fail";
100                         logger.error(EELFLoggerDelegate.errorLogger, "saveRoleFunction failed", e);
101                 }
102                 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(restCallStatus));
103                 JSONObject j = new JSONObject(msg);
104                 response.getWriter().write(j.toString());
105         }
106         
107         @RequestMapping(value = {"/role_function_list/addRoleFunction" }, method = RequestMethod.POST)
108         public void addRoleFunction(HttpServletRequest request, 
109                         HttpServletResponse response, @RequestBody String roleFunc) throws Exception {
110                 ObjectMapper mapper = new ObjectMapper();
111                 String restCallStatus = "";
112                 boolean canSave=true;
113                 try {
114                         String data = roleFunc;
115                         RoleFunction availableRoleFunction = mapper.readValue(data, RoleFunction.class);                
116                         String code = availableRoleFunction.getCode();
117                         RoleFunction domainRoleFunction = service.getRoleFunction(code);
118                         domainRoleFunction.setName(availableRoleFunction.getName());
119                         domainRoleFunction.setCode(code); 
120                         List<RoleFunction> currentRoleFunction = service.getRoleFunctions();
121                         restCallStatus="success";
122                         for(RoleFunction roleF:currentRoleFunction){
123                                 if(roleF.getCode().equals(code)){
124                                         restCallStatus="code exists";
125                                         canSave=false;
126                                         break;
127                                 }
128                         }
129                         if(canSave)
130                                 service.saveRoleFunction(domainRoleFunction);
131                 } catch (Exception e) {
132                         restCallStatus="fail";
133                         logger.error(EELFLoggerDelegate.errorLogger, "addRoleFunction failed", e);
134                 }
135                 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(restCallStatus));
136                 JSONObject j = new JSONObject(msg);
137                 response.getWriter().write(j.toString());
138         }
139
140         @RequestMapping(value = {"/role_function_list/removeRoleFunction" }, method = RequestMethod.POST)
141         public void removeRoleFunction(HttpServletRequest request, 
142                         HttpServletResponse response, @RequestBody String roleFunc) throws Exception {
143                 ObjectMapper mapper = new ObjectMapper();
144                 String restCallStatus = "";
145                 try {
146                         String data = roleFunc;
147                 
148                         RoleFunction availableRoleFunction = mapper.readValue(data, RoleFunction.class);
149
150                         RoleFunction domainRoleFunction = service.getRoleFunction(availableRoleFunction.getCode());
151                         
152                         service.deleteRoleFunction(domainRoleFunction);
153                         logger.info(EELFLoggerDelegate.auditLogger, "Remove role function " + domainRoleFunction.getName());
154                         restCallStatus="success";
155                 } catch (Exception e) {
156                         restCallStatus="fail";
157                         logger.error(EELFLoggerDelegate.errorLogger, "removeRoleFunction failed", 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         public String getViewName() {
165                 return viewName;
166         }
167         public void setViewName(String viewName) {
168                 this.viewName = viewName;
169         }
170         
171         
172 }