2 * ================================================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ================================================================================
20 package org.openecomp.portalsdk.analytics.system.fusion.adapter;
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;
29 import java.util.TreeSet;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpSession;
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;
47 @Service("raptorAdapter")
48 public class RaptorAdapter extends FusionAdapter {
51 private static DataAccessService dataAccessService;
53 static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RaptorAdapter.class);
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";
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());
66 /** Returns ID of the user currently logged in */
67 public static String getUserID(HttpServletRequest request) {
68 return String.valueOf(UserUtils.getUserId(request));
72 public static String getUserID(String user_id) {
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());
82 public static String getUserBackdoorLoginId(String user_id) {
83 return getUserLoginId(user_id);
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));
91 List list = getDataAccessService().executeNamedQuery("getUserNameById", params, null);
93 String firstName = "";
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
104 return lastName + ", " + firstName;
107 public static String getUserName(HttpServletRequest request) {
108 User user = UserUtils.getUserSession(request);
109 return user.getLastName() + ", " + user.getFirstName();
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);
117 if (list != null && !list.isEmpty())
118 email = (String) list.get(0);
122 public static String getUserEmail(HttpServletRequest request) {
123 User user = UserUtils.getUserSession(request);
124 return user.getEmail();
127 public static String getUserLoginId(String userId) {
131 List list = getDataAccessService().getList(User.class, " where user_id = " + userId, null, null);
133 if (!list.isEmpty()) {
134 User user = (User)list.get(0);
135 loginId = user.getLoginId(); // firstName scalar
139 logger.error(EELFLoggerDelegate.debugLogger, ("error while getting login id : Exception" + e.getMessage()));
145 public static String getUserLoginId(HttpServletRequest request) {
146 User user = UserUtils.getUserSession(request);
147 return user.getLoginId();
150 /** Obtains list of all users (in IdNameValue objects) */
151 public static Map<Long, String> getAllUsers(String customizedQuery, String param, boolean isAdmin) {
153 Map<Long, String> map = new LinkedHashMap<Long, String>();
155 if(customizedQuery.length()>0 && !isAdmin) {
157 users = getDataAccessService().executeSQLQuery(customizedQuery, IdName.class, null);
160 Iterator i = users.iterator();
161 while (i.hasNext()) {
162 IdName item = (IdName)i.next();
163 map.put(item.getId(), item.getName());
168 users = getDataAccessService().executeNamedQuery("getAllUsers", null, 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);
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));
188 List list = getDataAccessService().executeNamedQuery("getRoleNameById", params, null);
190 String roleName = "";
193 if (!list.isEmpty()) {
194 roleName = (String) list.get(0); // name scalar
201 /** Obtains list of all roles (in IdNameValue objects) */
202 public static Map<Long, String> getAllRolesUsingCustomizedQuery(String customizedQuery, String param, boolean isAdmin) {
205 Map<Long, String> map = new LinkedHashMap<Long, String>();
207 if(customizedQuery.length()>0 && !isAdmin) {
209 roles = getDataAccessService().executeSQLQuery(customizedQuery, IdName.class, null);
212 Iterator i = roles.iterator();
213 while (i.hasNext()) {
214 IdName item = (IdName)i.next();
215 map.put(item.getId(), item.getName());
220 roles = getDataAccessService().executeNamedQuery("getAllRoles", null, 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
236 public static Set getUserRoles(HttpServletRequest request) {
237 return UserUtils.getRoles(request).keySet();
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>();
245 // params.put("user_id", new Long(userId));
247 // List usersRolesList = getDataAccessService().executeNamedQuery("getAllUsersRoles", params, null);
248 // Iterator i = usersRolesList.iterator();
249 // while (i.hasNext()) {
250 // Object[] userRole = (Object[]) i.next();
252 // Long roleId = (Long) userRole[1]; // role id scalar
253 // userRoles.add(roleId);
256 userRoles = getActiveUsersRoleIds(new Long(userId));
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);
271 // public static void processErrorNotification(HttpServletRequest request, Exception e) {
272 //org.openecomp.portalsdk.core.web.support.AppUtils.processError(e, logger, request);
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();
280 public static String formatUserDateForDBTimeZone(String dateValue,String inPattern,
281 Long userId, String outPattern)throws Exception{
282 return DateUtils.formatUserDateForDBTimeZone(dateValue,inPattern,userId,outPattern);
285 public static String getCurrentDBDateForUser(String inPattern,Long userId)throws Exception{
286 return DateUtils.getCurrentDBDateForUser(inPattern, userId);
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());
298 return allActiveUserRoleIds;
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());
310 return allActiveUserRoleIds;
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());
322 return allActiveRoleFunctions;
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);
334 Iterator<Role> allActiveUserRolesIterator = allActiveUserRoles.iterator();
335 while(allActiveUserRolesIterator.hasNext()){
336 Role role = allActiveUserRolesIterator.next();
337 if(!role.getActive()){
338 allActiveUserRolesIterator.remove();
342 return allActiveUserRoles;
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);
352 return allchildRoles;
356 public static DataAccessService getDataAccessService() {
357 return org.openecomp.portalsdk.core.web.support.AppUtils.getDataAccessService();
361 public static void setDataAccessService(DataAccessService dataAccessService) {
362 dataAccessService = dataAccessService;