[PORTAL-16 PORTAL-18] Widget ms; staging
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / service / FunctionalMenuServiceImpl.java
index a6a1120..6a71bc7 100644 (file)
-/*-\r
- * ================================================================================\r
- * ECOMP Portal\r
- * ================================================================================\r
- * Copyright (C) 2017 AT&T Intellectual Property\r
- * ================================================================================\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- * \r
- *      http://www.apache.org/licenses/LICENSE-2.0\r
- * \r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- * ================================================================================\r
- */\r
-package org.openecomp.portalapp.portal.service;\r
-\r
-import java.util.ArrayList;\r
-import java.util.HashMap;\r
-import java.util.List;\r
-import java.util.Map;\r
-\r
-import javax.annotation.PostConstruct;\r
-import javax.servlet.http.HttpServletResponse;\r
-\r
-import org.apache.commons.lang3.StringUtils;\r
-import org.hibernate.Criteria;\r
-import org.hibernate.Query;\r
-import org.hibernate.Session;\r
-import org.hibernate.SessionFactory;\r
-import org.hibernate.Transaction;\r
-import org.hibernate.criterion.Projections;\r
-import org.hibernate.criterion.Restrictions;\r
-import org.springframework.beans.factory.annotation.Autowired;\r
-import org.springframework.context.annotation.EnableAspectJAutoProxy;\r
-import org.springframework.stereotype.Service;\r
-\r
-import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;\r
-import org.openecomp.portalsdk.core.service.DataAccessService;\r
-import org.openecomp.portalsdk.core.util.SystemProperties;\r
-import org.openecomp.portalapp.portal.domain.EPApp;\r
-import org.openecomp.portalapp.portal.domain.EPUser;\r
-import org.openecomp.portalapp.portal.domain.FunctionalMenuItemWithAppID;\r
-import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;\r
-import org.openecomp.portalapp.portal.transport.BusinessCardApplicationRole;\r
-import org.openecomp.portalapp.portal.transport.FavoritesFunctionalMenuItem;\r
-import org.openecomp.portalapp.portal.transport.FavoritesFunctionalMenuItemJson;\r
-import org.openecomp.portalapp.portal.transport.FieldsValidator;\r
-import org.openecomp.portalapp.portal.transport.FunctionalMenuItem;\r
-import org.openecomp.portalapp.portal.transport.FunctionalMenuItemWithRoles;\r
-import org.openecomp.portalapp.portal.transport.FunctionalMenuRole;\r
-import org.openecomp.portalapp.portal.utils.EPCommonSystemProperties;\r
-import org.openecomp.portalapp.portal.utils.EcompPortalUtils;\r
-\r
-@Service("functionalMenuService")\r
-@org.springframework.context.annotation.Configuration\r
-@EnableAspectJAutoProxy\r
-@EPMetricsLog\r
-public class FunctionalMenuServiceImpl implements FunctionalMenuService {\r
-       EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(FunctionalMenuServiceImpl.class);\r
-\r
-       private Long ACCOUNT_ADMIN_ROLE_ID = 999L;\r
-       private String RESTRICTED_APP_ROLE_ID = "900";\r
-\r
-       @Autowired\r
-       private DataAccessService dataAccessService;\r
-       @Autowired\r
-       private SessionFactory sessionFactory;\r
-\r
-       @PostConstruct\r
-       private void init() {\r
-               try {\r
-                       ACCOUNT_ADMIN_ROLE_ID = Long.valueOf(SystemProperties.getProperty(EPCommonSystemProperties.ACCOUNT_ADMIN_ROLE_ID));\r
-                       RESTRICTED_APP_ROLE_ID = SystemProperties.getProperty(EPCommonSystemProperties.RESTRICTED_APP_ROLE_ID);\r
-               } catch(Exception e) {\r
-               }\r
-       }\r
-       \r
-       public List<FunctionalMenuItem> getFunctionalMenuItems(EPUser user) {\r
-               List<FunctionalMenuItem> menuItems = new ArrayList<FunctionalMenuItem>();\r
-               return menuItems;\r
-       }\r
-\r
-       public List<FunctionalMenuItem> getFunctionalMenuItems() {\r
-               return getFunctionalMenuItems(false);\r
-       }\r
-       \r
-       public List<FunctionalMenuItem> getFunctionalMenuItems(Boolean all) {\r
-               // Divide this into 2 queries: one which returns the bottom-level menu items associated with Restricted apps,\r
-               // and one that returns all the other menu items. Then we can easily add the boolean flag\r
-               // restrictedApp to each FunctionalMenuItem, to be used by the front end.\r
-               String activeWhereClause = "";\r
-               if (! all) {\r
-                       activeWhereClause = " AND UPPER(m.active_yn) = 'Y' ";\r
-               }\r
-               String sql = "SELECT m.menu_id, m.column_num, m.text, m.parent_menu_id, m.url, m.active_yn, r.app_id "\r
-                               + "FROM fn_menu_functional m, fn_menu_functional_roles r "\r
-                               + "WHERE m.menu_id = r.menu_id "\r
-                               + activeWhereClause //" AND UPPER(m.active_yn) = 'Y' "\r
-                               + " AND r.role_id != '" + RESTRICTED_APP_ROLE_ID + "' "\r
-                       + " UNION "\r
-                       + " SELECT m.menu_id, m.column_num, m.text, m.parent_menu_id, m.url, m.active_yn,-1 app_id "\r
-                               + " FROM fn_menu_functional m "\r
-                               + " WHERE m.url='' "\r
-                               + activeWhereClause; //" AND UPPER(m.active_yn) = 'Y' ";\r
-               logQuery(sql);\r
-\r
-               @SuppressWarnings("unchecked")\r
-               List<FunctionalMenuItemWithAppID> menuItemsWithAppIdList = dataAccessService.executeSQLQuery(sql,FunctionalMenuItemWithAppID.class, null);\r
-               List<FunctionalMenuItem> menuItems = new ArrayList<>(); \r
-               menuItems = transformFunctionalMenuItemWithAppIDToFunctionalMenuItem(menuItemsWithAppIdList);\r
-               for (FunctionalMenuItem menuItem : menuItems) {\r
-                       menuItem.restrictedApp = false;\r
-               }\r
-               \r
-               sql = "SELECT m.menu_id, m.column_num, m.text, m.parent_menu_id, m.url, m.active_yn, r.app_id "\r
-                               + "FROM fn_menu_functional m, fn_menu_functional_roles r "\r
-                               + "WHERE m.menu_id = r.menu_id "\r
-                               + activeWhereClause //" AND UPPER(m.active_yn) = 'Y' "\r
-                               + " AND r.role_id = '" + RESTRICTED_APP_ROLE_ID + "' ";\r
-               logQuery(sql);\r
-               @SuppressWarnings("unchecked")\r
-               List<FunctionalMenuItem> menuItems2 = dataAccessService.executeSQLQuery(sql, FunctionalMenuItem.class, null);\r
-               for (FunctionalMenuItem menuItem : menuItems2) {\r
-                       menuItem.restrictedApp = true;\r
-                       menuItems.add(menuItem);\r
-               }\r
-               \r
-               return menuItems;\r
-       }\r
-\r
-       \r
-       public List<FunctionalMenuItem> getFunctionalMenuItemsForNotificationTree(Boolean all) {\r
-               // Divide this into 2 queries: one which returns the bottom-level menu items associated with Restricted apps,\r
-               // and one that returns all the other menu items which are active. Then we can easily add the boolean flag\r
-               // restrictedApp to each FunctionalMenuItem, to be used by the front end.\r
-               String activeWhereClause = "";\r
-               if (! all) {\r
-                       activeWhereClause = " AND UPPER(m.active_yn) = 'Y' ";\r
-               }\r
-               String sql = "SELECT m.menu_id, m.column_num, m.text, m.parent_menu_id, m.url, m.active_yn, r.app_id "\r
-                               + "FROM fn_menu_functional m, fn_menu_functional_roles r "\r
-                               + "WHERE m.menu_id = r.menu_id "\r
-                               + activeWhereClause + " AND UPPER(m.active_yn) = 'Y' "\r
-                               + " AND r.role_id != '" + RESTRICTED_APP_ROLE_ID + "' "\r
-                       + " UNION "\r
-                       + " SELECT m.menu_id, m.column_num, m.text, m.parent_menu_id, m.url, m.active_yn,-1 app_id "\r
-                               + " FROM fn_menu_functional m "\r
-                               + " WHERE m.url='' "\r
-                               + activeWhereClause + " AND UPPER(m.active_yn) = 'Y' ";\r
-               logQuery(sql);\r
-\r
-               @SuppressWarnings("unchecked")\r
-               List<FunctionalMenuItemWithAppID> menuItemsWithAppIdList = dataAccessService.executeSQLQuery(sql,FunctionalMenuItemWithAppID.class, null);\r
-               List<FunctionalMenuItem> menuItems = new ArrayList<>(); \r
-               menuItems = transformFunctionalMenuItemWithAppIDToFunctionalMenuItem(menuItemsWithAppIdList);\r
-               for (FunctionalMenuItem menuItem : menuItems) {\r
-                       menuItem.restrictedApp = false;\r
-               }\r
-               \r
-               sql = "SELECT m.menu_id, m.column_num, m.text, m.parent_menu_id, m.url, m.active_yn, r.app_id "\r
-                               + "FROM fn_menu_functional m, fn_menu_functional_roles r "\r
-                               + "WHERE m.menu_id = r.menu_id "\r
-                               + activeWhereClause + " AND UPPER(m.active_yn) = 'Y' "\r
-                               + " AND r.role_id = '" + RESTRICTED_APP_ROLE_ID + "' ";\r
-               logQuery(sql);\r
-               @SuppressWarnings("unchecked")\r
-               List<FunctionalMenuItem> menuItems2 = dataAccessService.executeSQLQuery(sql, FunctionalMenuItem.class, null);\r
-               for (FunctionalMenuItem menuItem : menuItems2) {\r
-                       menuItem.restrictedApp = true;\r
-                       menuItems.add(menuItem);\r
-               }\r
-               \r
-               return menuItems;\r
-       }\r
-\r
-       public List<FunctionalMenuItem> getFunctionalMenuItemsForApp(Integer appId) {\r
-               String sql = "SELECT DISTINCT m1.menu_id, m1.column_num, m1.text, m1.parent_menu_id, m1.url, m.active_yn "\r
-                               + " FROM fn_menu_functional m, fn_menu_functional m1, fn_menu_functional_ancestors a, fn_menu_functional_roles mr "\r
-                               + " WHERE " + " mr.app_id='" + appId + "' " + " AND mr.menu_id = m.menu_id " + " AND UPPER(m.active_yn) = 'Y'"\r
-                               + " AND UPPER(m1.active_yn) ='Y'" + " AND a.menu_id = m.menu_id " + " AND a.ancestor_menu_id = m1.menu_id";\r
-               logQuery(sql);\r
-               logger.debug(EELFLoggerDelegate.debugLogger, "getFunctionalMenuItemsForApp: logged the query");\r
-\r
-               @SuppressWarnings("unchecked")\r
-               List<FunctionalMenuItem> menuItems = dataAccessService.executeSQLQuery(sql, FunctionalMenuItem.class, null);\r
-               \r
-               return menuItems;\r
-       }\r
-       /**\r
-        * convert List of FunctionalMenuItemWithAppID into List of FunctionalMenuItem\r
-        * \r
-        */\r
-       public List<FunctionalMenuItem> transformFunctionalMenuItemWithAppIDToFunctionalMenuItem(List<FunctionalMenuItemWithAppID> functionalMenuItemWithAppIDList) {\r
-               List<FunctionalMenuItem> functionalMenuItemList = new ArrayList<FunctionalMenuItem>();\r
-               for (FunctionalMenuItemWithAppID functionalMenuItemWithAppID : functionalMenuItemWithAppIDList) {\r
-                       FunctionalMenuItem menuItem = new FunctionalMenuItem();\r
-                       menuItem.menuId=functionalMenuItemWithAppID.menuId;\r
-                       menuItem.column=functionalMenuItemWithAppID.column;\r
-                       menuItem.text=functionalMenuItemWithAppID.text;\r
-                       menuItem.parentMenuId=functionalMenuItemWithAppID.parentMenuId;\r
-                       menuItem.url=functionalMenuItemWithAppID.url;\r
-                       menuItem.active_yn=functionalMenuItemWithAppID.active_yn;\r
-                       menuItem.appid=functionalMenuItemWithAppID.appid;\r
-                       menuItem.setRoles(functionalMenuItemWithAppID.roles);\r
-                       menuItem.restrictedApp=functionalMenuItemWithAppID.restrictedApp;\r
-                       functionalMenuItemList.add(menuItem);\r
-               }\r
-               return functionalMenuItemList;\r
-       }\r
-       \r
-       public List<FunctionalMenuItem> getFunctionalMenuItemsForUser(String orgUserId) {\r
-               // m represents the functional menu items that are the leaf nodes\r
-               // m1 represents the functional menu items for all the nodes\r
-\r
-               // Divide this into 2 queries: one which returns the bottom-level menu items associated with Restricted apps,\r
-               // and one that returns all the other menu items. Then we can easily add the boolean flag\r
-               // restrictedApp to each FunctionalMenuItem, to be used by the front end.\r
-               String sql = "SELECT DISTINCT m1.menu_id, m1.column_num, m1.text, m1.parent_menu_id, m1.url, m.active_yn "\r
-                               + " FROM fn_menu_functional m, fn_menu_functional m1, fn_menu_functional_ancestors a, "\r
-                               + " fn_menu_functional_roles mr, fn_user u , fn_user_role ur " + " WHERE " + " u.org_user_id='" + orgUserId\r
-                               + "' " + " AND u.user_id = ur.user_id " + " AND ur.app_id = mr.app_id " +\r
-                               // " AND ur.role_id = mr.role_id " +\r
-                               " AND (ur.role_id = mr.role_id " + "     OR ur.role_id = '" + ACCOUNT_ADMIN_ROLE_ID + "') "\r
-                               + " AND m.menu_id = mr.menu_id " + " AND UPPER(m.active_yn) = 'Y'" + " AND UPPER(m1.active_yn) ='Y' "\r
-                               + " AND a.menu_id = m.menu_id " + " AND a.ancestor_menu_id = m1.menu_id "\r
-                               + " UNION "\r
-                               // the ancestors of the restricted app menu items\r
-                               + " select m1.menu_id, m1.column_num, m1.text, m1.parent_menu_id, m1.url, m1.active_yn "\r
-                               + " FROM fn_menu_functional m, fn_menu_functional_roles mr, fn_menu_functional m1, "\r
-                               + " fn_menu_functional_ancestors a "\r
-                               + " where a.menu_id = m.menu_id "\r
-                               + " AND a.ancestor_menu_id = m1.menu_id "\r
-                               + " AND m.menu_id != m1.menu_id "\r
-                               + " AND m.menu_id = mr.menu_id "\r
-                               + " AND mr.role_id = '" + RESTRICTED_APP_ROLE_ID + "' "\r
-                               + " AND UPPER(m.active_yn) = 'Y'" + " AND UPPER(m1.active_yn) ='Y' "\r
-                               // Add the Favorites menu item\r
-                               + " UNION "\r
-                               + " SELECT m.menu_id, m.column_num, m.text, m.parent_menu_id, m.url, m.active_yn "\r
-                               + " FROM fn_menu_functional m "\r
-                               + " WHERE m.text in ('Favorites','Get Access','Contact Us','Support','User Guide','Help')";\r
-               \r
-               logQuery(sql);\r
-               logger.debug(EELFLoggerDelegate.debugLogger, "getFunctionalMenuItemsForUser: logged the query");\r
-\r
-               @SuppressWarnings("unchecked")\r
-               List<FunctionalMenuItem> menuItems = dataAccessService.executeSQLQuery(sql, FunctionalMenuItem.class, null);\r
-               for (FunctionalMenuItem menuItem : menuItems) {\r
-                       menuItem.restrictedApp = false;\r
-               }\r
-               \r
-               sql = " SELECT m.menu_id, m.column_num, m.text, m.parent_menu_id, m.url, m.active_yn "\r
-                       + " FROM fn_menu_functional m, fn_menu_functional_roles r "\r
-                       + " WHERE m.menu_id = r.menu_id "\r
-                       + " AND UPPER(m.active_yn) = 'Y' "\r
-                       + " AND r.role_id = '" + RESTRICTED_APP_ROLE_ID + "' ";\r
-               logQuery(sql);\r
-               @SuppressWarnings("unchecked")\r
-               List<FunctionalMenuItem> menuItems2 = dataAccessService.executeSQLQuery(sql, FunctionalMenuItem.class, null);\r
-               for (FunctionalMenuItem menuItem : menuItems2) {\r
-                       menuItem.restrictedApp = true;\r
-                       menuItems.add(menuItem);\r
-               }\r
-               \r
-               return menuItems;\r
-       }\r
-\r
-       public FunctionalMenuItem getFunctionalMenuItemDetails(Integer menuid) {\r
-               // First, fill in the fields that apply to all menu items\r
-\r
-               String sql = "SELECT * FROM fn_menu_functional WHERE menu_id = '" + menuid + "'";\r
-               logQuery(sql);\r
-               @SuppressWarnings("unchecked")\r
-               List<FunctionalMenuItem> menuItems = dataAccessService.executeSQLQuery(sql, FunctionalMenuItem.class, null);\r
-               FunctionalMenuItem menuItem = (menuItems == null || menuItems.isEmpty() ? null : menuItems.get(0));\r
-               // If it is a bottom-level menu item, must fill in the appid and the\r
-               // roles\r
-               sql = "SELECT * FROM fn_menu_functional_roles WHERE menu_id = '" + menuid + "'";\r
-               logQuery(sql);\r
-               @SuppressWarnings("unchecked")\r
-               List<FunctionalMenuRole> roleItems = dataAccessService.executeSQLQuery(sql, FunctionalMenuRole.class, null);\r
-               if (roleItems.size() > 0) {\r
-                       Integer appid = roleItems.get(0).appId;\r
-                       menuItem.appid = appid;\r
-                       List<Integer> roles = new ArrayList<Integer>();\r
-                       for (FunctionalMenuRole roleItem : roleItems) {\r
-                               logger.debug(EELFLoggerDelegate.debugLogger, "LR: app_id: " + roleItem.appId + "; role_id: " + roleItem.roleId + "\n");\r
-                               roles.add(roleItem.roleId);\r
-                       }\r
-                       menuItem.setRoles(roles);\r
-               }\r
-               \r
-               return menuItem;\r
-       }\r
-\r
-       private FieldsValidator menuItemFieldsChecker(FunctionalMenuItemWithRoles menuItemJson) {\r
-               FieldsValidator fieldsValidator = new FieldsValidator();\r
-               try {\r
-                       // TODO: validate all the fields\r
-                       @SuppressWarnings("unchecked")\r
-                       List<FunctionalMenuItem> functionalMenuItems = dataAccessService.getList(FunctionalMenuItem.class,\r
-                                       " where text = '" + menuItemJson.text + "'", null, null);\r
-                       \r
-                       boolean dublicatedName = false;\r
-                       for (FunctionalMenuItem fnMenuItem : functionalMenuItems) {\r
-                               if (menuItemJson.menuId != null && menuItemJson.menuId.equals(fnMenuItem.menuId)) {\r
-                                       // FunctionalMenuItem should not be compared with itself\r
-                                       continue;\r
-                               }\r
-\r
-                               if (!dublicatedName && fnMenuItem.text.equalsIgnoreCase(menuItemJson.text)) {\r
-                                       dublicatedName = true;\r
-                                       break;\r
-                               }\r
-                       }\r
-                       if (dublicatedName) {\r
-                               fieldsValidator.addProblematicFieldName("text");\r
-                               fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_CONFLICT);\r
-                               fieldsValidator.errorCode = new Long(EPCommonSystemProperties.DUBLICATED_FIELD_VALUE_ECOMP_ERROR);\r
-                               logger.debug(EELFLoggerDelegate.debugLogger, "In menuItemFieldsChecker, Error: we have an duplicate text field");\r
-                       } else if (StringUtils.isEmpty(menuItemJson.text) && menuItemJson.menuId == null) { \r
-                               // text must be non empty for a create. For an edit, can be empty, which means it is a move request.\r
-                               // a null menuId indicates a create.\r
-                               fieldsValidator.addProblematicFieldName("text");\r
-                               fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_BAD_REQUEST);\r
-                               logger.debug(EELFLoggerDelegate.debugLogger, "In menuItemFieldsChecker, Error: we have an empty text field");\r
-                       } else {\r
-                               // The url, appid, and roles must either be all filled or all empty.\r
-                               Boolean urlIsEmpty = StringUtils.isEmpty(menuItemJson.url);\r
-                               Boolean rolesIsEmpty = menuItemJson.getRoles() == null || menuItemJson.getRoles().isEmpty();\r
-                               Boolean appidIsEmpty = menuItemJson.appid == null || menuItemJson.appid == 0;\r
-                               logger.debug(EELFLoggerDelegate.debugLogger, "LR: menuItemfieldsChecker: urlIsEmpty: " + urlIsEmpty + "; rolesIsEmpty: " + rolesIsEmpty + "; appidIsEmpty: " + appidIsEmpty +"\n");\r
-                               if (!((urlIsEmpty && rolesIsEmpty && appidIsEmpty) || (!urlIsEmpty && !rolesIsEmpty && !appidIsEmpty)))\r
-                               {\r
-                                       fieldsValidator.addProblematicFieldName("url,roles,appid");\r
-                                       fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_BAD_REQUEST);\r
-                                       logger.debug(EELFLoggerDelegate.debugLogger, "In menuItemFieldsChecker, Error: we don't have: either all 3 fields empty or all 3 fields nonempty");\r
-                               } else {\r
-                                       logger.debug(EELFLoggerDelegate.debugLogger, "In menuItemFieldsChecker, Success: either all 3 fields empty or all 3 fields nonempty");\r
-                               }\r
-                       }\r
-               } catch (Exception e) {\r
-                       logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred while validating the FunctionalMenuItems. Details: " + EcompPortalUtils.getStackTrace(e));\r
-                       fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);\r
-               }\r
-               \r
-               return fieldsValidator;\r
-       }\r
-       \r
-       // Turn foreign key checks on or off\r
-       protected void setForeignKeys(Session localSession, Boolean on) {\r
-               String keyCheck = "0";\r
-               if (on) {\r
-                       keyCheck = "1";\r
-               }\r
-               String sql = "set FOREIGN_KEY_CHECKS="+keyCheck;\r
-               logQuery(sql);\r
-               Query query = localSession.createSQLQuery(sql);\r
-               query.executeUpdate();\r
-       }\r
-\r
-       public FieldsValidator createFunctionalMenuItem(FunctionalMenuItemWithRoles menuItemJson) {\r
-               FieldsValidator fieldsValidator = menuItemFieldsChecker(menuItemJson);\r
-               if (fieldsValidator.httpStatusCode.intValue() == HttpServletResponse.SC_OK) {\r
-                       logger.debug(EELFLoggerDelegate.debugLogger, "LR: createFunctionalMenuItem: test 1");\r
-                       boolean result = false;\r
-                       Session localSession = null;\r
-                       Transaction transaction = null;\r
-                       try {\r
-                               FunctionalMenuItem menuItem = new FunctionalMenuItem();\r
-                               menuItem.appid = menuItemJson.appid;\r
-                               menuItem.setRoles(menuItemJson.getRoles());\r
-                               menuItem.url = menuItemJson.url;\r
-                               menuItem.text = menuItemJson.text;\r
-                               menuItem.parentMenuId = menuItemJson.parentMenuId;\r
-                               menuItem.active_yn = "Y";\r
-                               localSession = sessionFactory.openSession();\r
-                               \r
-                               // If the app is disabled, deactivate the menu item.\r
-                               if (menuItemJson.appid != null) {\r
-                                       Long appidLong = Long.valueOf(menuItemJson.appid);\r
-                                       EPApp app = (EPApp) localSession.get(EPApp.class, appidLong);\r
-                                       if (app != null && ! app.getEnabled()) {\r
-                                               menuItem.active_yn = "N";\r
-                                       }\r
-                               }\r
-\r
-                               // Set the column number to 1 higher than the highest column\r
-                               // number under this parent.\r
-                               Criteria criteria = localSession.createCriteria(FunctionalMenuItem.class);\r
-                               criteria.setProjection(Projections.max("column"));\r
-                               criteria.add(Restrictions.eq("parentMenuId", menuItem.parentMenuId));\r
-                               Integer maxColumn = (Integer) criteria.uniqueResult();\r
-                               if (maxColumn == null) {\r
-                                       maxColumn = 0;\r
-                               }\r
-                               menuItem.column = maxColumn + 1;\r
-                               logger.debug(EELFLoggerDelegate.debugLogger, "about to create menu item: " + menuItem.toString());\r
-\r
-                               transaction = localSession.beginTransaction();\r
-                               // localSession.saveOrUpdate(newMenuItem);\r
-                               localSession.save(menuItem);\r
-                               Long menuid = menuItem.menuId;\r
-                               menuItemJson.menuId = menuid;\r
-                               logger.debug(EELFLoggerDelegate.debugLogger, "after saving menu object, new id: " + menuid);\r
-\r
-                               // Next, save all the roles\r
-\r
-                               addRoles(menuItemJson, localSession);\r
-                               transaction.commit();\r
-                               result = true;\r
-                       } catch (Exception e) {\r
-                               EcompPortalUtils.rollbackTransaction(transaction, \r
-                                               "createFunctionalMenuItem rollback, exception = " + e);\r
-                               logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));\r
-                       } finally {\r
-                               EcompPortalUtils.closeLocalSession(localSession, "createFunctionalMenuItem");\r
-                       }\r
-                       if (result) {\r
-                       } else {\r
-                               logger.debug(EELFLoggerDelegate.debugLogger, "LR: createFunctionalMenuItem: no result. setting httpStatusCode to "\r
-                                               + HttpServletResponse.SC_INTERNAL_SERVER_ERROR);\r
-                               fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);\r
-                       }\r
-               } else {\r
-                       logger.error(EELFLoggerDelegate.errorLogger, "FunctionalMenuServiceImpl.createFunctionalMenuItem: bad request");\r
-               }\r
-               return fieldsValidator;\r
-       }\r
-\r
-       /* Add all the roles in the menu item to the database */\r
-       public void addRoles(FunctionalMenuItemWithRoles menuItemJson, Session localSession) {\r
-               logger.debug(EELFLoggerDelegate.debugLogger, "entering addRoles.");\r
-               List<Integer> roles = menuItemJson.getRoles();\r
-               if (roles != null && roles.size() > 0) {\r
-                       Integer appid = menuItemJson.appid;\r
-                       Long menuid = menuItemJson.menuId;\r
-                       for (Integer roleid : roles) {\r
-                               logger.debug(EELFLoggerDelegate.debugLogger, "about to create record for role: " + roleid);\r
-                               FunctionalMenuRole role = new FunctionalMenuRole();\r
-                               role.appId = appid;\r
-                               role.menuId = menuid;\r
-                               role.roleId = roleid;\r
-                               localSession.save(role);\r
-                               logger.debug(EELFLoggerDelegate.debugLogger, "after saving role menu object, new id: " + role.id);\r
-                       }\r
-               }\r
-       }\r
-\r
-       /* Delete all the roles associated with the menu item from the database */\r
-       public void deleteRoles(Long menuId) {\r
-               dataAccessService.deleteDomainObjects(FunctionalMenuRole.class, "menu_id='" + menuId + "'", null);\r
-       }\r
-\r
-       /* Delete all favorites associated with the menu item from the database */\r
-       public void deleteFavorites(Long menuId) {\r
-               dataAccessService.deleteDomainObjects(FavoritesFunctionalMenuItem.class, "menu_id='" + menuId + "'", null);\r
-       }\r
-       \r
-       private Boolean parentMenuIdEqual(Integer menuId1, Integer menuId2) {\r
-               return ((menuId1 == null && menuId2 == null) || (menuId1 != null && menuId1.equals(menuId2)));\r
-       }\r
-\r
-       private void updateColumnForSiblings(Session localSession, Long menuId, Integer oldParentMenuId,\r
-                       Integer newParentMenuId, Integer oldColumn, Integer newColumn) {\r
-               logger.debug(EELFLoggerDelegate.debugLogger, "entering updateColumnForSiblings");\r
-               Criteria criteria = localSession.createCriteria(FunctionalMenuItem.class);\r
-               criteria.add(Restrictions.ne("menuId", menuId));\r
-               if (parentMenuIdEqual(oldParentMenuId, newParentMenuId)) {\r
-                       logger.debug(EELFLoggerDelegate.debugLogger, "moving under the same parent");\r
-                       // We are moving to a new position under the same parent\r
-                       if (newParentMenuId == null) {\r
-                               logger.debug(EELFLoggerDelegate.debugLogger, "newParentMenuId is null, so using isNull");\r
-                               criteria.add(Restrictions.isNull("parentMenuId"));\r
-                       } else {\r
-                               logger.debug(EELFLoggerDelegate.debugLogger, "newParentMenuId is NOT null, so using eq");\r
-                               criteria.add(Restrictions.eq("parentMenuId", newParentMenuId));\r
-                       }\r
-                       if (oldColumn > newColumn) {\r
-                               logger.debug(EELFLoggerDelegate.debugLogger, "moving to a lower column under the same parent");\r
-                               // We are moving to a lower column under the same parent\r
-                               criteria.add(Restrictions.ge("column", newColumn));\r
-                               criteria.add(Restrictions.lt("column", oldColumn));\r
-                               @SuppressWarnings("unchecked")\r
-                               List<FunctionalMenuItem> menuItems = criteria.list();\r
-                               for (FunctionalMenuItem menuItem : menuItems) {\r
-                                       menuItem.column += 1;\r
-                                       localSession.save(menuItem);\r
-                               }\r
-                       } else if (oldColumn < newColumn) {\r
-                               logger.debug(EELFLoggerDelegate.debugLogger, "moving to a higher column under the same parent");\r
-                               // We are moving to a higher column under the same parent\r
-                               criteria.add(Restrictions.gt("column", oldColumn));\r
-                               criteria.add(Restrictions.le("column", newColumn));\r
-                               @SuppressWarnings("unchecked")\r
-                               List<FunctionalMenuItem> menuItems = criteria.list();\r
-                               for (FunctionalMenuItem menuItem : menuItems) {\r
-                                       menuItem.column -= 1;\r
-                                       localSession.save(menuItem);\r
-                               }\r
-                       } else {\r
-                               // No info has changed\r
-                               logger.debug(EELFLoggerDelegate.debugLogger, "no info has changed, so we are not moving");\r
-                       }\r
-               } else {\r
-                       logger.debug(EELFLoggerDelegate.debugLogger, "moving under a new parent");\r
-                       // We are moving under a new parent.\r
-\r
-                       // Adjust the children under the old parent\r
-                       logger.debug(EELFLoggerDelegate.debugLogger, "about to adjust the children under the old parent");\r
-\r
-                       // If the parentId is null, must check for its children differently\r
-                       if (oldParentMenuId == null) {\r
-                               logger.debug(EELFLoggerDelegate.debugLogger, "oldParentMenuId is null, so using isNull");\r
-                               criteria.add(Restrictions.isNull("parentMenuId"));\r
-                       } else {\r
-                               logger.debug(EELFLoggerDelegate.debugLogger, "oldParentMenuId is NOT null, so using eq");\r
-                               criteria.add(Restrictions.eq("parentMenuId", oldParentMenuId));\r
-                       }\r
-\r
-                       criteria.add(Restrictions.gt("column", oldColumn));\r
-                       @SuppressWarnings("unchecked")\r
-                       List<FunctionalMenuItem> menuItems1 = criteria.list();\r
-                       for (FunctionalMenuItem menuItem : menuItems1) {\r
-                               menuItem.column -= 1;\r
-                               localSession.save(menuItem);\r
-                       }\r
-                       // Adjust the children under the new parent.\r
-                       logger.debug(EELFLoggerDelegate.debugLogger, "about to adjust the children under the new parent");\r
-                       logger.debug(EELFLoggerDelegate.debugLogger, "get all menu items where menuId!=" + menuId + "; parentMenuId==" + newParentMenuId\r
-                                       + "; column>=" + newColumn);\r
-                       criteria = localSession.createCriteria(FunctionalMenuItem.class);\r
-                       criteria.add(Restrictions.ne("menuId", menuId));\r
-                       if (newParentMenuId == null) {\r
-                               logger.debug(EELFLoggerDelegate.debugLogger, "newParentMenuId is null, so using isNull");\r
-                               criteria.add(Restrictions.isNull("parentMenuId"));\r
-                       } else {\r
-                               logger.debug(EELFLoggerDelegate.debugLogger, "newParentMenuId is NOT null, so using eq");\r
-                               criteria.add(Restrictions.eq("parentMenuId", newParentMenuId));\r
-                       }\r
-\r
-                       criteria.add(Restrictions.ge("column", newColumn));\r
-                       @SuppressWarnings("unchecked")\r
-                       List<FunctionalMenuItem> menuItems2 = criteria.list();\r
-                       if (menuItems2 != null) {\r
-                               logger.debug(EELFLoggerDelegate.debugLogger, "found " + menuItems2.size() + " menu items");\r
-                       } else {\r
-                               logger.debug(EELFLoggerDelegate.debugLogger, "found null menu items");\r
-                       }\r
-                       for (FunctionalMenuItem menuItem : menuItems2) {\r
-                               menuItem.column += 1;\r
-                               localSession.save(menuItem);\r
-                       }\r
-               }\r
-               logger.debug(EELFLoggerDelegate.debugLogger, "done with updateColumnForSiblings");\r
-       }\r
-\r
-       public void removeAppInfo(Session localSession, Long menuId) {\r
-               // Remove the url, role, and app info from a menu item\r
-               FunctionalMenuItem menuItem = (FunctionalMenuItem) localSession.get(FunctionalMenuItem.class, menuId);\r
-               menuItem.url = "";\r
-               deleteRoles(menuId);\r
-       }\r
-\r
-       public FieldsValidator editFunctionalMenuItem(FunctionalMenuItemWithRoles menuItemJson) {\r
-               boolean result                  = false;\r
-               Session localSession    = null;\r
-               Transaction transaction = null;\r
-               Long menuId                     = menuItemJson.menuId;\r
-\r
-               logger.debug(EELFLoggerDelegate.debugLogger, "LR: editFunctionalMenuItem: test 1");\r
-               FieldsValidator fieldsValidator = menuItemFieldsChecker(menuItemJson);\r
-               if (fieldsValidator.httpStatusCode.intValue() == HttpServletResponse.SC_OK) {\r
-                       // TODO: make sure menuId is here. And, it might not already exist\r
-                       // in db table.\r
-                       if (menuId == null) {\r
-                               fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_BAD_REQUEST);\r
-                               logger.error(EELFLoggerDelegate.errorLogger, "FunctionalMenuServiceImpl.editFunctionalMenuItem: bad request");\r
-                       } else {\r
-                               // To simplify the code, assume we will have a transaction\r
-                               try {\r
-                                       localSession = sessionFactory.openSession();\r
-                                       transaction = localSession.beginTransaction();\r
-\r
-                                       // Get the existing info associated with menuItem from the DB\r
-                                       FunctionalMenuItem menuItem = (FunctionalMenuItem) localSession.get(FunctionalMenuItem.class, menuId);\r
-                                       Integer oldColumn = menuItem.column;\r
-                                       Integer oldParentMenuId = menuItem.parentMenuId;\r
-                                       Integer newColumn = menuItemJson.column;\r
-                                       Integer newParentMenuId = menuItemJson.parentMenuId;\r
-\r
-                                       logger.debug(EELFLoggerDelegate.debugLogger, "prev info: column: " + oldColumn + "; parentMenuId: " + oldParentMenuId);\r
-\r
-                                       if (menuItemJson.appid != null && menuItemJson.getRoles() != null && !menuItemJson.getRoles().isEmpty()\r
-                                                       && menuItemJson.url != null && !menuItemJson.url.isEmpty() && menuItemJson.text != null\r
-                                                       && !menuItemJson.text.isEmpty()) {\r
-                                               // Scenario: appid, roles, url and text are all non-null.\r
-                                               // This menu item is associated with an app.\r
-                                               // (Note: this should only occur for a bottom-level menu\r
-                                               // item with no children.)\r
-                                               // 1) Remove all the records from fn_menu_functional_role\r
-                                               // for this menuId.\r
-                                               // 2) Add records to the fn_menu_function_role table for the\r
-                                               // appId and each roleId\r
-                                               // 3) Update the url and text for this menu item.\r
-\r
-                                               // Because of foreign key constraints, delete the roles,\r
-                                               // then update the menuItem then add the roles.\r
-                                               deleteRoles(menuId);\r
-                                               // Assumption: this is not a Move, so don't change the\r
-                                               // parentMenuId and column.\r
-                                               menuItem.appid = menuItemJson.appid;\r
-                                               menuItem.setRoles(menuItemJson.getRoles());\r
-                                               menuItem.url = menuItemJson.url;\r
-                                               menuItem.text = menuItemJson.text;\r
-                                               \r
-                                               // If the app is disabled, deactivate the menu item.\r
-                                               Long appidLong = Long.valueOf(menuItemJson.appid);\r
-                                               EPApp app = (EPApp) localSession.get(EPApp.class, appidLong);\r
-                                               if (app != null && ! app.getEnabled()) {\r
-                                                       menuItem.active_yn = "N";\r
-                                               } else {\r
-                                                       menuItem.active_yn = "Y";\r
-                                               }\r
-\r
-\r
-                                               localSession.update(menuItem);\r
-                                               addRoles(menuItemJson, localSession);\r
-\r
-                                       } else if (menuItemJson.appid == null && (menuItemJson.getRoles() == null || menuItemJson.getRoles().isEmpty())\r
-                                                       && (menuItemJson.url == null || menuItemJson.url.isEmpty()) && menuItemJson.text != null\r
-                                                       && !menuItemJson.text.isEmpty()) {\r
-                                               // Scenario: appid, roles and url are all null; text is\r
-                                               // non-null.\r
-                                               // This menu item is NOT associated with an app.\r
-                                               // 1) Remove all the records from fn_menu_functional_role\r
-                                               // for this menuId\r
-                                               // (in case it was previously associated with an app).\r
-                                               // 2) Update the text for this menu item.\r
-                                               // 3) Set the url to ""\r
-                                               deleteRoles(menuId);\r
-                                               // Assumption: this is not a Move, so don't change the\r
-                                               // parentMenuId and column.\r
-                                               menuItem.text = menuItemJson.text;\r
-                                               menuItem.url = "";\r
-                                               menuItem.active_yn = "Y";\r
-                                               localSession.update(menuItem);\r
-\r
-                                       } else if (newColumn != null) {\r
-                                               // This is a "move" request.\r
-                                               // Menu item has been moved to a different position under\r
-                                               // the same parent, or under a new parent.\r
-                                               logger.debug(EELFLoggerDelegate.debugLogger, "Doing a move operation.");\r
-                                               if (parentMenuIdEqual(oldParentMenuId, newParentMenuId)) {\r
-                                                       // The parent is the same. We have just changed the\r
-                                                       // column\r
-                                                       logger.debug(EELFLoggerDelegate.debugLogger, "moving under the same parent");\r
-                                                       menuItem.column = newColumn;\r
-                                                       localSession.update(menuItem);\r
-                                               } else {\r
-                                                       logger.debug(EELFLoggerDelegate.debugLogger, "moving under a different parent");\r
-                                                       menuItem.parentMenuId = newParentMenuId;\r
-                                                       menuItem.column = newColumn;\r
-                                                       localSession.update(menuItem);\r
-                                                       // If we are moving under a new parent, must delete any\r
-                                                       // app/role info from\r
-                                                       // the new parent, since it is no longer a leaf menu\r
-                                                       // item and cannot have app info\r
-                                                       // associated with it. The front end will have warned\r
-                                                       // the user and gotten confirmation.\r
-                                                       if (menuItemJson.parentMenuId != null) {\r
-                                                               Long parentMenuIdLong = new Long(menuItemJson.parentMenuId);\r
-                                                               removeAppInfo(localSession, parentMenuIdLong);\r
-                                                               // deleteRoles(parentMenuIdLong);\r
-                                                       }\r
-                                               }\r
-                                               // must update the column for all old and new sibling menu\r
-                                               // items\r
-                                               updateColumnForSiblings(localSession, menuId, oldParentMenuId, newParentMenuId, oldColumn,\r
-                                                               newColumn);\r
-                                       }\r
-\r
-                                       transaction.commit();\r
-                                       logger.debug(EELFLoggerDelegate.debugLogger, "LR: editFunctionalMenuItem: finished committing transaction");\r
-                                       result = true;\r
-                               } catch (Exception e) {\r
-                                       EcompPortalUtils.rollbackTransaction(transaction,\r
-                                                       "createFunctionalMenuItem rollback, exception = " + e);\r
-                                       logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));\r
-                               } finally {\r
-                                       EcompPortalUtils.closeLocalSession(localSession, "editFunctionalMenuItem");\r
-                               }\r
-                               \r
-                               if (result) {\r
-                               } else {\r
-                                       logger.debug(EELFLoggerDelegate.debugLogger, "LR: createFunctionalMenuItem: no result. setting httpStatusCode to "\r
-                                                       + HttpServletResponse.SC_INTERNAL_SERVER_ERROR);\r
-                                       fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);\r
-                               }\r
-                       }\r
-               }       \r
-               \r
-               return fieldsValidator;\r
-       }\r
-\r
-       public FieldsValidator deleteFunctionalMenuItem(Long menuId) {\r
-               FieldsValidator fieldsValidator = new FieldsValidator();\r
-               logger.debug(EELFLoggerDelegate.debugLogger, "LR: deleteFunctionalMenuItem: test 1");\r
-               boolean result = false;\r
-               Session localSession = null;\r
-               Transaction transaction = null;\r
-\r
-               try {\r
-                       localSession = sessionFactory.openSession();\r
-                       transaction = localSession.beginTransaction();\r
-                       // We must turn off foreign keys before deleting a menu item. Otherwise there will be a \r
-                       // constraint violation from the ancestors table.\r
-                       setForeignKeys(localSession, false);\r
-                       deleteRoles(menuId);\r
-                       logger.debug(EELFLoggerDelegate.debugLogger, "deleteFunctionalMenuItem: after deleting roles");\r
-                       deleteFavorites(menuId);\r
-                       logger.debug(EELFLoggerDelegate.debugLogger, "deleteFunctionalMenuItem: after deleting favorites");\r
-                       localSession.delete(localSession.get(FunctionalMenuItem.class, menuId));\r
-                       logger.debug(EELFLoggerDelegate.debugLogger, "deleteFunctionalMenuItem: about to commit");\r
-                       transaction.commit();\r
-                       result = true;\r
-               } catch (Exception e) {\r
-                       EcompPortalUtils.rollbackTransaction(transaction,\r
-                                       "deleteFunctionalMenuItem rollback, exception = " + e);\r
-                       logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));\r
-               } finally {\r
-                       EcompPortalUtils.closeLocalSession(localSession, "deleteFunctionalMenuItem");\r
-               }\r
-               if (result) {\r
-               } else {\r
-                       logger.debug(EELFLoggerDelegate.debugLogger, "LR: deleteFunctionalMenuItem: no result. setting httpStatusCode to "\r
-                                       + HttpServletResponse.SC_INTERNAL_SERVER_ERROR);\r
-                       fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);\r
-               }\r
-               return fieldsValidator;\r
-       }\r
-       \r
-       // Regenerate the fn_menu_functional_ancestors table, which is used\r
-       // by the queries that return the functional menu items.\r
-       public FieldsValidator regenerateAncestorTable() {\r
-               FieldsValidator fieldsValidator = new FieldsValidator();\r
-               Session localSession = null;\r
-               Transaction transaction = null;\r
-\r
-               try {\r
-                       localSession = sessionFactory.openSession();\r
-                       transaction = localSession.beginTransaction();\r
-                       String sql = "DELETE FROM fn_menu_functional_ancestors";\r
-                       logQuery(sql);\r
-                       Query query = localSession.createSQLQuery(sql);\r
-                       query.executeUpdate();\r
-                       logger.debug(EELFLoggerDelegate.debugLogger, "regenerateAncestorTable: finished query 1");\r
-                       \r
-                       sql = "ALTER TABLE fn_menu_functional_ancestors AUTO_INCREMENT=1";\r
-                       logQuery(sql);\r
-                       query = localSession.createSQLQuery(sql);\r
-                       query.executeUpdate();\r
-                       logger.debug(EELFLoggerDelegate.debugLogger, "regenerateAncestorTable: reset AUTO_INCREMENT to 1");\r
-\r
-                       int depth = 0;\r
-                       sql = "INSERT INTO fn_menu_functional_ancestors(menu_id, ancestor_menu_id, depth) "\r
-                                       + "SELECT m.menu_id, m.menu_id, " + depth + " FROM fn_menu_functional m";\r
-                       logQuery(sql);\r
-                       query = localSession.createSQLQuery(sql);\r
-                       query.executeUpdate();\r
-                       logger.debug(EELFLoggerDelegate.debugLogger, "regenerateAncestorTable: finished query 2");\r
-                       for (depth = 0; depth < 3; depth++) {\r
-                               int depthPlusOne = depth + 1;\r
-                               sql = "INSERT INTO fn_menu_functional_ancestors(menu_id, ancestor_menu_id, depth) "\r
-                                               + " SELECT a.menu_id, m.parent_menu_id, " + depthPlusOne\r
-                                               + " FROM fn_menu_functional m, fn_menu_functional_ancestors a " + " WHERE a.depth='" + depth\r
-                                               + "' AND " + " a.ancestor_menu_id = m.menu_id AND " + " m.parent_menu_id != '-1'";\r
-                               logQuery(sql);\r
-                               query = localSession.createSQLQuery(sql);\r
-                               query.executeUpdate();\r
-                       }\r
-                       logger.debug(EELFLoggerDelegate.debugLogger, "regenerateAncestorTable: finished query 3");\r
-                       transaction.commit();\r
-               } catch (Exception e) {\r
-                       EcompPortalUtils.rollbackTransaction(transaction,\r
-                                       "regenerateAncestorTable rollback, exception = " + e);\r
-                       logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));\r
-                       fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);\r
-               } finally {\r
-                       EcompPortalUtils.closeLocalSession(localSession, "regenerateAncestorTable");\r
-               }\r
-               return fieldsValidator;\r
-       }\r
-\r
-       private void logQuery(String sql) {\r
-               logger.debug(EELFLoggerDelegate.debugLogger, "Executing query: " + sql);\r
-       }\r
-\r
-       public FieldsValidator setFavoriteItem(FavoritesFunctionalMenuItem menuItemJson) {\r
-               boolean result = false;\r
-               FieldsValidator fieldsValidator = new FieldsValidator();\r
-               \r
-               Session localSession = null;\r
-               Transaction transaction = null;\r
-               \r
-               try {\r
-            logger.debug(EELFLoggerDelegate.debugLogger, String.format("Before adding favorite for user id:{0} and menu id:{1} ",menuItemJson.userId,menuItemJson.menuId));\r
-                       localSession = sessionFactory.openSession();\r
-                       transaction = localSession.beginTransaction();\r
-                       localSession.save(menuItemJson);\r
-                       transaction.commit();                           \r
-                       result = true;\r
-            logger.debug(EELFLoggerDelegate.debugLogger, String.format("After adding favorite for user id:{0} and menu id:{1} ",menuItemJson.userId,menuItemJson.menuId));                                                             \r
-               } catch (Exception e) {\r
-                       EcompPortalUtils.rollbackTransaction(transaction,"setFavoriteItem rollback, exception = " + e);\r
-                       logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));\r
-               } finally {\r
-                       EcompPortalUtils.closeLocalSession(localSession, "setFavoriteItem");\r
-               }                                                                                       \r
-               \r
-               if(result) {\r
-               }\r
-               else {\r
-                       fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);\r
-               }\r
-               \r
-               return fieldsValidator;\r
-       }\r
-       \r
-       public List<FavoritesFunctionalMenuItemJson> getFavoriteItems(Long userId) {            \r
-               try {\r
-                       logger.debug(EELFLoggerDelegate.debugLogger, "Before getting favorites for user id: " + userId);\r
-                       \r
-                       // Divide this into 2 queries: one which returns the favorites items associated with Restricted apps,\r
-                       // and one that returns all the other favorites items. Then we can easily add the boolean flag\r
-                       // restrictedApp to each FavoritesFunctionalMenuItemJson, to be used by the front end.\r
-\r
-                       String sql = "SELECT f.user_id,f.menu_id,m.text,m.url "\r
-                                       + " FROM fn_menu_favorites f, fn_menu_functional m, fn_menu_functional_roles mr "\r
-                                       + " WHERE f.user_id='" + userId + "' AND f.menu_id = m.menu_id "\r
-                                       + " AND f.menu_id = mr.menu_id "\r
-                                       + " AND mr.role_id = '" + RESTRICTED_APP_ROLE_ID + "' ";\r
-\r
-                       @SuppressWarnings("unchecked")\r
-                       List<FavoritesFunctionalMenuItemJson> menuItems = dataAccessService.executeSQLQuery(sql, FavoritesFunctionalMenuItemJson.class, null);\r
-                       for (FavoritesFunctionalMenuItemJson menuItem : menuItems) {\r
-                               menuItem.restrictedApp = true;\r
-                       }\r
-                       \r
-                       sql = "SELECT DISTINCT f.user_id,f.menu_id,m.text,m.url "\r
-                                       + " FROM fn_menu_favorites f, fn_menu_functional m, fn_menu_functional_roles mr "\r
-                                       + " WHERE f.user_id='" + userId + "' AND f.menu_id = m.menu_id "\r
-                                       + " AND f.menu_id = mr.menu_id "\r
-                                       + " AND mr.role_id != '" + RESTRICTED_APP_ROLE_ID + "' ";\r
-                       @SuppressWarnings("unchecked")\r
-                       List<FavoritesFunctionalMenuItemJson> menuItems2 = dataAccessService.executeSQLQuery(sql, FavoritesFunctionalMenuItemJson.class, null);\r
-                       for (FavoritesFunctionalMenuItemJson menuItem : menuItems2) {\r
-                               menuItem.restrictedApp = false;\r
-                               menuItems.add(menuItem);\r
-                       }\r
-                       \r
-                       return menuItems;\r
-               } catch (Exception e) {\r
-                       logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred in FunctionalMenuServiceImpl.getFavoriteItems. Details: " + EcompPortalUtils.getStackTrace(e));\r
-                       List<FavoritesFunctionalMenuItemJson> menuItems = new ArrayList<FavoritesFunctionalMenuItemJson>();\r
-                       return menuItems;\r
-               }                                                                                                                                       \r
-       }\r
-       \r
-       public FieldsValidator removeFavoriteItem(Long userId, Long menuId) {\r
-               boolean result = false;\r
-               FieldsValidator fieldsValidator = new FieldsValidator();\r
-               \r
-               Session localSession = null;\r
-               Transaction transaction = null;\r
-               \r
-               try {                   \r
-                       \r
-                       FavoritesFunctionalMenuItem menuItemJson = new FavoritesFunctionalMenuItem();\r
-                       menuItemJson.userId = userId;\r
-                       menuItemJson.menuId = menuId;\r
-                       \r
-                       localSession = sessionFactory.openSession();\r
-                       transaction = localSession.beginTransaction();\r
-                       localSession.delete(menuItemJson);\r
-                       localSession.flush();\r
-                       transaction.commit();                           \r
-                       result = true;\r
-                       logger.debug(EELFLoggerDelegate.debugLogger, String.format("After removing favorite for user id: " + userId + "; menu id: " + menuId));                                                         \r
-               } catch (Exception e) {\r
-                       EcompPortalUtils.rollbackTransaction(transaction,"removeFavoriteItem rollback, exception = " + e);\r
-                       logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));\r
-               } finally {                                                             \r
-                       EcompPortalUtils.closeLocalSession(localSession, "removeFavoriteItem");\r
-               }                                                                                       \r
-               \r
-               if(result) {\r
-               }\r
-               else {\r
-                       fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);\r
-               }\r
-               \r
-               return fieldsValidator;\r
-       }\r
-       \r
-       @Override\r
-       public void assignHelpURLs(List<FunctionalMenuItem> menuItems) {\r
-               try {\r
-                       String user_guide_link = SystemProperties.getProperty(EPCommonSystemProperties.USER_GUIDE_URL);\r
-                       \r
-                       for(FunctionalMenuItem menuItem: menuItems){\r
-                               if(menuItem.text.equalsIgnoreCase("Contact Us")){\r
-                                       menuItem.setUrl("contactUs");\r
-                                       //menuItem.setRestrictedApp(true);\r
-                               }\r
-                               if(menuItem.text.equalsIgnoreCase("Get Access")) {\r
-                                       menuItem.setUrl("getAccess");\r
-                               }\r
-                               if(menuItem.text.equalsIgnoreCase("User Guide")) {\r
-                                       menuItem.setUrl(user_guide_link);\r
-                                       menuItem.setRestrictedApp(true);\r
-                               }\r
-                       }\r
-               } catch (Exception e) {         \r
-                       logger.error(EELFLoggerDelegate.errorLogger, "assignHelpURLs process failed. Details: " + EcompPortalUtils.getStackTrace(e));\r
-               }\r
-               \r
-       }\r
-\r
-       public List<FunctionalMenuRole> getFunctionalMenuRole() {\r
-               String sql = "SELECT * from fn_menu_functional_roles";\r
-               logQuery(sql);\r
-               logger.debug(EELFLoggerDelegate.debugLogger, "getFunctionalMenuRole: logged the query");\r
-\r
-               @SuppressWarnings("unchecked")\r
-               List<FunctionalMenuRole> functionalMenuRole = dataAccessService.executeSQLQuery(sql, FunctionalMenuRole.class, null);\r
-               \r
-               return functionalMenuRole;\r
-       }\r
-\r
-       @SuppressWarnings("unchecked")\r
-       @Override\r
-       public List<BusinessCardApplicationRole> getUserAppRolesList(String userId) {\r
-               Map<String, String> params = new HashMap<>();\r
-               params.put("userId", userId);\r
-\r
-               List<BusinessCardApplicationRole> userAppRoles = null;\r
-               try {\r
-                       userAppRoles = dataAccessService\r
-                                       .executeNamedQuery("getUserApproles", params, null);\r
-               } catch (Exception e) {\r
-                       // TODO Auto-generated catch block\r
-                       logger.error(EELFLoggerDelegate.errorLogger, "getUserAppRolesList failed", e);\r
-               }               \r
-               return userAppRoles;\r
-       }\r
-       \r
-}\r
+/*-
+ * ================================================================================
+ * ECOMP Portal
+ * ================================================================================
+ * 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.portalapp.portal.service;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.annotation.PostConstruct;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.commons.lang3.StringUtils;
+import org.hibernate.Criteria;
+import org.hibernate.Query;
+import org.hibernate.Session;
+import org.hibernate.SessionFactory;
+import org.hibernate.Transaction;
+import org.hibernate.criterion.Projections;
+import org.hibernate.criterion.Restrictions;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.EnableAspectJAutoProxy;
+import org.springframework.stereotype.Service;
+
+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.portalapp.portal.domain.EPApp;
+import org.openecomp.portalapp.portal.domain.EPUser;
+import org.openecomp.portalapp.portal.domain.FunctionalMenuItemWithAppID;
+import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
+import org.openecomp.portalapp.portal.transport.BusinessCardApplicationRole;
+import org.openecomp.portalapp.portal.transport.FavoritesFunctionalMenuItem;
+import org.openecomp.portalapp.portal.transport.FavoritesFunctionalMenuItemJson;
+import org.openecomp.portalapp.portal.transport.FieldsValidator;
+import org.openecomp.portalapp.portal.transport.FunctionalMenuItem;
+import org.openecomp.portalapp.portal.transport.FunctionalMenuItemWithRoles;
+import org.openecomp.portalapp.portal.transport.FunctionalMenuRole;
+import org.openecomp.portalapp.portal.utils.EPCommonSystemProperties;
+import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
+
+@Service("functionalMenuService")
+@org.springframework.context.annotation.Configuration
+@EnableAspectJAutoProxy
+@EPMetricsLog
+public class FunctionalMenuServiceImpl implements FunctionalMenuService {
+       EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(FunctionalMenuServiceImpl.class);
+
+       private Long ACCOUNT_ADMIN_ROLE_ID = 999L;
+       private String RESTRICTED_APP_ROLE_ID = "900";
+
+       @Autowired
+       private DataAccessService dataAccessService;
+       @Autowired
+       private SessionFactory sessionFactory;
+
+       @PostConstruct
+       private void init() {
+               try {
+                       ACCOUNT_ADMIN_ROLE_ID = Long.valueOf(SystemProperties.getProperty(EPCommonSystemProperties.ACCOUNT_ADMIN_ROLE_ID));
+                       RESTRICTED_APP_ROLE_ID = SystemProperties.getProperty(EPCommonSystemProperties.RESTRICTED_APP_ROLE_ID);
+               } catch(Exception e) {
+               }
+       }
+       
+       public List<FunctionalMenuItem> getFunctionalMenuItems(EPUser user) {
+               List<FunctionalMenuItem> menuItems = new ArrayList<FunctionalMenuItem>();
+               return menuItems;
+       }
+
+       public List<FunctionalMenuItem> getFunctionalMenuItems() {
+               return getFunctionalMenuItems(false);
+       }
+       
+       public List<FunctionalMenuItem> getFunctionalMenuItems(Boolean all) {
+               // Divide this into 2 queries: one which returns the bottom-level menu items associated with Restricted apps,
+               // and one that returns all the other menu items. Then we can easily add the boolean flag
+               // restrictedApp to each FunctionalMenuItem, to be used by the front end.
+               String activeWhereClause = "";
+               if (! all) {
+                       activeWhereClause = " AND UPPER(m.active_yn) = 'Y' ";
+               }
+               String sql = "SELECT m.menu_id, m.column_num, m.text, m.parent_menu_id, m.url, m.active_yn, r.app_id "
+                               + "FROM fn_menu_functional m, fn_menu_functional_roles r "
+                               + "WHERE m.menu_id = r.menu_id "
+                               + activeWhereClause //" AND UPPER(m.active_yn) = 'Y' "
+                               + " AND r.role_id != '" + RESTRICTED_APP_ROLE_ID + "' "
+                       + " UNION "
+                       + " SELECT m.menu_id, m.column_num, m.text, m.parent_menu_id, m.url, m.active_yn,-1 app_id "
+                               + " FROM fn_menu_functional m "
+                               + " WHERE m.url='' "
+                               + activeWhereClause; //" AND UPPER(m.active_yn) = 'Y' ";
+               logQuery(sql);
+
+               @SuppressWarnings("unchecked")
+               List<FunctionalMenuItemWithAppID> menuItemsWithAppIdList = dataAccessService.executeSQLQuery(sql,FunctionalMenuItemWithAppID.class, null);
+               List<FunctionalMenuItem> menuItems = new ArrayList<>(); 
+               menuItems = transformFunctionalMenuItemWithAppIDToFunctionalMenuItem(menuItemsWithAppIdList);
+               for (FunctionalMenuItem menuItem : menuItems) {
+                       menuItem.restrictedApp = false;
+               }
+               
+               sql = "SELECT m.menu_id, m.column_num, m.text, m.parent_menu_id, m.url, m.active_yn, r.app_id "
+                               + "FROM fn_menu_functional m, fn_menu_functional_roles r "
+                               + "WHERE m.menu_id = r.menu_id "
+                               + activeWhereClause //" AND UPPER(m.active_yn) = 'Y' "
+                               + " AND r.role_id = '" + RESTRICTED_APP_ROLE_ID + "' ";
+               logQuery(sql);
+               @SuppressWarnings("unchecked")
+               List<FunctionalMenuItem> menuItems2 = dataAccessService.executeSQLQuery(sql, FunctionalMenuItem.class, null);
+               for (FunctionalMenuItem menuItem : menuItems2) {
+                       menuItem.restrictedApp = true;
+                       menuItems.add(menuItem);
+               }
+               
+               return menuItems;
+       }
+
+       
+       public List<FunctionalMenuItem> getFunctionalMenuItemsForNotificationTree(Boolean all) {
+               // Divide this into 2 queries: one which returns the bottom-level menu items associated with Restricted apps,
+               // and one that returns all the other menu items which are active. Then we can easily add the boolean flag
+               // restrictedApp to each FunctionalMenuItem, to be used by the front end.
+               String activeWhereClause = "";
+               if (! all) {
+                       activeWhereClause = " AND UPPER(m.active_yn) = 'Y' ";
+               }
+               String sql = "SELECT m.menu_id, m.column_num, m.text, m.parent_menu_id, m.url, m.active_yn, r.app_id "
+                               + "FROM fn_menu_functional m, fn_menu_functional_roles r "
+                               + "WHERE m.menu_id = r.menu_id "
+                               + activeWhereClause + " AND UPPER(m.active_yn) = 'Y' "
+                               + " AND r.role_id != '" + RESTRICTED_APP_ROLE_ID + "' "
+                       + " UNION "
+                       + " SELECT m.menu_id, m.column_num, m.text, m.parent_menu_id, m.url, m.active_yn,-1 app_id "
+                               + " FROM fn_menu_functional m "
+                               + " WHERE m.url='' "
+                               + activeWhereClause + " AND UPPER(m.active_yn) = 'Y' ";
+               logQuery(sql);
+
+               @SuppressWarnings("unchecked")
+               List<FunctionalMenuItemWithAppID> menuItemsWithAppIdList = dataAccessService.executeSQLQuery(sql,FunctionalMenuItemWithAppID.class, null);
+               List<FunctionalMenuItem> menuItems = new ArrayList<>(); 
+               menuItems = transformFunctionalMenuItemWithAppIDToFunctionalMenuItem(menuItemsWithAppIdList);
+               for (FunctionalMenuItem menuItem : menuItems) {
+                       menuItem.restrictedApp = false;
+               }
+               
+               sql = "SELECT m.menu_id, m.column_num, m.text, m.parent_menu_id, m.url, m.active_yn, r.app_id "
+                               + "FROM fn_menu_functional m, fn_menu_functional_roles r "
+                               + "WHERE m.menu_id = r.menu_id "
+                               + activeWhereClause + " AND UPPER(m.active_yn) = 'Y' "
+                               + " AND r.role_id = '" + RESTRICTED_APP_ROLE_ID + "' ";
+               logQuery(sql);
+               @SuppressWarnings("unchecked")
+               List<FunctionalMenuItem> menuItems2 = dataAccessService.executeSQLQuery(sql, FunctionalMenuItem.class, null);
+               for (FunctionalMenuItem menuItem : menuItems2) {
+                       menuItem.restrictedApp = true;
+                       menuItems.add(menuItem);
+               }
+               
+               return menuItems;
+       }
+
+       public List<FunctionalMenuItem> getFunctionalMenuItemsForApp(Integer appId) {
+               String sql = "SELECT DISTINCT m1.menu_id, m1.column_num, m1.text, m1.parent_menu_id, m1.url, m.active_yn "
+                               + " FROM fn_menu_functional m, fn_menu_functional m1, fn_menu_functional_ancestors a, fn_menu_functional_roles mr "
+                               + " WHERE " + " mr.app_id='" + appId + "' " + " AND mr.menu_id = m.menu_id " + " AND UPPER(m.active_yn) = 'Y'"
+                               + " AND UPPER(m1.active_yn) ='Y'" + " AND a.menu_id = m.menu_id " + " AND a.ancestor_menu_id = m1.menu_id";
+               logQuery(sql);
+               logger.debug(EELFLoggerDelegate.debugLogger, "getFunctionalMenuItemsForApp: logged the query");
+
+               @SuppressWarnings("unchecked")
+               List<FunctionalMenuItem> menuItems = dataAccessService.executeSQLQuery(sql, FunctionalMenuItem.class, null);
+               
+               return menuItems;
+       }
+       /**
+        * convert List of FunctionalMenuItemWithAppID into List of FunctionalMenuItem
+        * 
+        */
+       public List<FunctionalMenuItem> transformFunctionalMenuItemWithAppIDToFunctionalMenuItem(List<FunctionalMenuItemWithAppID> functionalMenuItemWithAppIDList) {
+               List<FunctionalMenuItem> functionalMenuItemList = new ArrayList<FunctionalMenuItem>();
+               for (FunctionalMenuItemWithAppID functionalMenuItemWithAppID : functionalMenuItemWithAppIDList) {
+                       FunctionalMenuItem menuItem = new FunctionalMenuItem();
+                       menuItem.menuId=functionalMenuItemWithAppID.menuId;
+                       menuItem.column=functionalMenuItemWithAppID.column;
+                       menuItem.text=functionalMenuItemWithAppID.text;
+                       menuItem.parentMenuId=functionalMenuItemWithAppID.parentMenuId;
+                       menuItem.url=functionalMenuItemWithAppID.url;
+                       menuItem.active_yn=functionalMenuItemWithAppID.active_yn;
+                       menuItem.appid=functionalMenuItemWithAppID.appid;
+                       menuItem.setRoles(functionalMenuItemWithAppID.roles);
+                       menuItem.restrictedApp=functionalMenuItemWithAppID.restrictedApp;
+                       functionalMenuItemList.add(menuItem);
+               }
+               return functionalMenuItemList;
+       }
+       
+       public List<FunctionalMenuItem> getFunctionalMenuItemsForUser(String orgUserId) {
+               // m represents the functional menu items that are the leaf nodes
+               // m1 represents the functional menu items for all the nodes
+
+               // Divide this into 2 queries: one which returns the bottom-level menu items associated with Restricted apps,
+               // and one that returns all the other menu items. Then we can easily add the boolean flag
+               // restrictedApp to each FunctionalMenuItem, to be used by the front end.
+               String sql = "SELECT DISTINCT m1.menu_id, m1.column_num, m1.text, m1.parent_menu_id, m1.url, m.active_yn "
+                               + " FROM fn_menu_functional m, fn_menu_functional m1, fn_menu_functional_ancestors a, "
+                               + " fn_menu_functional_roles mr, fn_user u , fn_user_role ur " + " WHERE " + " u.org_user_id='" + orgUserId
+                               + "' " + " AND u.user_id = ur.user_id " + " AND ur.app_id = mr.app_id " +
+                               // " AND ur.role_id = mr.role_id " +
+                               " AND (ur.role_id = mr.role_id " + "     OR ur.role_id = '" + ACCOUNT_ADMIN_ROLE_ID + "') "
+                               + " AND m.menu_id = mr.menu_id " + " AND UPPER(m.active_yn) = 'Y'" + " AND UPPER(m1.active_yn) ='Y' "
+                               + " AND a.menu_id = m.menu_id " + " AND a.ancestor_menu_id = m1.menu_id "
+                               + " UNION "
+                               // the ancestors of the restricted app menu items
+                               + " select m1.menu_id, m1.column_num, m1.text, m1.parent_menu_id, m1.url, m1.active_yn "
+                               + " FROM fn_menu_functional m, fn_menu_functional_roles mr, fn_menu_functional m1, "
+                               + " fn_menu_functional_ancestors a "
+                               + " where a.menu_id = m.menu_id "
+                               + " AND a.ancestor_menu_id = m1.menu_id "
+                               + " AND m.menu_id != m1.menu_id "
+                               + " AND m.menu_id = mr.menu_id "
+                               + " AND mr.role_id = '" + RESTRICTED_APP_ROLE_ID + "' "
+                               + " AND UPPER(m.active_yn) = 'Y'" + " AND UPPER(m1.active_yn) ='Y' "
+                               // Add the Favorites menu item
+                               + " UNION "
+                               + " SELECT m.menu_id, m.column_num, m.text, m.parent_menu_id, m.url, m.active_yn "
+                               + " FROM fn_menu_functional m "
+                               + " WHERE m.text in ('Favorites','Get Access','Contact Us','Support','User Guide','Help')";
+               
+               logQuery(sql);
+               logger.debug(EELFLoggerDelegate.debugLogger, "getFunctionalMenuItemsForUser: logged the query");
+
+               @SuppressWarnings("unchecked")
+               List<FunctionalMenuItem> menuItems = dataAccessService.executeSQLQuery(sql, FunctionalMenuItem.class, null);
+               for (FunctionalMenuItem menuItem : menuItems) {
+                       menuItem.restrictedApp = false;
+               }
+               
+               sql = " SELECT m.menu_id, m.column_num, m.text, m.parent_menu_id, m.url, m.active_yn "
+                       + " FROM fn_menu_functional m, fn_menu_functional_roles r "
+                       + " WHERE m.menu_id = r.menu_id "
+                       + " AND UPPER(m.active_yn) = 'Y' "
+                       + " AND r.role_id = '" + RESTRICTED_APP_ROLE_ID + "' ";
+               logQuery(sql);
+               @SuppressWarnings("unchecked")
+               List<FunctionalMenuItem> menuItems2 = dataAccessService.executeSQLQuery(sql, FunctionalMenuItem.class, null);
+               for (FunctionalMenuItem menuItem : menuItems2) {
+                       menuItem.restrictedApp = true;
+                       menuItems.add(menuItem);
+               }
+               
+               return menuItems;
+       }
+
+       public FunctionalMenuItem getFunctionalMenuItemDetails(Integer menuid) {
+               // First, fill in the fields that apply to all menu items
+
+               String sql = "SELECT * FROM fn_menu_functional WHERE menu_id = '" + menuid + "'";
+               logQuery(sql);
+               @SuppressWarnings("unchecked")
+               List<FunctionalMenuItem> menuItems = dataAccessService.executeSQLQuery(sql, FunctionalMenuItem.class, null);
+               FunctionalMenuItem menuItem = (menuItems == null || menuItems.isEmpty() ? null : menuItems.get(0));
+               // If it is a bottom-level menu item, must fill in the appid and the
+               // roles
+               sql = "SELECT * FROM fn_menu_functional_roles WHERE menu_id = '" + menuid + "'";
+               logQuery(sql);
+               @SuppressWarnings("unchecked")
+               List<FunctionalMenuRole> roleItems = dataAccessService.executeSQLQuery(sql, FunctionalMenuRole.class, null);
+               if (roleItems.size() > 0) {
+                       Integer appid = roleItems.get(0).appId;
+                       menuItem.appid = appid;
+                       List<Integer> roles = new ArrayList<Integer>();
+                       for (FunctionalMenuRole roleItem : roleItems) {
+                               logger.debug(EELFLoggerDelegate.debugLogger, "LR: app_id: " + roleItem.appId + "; role_id: " + roleItem.roleId + "\n");
+                               roles.add(roleItem.roleId);
+                       }
+                       menuItem.setRoles(roles);
+               }
+               
+               return menuItem;
+       }
+
+       private FieldsValidator menuItemFieldsChecker(FunctionalMenuItemWithRoles menuItemJson) {
+               FieldsValidator fieldsValidator = new FieldsValidator();
+               try {
+                       // TODO: validate all the fields
+                       @SuppressWarnings("unchecked")
+                       List<FunctionalMenuItem> functionalMenuItems = dataAccessService.getList(FunctionalMenuItem.class,
+                                       " where text = '" + menuItemJson.text + "'", null, null);
+                       
+                       boolean dublicatedName = false;
+                       for (FunctionalMenuItem fnMenuItem : functionalMenuItems) {
+                               if (menuItemJson.menuId != null && menuItemJson.menuId.equals(fnMenuItem.menuId)) {
+                                       // FunctionalMenuItem should not be compared with itself
+                                       continue;
+                               }
+
+                               if (!dublicatedName && fnMenuItem.text.equalsIgnoreCase(menuItemJson.text)) {
+                                       dublicatedName = true;
+                                       break;
+                               }
+                       }
+                       if (dublicatedName) {
+                               fieldsValidator.addProblematicFieldName("text");
+                               fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_CONFLICT);
+                               fieldsValidator.errorCode = new Long(EPCommonSystemProperties.DUBLICATED_FIELD_VALUE_ECOMP_ERROR);
+                               logger.debug(EELFLoggerDelegate.debugLogger, "In menuItemFieldsChecker, Error: we have an duplicate text field");
+                       } else if (StringUtils.isEmpty(menuItemJson.text) && menuItemJson.menuId == null) { 
+                               // text must be non empty for a create. For an edit, can be empty, which means it is a move request.
+                               // a null menuId indicates a create.
+                               fieldsValidator.addProblematicFieldName("text");
+                               fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_BAD_REQUEST);
+                               logger.debug(EELFLoggerDelegate.debugLogger, "In menuItemFieldsChecker, Error: we have an empty text field");
+                       } else {
+                               // The url, appid, and roles must either be all filled or all empty.
+                               Boolean urlIsEmpty = StringUtils.isEmpty(menuItemJson.url);
+                               Boolean rolesIsEmpty = menuItemJson.getRoles() == null || menuItemJson.getRoles().isEmpty();
+                               Boolean appidIsEmpty = menuItemJson.appid == null || menuItemJson.appid == 0;
+                               logger.debug(EELFLoggerDelegate.debugLogger, "LR: menuItemfieldsChecker: urlIsEmpty: " + urlIsEmpty + "; rolesIsEmpty: " + rolesIsEmpty + "; appidIsEmpty: " + appidIsEmpty +"\n");
+                               if (!((urlIsEmpty && rolesIsEmpty && appidIsEmpty) || (!urlIsEmpty && !rolesIsEmpty && !appidIsEmpty)))
+                               {
+                                       fieldsValidator.addProblematicFieldName("url,roles,appid");
+                                       fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_BAD_REQUEST);
+                                       logger.debug(EELFLoggerDelegate.debugLogger, "In menuItemFieldsChecker, Error: we don't have: either all 3 fields empty or all 3 fields nonempty");
+                               } else {
+                                       logger.debug(EELFLoggerDelegate.debugLogger, "In menuItemFieldsChecker, Success: either all 3 fields empty or all 3 fields nonempty");
+                               }
+                       }
+               } catch (Exception e) {
+                       logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred while validating the FunctionalMenuItems. Details: " + EcompPortalUtils.getStackTrace(e));
+                       fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+               }
+               
+               return fieldsValidator;
+       }
+       
+       // Turn foreign key checks on or off
+       protected void setForeignKeys(Session localSession, Boolean on) {
+               String keyCheck = "0";
+               if (on) {
+                       keyCheck = "1";
+               }
+               String sql = "set FOREIGN_KEY_CHECKS="+keyCheck;
+               logQuery(sql);
+               Query query = localSession.createSQLQuery(sql);
+               query.executeUpdate();
+       }
+
+       public FieldsValidator createFunctionalMenuItem(FunctionalMenuItemWithRoles menuItemJson) {
+               FieldsValidator fieldsValidator = menuItemFieldsChecker(menuItemJson);
+               if (fieldsValidator.httpStatusCode.intValue() == HttpServletResponse.SC_OK) {
+                       logger.debug(EELFLoggerDelegate.debugLogger, "LR: createFunctionalMenuItem: test 1");
+                       boolean result = false;
+                       Session localSession = null;
+                       Transaction transaction = null;
+                       try {
+                               FunctionalMenuItem menuItem = new FunctionalMenuItem();
+                               menuItem.appid = menuItemJson.appid;
+                               menuItem.setRoles(menuItemJson.getRoles());
+                               menuItem.url = menuItemJson.url;
+                               menuItem.text = menuItemJson.text;
+                               menuItem.parentMenuId = menuItemJson.parentMenuId;
+                               menuItem.active_yn = "Y";
+                               localSession = sessionFactory.openSession();
+                               
+                               // If the app is disabled, deactivate the menu item.
+                               if (menuItemJson.appid != null) {
+                                       Long appidLong = Long.valueOf(menuItemJson.appid);
+                                       EPApp app = (EPApp) localSession.get(EPApp.class, appidLong);
+                                       if (app != null && ! app.getEnabled()) {
+                                               menuItem.active_yn = "N";
+                                       }
+                               }
+
+                               // Set the column number to 1 higher than the highest column
+                               // number under this parent.
+                               Criteria criteria = localSession.createCriteria(FunctionalMenuItem.class);
+                               criteria.setProjection(Projections.max("column"));
+                               criteria.add(Restrictions.eq("parentMenuId", menuItem.parentMenuId));
+                               Integer maxColumn = (Integer) criteria.uniqueResult();
+                               if (maxColumn == null) {
+                                       maxColumn = 0;
+                               }
+                               menuItem.column = maxColumn + 1;
+                               logger.debug(EELFLoggerDelegate.debugLogger, "about to create menu item: " + menuItem.toString());
+
+                               transaction = localSession.beginTransaction();
+                               // localSession.saveOrUpdate(newMenuItem);
+                               localSession.save(menuItem);
+                               Long menuid = menuItem.menuId;
+                               menuItemJson.menuId = menuid;
+                               logger.debug(EELFLoggerDelegate.debugLogger, "after saving menu object, new id: " + menuid);
+
+                               // Next, save all the roles
+
+                               addRoles(menuItemJson, localSession);
+                               transaction.commit();
+                               result = true;
+                       } catch (Exception e) {
+                               EcompPortalUtils.rollbackTransaction(transaction, 
+                                               "createFunctionalMenuItem rollback, exception = " + e);
+                               logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
+                       } finally {
+                               EcompPortalUtils.closeLocalSession(localSession, "createFunctionalMenuItem");
+                       }
+                       if (result) {
+                       } else {
+                               logger.debug(EELFLoggerDelegate.debugLogger, "LR: createFunctionalMenuItem: no result. setting httpStatusCode to "
+                                               + HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+                               fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+                       }
+               } else {
+                       logger.error(EELFLoggerDelegate.errorLogger, "FunctionalMenuServiceImpl.createFunctionalMenuItem: bad request");
+               }
+               return fieldsValidator;
+       }
+
+       /* Add all the roles in the menu item to the database */
+       public void addRoles(FunctionalMenuItemWithRoles menuItemJson, Session localSession) {
+               logger.debug(EELFLoggerDelegate.debugLogger, "entering addRoles.");
+               List<Integer> roles = menuItemJson.getRoles();
+               if (roles != null && roles.size() > 0) {
+                       Integer appid = menuItemJson.appid;
+                       Long menuid = menuItemJson.menuId;
+                       for (Integer roleid : roles) {
+                               logger.debug(EELFLoggerDelegate.debugLogger, "about to create record for role: " + roleid);
+                               FunctionalMenuRole role = new FunctionalMenuRole();
+                               role.appId = appid;
+                               role.menuId = menuid;
+                               role.roleId = roleid;
+                               localSession.save(role);
+                               logger.debug(EELFLoggerDelegate.debugLogger, "after saving role menu object, new id: " + role.id);
+                       }
+               }
+       }
+
+       /* Delete all the roles associated with the menu item from the database */
+       public void deleteRoles(Long menuId) {
+               dataAccessService.deleteDomainObjects(FunctionalMenuRole.class, "menu_id='" + menuId + "'", null);
+       }
+
+       /* Delete all favorites associated with the menu item from the database */
+       public void deleteFavorites(Long menuId) {
+               dataAccessService.deleteDomainObjects(FavoritesFunctionalMenuItem.class, "menu_id='" + menuId + "'", null);
+       }
+       
+       private Boolean parentMenuIdEqual(Integer menuId1, Integer menuId2) {
+               return ((menuId1 == null && menuId2 == null) || (menuId1 != null && menuId1.equals(menuId2)));
+       }
+
+       private void updateColumnForSiblings(Session localSession, Long menuId, Integer oldParentMenuId,
+                       Integer newParentMenuId, Integer oldColumn, Integer newColumn) {
+               logger.debug(EELFLoggerDelegate.debugLogger, "entering updateColumnForSiblings");
+               Criteria criteria = localSession.createCriteria(FunctionalMenuItem.class);
+               criteria.add(Restrictions.ne("menuId", menuId));
+               if (parentMenuIdEqual(oldParentMenuId, newParentMenuId)) {
+                       logger.debug(EELFLoggerDelegate.debugLogger, "moving under the same parent");
+                       // We are moving to a new position under the same parent
+                       if (newParentMenuId == null) {
+                               logger.debug(EELFLoggerDelegate.debugLogger, "newParentMenuId is null, so using isNull");
+                               criteria.add(Restrictions.isNull("parentMenuId"));
+                       } else {
+                               logger.debug(EELFLoggerDelegate.debugLogger, "newParentMenuId is NOT null, so using eq");
+                               criteria.add(Restrictions.eq("parentMenuId", newParentMenuId));
+                       }
+                       if (oldColumn > newColumn) {
+                               logger.debug(EELFLoggerDelegate.debugLogger, "moving to a lower column under the same parent");
+                               // We are moving to a lower column under the same parent
+                               criteria.add(Restrictions.ge("column", newColumn));
+                               criteria.add(Restrictions.lt("column", oldColumn));
+                               @SuppressWarnings("unchecked")
+                               List<FunctionalMenuItem> menuItems = criteria.list();
+                               for (FunctionalMenuItem menuItem : menuItems) {
+                                       menuItem.column += 1;
+                                       localSession.save(menuItem);
+                               }
+                       } else if (oldColumn < newColumn) {
+                               logger.debug(EELFLoggerDelegate.debugLogger, "moving to a higher column under the same parent");
+                               // We are moving to a higher column under the same parent
+                               criteria.add(Restrictions.gt("column", oldColumn));
+                               criteria.add(Restrictions.le("column", newColumn));
+                               @SuppressWarnings("unchecked")
+                               List<FunctionalMenuItem> menuItems = criteria.list();
+                               for (FunctionalMenuItem menuItem : menuItems) {
+                                       menuItem.column -= 1;
+                                       localSession.save(menuItem);
+                               }
+                       } else {
+                               // No info has changed
+                               logger.debug(EELFLoggerDelegate.debugLogger, "no info has changed, so we are not moving");
+                       }
+               } else {
+                       logger.debug(EELFLoggerDelegate.debugLogger, "moving under a new parent");
+                       // We are moving under a new parent.
+
+                       // Adjust the children under the old parent
+                       logger.debug(EELFLoggerDelegate.debugLogger, "about to adjust the children under the old parent");
+
+                       // If the parentId is null, must check for its children differently
+                       if (oldParentMenuId == null) {
+                               logger.debug(EELFLoggerDelegate.debugLogger, "oldParentMenuId is null, so using isNull");
+                               criteria.add(Restrictions.isNull("parentMenuId"));
+                       } else {
+                               logger.debug(EELFLoggerDelegate.debugLogger, "oldParentMenuId is NOT null, so using eq");
+                               criteria.add(Restrictions.eq("parentMenuId", oldParentMenuId));
+                       }
+
+                       criteria.add(Restrictions.gt("column", oldColumn));
+                       @SuppressWarnings("unchecked")
+                       List<FunctionalMenuItem> menuItems1 = criteria.list();
+                       for (FunctionalMenuItem menuItem : menuItems1) {
+                               menuItem.column -= 1;
+                               localSession.save(menuItem);
+                       }
+                       // Adjust the children under the new parent.
+                       logger.debug(EELFLoggerDelegate.debugLogger, "about to adjust the children under the new parent");
+                       logger.debug(EELFLoggerDelegate.debugLogger, "get all menu items where menuId!=" + menuId + "; parentMenuId==" + newParentMenuId
+                                       + "; column>=" + newColumn);
+                       criteria = localSession.createCriteria(FunctionalMenuItem.class);
+                       criteria.add(Restrictions.ne("menuId", menuId));
+                       if (newParentMenuId == null) {
+                               logger.debug(EELFLoggerDelegate.debugLogger, "newParentMenuId is null, so using isNull");
+                               criteria.add(Restrictions.isNull("parentMenuId"));
+                       } else {
+                               logger.debug(EELFLoggerDelegate.debugLogger, "newParentMenuId is NOT null, so using eq");
+                               criteria.add(Restrictions.eq("parentMenuId", newParentMenuId));
+                       }
+
+                       criteria.add(Restrictions.ge("column", newColumn));
+                       @SuppressWarnings("unchecked")
+                       List<FunctionalMenuItem> menuItems2 = criteria.list();
+                       if (menuItems2 != null) {
+                               logger.debug(EELFLoggerDelegate.debugLogger, "found " + menuItems2.size() + " menu items");
+                       } else {
+                               logger.debug(EELFLoggerDelegate.debugLogger, "found null menu items");
+                       }
+                       for (FunctionalMenuItem menuItem : menuItems2) {
+                               menuItem.column += 1;
+                               localSession.save(menuItem);
+                       }
+               }
+               logger.debug(EELFLoggerDelegate.debugLogger, "done with updateColumnForSiblings");
+       }
+
+       public void removeAppInfo(Session localSession, Long menuId) {
+               // Remove the url, role, and app info from a menu item
+               FunctionalMenuItem menuItem = (FunctionalMenuItem) localSession.get(FunctionalMenuItem.class, menuId);
+               menuItem.url = "";
+               deleteRoles(menuId);
+       }
+
+       public FieldsValidator editFunctionalMenuItem(FunctionalMenuItemWithRoles menuItemJson) {
+               boolean result                  = false;
+               Session localSession    = null;
+               Transaction transaction = null;
+               Long menuId                     = menuItemJson.menuId;
+
+               logger.debug(EELFLoggerDelegate.debugLogger, "LR: editFunctionalMenuItem: test 1");
+               FieldsValidator fieldsValidator = menuItemFieldsChecker(menuItemJson);
+               if (fieldsValidator.httpStatusCode.intValue() == HttpServletResponse.SC_OK) {
+                       // TODO: make sure menuId is here. And, it might not already exist
+                       // in db table.
+                       if (menuId == null) {
+                               fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_BAD_REQUEST);
+                               logger.error(EELFLoggerDelegate.errorLogger, "FunctionalMenuServiceImpl.editFunctionalMenuItem: bad request");
+                       } else {
+                               // To simplify the code, assume we will have a transaction
+                               try {
+                                       localSession = sessionFactory.openSession();
+                                       transaction = localSession.beginTransaction();
+
+                                       // Get the existing info associated with menuItem from the DB
+                                       FunctionalMenuItem menuItem = (FunctionalMenuItem) localSession.get(FunctionalMenuItem.class, menuId);
+                                       Integer oldColumn = menuItem.column;
+                                       Integer oldParentMenuId = menuItem.parentMenuId;
+                                       Integer newColumn = menuItemJson.column;
+                                       Integer newParentMenuId = menuItemJson.parentMenuId;
+
+                                       logger.debug(EELFLoggerDelegate.debugLogger, "prev info: column: " + oldColumn + "; parentMenuId: " + oldParentMenuId);
+
+                                       if (menuItemJson.appid != null && menuItemJson.getRoles() != null && !menuItemJson.getRoles().isEmpty()
+                                                       && menuItemJson.url != null && !menuItemJson.url.isEmpty() && menuItemJson.text != null
+                                                       && !menuItemJson.text.isEmpty()) {
+                                               // Scenario: appid, roles, url and text are all non-null.
+                                               // This menu item is associated with an app.
+                                               // (Note: this should only occur for a bottom-level menu
+                                               // item with no children.)
+                                               // 1) Remove all the records from fn_menu_functional_role
+                                               // for this menuId.
+                                               // 2) Add records to the fn_menu_function_role table for the
+                                               // appId and each roleId
+                                               // 3) Update the url and text for this menu item.
+
+                                               // Because of foreign key constraints, delete the roles,
+                                               // then update the menuItem then add the roles.
+                                               deleteRoles(menuId);
+                                               // Assumption: this is not a Move, so don't change the
+                                               // parentMenuId and column.
+                                               menuItem.appid = menuItemJson.appid;
+                                               menuItem.setRoles(menuItemJson.getRoles());
+                                               menuItem.url = menuItemJson.url;
+                                               menuItem.text = menuItemJson.text;
+                                               
+                                               // If the app is disabled, deactivate the menu item.
+                                               Long appidLong = Long.valueOf(menuItemJson.appid);
+                                               EPApp app = (EPApp) localSession.get(EPApp.class, appidLong);
+                                               if (app != null && ! app.getEnabled()) {
+                                                       menuItem.active_yn = "N";
+                                               } else {
+                                                       menuItem.active_yn = "Y";
+                                               }
+
+
+                                               localSession.update(menuItem);
+                                               addRoles(menuItemJson, localSession);
+
+                                       } else if (menuItemJson.appid == null && (menuItemJson.getRoles() == null || menuItemJson.getRoles().isEmpty())
+                                                       && (menuItemJson.url == null || menuItemJson.url.isEmpty()) && menuItemJson.text != null
+                                                       && !menuItemJson.text.isEmpty()) {
+                                               // Scenario: appid, roles and url are all null; text is
+                                               // non-null.
+                                               // This menu item is NOT associated with an app.
+                                               // 1) Remove all the records from fn_menu_functional_role
+                                               // for this menuId
+                                               // (in case it was previously associated with an app).
+                                               // 2) Update the text for this menu item.
+                                               // 3) Set the url to ""
+                                               deleteRoles(menuId);
+                                               // Assumption: this is not a Move, so don't change the
+                                               // parentMenuId and column.
+                                               menuItem.text = menuItemJson.text;
+                                               menuItem.url = "";
+                                               menuItem.active_yn = "Y";
+                                               localSession.update(menuItem);
+
+                                       } else if (newColumn != null) {
+                                               // This is a "move" request.
+                                               // Menu item has been moved to a different position under
+                                               // the same parent, or under a new parent.
+                                               logger.debug(EELFLoggerDelegate.debugLogger, "Doing a move operation.");
+                                               if (parentMenuIdEqual(oldParentMenuId, newParentMenuId)) {
+                                                       // The parent is the same. We have just changed the
+                                                       // column
+                                                       logger.debug(EELFLoggerDelegate.debugLogger, "moving under the same parent");
+                                                       menuItem.column = newColumn;
+                                                       localSession.update(menuItem);
+                                               } else {
+                                                       logger.debug(EELFLoggerDelegate.debugLogger, "moving under a different parent");
+                                                       menuItem.parentMenuId = newParentMenuId;
+                                                       menuItem.column = newColumn;
+                                                       localSession.update(menuItem);
+                                                       // If we are moving under a new parent, must delete any
+                                                       // app/role info from
+                                                       // the new parent, since it is no longer a leaf menu
+                                                       // item and cannot have app info
+                                                       // associated with it. The front end will have warned
+                                                       // the user and gotten confirmation.
+                                                       if (menuItemJson.parentMenuId != null) {
+                                                               Long parentMenuIdLong = new Long(menuItemJson.parentMenuId);
+                                                               removeAppInfo(localSession, parentMenuIdLong);
+                                                               // deleteRoles(parentMenuIdLong);
+                                                       }
+                                               }
+                                               // must update the column for all old and new sibling menu
+                                               // items
+                                               updateColumnForSiblings(localSession, menuId, oldParentMenuId, newParentMenuId, oldColumn,
+                                                               newColumn);
+                                       }
+
+                                       transaction.commit();
+                                       logger.debug(EELFLoggerDelegate.debugLogger, "LR: editFunctionalMenuItem: finished committing transaction");
+                                       result = true;
+                               } catch (Exception e) {
+                                       EcompPortalUtils.rollbackTransaction(transaction,
+                                                       "createFunctionalMenuItem rollback, exception = " + e);
+                                       logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
+                               } finally {
+                                       EcompPortalUtils.closeLocalSession(localSession, "editFunctionalMenuItem");
+                               }
+                               
+                               if (result) {
+                               } else {
+                                       logger.debug(EELFLoggerDelegate.debugLogger, "LR: createFunctionalMenuItem: no result. setting httpStatusCode to "
+                                                       + HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+                                       fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+                               }
+                       }
+               }       
+               
+               return fieldsValidator;
+       }
+
+       public FieldsValidator deleteFunctionalMenuItem(Long menuId) {
+               FieldsValidator fieldsValidator = new FieldsValidator();
+               logger.debug(EELFLoggerDelegate.debugLogger, "LR: deleteFunctionalMenuItem: test 1");
+               boolean result = false;
+               Session localSession = null;
+               Transaction transaction = null;
+
+               try {
+                       localSession = sessionFactory.openSession();
+                       transaction = localSession.beginTransaction();
+                       // We must turn off foreign keys before deleting a menu item. Otherwise there will be a 
+                       // constraint violation from the ancestors table.
+                       setForeignKeys(localSession, false);
+                       deleteRoles(menuId);
+                       logger.debug(EELFLoggerDelegate.debugLogger, "deleteFunctionalMenuItem: after deleting roles");
+                       deleteFavorites(menuId);
+                       logger.debug(EELFLoggerDelegate.debugLogger, "deleteFunctionalMenuItem: after deleting favorites");
+                       localSession.delete(localSession.get(FunctionalMenuItem.class, menuId));
+                       logger.debug(EELFLoggerDelegate.debugLogger, "deleteFunctionalMenuItem: about to commit");
+                       transaction.commit();
+                       result = true;
+               } catch (Exception e) {
+                       EcompPortalUtils.rollbackTransaction(transaction,
+                                       "deleteFunctionalMenuItem rollback, exception = " + e);
+                       logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
+               } finally {
+                       EcompPortalUtils.closeLocalSession(localSession, "deleteFunctionalMenuItem");
+               }
+               if (result) {
+               } else {
+                       logger.debug(EELFLoggerDelegate.debugLogger, "LR: deleteFunctionalMenuItem: no result. setting httpStatusCode to "
+                                       + HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+                       fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+               }
+               return fieldsValidator;
+       }
+       
+       // Regenerate the fn_menu_functional_ancestors table, which is used
+       // by the queries that return the functional menu items.
+       public FieldsValidator regenerateAncestorTable() {
+               FieldsValidator fieldsValidator = new FieldsValidator();
+               Session localSession = null;
+               Transaction transaction = null;
+
+               try {
+                       localSession = sessionFactory.openSession();
+                       transaction = localSession.beginTransaction();
+                       String sql = "DELETE FROM fn_menu_functional_ancestors";
+                       logQuery(sql);
+                       Query query = localSession.createSQLQuery(sql);
+                       query.executeUpdate();
+                       logger.debug(EELFLoggerDelegate.debugLogger, "regenerateAncestorTable: finished query 1");
+                       
+                       sql = "ALTER TABLE fn_menu_functional_ancestors AUTO_INCREMENT=1";
+                       logQuery(sql);
+                       query = localSession.createSQLQuery(sql);
+                       query.executeUpdate();
+                       logger.debug(EELFLoggerDelegate.debugLogger, "regenerateAncestorTable: reset AUTO_INCREMENT to 1");
+
+                       int depth = 0;
+                       sql = "INSERT INTO fn_menu_functional_ancestors(menu_id, ancestor_menu_id, depth) "
+                                       + "SELECT m.menu_id, m.menu_id, " + depth + " FROM fn_menu_functional m";
+                       logQuery(sql);
+                       query = localSession.createSQLQuery(sql);
+                       query.executeUpdate();
+                       logger.debug(EELFLoggerDelegate.debugLogger, "regenerateAncestorTable: finished query 2");
+                       for (depth = 0; depth < 3; depth++) {
+                               int depthPlusOne = depth + 1;
+                               sql = "INSERT INTO fn_menu_functional_ancestors(menu_id, ancestor_menu_id, depth) "
+                                               + " SELECT a.menu_id, m.parent_menu_id, " + depthPlusOne
+                                               + " FROM fn_menu_functional m, fn_menu_functional_ancestors a " + " WHERE a.depth='" + depth
+                                               + "' AND " + " a.ancestor_menu_id = m.menu_id AND " + " m.parent_menu_id != '-1'";
+                               logQuery(sql);
+                               query = localSession.createSQLQuery(sql);
+                               query.executeUpdate();
+                       }
+                       logger.debug(EELFLoggerDelegate.debugLogger, "regenerateAncestorTable: finished query 3");
+                       transaction.commit();
+               } catch (Exception e) {
+                       EcompPortalUtils.rollbackTransaction(transaction,
+                                       "regenerateAncestorTable rollback, exception = " + e);
+                       logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
+                       fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+               } finally {
+                       EcompPortalUtils.closeLocalSession(localSession, "regenerateAncestorTable");
+               }
+               return fieldsValidator;
+       }
+
+       private void logQuery(String sql) {
+               logger.debug(EELFLoggerDelegate.debugLogger, "Executing query: " + sql);
+       }
+
+       public FieldsValidator setFavoriteItem(FavoritesFunctionalMenuItem menuItemJson) {
+               boolean result = false;
+               FieldsValidator fieldsValidator = new FieldsValidator();
+               
+               Session localSession = null;
+               Transaction transaction = null;
+               
+               try {
+            logger.debug(EELFLoggerDelegate.debugLogger, String.format("Before adding favorite for user id:{0} and menu id:{1} ",menuItemJson.userId,menuItemJson.menuId));
+                       localSession = sessionFactory.openSession();
+                       transaction = localSession.beginTransaction();
+                       localSession.save(menuItemJson);
+                       transaction.commit();                           
+                       result = true;
+            logger.debug(EELFLoggerDelegate.debugLogger, String.format("After adding favorite for user id:{0} and menu id:{1} ",menuItemJson.userId,menuItemJson.menuId));                                                             
+               } catch (Exception e) {
+                       EcompPortalUtils.rollbackTransaction(transaction,"setFavoriteItem rollback, exception = " + e);
+                       logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
+               } finally {
+                       EcompPortalUtils.closeLocalSession(localSession, "setFavoriteItem");
+               }                                                                                       
+               
+               if(result) {
+               }
+               else {
+                       fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+               }
+               
+               return fieldsValidator;
+       }
+       
+       public List<FavoritesFunctionalMenuItemJson> getFavoriteItems(Long userId) {            
+               try {
+                       logger.debug(EELFLoggerDelegate.debugLogger, "Before getting favorites for user id: " + userId);
+                       
+                       // Divide this into 2 queries: one which returns the favorites items associated with Restricted apps,
+                       // and one that returns all the other favorites items. Then we can easily add the boolean flag
+                       // restrictedApp to each FavoritesFunctionalMenuItemJson, to be used by the front end.
+
+                       String sql = "SELECT f.user_id,f.menu_id,m.text,m.url "
+                                       + " FROM fn_menu_favorites f, fn_menu_functional m, fn_menu_functional_roles mr "
+                                       + " WHERE f.user_id='" + userId + "' AND f.menu_id = m.menu_id "
+                                       + " AND f.menu_id = mr.menu_id "
+                                       + " AND mr.role_id = '" + RESTRICTED_APP_ROLE_ID + "' ";
+
+                       @SuppressWarnings("unchecked")
+                       List<FavoritesFunctionalMenuItemJson> menuItems = dataAccessService.executeSQLQuery(sql, FavoritesFunctionalMenuItemJson.class, null);
+                       for (FavoritesFunctionalMenuItemJson menuItem : menuItems) {
+                               menuItem.restrictedApp = true;
+                       }
+                       
+                       sql = "SELECT DISTINCT f.user_id,f.menu_id,m.text,m.url "
+                                       + " FROM fn_menu_favorites f, fn_menu_functional m, fn_menu_functional_roles mr "
+                                       + " WHERE f.user_id='" + userId + "' AND f.menu_id = m.menu_id "
+                                       + " AND f.menu_id = mr.menu_id "
+                                       + " AND mr.role_id != '" + RESTRICTED_APP_ROLE_ID + "' ";
+                       @SuppressWarnings("unchecked")
+                       List<FavoritesFunctionalMenuItemJson> menuItems2 = dataAccessService.executeSQLQuery(sql, FavoritesFunctionalMenuItemJson.class, null);
+                       for (FavoritesFunctionalMenuItemJson menuItem : menuItems2) {
+                               menuItem.restrictedApp = false;
+                               menuItems.add(menuItem);
+                       }
+                       
+                       return menuItems;
+               } catch (Exception e) {
+                       logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred in FunctionalMenuServiceImpl.getFavoriteItems. Details: " + EcompPortalUtils.getStackTrace(e));
+                       List<FavoritesFunctionalMenuItemJson> menuItems = new ArrayList<FavoritesFunctionalMenuItemJson>();
+                       return menuItems;
+               }                                                                                                                                       
+       }
+       
+       public FieldsValidator removeFavoriteItem(Long userId, Long menuId) {
+               boolean result = false;
+               FieldsValidator fieldsValidator = new FieldsValidator();
+               
+               Session localSession = null;
+               Transaction transaction = null;
+               
+               try {                   
+                       
+                       FavoritesFunctionalMenuItem menuItemJson = new FavoritesFunctionalMenuItem();
+                       menuItemJson.userId = userId;
+                       menuItemJson.menuId = menuId;
+                       
+                       localSession = sessionFactory.openSession();
+                       transaction = localSession.beginTransaction();
+                       localSession.delete(menuItemJson);
+                       localSession.flush();
+                       transaction.commit();                           
+                       result = true;
+                       logger.debug(EELFLoggerDelegate.debugLogger, String.format("After removing favorite for user id: " + userId + "; menu id: " + menuId));                                                         
+               } catch (Exception e) {
+                       EcompPortalUtils.rollbackTransaction(transaction,"removeFavoriteItem rollback, exception = " + e);
+                       logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
+               } finally {                                                             
+                       EcompPortalUtils.closeLocalSession(localSession, "removeFavoriteItem");
+               }                                                                                       
+               
+               if(result) {
+               }
+               else {
+                       fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+               }
+               
+               return fieldsValidator;
+       }
+       
+       @Override
+       public void assignHelpURLs(List<FunctionalMenuItem> menuItems) {
+               try {
+                       String user_guide_link = SystemProperties.getProperty(EPCommonSystemProperties.USER_GUIDE_URL);
+                       
+                       for(FunctionalMenuItem menuItem: menuItems){
+                               if(menuItem.text.equalsIgnoreCase("Contact Us")){
+                                       menuItem.setUrl("contactUs");
+                                       //menuItem.setRestrictedApp(true);
+                               }
+                               if(menuItem.text.equalsIgnoreCase("Get Access")) {
+                                       menuItem.setUrl("getAccess");
+                               }
+                               if(menuItem.text.equalsIgnoreCase("User Guide")) {
+                                       menuItem.setUrl(user_guide_link);
+                                       menuItem.setRestrictedApp(true);
+                               }
+                       }
+               } catch (Exception e) {         
+                       logger.error(EELFLoggerDelegate.errorLogger, "assignHelpURLs process failed. Details: " + EcompPortalUtils.getStackTrace(e));
+               }
+               
+       }
+
+       public List<FunctionalMenuRole> getFunctionalMenuRole() {
+               String sql = "SELECT * from fn_menu_functional_roles";
+               logQuery(sql);
+               logger.debug(EELFLoggerDelegate.debugLogger, "getFunctionalMenuRole: logged the query");
+
+               @SuppressWarnings("unchecked")
+               List<FunctionalMenuRole> functionalMenuRole = dataAccessService.executeSQLQuery(sql, FunctionalMenuRole.class, null);
+               
+               return functionalMenuRole;
+       }
+
+       @SuppressWarnings("unchecked")
+       @Override
+       public List<BusinessCardApplicationRole> getUserAppRolesList(String userId) {
+               Map<String, String> params = new HashMap<>();
+               params.put("userId", userId);
+
+               List<BusinessCardApplicationRole> userAppRoles = null;
+               try {
+                       userAppRoles = dataAccessService
+                                       .executeNamedQuery("getUserApproles", params, null);
+               } catch (Exception e) {
+                       // TODO Auto-generated catch block
+                       logger.error(EELFLoggerDelegate.errorLogger, "getUserAppRolesList failed", e);
+               }               
+               return userAppRoles;
+       }
+       
+}