summaryrefslogtreecommitdiffstats
path: root/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/service/EPAppCommonServiceImpl.java
blob: cec4f41986c154d08ae813752a09abf35503f3bb (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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
/*-
 * ============LICENSE_START==========================================
 * ONAP Portal
 * ===================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ===================================================================
 * Modifications Copyright (c) 2019 Samsung
 * ===================================================================
 *
 * 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============================================
 *
 * 
 */
package org.onap.portalapp.portal.service;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.json.JSONArray;
import org.json.JSONObject;
import org.onap.portalapp.portal.domain.AdminUserApp;
import org.onap.portalapp.portal.domain.AdminUserApplications;
import org.onap.portalapp.portal.domain.AppIdAndNameTransportModel;
import org.onap.portalapp.portal.domain.AppsResponse;
import org.onap.portalapp.portal.domain.EPApp;
import org.onap.portalapp.portal.domain.EPUser;
import org.onap.portalapp.portal.domain.EPUserAppRolesRequest;
import org.onap.portalapp.portal.domain.EPUserAppRolesRequestDetail;
import org.onap.portalapp.portal.domain.EPUserAppsManualSortPreference;
import org.onap.portalapp.portal.domain.EPUserAppsSortPreference;
import org.onap.portalapp.portal.domain.EPWidgetsManualSortPreference;
import org.onap.portalapp.portal.domain.EcompApp;
import org.onap.portalapp.portal.domain.MicroserviceData;
import org.onap.portalapp.portal.domain.UserRole;
import org.onap.portalapp.portal.domain.UserRoles;
import org.onap.portalapp.portal.ecomp.model.AppCatalogItem;
import org.onap.portalapp.portal.logging.format.EPAppMessagesEnum;
import org.onap.portalapp.portal.logging.logic.EPLogUtil;
import org.onap.portalapp.portal.transport.EPAppsManualPreference;
import org.onap.portalapp.portal.transport.EPAppsSortPreference;
import org.onap.portalapp.portal.transport.EPDeleteAppsManualSortPref;
import org.onap.portalapp.portal.transport.EPWidgetsSortPreference;
import org.onap.portalapp.portal.transport.FieldsValidator;
import org.onap.portalapp.portal.transport.FunctionalMenuItem;
import org.onap.portalapp.portal.transport.LocalRole;
import org.onap.portalapp.portal.transport.OnboardingApp;
import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
import org.onap.portalapp.portal.utils.EcompPortalUtils;
import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
import org.onap.portalsdk.core.onboarding.ueb.Helper;
import org.onap.portalsdk.core.onboarding.ueb.TopicManager;
import org.onap.portalsdk.core.onboarding.util.CipherUtil;
import org.onap.portalsdk.core.onboarding.util.KeyConstants;
import org.onap.portalsdk.core.onboarding.util.KeyProperties;
import org.onap.portalsdk.core.onboarding.util.PortalApiConstants;
import org.onap.portalsdk.core.onboarding.util.PortalApiProperties;
import org.onap.portalsdk.core.service.DataAccessService;
import org.onap.portalsdk.core.util.SystemProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

import com.att.nsa.apiClient.http.HttpException;
import com.att.nsa.cambria.client.CambriaClient.CambriaApiException;
import com.att.nsa.cambria.client.CambriaClientBuilders;
import com.att.nsa.cambria.client.CambriaIdentityManager;
import com.att.nsa.cambria.client.CambriaTopicManager;
import java.security.SecureRandom;


public class EPAppCommonServiceImpl implements EPAppService {

	protected String ECOMP_APP_ID = "1";
	protected String SUPER_ADMIN_ROLE_ID = "1";
	protected String ACCOUNT_ADMIN_ROLE_ID = "999";
	protected String RESTRICTED_APP_ROLE_ID = "900";

	//private static final String urlField = "url";
	private static final String nameSpaceField = "url";

	private static final String nameField = "name";

	private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPAppCommonServiceImpl.class);

	@Autowired
	private AdminRolesService adminRolesService;
	@Autowired
	protected SessionFactory sessionFactory;
	@Autowired
	private DataAccessService dataAccessService;	
	
	RestTemplate template = new RestTemplate();

	@PostConstruct
	private void init() {
		SUPER_ADMIN_ROLE_ID = SystemProperties.getProperty(EPCommonSystemProperties.SYS_ADMIN_ROLE_ID);
		ACCOUNT_ADMIN_ROLE_ID = SystemProperties.getProperty(EPCommonSystemProperties.ACCOUNT_ADMIN_ROLE_ID);
		ECOMP_APP_ID = SystemProperties.getProperty(EPCommonSystemProperties.ECOMP_APP_ID);
		RESTRICTED_APP_ROLE_ID = SystemProperties.getProperty(EPCommonSystemProperties.RESTRICTED_APP_ROLE_ID);
	}
	
	public Boolean onboardingAppFieldsValidation(OnboardingApp onboardingApp) {
		//FieldsValidator fieldsValidator = new FieldsValidator();

		if ((!onboardingApp.getRestrictedApp()) &&( onboardingApp.getAppName() == null || onboardingApp.getAppName().length() == 0 || onboardingApp.getRestrictedApp() == null
				|| onboardingApp.getLandingPage() == null || onboardingApp.getLandingPage().length() == 0 || onboardingApp.getRestUrl() == null || onboardingApp.getRestUrl().length() == 0
			    || onboardingApp.getAppBasicAuthUsername() == null || onboardingApp.getAppBasicAuthUsername().length() == 0
				|| onboardingApp.getIsOpen() == null
				|| (onboardingApp.getId() != null && onboardingApp.getId().equals(ECOMP_APP_ID)))
				// For a normal app (appType == PortalConstants.PortalAppId),
				// these fields must be filled
				// in.
				// For a restricted app (appType==2), they will be empty.
				|| ((onboardingApp.getRestrictedApp()) && (onboardingApp.getAppName() == null || onboardingApp.getAppName().length() == 0
						|| onboardingApp.getLandingPage() == null || onboardingApp.getLandingPage().length() == 0 || onboardingApp.getIsOpen() == null))) {
			return false;
		}
		return true;
		
	}
	
	private Boolean onboardingInactiveAppFieldsForValidation(OnboardingApp onboardingApp) {
		if (onboardingApp.getAppName() == null || onboardingApp.getAppName().length() == 0
				|| onboardingApp.getIsOpen() == null) {
			return false;
		}
		return true;
	}

	protected FieldsValidator onboardingAppFieldsChecker(OnboardingApp onboardingApp) {
		FieldsValidator fieldsValidator = new FieldsValidator();
		if (onboardingApp.getRolesInAAF()) {
			if (!onboardingApp.getIsEnabled()) {
				if (!onboardingInactiveAppFieldsForValidation(onboardingApp)) {
					fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_BAD_REQUEST);
				}
			} else if (onboardingApp.getIsEnabled()) {
				if (onboardingAppFieldsValidation(onboardingApp) == false || onboardingApp.getNameSpace() == null
						|| onboardingApp.getNameSpace().length() == 0) {
					fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_BAD_REQUEST);
				}
			}
		} else {
			if (!onboardingApp.getIsEnabled()) {
				if (!onboardingInactiveAppFieldsForValidation(onboardingApp)) {
					fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_BAD_REQUEST);
				}
			} else if (onboardingApp.getIsEnabled()) {
				if(onboardingApp.getRestrictedApp() && onboardingAppFieldsValidation(onboardingApp) == false){
					fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_BAD_REQUEST);
				}
				else if (!onboardingApp.getRestrictedApp() && (onboardingAppFieldsValidation(onboardingApp) == false || onboardingApp.getAppBasicAuthPassword() == null
						|| onboardingApp.getAppBasicAuthPassword().length() == 0)) {
					fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_BAD_REQUEST);
				}
			}
		}
		return fieldsValidator;
	}

	
	
	@Override
	public List<EPApp> getUserAsAdminApps(EPUser user) {
		if (adminRolesService.isAccountAdmin(user)) {
			String sql = "SELECT * FROM FN_APP join FN_USER_ROLE ON FN_USER_ROLE.APP_ID=FN_APP.APP_ID where "
					+ "FN_USER_ROLE.USER_ID=" + user.getId() + " AND FN_USER_ROLE.ROLE_ID=" + ACCOUNT_ADMIN_ROLE_ID
					+ " AND FN_APP.ENABLED = 'Y'";
			logQuery(sql);
			try {
				@SuppressWarnings("unchecked")
				List<EPApp> adminApps = dataAccessService.executeSQLQuery(sql, EPApp.class, null);
				return adminApps;
			} catch (Exception e) {
				EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError, e);
				return null;
			}
			
		} else {
			logger.error(EELFLoggerDelegate.errorLogger,
					"getUserAsAdminApps: only Account Admin may invoke this function!");
			return new ArrayList<EPApp>();
		}
	}
	
	

	@Override
	public List<EPApp> getUserByOrgUserIdAsAdminApps(String orgUserId) {
		String format = "SELECT * FROM FN_APP app INNER JOIN FN_USER_ROLE userrole ON userrole.APP_ID=app.APP_ID "
				+ "INNER JOIN FN_USER user on user.USER_ID = userrole.USER_ID "
				+ "WHERE user.org_user_id = '%s' AND userrole.ROLE_ID=" + ACCOUNT_ADMIN_ROLE_ID
				+ " AND FN_APP.ENABLED = 'Y'";

		String sql = String.format(format, orgUserId);
		logQuery(sql);

		try {
			@SuppressWarnings("unchecked")
			List<EPApp> adminApps = dataAccessService.executeSQLQuery(sql, EPApp.class, null);
			return adminApps;
		} catch (Exception e) {
			EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError, e);
			return null;
		}
	}

	@Override
	public List<EPApp> getAppsFullList() {
		@SuppressWarnings("unchecked")
		List<EPApp> apps = dataAccessService.getList(EPApp.class, null);
		return apps;
	}

	@Override
	public List<EcompApp> getEcompAppAppsFullList() {
		return transformAppsToEcompApps(getAppsFullList());
	}

	@Override
	public List<EcompApp> transformAppsToEcompApps(List<EPApp> appsList) {
		List<EcompApp> ecompAppList = new ArrayList<EcompApp>();
		for (EPApp app : appsList) {
			EcompApp ecompApp = new EcompApp();
			ecompApp.setId(app.getId());
			ecompApp.setName(app.getName());
			ecompApp.setImageUrl(app.getImageUrl());
			ecompApp.setDescription(app.getAppDescription());
			ecompApp.setNotes(app.getAppNotes());
			ecompApp.setUrl(app.getLandingPage());
			ecompApp.setAlternateUrl(app.getAlternateLandingPage());
			ecompApp.setUebTopicName(app.getUebTopicName());
			//ecompApp.setUebKey(app.getUebKey());
			ecompApp.setUebSecret(app.getUebSecret());
			ecompApp.setEnabled(app.getEnabled());
			ecompApp.setCentralAuth(app.getRolesInAAF());
			ecompApp.setNameSpace(app.getNameSpace());
			ecompApp.setRestrictedApp(app.isRestrictedApp());
			ecompAppList.add(ecompApp);
		}
		return ecompAppList;
	}

	@Override
	public EPApp getApp(Long appId) {
		try {
			return (EPApp) dataAccessService.getDomainObject(EPApp.class, appId, null);
		} catch (Exception e) {
			EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError, e);
			return null;
		}
	}

	
	
	@SuppressWarnings("unchecked")
	@Override
	public List<AppIdAndNameTransportModel> getAdminApps(EPUser user) {

		if (adminRolesService.isAccountAdmin(user) && adminRolesService.isRoleAdmin(user)) {
			final Map<String, Long> params = new HashMap<>();
			params.put("userId", user.getId());
			List applicationRoleswithAccountandRoleadmin = dataAccessService
					.executeNamedQuery("getApplicationsofTheUserwithAdminAndRoleAdmin", params, null);
			try {
				return applicationRoleswithAccountandRoleadmin;
			} catch (Exception e) {
				EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError, e);
				logger.error(EELFLoggerDelegate.errorLogger,
						"Exception occurred while fetching the list of user who has type account and role approver "
								+ user.getLoginId(),
						e);
			}
		}

		else {
			if (adminRolesService.isAccountAdmin(user)) {
				String format = "SELECT app.APP_ID, app.APP_NAME, app.APP_TYPE FROM FN_APP app inner join FN_USER_ROLE userrole ON userrole.APP_ID=app.APP_ID "
						+ "where userrole.USER_ID = %d AND userrole.ROLE_ID=" + ACCOUNT_ADMIN_ROLE_ID
						+ " AND (app.ENABLED = 'Y' OR app.APP_ID=1)";
				String sql = String.format(format, user.getId());
				logQuery(sql);
				try {
					return dataAccessService.executeSQLQuery(sql, AppIdAndNameTransportModel.class, null);
				} catch (Exception e) {
					EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError, e);
					logger.error(EELFLoggerDelegate.errorLogger,
							"Exception occurred while fetching the adminApps for user " + user.getLoginId(), e);
				}

			}

			if (adminRolesService.isRoleAdmin(user)) {
				final Map<String, Long> params = new HashMap<>();
				params.put("userId", user.getId());
				List applicationRoles = dataAccessService.executeNamedQuery("getApplicationsofTheUserContainsApprover",
						params, null);

				try {
					return applicationRoles;
				} catch (Exception e) {
					EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError, e);
					logger.error(EELFLoggerDelegate.errorLogger,
							"Exception occurred while fetching the list of user who has type approver "
									+ user.getLoginId(),
							e);
				}

			}
		}
		// sql += " AND app.APP_REST_ENDPOINT IS NOT NULL AND
		// app.APP_REST_ENDPOINT <> ''";

		return new ArrayList<AppIdAndNameTransportModel>();
	}

	@Override
	public EPApp getAppDetail(String appName) {
		final Map<String, String> params = new HashMap<String, String>();
		try {
			params.put("appName", appName);
			@SuppressWarnings("unchecked")
			List<EPApp> apps = (List<EPApp>) dataAccessService.executeNamedQuery("getMyloginAppDetails", params, null);
			return (apps.size() > 0) ? apps.get(0) : null;
		} catch(Exception e) {
			EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError, e);
			return null;
		}
	}
	
	@Override
	public EPApp getAppDetailByAppName(String appName) {
		final Map<String, String> params = new HashMap<String, String>();
		try {
			params.put("appName", appName);
			@SuppressWarnings("unchecked")
			List<EPApp> apps = (List<EPApp>) dataAccessService.executeNamedQuery("getAppDetailsByAppName", params, null);
			if (apps.size() > 0) {
				EPApp app = apps.get(0);
				if (!EcompPortalUtils.checkIfRemoteCentralAccessAllowed()) {
					app.setRolesInAAF(false);
				}
				return app;
			} else{
				return null;
			}
		} catch (Exception e) {
			EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError, e);
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AppIdAndNameTransportModel> getAppsForSuperAdminAndAccountAdmin(EPUser user) {
		if (adminRolesService.isSuperAdmin(user) || adminRolesService.isAccountAdmin(user)) {
			String format = "";
			String sql = "";
			if (adminRolesService.isSuperAdmin(user)) {
				format = "SELECT app.APP_ID, app.APP_NAME, app.APP_TYPE FROM FN_APP app "
						+ "where app.ENABLED = 'Y' AND app.app_type = 1";
			} else {
				format = "SELECT app.APP_ID, app.APP_NAME, APP_TYPE FROM FN_APP app inner join FN_USER_ROLE userrole ON userrole.APP_ID=app.APP_ID "
						+ "where userrole.USER_ID = %d AND userrole.ROLE_ID=" + ACCOUNT_ADMIN_ROLE_ID
						+ " AND app.ENABLED = 'Y' AND app.app_type = 1";
			}
			sql = String.format(format, user.getId());
			// sql += " AND app.APP_REST_ENDPOINT IS NOT NULL AND
			// app.APP_REST_ENDPOINT <> ''";
			logQuery(sql);
			try {
				return dataAccessService.executeSQLQuery(sql, AppIdAndNameTransportModel.class, null);
			} catch (Exception e) {
				EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError, e);
				logger.error(EELFLoggerDelegate.errorLogger,
						"Exception occurred while fetching the adminApps for user " + user.getLoginId(), e);
			}
		}
		return new ArrayList<AppIdAndNameTransportModel>();
	}

	protected void logQuery(String sql) {
		logger.debug(EELFLoggerDelegate.debugLogger, "logQuery: " + sql);
	}

	public DataAccessService getDataAccessService() {
		return dataAccessService;
	}

	public void setDataAccessService(DataAccessService dataAccessService) {
		this.dataAccessService = dataAccessService;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AdminUserApplications> getAppsAdmins() {
		try {
			Map<String, String> params = new HashMap<>();
			params.put("accountAdminRoleId", ACCOUNT_ADMIN_ROLE_ID);
			List<AdminUserApp> adminApps = (List<AdminUserApp>) dataAccessService.executeNamedQuery("getAppsAdmins",
					params, null);
			return aggregateRowsResultsByUserId(adminApps);
		} catch (Exception e) {
			EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError, e);
			return null;
		}
	}

	private List<AdminUserApplications> aggregateRowsResultsByUserId(List<AdminUserApp> adminApps) {
		HashMap<Long, AdminUserApplications> adminUserApplications = new HashMap<Long, AdminUserApplications>();
		for (AdminUserApp app : adminApps) {
			Long userId = app.getUser_Id();
			if (adminUserApplications.get(userId) == null)
				adminUserApplications.put(userId, new AdminUserApplications(app));
			else
				adminUserApplications.get(userId).addApp(app.getAppId(), app.getAppName());
		}
		return new ArrayList<AdminUserApplications>(adminUserApplications.values());
	}

	@Override
	public List<AppsResponse> getAllApps(Boolean all) {
		// If all is true, return both active and inactive apps. Otherwise, just
		// active apps.
		@SuppressWarnings("unchecked")
		// Sort the list by application name so the drop-down looks pretty.
		List<EPApp> apps = all
				? (List<EPApp>) dataAccessService.getList(EPApp.class, " where id != " + ECOMP_APP_ID, "name", null)
				: (List<EPApp>) dataAccessService.getList(EPApp.class,
						" where ( enabled = 'Y' or id = " + ECOMP_APP_ID + ")", "name", null);

		List<AppsResponse> appsModified = new ArrayList<AppsResponse>();
		for (EPApp app : apps) {
			appsModified.add(new AppsResponse(app.getId(), app.getName(), app.isRestrictedApp(), app.getEnabled()));
		}
		return appsModified;
	}

	
	@Override
	public List<AppsResponse> getAllApplications(Boolean all) {
		// If all is true, return both active and inactive apps. Otherwise, just
		// active apps.
		@SuppressWarnings("unchecked")
		// Sort the list by application name so the drop-down looks pretty.
		List<EPApp> apps = all
				? (List<EPApp>) dataAccessService.getList(EPApp.class, " where id != " + ECOMP_APP_ID, "name", null)
						:dataAccessService.getList(EPApp.class, null);

		List<AppsResponse> appsModified = new ArrayList<AppsResponse>();
		for (EPApp app : apps) {
			appsModified.add(new AppsResponse(app.getId(), app.getName(), app.isRestrictedApp(), app.getEnabled()));
		}
		return appsModified;
	}
	@Override
	public UserRoles getUserProfile(String loginId) {
		final Map<String, String> params = new HashMap<>();
		params.put("org_user_id", loginId);
		@SuppressWarnings("unchecked")
		List<UserRole> userRoleList = dataAccessService.executeNamedQuery( "getUserRoles", params, null);
		ArrayList<UserRoles> usersRolesList = aggregateUserProfileRowsResultsByRole(userRoleList);
		if (usersRolesList == null || usersRolesList.size() < 1)
			return null;

		return usersRolesList.get(0);
	}

	@Override
	public UserRoles getUserProfileNormalized(EPUser user) {
		// Check database.
		UserRoles userAndRoles = getUserProfile(user.getLoginId());
		// If no roles are defined, treat this user as a guest.
		if (user.isGuest() || userAndRoles == null) {
			logger.debug(EELFLoggerDelegate.debugLogger, "getUserProfile: treating user {} as guest",
					user.getLoginId());
			UserRole userRole = new UserRole();
			userRole.setUser_Id(user.getId());
			userRole.setOrgUserId(user.getLoginId());
			userRole.setFirstName(user.getFirstName());
			userRole.setLastName(user.getLastName());
			userRole.setRoleId(-1L);
			userRole.setRoleName("Guest");
			userRole.setUser_Id(-1L);
			userAndRoles = new UserRoles(userRole);
		}

		return userAndRoles;
	}

	protected ArrayList<UserRoles> aggregateUserProfileRowsResultsByRole(List<UserRole> userRoleList) {
		HashMap<String, UserRoles> userRoles = new HashMap<String, UserRoles>();
		for (UserRole user : userRoleList) {
			String orgUserId = user.getOrgUserId();
			if (userRoles.get(orgUserId) == null)
				userRoles.put(orgUserId, new UserRoles(user));
			else
				userRoles.get(orgUserId).addRole(user.getRoleName());
		}
		return new ArrayList<UserRoles>(userRoles.values());
	}

	private boolean isRestrictedApp(Long appId) {
		EPApp app = getApp(appId);
		return app.isRestrictedApp();
	}

	// For the functional menu edit
	@Override
	public List<LocalRole> getAppRoles(Long appId) {
		String sql = "";
		if (isRestrictedApp(appId)) {
			sql = "SELECT ROLE_ID, ROLE_NAME from FN_ROLE where UPPER(ACTIVE_YN) = 'Y' AND ROLE_ID = '" + RESTRICTED_APP_ROLE_ID + "'";
		}else if(appId == 1){
			sql = "SELECT ROLE_ID, ROLE_NAME from FN_ROLE where UPPER(ACTIVE_YN) = 'Y' AND APP_ID IS NULL";
		}else{
			sql = "SELECT ROLE_ID, ROLE_NAME from FN_ROLE where UPPER(ACTIVE_YN) = 'Y' AND APP_ID = '" + appId + "'";
		}
		logQuery(sql);
		@SuppressWarnings("unchecked")
		List<LocalRole> appRoles = dataAccessService.executeSQLQuery(sql, LocalRole.class, null);
		return appRoles;
	}

	protected String userAppsQuery(EPUser user) {
		StringBuilder query = new StringBuilder();
		if (adminRolesService.isSuperAdmin(user)) {
			query.append("SELECT * FROM FN_APP where FN_APP.ENABLED = 'Y' ORDER BY APP_NAME");
		} else {
			query.append("SELECT * FROM FN_APP join FN_USER_ROLE ON FN_USER_ROLE.APP_ID = FN_APP.APP_ID where ");
			query.append(
					"FN_USER_ROLE.USER_ID = " + user.getId() + " AND FN_USER_ROLE.ROLE_ID != " + SUPER_ADMIN_ROLE_ID);
			query.append(" AND FN_APP.ENABLED = 'Y'");
		}
		return query.toString();
	}

	/*protected FieldsValidator onboardingAppFieldsChecker(OnboardingApp onboardingApp) {
		FieldsValidator fieldsValidator = new FieldsValidator();
		if(onboardingApp.isCentralAuth){
		if (onboardingApp.name == null || onboardingApp.name.length() == 0 || onboardingApp.url == null
				|| onboardingApp.url.length() == 0 || onboardingApp.getRestrictedApp() == null
				|| onboardingApp.getIsOpen() == null || onboardingApp.getIsEnabled() == null
				|| (onboardingApp.getId() != null && ECOMP_APP_ID.equals(onboardingApp.getId().toString()))
				// For a normal app (appType == PortalConstants.PortalAppId),
				// these fields must be filled
				// in.
				// For a restricted app (appType==2), they will be empty.
				|| ((!onboardingApp.getRestrictedApp()) && (onboardingApp.myLoginsAppName == null
						|| onboardingApp.myLoginsAppName.length() == 0 || onboardingApp.myLoginsAppOwner == null
						|| onboardingApp.myLoginsAppOwner.length() == 0 || onboardingApp.username == null
						|| onboardingApp.username.length() == 0 ))) {
			fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_BAD_REQUEST);
		}
		}else{

			if (onboardingApp.name == null || onboardingApp.name.length() == 0 || onboardingApp.url == null
					|| onboardingApp.url.length() == 0 || onboardingApp.getRestrictedApp() == null
					|| onboardingApp.getIsOpen() == null || onboardingApp.getIsEnabled() == null
					|| (onboardingApp.getId() != null && ECOMP_APP_ID.equals(onboardingApp.getId().toString()))
					// For a normal app (appType == PortalConstants.PortalAppId),
					// these fields must be filled
					// in.
					// For a restricted app (appType==2), they will be empty.
					|| ((!onboardingApp.getRestrictedApp()) && (onboardingApp.myLoginsAppName == null
							|| onboardingApp.myLoginsAppName.length() == 0 || onboardingApp.myLoginsAppOwner == null
							|| onboardingApp.myLoginsAppOwner.length() == 0 || onboardingApp.username == null
							|| onboardingApp.username.length() == 0 || onboardingApp.appPassword == null
							|| onboardingApp.appPassword.length() == 0))) {
				fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_BAD_REQUEST);
			}
			
			
		}
		return fieldsValidator;
	}*/

	@Override
	public List<EPApp> getUserApps(EPUser user) {
		List<EPApp> openApps = getOpenApps();

		if (user.isGuest()) {
			return openApps;
		} else {
			String sql = userAppsQuery(user);
			logQuery(sql);

			// TreeSet<EPApp> distinctApps = new TreeSet<EPApp>();
			List<EPApp> appsList = new ArrayList<>();
			@SuppressWarnings("unchecked")
			List<EPApp> adminApps = dataAccessService.executeSQLQuery(sql, EPApp.class, null);
			HashSet<EPApp> appSet = new HashSet<>();
			for (EPApp app : adminApps) {
				appSet.add(app);
				appsList.add(app);
			}

			for (EPApp app : openApps) {
				if (!appSet.contains(app))
					appsList.add(app);
			}

			return appsList;
		}
	}

	@Override
	public List<EPApp> getPersAdminApps(EPUser user) {
		final Map<String, Long> params = new HashMap<>();
		params.put("userId", user.getId());
		// Named query is stored in EP.hbm.xml, mapped to EPApp
		@SuppressWarnings("unchecked")
		List<EPApp> list = dataAccessService.executeNamedQuery("getPersAdminApps", params, null);
		return list;
	}

	@Override
	public List<EPApp> getPersUserApps(EPUser user) {
		final Map<String, Long> params = new HashMap<>();
		params.put("userId", user.getId());
		// Named query is stored in EP.hbm.xml, mapped to EPApp
		@SuppressWarnings("unchecked")
		List<EPApp> list = dataAccessService.executeNamedQuery("getPersUserApps", params, null);
		return list;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * org.onap.portalapp.portal.service.EPAppService#getAppCatalog(
	 * org.onap.portalapp.portal.domain.EPUser)
	 */
	@Override
	public List<AppCatalogItem> getUserAppCatalog(EPUser user) {
		final Map<String, Long> params = new HashMap<>();
		params.put("userId", user.getId());
		// Named query is stored in EP.hbm.xml, mapped to AppCatalogItem
		@SuppressWarnings("unchecked")
		List<AppCatalogItem> list = dataAccessService.executeNamedQuery("getUserAppCatalog", params, null);
		return list;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * org.onap.portalapp.portal.service.EPAppService#getAdminAppCatalog(
	 * org.onap.portalapp.portal.domain.EPUser)
	 */
	@Override
	public List<AppCatalogItem> getAdminAppCatalog(EPUser user) {
		final Map<String, Long> params = new HashMap<>();
		params.put("userId", user.getId());
		// Named query is stored in EP.hbm.xml, mapped to AppCatalogItem
		@SuppressWarnings("unchecked")
		List<AppCatalogItem> list = dataAccessService.executeNamedQuery("getAdminAppCatalog", params, null);
		return list;
	}

	private List<EPApp> getOpenApps() {
		@SuppressWarnings("unchecked")
		List<EPApp> openApps = dataAccessService.getList(EPApp.class, " where open='Y' and enabled='Y'", null, null);
		return openApps;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<EPApp> getAppsOrderByName(EPUser user) {
		final Map<String, Long> params = new HashMap<>();
		List<EPApp> sortedAppsByName = null;
		try {
			if (adminRolesService.isSuperAdmin(user)) {
				params.put("userId", user.getId());
				sortedAppsByName = dataAccessService.executeNamedQuery("getPersAdminAppsOrderByName", params, null);
			} else {
				params.put("userId", user.getId());
				sortedAppsByName = dataAccessService.executeNamedQuery("getPersUserAppsOrderByName", params, null);
			}
		} catch (Exception e) {
			logger.error(EELFLoggerDelegate.errorLogger, "getAppsOrderByName failed", e);
		}
		return sortedAppsByName;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<EPApp> getAppsOrderByLastUsed(EPUser user) {

		final Map<String, Long> params = new HashMap<>();
		List<EPApp> sortedAppsByLastUsed = new ArrayList<EPApp>();
		List<EPApp> finalsortedAppsByLastUsed = new ArrayList<EPApp>();
		try {
			if (adminRolesService.isSuperAdmin(user)) {
				params.put("userId", user.getId());
				sortedAppsByLastUsed = dataAccessService.executeNamedQuery("getAdminAppsOrderByLastUsed", params, null);
			} else {
				params.put("userId", user.getId());
				sortedAppsByLastUsed = dataAccessService.executeNamedQuery("getUserAppsOrderByLastUsed", params, null);
			}
			Set<String> epAppSet = new HashSet<String>();
			for (EPApp eapp : sortedAppsByLastUsed)
				if (!epAppSet.contains(eapp.getName())) {
					finalsortedAppsByLastUsed.add(eapp);
					epAppSet.add(eapp.getName());
				}

		} catch (Exception e) {
			logger.error(EELFLoggerDelegate.errorLogger, "getAppsOrderByLastUsed failed", e);
		}
		return finalsortedAppsByLastUsed;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<EPApp> getAppsOrderByMostUsed(EPUser user) {
		final Map<String, Long> params = new HashMap<>();
		List<EPApp> sortedAppsByMostUsed = new ArrayList<EPApp>();
		List<EPApp> finalsortedAppsByMostUsed = new ArrayList<EPApp>();
		try {
			if (adminRolesService.isSuperAdmin(user)) {
				params.put("userId", user.getId());
				sortedAppsByMostUsed = dataAccessService.executeNamedQuery("getAdminAppsOrderByMostUsed", params, null);
			} else {
				params.put("userId", user.getId());
				sortedAppsByMostUsed = dataAccessService.executeNamedQuery("getUserAppsOrderByMostUsed", params, null);
			}
			Set<String> epAppSet = new HashSet<String>();

			for (EPApp eapp : sortedAppsByMostUsed) {
				if (!epAppSet.contains(eapp.getName())) {
					finalsortedAppsByMostUsed.add(eapp);
					epAppSet.add(eapp.getName());
				}
			}
		} catch (Exception e) {
			logger.error(EELFLoggerDelegate.errorLogger, "getAppsOrderByMostUsed failed", e);
		}

		return finalsortedAppsByMostUsed;
	}

	/*
	 * This Method retrieves the User Apps by Sort Manual Preference
	 *
	 * @param: user--contains LoggedIn User Data
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<EPApp> getAppsOrderByManual(EPUser user) {
		final Map<String, Long> params = new HashMap<>();
		List<EPApp> sortedAppsByManual = new ArrayList<EPApp>();
		List<EPApp> finalsortedAppsByManual = new ArrayList<EPApp>();
		try {
			if (adminRolesService.isSuperAdmin(user)) {
				params.put("userId", user.getId());
				sortedAppsByManual = dataAccessService.executeNamedQuery("getAdminAppsOrderByManual", params, null);
			} else {
				params.put("userId", user.getId());
				sortedAppsByManual = dataAccessService.executeNamedQuery("getUserAppsOrderByManual", params, null);
			}
			Set<String> epAppSet = new HashSet<String>();

			for (EPApp eapp : sortedAppsByManual) {
				if (!epAppSet.contains(eapp.getName())) {
					finalsortedAppsByManual.add(eapp);
					epAppSet.add(eapp.getName());
				}
			}
		} catch (Exception e) {
			logger.error(EELFLoggerDelegate.errorLogger, "getAppsOrderByManual failed", e);
		}
		return finalsortedAppsByManual;
	}
	
	@Override
	public List<OnboardingApp> getOnboardingApps() {
		@SuppressWarnings("unchecked")
		List<EPApp> apps = dataAccessService.getList(EPApp.class, " where id!=" + ECOMP_APP_ID, null, null);
		List<OnboardingApp> onboardingAppsList = new ArrayList<OnboardingApp>();
		for (EPApp app : apps) {
			OnboardingApp onboardingApp = new OnboardingApp();
			app.setAppBasicAuthPassword(EPCommonSystemProperties.APP_DISPLAY_PASSWORD);//to hide password from get request
			createOnboardingFromApp(app, onboardingApp);
			onboardingAppsList.add(onboardingApp);
		}
		return onboardingAppsList;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<OnboardingApp> getAdminAppsOfUser(EPUser user) {
		
		List<OnboardingApp> onboardingAppsList = new ArrayList<OnboardingApp>();
		List<Integer> userAdminApps = new ArrayList<>();
		final Map<String, Long> userParams = new HashMap<>();
		userParams.put("userId", user.getId());	
		userAdminApps =  dataAccessService.executeNamedQuery("getAllAdminAppsofTheUser", userParams, null);
		
//		userAdminApps.removeIf(x -> x == Integer.valueOf(ECOMP_APP_ID));
		
		logger.debug(EELFLoggerDelegate.debugLogger, "Is account admin for userAdminApps() - for user {}, found userAdminAppsSize {}", user.getOrgUserId(), userAdminApps.size());
		onboardingAppsList = getOnboardingApps();
		
		final List<Integer> userAdminApps1 = userAdminApps;
		List<OnboardingApp> userApplicationAdmins = onboardingAppsList.stream().filter(x -> userAdminApps1.contains((int) (long)x.getId())).collect(Collectors.toList());
		
        return userApplicationAdmins;
	}

	@Override
	public List<OnboardingApp> getEnabledNonOpenOnboardingApps() {
		@SuppressWarnings("unchecked")
		List<EPApp> apps = dataAccessService.getList(EPApp.class,
				" where enabled = true and open = false and app_type!= 3 and id!=" + ECOMP_APP_ID, null, null);
	
		List<OnboardingApp> onboardingAppsList = new ArrayList<OnboardingApp>();
		for (EPApp app : apps) {
			OnboardingApp onboardingApp = new OnboardingApp();
			createOnboardingFromApp(app, onboardingApp);
			onboardingAppsList.add(onboardingApp);
		}
		return onboardingAppsList;
	}

	@SuppressWarnings("unchecked")
	private void validateOnboardingApp(OnboardingApp onboardingApp, FieldsValidator fieldsValidator) {
		boolean duplicatedNameSpace = false;
		boolean duplicatedName = false;
		List<EPApp> apps;
		if (onboardingApp.getId() == null) {
			List<Criterion> restrictionsList = new ArrayList<Criterion>();
			Criterion nameCrit = Restrictions.eq("name",onboardingApp.getAppName());
			Criterion nameSpaceCrit = null;
			Criterion	orCrit = null;
			if (onboardingApp.getRolesInAAF()) {
				nameSpaceCrit = Restrictions.eq("nameSpace", onboardingApp.getNameSpace());
				orCrit = Restrictions.or(nameCrit, nameSpaceCrit);
			} else
				orCrit = Restrictions.or(nameCrit);
			restrictionsList.add(orCrit);
			apps = (List<EPApp>) dataAccessService.getList(EPApp.class, null, restrictionsList, null);
		} else {
			List<Criterion> restrictionsList = new ArrayList<Criterion>();
			Criterion idCrit =Restrictions.eq("id", onboardingApp.getId());
			Criterion nameCrit = Restrictions.eq("name",onboardingApp.getAppName());
			Criterion nameSpaceCrit = null;
			Criterion orCrit= null;
			if (onboardingApp.getRolesInAAF()) {
				nameSpaceCrit = Restrictions.eq("nameSpace",onboardingApp.getNameSpace());
				orCrit = Restrictions.or(idCrit, nameSpaceCrit, nameCrit);
			}
			else
			 orCrit = Restrictions.or(idCrit, nameCrit);
			
			restrictionsList.add(orCrit);
			apps = (List<EPApp>) dataAccessService.getList(EPApp.class, null, restrictionsList, null);
			
		}
		for (EPApp app : apps) {
			if (onboardingApp.getId() != null && onboardingApp.getId().equals(app.getId())) {
				continue;
			}
			if (!duplicatedName && app.getName().equalsIgnoreCase(onboardingApp.getAppName())) {
				duplicatedName = true;
				if (duplicatedName) {
					break;
				}
			}
			if (!duplicatedNameSpace && app.getNameSpace().equalsIgnoreCase(onboardingApp.getNameSpace())) {
				duplicatedNameSpace = true;
				if (duplicatedNameSpace) {
					break;
				}
			}
			
		}
		if (duplicatedNameSpace || duplicatedName) {
			if (duplicatedNameSpace) {
				fieldsValidator.addProblematicFieldName(nameSpaceField);
			}
			if (duplicatedName) {
				fieldsValidator.addProblematicFieldName(nameField);
			}
			fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_CONFLICT);
			fieldsValidator.errorCode = new Long(EPCommonSystemProperties.DUBLICATED_FIELD_VALUE_ECOMP_ERROR);
		}
	}

	@Override
	public FieldsValidator modifyOnboardingApp(OnboardingApp modifiedOnboardingApp, EPUser user) {
		logger.debug(EELFLoggerDelegate.debugLogger, "LR: entering modifyOnboardingApp");
		FieldsValidator fieldsValidator = onboardingAppFieldsChecker(modifiedOnboardingApp);
		if (fieldsValidator.httpStatusCode.intValue() == HttpServletResponse.SC_OK) {
			validateOnboardingApp(modifiedOnboardingApp, fieldsValidator);
		}
		if (fieldsValidator.httpStatusCode.intValue() == HttpServletResponse.SC_OK) {
			if (modifiedOnboardingApp.getId() != null) {
				updateApp(modifiedOnboardingApp.getId(), modifiedOnboardingApp, fieldsValidator, user);
				logger.info(EELFLoggerDelegate.auditLogger, "Updated " + modifiedOnboardingApp.getAppName() + 
						" onboarding application details by user " + user.getLoginId());
			} else {
				fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_BAD_REQUEST);
			}
		}
		return fieldsValidator;
	}

	@Override
	public FieldsValidator addOnboardingApp(OnboardingApp newOnboardingApp, EPUser user) {
		FieldsValidator fieldsValidator = onboardingAppFieldsChecker(newOnboardingApp);
		if (fieldsValidator.httpStatusCode.intValue() == HttpServletResponse.SC_OK) {
			validateOnboardingApp(newOnboardingApp, fieldsValidator);
		}
		if (fieldsValidator.httpStatusCode.intValue() == HttpServletResponse.SC_OK) {
			if (newOnboardingApp.getId() == null) {
				updateApp(null, newOnboardingApp, fieldsValidator, user);
				logger.info(EELFLoggerDelegate.auditLogger, "Added " + newOnboardingApp.getAppName() + 
						" Onboarding application by user " + user.getLoginId());
			} else {
				fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_BAD_REQUEST);
			}
		}
		return fieldsValidator;
	}

	@SuppressWarnings("unchecked")
	@Override
	public FieldsValidator deleteOnboardingApp(EPUser user, Long appid) {
		FieldsValidator fieldsValidator = new FieldsValidator();
		if (!adminRolesService.isSuperAdmin(user)) {
			fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_FORBIDDEN);
			return fieldsValidator;
		}
		final Map<String, Long> params = new HashMap<>();
		params.put("app_id", appid);
		
		//Checking if App is associated with any exiting microservices- ep_microservice:
		final Map<String, Long> queryparams = new HashMap<>();
		queryparams.put("applicationId", appid);
		List<MicroserviceData> microservicesList  = dataAccessService.executeNamedQuery( "getMicroservicesByAppId", queryparams, null);
		if(microservicesList!=null && microservicesList.size()>0) {
			fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
			return fieldsValidator;
		}
		
		List<EPUserAppRolesRequest> EPUserAppRolesRequestList= new ArrayList<>();
		EPUserAppRolesRequestList = dataAccessService.executeNamedQuery( "getRequestIdsForApp", params, null);
	    for(int i=0;i<EPUserAppRolesRequestList.size();i++)
	    {
	     dataAccessService.deleteDomainObjects(EPUserAppRolesRequestDetail.class , "req_id=" + EPUserAppRolesRequestList.get(i).getId(),null);
	    	
	    }
	    Session localSession = null;
		Transaction transaction = null;
		Boolean result = false;
		try {
			localSession = sessionFactory.openSession();
			transaction = localSession.beginTransaction();
			
			// 1) Remove the URL for any functional menu item associated with
			// this app
			logger.debug(EELFLoggerDelegate.debugLogger, "deleteOnboardingApp: Deleting URL for any functional menu item associated with app");
			// Named query is stored in EP.hbm.xml, mapped to EPApp
			 dataAccessService.executeNamedQuery("updateMenuFunctionalAndRoles", params, null);
			logger.debug(EELFLoggerDelegate.debugLogger, "deleteOnboardingApp:  Deleted URL for any functional menu item associated with app");

			logger.debug(EELFLoggerDelegate.debugLogger, "deleteOnboardingApp: Deleting favorites associated with a menu item that is associated with this app");
			// 2)Remove any favorites associated with a menu item that is
			// associated with this app
			dataAccessService.executeNamedQuery("removeAppFromMenuFavorites", params, null);
			logger.debug(EELFLoggerDelegate.debugLogger, "deleteOnboardingApp: Deleted favorites associated with a menu item that is associated with this app");

			logger.debug(EELFLoggerDelegate.debugLogger, "deleteOnboardingApp: Deleting role, appid records from fn_menu_functional_role that are associated with this app");
			// 3)Remove all role, appid records from fn_menu_functional_role
			// that are associated with this app
			 dataAccessService.executeNamedQuery("removeAppFromMenuFunctionalRoles", params, null);
			logger.debug(EELFLoggerDelegate.debugLogger, "deleteOnboardingApp: Deleted role, appid records from fn_menu_functional_role that are associated with this app");

			logger.debug(EELFLoggerDelegate.debugLogger, "deleteOnboardingApp: Deleting all roles, rolefunctions, appid records from ep_app_role_function that are associated with this app");
			// 4)Remove all roles, rolefunctions, appid records from ep_app_role_function
			// that are associated with this app
			 dataAccessService.executeNamedQuery("removeAppFromEpAppRoleFunction", params, null);
			 logger.debug(EELFLoggerDelegate.debugLogger, "deleteOnboardingApp: Deleted all roles, rolefunctions, appid records from ep_app_role_function that are associated with this app");
			 
			 logger.debug(EELFLoggerDelegate.debugLogger, "deleteOnboardingApp: Deleting all rolefunctions, appid records from ep_app_function that are associated with this app");
			// 5)Remove all rolefunctions, appid records from ep_app_function
			// that are associated with this app
			 dataAccessService.executeNamedQuery("removeAppFromEpAppFunction", params, null);
			 logger.debug(EELFLoggerDelegate.debugLogger, "deleteOnboardingApp:  Deleted all rolefunctions, appid records from ep_app_function that are associated with this app");
	 
			 logger.debug(EELFLoggerDelegate.debugLogger, "deleteOnboardingApp: Deleting all records from fn_user_role associated with this app");
			// 6)Remove all records from fn_user_role associated with this app
			 dataAccessService.executeNamedQuery("removeAppFromFnUserRole", params, null);
			 logger.debug(EELFLoggerDelegate.debugLogger, "deleteOnboardingApp: Deleted all records from fn_user_role associated with this app");
			 
			 logger.debug(EELFLoggerDelegate.debugLogger, "deleteOnboardingApp: Deleting any widgets associated with this app");
			// 7)Remove any widgets associated with this app
			 dataAccessService.executeNamedQuery("removeAppFromEpWidgetCatalogRole", params, null);
			 logger.debug(EELFLoggerDelegate.debugLogger, "deleteOnboardingApp: Deleted widgets associated with this app");
			 
			 logger.debug(EELFLoggerDelegate.debugLogger, "deleteOnboardingApp: Deleting any roles associated with this app");
			// 8)Remove any roles associated with this app
			 dataAccessService.executeNamedQuery("removeAppFromEpRoleNotification", params, null);
			 logger.debug(EELFLoggerDelegate.debugLogger, "deleteOnboardingApp: Deleted roles associated with this app");
			 
			 logger.debug(EELFLoggerDelegate.debugLogger, "deleteOnboardingApp: Deleting all records from fn_role associated with this app");
			// 9)Remove all records from fn_role associated with this app
			 dataAccessService.executeNamedQuery("removeAppFromFnRole", params, null);
			 logger.debug(EELFLoggerDelegate.debugLogger, "deleteOnboardingApp: Deleted all records from fn_role associated with this app");
			
			 logger.debug(EELFLoggerDelegate.debugLogger, "deleteOnboardingApp: Deleting in table fn_app_contact_us entries associated with this app");
			 // 10)Remove app contact us entries
			 dataAccessService.executeNamedQuery("removeAppFromAppContactUs", params, null);
			 logger.debug(EELFLoggerDelegate.debugLogger, "deleteOnboardingApp: Deleted in table fn_app_contact_us entries associated with this app");

			 logger.debug(EELFLoggerDelegate.debugLogger, "deleteOnboardingApp: Deleting rows in the fn_pers_user_app_sel table");
			// 11)Remove rows in the app personalization selection table
			 dataAccessService.executeNamedQuery("removeAppFromEpPersUserAppSel", params, null);
			 logger.debug(EELFLoggerDelegate.debugLogger, "deleteOnboardingApp: Deleted rows in the fn_pers_user_app_sel table");
			 
			 logger.debug(EELFLoggerDelegate.debugLogger, "deleteOnboardingApp: Deleting rows in the ep_pers_user_app_man_sort table");
			// 12)Remove rows in the app personalization sort table
			 dataAccessService.executeNamedQuery("removeAppFromEpPersUserAppManSort", params, null);
			 logger.debug(EELFLoggerDelegate.debugLogger, "deleteOnboardingApp: Deleted rows in the ep_pers_user_app_man_sort table");
			 
			 logger.debug(EELFLoggerDelegate.debugLogger, "deleteOnboardingApp: Deleting rows in the ep_user_roles_request table");
			// 13)Remove rows in the app personalization sort table
			 dataAccessService.executeNamedQuery("removeAppFromEpUserRolesRequest", params, null);
			 logger.debug(EELFLoggerDelegate.debugLogger, "deleteOnboardingApp: Deleted rows in the ep_user_roles_request table");
			 
			 logger.debug(EELFLoggerDelegate.debugLogger, "deleteOnboardingApp: Deleting rows in the ep_web_analytics_source");
			// 14)Remove rows in the ep_web_analytics_source
			 dataAccessService.executeNamedQuery("removeAppFromEpWebAnalytics", params, null);
			 logger.debug(EELFLoggerDelegate.debugLogger, "deleteOnboardingApp: Deleted rows in the ep_web_analytics_source");
			 
			 logger.debug(EELFLoggerDelegate.debugLogger, "deleteOnboardingApp: Deleting the app ");
			// 15)Delete the app
			 dataAccessService.executeNamedQuery("removeAppFromFnApp", params, null);
			 logger.debug(EELFLoggerDelegate.debugLogger, "deleteOnboardingApp: Deleted the app");
			
			/*
			// 1) Remove the URL for any functional menu item associated with
			// this app
			String sql = "UPDATE fn_menu_functional m, fn_menu_functional_roles mr SET m.url='' "
					+ " WHERE m.menu_id=mr.menu_id " + " AND mr.app_id='" + appid + "'";
			logQuery(sql);
			Query query = localSession.createSQLQuery(sql);
			query.executeUpdate();

			// Remove any favorites associated with a menu item that is
			// associated with this app
			sql = "Delete from fn_menu_favorites " + " using fn_menu_favorites inner join fn_menu_functional_roles "
					+ " where fn_menu_functional_roles.app_id='" + appid + "' "
					+ " AND fn_menu_functional_roles.menu_id=fn_menu_favorites.menu_id";
			logQuery(sql);
			query = localSession.createSQLQuery(sql);
			query.executeUpdate();

			// Remove all role, appid records from fn_menu_functional_role
			// that are associated with this app
			sql = "delete from fn_menu_functional_roles where app_id='" + appid + "'";
			logQuery(sql);
			query = localSession.createSQLQuery(sql);
			query.executeUpdate();
			
			
			// Remove all roles, rolefunctions, appid records from ep_app_role_function
			// that are associated with this app
		    sql = "DELETE FROM ep_app_role_function WHERE app_id='" + appid + "'";
			logger.debug(EELFLoggerDelegate.debugLogger, "Executing query: " + sql);
			 query = localSession.createSQLQuery(sql);
			query.executeUpdate();
			
			//Remove all rolefunctions, appid records from ep_app_function
			// that are associated with this app
			sql = "DELETE FROM ep_app_function WHERE app_id='" + appid + "'";
			logger.debug(EELFLoggerDelegate.debugLogger, "Executing query: " + sql);
			query = localSession.createSQLQuery(sql);
			query.executeUpdate();

			// Remove all records from fn_user_role associated with this app
			sql = "delete from fn_user_role where app_id='" + appid + "'";
			logQuery(sql);
			query = localSession.createSQLQuery(sql);
			query.executeUpdate();

			// Remove any widgets associated with this app
			sql = "delete from ep_widget_catalog_role where app_id='" + appid + "'";
			logQuery(sql);
			query = localSession.createSQLQuery(sql);
			query.executeUpdate();

			// Remove any roles associated with this app
			sql = "delete from ep_role_notification " + " using ep_role_notification inner join fn_role "
					+ " where fn_role.app_id='" + appid + "' " + " and ep_role_notification.role_id= fn_role.role_id";
			logQuery(sql);
			query = localSession.createSQLQuery(sql);
			query.executeUpdate();

			// Remove all records from fn_role associated with this app
			sql = "delete from fn_role where app_id='" + appid + "'";
			logQuery(sql);
			query = localSession.createSQLQuery(sql);
			query.executeUpdate();

			// Remove app contact us entries
			sql = "delete from fn_app_contact_us where app_id='" + appid + "'";
			logQuery(sql);
			query = localSession.createSQLQuery(sql);
			query.executeUpdate();

			// Remove rows in the app personalization selection table
			sql = "delete from fn_pers_user_app_sel where app_id='" + appid + "'";
			logQuery(sql);
			query = localSession.createSQLQuery(sql);
			query.executeUpdate();

			// Remove rows in the app personalization sort table
			sql = "delete from ep_pers_user_app_man_sort where app_id='" + appid + "'";
			logQuery(sql);
			query = localSession.createSQLQuery(sql);
			query.executeUpdate();

			// Remove rows in the app personalization sort table
			sql = "delete from ep_user_roles_request where app_id='" + appid + "'";
			logQuery(sql);
			query = localSession.createSQLQuery(sql);
			query.executeUpdate();

			// Remove rows in the app personalization sort table
			sql = "delete from ep_web_analytics_source where app_id='" + appid + "'";
			logQuery(sql);
			query = localSession.createSQLQuery(sql);
			query.executeUpdate();

			// Delete the app
			sql = "delete from fn_app where app_id='" + appid + "'";
			logQuery(sql);
			query = localSession.createSQLQuery(sql);
			query.executeUpdate();

			transaction.commit();
			*/
			result = true;
			logger.debug(EELFLoggerDelegate.debugLogger, "deleteOnboardingApp success");
		} catch (Exception e) {
			logger.error(EELFLoggerDelegate.errorLogger, "deleteOnboardingApp failed", e);
			EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError);
			EcompPortalUtils.rollbackTransaction(transaction, "deleteOnboardingApp rollback, exception = " + e);
		} finally {
			EcompPortalUtils.closeLocalSession(localSession, "deleteOnboardingApp");
		}
		if (!result) {
			fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
		}
		return fieldsValidator;
	}

	private static Object syncRests = new Object();

	// An app has been enabled/disabled. Must enable/disable all associated
	// functional menu items.
	protected void setFunctionalMenuItemsEnabled(Session localSession, Boolean enabled, Long appId) {
		String active_yn = enabled ? "Y" : "N";
		String 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 r.app_id = '" + appId + "' ";
		logQuery(sql);
		@SuppressWarnings("unchecked")
		List<FunctionalMenuItem> menuItems = dataAccessService.executeSQLQuery(sql, FunctionalMenuItem.class, null);
		for (FunctionalMenuItem menuItem : menuItems) {
			FunctionalMenuItem myMenuItem = (FunctionalMenuItem) localSession.get(FunctionalMenuItem.class,
					menuItem.menuId);
			myMenuItem.active_yn = active_yn;
			localSession.save(myMenuItem);
		}
	}

	// Attention! If (appId == null) we use this function to create application
	// otherwise we use it to modify existing application
	protected void updateApp(Long appId, OnboardingApp onboardingApp, FieldsValidator fieldsValidator, EPUser user) {
		logger.debug(EELFLoggerDelegate.debugLogger, "LR: entering updateApp");
		// Separate out the code for a restricted app, since it doesn't need any
		// of the UEB code.
		if (Boolean.TRUE.equals(onboardingApp.getRestrictedApp())) {
			boolean result = false;
			Session localSession = null;
			Transaction transaction = null;
			try {
				localSession = sessionFactory.openSession();
				transaction = localSession.beginTransaction();
				EPApp app;
				if (appId == null) {
					app = new EPApp();
				} else {
					app = (EPApp) localSession.get(EPApp.class, appId);
					if (app == null || app.getId() == null) { // App is already
						// deleted!
						transaction.commit();
						localSession.close();
						fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_NOT_FOUND);
						return;
					}
				}
				createAppFromOnboarding(app, onboardingApp, localSession);
				localSession.saveOrUpdate(app);
				// Enable or disable all menu items associated with this app
				setFunctionalMenuItemsEnabled(localSession, onboardingApp.getIsEnabled(), appId);
				transaction.commit();
				result = true;
			} catch (Exception e) {
				logger.error(EELFLoggerDelegate.errorLogger, "updateApp failed", e);
				EcompPortalUtils.rollbackTransaction(transaction,
						"updateApp rollback, exception = " + e.toString());
			} finally {
				EcompPortalUtils.closeLocalSession(localSession, "updateApp");
			}
			if (!result) {
				fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			}

		} else {
			updateRestrictedApp(appId, onboardingApp, fieldsValidator, user);
			
		}
	}
	

	protected void updateRestrictedApp(Long appId, OnboardingApp onboardingApp, FieldsValidator fieldsValidator,
			EPUser user) {
		synchronized (syncRests) {
			boolean result = false;
			Session localSession = null;
			Transaction transaction = null;
			try {
				localSession = sessionFactory.openSession();
				transaction = localSession.beginTransaction();
				EPApp app;
				if (appId == null) {
					app = new EPApp();
					/*
					 * In the parent class, the UEB code is responsible for generating the
					 * keys/secret/mailbox but UEB Messaging is not actually being used currently;
					 * may be used in future at which point we can just remove this method and
					 * depend on parent class's method So, using UUID generator to generate the
					 * unique key instead.
					 */
					String uuidStr = UUID.randomUUID().toString();
					String appKey = uuidStr;
					String appSecret = uuidStr;
					String appMailboxName = "ECOMP-PORTAL-OUTBOX";
					onboardingApp.setUebTopicName(appMailboxName);
					onboardingApp.setUebKey(appKey);
					onboardingApp.setUebSecret(appSecret);
				} else {
					app = (EPApp) localSession.get(EPApp.class, appId);
					if (app == null || app.getId() == null) {
						// App is already deleted!
						transaction.commit();
						localSession.close();
						fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_NOT_FOUND);
						return;
					}
				}
				logger.debug(EELFLoggerDelegate.debugLogger,
						"updateRestrictedApp: about to call createAppFromOnboarding");
				createAppFromOnboarding(app, onboardingApp, localSession);
				logger.debug(EELFLoggerDelegate.debugLogger,
						"updateRestrictedApp: finished calling createAppFromOnboarding");
				localSession.saveOrUpdate(app);
				logger.debug(EELFLoggerDelegate.debugLogger,
						"updateRestrictedApp: finished calling localSession.saveOrUpdate");
				// Enable or disable all menu items associated with this app
				setFunctionalMenuItemsEnabled(localSession, onboardingApp.getIsEnabled(), appId);
				logger.debug(EELFLoggerDelegate.debugLogger,
						"updateRestrictedApp: finished calling setFunctionalMenuItemsEnabled");
				transaction.commit();
				logger.debug(EELFLoggerDelegate.debugLogger,
						"updateRestrictedApp: finished calling transaction.commit");
				result = true;
			} catch (Exception e) {
				logger.error(EELFLoggerDelegate.errorLogger, "updateRestrictedApp failed", e);
				EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeUebRegisterOnboardingAppError, e);
				EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError, e);
				EcompPortalUtils.rollbackTransaction(transaction,
						"updateRestrictedApp rollback, exception = " + e.toString());
			} finally {
				EcompPortalUtils.closeLocalSession(localSession, "updateRestrictedApp");
			}
			if (!result) {
				fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			}
		}

	}

	@Deprecated
	protected void updateRestrictedAppUeb(Long appId, OnboardingApp onboardingApp, FieldsValidator fieldsValidator,
			EPUser user) {
		synchronized (syncRests) {
			boolean result = false;
			Session localSession = null;
			Transaction transaction = null;
			try {
				localSession = sessionFactory.openSession();
				transaction = localSession.beginTransaction();
				EPApp app;
				if (appId == null) {
					app = new EPApp();
					// -------------------------------------------------------------------------------------------
					// Register this App with the UEB communication server.
					// Save
					// the App's unique mailbox/topic
					// name and keys to the FN_APP table. The App's mailbox
					// and
					// keys will be visible to the
					// admin on the ONAP portal.
					// -------------------------------------------------------------------------------------------
					TopicManager topicManager = new TopicManager() {

						EPAppCommonServiceImpl service;

						public void init(EPAppCommonServiceImpl _service) {
							service = _service;
						}

						public void createTopic(String key, String secret, String topicName,
								String topicDescription) throws HttpException, CambriaApiException, IOException {

							init(EPAppCommonServiceImpl.this);
							final LinkedList<String> urlList = (LinkedList<String>) Helper.uebUrlList();
							if (logger.isInfoEnabled()) {
								logger.info("==> createTopic");
								logger.info("topicName: " + topicName);
								logger.info("topicDescription: " + topicDescription);
							}
							CambriaTopicManager tm = null;
							try {
								tm = service.getTopicManager(urlList, key, secret);
							} catch (Exception e) {
								logger.error("pub.build Exception ", e);
								throw new CambriaApiException(topicName);
							}
							tm.createTopic(topicName, topicDescription, 1, 1);
						}

						public void addPublisher(String topicOwnerKey, String topicOwnerSecret, String publisherKey,
								String topicName) throws HttpException, CambriaApiException, IOException {
							logger.info("==> addPublisher to topic " + topicName);
							final LinkedList<String> urlList = (LinkedList<String>) Helper.uebUrlList();
							CambriaTopicManager tm = null;
							try {
								tm = service.getTopicManager(urlList, topicOwnerKey, topicOwnerSecret);
							} catch (Exception e) {
								logger.error("pub.build Exception ", e);
								throw new CambriaApiException(topicName);
							}
							tm.allowProducer(topicName, publisherKey);
						}

					};
					final CambriaIdentityManager im = new CambriaClientBuilders.IdentityManagerBuilder()
							.usingHosts(Helper.uebUrlList()).build();
					com.att.nsa.apiClient.credentials.ApiCredential credential = im.createApiKey(user.getEmail(),
							"ONAP Portal Owner");
					String appKey = credential.getApiKey();
					String appSecret = credential.getApiSecret();
					String appMailboxName = null;

					int maxNumAttemptsToCreateATopic = 3;
					boolean successfullyCreatedMailbox = false;
					for (int i = 0; i < maxNumAttemptsToCreateATopic; i++) {
						appMailboxName = "ECOMP-PORTAL-OUTBOX-" + (int) (Math.random() * 100000.0);

						try {
							topicManager.createTopic(
									PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY),
									PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_SECRET),
									appMailboxName, "ECOMP outbox for app" + onboardingApp.getAppName());
							successfullyCreatedMailbox = true;
							logger.debug(EELFLoggerDelegate.debugLogger,
									"Successfully created " + appMailboxName + " for App " + onboardingApp.getAppName());
							logger.debug(EELFLoggerDelegate.debugLogger, "    Key = " + appKey + " Secret = "
									+ appSecret + " generated using = " + user.getEmail());
							break;
						} catch (HttpException e) {
							EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeUebConnectionError, e);
							if (e.getStatusCode() == 409) {
								logger.error(EELFLoggerDelegate.errorLogger, "Topic/mailbox " + appMailboxName
										+ " already exists. Will try using a different name", e);
							} else {
								logger.error(EELFLoggerDelegate.errorLogger, "HttpException when onboarding App: ",
										e);
							}
						}
					}

					if (successfullyCreatedMailbox) {
						onboardingApp.setUebTopicName(appMailboxName);
						onboardingApp.setUebKey(appKey);
						onboardingApp.setUebSecret(appSecret);

						try {
							/*
							 * EP is a publisher to this App's new mailbox
							 */
							topicManager.addPublisher(
									PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY),
									PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_SECRET),
									PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY),
									appMailboxName);

							/*
							 * This App is a subscriber of its own mailbox
							 */
							topicManager.addSubscriber(
									PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY),
									PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_SECRET), appKey,
									appMailboxName);

							/*
							 * This App is a publisher to EP
							 */
							topicManager.addPublisher(
									PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY),
									PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_SECRET), appKey,
									PortalApiProperties.getProperty(PortalApiConstants.ECOMP_PORTAL_INBOX_NAME));
						} catch (HttpException | CambriaApiException | IOException e) {
							EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeUebRegisterOnboardingAppError, e);
							logger.error(EELFLoggerDelegate.errorLogger,
									"Error when configuring Publisher/Subscriber for App's new mailbox", e);
							transaction.commit();
							localSession.close();
							fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_CONFLICT);
							return;
						}
					} else {
						transaction.commit();
						localSession.close();
						fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_CONFLICT);
						return;
					}
				} else {
					app = (EPApp) localSession.get(EPApp.class, appId);
					if (app == null || app.getId() == null) {
						// App is already deleted!
						transaction.commit();
						localSession.close();
						fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_NOT_FOUND);
						return;
					}
				}
				logger.debug(EELFLoggerDelegate.debugLogger, "LR: about to call createAppFromOnboarding");
				createAppFromOnboarding(app, onboardingApp, localSession);
				logger.debug(EELFLoggerDelegate.debugLogger,
						"LR: updateApp: finished calling createAppFromOnboarding");
				localSession.saveOrUpdate(app);
				logger.debug(EELFLoggerDelegate.debugLogger,
						"LR: updateApp: finished calling localSession.saveOrUpdate");
				// Enable or disable all menu items associated with this app
				setFunctionalMenuItemsEnabled(localSession, onboardingApp.getIsEnabled(), appId);
				logger.debug(EELFLoggerDelegate.debugLogger,
						"LR: updateApp: finished calling setFunctionalMenuItemsEnabled");
				transaction.commit();
				logger.debug(EELFLoggerDelegate.debugLogger, "LR: updateApp: finished calling transaction.commit");
				logger.debug(EELFLoggerDelegate.debugLogger,
						"LR: updateApp: finished calling epUebHelper.addPublisher");
				result = true;
			} catch (Exception e) {
				logger.error(EELFLoggerDelegate.errorLogger, "updateApp failed", e);
				EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeUebRegisterOnboardingAppError, e);
				EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeDaoSystemError, e);
				EcompPortalUtils.rollbackTransaction(transaction,
						"updateApp rollback, exception = " + e.toString());
			} finally {
				EcompPortalUtils.closeLocalSession(localSession, "updateApp");
			}
			if (!result) {
				fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			}
		}

	}

	public CambriaTopicManager getTopicManager(List<String> urlList, String key, String secret)
			throws GeneralSecurityException, Exception {
		throw new Exception("This method can only be invoked from child class");
	}

	/**
	 * Populates a transport model of the application from a database row model.
	 * Leaves out the thumbnail because the FE fetches images via a different
	 * API.
	 * 
	 * @param app
	 *            Model of database row
	 * @param onboardingApp
	 *            Model for transport as JSON
	 */
	@Override
	public void createOnboardingFromApp(EPApp app, OnboardingApp onboardingApp) {
		onboardingApp.setId(app.getId());
		onboardingApp.setAppName(app.getName());
		onboardingApp.setImageUrl(app.getImageUrl());
		onboardingApp.setAppDescription(app.getAppDescription());
		onboardingApp.setAppNotes(app.getAppNotes());
		onboardingApp.setLandingPage(app.getLandingPage());
		onboardingApp.setAlternateLandingPage(app.getAlternateLandingPage());
		onboardingApp.setRestUrl(app.getAppRestEndpoint());
		onboardingApp.setIsOpen(app.getOpen());
		onboardingApp.setIsEnabled(app.getEnabled());
		onboardingApp.setAppBasicAuthUsername(app.getAppBasicAuthUsername());
		
		String effectivePwd = null;
		if (app.getAppBasicAuthPassword().equals(EPCommonSystemProperties.APP_DISPLAY_PASSWORD))
			effectivePwd = EPCommonSystemProperties.APP_DISPLAY_PASSWORD;
		else
			effectivePwd = decryptedPassword(app.getAppBasicAuthPassword(), app);
		
		onboardingApp.setAppBasicAuthPassword(effectivePwd);
		onboardingApp.setUebTopicName(app.getUebTopicName());
		onboardingApp.setUebKey(app.getUebKey());
		onboardingApp.setUebSecret(app.getUebSecret());
		onboardingApp.setRolesInAAF(app.getRolesInAAF());
		onboardingApp.setNameSpace(app.getNameSpace());
		onboardingApp.setRestrictedApp(app.isRestrictedApp());
		onboardingApp.setModeOfIntegration(app.getModeOfIntegration());
		onboardingApp.setAppAck(app.getAppAck());
		onboardingApp.setUsesCadi(app.getUsesCadi());
		onboardingApp.setApplicationType(app.getAppType().toString());
	}

	/**
	 * Creates a database object for an application from an uploaded transport
	 * model. Must decode the thumbnail, if any.
	 * 
	 * @param app
	 * @param onboardingApp
	 * @param localSession
	 * @return The first argument.
	 */
	protected EPApp createAppFromOnboarding(EPApp app, OnboardingApp onboardingApp, Session localSession) {
		app.setName(onboardingApp.getAppName());
		app.setAppDescription(onboardingApp.getAppDescription());
		app.setAppNotes(onboardingApp.getAppNotes());
		app.setLandingPage(onboardingApp.getLandingPage());
		app.setAlternateLandingPage(onboardingApp.getAlternateLandingPage());
		app.setAppRestEndpoint(onboardingApp.getRestUrl());
		app.setOpen(onboardingApp.getIsOpen());
		app.setEnabled(onboardingApp.getIsEnabled());
		app.setAppBasicAuthUsername(onboardingApp.getAppBasicAuthUsername());
		if(!onboardingApp.getAppBasicAuthPassword().equals(EPCommonSystemProperties.APP_DISPLAY_PASSWORD))
		app.setAppBasicAuthPassword(this.encryptedPassword(onboardingApp.getAppBasicAuthPassword(), app));
		//app.setUebTopicName(onboardingApp.uebTopicName);
		app.setUebKey(onboardingApp.getUebKey());
		app.setUebSecret(onboardingApp.getUebSecret());
		app.setRolesInAAF(onboardingApp.getRolesInAAF());
		app.setNameSpace(onboardingApp.getNameSpace());
		app.setAppType(new Integer(onboardingApp.getApplicationType()));		
		app.setModeOfIntegration(onboardingApp.getModeOfIntegration());
		app.setAppAck(onboardingApp.getAppAck());
		app.setUsesCadi(onboardingApp.getUsesCadi());
		
		
		if (!StringUtils.isEmpty(onboardingApp.getThumbnail())) {
			logger.debug(EELFLoggerDelegate.debugLogger, "createAppFromOnboarding: onboarding thumbnail is NOT empty");
			String[] splitBase64Thumbnail = onboardingApp.getThumbnail().split("base64,");
			logger.debug(EELFLoggerDelegate.debugLogger,
					"createAppFromOnboarding: length of splitBase64Thumbnail: " + splitBase64Thumbnail.length);
			if (splitBase64Thumbnail.length > 1) {
				// This occurs when we have a new image, not an existing image
				byte[] decodedImage = Base64.getDecoder().decode(splitBase64Thumbnail[1].getBytes());
				logger.debug(EELFLoggerDelegate.debugLogger, "createAppFromOnboarding: finished calling decode");
				// This is basically a boolean indicator that an image is
				// present.
				app.setImageUrl(constructImageName(onboardingApp));
				app.setThumbnail(decodedImage);
			}
		} else if (app.getThumbnail() != null && onboardingApp.getImageLink() == null) {
			// The thumbnail that came in from the json is empty; the previous
			// thumbnail is NOT empty. Must delete it.
			logger.debug(EELFLoggerDelegate.debugLogger,
					"createAppFromOnboarding: onboarding thumbnail is empty; db thumbnail is NOT null");
			app.setImageUrl(null);
			app.setThumbnail(null);
		} else {
			logger.debug(EELFLoggerDelegate.debugLogger,
					"createAppFromOnboarding: making no changes to thumbnail as imageLink is not null");
		}
		return app;
	}

	protected String constructImageName(OnboardingApp onboardingApp) {
		String appLandingPageURL = onboardingApp.getLandingPage();
		SecureRandom rand = new SecureRandom();
		if(appLandingPageURL == null) {
			appLandingPageURL = "";
		}
		 return "portal_" + String.valueOf(appLandingPageURL.hashCode() + "_" +  rand.nextInt(100000))
         + ".png";
	}

	// Don't encrypt or decrypt the password if it is null or the empty string
	private String decryptedPassword(String encryptedAppPwd, EPApp app) {
		String result = "";
		if (encryptedAppPwd != null && !encryptedAppPwd.isEmpty()) {
			try {
				result = CipherUtil.decryptPKC(encryptedAppPwd,
						KeyProperties.getProperty(KeyConstants.CIPHER_ENCRYPTION_KEY));
			} catch (Exception e) {
				logger.error(EELFLoggerDelegate.errorLogger, "decryptedPassword failed for app " + app.getName(), e);
			}
		}
		return result;
	}

	protected String encryptedPassword(String decryptedAppPwd, EPApp app) {
		String result = "";
		if (decryptedAppPwd != null && !decryptedAppPwd.isEmpty()) {
			try {
				result = CipherUtil.encryptPKC(decryptedAppPwd,
						KeyProperties.getProperty(KeyConstants.CIPHER_ENCRYPTION_KEY));
			} catch (Exception e) {
				logger.error(EELFLoggerDelegate.errorLogger, "encryptedPassword failed for app " + app.getName(), e);
			}
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public FieldsValidator saveWidgetsSortManual(List<EPWidgetsSortPreference> widgetsSortManual, EPUser user) {
		FieldsValidator fieldsValidator = new FieldsValidator();
		final Map<String, Long> params = new HashMap<>();
		List<EPWidgetsManualSortPreference> epManualWidgets = new ArrayList<EPWidgetsManualSortPreference>();

		try {
			params.put("userId", user.getId());
			epManualWidgets = dataAccessService.executeNamedQuery("userWidgetManualSortPrfQuery", params, null);
			Map<Long, EPWidgetsManualSortPreference> existingWidgetsIds = new HashMap<Long, EPWidgetsManualSortPreference>();
			for (EPWidgetsManualSortPreference userWidgetManualPref : epManualWidgets) {
				existingWidgetsIds.put(userWidgetManualPref.getWidgetId(), userWidgetManualPref);
			}
			for (EPWidgetsSortPreference epWidgetsManPref : widgetsSortManual) {
				if (epWidgetsManPref.getWidgetid() != null) {
					Long widgetid = epWidgetsManPref.getWidgetid();
					if (existingWidgetsIds.containsKey(widgetid)) {
						EPWidgetsManualSortPreference epWidgetsManualSort = existingWidgetsIds.get(widgetid);
						epWidgetsManualSort.setWidgetRow(epWidgetsManPref.getRow());
						epWidgetsManualSort.setWidgetCol(epWidgetsManPref.getCol());
						epWidgetsManualSort.setWidgetWidth(epWidgetsManPref.getSizeX());
						epWidgetsManualSort.setWidgetHeight(epWidgetsManPref.getSizeY());
						HashMap<String, Long> additionalUpdateParam = new HashMap<>();
						additionalUpdateParam.put("userId", epWidgetsManualSort.getUserId());
						dataAccessService.saveDomainObject(epWidgetsManualSort, additionalUpdateParam);
					} else {
						EPWidgetsManualSortPreference epWidgetsManualSort = new EPWidgetsManualSortPreference();
						epWidgetsManualSort.setWidgetId(epWidgetsManPref.getWidgetid());
						epWidgetsManualSort.setWidgetRow(epWidgetsManPref.getRow());
						epWidgetsManualSort.setWidgetCol(epWidgetsManPref.getCol());
						epWidgetsManualSort.setWidgetWidth(epWidgetsManPref.getSizeX());
						epWidgetsManualSort.setWidgetHeight(epWidgetsManPref.getSizeY());
						epWidgetsManualSort.setUserId(user.getId());
						dataAccessService.saveDomainObject(epWidgetsManualSort, null);
					}
					fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_OK);
				}
			}
		} catch (Exception e) {
			logger.error(EELFLoggerDelegate.errorLogger, "saveWidgetsSortManual failed", e);
			fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
		}
		return fieldsValidator;
	}

	@SuppressWarnings("unchecked")
	@Override
	public FieldsValidator deleteUserWidgetSortPref(List<EPWidgetsSortPreference> delWidgetSortPref, EPUser user) {
		FieldsValidator fieldsValidator = new FieldsValidator();
		final Map<String, Long> params = new HashMap<>();
		List<EPWidgetsManualSortPreference> epWidgets = new ArrayList<EPWidgetsManualSortPreference>();
		try {
			params.put("userId", user.getId());
			epWidgets = dataAccessService.executeNamedQuery("userWidgetManualSortPrfQuery", params, null);
			Map<Long, EPWidgetsManualSortPreference> existingWidgetIds = new HashMap<Long, EPWidgetsManualSortPreference>();
			for (EPWidgetsManualSortPreference userWidgetSortPref : epWidgets) {
				existingWidgetIds.put(userWidgetSortPref.getWidgetId(), userWidgetSortPref);
			}
			for (EPWidgetsSortPreference delEpWidgetsManPref : delWidgetSortPref) {
				if (delEpWidgetsManPref.getWidgetid() != null) {
					Long widgetId = delEpWidgetsManPref.getWidgetid();
					if (existingWidgetIds.containsKey(widgetId)) {
						params.put("widgetId",widgetId);
						dataAccessService.executeNamedQuery("deleteUserWidgetPlacement", params, null);
					}
					fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_OK);
				}
			}
		} catch (Exception e) {
			logger.error(EELFLoggerDelegate.errorLogger, "deleteUserWidgetSortPref failed", e);
			fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
		}
		return fieldsValidator;
	}

	/*
	 * This Method Stores the Sort Order of User Apps by Sort Manual Preference
	 *
	 * @param: appsSortManual--contains User Apps Data
	 *
	 * @param: user--contains LoggedIn User Data
	 */
	@SuppressWarnings("unchecked")
	@Override
	public FieldsValidator saveAppsSortManual(List<EPAppsManualPreference> appsSortManual, EPUser user) {
		FieldsValidator fieldsValidator = new FieldsValidator();
		final Map<String, Long> params = new HashMap<>();
		List<EPUserAppsManualSortPreference> epManualApps = new ArrayList<EPUserAppsManualSortPreference>();

		try {
			params.put("userId", user.getId());
			epManualApps = dataAccessService.executeNamedQuery("userAppsManualSortPrfQuery", params, null);
			Map<Long, EPUserAppsManualSortPreference> existingAppIds = new HashMap<Long, EPUserAppsManualSortPreference>();
			for (EPUserAppsManualSortPreference userAppManualPref : epManualApps) {
				existingAppIds.put(userAppManualPref.getAppId(), userAppManualPref);
			}
			for (EPAppsManualPreference epAppsManPref : appsSortManual) {
				if (epAppsManPref.getAppid() != null) {
					Long appid = epAppsManPref.getAppid();
					if (existingAppIds.containsKey(appid)) {
						EPUserAppsManualSortPreference epAppsManualSort = existingAppIds.get(appid);
						epAppsManualSort
								.setAppManualSortOrder((epAppsManPref.getCol() + (6 * epAppsManPref.getRow())) + 1);
						HashMap<String, Long> additionalUpdateParam = new HashMap<>();
						additionalUpdateParam.put("userId", epAppsManualSort.getUserId());
						dataAccessService.saveDomainObject(epAppsManualSort, additionalUpdateParam);
					} else {
						EPUserAppsManualSortPreference epAppsManualSort = new EPUserAppsManualSortPreference();
						epAppsManualSort.setAppId(epAppsManPref.getAppid());
						epAppsManualSort
								.setAppManualSortOrder((epAppsManPref.getCol() + (6 * epAppsManPref.getRow())) + 1);
						epAppsManualSort.setUserId(user.getId());
						dataAccessService.saveDomainObject(epAppsManualSort, null);
					}
					fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_OK);
				}
			}
		} catch (Exception e) {
			logger.error(EELFLoggerDelegate.errorLogger, "saveAppsSortManual failed", e);
			fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
		}
		return fieldsValidator;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.onap.portalapp.portal.service.EPAppService#
	 * deleteUserAppSortManual(java.lang.String,
	 * org.onap.portalapp.portal.domain.EPUser)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public FieldsValidator deleteUserAppSortManual(EPDeleteAppsManualSortPref delAppSortManual, EPUser user) {
		FieldsValidator fieldsValidator = new FieldsValidator();
		final Map<String, Long> params = new HashMap<>();
		List<EPUserAppsManualSortPreference> epManualApps = new ArrayList<EPUserAppsManualSortPreference>();
		try {
			params.put("userId", user.getId());
			epManualApps = dataAccessService.executeNamedQuery("userAppsManualSortPrfQuery", params, null);
			Map<Long, EPUserAppsManualSortPreference> existingAppIds = new HashMap<Long, EPUserAppsManualSortPreference>();
			for (EPUserAppsManualSortPreference userAppPref : epManualApps) {
				existingAppIds.put(userAppPref.getAppId(), userAppPref);
			}
			if (existingAppIds.containsKey(delAppSortManual.getAppId()) && !delAppSortManual.isSelect()) {
				dataAccessService.deleteDomainObjects(EPUserAppsManualSortPreference.class,
						"app_id=" + delAppSortManual.getAppId() + " AND user_id=" + user.getId(), null);
				fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_OK);
			}
		} catch (Exception e) {
			logger.error(EELFLoggerDelegate.errorLogger, "deleteUserAppSortManual failed", e);
			fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
		}
		return fieldsValidator;
	}

	@SuppressWarnings("unchecked")
	@Override
	public FieldsValidator saveAppsSortPreference(EPAppsSortPreference appsSortPreference, EPUser user) {
		FieldsValidator fieldsValidator = new FieldsValidator();
		final Map<String, Long> params = new HashMap<>();
		List<EPUserAppsSortPreference> epSortTypes = new ArrayList<EPUserAppsSortPreference>();
		EPUserAppsSortPreference usrSortPr = null;
		try {
			params.put("userId", user.getId());
			epSortTypes = dataAccessService.executeNamedQuery("userAppsSortPreferenceQuery", params, null);
			if (epSortTypes.size() == 0) {
				usrSortPr = new EPUserAppsSortPreference();
				usrSortPr.setUserId((int)(long)(user.getId()));
				usrSortPr.setSortPref(appsSortPreference.getValue());
				dataAccessService.saveDomainObject(usrSortPr, null);
				fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_OK);
			} else {
				usrSortPr = epSortTypes.get(0);
				usrSortPr.setSortPref(appsSortPreference.getValue());
				HashMap<String, Integer> additionalUpdateParam = new HashMap<String, Integer>();
				additionalUpdateParam.put("userId", usrSortPr.getUserId());
				dataAccessService.saveDomainObject(usrSortPr, additionalUpdateParam);
				fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_OK);
			}
		} catch (Exception e) {
			logger.error(EELFLoggerDelegate.errorLogger, "saveAppsSortPreference failed", e);
			fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
		}
		return fieldsValidator;
	}

	@SuppressWarnings("unchecked")
	@Override
	public String getUserAppsSortTypePreference(EPUser user) {
		final Map<String, Long> params = new HashMap<>();
		List<EPUserAppsSortPreference> userSortPrefs = new ArrayList<EPUserAppsSortPreference>();
		try {
			params.put("userId", user.getId());
			userSortPrefs = dataAccessService.executeNamedQuery("userAppsSortPreferenceQuery", params, null);
			if (userSortPrefs.size() > 0)
				return userSortPrefs.get(0).getSortPref();
			else
				return null;
		} catch (Exception e) {
			logger.error(EELFLoggerDelegate.errorLogger, "getUserAppsSortTypePreference failed", e);
		}
		return null;

	}

	@Override
	public List<EPApp> getUserRemoteApps(String id) {
		throw new RuntimeException(" Cannot be called from parent class");
	}
	
	@Override
	public UserRoles getUserProfileForLeftMenu(String loginId) {
		final Map<String, String> params = new HashMap<>();
		params.put("org_user_id", loginId);
		@SuppressWarnings("unchecked")
		List<UserRole> userRoleList = dataAccessService.executeNamedQuery( "getUserRolesForLeftMenu", params, null);
		ArrayList<UserRoles> usersRolesList = aggregateUserProfileRowsResultsByRole(userRoleList);
		if (usersRolesList == null || usersRolesList.size() < 1)
			return null;

		return usersRolesList.get(0);
	}
	
	
	@Override
	public UserRoles getUserProfileForRolesLeftMenu(String loginId) {
		final Map<String, String> params = new HashMap<>();
		params.put("org_user_id", loginId);
		@SuppressWarnings("unchecked")
		List<UserRole> userRoleList = dataAccessService.executeNamedQuery( "getRolesForLeftMenu", params, null);
		ArrayList<UserRoles> usersRolesList = aggregateUserProfileRowsResultsByRole(userRoleList);
		if (usersRolesList == null || usersRolesList.size() < 1)
			return null;

		return usersRolesList.get(0);
	}
	
	@Override
	public UserRoles getUserProfileNormalizedForLeftMenu(EPUser user) {
		// Check database.
		UserRoles userAndRoles = getUserProfileForLeftMenu(user.getLoginId());
		// If no roles are defined, treat this user as a guest.
		if (user.isGuest() || userAndRoles == null) {
			logger.debug(EELFLoggerDelegate.debugLogger, "getUserProfileForLeftMenu: treating user {} as guest",
					user.getLoginId());
			userAndRoles = createUserRoles(user);
		}

		return userAndRoles;
	}
	
	@Override
	public UserRoles getUserProfileNormalizedForRolesLeftMenu(EPUser user) {
		// Check database.
		UserRoles userAndRoles = getUserProfileForRolesLeftMenu(user.getLoginId());
		// If no roles are defined, treat this user as a guest.
		if (user.isGuest() || userAndRoles == null) {
			logger.debug(EELFLoggerDelegate.debugLogger, "getUserProfileForLeftMenu: treating user {} as guest",
					user.getLoginId());
			userAndRoles = createUserRoles(user);
		}

		return userAndRoles;
	}

	
	public UserRoles createUserRoles(EPUser user)
	{
		UserRole userRole = new UserRole();
		userRole.setUser_Id(user.getId());
		userRole.setOrgUserId(user.getLoginId());
		userRole.setFirstName(user.getFirstName());
		userRole.setLastName(user.getLastName());
		userRole.setRoleId(-1L);
		userRole.setRoleName("Guest");
		userRole.setUser_Id(-1L);
		UserRoles userAndRoles = new UserRoles(userRole);
		return userAndRoles;
		
	}

	@SuppressWarnings("unused")
	@Override
	public ResponseEntity<String> checkIfNameSpaceIsValid(String namespace) throws Exception {
		HttpHeaders headers = EcompPortalUtils.base64encodeKeyForAAFBasicAuth();
		HttpEntity<String> entity = new HttpEntity<>(headers);
		logger.debug(EELFLoggerDelegate.debugLogger, "checkIfNameSpaceExists: Connecting to External Auth system for : "+namespace);
		ResponseEntity<String> response = null;
		try {
			
			String namespaceUrl = SystemProperties.
					getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL) + "nss/" + namespace;
			
			response = template.exchange(namespaceUrl, HttpMethod.GET, entity, String.class);
			logger.debug(EELFLoggerDelegate.debugLogger, "checkIfNameSpaceExists for"+ namespace ,
					response.getStatusCode().value());
			if (response.getStatusCode().value() == 200) {
				String res = response.getBody();
				JSONObject jsonObj = new JSONObject(res);
				JSONArray namespaceArray = jsonObj.getJSONArray("ns");
				if(!namespaceArray.getJSONObject(0).has("admin")){
					logger.error(EELFLoggerDelegate.errorLogger,
							"No admins are available for requested namespace:" + namespace);		
					throw new HttpClientErrorException(HttpStatus.UNAUTHORIZED,
							"Portal Mechid is not an admin of" + namespace);
				}
				
				JSONArray namespaceAdminArray = namespaceArray.getJSONObject(0).getJSONArray("admin");
				ArrayList<String> list = new ArrayList<String>();
				if (namespaceAdminArray != null) {
					int len = namespaceAdminArray.length();
					for (int i = 0; i < len; i++) {
						list.add(namespaceAdminArray.get(i).toString());
					}
				}
				logger.debug(EELFLoggerDelegate.debugLogger, "List of Admins of requested namespace" + list);
				final String userName = SystemProperties
						.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_AUTH_USER_NAME);
				boolean idExists = list.stream().anyMatch(t -> userName.equals(t));
				if (false) {
					logger.error(EELFLoggerDelegate.errorLogger,
							"Portal mechid is not admin of requested namespace:" + namespace);
					throw new HttpClientErrorException(HttpStatus.UNAUTHORIZED,
							"Portal Mechid is not an admin of" + namespace);
				}
			}
			
		} catch (HttpClientErrorException e) {
			logger.error(EELFLoggerDelegate.errorLogger, "checkIfNameSpaceExists failed", e);
			EPLogUtil.logExternalAuthAccessAlarm(logger, e.getStatusCode());
				throw e;
		}
		return response;

	}
}