X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=ecomp-portal-BE-common%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fportalapp%2Fportal%2Fservice%2FEPRoleServiceImpl.java;h=4adf1fdb213a87618a58f67ebc6befa2e6382f88;hb=a94388ed941c82a38b2e631cc4fd77bac212335b;hp=92cbe90e95912a1012c4aeaefea742d850a82f64;hpb=ace89e59c9838b666ddd02c08fd5e39a94eee39a;p=portal.git diff --git a/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/service/EPRoleServiceImpl.java b/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/service/EPRoleServiceImpl.java index 92cbe90e..4adf1fdb 100644 --- a/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/service/EPRoleServiceImpl.java +++ b/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/service/EPRoleServiceImpl.java @@ -43,7 +43,6 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeSet; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.EnableAspectJAutoProxy; import org.springframework.stereotype.Service; @@ -61,146 +60,154 @@ import org.onap.portalsdk.core.service.DataAccessService; @EnableAspectJAutoProxy @EPMetricsLog public class EPRoleServiceImpl implements EPRoleService { - EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPRoleServiceImpl.class); - - @Autowired - private DataAccessService dataAccessService; - - @Autowired - ExternalAccessRolesService externalAccessRolesService; - - @SuppressWarnings("unchecked") - public List getRoleFunctions() { - // List msgDB = getDataAccessService().getList(Profile.class, null); - return getDataAccessService().getList(RoleFunction.class, null); - } - - @SuppressWarnings("unchecked") - public List getAvailableChildRoles(Long roleId) { - List availableChildRoles = (List) getDataAccessService().getList(EPRole.class, null); - if (roleId == null || roleId == 0) { - return availableChildRoles; - } - - EPRole currentRole = (EPRole) getDataAccessService().getDomainObject(EPRole.class, roleId, null); - Set allParentRoles = new TreeSet(); - allParentRoles = getAllParentRolesAsList(currentRole, allParentRoles); - - Iterator availableChildRolesIterator = availableChildRoles.iterator(); - while (availableChildRolesIterator.hasNext()) { - EPRole role = availableChildRolesIterator.next(); - if (!role.getActive() || allParentRoles.contains(role) || role.getId().equals(roleId)) { - availableChildRolesIterator.remove(); - } - } - return availableChildRoles; - } - - private Set getAllParentRolesAsList(EPRole role, Set allParentRoles) { - Set parentRoles = role.getParentRoles(); - allParentRoles.addAll(parentRoles); - Iterator parentRolesIterator = parentRoles.iterator(); - while (parentRolesIterator.hasNext()) { - getAllParentRolesAsList(parentRolesIterator.next(), allParentRoles); - } - return allParentRoles; - } - - public RoleFunction getRoleFunction(String code) { - return (RoleFunction) getDataAccessService().getDomainObject(RoleFunction.class, code, null); - } - - public void saveRoleFunction(RoleFunction domainRoleFunction) { - getDataAccessService().saveDomainObject(domainRoleFunction, null); - } - - public void deleteRoleFunction(RoleFunction domainRoleFunction) { - getDataAccessService().deleteDomainObject(domainRoleFunction, null); - } - - public EPRole getRole(Long id) { - return (EPRole) getDataAccessService().getDomainObject(EPRole.class, id, null); - } - - // TODO: refactor - private static final String getAppRoleSqlFormat = "SELECT * FROM fn_role where APP_ID = %s AND APP_ROLE_ID = %s"; - - @SuppressWarnings("unchecked") - public EPRole getRole(Long appId, Long appRoleid) { - if (appId == null || appRoleid == null) { - logger.error(EELFLoggerDelegate.errorLogger, String.format( - "getRole does not support null appId or roleId. appRoleid=%s, appRoleid=%s", appId, appRoleid)); - return null; - } - - String sql = String.format(getAppRoleSqlFormat, appId, appRoleid); - - List roles = (List) dataAccessService.executeSQLQuery(sql, EPRole.class, null); - int resultsCount = roles.size(); - if (resultsCount > 1) { - 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)); - 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."); - return roles.get(0); - } else if (resultsCount == 1) { - return roles.get(0); - } - return null; - } - - @SuppressWarnings("unchecked") - public EPRole getAppRole(String roleName, Long appId) { - - final Map params = new HashMap(); - final Map portalParams = new HashMap(); - List roles = null; - params.put("appId", appId.toString()); - params.put("roleName", roleName); - portalParams.put("appRoleName", roleName); - - List roleList = externalAccessRolesService.getPortalAppRoleInfo(PortalConstants.ACCOUNT_ADMIN_ROLE_ID); - EPRole role = new EPRole(); - if(roleList.size()>0){ - role = roleList.get(0);} - logger.debug(EELFLoggerDelegate.debugLogger, "Requested RoleName is "+role.getName()); - - if (appId == 1 || roleName.equals(role.getName())) { - roles = (List) dataAccessService.executeNamedQuery("getPortalAppRoles", portalParams, null); - } else if (appId != 1 && !roleName.equals(role.getName())) { - roles = (List) dataAccessService.executeNamedQuery("getAppRoles", params, null); - } - int resultsCount = (roles == null ? 0 : roles.size()); - if (resultsCount > 1) { - 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."); - return roles.get(0); - } else if (resultsCount == 1) { - return roles.get(0); - } - return null; - } - - public void saveRole(EPRole domainRole) { - getDataAccessService().saveDomainObject(domainRole, null); - } - - public void deleteRole(EPRole domainRole) { - getDataAccessService().deleteDomainObject(domainRole, null); - } - - @SuppressWarnings("unchecked") - public List getAvailableRoles() { - return getDataAccessService().getList(EPRole.class, null); - } - - public DataAccessService getDataAccessService() { - return dataAccessService; - } - - public void setDataAccessService(DataAccessService dataAccessService) { - this.dataAccessService = dataAccessService; - } + EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPRoleServiceImpl.class); + + private static final String GET_APP_ROLE_SQL_FORMAT = + "SELECT * FROM fn_role where APP_ID = %s AND APP_ROLE_ID = %s"; + + @Autowired + private DataAccessService dataAccessService; + + @Autowired + ExternalAccessRolesService externalAccessRolesService; + + @SuppressWarnings("unchecked") + public List getRoleFunctions() { + return getDataAccessService().getList(RoleFunction.class, null); + } + + @SuppressWarnings("unchecked") + public List getAvailableChildRoles(Long roleId) { + List availableChildRoles = + (List) getDataAccessService().getList(EPRole.class, null); + if (roleId == null || roleId == 0) { + return availableChildRoles; + } + + EPRole currentRole = + (EPRole) getDataAccessService().getDomainObject(EPRole.class, roleId, null); + Set allParentRoles = new TreeSet<>(); + allParentRoles = getAllParentRolesAsList(currentRole, allParentRoles); + + Iterator availableChildRolesIterator = availableChildRoles.iterator(); + while (availableChildRolesIterator.hasNext()) { + EPRole role = availableChildRolesIterator.next(); + if (!role.getActive() || allParentRoles.contains(role) || role.getId().equals(roleId)) { + availableChildRolesIterator.remove(); + } + } + return availableChildRoles; + } + + private Set getAllParentRolesAsList(EPRole role, Set allParentRoles) { + Set parentRoles = role.getParentRoles(); + allParentRoles.addAll(parentRoles); + Iterator parentRolesIterator = parentRoles.iterator(); + while (parentRolesIterator.hasNext()) { + getAllParentRolesAsList(parentRolesIterator.next(), allParentRoles); + } + return allParentRoles; + } + + public RoleFunction getRoleFunction(String code) { + return (RoleFunction) getDataAccessService().getDomainObject(RoleFunction.class, code, + null); + } + + public void saveRoleFunction(RoleFunction domainRoleFunction) { + getDataAccessService().saveDomainObject(domainRoleFunction, null); + } + + public void deleteRoleFunction(RoleFunction domainRoleFunction) { + getDataAccessService().deleteDomainObject(domainRoleFunction, null); + } + + public EPRole getRole(Long id) { + return (EPRole) getDataAccessService().getDomainObject(EPRole.class, id, null); + } + + @SuppressWarnings("unchecked") + public EPRole getRole(Long appId, Long appRoleid) { + if (appId == null || appRoleid == null) { + logger.error(EELFLoggerDelegate.errorLogger, String.format( + "getRole does not support null appId or roleId. appRoleid=%s, appRoleid=%s", + appId, appRoleid)); + return null; + } + + String sql = String.format(GET_APP_ROLE_SQL_FORMAT, appId, appRoleid); + + List roles = + (List) dataAccessService.executeSQLQuery(sql, EPRole.class, null); + int resultsCount = roles.size(); + if (resultsCount > 1) { + 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)); + 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."); + return roles.get(0); + } else if (resultsCount == 1) { + return roles.get(0); + } + return null; + } + + @SuppressWarnings("unchecked") + public EPRole getAppRole(String roleName, Long appId) { + + final Map params = new HashMap<>(); + final Map portalParams = new HashMap<>(); + List roles = null; + params.put("appId", appId.toString()); + params.put("roleName", roleName); + portalParams.put("appRoleName", roleName); + + List roleList = externalAccessRolesService + .getPortalAppRoleInfo(PortalConstants.ACCOUNT_ADMIN_ROLE_ID); + EPRole role = new EPRole(); + if (!roleList.isEmpty()) { + role = roleList.get(0); + } + logger.debug(EELFLoggerDelegate.debugLogger, "Requested RoleName is " + role.getName()); + + if (appId == 1 || roleName.equals(role.getName())) { + roles = (List) dataAccessService.executeNamedQuery("getPortalAppRoles", + portalParams, null); + } else if (appId != 1 && !roleName.equals(role.getName())) { + roles = (List) dataAccessService.executeNamedQuery("getAppRoles", params, null); + } + int resultsCount = (roles == null ? 0 : roles.size()); + if (resultsCount > 1) { + 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."); + if (roles != null) { + return roles.get(0); + } + } else if (roles!=null && resultsCount == 1) { + return roles.get(0); + } + return null; + } + + public void saveRole(EPRole domainRole) { + getDataAccessService().saveDomainObject(domainRole, null); + } + + public void deleteRole(EPRole domainRole) { + getDataAccessService().deleteDomainObject(domainRole, null); + } + + @SuppressWarnings("unchecked") + public List getAvailableRoles() { + return getDataAccessService().getList(EPRole.class, null); + } + + public DataAccessService getDataAccessService() { + return dataAccessService; + } + + public void setDataAccessService(DataAccessService dataAccessService) { + this.dataAccessService = dataAccessService; + } }