1 package org.openecomp.portalsdk.core.service;
3 import java.util.HashSet;
4 import java.util.Iterator;
7 import java.util.TreeSet;
9 import org.openecomp.portalsdk.core.domain.Role;
10 import org.openecomp.portalsdk.core.domain.RoleFunction;
11 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
12 import org.springframework.beans.factory.annotation.Autowired;
13 import org.springframework.transaction.annotation.Transactional;
15 import com.fasterxml.jackson.databind.ObjectMapper;
16 import com.fasterxml.jackson.databind.type.TypeFactory;
19 public class RoleServiceCentralizedAccess implements RoleService {
21 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RoleServiceCentralizedAccess.class);
24 AppService appService;
27 RestApiRequestBuilder restApiRequestBuilder;
30 public List<RoleFunction> getRoleFunctions(String loginId) throws Exception {
32 List<RoleFunction> roleFunctionList = null;
33 String role_function_list = "";
34 role_function_list = restApiRequestBuilder.getViaREST("/functions", true, loginId);
35 ObjectMapper mapper = new ObjectMapper();
36 roleFunctionList = mapper.readValue(role_function_list,
37 TypeFactory.defaultInstance().constructCollectionType(List.class, RoleFunction.class));
38 return roleFunctionList;
42 public List<Role> getAvailableChildRoles(String loginId, Long roleId) throws Exception {
43 List<Role> availableChildRoles = getAvailableRoles(loginId);
44 if (roleId == null || roleId == 0) {
45 return availableChildRoles;
48 Role currentRole = getRole(loginId, roleId);
49 Set<Role> allParentRoles = new TreeSet<Role>();
50 allParentRoles = getAllParentRolesAsList(loginId, currentRole, allParentRoles);
52 Iterator<Role> availableChildRolesIterator = availableChildRoles.iterator();
53 while (availableChildRolesIterator.hasNext()) {
54 Role role = availableChildRolesIterator.next();
55 if (!role.getActive() || allParentRoles.contains(role) || role.getId().equals(roleId)) {
56 availableChildRolesIterator.remove();
59 return availableChildRoles;
62 @SuppressWarnings("unchecked")
63 private Set<Role> getAllParentRolesAsList(String loginId, Role role, Set<Role> allParentRoles) {
64 Set<Role> parentRoles = role.getParentRoles();
65 allParentRoles.addAll(parentRoles);
66 Iterator<Role> parentRolesIterator = parentRoles.iterator();
67 while (parentRolesIterator.hasNext()) {
68 getAllParentRolesAsList(loginId, parentRolesIterator.next(), allParentRoles);
70 return allParentRoles;
74 public Role getRole(String loginId, Long id) throws Exception {
75 ObjectMapper mapper = new ObjectMapper();
76 String roleString = restApiRequestBuilder.getViaREST("/role/" + id, true, loginId);
77 Role role = new Role();
78 role = mapper.readValue(roleString, Role.class);
79 if (role.getRoleFunctions() != null) {
80 @SuppressWarnings("unchecked")
81 Set<RoleFunction> roleFunctionList = role.getRoleFunctions();
82 Set<RoleFunction> roleFunctionListNew = new HashSet<>();
83 Iterator<RoleFunction> itetaror = roleFunctionList.iterator();
84 while (itetaror.hasNext()) {
85 Object nextValue = itetaror.next();
86 RoleFunction roleFun = mapper.convertValue(nextValue, RoleFunction.class);
87 roleFunctionListNew.add(roleFun);
90 role.setRoleFunctions(roleFunctionListNew);
92 logger.info(EELFLoggerDelegate.applicationLogger, "role_id" + role.getId());
98 public void saveRole(String loginId, Role domainRole) throws Exception {
99 ObjectMapper mapper = new ObjectMapper();
100 String role = mapper.writeValueAsString(domainRole);
102 restApiRequestBuilder.postViaREST("/role", true, role, loginId);
103 } catch (Exception e) {
104 logger.error(EELFLoggerDelegate.errorLogger, "saveRole Failed", e);
105 throw new Exception(e.getMessage());
110 public void deleteRole(String loginId, Role domainRole) throws Exception {
111 ObjectMapper mapper = new ObjectMapper();
112 String role = mapper.writeValueAsString(domainRole);
113 String roleName = domainRole.getName().replaceAll(" ", "%20");
115 restApiRequestBuilder.deleteViaRest("/deleteRole/"+ roleName, true, role, null, loginId);
116 } catch (Exception e) {
117 logger.error(EELFLoggerDelegate.errorLogger, "deleteRole Failed", e);
118 throw new Exception(e.getMessage());
122 public List<Role> getAvailableRoles(String requestedLoginId) throws Exception {
123 ObjectMapper mapper = new ObjectMapper();
124 String roleList = restApiRequestBuilder.getViaREST("/roles", true, requestedLoginId);
125 List<Role> roles = null;
126 roles = mapper.readValue(roleList,
127 TypeFactory.defaultInstance().constructCollectionType(List.class, Role.class));
132 public List<Role> getActiveRoles(String requestedLoginId) throws Exception {
133 ObjectMapper mapper = new ObjectMapper();
134 String roleString = restApiRequestBuilder.getViaREST("/activeRoles", true, requestedLoginId);
135 List<Role> roles = null;
136 roles = mapper.readValue(roleString,
137 TypeFactory.defaultInstance().constructCollectionType(List.class, Role.class));
143 public RoleFunction getRoleFunction(String requestedLoginId, String code) throws Exception {
144 ObjectMapper mapper = new ObjectMapper();
145 String responseString = restApiRequestBuilder.getViaREST("/function/" + code, true, requestedLoginId);
146 RoleFunction roleFunction = new RoleFunction();
147 if (!responseString.isEmpty()) {
148 roleFunction = mapper.readValue(responseString, RoleFunction.class);
154 public void saveRoleFunction(String requestedLoginId, RoleFunction domainRoleFunction) throws Exception {
155 ObjectMapper mapper = new ObjectMapper();
156 String roleFunction = mapper.writeValueAsString(domainRoleFunction);
158 restApiRequestBuilder.postViaREST("/roleFunction", true, roleFunction, requestedLoginId);
160 logger.error(EELFLoggerDelegate.errorLogger, "saveRoleFunction Failed", e);
161 throw new Exception(e.getMessage());
166 public void deleteRoleFunction(String requestedLoginId, RoleFunction domainRoleFunction) throws Exception {
167 String code = domainRoleFunction.getCode();
168 ObjectMapper mapper = new ObjectMapper();
169 String roleFunction = mapper.writeValueAsString(domainRoleFunction);
171 restApiRequestBuilder.deleteViaRest("/roleFunction/"+ code, true, roleFunction, null, requestedLoginId);
172 } catch (Exception e) {
173 logger.error(EELFLoggerDelegate.errorLogger, "deleteRoleFunction Failed ", e);
174 throw new Exception(e.getMessage());
179 public void deleteDependcyRoleRecord(String requestedLoginId, Long id) {
181 restApiRequestBuilder.deleteViaRest("/deleteDependcyRoleRecord/" + id, true, null, null, requestedLoginId);
182 } catch (Exception e) {
183 logger.error(EELFLoggerDelegate.errorLogger, "deleteDependcyRoleRecord Failed", e);