Null check for ClientResponse in PolicyUril.java
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / service / EPRoleServiceImpl.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the “License”);
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the “License”);
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.openecomp.portalapp.portal.service;
39
40 import java.util.HashMap;
41 import java.util.Iterator;
42 import java.util.List;
43 import java.util.Map;
44 import java.util.Set;
45 import java.util.TreeSet;
46
47 import org.springframework.beans.factory.annotation.Autowired;
48 import org.springframework.context.annotation.EnableAspectJAutoProxy;
49 import org.springframework.stereotype.Service;
50 import org.springframework.transaction.annotation.Transactional;
51
52 import org.openecomp.portalsdk.core.domain.RoleFunction;
53 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
54 import org.openecomp.portalsdk.core.service.DataAccessService;
55 import org.openecomp.portalapp.portal.domain.EPRole;
56 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
57 import org.openecomp.portalapp.portal.utils.PortalConstants;
58
59 @Service("epRoleService")
60 @Transactional
61 @org.springframework.context.annotation.Configuration
62 @EnableAspectJAutoProxy
63 @EPMetricsLog
64 public class EPRoleServiceImpl implements EPRoleService {
65         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPRoleServiceImpl.class);
66
67         @Autowired
68         private DataAccessService dataAccessService;
69
70         @SuppressWarnings("unchecked")
71         public List<RoleFunction> getRoleFunctions() {
72                 // List msgDB = getDataAccessService().getList(Profile.class, null);
73                 return getDataAccessService().getList(RoleFunction.class, null);
74         }
75
76         @SuppressWarnings("unchecked")
77         public List<EPRole> getAvailableChildRoles(Long roleId) {
78                 List<EPRole> availableChildRoles = (List<EPRole>) getDataAccessService().getList(EPRole.class, null);
79                 if (roleId == null || roleId == 0) {
80                         return availableChildRoles;
81                 }
82
83                 EPRole currentRole = (EPRole) getDataAccessService().getDomainObject(EPRole.class, roleId, null);
84                 Set<EPRole> allParentRoles = new TreeSet<EPRole>();
85                 allParentRoles = getAllParentRolesAsList(currentRole, allParentRoles);
86
87                 Iterator<EPRole> availableChildRolesIterator = availableChildRoles.iterator();
88                 while (availableChildRolesIterator.hasNext()) {
89                         EPRole role = availableChildRolesIterator.next();
90                         if (!role.getActive() || allParentRoles.contains(role) || role.getId().equals(roleId)) {
91                                 availableChildRolesIterator.remove();
92                         }
93                 }
94                 return availableChildRoles;
95         }
96
97         private Set<EPRole> getAllParentRolesAsList(EPRole role, Set<EPRole> allParentRoles) {
98                 Set<EPRole> parentRoles = role.getParentRoles();
99                 allParentRoles.addAll(parentRoles);
100                 Iterator<EPRole> parentRolesIterator = parentRoles.iterator();
101                 while (parentRolesIterator.hasNext()) {
102                         getAllParentRolesAsList(parentRolesIterator.next(), allParentRoles);
103                 }
104                 return allParentRoles;
105         }
106
107         public RoleFunction getRoleFunction(String code) {
108                 return (RoleFunction) getDataAccessService().getDomainObject(RoleFunction.class, code, null);
109         }
110
111         public void saveRoleFunction(RoleFunction domainRoleFunction) {
112                 getDataAccessService().saveDomainObject(domainRoleFunction, null);
113         }
114
115         public void deleteRoleFunction(RoleFunction domainRoleFunction) {
116                 getDataAccessService().deleteDomainObject(domainRoleFunction, null);
117         }
118
119         public EPRole getRole(Long id) {
120                 return (EPRole) getDataAccessService().getDomainObject(EPRole.class, id, null);
121         }
122
123         // TODO: refactor
124         private static final String getAppRoleSqlFormat = "SELECT * FROM fn_role where APP_ID = %s AND APP_ROLE_ID = %s";
125
126         @SuppressWarnings("unchecked")
127         public EPRole getRole(Long appId, Long appRoleid) {
128                 if (appId == null || appRoleid == null) {
129                         logger.error(EELFLoggerDelegate.errorLogger, String.format(
130                                         "getRole does not support null appId or roleId. appRoleid=%s, appRoleid=%s", appId, appRoleid));
131                         return null;
132                 }
133
134                 String sql = String.format(getAppRoleSqlFormat, appId, appRoleid);
135
136                 List<EPRole> roles = (List<EPRole>) dataAccessService.executeSQLQuery(sql, EPRole.class, null);
137                 int resultsCount = roles.size();
138                 if (resultsCount > 1) {
139                         logger.error(EELFLoggerDelegate.errorLogger,
140                                         String.format(
141                                                         "search by appId=%s, appRoleid=%s should have returned 0 or 1 results. Got %d. This is an internal server error.",
142                                                         appId, appRoleid, resultsCount));
143                         logger.error(EELFLoggerDelegate.errorLogger,
144                                         "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.");
145                         return roles.get(0);
146                 } else if (resultsCount == 1) {
147                         return roles.get(0);
148                 }
149                 return null;
150         }
151
152         @SuppressWarnings("unchecked")
153         public EPRole getAppRole(String roleName, Long appId) {
154
155                 final Map<String, String> params = new HashMap<String, String>();
156                 final Map<String, String> portalParams = new HashMap<String, String>();
157                 List<EPRole> roles = null;
158                 params.put("appId", appId.toString());
159                 params.put("roleName", roleName);
160                 portalParams.put("appRoleName", roleName);
161                 if (appId == 1 || roleName.equals(PortalConstants.ADMIN_ROLE)) {
162                         roles = (List<EPRole>) dataAccessService.executeNamedQuery("getPortalAppRoles", portalParams, null);
163                 } else if (appId != 1 && !roleName.equals(PortalConstants.ADMIN_ROLE)) {
164                         roles = (List<EPRole>) dataAccessService.executeNamedQuery("getAppRoles", params, null);
165                 }
166                 int resultsCount = (roles == null ? 0 : roles.size());
167                 if (resultsCount > 1) {
168                         logger.error(EELFLoggerDelegate.errorLogger,
169                                         "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.");
170                         return roles.get(0);
171                 } else if (resultsCount == 1) {
172                         return roles.get(0);
173                 }
174                 return null;
175         }
176
177         public void saveRole(EPRole domainRole) {
178                 getDataAccessService().saveDomainObject(domainRole, null);
179         }
180
181         public void deleteRole(EPRole domainRole) {
182                 getDataAccessService().deleteDomainObject(domainRole, null);
183         }
184
185         @SuppressWarnings("unchecked")
186         public List<EPRole> getAvailableRoles() {
187                 return getDataAccessService().getList(EPRole.class, null);
188         }
189
190         public DataAccessService getDataAccessService() {
191                 return dataAccessService;
192         }
193
194         public void setDataAccessService(DataAccessService dataAccessService) {
195                 this.dataAccessService = dataAccessService;
196         }
197 }