org.sonar.api.platform.LocalExtensionRegistry Maven / Gradle / Ivy
/*
* Sonar, open source software quality management tool.
* Copyright (C) 2009 SonarSource SA
* mailto:contact AT sonarsource DOT com
*
* Sonar is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* Sonar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package org.sonar.api.platform;
import org.sonar.api.BatchComponent;
import org.sonar.api.ServerComponent;
import org.sonar.api.database.DatabaseSession;
import javax.persistence.Query;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* @since 2.2
*/
public class LocalExtensionRegistry implements BatchComponent, ServerComponent {
private DatabaseSession session;
public LocalExtensionRegistry(DatabaseSession session) {
this.session = session;
}
public List getPlugins() {
Query query = session.createQuery("FROM " + LocalExtension.class.getSimpleName() + " e WHERE e.type=:type");
query.setParameter("type", LocalExtension.Type.PLUGIN);
return (List) query.getResultList();
}
public List getExtensions() {
Query query = session.createQuery("FROM " + LocalExtension.class.getSimpleName());
return (List) query.getResultList();
}
public List getFiles() {
Query query = session.createQuery("FROM " + LocalExtensionFile.class.getSimpleName());
return query.getResultList();
}
public LocalExtension register(LocalExtension extension, List files) {
extension = session.save(extension);
Query query = session.createQuery("DELETE FROM " + LocalExtensionFile.class.getSimpleName() + " WHERE extensionId=:id");
query.setParameter("id", extension.getId());
query.executeUpdate();
for (File file : files) {
LocalExtensionFile localFile = new LocalExtensionFile();
localFile.setExtensionId(extension.getId());
localFile.setPluginKey(extension.getPluginKey());
localFile.setFilename(file.getName());
session.save(localFile);
}
session.commit();
return extension;
}
public void keep(List extensions) {
List ids = new ArrayList();
for (LocalExtension extension : extensions) {
ids.add(extension.getId());
}
if (!ids.isEmpty()) {
Query query = session.createQuery("DELETE " + LocalExtensionFile.class.getSimpleName() + " WHERE extensionId NOT IN (:ids)");
query.setParameter("ids", ids);
query.executeUpdate();
query = session.createQuery("DELETE " + LocalExtension.class.getSimpleName() + " WHERE id NOT IN (:ids)");
query.setParameter("ids", ids);
query.executeUpdate();
session.commit();
}
}
}