summaryrefslogtreecommitdiffstats
path: root/ecomp-portal-BE-common/src/main/java/org/openecomp/portalapp/portal/service/FunctionalMenuServiceImpl.java
blob: 26f1431eaf51dd31afe2bfeb056711ad6a7003e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-
 * ============LICENSE_START==========================================
 * ONAP Portal
 * ===================================================================
 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * ===================================================================
 *
 * Unless otherwise specified, all software contained herein is licensed
 * under the Apache License, Version 2.0 (the “License”);
 * you may not use this software 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.
 *
 * Unless otherwise specified, all documentation contained herein is licensed
 * under the Creative Commons License, Attribution 4.0 Intl. (the “License”);
 * you may not use this documentation except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *             https://creativecommons.org/licenses/by/4.0/
 *
 * Unless required by applicable law or agreed to in writing, documentation
 * 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.
 *
 * ============LICENSE_END============================================
 *
 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 */
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.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;
import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
import org.openecomp.portalsdk.core.service.DataAccessService;
import org.openecomp.portalsdk.core.util.SystemProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Service;

@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 && menuItem != null) {
			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, "menuItemFieldsChecker failed", 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) {
				logger.error(EELFLoggerDelegate.errorLogger, "createFunctionalMenuItem failed", e);
				EcompPortalUtils.rollbackTransaction(transaction,
						"createFunctionalMenuItem rollback, exception = " + e.toString());
			} 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) {
					logger.error(EELFLoggerDelegate.errorLogger, "editFunctionalMenuItem failed", e);
					EcompPortalUtils.rollbackTransaction(transaction,
							"createFunctionalMenuItem rollback, exception = " + e.toString());
				} 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) {
			logger.error(EELFLoggerDelegate.errorLogger, "deleteFunctionalMenuItem failed", e);
			EcompPortalUtils.rollbackTransaction(transaction,
					"deleteFunctionalMenuItem rollback, exception = " + e.toString());
		} 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) {
			logger.error(EELFLoggerDelegate.errorLogger, "regenerateAncestorTable failed", e);
			EcompPortalUtils.rollbackTransaction(transaction,
					"regenerateAncestorTable rollback, exception = " + e.toString());
			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) {
			logger.error(EELFLoggerDelegate.errorLogger, "setFavoriteItem failed", e);
			EcompPortalUtils.rollbackTransaction(transaction, "setFavoriteItem rollback, exception = " + e.toString());
		} 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, "getFavoriteItems failed", 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) {
			logger.error(EELFLoggerDelegate.errorLogger, "removeFavoriteItem failed", e);
			EcompPortalUtils.rollbackTransaction(transaction,
					"removeFavoriteItem rollback, exception = " + e.toString());
		} 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", 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;
	}

}