001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2009 SonarSource SA
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.core.plugin;
021    
022    import org.sonar.api.BatchComponent;
023    import org.sonar.api.ServerComponent;
024    import org.sonar.api.database.DatabaseSession;
025    import org.sonar.jpa.session.DatabaseSessionFactory;
026    
027    import javax.persistence.Query;
028    import java.util.ArrayList;
029    import java.util.List;
030    
031    /**
032     * @since 2.2
033     */
034    public class JpaPluginDao implements BatchComponent, ServerComponent {
035    
036      private DatabaseSessionFactory sessionFactory;
037    
038      public JpaPluginDao(DatabaseSessionFactory sessionFactory) {
039        this.sessionFactory = sessionFactory;
040      }
041    
042      public List<JpaPlugin> getPlugins() {
043        DatabaseSession session = sessionFactory.getSession();
044        Query query = session.createQuery("FROM " + JpaPlugin.class.getSimpleName());
045        return (List<JpaPlugin>) query.getResultList();
046      }
047    
048      public List<JpaPluginFile> getPluginFiles() {
049        DatabaseSession session = sessionFactory.getSession();
050        Query query = session.createQuery("FROM " + JpaPluginFile.class.getSimpleName());
051        return (List<JpaPluginFile>) query.getResultList();
052      }
053    
054      public void register(List<JpaPlugin> plugins) {
055        DatabaseSession session = sessionFactory.getSession();
056        List<Integer> ids = new ArrayList<Integer>();
057        for (JpaPlugin plugin : plugins) {
058          session.saveWithoutFlush(plugin);
059          ids.add(plugin.getId());
060        }
061        session.commit();
062    
063        if (ids.isEmpty()) {
064          session.createQuery("DELETE " + JpaPluginFile.class.getSimpleName()).executeUpdate();
065          session.createQuery("DELETE " + JpaPlugin.class.getSimpleName()).executeUpdate();
066    
067        } else {
068          Query query = session.createQuery("DELETE " + JpaPluginFile.class.getSimpleName() + " WHERE plugin.id NOT IN (:ids)");
069          query.setParameter("ids", ids);
070          query.executeUpdate();
071    
072          query = session.createQuery("DELETE " + JpaPlugin.class.getSimpleName() + " WHERE id NOT IN (:ids)");
073          query.setParameter("ids", ids);
074          query.executeUpdate();
075    
076        }
077        session.commit();
078      }
079    }