void setParent( DbSession dbSession, QualityProfileKey key, @Nullable QualityProfileKey parentKey) { QualityProfileDto profile = db.qualityProfileDao().getNonNullByKey(dbSession, key); if (parentKey == null) { // unset if parent is defined, else nothing to do removeParent(dbSession, profile); } else if (profile.getParentKey() == null || !profile.getParentKey().equals(parentKey)) { QualityProfileDto parentProfile = db.qualityProfileDao().getNonNullByKey(dbSession, parentKey); if (isDescendant(dbSession, profile, parentProfile)) { throw new BadRequestException( String.format( "Descendant profile '%s' can not be selected as parent of '%s'", parentKey, key)); } removeParent(dbSession, profile); // set new parent profile.setParent(parentKey.name()); db.qualityProfileDao().update(dbSession, profile); for (ActiveRuleDto parentActiveRule : db.activeRuleDao().findByProfileKey(dbSession, parentKey)) { RuleActivation activation = new RuleActivation(ActiveRuleKey.of(key, parentActiveRule.getKey().ruleKey())); activate(dbSession, activation); } } }
boolean isDescendant( DbSession dbSession, QualityProfileDto childProfile, @Nullable QualityProfileDto parentProfile) { QualityProfileDto currentParent = parentProfile; while (currentParent != null) { if (childProfile.getName().equals(currentParent.getName())) { return true; } if (currentParent.getParent() != null) { currentParent = db.qualityProfileDao().getByKey(dbSession, currentParent.getParentKey()); } else { currentParent = null; } } return false; }