3efe4382bc5784c08d0eca2d593c89488b395f55
[portal.git] / ecomp-portal-BE-common / 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.HashMap;
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Set;
27 import java.util.TreeSet;
28
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;
33
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;
40
41 @Service("epRoleService")
42 @Transactional
43 @org.springframework.context.annotation.Configuration
44 @EnableAspectJAutoProxy
45 @EPMetricsLog
46 public class EPRoleServiceImpl implements EPRoleService {
47         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPRoleServiceImpl.class);
48
49         @Autowired
50         private DataAccessService dataAccessService;
51
52         @SuppressWarnings("unchecked")
53         public List<RoleFunction> getRoleFunctions() {
54                 // List msgDB = getDataAccessService().getList(Profile.class, null);
55                 return getDataAccessService().getList(RoleFunction.class, null);
56         }
57
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;
63                 }
64
65                 EPRole currentRole = (EPRole) getDataAccessService().getDomainObject(EPRole.class, roleId, null);
66                 Set<EPRole> allParentRoles = new TreeSet<EPRole>();
67                 allParentRoles = getAllParentRolesAsList(currentRole, allParentRoles);
68
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();
74                         }
75                 }
76                 return availableChildRoles;
77         }
78
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);
85                 }
86                 return allParentRoles;
87         }
88
89         public RoleFunction getRoleFunction(String code) {
90                 return (RoleFunction) getDataAccessService().getDomainObject(RoleFunction.class, code, null);
91         }
92
93         public void saveRoleFunction(RoleFunction domainRoleFunction) {
94                 getDataAccessService().saveDomainObject(domainRoleFunction, null);
95         }
96
97         public void deleteRoleFunction(RoleFunction domainRoleFunction) {
98                 getDataAccessService().deleteDomainObject(domainRoleFunction, null);
99         }
100
101         public EPRole getRole(Long id) {
102                 return (EPRole) getDataAccessService().getDomainObject(EPRole.class, id, null);
103         }
104
105         // TODO: refactor
106         private static final String getAppRoleSqlFormat = "SELECT * FROM fn_role where APP_ID = %s AND APP_ROLE_ID = %s";
107
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));
113                         return null;
114                 }
115
116                 String sql = String.format(getAppRoleSqlFormat, appId, appRoleid);
117
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,
122                                         String.format(
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.");
127                         return roles.get(0);
128                 } else if (resultsCount == 1) {
129                         return roles.get(0);
130                 }
131                 return null;
132         }
133
134         @SuppressWarnings("unchecked")
135         public EPRole getAppRole(String roleName, Long appId) {
136
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);
147                 }
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.");
152                         return roles.get(0);
153                 } else if (resultsCount == 1) {
154                         return roles.get(0);
155                 }
156                 return null;
157         }
158
159         public void saveRole(EPRole domainRole) {
160                 getDataAccessService().saveDomainObject(domainRole, null);
161         }
162
163         public void deleteRole(EPRole domainRole) {
164                 getDataAccessService().deleteDomainObject(domainRole, null);
165         }
166
167         @SuppressWarnings("unchecked")
168         public List<EPRole> getAvailableRoles() {
169                 return getDataAccessService().getList(EPRole.class, null);
170         }
171
172         public DataAccessService getDataAccessService() {
173                 return dataAccessService;
174         }
175
176         public void setDataAccessService(DataAccessService dataAccessService) {
177                 this.dataAccessService = dataAccessService;
178         }
179 }