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;
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;
58 @Service("epRoleService")
60 @org.springframework.context.annotation.Configuration
61 @EnableAspectJAutoProxy
63 public class EPRoleServiceImpl implements EPRoleService {
64 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPRoleServiceImpl.class);
67 private DataAccessService dataAccessService;
70 ExternalAccessRolesService externalAccessRolesService;
72 @SuppressWarnings("unchecked")
73 public List<RoleFunction> getRoleFunctions() {
74 // List msgDB = getDataAccessService().getList(Profile.class, null);
75 return getDataAccessService().getList(RoleFunction.class, null);
78 @SuppressWarnings("unchecked")
79 public List<EPRole> getAvailableChildRoles(Long roleId) {
80 List<EPRole> availableChildRoles = (List<EPRole>) getDataAccessService().getList(EPRole.class, null);
81 if (roleId == null || roleId == 0) {
82 return availableChildRoles;
85 EPRole currentRole = (EPRole) getDataAccessService().getDomainObject(EPRole.class, roleId, null);
86 Set<EPRole> allParentRoles = new TreeSet<EPRole>();
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, null);
113 public void saveRoleFunction(RoleFunction domainRoleFunction) {
114 getDataAccessService().saveDomainObject(domainRoleFunction, null);
117 public void deleteRoleFunction(RoleFunction domainRoleFunction) {
118 getDataAccessService().deleteDomainObject(domainRoleFunction, null);
121 public EPRole getRole(Long id) {
122 return (EPRole) getDataAccessService().getDomainObject(EPRole.class, id, null);
126 private static final String getAppRoleSqlFormat = "SELECT * FROM fn_role where APP_ID = %s AND APP_ROLE_ID = %s";
128 @SuppressWarnings("unchecked")
129 public EPRole getRole(Long appId, Long appRoleid) {
130 if (appId == null || appRoleid == null) {
131 logger.error(EELFLoggerDelegate.errorLogger, String.format(
132 "getRole does not support null appId or roleId. appRoleid=%s, appRoleid=%s", appId, appRoleid));
136 String sql = String.format(getAppRoleSqlFormat, appId, appRoleid);
138 List<EPRole> roles = (List<EPRole>) dataAccessService.executeSQLQuery(sql, EPRole.class, null);
139 int resultsCount = roles.size();
140 if (resultsCount > 1) {
141 logger.error(EELFLoggerDelegate.errorLogger,
143 "search by appId=%s, appRoleid=%s should have returned 0 or 1 results. Got %d. This is an internal server error.",
144 appId, appRoleid, resultsCount));
145 logger.error(EELFLoggerDelegate.errorLogger,
146 "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.");
148 } else if (resultsCount == 1) {
154 @SuppressWarnings("unchecked")
155 public EPRole getAppRole(String roleName, Long appId) {
157 final Map<String, String> params = new HashMap<String, String>();
158 final Map<String, String> portalParams = new HashMap<String, String>();
159 List<EPRole> roles = null;
160 params.put("appId", appId.toString());
161 params.put("roleName", roleName);
162 portalParams.put("appRoleName", roleName);
164 List<EPRole> roleList = externalAccessRolesService.getPortalAppRoleInfo(PortalConstants.ACCOUNT_ADMIN_ROLE_ID);
165 EPRole role = new EPRole();
166 if(roleList.size()>0){
167 role = roleList.get(0);}
168 logger.debug(EELFLoggerDelegate.debugLogger, "Requested RoleName is "+role.getName());
170 if (appId == 1 || roleName.equals(role.getName())) {
171 roles = (List<EPRole>) dataAccessService.executeNamedQuery("getPortalAppRoles", portalParams, null);
172 } else if (appId != 1 && !roleName.equals(role.getName())) {
173 roles = (List<EPRole>) dataAccessService.executeNamedQuery("getAppRoles", params, null);
175 int resultsCount = (roles == null ? 0 : roles.size());
176 if (resultsCount > 1) {
177 logger.error(EELFLoggerDelegate.errorLogger,
178 "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.");
180 } else if (resultsCount == 1) {
186 public void saveRole(EPRole domainRole) {
187 getDataAccessService().saveDomainObject(domainRole, null);
190 public void deleteRole(EPRole domainRole) {
191 getDataAccessService().deleteDomainObject(domainRole, null);
194 @SuppressWarnings("unchecked")
195 public List<EPRole> getAvailableRoles() {
196 return getDataAccessService().getList(EPRole.class, null);
199 public DataAccessService getDataAccessService() {
200 return dataAccessService;
203 public void setDataAccessService(DataAccessService dataAccessService) {
204 this.dataAccessService = dataAccessService;