org.sonar.server.qualityprofile.db.ActiveRuleDao.java Source code

Java tutorial

Introduction

Here is the source code for org.sonar.server.qualityprofile.db.ActiveRuleDao.java

Source

/*
 * SonarQube, open source software quality management tool.
 * Copyright (C) 2008-2014 SonarSource
 * mailto:contact AT sonarsource DOT com
 *
 * SonarQube is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * SonarQube is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package org.sonar.server.qualityprofile.db;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import org.apache.ibatis.session.ResultContext;
import org.apache.ibatis.session.ResultHandler;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.utils.System2;
import org.sonar.core.persistence.DbSession;
import org.sonar.core.qualityprofile.db.ActiveRuleDto;
import org.sonar.core.qualityprofile.db.ActiveRuleKey;
import org.sonar.core.qualityprofile.db.ActiveRuleMapper;
import org.sonar.core.qualityprofile.db.ActiveRuleParamDto;
import org.sonar.core.qualityprofile.db.QualityProfileDao;
import org.sonar.core.qualityprofile.db.QualityProfileDto;
import org.sonar.core.rule.RuleDto;
import org.sonar.server.db.BaseDao;
import org.sonar.server.rule.db.RuleDao;
import org.sonar.server.search.IndexDefinition;
import org.sonar.server.search.action.IndexAction;
import org.sonar.server.search.action.KeyIndexAction;

import javax.annotation.CheckForNull;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class ActiveRuleDao extends BaseDao<ActiveRuleMapper, ActiveRuleDto, ActiveRuleKey> {

    private static final String QUALITY_PROFILE_IS_NOT_PERSISTED = "Quality profile is not persisted (missing id)";
    private static final String RULE_IS_NOT_PERSISTED = "Rule is not persisted";
    private static final String RULE_PARAM_IS_NOT_PERSISTED = "Rule param is not persisted";
    private static final String ACTIVE_RULE_KEY_CANNOT_BE_NULL = "ActiveRuleKey cannot be null";
    private static final String ACTIVE_RULE_IS_NOT_PERSISTED = "ActiveRule is not persisted";
    private static final String ACTIVE_RULE_IS_ALREADY_PERSISTED = "ActiveRule is already persisted";
    private static final String ACTIVE_RULE_PARAM_IS_NOT_PERSISTED = "ActiveRuleParam is not persisted";
    private static final String ACTIVE_RULE_PARAM_IS_ALREADY_PERSISTED = "ActiveRuleParam is already persisted";
    private static final String PARAMETER_NAME_CANNOT_BE_NULL = "ParameterName cannot be null";

    //TODO remove once key is finalized (used only to get id for SQL statement)
    private final RuleDao ruleDao;
    private final QualityProfileDao profileDao;

    public ActiveRuleDao(QualityProfileDao profileDao, RuleDao ruleDao) {
        this(profileDao, ruleDao, System2.INSTANCE);
    }

    @VisibleForTesting
    public ActiveRuleDao(QualityProfileDao profileDao, RuleDao ruleDao, System2 system) {
        super(IndexDefinition.ACTIVE_RULE, ActiveRuleMapper.class, system);
        this.ruleDao = ruleDao;
        this.profileDao = profileDao;
    }

    @Override
    public void synchronizeAfter(final DbSession session, Date date) {
        session.select("selectAllKeysAfterTimestamp", new Timestamp(date.getTime()), new ResultHandler() {
            @Override
            public void handleResult(ResultContext context) {
                Map<String, Object> fields = (Map<String, Object>) context.getResultObject();
                // "rule" is a reserved keyword in SQLServer, so "rulefield" is used
                ActiveRuleKey key = ActiveRuleKey.of((String) fields.get("profileKey"),
                        RuleKey.of((String) fields.get("repository"), (String) fields.get("rulefield")));
                session.enqueue(new KeyIndexAction<ActiveRuleKey>(getIndexType(), IndexAction.Method.UPSERT, key));
            }
        });
        session.commit();
    }

    /**
     * @deprecated do not use ids but keys
     */
    @CheckForNull
    @Deprecated
    public ActiveRuleDto getById(DbSession session, int activeRuleId) {
        ActiveRuleDto activeRule = mapper(session).selectById(activeRuleId);
        if (activeRule != null) {
            QualityProfileDto profile = profileDao.getById(activeRule.getProfileId(), session);
            RuleDto rule = ruleDao.getById(session, activeRule.getRulId());
            if (profile != null && rule != null) {
                activeRule.setKey(ActiveRuleKey.of(profile.getKey(), rule.getKey()));
                return activeRule;
            }
        }
        return null;
    }

    @Override
    protected ActiveRuleDto doGetNullableByKey(DbSession session, ActiveRuleKey key) {
        return mapper(session).selectByKey(key.qProfile(), key.ruleKey().repository(), key.ruleKey().rule());
    }

    @Override
    protected ActiveRuleDto doInsert(DbSession session, ActiveRuleDto item) {
        Preconditions.checkArgument(item.getProfileId() != null, QUALITY_PROFILE_IS_NOT_PERSISTED);
        Preconditions.checkArgument(item.getRulId() != null, RULE_IS_NOT_PERSISTED);
        Preconditions.checkArgument(item.getId() == null, ACTIVE_RULE_IS_ALREADY_PERSISTED);
        mapper(session).insert(item);
        return item;
    }

    @Override
    protected ActiveRuleDto doUpdate(DbSession session, ActiveRuleDto item) {
        Preconditions.checkArgument(item.getProfileId() != null, QUALITY_PROFILE_IS_NOT_PERSISTED);
        Preconditions.checkArgument(item.getRulId() != null, ActiveRuleDao.RULE_IS_NOT_PERSISTED);
        Preconditions.checkArgument(item.getId() != null, ACTIVE_RULE_IS_NOT_PERSISTED);
        mapper(session).update(item);
        return item;
    }

    @Override
    protected void doDeleteByKey(DbSession session, ActiveRuleKey key) {
        ActiveRuleDto activeRule = getNullableByKey(session, key);
        if (activeRule != null) {
            mapper(session).deleteParameters(activeRule.getId());
            mapper(session).delete(activeRule.getId());
        }
    }

    /**
     * Finder methods for Rules
     */

    public List<ActiveRuleDto> findByRule(DbSession dbSession, RuleDto rule) {
        Preconditions.checkNotNull(rule.getId(), RULE_IS_NOT_PERSISTED);
        return mapper(dbSession).selectByRuleId(rule.getId());
    }

    public List<ActiveRuleDto> findAll(DbSession dbSession) {
        return mapper(dbSession).selectAll();
    }

    public List<ActiveRuleParamDto> findAllParams(DbSession dbSession) {
        return mapper(dbSession).selectAllParams();
    }

    /**
     * Nested DTO ActiveRuleParams
     */

    public ActiveRuleParamDto addParam(DbSession session, ActiveRuleDto activeRule,
            ActiveRuleParamDto activeRuleParam) {
        Preconditions.checkArgument(activeRule.getId() != null, ACTIVE_RULE_IS_NOT_PERSISTED);
        Preconditions.checkArgument(activeRuleParam.getId() == null, ACTIVE_RULE_PARAM_IS_ALREADY_PERSISTED);
        Preconditions.checkNotNull(activeRuleParam.getRulesParameterId(), RULE_PARAM_IS_NOT_PERSISTED);

        activeRuleParam.setActiveRuleId(activeRule.getId());
        mapper(session).insertParameter(activeRuleParam);
        this.enqueueInsert(activeRuleParam, activeRule.getKey(), session);
        return activeRuleParam;
    }

    public void removeParamByKeyAndName(DbSession session, ActiveRuleKey key, String param) {
        //TODO SQL rewrite to delete by key
        ActiveRuleDto activeRule = getNullableByKey(session, key);
        if (activeRule != null) {
            ActiveRuleParamDto activeRuleParam = mapper(session).selectParamByActiveRuleAndKey(activeRule.getId(),
                    param);
            if (activeRuleParam != null) {
                mapper(session).deleteParameter(activeRuleParam.getId());
            }
        }
    }

    public void updateParam(DbSession session, ActiveRuleDto activeRule, ActiveRuleParamDto activeRuleParam) {
        Preconditions.checkNotNull(activeRule.getId(), ACTIVE_RULE_IS_NOT_PERSISTED);
        Preconditions.checkNotNull(activeRuleParam.getId(), ACTIVE_RULE_PARAM_IS_NOT_PERSISTED);
        mapper(session).updateParameter(activeRuleParam);
        this.enqueueUpdate(activeRuleParam, activeRule.getKey(), session);
    }

    public void deleteParam(DbSession session, ActiveRuleDto activeRule, ActiveRuleParamDto activeRuleParam) {
        Preconditions.checkNotNull(activeRule.getId(), ACTIVE_RULE_IS_NOT_PERSISTED);
        Preconditions.checkNotNull(activeRuleParam.getId(), ACTIVE_RULE_PARAM_IS_NOT_PERSISTED);
        mapper(session).deleteParameter(activeRuleParam.getId());
        this.enqueueDelete(activeRuleParam, activeRule.getKey(), session);
    }

    public void deleteByProfileKey(DbSession session, String profileKey) {
        /** Functional cascade for params */
        for (ActiveRuleDto activeRule : findByProfileKey(session, profileKey)) {
            delete(session, activeRule);
        }
    }

    public List<ActiveRuleDto> findByProfileKey(DbSession session, String profileKey) {
        return mapper(session).selectByProfileKey(profileKey);
    }

    /**
     * Finder methods for ActiveRuleParams
     */

    public List<ActiveRuleParamDto> findParamsByActiveRuleKey(DbSession session, ActiveRuleKey key) {
        Preconditions.checkNotNull(key, ACTIVE_RULE_KEY_CANNOT_BE_NULL);
        ActiveRuleDto activeRule = this.getByKey(session, key);
        return mapper(session).selectParamsByActiveRuleId(activeRule.getId());
    }

    @CheckForNull
    public ActiveRuleParamDto getParamByKeyAndName(ActiveRuleKey key, String name, DbSession session) {
        Preconditions.checkNotNull(key, ACTIVE_RULE_KEY_CANNOT_BE_NULL);
        Preconditions.checkNotNull(name, PARAMETER_NAME_CANNOT_BE_NULL);
        ActiveRuleDto activeRule = getNullableByKey(session, key);
        if (activeRule != null) {
            return mapper(session).selectParamByActiveRuleAndKey(activeRule.getId(), name);
        }
        return null;
    }

    public void deleteParamsByRuleParam(DbSession dbSession, RuleDto rule, String paramKey) {
        List<ActiveRuleDto> activeRules = findByRule(dbSession, rule);
        for (ActiveRuleDto activeRule : activeRules) {
            for (ActiveRuleParamDto activeParam : findParamsByActiveRuleKey(dbSession, activeRule.getKey())) {
                if (activeParam.getKey().equals(paramKey)) {
                    deleteParam(dbSession, activeRule, activeParam);
                }
            }
        }
    }
}