1821421a2a96e215112dd5ea2e089500f054a951
[portal.git] / ecomp-portal-BE-common / src / main / java / org / onap / portalapp / portal / service / EPRoleServiceImpl.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.HashMap;
41 import java.util.Iterator;
42 import java.util.List;
43 import java.util.Map;
44 import java.util.Set;
45 import java.util.TreeSet;
46
47 import org.springframework.beans.factory.annotation.Autowired;
48 import org.springframework.context.annotation.EnableAspectJAutoProxy;
49 import org.springframework.stereotype.Service;
50 import org.springframework.transaction.annotation.Transactional;
51 import org.onap.portalapp.portal.domain.EPRole;
52 import org.onap.portalapp.portal.logging.aop.EPMetricsLog;
53 import org.onap.portalapp.portal.utils.PortalConstants;
54 import org.onap.portalsdk.core.domain.RoleFunction;
55 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
56 import org.onap.portalsdk.core.service.DataAccessService;
57
58 @Service("epRoleService")
59 @Transactional
60 @org.springframework.context.annotation.Configuration
61 @EnableAspectJAutoProxy
62 @EPMetricsLog
63 public class EPRoleServiceImpl implements EPRoleService {
64         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPRoleServiceImpl.class);
65
66         @Autowired
67         private DataAccessService dataAccessService;
68
69         @SuppressWarnings("unchecked")
70         public List<RoleFunction> getRoleFunctions() {
71                 // List msgDB = getDataAccessService().getList(Profile.class, null);
72                 return getDataAccessService().getList(RoleFunction.class, null);
73         }
74
75         @SuppressWarnings("unchecked")
76         public List<EPRole> getAvailableChildRoles(Long roleId) {
77                 List<EPRole> availableChildRoles = (List<EPRole>) getDataAccessService().getList(EPRole.class, null);
78                 if (roleId == null || roleId == 0) {
79                         return availableChildRoles;
80                 }
81
82                 EPRole currentRole = (EPRole) getDataAccessService().getDomainObject(EPRole.class, roleId, null);
83                 Set<EPRole> allParentRoles = new TreeSet<EPRole>();
84                 allParentRoles = getAllParentRolesAsList(currentRole, allParentRoles);
85
86                 Iterator<EPRole> availableChildRolesIterator = availableChildRoles.iterator();
87                 while (availableChildRolesIterator.hasNext()) {
88                         EPRole role = availableChildRolesIterator.next();
89                         if (!role.getActive() || allParentRoles.contains(role) || role.getId().equals(roleId)) {
90                                 availableChildRolesIterator.remove();
91                         }
92                 }
93                 return availableChildRoles;
94         }
95
96         private Set<EPRole> getAllParentRolesAsList(EPRole role, Set<EPRole> allParentRoles) {
97                 Set<EPRole> parentRoles = role.getParentRoles();
98                 allParentRoles.addAll(parentRoles);
99                 Iterator<EPRole> parentRolesIterator = parentRoles.iterator();
100                 while (parentRolesIterator.hasNext()) {
101                         getAllParentRolesAsList(parentRolesIterator.next(), allParentRoles);
102                 }
103                 return allParentRoles;
104         }
105
106         public RoleFunction getRoleFunction(String code) {
107                 return (RoleFunction) getDataAccessService().getDomainObject(RoleFunction.class, code, null);
108         }
109
110         public void saveRoleFunction(RoleFunction domainRoleFunction) {
111                 getDataAccessService().saveDomainObject(domainRoleFunction, null);
112         }
113
114         public void deleteRoleFunction(RoleFunction domainRoleFunction) {
115                 getDataAccessService().deleteDomainObject(domainRoleFunction, null);
116         }
117
118         public EPRole getRole(Long id) {
119                 return (EPRole) getDataAccessService().getDomainObject(EPRole.class, id, null);
120         }
121
122         // TODO: refactor
123         private static final String getAppRoleSqlFormat = "SELECT * FROM fn_role where APP_ID = %s AND APP_ROLE_ID = %s";
124
125         @SuppressWarnings("unchecked")
126         public EPRole getRole(Long appId, Long appRoleid) {
127                 if (appId == null || appRoleid == null) {
128                         logger.error(EELFLoggerDelegate.errorLogger, String.format(
129                                         "getRole does not support null appId or roleId. appRoleid=%s, appRoleid=%s", appId, appRoleid));
130                         return null;
131                 }
132
133                 String sql = String.format(getAppRoleSqlFormat, appId, appRoleid);
134
135                 List<EPRole> roles = (List<EPRole>) dataAccessService.executeSQLQuery(sql, EPRole.class, null);
136                 int resultsCount = roles.size();
137                 if (resultsCount > 1) {
138                         logger.error(EELFLoggerDelegate.errorLogger,
139                                         String.format(
140                                                         "search by appId=%s, appRoleid=%s should have returned 0 or 1 results. Got %d. This is an internal server error.",
141                                                         appId, appRoleid, resultsCount));
142                         logger.error(EELFLoggerDelegate.errorLogger,
143                                         "Trying to recover from duplicates by returning the first search result. This issue should be treated, it is probably not critical because duplicate roles should be similar.");
144                         return roles.get(0);
145                 } else if (resultsCount == 1) {
146                         return roles.get(0);
147                 }
148                 return null;
149         }
150
151         @SuppressWarnings("unchecked")
152         public EPRole getAppRole(String roleName, Long appId) {
153
154                 final Map<String, String> params = new HashMap<String, String>();
155                 final Map<String, String> portalParams = new HashMap<String, String>();
156                 List<EPRole> roles = null;
157                 params.put("appId", appId.toString());
158                 params.put("roleName", roleName);
159                 portalParams.put("appRoleName", roleName);
160                 if (appId == 1 || roleName.equals(PortalConstants.ADMIN_ROLE)) {
161                         roles = (List<EPRole>) dataAccessService.executeNamedQuery("getPortalAppRoles", portalParams, null);
162                 } else if (appId != 1 && !roleName.equals(PortalConstants.ADMIN_ROLE)) {
163                         roles = (List<EPRole>) dataAccessService.executeNamedQuery("getAppRoles", params, null);
164                 }
165                 int resultsCount = (roles == null ? 0 : roles.size());
166                 if (resultsCount > 1) {
167                         logger.error(EELFLoggerDelegate.errorLogger,
168                                         "Trying to recover from duplicates by returning the first search result. This issue should be treated, it is probably not critical because duplicate roles should be similar.");
169                         return roles.get(0);
170                 } else if (resultsCount == 1) {
171                         return roles.get(0);
172                 }
173                 return null;
174         }
175
176         public void saveRole(EPRole domainRole) {
177                 getDataAccessService().saveDomainObject(domainRole, null);
178         }
179
180         public void deleteRole(EPRole domainRole) {
181                 getDataAccessService().deleteDomainObject(domainRole, null);
182         }
183
184         @SuppressWarnings("unchecked")
185         public List<EPRole> getAvailableRoles() {
186                 return getDataAccessService().getList(EPRole.class, null);
187         }
188
189         public DataAccessService getDataAccessService() {
190                 return dataAccessService;
191         }
192
193         public void setDataAccessService(DataAccessService dataAccessService) {
194                 this.dataAccessService = dataAccessService;
195         }
196 }