2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the “License”);
10 * you may not use this software except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the “License”);
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
38 package org.onap.portalsdk.analytics.system.fusion.adapter;
40 import java.util.HashMap;
41 import java.util.HashSet;
42 import java.util.Iterator;
43 import java.util.LinkedHashMap;
44 import java.util.List;
47 import java.util.TreeSet;
49 import javax.servlet.http.HttpServletRequest;
50 import javax.servlet.http.HttpSession;
52 import org.onap.portalsdk.analytics.system.AppUtils;
53 import org.onap.portalsdk.core.domain.Menu;
54 import org.onap.portalsdk.core.domain.MenuData;
55 import org.onap.portalsdk.core.domain.Role;
56 import org.onap.portalsdk.core.domain.RoleFunction;
57 import org.onap.portalsdk.core.domain.User;
58 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
59 import org.onap.portalsdk.core.service.DataAccessService;
60 import org.onap.portalsdk.core.util.SystemProperties;
61 import org.onap.portalsdk.core.web.support.UserUtils;
62 import org.springframework.beans.factory.annotation.Autowired;
63 import org.springframework.stereotype.Service;
65 @Service("raptorAdapter")
66 public class RaptorAdapter extends FusionAdapter {
68 private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RaptorAdapter.class);
71 private static DataAccessService dataAccessService;
73 public static final int RAPTOR_USER_ID = 20000; // RAPTOR system user id (for auditing purposes)
74 public static final String RAPTOR_CONTROLLER_CLASSNAME = "org.onap.portalsdk.analytics.controller.Controller";
75 public static final String KEY_USER_ROLES_CACHE = "userRoles";
77 public void initializeRaptor() {
78 org.onap.portalsdk.analytics.config.ConfigLoader.setConfigFilesPath(SystemProperties.getProperty(SystemProperties.RAPTOR_CONFIG_FILE_PATH));
79 org.onap.portalsdk.analytics.system.Globals.initializeSystem(getServletContext());
83 /** Returns ID of the user currently logged in */
84 public static String getUserID(HttpServletRequest request) {
85 return String.valueOf(UserUtils.getUserId(request));
89 public static String getUserID(String user_id) {
94 public static String getUserBackdoorLoginId(HttpServletRequest request) {
95 if(AppUtils.getRequestNvlValue(request, "login_id").length()>0) return AppUtils.getRequestNvlValue(request, "login_id");
96 return String.valueOf(UserUtils.getUserSession(request).getLoginId());
99 public static String getUserBackdoorLoginId(String user_id) {
100 return getUserLoginId(user_id);
103 /** Obtains user name by ID */
104 public static String getUserName(String userId) {
105 Map<String, Long> params = new HashMap<String, Long>();
106 params.put("user_id", new Long(userId));
108 List list = getDataAccessService().executeNamedQuery("getUserNameById", params, null);
110 String firstName = "";
111 String lastName = "";
114 if (!list.isEmpty()) {
115 Object[] user = (Object[]) list.get(0);
116 firstName = (String) user[0]; // firstName scalar
117 lastName = (String) user[1]; // lastName scalar
121 return lastName + ", " + firstName;
124 public static String getUserName(HttpServletRequest request) {
125 User user = UserUtils.getUserSession(request);
126 return user.getLastName() + ", " + user.getFirstName();
129 public static String getUserEmail(String userId) {
130 Map<String, Long> params = new HashMap<String, Long>();
131 params.put("user_id", new Long(userId));
132 List list = getDataAccessService().executeNamedQuery("getUserEmail", params, null);
134 if (list != null && !list.isEmpty())
135 email = (String) list.get(0);
139 public static String getUserEmail(HttpServletRequest request) {
140 User user = UserUtils.getUserSession(request);
141 return user.getEmail();
144 public static String getUserLoginId(String userId) {
148 List list = getDataAccessService().getList(User.class, " where user_id = " + userId, null, null);
150 if (!list.isEmpty()) {
151 User user = (User)list.get(0);
152 loginId = user.getLoginId(); // firstName scalar
156 logger.error(EELFLoggerDelegate.debugLogger, ("error while getting login id : Exception" + e.getMessage()));
162 public static String getUserLoginId(HttpServletRequest request) {
163 User user = UserUtils.getUserSession(request);
164 return user.getLoginId();
167 /** Obtains list of all users (in IdNameValue objects) */
168 public static Map<Long, String> getAllUsers(String customizedQuery, String param, boolean isAdmin) {
170 Map<Long, String> map = new LinkedHashMap<Long, String>();
172 if(customizedQuery.length()>0 && !isAdmin) {
174 users = getDataAccessService().executeSQLQuery(customizedQuery, IdName.class, null);
177 Iterator i = users.iterator();
178 while (i.hasNext()) {
179 IdName item = (IdName)i.next();
180 map.put(item.getId(), item.getName());
185 users = getDataAccessService().executeNamedQuery("getAllUsers", null, null);
187 Iterator i = users.iterator();
188 while (i.hasNext()) {
189 Object[] user = (Object[])i.next();
190 Long id = (Long)user[0]; // id scalar
191 String firstName = (String)user[1]; // firstName scalar
192 String lastName = (String)user[2]; // lastName scalar
193 map.put(id, lastName + ", " + firstName);
200 /** Obtains role name by ID */
201 public static String getRoleName(String roleId) {
202 Map<String, Long> params = new HashMap<String, Long>();
203 params.put("role_id", new Long(roleId));
205 List list = getDataAccessService().executeNamedQuery("getRoleNameById", params, null);
207 String roleName = "";
210 if (!list.isEmpty()) {
211 roleName = (String) list.get(0); // name scalar
218 /** Obtains list of all roles (in IdNameValue objects) */
219 public static Map<Long, String> getAllRolesUsingCustomizedQuery(String customizedQuery, String param, boolean isAdmin) {
222 Map<Long, String> map = new LinkedHashMap<Long, String>();
224 if(customizedQuery.length()>0 && !isAdmin) {
226 roles = getDataAccessService().executeSQLQuery(customizedQuery, IdName.class, null);
229 Iterator i = roles.iterator();
230 while (i.hasNext()) {
231 IdName item = (IdName)i.next();
232 map.put(item.getId(), item.getName());
237 roles = getDataAccessService().executeNamedQuery("getAllRoles", null, null);
240 Iterator i = roles.iterator();
241 while (i.hasNext()) {
242 Object[] role = (Object[])i.next();
243 Long id = (Long)role[0]; // id scalar
244 String name = (String)role[1]; // firstName scalar
253 public static Set getUserRoles(HttpServletRequest request) {
254 return UserUtils.getRoles(request).keySet();
257 public static Set getUserRoles(String userId) {
258 Set userRoles = new HashSet<Long>();
259 // Map usersRolesMap = new LinkedHashMap<Long, Set>();
260 // Map<String, Long> params = new HashMap<String, Long>();
262 // params.put("user_id", new Long(userId));
264 // List usersRolesList = getDataAccessService().executeNamedQuery("getAllUsersRoles", params, null);
265 // Iterator i = usersRolesList.iterator();
266 // while (i.hasNext()) {
267 // Object[] userRole = (Object[]) i.next();
269 // Long roleId = (Long) userRole[1]; // role id scalar
270 // userRoles.add(roleId);
273 userRoles = getActiveUsersRoleIds(new Long(userId));
279 /** this is used to get role for the current user. **/
280 public static synchronized boolean isCurrentUserInRole(HttpServletRequest request, String roleId) {
281 HttpSession session = request.getSession(false);
282 if(session!=null && session.getAttribute(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME))!=null)
283 return UserUtils.hasRole(request, roleId);
288 /** Obtains menu label by ID */
289 public static String getMenuLabel(String menuId) {
290 return ((Menu) getDataAccessService().getDomainObject(MenuData.class, new Long(menuId), null)).getLabel();
293 public static String formatUserDateForDBTimeZone(String dateValue,String inPattern,
294 Long userId, String outPattern)throws Exception{
295 return DateUtils.formatUserDateForDBTimeZone(dateValue,inPattern,userId,outPattern);
298 public static String getCurrentDBDateForUser(String inPattern,Long userId)throws Exception{
299 return DateUtils.getCurrentDBDateForUser(inPattern, userId);
302 public static Set<Long> getActiveUsersRoleIds(Long userId) {
303 Set<Role> allActiveUserRoles = getActiveUserRoles(userId);
304 Iterator<Role> allActiveUserRolesIterator = allActiveUserRoles.iterator();
305 Set<Long> allActiveUserRoleIds = new TreeSet<Long>();
306 while(allActiveUserRolesIterator.hasNext()){
307 Role role = allActiveUserRolesIterator.next();
308 allActiveUserRoleIds.add(role.getId());
311 return allActiveUserRoleIds;
314 public static Set<Long> getActiveUserRoleIds(Long userId) {
315 Set<Role> allActiveUserRoles = getActiveUserRoles(userId);
316 Iterator<Role> allActiveUserRolesIterator = allActiveUserRoles.iterator();
317 Set<Long> allActiveUserRoleIds = new TreeSet<Long>();
318 while(allActiveUserRolesIterator.hasNext()){
319 Role role = allActiveUserRolesIterator.next();
320 allActiveUserRoleIds.add(role.getId());
323 return allActiveUserRoleIds;
326 public static Set<RoleFunction> getActiveRoleFunctions(Long userId) {
327 Set<Role> allActiveUserRoles = getActiveUserRoles(userId);
328 Iterator<Role> allActiveUserRolesIterator = allActiveUserRoles.iterator();
329 Set<RoleFunction> allActiveRoleFunctions = new TreeSet<RoleFunction>();
330 while(allActiveUserRolesIterator.hasNext()){
331 Role role = allActiveUserRolesIterator.next();
332 allActiveRoleFunctions.addAll(role.getRoleFunctions());
335 return allActiveRoleFunctions;
338 public static Set<Role> getActiveUserRoles(Long userId) {
339 User user = (User)getDataAccessService().getDomainObject(User.class,userId,null);
340 Set<Role> allActiveUserRoles = new TreeSet<Role>();
341 allActiveUserRoles.addAll(user.getRoles());
342 Iterator<Role> userRolesIterator = user.getRoles().iterator();
343 while(userRolesIterator.hasNext()){
344 getAllChildRoles( userRolesIterator.next(),allActiveUserRoles);
347 Iterator<Role> allActiveUserRolesIterator = allActiveUserRoles.iterator();
348 while(allActiveUserRolesIterator.hasNext()){
349 Role role = allActiveUserRolesIterator.next();
350 if(!role.getActive()){
351 allActiveUserRolesIterator.remove();
355 return allActiveUserRoles;
358 public static Set<Role> getAllChildRoles(Role role, Set<Role> allchildRoles) {
359 Set<Role> childRoles = role.getChildRoles();
360 allchildRoles.addAll(childRoles);
361 Iterator<Role> childRolesIterator = childRoles.iterator();
362 while (childRolesIterator.hasNext()) {
363 getAllChildRoles(childRolesIterator.next(),allchildRoles);
365 return allchildRoles;
369 public static DataAccessService getDataAccessService() {
370 return org.onap.portalsdk.core.web.support.AppUtils.getDataAccessService();
374 public static void setDataAccessService(DataAccessService dataAccessService) {
375 dataAccessService = dataAccessService;