Replace ecomp references
[portal.git] / ecomp-portal-BE-common / src / main / java / org / onap / portalapp / portal / service / EPRoleFunctionServiceCentralizedImpl.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 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.portal.service;
39
40 import java.util.ArrayList;
41 import java.util.HashMap;
42 import java.util.HashSet;
43 import java.util.List;
44 import java.util.Map;
45 import java.util.Set;
46 import java.util.regex.Pattern;
47 import java.util.stream.Collectors;
48
49 import javax.servlet.http.HttpServletRequest;
50 import javax.servlet.http.HttpSession;
51
52 import org.apache.commons.codec.DecoderException;
53 import org.apache.commons.codec.binary.Hex;
54 import org.hibernate.SessionFactory;
55 import org.onap.portalapp.portal.controller.RoleManageController;
56 import org.onap.portalapp.portal.domain.CentralV2RoleFunction;
57 import org.onap.portalapp.portal.domain.EPRole;
58 import org.onap.portalapp.portal.domain.EPUser;
59 import org.onap.portalapp.portal.exceptions.RoleFunctionException;
60 import org.onap.portalapp.portal.utils.EcompPortalUtils;
61 import org.onap.portalapp.util.EPUserUtils;
62 import org.onap.portalsdk.core.domain.RoleFunction;
63 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
64 import org.onap.portalsdk.core.service.DataAccessService;
65 import org.onap.portalsdk.core.util.SystemProperties;
66 import org.springframework.beans.factory.annotation.Autowired;
67 import org.springframework.transaction.annotation.Transactional;
68
69
70 @Transactional
71 public class EPRoleFunctionServiceCentralizedImpl implements EPRoleFunctionService{
72
73         private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPRoleFunctionServiceCentralizedImpl.class);
74
75         @Autowired
76         private DataAccessService dataAccessService;
77         
78         @Autowired
79         private  SessionFactory sessionFactory;
80         
81         @Autowired
82         private  ExternalAccessRolesService externalAccessRolesService;
83         
84         @SuppressWarnings("unchecked")
85         @Override
86         public List<RoleFunction> getRoleFunctions() {
87                 List<CentralV2RoleFunction> getRoleFuncList = null;
88                 List<RoleFunction> getRoleFuncListOfPortal = new ArrayList<>();
89                 final Map<String, Long> params = new HashMap<>();
90                 params.put("appId", (long) 1);
91                 //Sync all functions from external system into ONAP portal DB
92                 getRoleFuncList = dataAccessService.executeNamedQuery("getAllRoleFunctions", params, null);
93                 for(CentralV2RoleFunction roleFunction : getRoleFuncList)
94                 {
95                         RoleFunction roleFun = new RoleFunction();
96                         String code = EcompPortalUtils.getFunctionCode(roleFunction.getCode());
97                         roleFun.setCode(code);
98                         roleFun.setName(roleFunction.getName());
99                         getRoleFuncListOfPortal.add(roleFun);
100                 }
101                 return getRoleFuncListOfPortal;
102         }
103
104         @SuppressWarnings({ "unchecked", "rawtypes" })
105         @Override
106         public Set getRoleFunctions(HttpServletRequest request, EPUser user) throws RoleFunctionException {
107                 HttpSession session = request.getSession();
108                 String userId = user.getId().toString();
109                 final Map<String, String> params = new HashMap<>();
110                 params.put("userId", userId);
111                 List getRoleFuncListOfPortal = dataAccessService.executeNamedQuery("getRoleFunctionsOfUser", params, null);
112                 Set<String> getRoleFuncListOfPortalSet = new HashSet<>(getRoleFuncListOfPortal);
113                 Set<String> roleFunSet = new HashSet<>();
114                 roleFunSet = getRoleFuncListOfPortalSet.stream().filter(x -> x.contains("|")).collect(Collectors.toSet());
115                 if (roleFunSet.size() > 0)
116                         for (String roleFunction : roleFunSet) {
117                                 String roleFun = EcompPortalUtils.getFunctionCode(roleFunction);
118                                 getRoleFuncListOfPortalSet.remove(roleFunction);
119                                 getRoleFuncListOfPortalSet.add(roleFun);
120                         }
121
122                 Set<String> finalRoleFunctionSet = new HashSet<>();
123                 for (String roleFn : getRoleFuncListOfPortalSet) {
124                         finalRoleFunctionSet.add(EPUserUtils.decodeFunctionCode(roleFn));
125                 }
126                 session.setAttribute(SystemProperties.getProperty(SystemProperties.ROLE_FUNCTIONS_ATTRIBUTE_NAME),
127                                 finalRoleFunctionSet);
128                 logger.debug(EELFLoggerDelegate.debugLogger,
129                                 "getRoleFunctions: RoleFunctions Of User" + finalRoleFunctionSet);
130                 return finalRoleFunctionSet;
131         }
132 }