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