979d6658c7dbfb3972eec76867149742384a796c
[portal/sdk.git] /
1 package org.openecomp.portalsdk.core.service;
2
3 import java.util.HashMap;
4 import java.util.HashSet;
5 import java.util.Iterator;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.Set;
9 import java.util.SortedSet;
10 import java.util.TreeSet;
11
12 import org.openecomp.portalsdk.core.domain.Role;
13 import org.openecomp.portalsdk.core.domain.RoleFunction;
14 import org.openecomp.portalsdk.core.domain.User;
15 import org.openecomp.portalsdk.core.domain.UserApp;
16 import org.springframework.beans.factory.annotation.Autowired;
17 import org.springframework.transaction.annotation.Transactional;
18
19 import com.fasterxml.jackson.databind.ObjectMapper;
20
21 @Transactional
22 public class UserServiceCentalizedImpl implements UserService {
23
24         @Autowired
25         AppService appService;
26         
27         @Autowired
28         RestApiRequestBuilder restApiRequestBuilder;
29         
30         @Autowired
31         LoginService loginService;
32                 
33         @Autowired
34         private DataAccessService  dataAccessService;
35
36         public DataAccessService getDataAccessService() {
37                 return dataAccessService;
38         }
39
40         public void setDataAccessService(DataAccessService dataAccessService) {
41                 this.dataAccessService = dataAccessService;
42         }
43
44         
45         @Override
46         public User getUser(String id) throws Exception {
47                 User user = null;
48                 String orgUserId = getUserByProfileId(id);
49                 String responseString = restApiRequestBuilder.getViaREST("/user/" + orgUserId, true, id);
50                 user = userMapper(responseString);
51                 return user;
52         }
53
54         public String getUserByProfileId(String id) {
55                 Map<String, Long> params = new HashMap<String, Long>();
56                 params.put("user_id", new Long(id));
57                 @SuppressWarnings("rawtypes")
58                 List list = getDataAccessService().executeNamedQuery("getUserByProfileId", params, null);
59                 String orgUserId = "";
60                 if (list != null && !list.isEmpty())
61                         orgUserId = (String) list.get(0);
62                 return orgUserId;
63         }
64
65         @Override
66         public User userMapper(String res) throws Exception
67         {
68                 User  user= new User();
69                 ObjectMapper mapper = new ObjectMapper();
70                 HashSet<RoleFunction> rolefun = null;
71         user = mapper.readValue(res, User.class);
72                 Set<RoleFunction> roleFunctionListNew = new HashSet<>();
73                 Set<RoleFunction> roleFunctionList  = new HashSet<>();
74                 SortedSet<UserApp> UserAppSet = new TreeSet<>();
75                 @SuppressWarnings("unchecked")
76                 Set<UserApp> setAppsObj = user.getUserApps();
77                 Iterator<UserApp> it = setAppsObj.iterator();
78                 while (it.hasNext()) {
79                         Object next = it.next();
80
81                         UserApp nextApp = mapper.convertValue(next, UserApp.class);
82                         rolefun = new HashSet<>();
83                         Role role = nextApp.getRole();
84                         roleFunctionList = role.getRoleFunctions();
85                         Iterator<RoleFunction> itetaror = roleFunctionList.iterator();
86                         while (itetaror.hasNext()) {
87                                 Object nextValue = itetaror.next();
88                                 RoleFunction roleFunction = mapper.convertValue(nextValue, RoleFunction.class);
89                                 roleFunctionListNew.add(roleFunction);
90                         }
91                         role.setRoleFunctions(roleFunctionListNew);
92                         nextApp.setRole(role);
93                         nextApp.getRole().getRoleFunctions();
94                         
95                         UserAppSet.add(nextApp);
96                         user.setUserApps(UserAppSet);
97                 }
98                 return user;
99         }
100
101 }