001/*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2013 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube 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 * SonarQube 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 License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019 */
020package org.sonar.api.database.configuration;
021
022import org.apache.commons.configuration.BaseConfiguration;
023import org.sonar.api.database.DatabaseSession;
024import org.sonar.jpa.session.DatabaseSessionFactory;
025
026import java.util.List;
027
028/**
029 * IMPORTANT : This class can't be moved to org.sonar.jpa.dao for backward-compatibility reasons.
030 * This class is still used in some plugins.
031 *
032 * @since 1.10
033 * @deprecated in 3.7. Replaced by {@link org.sonar.api.config.Settings}
034 */
035@Deprecated
036public class DatabaseConfiguration extends BaseConfiguration {
037  private DatabaseSessionFactory sessionFactory;
038  private DatabaseSession session;
039
040  public DatabaseConfiguration(DatabaseSessionFactory sessionFactory) {
041    this.sessionFactory = sessionFactory;
042    load();
043  }
044
045  public DatabaseConfiguration(DatabaseSession session) {
046    this.session = session;
047    load();
048  }
049
050  public final void load() {
051    clear();
052
053    // Ugly workaround before the move to myBatis
054    // Session is not up-to-date when Ruby on Rails inserts new rows in its own transaction. Seems like
055    // Hibernate keeps a cache...
056    getSession().commit();
057
058    List<Property> properties = getSession()
059      .createQuery("from " + Property.class.getSimpleName() + " p where p.resourceId is null and p.userId is null")
060      .getResultList();
061
062    if (properties != null) {
063      for (Property property : properties) {
064        setProperty(property.getKey(), property.getValue());
065      }
066    }
067  }
068
069  private DatabaseSession getSession() {
070    if (session != null) {
071      return session;
072    }
073    return sessionFactory.getSession();
074  }
075}