/** * Remove a favorite to the user. * @throws IllegalArgumentException if the component is not a favorite */ public void remove(DbSession dbSession, ComponentDto component, @Nullable Integer userId) { if (userId == null) { return; } int result = dbClient.propertiesDao().delete(dbSession, new PropertyDto() .setKey(PROP_FAVORITE_KEY) .setResourceId(component.getId()) .setUserId(userId)); checkArgument(result == 1, "Component '%s' is not a favorite", component.getDbKey()); } }
/** * 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)); }
/** * 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 static PropertyDto newPropertyDto(String key, String value, @Nullable Long componentId, @Nullable Integer userId) { PropertyDto propertyDto = new PropertyDto() .setKey(key) .setValue(value); if (componentId != null) { propertyDto.setResourceId(componentId); } if (userId != null) { propertyDto.setUserId(userId); } return propertyDto; }
private long insertProperty(String key, @Nullable String value, @Nullable Long resourceId, @Nullable Integer userId) { PropertyDto dto = new PropertyDto().setKey(key) .setResourceId(resourceId) .setUserId(userId) .setValue(value); db.properties().insertProperty(dto); return (long) db.selectFirst(session, "select id as \"id\" from properties" + " where prop_key='" + key + "'" + " and user_id" + (userId == null ? " is null" : "='" + userId + "'") + " and resource_id" + (resourceId == null ? " is null" : "='" + resourceId + "'")).get("id"); }
private void insertProperty(String key, @Nullable String value, @Nullable Long resourceId, @Nullable Integer userId) { PropertyDto dto = new PropertyDto().setKey(key) .setResourceId(resourceId) .setUserId(userId) .setValue(value); db.properties().insertProperty(dto); } }
/** * 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)); }
@Test public void testToString() { assertThat(new PropertyDto().setKey("foo:bar").setValue("value").setResourceId(123L).setUserId(456).toString()).isEqualTo("PropertyDto{foo:bar, value, 123, 456}"); }
@Test @UseDataProvider("valueUpdatesDataProvider") public void saveProperty_deletes_then_inserts_user_properties_when_they_exist_in_db(@Nullable String oldValue, @Nullable String newValue) throws SQLException { int userId = 90; long id = insertProperty("global", oldValue, null, userId, DATE_1); when(system2.now()).thenReturn(DATE_4); underTest.saveProperty(new PropertyDto().setKey("global").setUserId(userId).setValue(newValue)); assertThatPropertiesRow(id) .doesNotExist(); PropertiesRowAssert propertiesRowAssert = assertThatPropertiesRow("global") .hasNoResourceId() .hasUserId(userId) .hasCreatedAt(DATE_4); if (newValue == null || newValue.isEmpty()) { propertiesRowAssert.isEmpty(); } else if (newValue.length() > 4000) { propertiesRowAssert.hasClobValue(newValue); } else { propertiesRowAssert.hasTextValue(newValue); } }
public void add(ComponentDto componentDto, int userId) { dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto() .setKey(PROP_FAVORITE_KEY) .setUserId(userId) .setResourceId(componentDto.getId())); dbSession.commit(); }
@Test public void user_setting_is_not_updated() { propertyDb.insertProperty(newGlobalPropertyDto("my.key", "my user value").setUserId(42)); propertyDb.insertProperty(newGlobalPropertyDto("my.key", "my global value")); callForGlobalSetting("my.key", "my new global value"); assertGlobalSetting("my.key", "my new global value"); assertUserSetting("my.key", "my user value", 42); }
private void addFavourite(ComponentDto project) { dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto().setKey("favourite").setResourceId(project.getId()).setUserId(userSession.getUserId())); dbSession.commit(); }
underTest.saveProperty(new PropertyDto().setKey("user.null").setUserId(userId).setValue(null)); underTest.saveProperty(new PropertyDto().setKey("user.empty").setUserId(userId).setValue("")); underTest.saveProperty(new PropertyDto().setKey("user.text").setUserId(userId).setValue("some text")); underTest.saveProperty(new PropertyDto().setKey("user.4000").setUserId(userId).setValue(VALUE_SIZE_4000)); underTest.saveProperty(new PropertyDto().setKey("user.clob").setUserId(userId).setValue(VALUE_SIZE_4001));
@Test public void return_component_info_with_favourite() { ComponentDto project = insertOrganizationAndProject(); UserDto user = db.users().insertUser("obiwan"); propertyDbTester.insertProperty(new PropertyDto().setKey("favourite").setResourceId(project.getId()).setUserId(user.getId())); userSession.logIn(user).addProjectPermission(UserRole.USER, project); init(); executeAndVerify(project.getDbKey(), "return_component_info_with_favourite.json"); }
@Test public void test_example_response() { init(createPages()); OrganizationDto organizationDto = db.organizations().insertForKey("my-org-1"); ComponentDto project = newPrivateProjectDto(organizationDto, "ABCD") .setDbKey("org.codehaus.sonar:sonar") .setName("Sonarqube") .setDescription("Open source platform for continuous inspection of code quality"); componentDbTester.insertComponent(project); SnapshotDto analysis = newAnalysis(project) .setCreatedAt(parseDateTime("2016-12-06T11:44:00+0200").getTime()) .setVersion("6.3") .setLast(true); componentDbTester.insertSnapshot(analysis); when(resourceTypes.get(project.qualifier())).thenReturn(DefaultResourceTypes.get().getRootType()); UserDto user = db.users().insertUser("obiwan"); propertyDbTester.insertProperty(new PropertyDto().setKey("favourite").setResourceId(project.getId()).setUserId(user.getId())); addQualityProfiles(project, createQProfile("qp1", "Sonar Way Java", "java"), createQProfile("qp2", "Sonar Way Xoo", "xoo")); QualityGateDto qualityGateDto = db.qualityGates().insertQualityGate(db.getDefaultOrganization(), qg -> qg.setName("Sonar way")); db.qualityGates().associateProjectToQualityGate(project, qualityGateDto); userSession.logIn(user) .addProjectPermission(UserRole.USER, project) .addProjectPermission(UserRole.ADMIN, project); String result = execute(project.getDbKey()); assertJson(result).ignoreFields("snapshotDate", "key", "qualityGate.key").isSimilarTo(ws.getDef().responseExampleAsString()); }
private static PropertyDto newPropertyDto(String key, String value, @Nullable Long componentId, @Nullable Long userId) { PropertyDto propertyDto = new PropertyDto() .setKey(key) .setValue(value); if (componentId != null) { propertyDto.setResourceId(componentId); } if (userId != null) { propertyDto.setUserId(userId); } return propertyDto; }
/** * 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)); }
/** * 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)); }
/** * Remove a favorite to the user. * @throws BadRequestException if the component is not a favorite */ public void remove(DbSession dbSession, ComponentDto component, @Nullable Integer userId) { if (userId == null) { return; } int result = dbClient.propertiesDao().delete(dbSession, new PropertyDto() .setKey(PROP_FAVORITE_KEY) .setResourceId(component.getId()) .setUserId(userId)); checkRequest(result == 1, "Component '%s' is not a favorite", component.getDbKey()); } }
/** * 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); checkRequest(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)); }