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