summaryrefslogtreecommitdiffstats
path: root/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/controller/AppsController.java
blob: 9feecec1862ce1cdbe1f5058d9312a226a037e44 (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
/*-
 * ============LICENSE_START==========================================
 * ONAP Portal
 * ===================================================================
 * Copyright (C) 2019 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.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.onap.portalapp.controller.EPRestrictedBaseController;
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.EcompApp;
import org.onap.portalapp.portal.domain.UserRoles;
import org.onap.portalapp.portal.exceptions.InvalidApplicationException;
import org.onap.portalapp.portal.logging.aop.EPAuditLog;
import org.onap.portalapp.portal.logging.logic.EPLogUtil;
import org.onap.portalapp.portal.service.AdminRolesService;
import org.onap.portalapp.portal.service.EPAppService;
import org.onap.portalapp.portal.service.EPLeftMenuService;
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.LocalRole;
import org.onap.portalapp.portal.transport.OnboardingApp;
import org.onap.portalapp.portal.utils.EcompPortalUtils;
import org.onap.portalapp.portal.utils.PortalConstants;
import org.onap.portalapp.util.EPUserUtils;
import org.onap.portalapp.validation.DataValidator;
import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
import org.onap.portalsdk.core.util.SystemProperties;
import org.onap.portalsdk.core.web.support.AppUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.HttpClientErrorException;

@RestController
@EnableAspectJAutoProxy
@EPAuditLog
@NoArgsConstructor
@Getter
public class AppsController extends EPRestrictedBaseController {
	private static final String GET_RESULT = "GET result =";
	private static final String PUT_RESULT = "PUT result =";
	private static final String PORTAL_API_ONBOARDING_APPS = "/portalApi/onboardingApps";
	private static final String PORTAL_API_USER_APPS_ORDER_BY_SORT_PREF = "/portalApi/userAppsOrderBySortPref";

	private final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(AppsController.class);
	private final DataValidator dataValidator = new DataValidator();

	@Autowired
	private AdminRolesService adminRolesService;
	@Autowired
	private EPAppService appService;
	@Autowired
	private EPLeftMenuService leftMenuService;

	/**
	 * RESTful service method to fetch all Applications available to current
	 * user
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @param response
	 *            HttpServletResponse
	 * @return List<EcompApp>
	 */
	@RequestMapping(value = { "/portalApi/userApps" }, method = RequestMethod.GET, produces = "application/json")
	public List<EcompApp> getUserApps(HttpServletRequest request, HttpServletResponse response) {
		EPUser user = EPUserUtils.getUserSession(request);
		List<EcompApp> ecompApps = null;

		try {
			if (user == null) {
				EcompPortalUtils.setBadPermissions(user, response, "getUserApps");
			} else {
				ecompApps = appService.transformAppsToEcompApps(appService.getUserApps(user));
				EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/userApps", GET_RESULT, ecompApps);
			}
		} catch (Exception e) {
			logger.error(EELFLoggerDelegate.errorLogger, "getUserApps failed", e);
		}

		return ecompApps;
	}

	/**
	 * RESTful service method to fetch all applications accessible to the
	 * current user, with personalizations.
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @param response
	 *            HttpServletResponse
	 * @return List<EcompApp>
	 * @throws IOException
	 *             if sendError fails
	 */
	@RequestMapping(value = { "/portalApi/persUserApps" }, method = RequestMethod.GET, produces = "application/json")
	public List<EcompApp> getPersUserApps(HttpServletRequest request, HttpServletResponse response) throws IOException {
		EPUser user = EPUserUtils.getUserSession(request);
		List<EcompApp> ecompApps = null;
		try {
			if (user == null) {
				EcompPortalUtils.setBadPermissions(user, response, "getPersUserApps");
			} else {
				List<EPApp> apps = null;
				if (adminRolesService.isSuperAdmin(user))
					apps = appService.getPersAdminApps(user);
				else
					apps = appService.getPersUserApps(user);
				ecompApps = appService.transformAppsToEcompApps(apps);
				EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/userPersApps", GET_RESULT, ecompApps);
			}
		} catch (Exception e) {
			logger.error(EELFLoggerDelegate.errorLogger, "getPersUserApps failed", e);
			response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString());
		}
		return ecompApps;
	}

	/**
	 * RESTful service method to fetch applications for which the current user
	 * is an Administrator
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @param response
	 *            HttpServletResponse
	 * @return List<AppIdAndNameTransportModel>
	 */
	@RequestMapping(value = { "/portalApi/adminApps" }, method = RequestMethod.GET, produces = "application/json")
	public List<AppIdAndNameTransportModel> getAdminApps(HttpServletRequest request, HttpServletResponse response) {
		EPUser user = EPUserUtils.getUserSession(request);
		List<AppIdAndNameTransportModel> adminApps = null;

		try {			
			if (!adminRolesService.isAccountAdmin(user) && !adminRolesService.isRoleAdmin(user) ) {
				EcompPortalUtils.setBadPermissions(user, response, "getAdminApps");
			} else {
				adminApps = appService.getAdminApps(user);
				EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/adminApps", GET_RESULT, adminApps);
			}
		} catch (Exception e) {
			logger.error(EELFLoggerDelegate.errorLogger, "getAdminApps failed", e);
		}

		return adminApps;
	}

	/**
	 * RESTful service method to fetch Applications for user who is super admin
	 * and/or app admin.
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @param response
	 *            HttpServletResponse
	 * @return List<AppIdAndNameTransportModel>
	 */
	@RequestMapping(value = {
			"/portalApi/appsForSuperAdminAndAccountAdmin" }, method = RequestMethod.GET, produces = "application/json")
	public List<AppIdAndNameTransportModel> getAppsForSuperAdminAndAccountAdmin(HttpServletRequest request,
			HttpServletResponse response) {
		EPUser user = EPUserUtils.getUserSession(request);
		List<AppIdAndNameTransportModel> adminApps = null;

		try {
			if (!adminRolesService.isSuperAdmin(user) && !adminRolesService.isAccountAdmin(user) && !adminRolesService.isRoleAdmin(user) ) {
				EcompPortalUtils.setBadPermissions(user, response, "getAdminApps");
			} else {
				adminApps = appService.getAppsForSuperAdminAndAccountAdmin(user);
				EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/appsForSuperAdminAndAccountAdmin",
						GET_RESULT, adminApps);
			}
		} catch (Exception e) {
			logger.error(EELFLoggerDelegate.errorLogger, "getAppsForSuperAdminAndAccountAdmin failed", e);
		}

		return adminApps;
	}

	/**
	 * RESTful service method to fetch left menu items from the user'PORTAL_API_USER_APPS_ORDER_BY_SORT_PREF session.
	 * 
	 * @param request
	 *            HttpServletRequest
	 * @param response
	 *            HttpServletResponse
	 * @return JSON with left menu
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value = { "/portalApi/leftmenuItems" }, method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	public String getLeftMenuItems(HttpServletRequest request, HttpServletResponse response) {
		String menuList = null;
		Set menuSet = (Set) AppUtils.getSession(request)
				.getAttribute(SystemProperties.getProperty(SystemProperties.APPLICATION_MENU_ATTRIBUTE_NAME));

		Set roleFunctionSet = (Set) AppUtils.getSession(request)
				.getAttribute(SystemProperties.getProperty(SystemProperties.ROLE_FUNCTIONS_ATTRIBUTE_NAME));

		EPUser user = EPUserUtils.getUserSession(request);

		try {
			menuList = leftMenuService.getLeftMenuItems(user, menuSet, roleFunctionSet);
			EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/leftmenuItems", GET_RESULT, menuList);
		} catch (Exception e) {
			logger.error(EELFLoggerDelegate.errorLogger, "getLeftMenuItems failed", e);
		}
		return menuList;
	}

	@RequestMapping(value = {
			PORTAL_API_USER_APPS_ORDER_BY_SORT_PREF }, method = RequestMethod.GET, produces = "application/json")
	public List<EcompApp> getUserAppsOrderBySortPref(HttpServletRequest request, HttpServletResponse response) {
		EPUser user = EPUserUtils.getUserSession(request);
		List<EcompApp> ecompApps = null;
		try {
			if (user == null) {
				EcompPortalUtils.setBadPermissions(user, response, "getUserAppsOrderBySortPref");
			} else {
				String usrSortPref = request.getParameter("mparams");
				if (usrSortPref.isEmpty()) {
					usrSortPref = "N";
				}
				switch (usrSortPref) {
				case "N":
					ecompApps = appService.transformAppsToEcompApps(appService.getAppsOrderByName(user));
					EcompPortalUtils.logAndSerializeObject(logger, PORTAL_API_USER_APPS_ORDER_BY_SORT_PREF, GET_RESULT,
							ecompApps);
					break;
				case "L":
					ecompApps = appService.transformAppsToEcompApps(appService.getAppsOrderByLastUsed(user));
					EcompPortalUtils.logAndSerializeObject(logger, PORTAL_API_USER_APPS_ORDER_BY_SORT_PREF, GET_RESULT,
							ecompApps);
					break;
				case "F":
					ecompApps = appService.transformAppsToEcompApps(appService.getAppsOrderByMostUsed(user));
					EcompPortalUtils.logAndSerializeObject(logger, PORTAL_API_USER_APPS_ORDER_BY_SORT_PREF, GET_RESULT,
							ecompApps);
					break;
				case "M":
					ecompApps = appService.transformAppsToEcompApps(appService.getAppsOrderByManual(user));
					EcompPortalUtils.logAndSerializeObject(logger, PORTAL_API_USER_APPS_ORDER_BY_SORT_PREF, GET_RESULT,
							ecompApps);
					break;
				default:
					logger.error(EELFLoggerDelegate.errorLogger,
							"getUserAppsOrderBySortPref failed: no match for " + usrSortPref);
				}
			}
		} catch (Exception e) {
			logger.error(EELFLoggerDelegate.errorLogger, "getUserAppsOrderBySortPref failed", e);
		}
		return ecompApps;
	}

	/**
	 * Sets the user apps manual sort preference
	 * 
	 * @param request
	 *            HTTP servlet request
	 * @param response
	 *            HTTP servlet response
	 * @param epAppsManualPref
	 *            sort pref
	 * @return FieldsValidator
	 */
	@RequestMapping(value = {
			"/portalApi/saveUserAppsSortingManual" }, method = RequestMethod.PUT, produces = "application/json")
	public FieldsValidator putUserAppsSortingManual(HttpServletRequest request,
			@RequestBody List<EPAppsManualPreference> epAppsManualPref, HttpServletResponse response) {
		FieldsValidator fieldsValidator = null;

		if (isNotNullAndNotValid(epAppsManualPref)){
			fieldsValidator = new FieldsValidator();
			fieldsValidator.setHttpStatusCode((long) HttpServletResponse.SC_NOT_ACCEPTABLE);
			return fieldsValidator;
		}

		try {
			EPUser user = EPUserUtils.getUserSession(request);
			fieldsValidator = appService.saveAppsSortManual(epAppsManualPref, user);
			response.setStatus(fieldsValidator.httpStatusCode.intValue());
		} catch (Exception e) {
			logger.error(EELFLoggerDelegate.errorLogger, "putUserAppsSortingManual failed", e);
		}
		EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/saveUserAppsSortingManual", PUT_RESULT,
				response.getStatus());
		return fieldsValidator;
	}

	@RequestMapping(value = {
			"/portalApi/saveUserWidgetsSortManual" }, method = RequestMethod.PUT, produces = "application/json")
	public FieldsValidator putUserWidgetsSortManual(HttpServletRequest request,
			@RequestBody List<EPWidgetsSortPreference> saveManualWidgetSData, HttpServletResponse response) {
		FieldsValidator fieldsValidator = null;

		if (isNotNullAndNotValid(saveManualWidgetSData)){
			fieldsValidator = new FieldsValidator();
			fieldsValidator.setHttpStatusCode((long)HttpServletResponse.SC_NOT_ACCEPTABLE);
			return fieldsValidator;
		}

		try {
			EPUser user = EPUserUtils.getUserSession(request);
			fieldsValidator = appService.saveWidgetsSortManual(saveManualWidgetSData, user);
			response.setStatus(fieldsValidator.httpStatusCode.intValue());
		} catch (Exception e) {
			logger.error(EELFLoggerDelegate.errorLogger, "putUserWidgetsSortManual failed", e);
		}
		EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/putUserWidgetsSortManual", PUT_RESULT,
				response.getStatus());
		return fieldsValidator;
	}

	@RequestMapping(value = {
			"/portalApi/updateWidgetsSortPref" }, method = RequestMethod.PUT, produces = "application/json")
	public FieldsValidator putUserWidgetsSortPref(HttpServletRequest request,
			@RequestBody List<EPWidgetsSortPreference> delManualWidgetData, HttpServletResponse response) {
		FieldsValidator fieldsValidator = null;

		if (isNotNullAndNotValid(delManualWidgetData)){
			fieldsValidator = new FieldsValidator();
			fieldsValidator.setHttpStatusCode((long)HttpServletResponse.SC_NOT_ACCEPTABLE);
			return fieldsValidator;
		}

		try {
			EPUser user = EPUserUtils.getUserSession(request);
			fieldsValidator = appService.deleteUserWidgetSortPref(delManualWidgetData, user);
			response.setStatus(fieldsValidator.httpStatusCode.intValue());
		} catch (Exception e) {
			logger.error(EELFLoggerDelegate.errorLogger, "putUserWidgetsSortPref failed", e);

		}
		EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/putUserWidgetsSortPref", PUT_RESULT,
				response.getStatus());
		return fieldsValidator;
	}

	/**
	 * Deletes the user app manual sort preference record
	 * 
	 * @param request
	 *            HTTP servlet request
	 * @param response
	 *            HTTP servlet response
	 * @param delManualAppData
	 *            data to delete
	 * @return FieldsValidator
	 */
	@RequestMapping(value = {
			"/portalApi/UpdateUserAppsSortManual" }, method = RequestMethod.PUT, produces = "application/json")
	public FieldsValidator deleteUserAppSortManual(HttpServletRequest request,
			@RequestBody EPDeleteAppsManualSortPref delManualAppData, HttpServletResponse response) {
		FieldsValidator fieldsValidator = null;

		try {
			EPUser user = EPUserUtils.getUserSession(request);
			fieldsValidator = appService.deleteUserAppSortManual(delManualAppData, user);
			response.setStatus(fieldsValidator.httpStatusCode.intValue());
		} catch (Exception e) {
			logger.error(EELFLoggerDelegate.errorLogger, "deleteUserAppSortManual failed", e);

		}
		EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/deleteUserAppSortManual", PUT_RESULT,
				response.getStatus());
		return fieldsValidator;
	}

	@RequestMapping(value = {
			"/portalApi/saveUserAppsSortingPreference" }, method = RequestMethod.PUT, produces = "application/json")
	public FieldsValidator putUserAppsSortingPreference(HttpServletRequest request,
			@RequestBody EPAppsSortPreference userAppsValue, HttpServletResponse response) {
		FieldsValidator fieldsValidator = null;
		try {
			EPUser user = EPUserUtils.getUserSession(request);
			fieldsValidator = appService.saveAppsSortPreference(userAppsValue, user);
			response.setStatus(fieldsValidator.httpStatusCode.intValue());
		} catch (Exception e) {
			logger.error(EELFLoggerDelegate.errorLogger, "putUserAppsSortingPreference failed", e);

		}

		EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/putUserAppsSortingPreference", PUT_RESULT,
				response.getStatus());
		return fieldsValidator;
	}

	@RequestMapping(value = {
			"/portalApi/userAppsSortTypePreference" }, method = RequestMethod.GET, produces = "application/String")
	public String getUserAppsSortTypePreference(HttpServletRequest request, HttpServletResponse response) {
		EPUser user = EPUserUtils.getUserSession(request);
		String userSortPreference = null;

		try {
			if (user == null) {
				EcompPortalUtils.setBadPermissions(user, response, "userAppsSortTypePreference");
			} else {
				userSortPreference = appService.getUserAppsSortTypePreference(user);
				EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/userAppsSortTypePreference", GET_RESULT,
						userSortPreference);
			}
		} catch (Exception e) {
			logger.error(EELFLoggerDelegate.errorLogger, "getUserAppsSortTypePreference failed", e);
		}

		return userSortPreference;
	}

	/**
	 * RESTful service method to fetch Application Administrators to Super
	 * Administrator user. Attention: Users which have Super Administrator roles
	 * only are not included!
	 * 
	 * @param request
	 *            HTTP servlet request
	 * @param response
	 *            HTTP servlet response
	 * @return List<AdminUserApplications>
	 */
	@RequestMapping(value = { "/portalApi/accountAdmins" }, method = RequestMethod.GET, produces = "application/json")
	public List<AdminUserApplications> getAppsAdministrators(HttpServletRequest request, HttpServletResponse response) {
		EPUser user = EPUserUtils.getUserSession(request);
		List<AdminUserApplications> admins = null;
		try {
			if (!adminRolesService.isSuperAdmin(user)) {
				EcompPortalUtils.setBadPermissions(user, response, "getAppsAdministrators");
			} else {
				admins = appService.getAppsAdmins();
				EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/accountAdmins", GET_RESULT, admins);
			}
		} catch (Exception e) {
			logger.error(EELFLoggerDelegate.errorLogger, "getAppsAdministrators failed", e);
		}

		return admins;
	}

	@RequestMapping(value = { "/portalApi/availableApps" }, method = RequestMethod.GET, produces = "application/json")
	public List<AppsResponse> getApps(HttpServletRequest request, HttpServletResponse response) {
		EPUser user = EPUserUtils.getUserSession(request);
		List<AppsResponse> apps = null;
		try {
			if (!adminRolesService.isSuperAdmin(user)) {
				EcompPortalUtils.setBadPermissions(user, response, "getApps");
			} else {
				apps = appService.getAllApplications(false);
				EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/availableApps", GET_RESULT, apps);
			}
		} catch (Exception e) {
			logger.error(EELFLoggerDelegate.errorLogger, "getApps failed", e);
		}

		return apps;
	}

	/**
	 * Gets all apps, both active and inactive; i.e., all on-boarded apps,
	 * regardless of enabled status.
	 * 
	 * @param request
	 *            HTTP servlet request
	 * @param response
	 *            HTTP servlet response
	 * @return List of applications
	 */
	@RequestMapping(value = {
			"/portalApi/allAvailableApps" }, method = RequestMethod.GET, produces = "application/json")
	public List<AppsResponse> getAllApps(HttpServletRequest request, HttpServletResponse response) {
		EPUser user = EPUserUtils.getUserSession(request);
		List<AppsResponse> apps = null;
		try {
			if (!adminRolesService.isSuperAdmin(user)) {
				EcompPortalUtils.setBadPermissions(user, response, "getApps");
			} else {
				apps = appService.getAllApps(true);
				EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/availableApps", GET_RESULT, apps);
			}
		} catch (Exception e) {
			logger.error(EELFLoggerDelegate.errorLogger, "getAllApps failed", e);
		}

		return apps;
	}

	/**
	 * 
	 * @param request
	 *            HTTP servlet request
	 * @param response
	 *            HTTP servlet response
	 * @return List of applications
	 */
	@RequestMapping(value = { "/portalApi/appsFullList" }, method = RequestMethod.GET, produces = "application/json")
	public List<EcompApp> getAppsFullList(HttpServletRequest request, HttpServletResponse response) {
		EPUser user = EPUserUtils.getUserSession(request);
		List<EcompApp> ecompApps = null;
		if (user == null) {
			EcompPortalUtils.setBadPermissions(user, response, "getAppsFullList");
		} else {
			ecompApps = appService.getEcompAppAppsFullList();
			EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/appsFullList", GET_RESULT, ecompApps);
		}
		return ecompApps;
	}

	/**
	 * 
	 * @param request
	 *            HTTP servlet request
	 * @param response
	 *            HTTP servlet response
	 * @return UserRoles
	 */
	@RequestMapping(value = { "/portalApi/userProfile" }, method = RequestMethod.GET, produces = "application/json")
	public UserRoles getUserProfile(HttpServletRequest request, HttpServletResponse response) {
		EPUser user = EPUserUtils.getUserSession(request);
		UserRoles userAndRoles = null;
		try {
			if (user == null) {
				EcompPortalUtils.setBadPermissions(user, response, "getUserProfile");
			} else {
				userAndRoles = appService.getUserProfileNormalized(user);
			}
		} catch (Exception e) {
			logger.error(EELFLoggerDelegate.errorLogger, "getUserProfile failed", e);
		}

		EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/userProfile", "getUserProfile result =",
				userAndRoles);
		return userAndRoles;
	}

	/**
	 * 
	 * @param request
	 *            HTTP servlet request
	 * @param appId
	 *            application ID
	 * @return List<LocalRole>
	 */
	@RequestMapping(value = { "/portalApi/appRoles/{appId}" }, method = {
			RequestMethod.GET }, produces = "application/json")
	public List<LocalRole> getAppRoles(HttpServletRequest request, @PathVariable("appId") Long appId,
			HttpServletResponse response) {
		List<LocalRole> roleList = null;
		EPUser user = EPUserUtils.getUserSession(request);
		EPApp requestedApp = appService.getApp(appId);
		if (user != null && (adminRolesService.isAccountAdminOfApplication(user, requestedApp)
				|| (adminRolesService.isSuperAdmin(user) && requestedApp.getId() == PortalConstants.PORTAL_APP_ID))) {
			try {
				roleList = appService.getAppRoles(appId);
				EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/appRoles/" + appId, GET_RESULT,
						roleList);
			} catch (Exception e) {
				logger.error(EELFLoggerDelegate.errorLogger, "getAppRoles failed", e);
			}
		} else {
			EcompPortalUtils.setBadPermissions(user, response, "getAppRoles");
		}
		return roleList;
	}
	
	/**
	 * 
	 * Return single app information with appName as parameter
	 * 
	 * @param request
	 * @param response
	 * @return EPApp object
	 */
	@RequestMapping(value = { "/portalApi/singleAppInfo" }, method = {
			RequestMethod.GET }, produces = "application/json")
	public EPApp getSingleAppInfo(HttpServletRequest request, HttpServletResponse response) {
		EPApp app = null;
		EPUser user = EPUserUtils.getUserSession(request);
		try {
			String appName = request.getParameter("appParam");
			app = appService.getAppDetailByAppName(appName);
			if (user != null && (adminRolesService.isAccountAdminOfApplication(user, app)
					|| (adminRolesService.isSuperAdmin(user) && app.getId().equals(PortalConstants.PORTAL_APP_ID))))
				EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/singleAppInfo" + appName, GET_RESULT, app);
			else{
				app= null;
				EcompPortalUtils.setBadPermissions(user, response, "createAdmin");
			}

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

	/**
	 * 
	 * Return single app information with appId as parameter
	 * 
	 * @param request
	 * @param response
	 * @return EPApp object
	 */
	@RequestMapping(value = { "/portalApi/singleAppInfoById" }, method = {
			RequestMethod.GET }, produces = "application/json")
	public EPApp getSingleAppInfoById(HttpServletRequest request, HttpServletResponse response) {
		EPApp app = null;
		EPUser user = EPUserUtils.getUserSession(request);
		try {
			String appId = request.getParameter("appParam");
			app = appService.getApp(Long.valueOf(appId));
			if(!EcompPortalUtils.checkIfRemoteCentralAccessAllowed()) {
				app.setCentralAuth(false);
			}
			if (user != null && (adminRolesService.isAccountAdminOfApplication(user, app)
					|| (adminRolesService.isSuperAdmin(user) && app.getId().equals(PortalConstants.PORTAL_APP_ID))))
				EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/singleAppInfoById" + appId, GET_RESULT, app);
			else{
				app= null;
				EcompPortalUtils.setBadPermissions(user, response, "createAdmin");
			}

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

	/**
	 * 
	 * @param request
	 *            HTTP servlet request
	 * @param response
	 *            HTTP servlet response
	 * @return List<OnboardingApp>
	 */
	@RequestMapping(value = { PORTAL_API_ONBOARDING_APPS }, method = RequestMethod.GET, produces = "application/json")
	public List<OnboardingApp> getOnboardingApps(HttpServletRequest request, HttpServletResponse response) {
		EPUser user = EPUserUtils.getUserSession(request);
		List<OnboardingApp> onboardingApps = null;
		try {
			if (!adminRolesService.isSuperAdmin(user) && !adminRolesService.isAccountAdmin(user)) {
				EcompPortalUtils.setBadPermissions(user, response, "getOnboardingApps");
			} else {
				
				if(adminRolesService.isSuperAdmin(user)){
				onboardingApps = appService.getOnboardingApps();
				}
				else if(adminRolesService.isAccountAdmin(user))
				{
					//get all his admin apps
					onboardingApps =  appService.getAdminAppsOfUser(user);
				}
				EcompPortalUtils.logAndSerializeObject(logger, PORTAL_API_ONBOARDING_APPS, GET_RESULT,
						"onboardingApps of size " + (onboardingApps != null ? onboardingApps.size() : 0));
			}
		} catch (Exception e) {
			logger.error(EELFLoggerDelegate.errorLogger, "getOnboardingApps failed", e);
		}

		return onboardingApps;
	}

	/**
	 * 
	 * @param request
	 *            HTTP servlet request
	 * @param response
	 *            HTTP servlet response
	 * @param modifiedOnboardingApp
	 *            app to update
	 * @return FieldsValidator
	 * @throws Exception 
	 */
	@RequestMapping(value = { PORTAL_API_ONBOARDING_APPS }, method = RequestMethod.PUT, produces = "application/json")
	public FieldsValidator putOnboardingApp(HttpServletRequest request,
			@RequestBody OnboardingApp modifiedOnboardingApp, HttpServletResponse response) {
		FieldsValidator fieldsValidator = null;
		EPUser user = null;
		EPApp oldEPApp = appService.getApp(modifiedOnboardingApp.id);
		
		try {
			user = EPUserUtils.getUserSession(request);
			if (!adminRolesService.isSuperAdmin(user) && !adminRolesService.isAccountAdminOfAnyActiveorInactiveApplication(user, oldEPApp) ) {
				EcompPortalUtils.setBadPermissions(user, response, "putOnboardingApp");
			} else {
				if((oldEPApp.getCentralAuth() && modifiedOnboardingApp.isCentralAuth && !oldEPApp.getNameSpace().equalsIgnoreCase(modifiedOnboardingApp.nameSpace) && modifiedOnboardingApp.nameSpace!= null ) || (!oldEPApp.getCentralAuth() && modifiedOnboardingApp.isCentralAuth && modifiedOnboardingApp.nameSpace!= null))
				{
					checkIfNameSpaceIsValid(modifiedOnboardingApp, fieldsValidator, response);
				}	
				modifiedOnboardingApp.normalize();
				fieldsValidator = appService.modifyOnboardingApp(modifiedOnboardingApp, user);
				response.setStatus(fieldsValidator.httpStatusCode.intValue());
			}
		} catch (Exception e) {
			logger.error(EELFLoggerDelegate.errorLogger, "putOnboardingApps failed", e);
		}
		if(response.getStatus()==200) {
			try {
				String oldvaluesAsJson = new ObjectMapper().writeValueAsString(oldEPApp);
				String newvaluesAsJson = new ObjectMapper().writeValueAsString(modifiedOnboardingApp);
          logger.info(EELFLoggerDelegate.auditLogger, "/portalApi/onboardingApps, old values ={}", oldvaluesAsJson);
          logger.info(EELFLoggerDelegate.auditLogger, "/portalApi/onboardingApps, loginId={}, new values ={}",
              user != null ? user.getLoginId() : "", newvaluesAsJson);
			} catch (JsonProcessingException e) {
				logger.error(EELFLoggerDelegate.errorLogger, "putOnboardingApps failed", e);
			}
		}
		EcompPortalUtils.logAndSerializeObject(logger, PORTAL_API_ONBOARDING_APPS, PUT_RESULT,
				response.getStatus());
		return fieldsValidator;
	}

	
	
	/**
	 * 
	 * @param request
	 *            HTTP servlet request
	 * @param response
	 *            HTTP servlet response
	 * @param newOnboardingApp
	 *            app to add
	 * @return FieldsValidator
	 */
	@RequestMapping(value = { PORTAL_API_ONBOARDING_APPS }, method = RequestMethod.POST, produces = "application/json")
	public FieldsValidator postOnboardingApp(HttpServletRequest request, @RequestBody OnboardingApp newOnboardingApp,
			HttpServletResponse response) {
		FieldsValidator fieldsValidator = null;
		try {
			EPUser user = EPUserUtils.getUserSession(request);
			if (!adminRolesService.isSuperAdmin(user)) {
				EcompPortalUtils.setBadPermissions(user, response, "postOnboardingApps");
			} else {
				newOnboardingApp.normalize();
				checkIfNameSpaceIsValid(newOnboardingApp, fieldsValidator, response);
				fieldsValidator = appService.addOnboardingApp(newOnboardingApp, user);
				response.setStatus(fieldsValidator.httpStatusCode.intValue());
			}
			if(response.getStatus()==200) {
				try {
					String newvaluesAsJson = new ObjectMapper().writeValueAsString(newOnboardingApp);
					logger.info(EELFLoggerDelegate.auditLogger, "/portalApi/onboardingApps, loginId="+user.getLoginId()+",  values ="+newvaluesAsJson);
				} catch (JsonProcessingException e) {
					logger.error(EELFLoggerDelegate.errorLogger, "postOnboardingApps failed", e);
				}
			}
		} catch (Exception e) {
			logger.error(EELFLoggerDelegate.errorLogger, "postOnboardingApp failed", e);				
		}

		EcompPortalUtils.logAndSerializeObject(logger, PORTAL_API_ONBOARDING_APPS, "POST result =",
				response.getStatus());
		return fieldsValidator;
	}
	
	private FieldsValidator setResponse(HttpStatus statusCode, HttpServletResponse response)
	{
		FieldsValidator fieldsValidator = new FieldsValidator();
		if (statusCode == HttpStatus.NOT_FOUND || statusCode == HttpStatus.FORBIDDEN) {
			fieldsValidator.httpStatusCode = (long) HttpServletResponse.SC_NOT_FOUND;
			logger.error(EELFLoggerDelegate.errorLogger, "setResponse failed"+ "invalid namespace");
		}else if (statusCode == HttpStatus.UNAUTHORIZED) {
			fieldsValidator.httpStatusCode = (long) HttpServletResponse.SC_UNAUTHORIZED;
			logger.error(EELFLoggerDelegate.errorLogger, "setResponse failed"+ "unauthorized");
		} else{
			fieldsValidator.httpStatusCode = (long) HttpServletResponse.SC_BAD_REQUEST;
			logger.error(EELFLoggerDelegate.errorLogger, "setResponse failed ",statusCode);

		}
		response.setStatus(fieldsValidator.httpStatusCode.intValue());
		return fieldsValidator;
	}

	/**
	 * REST endpoint to process a request to delete an on-boarded application.
	 * 
	 * @param request
	 *            HTTP servlet request
	 * @param response
	 *            HTTP servlet response
	 * @param appId
	 *            ID of app to delete
	 * @return FieldsValidator
	 */
	@RequestMapping(value = { "/portalApi/onboardingApps/{appId}" }, method = {
			RequestMethod.DELETE }, produces = "application/json")
	public FieldsValidator deleteOnboardingApp(HttpServletRequest request, @PathVariable("appId") Long appId,
			HttpServletResponse response) {
		FieldsValidator fieldsValidator = null;
		try {
			EPUser user = EPUserUtils.getUserSession(request);
			if (!adminRolesService.isSuperAdmin(user)) {
				EcompPortalUtils.setBadPermissions(user, response, "deleteOnboardingApps");
			} else {
				fieldsValidator = appService.deleteOnboardingApp(user, appId);
				response.setStatus(fieldsValidator.httpStatusCode.intValue());
			}
			if (response.getStatus() == 200) {
				logger.info(EELFLoggerDelegate.auditLogger,
						"/portalApi/onboardingApps/" + appId + "deleted by user " + user.getLoginId());
			}
		} catch (Exception e) {
			logger.error(EELFLoggerDelegate.errorLogger, "deleteOnboardingApp failed", e);
			response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
		}
		
		EcompPortalUtils.logAndSerializeObject(logger, PORTAL_API_ONBOARDING_APPS + appId, "DELETE result =",
				response.getStatus());
		return fieldsValidator;
	}

	/**
	 * Gets the application thumbnail image; sets status 404 if none exists.
	 * 
	 * @param request
	 *            HTTP servlet request
	 * @param response
	 *            HTTP servlet response
	 * @param appId
	 *            Application ID
	 * @return Bytes with the app thumbnail image; null if not available.
	 */
	@RequestMapping(value = { "/portalApi/appThumbnail/{appId}" }, method = { RequestMethod.GET })
	public HttpEntity<byte[]> getAppThumbnail(HttpServletRequest request, @PathVariable("appId") Long appId,
			HttpServletResponse response) {
		EPApp app = appService.getApp(appId);
		if (app == null || app.getImageUrl() == null || app.getThumbnail() == null || app.getThumbnail().length == 0) {
			logger.debug(EELFLoggerDelegate.debugLogger,
					"getAppThumbnail: no app and/or no thumbnail for app " + appId);
			response.setStatus(HttpServletResponse.SC_NOT_FOUND);
			return null;
		}
		String url = app.getImageUrl();
		int indexOfDot = url.lastIndexOf('.');
		String urlSuffix = indexOfDot > 0 ? url.substring(indexOfDot + 1).toLowerCase() : "UNK";
		// Default to JPG if no usable suffix.
		MediaType mediaType = MediaType.IMAGE_JPEG;
		if ("png".equals(urlSuffix))
			mediaType = MediaType.IMAGE_PNG;
		else if ("gif".equals(urlSuffix))
			mediaType = MediaType.IMAGE_GIF;
		HttpHeaders header = new HttpHeaders();
		header.setContentType(mediaType);
		header.setContentLength(app.getThumbnail().length);
		return new HttpEntity<>(app.getThumbnail(), header);
	}
	
	private void checkIfNameSpaceIsValid(OnboardingApp modifiedOnboardingApp, FieldsValidator fieldsValidator, HttpServletResponse response)
		throws InvalidApplicationException {
		try {
			ResponseEntity<String> res  = appService.checkIfNameSpaceIsValid(modifiedOnboardingApp.nameSpace);
		} catch (HttpClientErrorException e) {
			logger.error(EELFLoggerDelegate.errorLogger, "checkIfNameSpaceExists failed", e);
			EPLogUtil.logExternalAuthAccessAlarm(logger, e.getStatusCode());
			if (e.getStatusCode() == HttpStatus.NOT_FOUND || e.getStatusCode() == HttpStatus.FORBIDDEN) {
				fieldsValidator = setResponse(e.getStatusCode(),response);
				throw new InvalidApplicationException("Invalid NameSpace");
			}else{
				fieldsValidator = setResponse(e.getStatusCode(),response);
				throw e;
			}
		} catch (Exception e) {
		    logger.error(EELFLoggerDelegate.errorLogger, "Exception in checkIfNameSpaceIsValid", e);
		}
	}

	private boolean isNotNullAndNotValid(Object o){
		return o!=null && !dataValidator.isValid(o);
	}
}