2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the "License");
10 * you may not use this software except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
38 package org.onap.portalapp.portal.service;
40 import java.util.ArrayList;
41 import java.util.HashMap;
42 import java.util.LinkedHashMap;
43 import java.util.List;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.context.annotation.EnableAspectJAutoProxy;
48 import org.springframework.stereotype.Service;
49 import org.springframework.transaction.annotation.Transactional;
50 import org.hibernate.criterion.Criterion;
51 import org.hibernate.criterion.Restrictions;
52 import org.onap.portalapp.portal.domain.EPApp;
53 import org.onap.portalapp.portal.domain.EPUser;
54 import org.onap.portalapp.portal.domain.EPUserApp;
55 import org.onap.portalapp.portal.domain.PersUserAppSelection;
56 import org.onap.portalapp.portal.domain.PersUserWidgetSelection;
57 import org.onap.portalapp.portal.logging.aop.EPMetricsLog;
58 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
59 import org.onap.portalsdk.core.service.DataAccessService;
61 @Service("persUserAppService")
63 @org.springframework.context.annotation.Configuration
64 @EnableAspectJAutoProxy
66 public class PersUserAppServiceImpl implements PersUserAppService {
68 private static final String APP_ID = "appId";
70 private static final String USER_ID = "userId";
72 private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PersUserAppServiceImpl.class);
75 private DataAccessService dataAccessService;
77 private AdminRolesService adminRolesService;
79 private UserRolesService userRolesService;
84 * @see org.openecomp.portalapp.portal.service.UserAppSelectService#
85 * setAppCatalogSelection(org.openecomp.portalapp.portal.domain.EPUser,
86 * org.openecomp.portalapp.portal.transport.AppCatalogSelection)
89 public void setPersUserAppValue(EPUser user, EPApp app, boolean select, boolean pending) {
90 if (user == null || app == null)
91 throw new IllegalArgumentException("setPersUserAppValue: Null values");
93 // Find the record for this user-app combo, if any
94 List<PersUserAppSelection> persList = getUsersAppSelection(user, app);
96 // Key constraint limits to 1 row
97 PersUserAppSelection persRow = null;
98 if (persList.size() == 1)
99 persRow = persList.get(0);
101 persRow = new PersUserAppSelection(null, user.getId(), app.getId(), null);
104 // Pending status is not meaningful for open apps.
106 logger.error(EELFLoggerDelegate.errorLogger,
107 "setPersUserAppValue: invalid request, ignoring set-pending for open app");
109 // Open apps have same behavior for regular and admin users
111 // Selection of an open app requires a record
112 persRow.setStatusCode("S"); // show
113 dataAccessService.saveDomainObject(persRow, null);
115 // De-selection of an open app requires no record
116 if (persRow.getId() != null)
117 dataAccessService.deleteDomainObject(persRow, null);
122 // Pending overrides select.
124 persRow.setStatusCode("P");
125 dataAccessService.saveDomainObject(persRow, null);
127 // Behavior depends on Portal (super) admin status, bcos an
128 // admin can force an app onto the dashboard.
129 boolean isPortalAdmin = adminRolesService.isSuperAdmin(user);
130 boolean adminUserHasAppRole = false;
132 List<EPUserApp> roles = userRolesService.getCachedAppRolesForUser(app.getId(), user.getId());
133 adminUserHasAppRole = (roles.size() > 0);
134 logger.debug(EELFLoggerDelegate.debugLogger, "setPersUserAppValue: app {}, admin user {}, role count {}",
135 app.getId(), user.getId(), roles.size());
140 // The special case: portal admin
141 persRow.setStatusCode("S"); // show
142 dataAccessService.saveDomainObject(persRow, null);
144 // User has role-based access to the app.
145 // Showing an accessible app requires no record.
146 if (persRow.getId() != null)
147 dataAccessService.deleteDomainObject(persRow, null);
151 if (isPortalAdmin && !adminUserHasAppRole) {
152 // The special case: portal admin, no role
153 if (persRow.getId() != null)
154 dataAccessService.deleteDomainObject(persRow, null);
156 // User has role-based access to the app.
157 // Hiding an accessible app requires a record
158 persRow.setStatusCode("H"); // hide
159 dataAccessService.saveDomainObject(persRow, null);
166 @SuppressWarnings("unchecked")
167 private List<PersUserAppSelection> getUsersAppSelection(EPUser user, EPApp app) {
168 List<Criterion> restrictionsList = new ArrayList<Criterion>();
169 Criterion userIdCriterion = Restrictions.eq("userId", user.getId());
170 Criterion appIdCriterion = Restrictions.eq("appId", app.getId());
171 restrictionsList.add(Restrictions.and(userIdCriterion, appIdCriterion));
172 return (List<PersUserAppSelection>) dataAccessService.getList(PersUserAppSelection.class, null, restrictionsList, null);