2 * ================================================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ================================================================================
20 package org.openecomp.portalapp.portal.service;
22 import java.util.HashMap;
23 import java.util.Iterator;
24 import java.util.List;
27 import java.util.TreeSet;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.context.annotation.EnableAspectJAutoProxy;
31 import org.springframework.stereotype.Service;
32 import org.springframework.transaction.annotation.Transactional;
34 import org.openecomp.portalsdk.core.domain.RoleFunction;
35 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
36 import org.openecomp.portalsdk.core.service.DataAccessService;
37 import org.openecomp.portalapp.portal.domain.EPRole;
38 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
39 import org.openecomp.portalapp.portal.utils.PortalConstants;
41 @Service("epRoleService")
43 @org.springframework.context.annotation.Configuration
44 @EnableAspectJAutoProxy
46 public class EPRoleServiceImpl implements EPRoleService {
47 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPRoleServiceImpl.class);
50 private DataAccessService dataAccessService;
52 @SuppressWarnings("unchecked")
53 public List<RoleFunction> getRoleFunctions() {
54 // List msgDB = getDataAccessService().getList(Profile.class, null);
55 return getDataAccessService().getList(RoleFunction.class, null);
58 @SuppressWarnings("unchecked")
59 public List<EPRole> getAvailableChildRoles(Long roleId) {
60 List<EPRole> availableChildRoles = (List<EPRole>) getDataAccessService().getList(EPRole.class, null);
61 if (roleId == null || roleId == 0) {
62 return availableChildRoles;
65 EPRole currentRole = (EPRole) getDataAccessService().getDomainObject(EPRole.class, roleId, null);
66 Set<EPRole> allParentRoles = new TreeSet<EPRole>();
67 allParentRoles = getAllParentRolesAsList(currentRole, allParentRoles);
69 Iterator<EPRole> availableChildRolesIterator = availableChildRoles.iterator();
70 while (availableChildRolesIterator.hasNext()) {
71 EPRole role = availableChildRolesIterator.next();
72 if (!role.getActive() || allParentRoles.contains(role) || role.getId().equals(roleId)) {
73 availableChildRolesIterator.remove();
76 return availableChildRoles;
79 private Set<EPRole> getAllParentRolesAsList(EPRole role, Set<EPRole> allParentRoles) {
80 Set<EPRole> parentRoles = role.getParentRoles();
81 allParentRoles.addAll(parentRoles);
82 Iterator<EPRole> parentRolesIterator = parentRoles.iterator();
83 while (parentRolesIterator.hasNext()) {
84 getAllParentRolesAsList(parentRolesIterator.next(), allParentRoles);
86 return allParentRoles;
89 public RoleFunction getRoleFunction(String code) {
90 return (RoleFunction) getDataAccessService().getDomainObject(RoleFunction.class, code, null);
93 public void saveRoleFunction(RoleFunction domainRoleFunction) {
94 getDataAccessService().saveDomainObject(domainRoleFunction, null);
97 public void deleteRoleFunction(RoleFunction domainRoleFunction) {
98 getDataAccessService().deleteDomainObject(domainRoleFunction, null);
101 public EPRole getRole(Long id) {
102 return (EPRole) getDataAccessService().getDomainObject(EPRole.class, id, null);
106 private static final String getAppRoleSqlFormat = "SELECT * FROM fn_role where APP_ID = %s AND APP_ROLE_ID = %s";
108 @SuppressWarnings("unchecked")
109 public EPRole getRole(Long appId, Long appRoleid) {
110 if (appId == null || appRoleid == null) {
111 logger.error(EELFLoggerDelegate.errorLogger, String.format(
112 "getRole does not support null appId or roleId. appRoleid=%s, appRoleid=%s", appId, appRoleid));
116 String sql = String.format(getAppRoleSqlFormat, appId, appRoleid);
118 List<EPRole> roles = (List<EPRole>) dataAccessService.executeSQLQuery(sql, EPRole.class, null);
119 int resultsCount = roles.size();
120 if (resultsCount > 1) {
121 logger.error(EELFLoggerDelegate.errorLogger,
123 "search by appId=%s, appRoleid=%s should have returned 0 or 1 results. Got %d. This is an internal server error.",
124 appId, appRoleid, resultsCount));
125 logger.error(EELFLoggerDelegate.errorLogger,
126 "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.");
128 } else if (resultsCount == 1) {
134 @SuppressWarnings("unchecked")
135 public EPRole getAppRole(String roleName, Long appId) {
137 final Map<String, String> params = new HashMap<String, String>();
138 final Map<String, String> portalParams = new HashMap<String, String>();
139 List<EPRole> roles = null;
140 params.put("appId", appId.toString());
141 params.put("roleName", roleName);
142 portalParams.put("appRoleName", roleName);
143 if (appId == 1 || roleName.equals(PortalConstants.ADMIN_ROLE)) {
144 roles = (List<EPRole>) dataAccessService.executeNamedQuery("getPortalAppRoles", portalParams, null);
145 } else if (appId != 1 && !roleName.equals(PortalConstants.ADMIN_ROLE)) {
146 roles = (List<EPRole>) dataAccessService.executeNamedQuery("getAppRoles", params, null);
148 int resultsCount = (roles == null ? 0 : roles.size());
149 if (resultsCount > 1) {
150 logger.error(EELFLoggerDelegate.errorLogger,
151 "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.");
153 } else if (resultsCount == 1) {
159 public void saveRole(EPRole domainRole) {
160 getDataAccessService().saveDomainObject(domainRole, null);
163 public void deleteRole(EPRole domainRole) {
164 getDataAccessService().deleteDomainObject(domainRole, null);
167 @SuppressWarnings("unchecked")
168 public List<EPRole> getAvailableRoles() {
169 return getDataAccessService().getList(EPRole.class, null);
172 public DataAccessService getDataAccessService() {
173 return dataAccessService;
176 public void setDataAccessService(DataAccessService dataAccessService) {
177 this.dataAccessService = dataAccessService;