summaryrefslogtreecommitdiffstats
path: root/ecomp-sdk/epsdk-analytics/src/main/java/org/openecomp/portalsdk/analytics/system/fusion/adapter/RaptorAdapter.java
blob: 2111935c3feb86bc33e1238a3565799acfb0f87d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*-
 * ================================================================================
 * eCOMP Portal SDK
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ================================================================================
 */
package org.openecomp.portalsdk.analytics.system.fusion.adapter;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.openecomp.portalsdk.analytics.system.AppUtils;
import org.openecomp.portalsdk.core.domain.Menu;
import org.openecomp.portalsdk.core.domain.MenuData;
import org.openecomp.portalsdk.core.domain.Role;
import org.openecomp.portalsdk.core.domain.RoleFunction;
import org.openecomp.portalsdk.core.domain.User;
import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
import org.openecomp.portalsdk.core.service.DataAccessService;
import org.openecomp.portalsdk.core.util.SystemProperties;
import org.openecomp.portalsdk.core.web.support.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("raptorAdapter")
public class RaptorAdapter extends FusionAdapter {
	
	@Autowired
	private static DataAccessService dataAccessService;

	static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RaptorAdapter.class);

  
    public static final int    RAPTOR_USER_ID              = 20000; // RAPTOR system user id (for auditing purposes)
    public static final String RAPTOR_CONTROLLER_CLASSNAME = "org.openecomp.portalsdk.analytics.controller.Controller";
    public static final String KEY_USER_ROLES_CACHE        =  "userRoles";
    
    public void initializeRaptor() {
        org.openecomp.portalsdk.analytics.config.ConfigLoader.setConfigFilesPath(SystemProperties.getProperty(SystemProperties.RAPTOR_CONFIG_FILE_PATH));
        org.openecomp.portalsdk.analytics.system.Globals.initializeSystem(getServletContext());
    }


    /** Returns ID of the user currently logged in */
    public static String getUserID(HttpServletRequest request) {
        return String.valueOf(UserUtils.getUserId(request));
    	//return null;
    }

    public static String getUserID(String user_id) {
        return user_id;
    }    
    

    public static String getUserBackdoorLoginId(HttpServletRequest request) {
      if(AppUtils.getRequestNvlValue(request, "login_id").length()>0) return AppUtils.getRequestNvlValue(request, "login_id");
        return String.valueOf(UserUtils.getUserSession(request).getLoginId());
    }

    public static String getUserBackdoorLoginId(String user_id) {
    	 return getUserLoginId(user_id);
      }

    /** Obtains user name by ID */
    public static String getUserName(String userId) {
        Map<String, Long> params = new HashMap<String, Long>();
        params.put("user_id", new Long(userId));

        List list = getDataAccessService().executeNamedQuery("getUserNameById", params, null);

        String firstName = "";
        String lastName = "";

        if (list != null) {
            if (!list.isEmpty()) {
                Object[] user = (Object[]) list.get(0);
                firstName = (String) user[0]; // firstName scalar
                lastName = (String) user[1]; // lastName scalar
            }
        }

        return lastName + ", " + firstName;
    }

    public static String getUserName(HttpServletRequest request) {
    	User user = UserUtils.getUserSession(request);
    	return user.getLastName() + ", " + user.getFirstName();
    }
    
    public static String getUserEmail(String userId) {
        Map<String, Long> params = new HashMap<String, Long>();
        params.put("user_id", new Long(userId));
        List list = getDataAccessService().executeNamedQuery("getUserEmail", params, null);
        String email = "";
        if (list != null && !list.isEmpty())
            email = (String) list.get(0);
        return email;
    }

    public static String getUserEmail(HttpServletRequest request) {
    	User user = UserUtils.getUserSession(request);
    	return user.getEmail();
    }
    
    public static String getUserLoginId(String userId) {
        
        String loginId = "";
        try{
        	List  list = getDataAccessService().getList(User.class, " where user_id = " + userId, null, null);
        	if (list != null) {
              if (!list.isEmpty()) {
                User user = (User)list.get(0);
                loginId = user.getLoginId(); // firstName scalar            
              }
            }
        }catch(Exception e){
        	logger.error(EELFLoggerDelegate.debugLogger, ("error while getting login id : Exception" + e.getMessage()));
        }
        return loginId;
      }

    
    public static String getUserLoginId(HttpServletRequest request) {
    	User user = UserUtils.getUserSession(request);
    	return user.getLoginId();
    }
    
    /** Obtains list of all users (in IdNameValue objects) */
    public static Map<Long, String> getAllUsers(String customizedQuery, String param, boolean isAdmin) {      
    	List users = null;
    	Map<Long, String> map = new LinkedHashMap<Long, String>();
    	
    	if(customizedQuery.length()>0 && !isAdmin) {

    		users = getDataAccessService().executeSQLQuery(customizedQuery, IdName.class, null);

        	if (users != null) {    		
        		Iterator i = users.iterator();
        		while (i.hasNext()) {
        			IdName item = (IdName)i.next();
        			map.put(item.getId(), item.getName());
        		}
        	}
	     	
       	} else {
            users = getDataAccessService().executeNamedQuery("getAllUsers", null, null);
            if (users != null) {
              Iterator i = users.iterator();
               while (i.hasNext()) {
              	 Object[]    user = (Object[])i.next();
              	 Long          id = (Long)user[0];   // id scalar
              	 String firstName = (String)user[1]; // firstName scalar
              	 String  lastName = (String)user[2]; // lastName scalar
              	 map.put(id, lastName + ", " + firstName);
              }
            }       		
       	}
      return map;
    }

    /** Obtains role name by ID */
    public static String getRoleName(String roleId) {
        Map<String, Long> params = new HashMap<String, Long>();
        params.put("role_id", new Long(roleId));

        List list = getDataAccessService().executeNamedQuery("getRoleNameById", params, null);

        String roleName = "";

        if (list != null) {
            if (!list.isEmpty()) {
                roleName = (String) list.get(0); // name scalar
            }
        }

        return roleName;
    }

    /** Obtains list of all roles (in IdNameValue objects) */
    public static Map<Long, String> getAllRolesUsingCustomizedQuery(String customizedQuery, String param, boolean isAdmin) {
        List roles = null;

    	Map<Long, String> map = new LinkedHashMap<Long, String>();

    	if(customizedQuery.length()>0  && !isAdmin) {

    		roles = getDataAccessService().executeSQLQuery(customizedQuery, IdName.class, null);

        	if (roles != null) {    		
        		Iterator i = roles.iterator();
        		while (i.hasNext()) {
        			IdName item = (IdName)i.next();
        			map.put(item.getId(), item.getName());
        		}
        	}
    	} else {

    		roles = getDataAccessService().executeNamedQuery("getAllRoles", null, null);      

        	if (roles != null) {
        		Iterator i = roles.iterator();
        		while (i.hasNext()) {
        			Object[]    role = (Object[])i.next();
        			Long          id = (Long)role[0];   // id scalar
        			String name = (String)role[1]; // firstName scalar
        			map.put(id, name);
        		}
        	}    		
    	}

    	return map;
    }

    public static Set getUserRoles(HttpServletRequest request) {
    	return UserUtils.getRoles(request).keySet();
    }
    
    public static Set getUserRoles(String userId) {
        Set     userRoles 			= new HashSet<Long>();
//        Map usersRolesMap 			= new LinkedHashMap<Long, Set>();
//        Map<String, Long> params 	= new HashMap<String, Long>();
//        
//        params.put("user_id", new Long(userId));
//        
//    	List usersRolesList 		= getDataAccessService().executeNamedQuery("getAllUsersRoles", params, null);
//    	Iterator    i 				= usersRolesList.iterator();
//        while (i.hasNext()) {
//        	Object[] userRole = (Object[]) i.next();
//
//        	Long roleId = (Long) userRole[1]; // role id scalar
//        	userRoles.add(roleId);
//
//        }
        userRoles = getActiveUsersRoleIds(new Long(userId));

        
    	return userRoles;
    }

	/** this is used to get role for the current user. **/
    public static synchronized boolean isCurrentUserInRole(HttpServletRequest request, String roleId) { 
    	HttpSession session = request.getSession(false);
    	if(session!=null && session.getAttribute(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME))!=null)
    		return UserUtils.hasRole(request, roleId);
    	else
    		return false;
    }
	
   // public static void processErrorNotification(HttpServletRequest request, Exception e) {
       //org.openecomp.portalsdk.core.web.support.AppUtils.processError(e, logger, request);
    //}

    /** Obtains menu label by ID */
    public static String getMenuLabel(String menuId) {
        return ((Menu) getDataAccessService().getDomainObject(MenuData.class, new Long(menuId), null)).getLabel();
    }
  
    public static String formatUserDateForDBTimeZone(String dateValue,String inPattern, 
    		Long userId, String outPattern)throws Exception{
    	return DateUtils.formatUserDateForDBTimeZone(dateValue,inPattern,userId,outPattern);
    }
    
    public static String getCurrentDBDateForUser(String inPattern,Long userId)throws Exception{
    	return DateUtils.getCurrentDBDateForUser(inPattern, userId);
    }
    
	public static Set<Long> getActiveUsersRoleIds(Long userId) {
		Set<Role> allActiveUserRoles = getActiveUserRoles(userId);
		Iterator<Role> allActiveUserRolesIterator = allActiveUserRoles.iterator();
		Set<Long> allActiveUserRoleIds = new TreeSet<Long>();
		while(allActiveUserRolesIterator.hasNext()){
			Role role = allActiveUserRolesIterator.next();
			allActiveUserRoleIds.add(role.getId());
		}

		return allActiveUserRoleIds;
	}
	
	public static Set<Long> getActiveUserRoleIds(Long userId) {
		Set<Role> allActiveUserRoles = getActiveUserRoles(userId);
		Iterator<Role> allActiveUserRolesIterator = allActiveUserRoles.iterator();
		Set<Long> allActiveUserRoleIds = new TreeSet<Long>();
		while(allActiveUserRolesIterator.hasNext()){
			Role role = allActiveUserRolesIterator.next();
			allActiveUserRoleIds.add(role.getId());
		}

		return allActiveUserRoleIds;
	}
	
	public static Set<RoleFunction> getActiveRoleFunctions(Long userId) {
		Set<Role> allActiveUserRoles = getActiveUserRoles(userId);
		Iterator<Role> allActiveUserRolesIterator = allActiveUserRoles.iterator();
		Set<RoleFunction> allActiveRoleFunctions = new TreeSet<RoleFunction>();
		while(allActiveUserRolesIterator.hasNext()){
			Role role = allActiveUserRolesIterator.next();
			allActiveRoleFunctions.addAll(role.getRoleFunctions());
		}

		return allActiveRoleFunctions;
	}

	public static Set<Role> getActiveUserRoles(Long userId) {
		User user = (User)getDataAccessService().getDomainObject(User.class,userId,null);
		Set<Role> allActiveUserRoles = new TreeSet<Role>();
		allActiveUserRoles.addAll(user.getRoles());
		Iterator<Role> userRolesIterator = user.getRoles().iterator();
		while(userRolesIterator.hasNext()){
			getAllChildRoles( userRolesIterator.next(),allActiveUserRoles);
		}
		
		Iterator<Role> allActiveUserRolesIterator = allActiveUserRoles.iterator();
		while(allActiveUserRolesIterator.hasNext()){
			Role role = allActiveUserRolesIterator.next();
			if(!role.getActive()){
				allActiveUserRolesIterator.remove();
			}
		}

		return allActiveUserRoles;
	}
	
	public static Set<Role> getAllChildRoles(Role role, Set<Role> allchildRoles) {
		Set<Role> childRoles = role.getChildRoles();
		allchildRoles.addAll(childRoles);
		Iterator<Role> childRolesIterator = childRoles.iterator();
		while (childRolesIterator.hasNext()) {
			getAllChildRoles(childRolesIterator.next(),allchildRoles);
		}
		return allchildRoles;
	}


	public static DataAccessService getDataAccessService() {
		return org.openecomp.portalsdk.core.web.support.AppUtils.getDataAccessService();
	}


	public static void setDataAccessService(DataAccessService dataAccessService) {
		dataAccessService = dataAccessService;
	}
	
	

}