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 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 = new RulesProfile("Default " + language.getName(), language.getKey(), true, false);
056    
057        } else if (profiles.size() == 1) {
058          profileToActivate = profiles.get(0);
059    
060        } else {
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) {
070            if (profileToActivate == null) {
071              profileToActivate = profiles.get(0);
072            }
073          }
074        }
075        if (!oneProfileIsActivated && profileToActivate != null) {
076          profileToActivate.setDefaultProfile(true);
077          session.saveWithoutFlush(profileToActivate);
078        }
079      }
080    }