2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
38 package org.onap.portalapp.portal.service;
40 import java.util.HashMap;
41 import java.util.Iterator;
42 import java.util.List;
45 import java.util.TreeSet;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.context.annotation.EnableAspectJAutoProxy;
48 import org.springframework.stereotype.Service;
49 import org.springframework.transaction.annotation.Transactional;
50 import org.onap.portalapp.portal.domain.EPRole;
51 import org.onap.portalapp.portal.logging.aop.EPMetricsLog;
52 import org.onap.portalapp.portal.utils.PortalConstants;
53 import org.onap.portalsdk.core.domain.RoleFunction;
54 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
55 import org.onap.portalsdk.core.service.DataAccessService;
57 @Service("epRoleService")
59 @org.springframework.context.annotation.Configuration
60 @EnableAspectJAutoProxy
62 public class EPRoleServiceImpl implements EPRoleService {
63 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPRoleServiceImpl.class);
66 private DataAccessService dataAccessService;
69 ExternalAccessRolesService externalAccessRolesService;
71 @SuppressWarnings("unchecked")
72 public List<RoleFunction> getRoleFunctions() {
73 return getDataAccessService().getList(RoleFunction.class, null);
76 @SuppressWarnings("unchecked")
77 public List<EPRole> getAvailableChildRoles(Long roleId) {
78 List<EPRole> availableChildRoles =
79 (List<EPRole>) getDataAccessService().getList(EPRole.class, null);
80 if (roleId == null || roleId == 0) {
81 return availableChildRoles;
85 (EPRole) getDataAccessService().getDomainObject(EPRole.class, roleId, null);
86 Set<EPRole> allParentRoles = new TreeSet<>();
87 allParentRoles = getAllParentRolesAsList(currentRole, allParentRoles);
89 Iterator<EPRole> availableChildRolesIterator = availableChildRoles.iterator();
90 while (availableChildRolesIterator.hasNext()) {
91 EPRole role = availableChildRolesIterator.next();
92 if (!role.getActive() || allParentRoles.contains(role) || role.getId().equals(roleId)) {
93 availableChildRolesIterator.remove();
96 return availableChildRoles;
99 private Set<EPRole> getAllParentRolesAsList(EPRole role, Set<EPRole> allParentRoles) {
100 Set<EPRole> parentRoles = role.getParentRoles();
101 allParentRoles.addAll(parentRoles);
102 Iterator<EPRole> parentRolesIterator = parentRoles.iterator();
103 while (parentRolesIterator.hasNext()) {
104 getAllParentRolesAsList(parentRolesIterator.next(), allParentRoles);
106 return allParentRoles;
109 public RoleFunction getRoleFunction(String code) {
110 return (RoleFunction) getDataAccessService().getDomainObject(RoleFunction.class, code,
114 public void saveRoleFunction(RoleFunction domainRoleFunction) {
115 getDataAccessService().saveDomainObject(domainRoleFunction, null);
118 public void deleteRoleFunction(RoleFunction domainRoleFunction) {
119 getDataAccessService().deleteDomainObject(domainRoleFunction, null);
122 public EPRole getRole(Long id) {
123 return (EPRole) getDataAccessService().getDomainObject(EPRole.class, id, null);
127 private static final String GET_APP_ROLE_SQL_FORMAT =
128 "SELECT * FROM fn_role where APP_ID = %s AND APP_ROLE_ID = %s";
130 @SuppressWarnings("unchecked")
131 public EPRole getRole(Long appId, Long appRoleid) {
132 if (appId == null || appRoleid == null) {
133 logger.error(EELFLoggerDelegate.errorLogger, String.format(
134 "getRole does not support null appId or roleId. appRoleid=%s, appRoleid=%s",
139 String sql = String.format(GET_APP_ROLE_SQL_FORMAT, appId, appRoleid);
142 (List<EPRole>) dataAccessService.executeSQLQuery(sql, EPRole.class, null);
143 int resultsCount = roles.size();
144 if (resultsCount > 1) {
145 logger.error(EELFLoggerDelegate.errorLogger, String.format(
146 "search by appId=%s, appRoleid=%s should have returned 0 or 1 results. Got %d. This is an internal server error.",
147 appId, appRoleid, resultsCount));
148 logger.error(EELFLoggerDelegate.errorLogger,
149 "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.");
151 } else if (resultsCount == 1) {
157 @SuppressWarnings("unchecked")
158 public EPRole getAppRole(String roleName, Long appId) {
160 final Map<String, String> params = new HashMap<>();
161 final Map<String, String> portalParams = new HashMap<>();
162 List<EPRole> roles = null;
163 params.put("appId", appId.toString());
164 params.put("roleName", roleName);
165 portalParams.put("appRoleName", roleName);
167 List<EPRole> roleList = externalAccessRolesService
168 .getPortalAppRoleInfo(PortalConstants.ACCOUNT_ADMIN_ROLE_ID);
169 EPRole role = new EPRole();
170 if (!roleList.isEmpty()) {
171 role = roleList.get(0);
173 logger.debug(EELFLoggerDelegate.debugLogger, "Requested RoleName is " + role.getName());
175 if (appId == 1 || roleName.equals(role.getName())) {
176 roles = (List<EPRole>) dataAccessService.executeNamedQuery("getPortalAppRoles",
178 } else if (appId != 1 && !roleName.equals(role.getName())) {
179 roles = (List<EPRole>) dataAccessService.executeNamedQuery("getAppRoles", params, null);
181 int resultsCount = (roles == null ? 0 : roles.size());
182 if (resultsCount > 1) {
183 logger.error(EELFLoggerDelegate.errorLogger,
184 "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.");
188 } else if (resultsCount == 1) {
194 public void saveRole(EPRole domainRole) {
195 getDataAccessService().saveDomainObject(domainRole, null);
198 public void deleteRole(EPRole domainRole) {
199 getDataAccessService().deleteDomainObject(domainRole, null);
202 @SuppressWarnings("unchecked")
203 public List<EPRole> getAvailableRoles() {
204 return getDataAccessService().getList(EPRole.class, null);
207 public DataAccessService getDataAccessService() {
208 return dataAccessService;
211 public void setDataAccessService(DataAccessService dataAccessService) {
212 this.dataAccessService = dataAccessService;