001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2008-2011 SonarSource 004 * mailto:contact AT sonarsource DOT com 005 * 006 * Sonar is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 3 of the License, or (at your option) any later version. 010 * 011 * Sonar is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public 017 * License along with Sonar; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 019 */ 020 package org.sonar.server.startup; 021 022 import com.google.common.collect.Sets; 023 import org.sonar.api.database.DatabaseSession; 024 import org.sonar.api.profiles.RulesProfile; 025 import org.sonar.api.resources.Language; 026 import org.sonar.api.utils.TimeProfiler; 027 import org.sonar.jpa.session.DatabaseSessionFactory; 028 029 import javax.persistence.Query; 030 import java.util.Set; 031 032 /** 033 * @since 2.6 034 */ 035 public final class EnableProfiles { 036 037 private Language[] languages; 038 private DatabaseSessionFactory sessionFactory; 039 040 041 public EnableProfiles(Language[] languages, DatabaseSessionFactory sessionFactory, RegisterProvidedProfiles registerProfilesBefore) {// NOSONAR the parameter registerProfilesBefore is used to define the execution order of startup components 042 this.languages = languages; 043 this.sessionFactory = sessionFactory; 044 } 045 046 public void start() { 047 TimeProfiler profiler = new TimeProfiler().start("Enable profiles"); 048 DatabaseSession session = sessionFactory.getSession(); 049 Set<String> languages = getLanguageKeys(); 050 051 enableProfilesOnKnownLanguages(languages, session); 052 disableProfilesOnMissingLanguages(languages, session); 053 054 session.commit(); 055 profiler.stop(); 056 } 057 058 private void enableProfilesOnKnownLanguages(Set<String> languages, DatabaseSession session) { 059 Query query = session.createQuery("update " + RulesProfile.class.getSimpleName() + " set enabled=:enabled where language in (:languages)"); 060 query.setParameter("enabled", Boolean.TRUE); 061 query.setParameter("languages", languages); 062 query.executeUpdate(); 063 } 064 065 private void disableProfilesOnMissingLanguages(Set<String> languages, DatabaseSession session) { 066 Query query = session.createQuery("update " + RulesProfile.class.getSimpleName() + " set enabled=:enabled where language not in (:languages)"); 067 query.setParameter("enabled", Boolean.FALSE); 068 query.setParameter("languages", languages); 069 query.executeUpdate(); 070 } 071 072 private Set<String> getLanguageKeys() { 073 Set<String> keys = Sets.newLinkedHashSet(); 074 for (Language language : languages) { 075 keys.add(language.getKey()); 076 } 077 return keys; 078 } 079 }