private Set<String> loadFavoriteProjectUuids(DbSession dbSession) { if (!userSession.isLoggedIn()) { return Collections.emptySet(); } List<PropertyDto> props = dbClient.propertiesDao().selectByQuery( PropertyQuery.builder() .setUserId(userSession.getUserId()) .setKey("favourite") .build(), dbSession); List<Long> favoriteDbIds = props.stream() .map(PropertyDto::getResourceId) .collect(MoreCollectors.toList(props.size())); return dbClient.componentDao().selectByIds(dbSession, favoriteDbIds).stream() .filter(ComponentDto::isEnabled) .filter(f -> f.qualifier().equals(Qualifiers.PROJECT)) .map(ComponentDto::uuid) .collect(MoreCollectors.toSet()); }
public PropertyQuery build() { return new PropertyQuery(this); } }
private UnaryOperator<ListResponse.Builder> addNotifications(DbSession dbSession, UserDto user) { return response -> { List<PropertyDto> properties = dbClient.propertiesDao().selectByQuery(PropertyQuery.builder().setUserId(user.getId()).build(), dbSession); Map<Long, ComponentDto> componentsById = searchProjects(dbSession, properties); Map<String, OrganizationDto> organizationsByUuid = getOrganizations(dbSession, componentsById.values()); Predicate<PropertyDto> isNotification = prop -> prop.getKey().startsWith("notification."); Predicate<PropertyDto> isComponentInDb = prop -> prop.getResourceId() == null || componentsById.containsKey(prop.getResourceId()); Notification.Builder notification = Notification.newBuilder(); properties.stream() .filter(isNotification) .filter(channelAndDispatcherAuthorized()) .filter(isComponentInDb) .map(toWsNotification(notification, organizationsByUuid, componentsById)) .sorted(comparing(Notification::getProject, nullsFirst(naturalOrder())) .thenComparing(comparing(Notification::getChannel)) .thenComparing(comparing(Notification::getType))) .forEach(response::addNotifications); return response; }; }
public PropertyQuery build() { return new PropertyQuery(this); } }
/** * @return the list of favorite components of the authenticated user. Empty list if the user is not authenticated */ public List<ComponentDto> list() { if (!userSession.isLoggedIn()) { return emptyList(); } try (DbSession dbSession = dbClient.openSession(false)) { PropertyQuery dbQuery = PropertyQuery.builder() .setKey(PROP_FAVORITE_KEY) .setUserId(userSession.getUserId()) .build(); Set<Long> componentIds = dbClient.propertiesDao().selectByQuery(dbQuery, dbSession).stream().map(PropertyDto::getResourceId).collect(Collectors.toSet()); return dbClient.componentDao().selectByIds(dbSession, componentIds).stream() .sorted(Comparator.comparing(ComponentDto::name)) .collect(toList()); } } }
/** * Add a notification to a user. */ public void add(DbSession dbSession, String channel, String dispatcher, UserDto user, @Nullable ComponentDto project) { String key = String.join(".", PROP_NOTIFICATION_PREFIX, dispatcher, channel); Long projectId = project == null ? null : project.getId(); List<PropertyDto> existingNotification = dbClient.propertiesDao().selectByQuery( PropertyQuery.builder() .setKey(key) .setComponentId(projectId) .setUserId(user.getId()) .build(), dbSession).stream() .filter(notificationScope(project)) .collect(MoreCollectors.toList()); checkArgument(existingNotification.isEmpty() || !PROP_NOTIFICATION_VALUE.equals(existingNotification.get(0).getValue()), "Notification already added"); dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto() .setKey(key) .setUserId(user.getId()) .setValue(PROP_NOTIFICATION_VALUE) .setResourceId(projectId)); }
private boolean isFavourite(DbSession session, ComponentDto component) { PropertyQuery propertyQuery = PropertyQuery.builder() .setUserId(userSession.getUserId()) .setKey("favourite") .setComponentId(component.getId()) .build(); List<PropertyDto> componentFavourites = dbClient.propertiesDao().selectByQuery(propertyQuery, session); return componentFavourites.size() == 1; }
/** * Remove a notification from a user. */ public void remove(DbSession dbSession, String channel, String dispatcher, UserDto user, @Nullable ComponentDto project) { String key = String.join(".", PROP_NOTIFICATION_PREFIX, dispatcher, channel); Long projectId = project == null ? null : project.getId(); List<PropertyDto> existingNotification = dbClient.propertiesDao().selectByQuery( PropertyQuery.builder() .setKey(key) .setComponentId(projectId) .setUserId(user.getId()) .build(), dbSession).stream() .filter(notificationScope(project)) .collect(MoreCollectors.toList()); checkArgument(!existingNotification.isEmpty() && PROP_NOTIFICATION_VALUE.equals(existingNotification.get(0).getValue()), "Notification doesn't exist"); dbClient.propertiesDao().delete(dbSession, new PropertyDto() .setKey(key) .setUserId(user.getId()) .setValue(PROP_NOTIFICATION_VALUE) .setResourceId(projectId)); }
public void assertExists(String channel, String dispatcher, int userId, @Nullable ComponentDto component) { List<PropertyDto> result = dbClient.propertiesDao().selectByQuery(PropertyQuery.builder() .setKey(String.join(".", PROP_NOTIFICATION_PREFIX, dispatcher, channel)) .setComponentId(component == null ? null : component.getId()) .setUserId(userId) .build(), dbSession).stream() .filter(prop -> component == null ? prop.getResourceId() == null : prop.getResourceId() != null) .collect(MoreCollectors.toList()); assertThat(result).hasSize(1); assertThat(result.get(0).getValue()).isEqualTo("true"); }
public void assertDoesNotExist(String channel, String dispatcher, int userId, @Nullable ComponentDto component) { List<PropertyDto> result = dbClient.propertiesDao().selectByQuery(PropertyQuery.builder() .setKey(String.join(".", PROP_NOTIFICATION_PREFIX, dispatcher, channel)) .setComponentId(component == null ? null : component.getId()) .setUserId(userId) .build(), dbSession); assertThat(result).isEmpty(); } }
private void assertFavorite() { assertThat(dbClient.propertiesDao().selectByQuery(PropertyQuery.builder() .setUserId(USER_ID) .setComponentId(COMPONENT_ID) .build(), dbSession)).hasSize(1); }
private void assertNoFavorite() { assertThat(dbClient.propertiesDao().selectByQuery(PropertyQuery.builder() .setUserId(USER_ID) .setComponentId(COMPONENT_ID) .build(), dbSession)).isEmpty(); } }
/** * Set favorite to the logged in user. If no user, no action is done */ public void add(DbSession dbSession, ComponentDto componentDto, @Nullable Integer userId) { if (userId == null) { return; } List<PropertyDto> existingFavoriteOnComponent = dbClient.propertiesDao().selectByQuery(PropertyQuery.builder() .setKey(PROP_FAVORITE_KEY) .setUserId(userId) .setComponentId(componentDto.getId()) .build(), dbSession); checkArgument(existingFavoriteOnComponent.isEmpty(), "Component '%s' is already a favorite", componentDto.getDbKey()); dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto() .setKey(PROP_FAVORITE_KEY) .setResourceId(componentDto.getId()) .setUserId(userId)); }
private void assertProjectPropertyDoesNotExist(ComponentDto component, String key) { assertThat(dbClient.propertiesDao().selectByQuery(PropertyQuery.builder().setComponentId(component.getId()).setKey(key).build(), dbSession)).isEmpty(); }
private void assertProjectPropertyDoesNotExist(String key) { assertThat(dbClient.propertiesDao().selectByQuery(PropertyQuery.builder().setComponentId(project.getId()).setKey(key).build(), dbSession)).isEmpty(); }
public boolean hasNoFavorite(ComponentDto componentDto) { List<PropertyDto> result = dbClient.propertiesDao().selectByQuery(PropertyQuery.builder() .setKey(PROP_FAVORITE_KEY) .setComponentId(componentDto.getId()) .build(), dbSession); return result.isEmpty(); } }
@Test public void add_a_project() { ComponentDto project = insertProjectAndPermissions(); TestResponse result = call(PROJECT_KEY); assertThat(result.getStatus()).isEqualTo(HTTP_NO_CONTENT); List<PropertyDto> favorites = dbClient.propertiesDao().selectByQuery(PropertyQuery.builder() .setUserId(USER_ID) .setKey("favourite") .build(), dbSession); assertThat(favorites).hasSize(1); PropertyDto favorite = favorites.get(0); assertThat(favorite) .extracting(PropertyDto::getResourceId, PropertyDto::getUserId, PropertyDto::getKey) .containsOnly(project.getId(), USER_ID, "favourite"); }
private void assertUserSetting(String key, String value, int userId) { List<PropertyDto> result = dbClient.propertiesDao().selectByQuery(PropertyQuery.builder().setKey(key).setUserId(userId).build(), dbSession); assertThat(result).hasSize(1) .extracting(PropertyDto::getKey, PropertyDto::getValue, PropertyDto::getUserId) .containsExactly(tuple(key, value, userId)); }
public boolean hasFavorite(ComponentDto componentDto, int userId) { List<PropertyDto> result = dbClient.propertiesDao().selectByQuery(PropertyQuery.builder() .setKey(PROP_FAVORITE_KEY) .setComponentId(componentDto.getId()) .setUserId(userId) .build(), dbSession); return !result.isEmpty(); }
private void assertUserPropertyExists(String key, UserDto user) { assertThat(dbClient.propertiesDao().selectByQuery(PropertyQuery.builder() .setKey(key) .setUserId(user.getId()) .build(), dbSession)).isNotEmpty(); }