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.Iterator;
23 import java.util.List;
25 import java.util.TreeSet;
27 import org.openecomp.portalapp.portal.domain.EPRole;
28 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
29 import org.openecomp.portalsdk.core.domain.RoleFunction;
30 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
31 import org.openecomp.portalsdk.core.service.DataAccessService;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.context.annotation.EnableAspectJAutoProxy;
34 import org.springframework.stereotype.Service;
35 import org.springframework.transaction.annotation.Transactional;
37 @Service("epRoleService")
39 @org.springframework.context.annotation.Configuration
40 @EnableAspectJAutoProxy
42 public class EPRoleServiceImpl implements EPRoleService{
43 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPRoleServiceImpl.class);
46 private DataAccessService dataAccessService;
48 @SuppressWarnings("unchecked")
49 public List<RoleFunction> getRoleFunctions() {
50 //List msgDB = getDataAccessService().getList(Profile.class, null);
51 return getDataAccessService().getList(RoleFunction.class, null);
54 @SuppressWarnings("unchecked")
55 public List<EPRole> getAvailableChildRoles(Long roleId) {
56 List<EPRole> availableChildRoles = (List<EPRole>)getDataAccessService().getList(EPRole.class,null);
57 if(roleId==null || roleId==0){
58 return availableChildRoles;
61 EPRole currentRole = (EPRole)getDataAccessService().getDomainObject(EPRole.class,roleId,null);
62 Set<EPRole> allParentRoles = new TreeSet<EPRole>();
63 allParentRoles = getAllParentRolesAsList(currentRole, allParentRoles);
66 Iterator<EPRole> availableChildRolesIterator = availableChildRoles.iterator();
67 while (availableChildRolesIterator.hasNext()) {
68 EPRole role = availableChildRolesIterator.next();
69 if(!role.getActive() || allParentRoles.contains(role) || role.getId().equals(roleId)){
70 availableChildRolesIterator.remove();
73 return availableChildRoles;
76 private Set<EPRole> getAllParentRolesAsList(EPRole role, Set<EPRole> allParentRoles) {
77 Set<EPRole> parentRoles = role.getParentRoles();
78 allParentRoles.addAll(parentRoles);
79 Iterator<EPRole> parentRolesIterator = parentRoles.iterator();
80 while (parentRolesIterator.hasNext()) {
81 getAllParentRolesAsList(parentRolesIterator.next(),allParentRoles);
83 return allParentRoles;
86 public RoleFunction getRoleFunction(String code) {
87 return (RoleFunction)getDataAccessService().getDomainObject(RoleFunction.class, code, null);
90 public void saveRoleFunction(RoleFunction domainRoleFunction) {
91 getDataAccessService().saveDomainObject(domainRoleFunction, null);
94 public void deleteRoleFunction(RoleFunction domainRoleFunction) {
95 getDataAccessService().deleteDomainObject(domainRoleFunction, null);
98 public EPRole getRole(Long id) {
99 return (EPRole)getDataAccessService().getDomainObject(EPRole.class, id, null);
103 private static final String getAppRoleSqlFormat ="SELECT * FROM fn_role where APP_ID = %s AND APP_ROLE_ID = %s";
105 @SuppressWarnings("unchecked")
106 public EPRole getRole(Long appId, Long appRoleid) {
107 if(appId == null || appRoleid == null){
108 logger.error(EELFLoggerDelegate.errorLogger, String.format("getRole does not support null appId or roleId. appRoleid=%s, appRoleid=%s", appId, appRoleid));
112 String sql = String.format(getAppRoleSqlFormat, appId, appRoleid);
114 List<EPRole> roles = (List<EPRole>)dataAccessService.executeSQLQuery(sql, EPRole.class, null);
115 int resultsCount = roles.size();
116 if(resultsCount > 1){
117 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));
118 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.");
120 } else if(resultsCount == 1){
126 public void saveRole(EPRole domainRole) {
127 getDataAccessService().saveDomainObject(domainRole, null);
130 public void deleteRole(EPRole domainRole) {
131 getDataAccessService().deleteDomainObject(domainRole, null);
134 @SuppressWarnings("unchecked")
135 public List<EPRole> getAvailableRoles() {
136 return getDataAccessService().getList(EPRole.class, null);
139 public DataAccessService getDataAccessService() {
140 return dataAccessService;
144 public void setDataAccessService(DataAccessService dataAccessService) {
145 this.dataAccessService = dataAccessService;