2111935c3feb86bc33e1238a3565799acfb0f87d
[portal/sdk.git] /
1 /*-
2  * ================================================================================
3  * eCOMP Portal SDK
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.portalsdk.analytics.system.fusion.adapter;
21
22 import java.util.HashMap;
23 import java.util.HashSet;
24 import java.util.Iterator;
25 import java.util.LinkedHashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Set;
29 import java.util.TreeSet;
30
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpSession;
33
34 import org.openecomp.portalsdk.analytics.system.AppUtils;
35 import org.openecomp.portalsdk.core.domain.Menu;
36 import org.openecomp.portalsdk.core.domain.MenuData;
37 import org.openecomp.portalsdk.core.domain.Role;
38 import org.openecomp.portalsdk.core.domain.RoleFunction;
39 import org.openecomp.portalsdk.core.domain.User;
40 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
41 import org.openecomp.portalsdk.core.service.DataAccessService;
42 import org.openecomp.portalsdk.core.util.SystemProperties;
43 import org.openecomp.portalsdk.core.web.support.UserUtils;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.stereotype.Service;
46
47 @Service("raptorAdapter")
48 public class RaptorAdapter extends FusionAdapter {
49         
50         @Autowired
51         private static DataAccessService dataAccessService;
52
53         static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RaptorAdapter.class);
54
55   
56     public static final int    RAPTOR_USER_ID              = 20000; // RAPTOR system user id (for auditing purposes)
57     public static final String RAPTOR_CONTROLLER_CLASSNAME = "org.openecomp.portalsdk.analytics.controller.Controller";
58     public static final String KEY_USER_ROLES_CACHE        =  "userRoles";
59     
60     public void initializeRaptor() {
61         org.openecomp.portalsdk.analytics.config.ConfigLoader.setConfigFilesPath(SystemProperties.getProperty(SystemProperties.RAPTOR_CONFIG_FILE_PATH));
62         org.openecomp.portalsdk.analytics.system.Globals.initializeSystem(getServletContext());
63     }
64
65
66     /** Returns ID of the user currently logged in */
67     public static String getUserID(HttpServletRequest request) {
68         return String.valueOf(UserUtils.getUserId(request));
69         //return null;
70     }
71
72     public static String getUserID(String user_id) {
73         return user_id;
74     }    
75     
76
77     public static String getUserBackdoorLoginId(HttpServletRequest request) {
78       if(AppUtils.getRequestNvlValue(request, "login_id").length()>0) return AppUtils.getRequestNvlValue(request, "login_id");
79         return String.valueOf(UserUtils.getUserSession(request).getLoginId());
80     }
81
82     public static String getUserBackdoorLoginId(String user_id) {
83          return getUserLoginId(user_id);
84       }
85
86     /** Obtains user name by ID */
87     public static String getUserName(String userId) {
88         Map<String, Long> params = new HashMap<String, Long>();
89         params.put("user_id", new Long(userId));
90
91         List list = getDataAccessService().executeNamedQuery("getUserNameById", params, null);
92
93         String firstName = "";
94         String lastName = "";
95
96         if (list != null) {
97             if (!list.isEmpty()) {
98                 Object[] user = (Object[]) list.get(0);
99                 firstName = (String) user[0]; // firstName scalar
100                 lastName = (String) user[1]; // lastName scalar
101             }
102         }
103
104         return lastName + ", " + firstName;
105     }
106
107     public static String getUserName(HttpServletRequest request) {
108         User user = UserUtils.getUserSession(request);
109         return user.getLastName() + ", " + user.getFirstName();
110     }
111     
112     public static String getUserEmail(String userId) {
113         Map<String, Long> params = new HashMap<String, Long>();
114         params.put("user_id", new Long(userId));
115         List list = getDataAccessService().executeNamedQuery("getUserEmail", params, null);
116         String email = "";
117         if (list != null && !list.isEmpty())
118             email = (String) list.get(0);
119         return email;
120     }
121
122     public static String getUserEmail(HttpServletRequest request) {
123         User user = UserUtils.getUserSession(request);
124         return user.getEmail();
125     }
126     
127     public static String getUserLoginId(String userId) {
128         
129         String loginId = "";
130         try{
131                 List  list = getDataAccessService().getList(User.class, " where user_id = " + userId, null, null);
132                 if (list != null) {
133               if (!list.isEmpty()) {
134                 User user = (User)list.get(0);
135                 loginId = user.getLoginId(); // firstName scalar            
136               }
137             }
138         }catch(Exception e){
139                 logger.error(EELFLoggerDelegate.debugLogger, ("error while getting login id : Exception" + e.getMessage()));
140         }
141         return loginId;
142       }
143
144     
145     public static String getUserLoginId(HttpServletRequest request) {
146         User user = UserUtils.getUserSession(request);
147         return user.getLoginId();
148     }
149     
150     /** Obtains list of all users (in IdNameValue objects) */
151     public static Map<Long, String> getAllUsers(String customizedQuery, String param, boolean isAdmin) {      
152         List users = null;
153         Map<Long, String> map = new LinkedHashMap<Long, String>();
154         
155         if(customizedQuery.length()>0 && !isAdmin) {
156
157                 users = getDataAccessService().executeSQLQuery(customizedQuery, IdName.class, null);
158
159                 if (users != null) {                    
160                         Iterator i = users.iterator();
161                         while (i.hasNext()) {
162                                 IdName item = (IdName)i.next();
163                                 map.put(item.getId(), item.getName());
164                         }
165                 }
166                 
167         } else {
168             users = getDataAccessService().executeNamedQuery("getAllUsers", null, null);
169             if (users != null) {
170               Iterator i = users.iterator();
171                while (i.hasNext()) {
172                  Object[]    user = (Object[])i.next();
173                  Long          id = (Long)user[0];   // id scalar
174                  String firstName = (String)user[1]; // firstName scalar
175                  String  lastName = (String)user[2]; // lastName scalar
176                  map.put(id, lastName + ", " + firstName);
177               }
178             }                   
179         }
180       return map;
181     }
182
183     /** Obtains role name by ID */
184     public static String getRoleName(String roleId) {
185         Map<String, Long> params = new HashMap<String, Long>();
186         params.put("role_id", new Long(roleId));
187
188         List list = getDataAccessService().executeNamedQuery("getRoleNameById", params, null);
189
190         String roleName = "";
191
192         if (list != null) {
193             if (!list.isEmpty()) {
194                 roleName = (String) list.get(0); // name scalar
195             }
196         }
197
198         return roleName;
199     }
200
201     /** Obtains list of all roles (in IdNameValue objects) */
202     public static Map<Long, String> getAllRolesUsingCustomizedQuery(String customizedQuery, String param, boolean isAdmin) {
203         List roles = null;
204
205         Map<Long, String> map = new LinkedHashMap<Long, String>();
206
207         if(customizedQuery.length()>0  && !isAdmin) {
208
209                 roles = getDataAccessService().executeSQLQuery(customizedQuery, IdName.class, null);
210
211                 if (roles != null) {                    
212                         Iterator i = roles.iterator();
213                         while (i.hasNext()) {
214                                 IdName item = (IdName)i.next();
215                                 map.put(item.getId(), item.getName());
216                         }
217                 }
218         } else {
219
220                 roles = getDataAccessService().executeNamedQuery("getAllRoles", null, null);      
221
222                 if (roles != null) {
223                         Iterator i = roles.iterator();
224                         while (i.hasNext()) {
225                                 Object[]    role = (Object[])i.next();
226                                 Long          id = (Long)role[0];   // id scalar
227                                 String name = (String)role[1]; // firstName scalar
228                                 map.put(id, name);
229                         }
230                 }               
231         }
232
233         return map;
234     }
235
236     public static Set getUserRoles(HttpServletRequest request) {
237         return UserUtils.getRoles(request).keySet();
238     }
239     
240     public static Set getUserRoles(String userId) {
241         Set     userRoles                       = new HashSet<Long>();
242 //        Map usersRolesMap                     = new LinkedHashMap<Long, Set>();
243 //        Map<String, Long> params      = new HashMap<String, Long>();
244 //        
245 //        params.put("user_id", new Long(userId));
246 //        
247 //      List usersRolesList             = getDataAccessService().executeNamedQuery("getAllUsersRoles", params, null);
248 //      Iterator    i                           = usersRolesList.iterator();
249 //        while (i.hasNext()) {
250 //              Object[] userRole = (Object[]) i.next();
251 //
252 //              Long roleId = (Long) userRole[1]; // role id scalar
253 //              userRoles.add(roleId);
254 //
255 //        }
256         userRoles = getActiveUsersRoleIds(new Long(userId));
257
258         
259         return userRoles;
260     }
261
262         /** this is used to get role for the current user. **/
263     public static synchronized boolean isCurrentUserInRole(HttpServletRequest request, String roleId) { 
264         HttpSession session = request.getSession(false);
265         if(session!=null && session.getAttribute(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME))!=null)
266                 return UserUtils.hasRole(request, roleId);
267         else
268                 return false;
269     }
270         
271    // public static void processErrorNotification(HttpServletRequest request, Exception e) {
272        //org.openecomp.portalsdk.core.web.support.AppUtils.processError(e, logger, request);
273     //}
274
275     /** Obtains menu label by ID */
276     public static String getMenuLabel(String menuId) {
277         return ((Menu) getDataAccessService().getDomainObject(MenuData.class, new Long(menuId), null)).getLabel();
278     }
279   
280     public static String formatUserDateForDBTimeZone(String dateValue,String inPattern, 
281                 Long userId, String outPattern)throws Exception{
282         return DateUtils.formatUserDateForDBTimeZone(dateValue,inPattern,userId,outPattern);
283     }
284     
285     public static String getCurrentDBDateForUser(String inPattern,Long userId)throws Exception{
286         return DateUtils.getCurrentDBDateForUser(inPattern, userId);
287     }
288     
289         public static Set<Long> getActiveUsersRoleIds(Long userId) {
290                 Set<Role> allActiveUserRoles = getActiveUserRoles(userId);
291                 Iterator<Role> allActiveUserRolesIterator = allActiveUserRoles.iterator();
292                 Set<Long> allActiveUserRoleIds = new TreeSet<Long>();
293                 while(allActiveUserRolesIterator.hasNext()){
294                         Role role = allActiveUserRolesIterator.next();
295                         allActiveUserRoleIds.add(role.getId());
296                 }
297
298                 return allActiveUserRoleIds;
299         }
300         
301         public static Set<Long> getActiveUserRoleIds(Long userId) {
302                 Set<Role> allActiveUserRoles = getActiveUserRoles(userId);
303                 Iterator<Role> allActiveUserRolesIterator = allActiveUserRoles.iterator();
304                 Set<Long> allActiveUserRoleIds = new TreeSet<Long>();
305                 while(allActiveUserRolesIterator.hasNext()){
306                         Role role = allActiveUserRolesIterator.next();
307                         allActiveUserRoleIds.add(role.getId());
308                 }
309
310                 return allActiveUserRoleIds;
311         }
312         
313         public static Set<RoleFunction> getActiveRoleFunctions(Long userId) {
314                 Set<Role> allActiveUserRoles = getActiveUserRoles(userId);
315                 Iterator<Role> allActiveUserRolesIterator = allActiveUserRoles.iterator();
316                 Set<RoleFunction> allActiveRoleFunctions = new TreeSet<RoleFunction>();
317                 while(allActiveUserRolesIterator.hasNext()){
318                         Role role = allActiveUserRolesIterator.next();
319                         allActiveRoleFunctions.addAll(role.getRoleFunctions());
320                 }
321
322                 return allActiveRoleFunctions;
323         }
324
325         public static Set<Role> getActiveUserRoles(Long userId) {
326                 User user = (User)getDataAccessService().getDomainObject(User.class,userId,null);
327                 Set<Role> allActiveUserRoles = new TreeSet<Role>();
328                 allActiveUserRoles.addAll(user.getRoles());
329                 Iterator<Role> userRolesIterator = user.getRoles().iterator();
330                 while(userRolesIterator.hasNext()){
331                         getAllChildRoles( userRolesIterator.next(),allActiveUserRoles);
332                 }
333                 
334                 Iterator<Role> allActiveUserRolesIterator = allActiveUserRoles.iterator();
335                 while(allActiveUserRolesIterator.hasNext()){
336                         Role role = allActiveUserRolesIterator.next();
337                         if(!role.getActive()){
338                                 allActiveUserRolesIterator.remove();
339                         }
340                 }
341
342                 return allActiveUserRoles;
343         }
344         
345         public static Set<Role> getAllChildRoles(Role role, Set<Role> allchildRoles) {
346                 Set<Role> childRoles = role.getChildRoles();
347                 allchildRoles.addAll(childRoles);
348                 Iterator<Role> childRolesIterator = childRoles.iterator();
349                 while (childRolesIterator.hasNext()) {
350                         getAllChildRoles(childRolesIterator.next(),allchildRoles);
351                 }
352                 return allchildRoles;
353         }
354
355
356         public static DataAccessService getDataAccessService() {
357                 return org.openecomp.portalsdk.core.web.support.AppUtils.getDataAccessService();
358         }
359
360
361         public static void setDataAccessService(DataAccessService dataAccessService) {
362                 dataAccessService = dataAccessService;
363         }
364         
365         
366
367 }