nexus site path corrected
[portal.git] / ecomp-portal-BE / src / main / java / org / openecomp / portalapp / portal / service / EPRoleServiceImpl.java
1 /*-
2  * ================================================================================
3  * eCOMP Portal
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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  * ================================================================================
19  */
20 package org.openecomp.portalapp.portal.service;
21
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.Set;
25 import java.util.TreeSet;
26
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;
36
37 @Service("epRoleService")
38 @Transactional
39 @org.springframework.context.annotation.Configuration
40 @EnableAspectJAutoProxy
41 @EPMetricsLog
42 public class EPRoleServiceImpl implements EPRoleService{
43         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPRoleServiceImpl.class);
44
45         @Autowired
46         private DataAccessService  dataAccessService;
47         
48         @SuppressWarnings("unchecked")  
49         public List<RoleFunction> getRoleFunctions() {
50                 //List msgDB = getDataAccessService().getList(Profile.class, null);
51                 return getDataAccessService().getList(RoleFunction.class, null);
52         }
53         
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;
59                 }
60                 
61                 EPRole currentRole = (EPRole)getDataAccessService().getDomainObject(EPRole.class,roleId,null);
62                 Set<EPRole> allParentRoles = new TreeSet<EPRole>();
63                 allParentRoles = getAllParentRolesAsList(currentRole, allParentRoles);
64
65                 
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();
71                         }
72                 }
73                 return availableChildRoles;
74         }
75         
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);
82                 }
83                 return allParentRoles;
84         }
85         
86         public RoleFunction getRoleFunction(String code) {
87                 return (RoleFunction)getDataAccessService().getDomainObject(RoleFunction.class, code, null);
88         }
89         
90         public void saveRoleFunction(RoleFunction domainRoleFunction) {
91                 getDataAccessService().saveDomainObject(domainRoleFunction, null);
92         }
93         
94         public void deleteRoleFunction(RoleFunction domainRoleFunction) {
95                 getDataAccessService().deleteDomainObject(domainRoleFunction, null);
96         }
97         
98         public EPRole getRole(Long id) {
99                 return (EPRole)getDataAccessService().getDomainObject(EPRole.class, id, null);
100         }
101         
102         // TODO: refactor
103         private static final String getAppRoleSqlFormat ="SELECT * FROM fn_role where APP_ID = %s AND APP_ROLE_ID = %s";
104         
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));
109                         return null;
110                 }
111                 
112                 String sql = String.format(getAppRoleSqlFormat, appId, appRoleid);
113                 
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.");
119                         return roles.get(0);
120                 } else if(resultsCount == 1){
121                         return roles.get(0);
122                 }
123                 return null;
124         }
125         
126         public void saveRole(EPRole domainRole) {
127                 getDataAccessService().saveDomainObject(domainRole, null);
128         }
129         
130         public void deleteRole(EPRole domainRole) {
131                 getDataAccessService().deleteDomainObject(domainRole, null);
132         }
133         
134         @SuppressWarnings("unchecked")
135         public List<EPRole> getAvailableRoles() {
136                 return getDataAccessService().getList(EPRole.class, null);
137         }
138
139         public DataAccessService getDataAccessService() {
140                 return dataAccessService;
141         }
142
143
144         public void setDataAccessService(DataAccessService dataAccessService) {
145                 this.dataAccessService = dataAccessService;
146         }
147 }
148
149