private static Predicate<PropertyDto> notificationScope(@Nullable ComponentDto project) { return prop -> project == null ? (prop.getResourceId() == null) : (prop.getResourceId() != null); } }
private static List<PropertyDto> getPropertySets(String propertyKey, List<PropertyDto> propertySets, @Nullable Long componentId) { return propertySets.stream() .filter(propertyDto -> Objects.equals(propertyDto.getResourceId(), componentId)) .filter(propertyDto -> propertyDto.getKey().startsWith(propertyKey + ".")) .collect(Collectors.toList()); }
private boolean isDispatcherAuthorized(PropertyDto prop, String dispatcher) { return (prop.getResourceId() != null && dispatchers.getProjectDispatchers().contains(dispatcher)) || dispatchers.getGlobalDispatchers().contains(dispatcher); }
private static Function<PropertyDto, Notification> toWsNotification(Notification.Builder notification, Map<String, OrganizationDto> organizationsByUuid, Map<Long, ComponentDto> projectsById) { return property -> { notification.clear(); List<String> propertyKey = Splitter.on(".").splitToList(property.getKey()); notification.setType(propertyKey.get(1)); notification.setChannel(propertyKey.get(2)); ofNullable(property.getResourceId()).ifPresent(componentId -> populateProjectFields(notification, componentId, organizationsByUuid, projectsById)); return notification.build(); }; }
private Setting(PropertyDto propertyDto, List<PropertyDto> propertyDtoSetValues, @Nullable PropertyDefinition definition) { this.key = propertyDto.getKey(); this.value = propertyDto.getValue(); this.componentId = propertyDto.getResourceId(); this.definition = definition; this.propertySets = buildPropertySetValuesAsMap(key, propertyDtoSetValues); this.isDefault = false; }
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 PropertyDtoAssert hasNoResourceId() { isNotNull(); if (actual.getResourceId() != null) { failWithMessage("Expected PropertyDto to have resourceId to be null but was <%s>", actual.getResourceId()); } return this; }
/** * Return list of propertyDto by component uuid, sorted from project to lowest module */ private Multimap<String, PropertyDto> loadComponentSettings(DbSession dbSession, Optional<String> key, ComponentDto component) { List<String> componentUuids = DOT_SPLITTER.splitToList(component.moduleUuidPath()); List<ComponentDto> componentDtos = dbClient.componentDao().selectByUuids(dbSession, componentUuids); Set<Long> componentIds = componentDtos.stream().map(ComponentDto::getId).collect(Collectors.toSet()); Map<Long, String> uuidsById = componentDtos.stream().collect(Collectors.toMap(ComponentDto::getId, ComponentDto::uuid)); List<PropertyDto> properties = key.isPresent() ? dbClient.propertiesDao().selectPropertiesByKeysAndComponentIds(dbSession, Collections.singleton(key.get()), componentIds) : dbClient.propertiesDao().selectPropertiesByComponentIds(dbSession, componentIds); Multimap<String, PropertyDto> propertyDtosByUuid = TreeMultimap.create(Ordering.explicit(componentUuids), Ordering.arbitrary()); for (PropertyDto propertyDto : properties) { Long componentId = propertyDto.getResourceId(); String componentUuid = uuidsById.get(componentId); propertyDtosByUuid.put(componentUuid, propertyDto); } return propertyDtosByUuid; }
public PropertyDtoAssert hasResourceId(long expected) { isNotNull(); if (!Objects.equals(actual.getResourceId(), expected)) { failWithMessage("Expected PropertyDto to have resourceId to be <%s> but was <%s>", true, actual.getResourceId()); } return this; }
/** * Return list of settings by component uuid, sorted from project to lowest module */ private Multimap<String, Setting> loadComponentSettings(DbSession dbSession, Set<String> keys, ComponentDto component) { List<String> componentUuids = DOT_SPLITTER.splitToList(component.moduleUuidPath()); List<ComponentDto> componentDtos = dbClient.componentDao().selectByUuids(dbSession, componentUuids); Set<Long> componentIds = componentDtos.stream().map(ComponentDto::getId).collect(Collectors.toSet()); Map<Long, String> uuidsById = componentDtos.stream().collect(Collectors.toMap(ComponentDto::getId, ComponentDto::uuid)); List<PropertyDto> properties = dbClient.propertiesDao().selectPropertiesByKeysAndComponentIds(dbSession, keys, componentIds); List<PropertyDto> propertySets = dbClient.propertiesDao().selectPropertiesByKeysAndComponentIds(dbSession, getPropertySetKeys(properties), componentIds); Multimap<String, Setting> settingsByUuid = TreeMultimap.create(Ordering.explicit(componentUuids), Ordering.arbitrary()); for (PropertyDto propertyDto : properties) { Long componentId = propertyDto.getResourceId(); String componentUuid = uuidsById.get(componentId); String propertyKey = propertyDto.getKey(); settingsByUuid.put(componentUuid, Setting.createFromDto(propertyDto, getPropertySets(propertyKey, propertySets, componentId), propertyDefinitions.get(propertyKey))); } return settingsByUuid; }
public int delete(DbSession dbSession, PropertyDto dto) { return getMapper(dbSession).delete(dto.getKey(), dto.getUserId(), dto.getResourceId()); }
/** * Saves the specified property and its value. * <p> * If {@link PropertyDto#getValue()} is {@code null} or empty, the properties is persisted as empty. * </p> * * @throws IllegalArgumentException if {@link PropertyDto#getKey()} is {@code null} or empty */ public void saveProperty(DbSession session, PropertyDto property) { save(getMapper(session), property.getKey(), property.getUserId(), property.getResourceId(), property.getValue()); }
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"); }
private static List<PropertyDto> getPropertySets(String propertyKey, List<PropertyDto> propertySets, @Nullable Long componentId) { return propertySets.stream() .filter(propertyDto -> Objects.equals(propertyDto.getResourceId(), componentId)) .filter(propertyDto -> propertyDto.getKey().startsWith(propertyKey + ".")) .collect(Collectors.toList()); }
private boolean isDispatcherAuthorized(PropertyDto prop, String dispatcher) { return (prop.getResourceId() != null && dispatchers.getProjectDispatchers().contains(dispatcher)) || dispatchers.getGlobalDispatchers().contains(dispatcher); }
private TreeModuleSettings(DbSession session, List<ComponentDto> moduleChildren, List<PropertyDto> moduleChildrenSettings) { modulesByKey = moduleChildren.stream().collect(uniqueIndex(ComponentDto::getKey)); moduleChildrenByModuleUuid = ArrayListMultimap.create(); Set<Long> propertiesByComponentId = moduleChildrenSettings.stream().map(PropertyDto::getResourceId).collect(MoreCollectors.toSet()); Map<Long, ComponentDto> componentsById = dbClient.componentDao().selectByIds(session, propertiesByComponentId).stream().collect(uniqueIndex(ComponentDto::getId)); propertiesByModuleKey = moduleChildrenSettings.stream().collect(index(s -> componentsById.get(s.getResourceId()).getKey())); moduleChildrenByModuleUuid = moduleChildren.stream().filter(c -> c.moduleUuid() != null).collect(index(ComponentDto::moduleUuid)); }
private Setting(PropertyDto propertyDto, List<PropertyDto> propertyDtoSetValues, @Nullable PropertyDefinition definition) { this.key = propertyDto.getKey(); this.value = propertyDto.getValue(); this.componentId = propertyDto.getResourceId(); this.definition = definition; this.propertySets = buildPropertySetValuesAsMap(key, propertyDtoSetValues); this.isDefault = false; }
private static Function<PropertyDto, Notification> toWsNotification(Notification.Builder notification, Map<String, OrganizationDto> organizationsByUuid, Map<Long, ComponentDto> projectsById) { return property -> { notification.clear(); List<String> propertyKey = Splitter.on(".").splitToList(property.getKey()); notification.setType(propertyKey.get(1)); notification.setChannel(propertyKey.get(2)); setNullable(property.getResourceId(), componentId -> populateProjectFields(notification, componentId, organizationsByUuid, projectsById)); return notification.build(); }; }
public int delete(DbSession dbSession, PropertyDto dto) { return getMapper(dbSession).delete(dto.getKey(), dto.getUserId(), dto.getResourceId()); }
/** * Saves the specified property and its value. * <p> * If {@link PropertyDto#getValue()} is {@code null} or empty, the properties is persisted as empty. * </p> * * @throws IllegalArgumentException if {@link PropertyDto#getKey()} is {@code null} or empty */ public void saveProperty(DbSession session, PropertyDto property) { save(getMapper(session), property.getKey(), property.getUserId(), property.getResourceId(), property.getValue()); }