+ /**
+ * Retrieves and returns a list of user app roles for local and remote
+ * applications based on the app id.
+ *
+ * @param appId
+ * @return list of user app roles
+ * @throws HTTPException
+ */
+ public List<UserApplicationRoles> getUsersFromAppEndpoint(Long appId) throws HTTPException {
+ ArrayList<UserApplicationRoles> userApplicationRoles = new ArrayList<UserApplicationRoles>();
+
+ EPApp app = appsService.getApp(appId);
+ // If local or centralized application
+ if (appId == PortalConstants.PORTAL_APP_ID || app.getCentralAuth()) {
+ @SuppressWarnings("unchecked")
+ List<EPUser> userList = (List<EPUser>) dataAccessService.executeNamedQuery("getActiveUsers", null, null);
+ for (EPUser user : userList) {
+ UserApplicationRoles userWithAppRoles = convertToUserApplicationRoles(appId, user, app);
+ if (userWithAppRoles.getRoles() != null && userWithAppRoles.getRoles().size() > 0)
+ userApplicationRoles.add(userWithAppRoles);
+ }
+ }
+ // remote app
+ else {
+ RemoteUserWithRoles[] remoteUsers = null;
+ String remoteUsersString = applicationsRestClientService.getIncomingJsonString(appId, "/users");
+ remoteUsers = doGetUsers(isAppUpgradeVersion(app), remoteUsersString);
+ userApplicationRoles = new ArrayList<UserApplicationRoles>();
+ for (RemoteUserWithRoles remoteUser : remoteUsers) {
+ UserApplicationRoles userWithRemoteAppRoles = convertToUserApplicationRoles(appId, remoteUser);
+ if (userWithRemoteAppRoles.getRoles() != null && userWithRemoteAppRoles.getRoles().size() > 0) {
+ userApplicationRoles.add(userWithRemoteAppRoles);
+ } else {
+ logger.debug(EELFLoggerDelegate.debugLogger, "User " + userWithRemoteAppRoles.getOrgUserId()
+ + " doesn't have any roles assigned to any app.");
+ }
+ }
+ }
+ return userApplicationRoles;
+ }
+
+ /**
+ *
+ * @param appId
+ * @param user
+ * @param appgetUsersFromAppEndpoint
+ * @return
+ */
+ private UserApplicationRoles convertToUserApplicationRoles(Long appId, EPUser user, EPApp app) {
+ UserApplicationRoles userWithRemoteAppRoles = new UserApplicationRoles();
+ userWithRemoteAppRoles.setAppId(appId);
+ userWithRemoteAppRoles.setOrgUserId(user.getOrgUserId());
+ userWithRemoteAppRoles.setFirstName(user.getFirstName());
+ userWithRemoteAppRoles.setLastName(user.getLastName());
+ userWithRemoteAppRoles.setRoles(convertToRemoteRoleList(user, app));
+ return userWithRemoteAppRoles;
+ }
+
+ /**
+ *
+ * @param user
+ * @param app
+ * @return
+ */
+ private List<RemoteRole> convertToRemoteRoleList(EPUser user, EPApp app) {
+ List<RemoteRole> roleList = new ArrayList<RemoteRole>();
+ SortedSet<EPRole> roleSet = user.getAppEPRoles(app);
+ for (EPRole role : roleSet) {
+ RemoteRole rRole = new RemoteRole();
+ rRole.setId(role.getId());
+ rRole.setName(role.getName());
+ roleList.add(rRole);
+ }
+ return roleList;
+ }
+
+ /**
+ *
+ * Returns RemoteUserWithRoles reading the value from remote application user in the form a string from an ObjectMapper
+ * @param postOpenSource
+ * @param remoteUsersString
+ * @return RemoteUserWithRoles
+ */
+ public RemoteUserWithRoles[] doGetUsers(boolean postOpenSource, String remoteUsersString) {
+ ObjectMapper mapper = new ObjectMapper();
+ try {
+ return mapper.readValue(remoteUsersString, RemoteUserWithRoles[].class);
+ } catch (IOException e) {
+ logger.error(EELFLoggerDelegate.errorLogger, "doGetUsers : Failed : Unexpected property in incoming JSON",
+ e);
+ logger.error(EELFLoggerDelegate.errorLogger,
+ "doGetUsers : Incoming JSON that caused it --> " + remoteUsersString);
+ }
+
+ return new RemoteUserWithRoles[0];
+ }