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