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.api.platform;
021
022 import org.sonar.api.BatchComponent;
023 import org.sonar.api.ServerComponent;
024 import org.sonar.api.database.DatabaseSession;
025
026 import javax.persistence.Query;
027 import java.io.File;
028 import java.util.ArrayList;
029 import java.util.List;
030
031 /**
032 * @since 2.2
033 */
034 public class LocalExtensionRegistry implements BatchComponent, ServerComponent {
035
036 private DatabaseSession session;
037
038 public LocalExtensionRegistry(DatabaseSession session) {
039 this.session = session;
040 }
041
042 public List<LocalExtension> getPlugins() {
043 Query query = session.createQuery("FROM " + LocalExtension.class.getSimpleName() + " e WHERE e.type=:type");
044 query.setParameter("type", LocalExtension.Type.PLUGIN);
045 return (List<LocalExtension>) query.getResultList();
046 }
047
048
049 public List<LocalExtension> getExtensions() {
050 Query query = session.createQuery("FROM " + LocalExtension.class.getSimpleName());
051 return (List<LocalExtension>) query.getResultList();
052 }
053
054
055 public List<LocalExtensionFile> getFiles() {
056 Query query = session.createQuery("FROM " + LocalExtensionFile.class.getSimpleName());
057 return query.getResultList();
058 }
059
060 public LocalExtension register(LocalExtension extension, List<File> files) {
061 extension = session.save(extension);
062 Query query = session.createQuery("DELETE FROM " + LocalExtensionFile.class.getSimpleName() + " WHERE extensionId=:id");
063 query.setParameter("id", extension.getId());
064 query.executeUpdate();
065
066 for (File file : files) {
067 LocalExtensionFile localFile = new LocalExtensionFile();
068 localFile.setExtensionId(extension.getId());
069 localFile.setPluginKey(extension.getPluginKey());
070 localFile.setFilename(file.getName());
071 session.save(localFile);
072 }
073
074 session.commit();
075 return extension;
076 }
077
078 public void keep(List<LocalExtension> extensions) {
079 List<Integer> ids = new ArrayList<Integer>();
080 for (LocalExtension extension : extensions) {
081 ids.add(extension.getId());
082 }
083
084 if (!ids.isEmpty()) {
085 Query query = session.createQuery("DELETE " + LocalExtensionFile.class.getSimpleName() + " WHERE extensionId NOT IN (:ids)");
086 query.setParameter("ids", ids);
087 query.executeUpdate();
088
089 query = session.createQuery("DELETE " + LocalExtension.class.getSimpleName() + " WHERE id NOT IN (:ids)");
090 query.setParameter("ids", ids);
091 query.executeUpdate();
092 session.commit();
093 }
094 }
095 }