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.portalapp.portal.service;
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
27 import javax.annotation.PostConstruct;
28 import javax.servlet.http.HttpServletResponse;
30 import org.apache.commons.lang3.StringUtils;
31 import org.hibernate.Criteria;
32 import org.hibernate.Query;
33 import org.hibernate.Session;
34 import org.hibernate.SessionFactory;
35 import org.hibernate.Transaction;
36 import org.hibernate.criterion.Projections;
37 import org.hibernate.criterion.Restrictions;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.context.annotation.EnableAspectJAutoProxy;
40 import org.springframework.stereotype.Service;
42 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
43 import org.openecomp.portalsdk.core.service.DataAccessService;
44 import org.openecomp.portalsdk.core.util.SystemProperties;
45 import org.openecomp.portalapp.portal.domain.EPApp;
46 import org.openecomp.portalapp.portal.domain.EPUser;
47 import org.openecomp.portalapp.portal.domain.FunctionalMenuItemWithAppID;
48 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
49 import org.openecomp.portalapp.portal.transport.BusinessCardApplicationRole;
50 import org.openecomp.portalapp.portal.transport.FavoritesFunctionalMenuItem;
51 import org.openecomp.portalapp.portal.transport.FavoritesFunctionalMenuItemJson;
52 import org.openecomp.portalapp.portal.transport.FieldsValidator;
53 import org.openecomp.portalapp.portal.transport.FunctionalMenuItem;
54 import org.openecomp.portalapp.portal.transport.FunctionalMenuItemWithRoles;
55 import org.openecomp.portalapp.portal.transport.FunctionalMenuRole;
56 import org.openecomp.portalapp.portal.utils.EPCommonSystemProperties;
57 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
59 @Service("functionalMenuService")
60 @org.springframework.context.annotation.Configuration
61 @EnableAspectJAutoProxy
63 public class FunctionalMenuServiceImpl implements FunctionalMenuService {
64 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(FunctionalMenuServiceImpl.class);
66 private Long ACCOUNT_ADMIN_ROLE_ID = 999L;
67 private String RESTRICTED_APP_ROLE_ID = "900";
70 private DataAccessService dataAccessService;
72 private SessionFactory sessionFactory;
77 ACCOUNT_ADMIN_ROLE_ID = Long.valueOf(SystemProperties.getProperty(EPCommonSystemProperties.ACCOUNT_ADMIN_ROLE_ID));
78 RESTRICTED_APP_ROLE_ID = SystemProperties.getProperty(EPCommonSystemProperties.RESTRICTED_APP_ROLE_ID);
79 } catch(Exception e) {
83 public List<FunctionalMenuItem> getFunctionalMenuItems(EPUser user) {
84 List<FunctionalMenuItem> menuItems = new ArrayList<FunctionalMenuItem>();
88 public List<FunctionalMenuItem> getFunctionalMenuItems() {
89 return getFunctionalMenuItems(false);
92 public List<FunctionalMenuItem> getFunctionalMenuItems(Boolean all) {
93 // Divide this into 2 queries: one which returns the bottom-level menu items associated with Restricted apps,
94 // and one that returns all the other menu items. Then we can easily add the boolean flag
95 // restrictedApp to each FunctionalMenuItem, to be used by the front end.
96 String activeWhereClause = "";
98 activeWhereClause = " AND UPPER(m.active_yn) = 'Y' ";
100 String sql = "SELECT m.menu_id, m.column_num, m.text, m.parent_menu_id, m.url, m.active_yn, r.app_id "
101 + "FROM fn_menu_functional m, fn_menu_functional_roles r "
102 + "WHERE m.menu_id = r.menu_id "
103 + activeWhereClause //" AND UPPER(m.active_yn) = 'Y' "
104 + " AND r.role_id != '" + RESTRICTED_APP_ROLE_ID + "' "
106 + " SELECT m.menu_id, m.column_num, m.text, m.parent_menu_id, m.url, m.active_yn,-1 app_id "
107 + " FROM fn_menu_functional m "
109 + activeWhereClause; //" AND UPPER(m.active_yn) = 'Y' ";
112 @SuppressWarnings("unchecked")
113 List<FunctionalMenuItemWithAppID> menuItemsWithAppIdList = dataAccessService.executeSQLQuery(sql,FunctionalMenuItemWithAppID.class, null);
114 List<FunctionalMenuItem> menuItems = new ArrayList<>();
115 menuItems = transformFunctionalMenuItemWithAppIDToFunctionalMenuItem(menuItemsWithAppIdList);
116 for (FunctionalMenuItem menuItem : menuItems) {
117 menuItem.restrictedApp = false;
120 sql = "SELECT m.menu_id, m.column_num, m.text, m.parent_menu_id, m.url, m.active_yn, r.app_id "
121 + "FROM fn_menu_functional m, fn_menu_functional_roles r "
122 + "WHERE m.menu_id = r.menu_id "
123 + activeWhereClause //" AND UPPER(m.active_yn) = 'Y' "
124 + " AND r.role_id = '" + RESTRICTED_APP_ROLE_ID + "' ";
126 @SuppressWarnings("unchecked")
127 List<FunctionalMenuItem> menuItems2 = dataAccessService.executeSQLQuery(sql, FunctionalMenuItem.class, null);
128 for (FunctionalMenuItem menuItem : menuItems2) {
129 menuItem.restrictedApp = true;
130 menuItems.add(menuItem);
137 public List<FunctionalMenuItem> getFunctionalMenuItemsForNotificationTree(Boolean all) {
138 // Divide this into 2 queries: one which returns the bottom-level menu items associated with Restricted apps,
139 // and one that returns all the other menu items which are active. Then we can easily add the boolean flag
140 // restrictedApp to each FunctionalMenuItem, to be used by the front end.
141 String activeWhereClause = "";
143 activeWhereClause = " AND UPPER(m.active_yn) = 'Y' ";
145 String sql = "SELECT m.menu_id, m.column_num, m.text, m.parent_menu_id, m.url, m.active_yn, r.app_id "
146 + "FROM fn_menu_functional m, fn_menu_functional_roles r "
147 + "WHERE m.menu_id = r.menu_id "
148 + activeWhereClause + " AND UPPER(m.active_yn) = 'Y' "
149 + " AND r.role_id != '" + RESTRICTED_APP_ROLE_ID + "' "
151 + " SELECT m.menu_id, m.column_num, m.text, m.parent_menu_id, m.url, m.active_yn,-1 app_id "
152 + " FROM fn_menu_functional m "
154 + activeWhereClause + " AND UPPER(m.active_yn) = 'Y' ";
157 @SuppressWarnings("unchecked")
158 List<FunctionalMenuItemWithAppID> menuItemsWithAppIdList = dataAccessService.executeSQLQuery(sql,FunctionalMenuItemWithAppID.class, null);
159 List<FunctionalMenuItem> menuItems = new ArrayList<>();
160 menuItems = transformFunctionalMenuItemWithAppIDToFunctionalMenuItem(menuItemsWithAppIdList);
161 for (FunctionalMenuItem menuItem : menuItems) {
162 menuItem.restrictedApp = false;
165 sql = "SELECT m.menu_id, m.column_num, m.text, m.parent_menu_id, m.url, m.active_yn, r.app_id "
166 + "FROM fn_menu_functional m, fn_menu_functional_roles r "
167 + "WHERE m.menu_id = r.menu_id "
168 + activeWhereClause + " AND UPPER(m.active_yn) = 'Y' "
169 + " AND r.role_id = '" + RESTRICTED_APP_ROLE_ID + "' ";
171 @SuppressWarnings("unchecked")
172 List<FunctionalMenuItem> menuItems2 = dataAccessService.executeSQLQuery(sql, FunctionalMenuItem.class, null);
173 for (FunctionalMenuItem menuItem : menuItems2) {
174 menuItem.restrictedApp = true;
175 menuItems.add(menuItem);
181 public List<FunctionalMenuItem> getFunctionalMenuItemsForApp(Integer appId) {
182 String sql = "SELECT DISTINCT m1.menu_id, m1.column_num, m1.text, m1.parent_menu_id, m1.url, m.active_yn "
183 + " FROM fn_menu_functional m, fn_menu_functional m1, fn_menu_functional_ancestors a, fn_menu_functional_roles mr "
184 + " WHERE " + " mr.app_id='" + appId + "' " + " AND mr.menu_id = m.menu_id " + " AND UPPER(m.active_yn) = 'Y'"
185 + " AND UPPER(m1.active_yn) ='Y'" + " AND a.menu_id = m.menu_id " + " AND a.ancestor_menu_id = m1.menu_id";
187 logger.debug(EELFLoggerDelegate.debugLogger, "getFunctionalMenuItemsForApp: logged the query");
189 @SuppressWarnings("unchecked")
190 List<FunctionalMenuItem> menuItems = dataAccessService.executeSQLQuery(sql, FunctionalMenuItem.class, null);
195 * convert List of FunctionalMenuItemWithAppID into List of FunctionalMenuItem
198 public List<FunctionalMenuItem> transformFunctionalMenuItemWithAppIDToFunctionalMenuItem(List<FunctionalMenuItemWithAppID> functionalMenuItemWithAppIDList) {
199 List<FunctionalMenuItem> functionalMenuItemList = new ArrayList<FunctionalMenuItem>();
200 for (FunctionalMenuItemWithAppID functionalMenuItemWithAppID : functionalMenuItemWithAppIDList) {
201 FunctionalMenuItem menuItem = new FunctionalMenuItem();
202 menuItem.menuId=functionalMenuItemWithAppID.menuId;
203 menuItem.column=functionalMenuItemWithAppID.column;
204 menuItem.text=functionalMenuItemWithAppID.text;
205 menuItem.parentMenuId=functionalMenuItemWithAppID.parentMenuId;
206 menuItem.url=functionalMenuItemWithAppID.url;
207 menuItem.active_yn=functionalMenuItemWithAppID.active_yn;
208 menuItem.appid=functionalMenuItemWithAppID.appid;
209 menuItem.setRoles(functionalMenuItemWithAppID.roles);
210 menuItem.restrictedApp=functionalMenuItemWithAppID.restrictedApp;
211 functionalMenuItemList.add(menuItem);
213 return functionalMenuItemList;
216 public List<FunctionalMenuItem> getFunctionalMenuItemsForUser(String orgUserId) {
217 // m represents the functional menu items that are the leaf nodes
218 // m1 represents the functional menu items for all the nodes
220 // Divide this into 2 queries: one which returns the bottom-level menu items associated with Restricted apps,
221 // and one that returns all the other menu items. Then we can easily add the boolean flag
222 // restrictedApp to each FunctionalMenuItem, to be used by the front end.
223 String sql = "SELECT DISTINCT m1.menu_id, m1.column_num, m1.text, m1.parent_menu_id, m1.url, m.active_yn "
224 + " FROM fn_menu_functional m, fn_menu_functional m1, fn_menu_functional_ancestors a, "
225 + " fn_menu_functional_roles mr, fn_user u , fn_user_role ur " + " WHERE " + " u.org_user_id='" + orgUserId
226 + "' " + " AND u.user_id = ur.user_id " + " AND ur.app_id = mr.app_id " +
227 // " AND ur.role_id = mr.role_id " +
228 " AND (ur.role_id = mr.role_id " + " OR ur.role_id = '" + ACCOUNT_ADMIN_ROLE_ID + "') "
229 + " AND m.menu_id = mr.menu_id " + " AND UPPER(m.active_yn) = 'Y'" + " AND UPPER(m1.active_yn) ='Y' "
230 + " AND a.menu_id = m.menu_id " + " AND a.ancestor_menu_id = m1.menu_id "
232 // the ancestors of the restricted app menu items
233 + " select m1.menu_id, m1.column_num, m1.text, m1.parent_menu_id, m1.url, m1.active_yn "
234 + " FROM fn_menu_functional m, fn_menu_functional_roles mr, fn_menu_functional m1, "
235 + " fn_menu_functional_ancestors a "
236 + " where a.menu_id = m.menu_id "
237 + " AND a.ancestor_menu_id = m1.menu_id "
238 + " AND m.menu_id != m1.menu_id "
239 + " AND m.menu_id = mr.menu_id "
240 + " AND mr.role_id = '" + RESTRICTED_APP_ROLE_ID + "' "
241 + " AND UPPER(m.active_yn) = 'Y'" + " AND UPPER(m1.active_yn) ='Y' "
242 // Add the Favorites menu item
244 + " SELECT m.menu_id, m.column_num, m.text, m.parent_menu_id, m.url, m.active_yn "
245 + " FROM fn_menu_functional m "
246 + " WHERE m.text in ('Favorites','Get Access','Contact Us','Support','User Guide','Help')";
249 logger.debug(EELFLoggerDelegate.debugLogger, "getFunctionalMenuItemsForUser: logged the query");
251 @SuppressWarnings("unchecked")
252 List<FunctionalMenuItem> menuItems = dataAccessService.executeSQLQuery(sql, FunctionalMenuItem.class, null);
253 for (FunctionalMenuItem menuItem : menuItems) {
254 menuItem.restrictedApp = false;
257 sql = " SELECT m.menu_id, m.column_num, m.text, m.parent_menu_id, m.url, m.active_yn "
258 + " FROM fn_menu_functional m, fn_menu_functional_roles r "
259 + " WHERE m.menu_id = r.menu_id "
260 + " AND UPPER(m.active_yn) = 'Y' "
261 + " AND r.role_id = '" + RESTRICTED_APP_ROLE_ID + "' ";
263 @SuppressWarnings("unchecked")
264 List<FunctionalMenuItem> menuItems2 = dataAccessService.executeSQLQuery(sql, FunctionalMenuItem.class, null);
265 for (FunctionalMenuItem menuItem : menuItems2) {
266 menuItem.restrictedApp = true;
267 menuItems.add(menuItem);
273 public FunctionalMenuItem getFunctionalMenuItemDetails(Integer menuid) {
274 // First, fill in the fields that apply to all menu items
276 String sql = "SELECT * FROM fn_menu_functional WHERE menu_id = '" + menuid + "'";
278 @SuppressWarnings("unchecked")
279 List<FunctionalMenuItem> menuItems = dataAccessService.executeSQLQuery(sql, FunctionalMenuItem.class, null);
280 FunctionalMenuItem menuItem = (menuItems == null || menuItems.isEmpty() ? null : menuItems.get(0));
281 // If it is a bottom-level menu item, must fill in the appid and the
283 sql = "SELECT * FROM fn_menu_functional_roles WHERE menu_id = '" + menuid + "'";
285 @SuppressWarnings("unchecked")
286 List<FunctionalMenuRole> roleItems = dataAccessService.executeSQLQuery(sql, FunctionalMenuRole.class, null);
287 if (roleItems.size() > 0) {
288 Integer appid = roleItems.get(0).appId;
289 menuItem.appid = appid;
290 List<Integer> roles = new ArrayList<Integer>();
291 for (FunctionalMenuRole roleItem : roleItems) {
292 logger.debug(EELFLoggerDelegate.debugLogger, "LR: app_id: " + roleItem.appId + "; role_id: " + roleItem.roleId + "\n");
293 roles.add(roleItem.roleId);
295 menuItem.setRoles(roles);
301 private FieldsValidator menuItemFieldsChecker(FunctionalMenuItemWithRoles menuItemJson) {
302 FieldsValidator fieldsValidator = new FieldsValidator();
304 // TODO: validate all the fields
305 @SuppressWarnings("unchecked")
306 List<FunctionalMenuItem> functionalMenuItems = dataAccessService.getList(FunctionalMenuItem.class,
307 " where text = '" + menuItemJson.text + "'", null, null);
309 boolean dublicatedName = false;
310 for (FunctionalMenuItem fnMenuItem : functionalMenuItems) {
311 if (menuItemJson.menuId != null && menuItemJson.menuId.equals(fnMenuItem.menuId)) {
312 // FunctionalMenuItem should not be compared with itself
316 if (!dublicatedName && fnMenuItem.text.equalsIgnoreCase(menuItemJson.text)) {
317 dublicatedName = true;
321 if (dublicatedName) {
322 fieldsValidator.addProblematicFieldName("text");
323 fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_CONFLICT);
324 fieldsValidator.errorCode = new Long(EPCommonSystemProperties.DUBLICATED_FIELD_VALUE_ECOMP_ERROR);
325 logger.debug(EELFLoggerDelegate.debugLogger, "In menuItemFieldsChecker, Error: we have an duplicate text field");
326 } else if (StringUtils.isEmpty(menuItemJson.text) && menuItemJson.menuId == null) {
327 // text must be non empty for a create. For an edit, can be empty, which means it is a move request.
328 // a null menuId indicates a create.
329 fieldsValidator.addProblematicFieldName("text");
330 fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_BAD_REQUEST);
331 logger.debug(EELFLoggerDelegate.debugLogger, "In menuItemFieldsChecker, Error: we have an empty text field");
333 // The url, appid, and roles must either be all filled or all empty.
334 Boolean urlIsEmpty = StringUtils.isEmpty(menuItemJson.url);
335 Boolean rolesIsEmpty = menuItemJson.getRoles() == null || menuItemJson.getRoles().isEmpty();
336 Boolean appidIsEmpty = menuItemJson.appid == null || menuItemJson.appid == 0;
337 logger.debug(EELFLoggerDelegate.debugLogger, "LR: menuItemfieldsChecker: urlIsEmpty: " + urlIsEmpty + "; rolesIsEmpty: " + rolesIsEmpty + "; appidIsEmpty: " + appidIsEmpty +"\n");
338 if (!((urlIsEmpty && rolesIsEmpty && appidIsEmpty) || (!urlIsEmpty && !rolesIsEmpty && !appidIsEmpty)))
340 fieldsValidator.addProblematicFieldName("url,roles,appid");
341 fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_BAD_REQUEST);
342 logger.debug(EELFLoggerDelegate.debugLogger, "In menuItemFieldsChecker, Error: we don't have: either all 3 fields empty or all 3 fields nonempty");
344 logger.debug(EELFLoggerDelegate.debugLogger, "In menuItemFieldsChecker, Success: either all 3 fields empty or all 3 fields nonempty");
347 } catch (Exception e) {
348 logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred while validating the FunctionalMenuItems. Details: " + EcompPortalUtils.getStackTrace(e));
349 fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
352 return fieldsValidator;
355 // Turn foreign key checks on or off
356 protected void setForeignKeys(Session localSession, Boolean on) {
357 String keyCheck = "0";
361 String sql = "set FOREIGN_KEY_CHECKS="+keyCheck;
363 Query query = localSession.createSQLQuery(sql);
364 query.executeUpdate();
367 public FieldsValidator createFunctionalMenuItem(FunctionalMenuItemWithRoles menuItemJson) {
368 FieldsValidator fieldsValidator = menuItemFieldsChecker(menuItemJson);
369 if (fieldsValidator.httpStatusCode.intValue() == HttpServletResponse.SC_OK) {
370 logger.debug(EELFLoggerDelegate.debugLogger, "LR: createFunctionalMenuItem: test 1");
371 boolean result = false;
372 Session localSession = null;
373 Transaction transaction = null;
375 FunctionalMenuItem menuItem = new FunctionalMenuItem();
376 menuItem.appid = menuItemJson.appid;
377 menuItem.setRoles(menuItemJson.getRoles());
378 menuItem.url = menuItemJson.url;
379 menuItem.text = menuItemJson.text;
380 menuItem.parentMenuId = menuItemJson.parentMenuId;
381 menuItem.active_yn = "Y";
382 localSession = sessionFactory.openSession();
384 // If the app is disabled, deactivate the menu item.
385 if (menuItemJson.appid != null) {
386 Long appidLong = Long.valueOf(menuItemJson.appid);
387 EPApp app = (EPApp) localSession.get(EPApp.class, appidLong);
388 if (app != null && ! app.getEnabled()) {
389 menuItem.active_yn = "N";
393 // Set the column number to 1 higher than the highest column
394 // number under this parent.
395 Criteria criteria = localSession.createCriteria(FunctionalMenuItem.class);
396 criteria.setProjection(Projections.max("column"));
397 criteria.add(Restrictions.eq("parentMenuId", menuItem.parentMenuId));
398 Integer maxColumn = (Integer) criteria.uniqueResult();
399 if (maxColumn == null) {
402 menuItem.column = maxColumn + 1;
403 logger.debug(EELFLoggerDelegate.debugLogger, "about to create menu item: " + menuItem.toString());
405 transaction = localSession.beginTransaction();
406 // localSession.saveOrUpdate(newMenuItem);
407 localSession.save(menuItem);
408 Long menuid = menuItem.menuId;
409 menuItemJson.menuId = menuid;
410 logger.debug(EELFLoggerDelegate.debugLogger, "after saving menu object, new id: " + menuid);
412 // Next, save all the roles
414 addRoles(menuItemJson, localSession);
415 transaction.commit();
417 } catch (Exception e) {
418 EcompPortalUtils.rollbackTransaction(transaction,
419 "createFunctionalMenuItem rollback, exception = " + e);
420 logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
422 EcompPortalUtils.closeLocalSession(localSession, "createFunctionalMenuItem");
426 logger.debug(EELFLoggerDelegate.debugLogger, "LR: createFunctionalMenuItem: no result. setting httpStatusCode to "
427 + HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
428 fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
431 logger.error(EELFLoggerDelegate.errorLogger, "FunctionalMenuServiceImpl.createFunctionalMenuItem: bad request");
433 return fieldsValidator;
436 /* Add all the roles in the menu item to the database */
437 public void addRoles(FunctionalMenuItemWithRoles menuItemJson, Session localSession) {
438 logger.debug(EELFLoggerDelegate.debugLogger, "entering addRoles.");
439 List<Integer> roles = menuItemJson.getRoles();
440 if (roles != null && roles.size() > 0) {
441 Integer appid = menuItemJson.appid;
442 Long menuid = menuItemJson.menuId;
443 for (Integer roleid : roles) {
444 logger.debug(EELFLoggerDelegate.debugLogger, "about to create record for role: " + roleid);
445 FunctionalMenuRole role = new FunctionalMenuRole();
447 role.menuId = menuid;
448 role.roleId = roleid;
449 localSession.save(role);
450 logger.debug(EELFLoggerDelegate.debugLogger, "after saving role menu object, new id: " + role.id);
455 /* Delete all the roles associated with the menu item from the database */
456 public void deleteRoles(Long menuId) {
457 dataAccessService.deleteDomainObjects(FunctionalMenuRole.class, "menu_id='" + menuId + "'", null);
460 /* Delete all favorites associated with the menu item from the database */
461 public void deleteFavorites(Long menuId) {
462 dataAccessService.deleteDomainObjects(FavoritesFunctionalMenuItem.class, "menu_id='" + menuId + "'", null);
465 private Boolean parentMenuIdEqual(Integer menuId1, Integer menuId2) {
466 return ((menuId1 == null && menuId2 == null) || (menuId1 != null && menuId1.equals(menuId2)));
469 private void updateColumnForSiblings(Session localSession, Long menuId, Integer oldParentMenuId,
470 Integer newParentMenuId, Integer oldColumn, Integer newColumn) {
471 logger.debug(EELFLoggerDelegate.debugLogger, "entering updateColumnForSiblings");
472 Criteria criteria = localSession.createCriteria(FunctionalMenuItem.class);
473 criteria.add(Restrictions.ne("menuId", menuId));
474 if (parentMenuIdEqual(oldParentMenuId, newParentMenuId)) {
475 logger.debug(EELFLoggerDelegate.debugLogger, "moving under the same parent");
476 // We are moving to a new position under the same parent
477 if (newParentMenuId == null) {
478 logger.debug(EELFLoggerDelegate.debugLogger, "newParentMenuId is null, so using isNull");
479 criteria.add(Restrictions.isNull("parentMenuId"));
481 logger.debug(EELFLoggerDelegate.debugLogger, "newParentMenuId is NOT null, so using eq");
482 criteria.add(Restrictions.eq("parentMenuId", newParentMenuId));
484 if (oldColumn > newColumn) {
485 logger.debug(EELFLoggerDelegate.debugLogger, "moving to a lower column under the same parent");
486 // We are moving to a lower column under the same parent
487 criteria.add(Restrictions.ge("column", newColumn));
488 criteria.add(Restrictions.lt("column", oldColumn));
489 @SuppressWarnings("unchecked")
490 List<FunctionalMenuItem> menuItems = criteria.list();
491 for (FunctionalMenuItem menuItem : menuItems) {
492 menuItem.column += 1;
493 localSession.save(menuItem);
495 } else if (oldColumn < newColumn) {
496 logger.debug(EELFLoggerDelegate.debugLogger, "moving to a higher column under the same parent");
497 // We are moving to a higher column under the same parent
498 criteria.add(Restrictions.gt("column", oldColumn));
499 criteria.add(Restrictions.le("column", newColumn));
500 @SuppressWarnings("unchecked")
501 List<FunctionalMenuItem> menuItems = criteria.list();
502 for (FunctionalMenuItem menuItem : menuItems) {
503 menuItem.column -= 1;
504 localSession.save(menuItem);
507 // No info has changed
508 logger.debug(EELFLoggerDelegate.debugLogger, "no info has changed, so we are not moving");
511 logger.debug(EELFLoggerDelegate.debugLogger, "moving under a new parent");
512 // We are moving under a new parent.
514 // Adjust the children under the old parent
515 logger.debug(EELFLoggerDelegate.debugLogger, "about to adjust the children under the old parent");
517 // If the parentId is null, must check for its children differently
518 if (oldParentMenuId == null) {
519 logger.debug(EELFLoggerDelegate.debugLogger, "oldParentMenuId is null, so using isNull");
520 criteria.add(Restrictions.isNull("parentMenuId"));
522 logger.debug(EELFLoggerDelegate.debugLogger, "oldParentMenuId is NOT null, so using eq");
523 criteria.add(Restrictions.eq("parentMenuId", oldParentMenuId));
526 criteria.add(Restrictions.gt("column", oldColumn));
527 @SuppressWarnings("unchecked")
528 List<FunctionalMenuItem> menuItems1 = criteria.list();
529 for (FunctionalMenuItem menuItem : menuItems1) {
530 menuItem.column -= 1;
531 localSession.save(menuItem);
533 // Adjust the children under the new parent.
534 logger.debug(EELFLoggerDelegate.debugLogger, "about to adjust the children under the new parent");
535 logger.debug(EELFLoggerDelegate.debugLogger, "get all menu items where menuId!=" + menuId + "; parentMenuId==" + newParentMenuId
536 + "; column>=" + newColumn);
537 criteria = localSession.createCriteria(FunctionalMenuItem.class);
538 criteria.add(Restrictions.ne("menuId", menuId));
539 if (newParentMenuId == null) {
540 logger.debug(EELFLoggerDelegate.debugLogger, "newParentMenuId is null, so using isNull");
541 criteria.add(Restrictions.isNull("parentMenuId"));
543 logger.debug(EELFLoggerDelegate.debugLogger, "newParentMenuId is NOT null, so using eq");
544 criteria.add(Restrictions.eq("parentMenuId", newParentMenuId));
547 criteria.add(Restrictions.ge("column", newColumn));
548 @SuppressWarnings("unchecked")
549 List<FunctionalMenuItem> menuItems2 = criteria.list();
550 if (menuItems2 != null) {
551 logger.debug(EELFLoggerDelegate.debugLogger, "found " + menuItems2.size() + " menu items");
553 logger.debug(EELFLoggerDelegate.debugLogger, "found null menu items");
555 for (FunctionalMenuItem menuItem : menuItems2) {
556 menuItem.column += 1;
557 localSession.save(menuItem);
560 logger.debug(EELFLoggerDelegate.debugLogger, "done with updateColumnForSiblings");
563 public void removeAppInfo(Session localSession, Long menuId) {
564 // Remove the url, role, and app info from a menu item
565 FunctionalMenuItem menuItem = (FunctionalMenuItem) localSession.get(FunctionalMenuItem.class, menuId);
570 public FieldsValidator editFunctionalMenuItem(FunctionalMenuItemWithRoles menuItemJson) {
571 boolean result = false;
572 Session localSession = null;
573 Transaction transaction = null;
574 Long menuId = menuItemJson.menuId;
576 logger.debug(EELFLoggerDelegate.debugLogger, "LR: editFunctionalMenuItem: test 1");
577 FieldsValidator fieldsValidator = menuItemFieldsChecker(menuItemJson);
578 if (fieldsValidator.httpStatusCode.intValue() == HttpServletResponse.SC_OK) {
579 // TODO: make sure menuId is here. And, it might not already exist
581 if (menuId == null) {
582 fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_BAD_REQUEST);
583 logger.error(EELFLoggerDelegate.errorLogger, "FunctionalMenuServiceImpl.editFunctionalMenuItem: bad request");
585 // To simplify the code, assume we will have a transaction
587 localSession = sessionFactory.openSession();
588 transaction = localSession.beginTransaction();
590 // Get the existing info associated with menuItem from the DB
591 FunctionalMenuItem menuItem = (FunctionalMenuItem) localSession.get(FunctionalMenuItem.class, menuId);
592 Integer oldColumn = menuItem.column;
593 Integer oldParentMenuId = menuItem.parentMenuId;
594 Integer newColumn = menuItemJson.column;
595 Integer newParentMenuId = menuItemJson.parentMenuId;
597 logger.debug(EELFLoggerDelegate.debugLogger, "prev info: column: " + oldColumn + "; parentMenuId: " + oldParentMenuId);
599 if (menuItemJson.appid != null && menuItemJson.getRoles() != null && !menuItemJson.getRoles().isEmpty()
600 && menuItemJson.url != null && !menuItemJson.url.isEmpty() && menuItemJson.text != null
601 && !menuItemJson.text.isEmpty()) {
602 // Scenario: appid, roles, url and text are all non-null.
603 // This menu item is associated with an app.
604 // (Note: this should only occur for a bottom-level menu
605 // item with no children.)
606 // 1) Remove all the records from fn_menu_functional_role
608 // 2) Add records to the fn_menu_function_role table for the
609 // appId and each roleId
610 // 3) Update the url and text for this menu item.
612 // Because of foreign key constraints, delete the roles,
613 // then update the menuItem then add the roles.
615 // Assumption: this is not a Move, so don't change the
616 // parentMenuId and column.
617 menuItem.appid = menuItemJson.appid;
618 menuItem.setRoles(menuItemJson.getRoles());
619 menuItem.url = menuItemJson.url;
620 menuItem.text = menuItemJson.text;
622 // If the app is disabled, deactivate the menu item.
623 Long appidLong = Long.valueOf(menuItemJson.appid);
624 EPApp app = (EPApp) localSession.get(EPApp.class, appidLong);
625 if (app != null && ! app.getEnabled()) {
626 menuItem.active_yn = "N";
628 menuItem.active_yn = "Y";
632 localSession.update(menuItem);
633 addRoles(menuItemJson, localSession);
635 } else if (menuItemJson.appid == null && (menuItemJson.getRoles() == null || menuItemJson.getRoles().isEmpty())
636 && (menuItemJson.url == null || menuItemJson.url.isEmpty()) && menuItemJson.text != null
637 && !menuItemJson.text.isEmpty()) {
638 // Scenario: appid, roles and url are all null; text is
640 // This menu item is NOT associated with an app.
641 // 1) Remove all the records from fn_menu_functional_role
643 // (in case it was previously associated with an app).
644 // 2) Update the text for this menu item.
645 // 3) Set the url to ""
647 // Assumption: this is not a Move, so don't change the
648 // parentMenuId and column.
649 menuItem.text = menuItemJson.text;
651 menuItem.active_yn = "Y";
652 localSession.update(menuItem);
654 } else if (newColumn != null) {
655 // This is a "move" request.
656 // Menu item has been moved to a different position under
657 // the same parent, or under a new parent.
658 logger.debug(EELFLoggerDelegate.debugLogger, "Doing a move operation.");
659 if (parentMenuIdEqual(oldParentMenuId, newParentMenuId)) {
660 // The parent is the same. We have just changed the
662 logger.debug(EELFLoggerDelegate.debugLogger, "moving under the same parent");
663 menuItem.column = newColumn;
664 localSession.update(menuItem);
666 logger.debug(EELFLoggerDelegate.debugLogger, "moving under a different parent");
667 menuItem.parentMenuId = newParentMenuId;
668 menuItem.column = newColumn;
669 localSession.update(menuItem);
670 // If we are moving under a new parent, must delete any
671 // app/role info from
672 // the new parent, since it is no longer a leaf menu
673 // item and cannot have app info
674 // associated with it. The front end will have warned
675 // the user and gotten confirmation.
676 if (menuItemJson.parentMenuId != null) {
677 Long parentMenuIdLong = new Long(menuItemJson.parentMenuId);
678 removeAppInfo(localSession, parentMenuIdLong);
679 // deleteRoles(parentMenuIdLong);
682 // must update the column for all old and new sibling menu
684 updateColumnForSiblings(localSession, menuId, oldParentMenuId, newParentMenuId, oldColumn,
688 transaction.commit();
689 logger.debug(EELFLoggerDelegate.debugLogger, "LR: editFunctionalMenuItem: finished committing transaction");
691 } catch (Exception e) {
692 EcompPortalUtils.rollbackTransaction(transaction,
693 "createFunctionalMenuItem rollback, exception = " + e);
694 logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
696 EcompPortalUtils.closeLocalSession(localSession, "editFunctionalMenuItem");
701 logger.debug(EELFLoggerDelegate.debugLogger, "LR: createFunctionalMenuItem: no result. setting httpStatusCode to "
702 + HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
703 fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
708 return fieldsValidator;
711 public FieldsValidator deleteFunctionalMenuItem(Long menuId) {
712 FieldsValidator fieldsValidator = new FieldsValidator();
713 logger.debug(EELFLoggerDelegate.debugLogger, "LR: deleteFunctionalMenuItem: test 1");
714 boolean result = false;
715 Session localSession = null;
716 Transaction transaction = null;
719 localSession = sessionFactory.openSession();
720 transaction = localSession.beginTransaction();
721 // We must turn off foreign keys before deleting a menu item. Otherwise there will be a
722 // constraint violation from the ancestors table.
723 setForeignKeys(localSession, false);
725 logger.debug(EELFLoggerDelegate.debugLogger, "deleteFunctionalMenuItem: after deleting roles");
726 deleteFavorites(menuId);
727 logger.debug(EELFLoggerDelegate.debugLogger, "deleteFunctionalMenuItem: after deleting favorites");
728 localSession.delete(localSession.get(FunctionalMenuItem.class, menuId));
729 logger.debug(EELFLoggerDelegate.debugLogger, "deleteFunctionalMenuItem: about to commit");
730 transaction.commit();
732 } catch (Exception e) {
733 EcompPortalUtils.rollbackTransaction(transaction,
734 "deleteFunctionalMenuItem rollback, exception = " + e);
735 logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
737 EcompPortalUtils.closeLocalSession(localSession, "deleteFunctionalMenuItem");
741 logger.debug(EELFLoggerDelegate.debugLogger, "LR: deleteFunctionalMenuItem: no result. setting httpStatusCode to "
742 + HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
743 fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
745 return fieldsValidator;
748 // Regenerate the fn_menu_functional_ancestors table, which is used
749 // by the queries that return the functional menu items.
750 public FieldsValidator regenerateAncestorTable() {
751 FieldsValidator fieldsValidator = new FieldsValidator();
752 Session localSession = null;
753 Transaction transaction = null;
756 localSession = sessionFactory.openSession();
757 transaction = localSession.beginTransaction();
758 String sql = "DELETE FROM fn_menu_functional_ancestors";
760 Query query = localSession.createSQLQuery(sql);
761 query.executeUpdate();
762 logger.debug(EELFLoggerDelegate.debugLogger, "regenerateAncestorTable: finished query 1");
764 sql = "ALTER TABLE fn_menu_functional_ancestors AUTO_INCREMENT=1";
766 query = localSession.createSQLQuery(sql);
767 query.executeUpdate();
768 logger.debug(EELFLoggerDelegate.debugLogger, "regenerateAncestorTable: reset AUTO_INCREMENT to 1");
771 sql = "INSERT INTO fn_menu_functional_ancestors(menu_id, ancestor_menu_id, depth) "
772 + "SELECT m.menu_id, m.menu_id, " + depth + " FROM fn_menu_functional m";
774 query = localSession.createSQLQuery(sql);
775 query.executeUpdate();
776 logger.debug(EELFLoggerDelegate.debugLogger, "regenerateAncestorTable: finished query 2");
777 for (depth = 0; depth < 3; depth++) {
778 int depthPlusOne = depth + 1;
779 sql = "INSERT INTO fn_menu_functional_ancestors(menu_id, ancestor_menu_id, depth) "
780 + " SELECT a.menu_id, m.parent_menu_id, " + depthPlusOne
781 + " FROM fn_menu_functional m, fn_menu_functional_ancestors a " + " WHERE a.depth='" + depth
782 + "' AND " + " a.ancestor_menu_id = m.menu_id AND " + " m.parent_menu_id != '-1'";
784 query = localSession.createSQLQuery(sql);
785 query.executeUpdate();
787 logger.debug(EELFLoggerDelegate.debugLogger, "regenerateAncestorTable: finished query 3");
788 transaction.commit();
789 } catch (Exception e) {
790 EcompPortalUtils.rollbackTransaction(transaction,
791 "regenerateAncestorTable rollback, exception = " + e);
792 logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
793 fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
795 EcompPortalUtils.closeLocalSession(localSession, "regenerateAncestorTable");
797 return fieldsValidator;
800 private void logQuery(String sql) {
801 logger.debug(EELFLoggerDelegate.debugLogger, "Executing query: " + sql);
804 public FieldsValidator setFavoriteItem(FavoritesFunctionalMenuItem menuItemJson) {
805 boolean result = false;
806 FieldsValidator fieldsValidator = new FieldsValidator();
808 Session localSession = null;
809 Transaction transaction = null;
812 logger.debug(EELFLoggerDelegate.debugLogger, String.format("Before adding favorite for user id:{0} and menu id:{1} ",menuItemJson.userId,menuItemJson.menuId));
813 localSession = sessionFactory.openSession();
814 transaction = localSession.beginTransaction();
815 localSession.save(menuItemJson);
816 transaction.commit();
818 logger.debug(EELFLoggerDelegate.debugLogger, String.format("After adding favorite for user id:{0} and menu id:{1} ",menuItemJson.userId,menuItemJson.menuId));
819 } catch (Exception e) {
820 EcompPortalUtils.rollbackTransaction(transaction,"setFavoriteItem rollback, exception = " + e);
821 logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
823 EcompPortalUtils.closeLocalSession(localSession, "setFavoriteItem");
829 fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
832 return fieldsValidator;
835 public List<FavoritesFunctionalMenuItemJson> getFavoriteItems(Long userId) {
837 logger.debug(EELFLoggerDelegate.debugLogger, "Before getting favorites for user id: " + userId);
839 // Divide this into 2 queries: one which returns the favorites items associated with Restricted apps,
840 // and one that returns all the other favorites items. Then we can easily add the boolean flag
841 // restrictedApp to each FavoritesFunctionalMenuItemJson, to be used by the front end.
843 String sql = "SELECT f.user_id,f.menu_id,m.text,m.url "
844 + " FROM fn_menu_favorites f, fn_menu_functional m, fn_menu_functional_roles mr "
845 + " WHERE f.user_id='" + userId + "' AND f.menu_id = m.menu_id "
846 + " AND f.menu_id = mr.menu_id "
847 + " AND mr.role_id = '" + RESTRICTED_APP_ROLE_ID + "' ";
849 @SuppressWarnings("unchecked")
850 List<FavoritesFunctionalMenuItemJson> menuItems = dataAccessService.executeSQLQuery(sql, FavoritesFunctionalMenuItemJson.class, null);
851 for (FavoritesFunctionalMenuItemJson menuItem : menuItems) {
852 menuItem.restrictedApp = true;
855 sql = "SELECT DISTINCT f.user_id,f.menu_id,m.text,m.url "
856 + " FROM fn_menu_favorites f, fn_menu_functional m, fn_menu_functional_roles mr "
857 + " WHERE f.user_id='" + userId + "' AND f.menu_id = m.menu_id "
858 + " AND f.menu_id = mr.menu_id "
859 + " AND mr.role_id != '" + RESTRICTED_APP_ROLE_ID + "' ";
860 @SuppressWarnings("unchecked")
861 List<FavoritesFunctionalMenuItemJson> menuItems2 = dataAccessService.executeSQLQuery(sql, FavoritesFunctionalMenuItemJson.class, null);
862 for (FavoritesFunctionalMenuItemJson menuItem : menuItems2) {
863 menuItem.restrictedApp = false;
864 menuItems.add(menuItem);
868 } catch (Exception e) {
869 logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred in FunctionalMenuServiceImpl.getFavoriteItems. Details: " + EcompPortalUtils.getStackTrace(e));
870 List<FavoritesFunctionalMenuItemJson> menuItems = new ArrayList<FavoritesFunctionalMenuItemJson>();
875 public FieldsValidator removeFavoriteItem(Long userId, Long menuId) {
876 boolean result = false;
877 FieldsValidator fieldsValidator = new FieldsValidator();
879 Session localSession = null;
880 Transaction transaction = null;
884 FavoritesFunctionalMenuItem menuItemJson = new FavoritesFunctionalMenuItem();
885 menuItemJson.userId = userId;
886 menuItemJson.menuId = menuId;
888 localSession = sessionFactory.openSession();
889 transaction = localSession.beginTransaction();
890 localSession.delete(menuItemJson);
891 localSession.flush();
892 transaction.commit();
894 logger.debug(EELFLoggerDelegate.debugLogger, String.format("After removing favorite for user id: " + userId + "; menu id: " + menuId));
895 } catch (Exception e) {
896 EcompPortalUtils.rollbackTransaction(transaction,"removeFavoriteItem rollback, exception = " + e);
897 logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
899 EcompPortalUtils.closeLocalSession(localSession, "removeFavoriteItem");
905 fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
908 return fieldsValidator;
912 public void assignHelpURLs(List<FunctionalMenuItem> menuItems) {
914 String user_guide_link = SystemProperties.getProperty(EPCommonSystemProperties.USER_GUIDE_URL);
916 for(FunctionalMenuItem menuItem: menuItems){
917 if(menuItem.text.equalsIgnoreCase("Contact Us")){
918 menuItem.setUrl("contactUs");
919 //menuItem.setRestrictedApp(true);
921 if(menuItem.text.equalsIgnoreCase("Get Access")) {
922 menuItem.setUrl("getAccess");
924 if(menuItem.text.equalsIgnoreCase("User Guide")) {
925 menuItem.setUrl(user_guide_link);
926 menuItem.setRestrictedApp(true);
929 } catch (Exception e) {
930 logger.error(EELFLoggerDelegate.errorLogger, "assignHelpURLs process failed. Details: " + EcompPortalUtils.getStackTrace(e));
935 public List<FunctionalMenuRole> getFunctionalMenuRole() {
936 String sql = "SELECT * from fn_menu_functional_roles";
938 logger.debug(EELFLoggerDelegate.debugLogger, "getFunctionalMenuRole: logged the query");
940 @SuppressWarnings("unchecked")
941 List<FunctionalMenuRole> functionalMenuRole = dataAccessService.executeSQLQuery(sql, FunctionalMenuRole.class, null);
943 return functionalMenuRole;
946 @SuppressWarnings("unchecked")
948 public List<BusinessCardApplicationRole> getUserAppRolesList(String userId) {
949 Map<String, String> params = new HashMap<>();
950 params.put("userId", userId);
952 List<BusinessCardApplicationRole> userAppRoles = null;
954 userAppRoles = dataAccessService
955 .executeNamedQuery("getUserApproles", params, null);
956 } catch (Exception e) {
957 // TODO Auto-generated catch block
958 logger.error(EELFLoggerDelegate.errorLogger, "getUserAppRolesList failed", e);