001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 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 org.sonar.api.database.DatabaseSession;
023 import org.sonar.api.profiles.RulesProfile;
024 import org.sonar.api.resources.Language;
025 import org.sonar.api.utils.Logs;
026 import org.sonar.jpa.session.DatabaseSessionFactory;
027
028 import java.util.Iterator;
029 import java.util.List;
030
031 public final class ActivateDefaultProfiles {
032
033 private final DatabaseSessionFactory sessionFactory;
034 private final Language[] languages;
035
036 public ActivateDefaultProfiles(DatabaseSessionFactory sessionFactory, Language[] languages, RegisterProvidedProfiles registerProfilesBefore) {// NOSONAR the parameter registerProfilesBefore is used to define the execution order of startup components
037 this.sessionFactory = sessionFactory;
038 this.languages = languages;
039 }
040
041 public void start() {
042 DatabaseSession session = sessionFactory.getSession();
043 for (Language language : languages) {
044 Logs.INFO.info("Activate default profile for " + language.getKey());
045 activateDefaultProfile(language, session);
046 }
047 session.commit();
048 }
049
050 public void activateDefaultProfile(Language language, DatabaseSession session) {
051 List<RulesProfile> profiles = session.getResults(RulesProfile.class, "language", language.getKey());
052 RulesProfile profileToActivate = null;
053 boolean oneProfileIsActivated = false;
054 if (profiles.isEmpty()) {
055 profileToActivate = RulesProfile.create("Default " + language.getName(), language.getKey());
056 profileToActivate.setDefaultProfile(true);
057 profileToActivate.setProvided(false);
058 } else if (profiles.size() == 1) {
059 profileToActivate = profiles.get(0);
060 } else if (!activeProfileFoundInDB(profiles)) {
061 Iterator<RulesProfile> iterator = profiles.iterator();
062 while (iterator.hasNext() && !oneProfileIsActivated) {
063 RulesProfile profile = iterator.next();
064 oneProfileIsActivated = profile.getDefaultProfile();
065 if (RulesProfile.SONAR_WAY_NAME.equals(profile.getName())) {
066 profileToActivate = profile;
067 }
068 }
069 if (!oneProfileIsActivated && profileToActivate == null) {
070 profileToActivate = profiles.get(0);
071 }
072 }
073 activateProfileIfNeeded(session, profileToActivate, oneProfileIsActivated);
074 }
075
076 private void activateProfileIfNeeded(DatabaseSession session, RulesProfile profileToActivate, boolean oneProfileIsActivated) {
077 if (!oneProfileIsActivated && profileToActivate != null) {
078 profileToActivate.setDefaultProfile(true);
079 session.saveWithoutFlush(profileToActivate);
080 }
081 }
082
083 private boolean activeProfileFoundInDB(List<RulesProfile> profiles) {
084 for (RulesProfile rulesProfile : profiles) {
085 if (rulesProfile.getDefaultProfile()) {
086 return true;
087 }
088 }
089 return false;
090 }
091 }