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 // if (extension.getId()!=null) { 062 // extension = session.reattach(LocalExtension.class, extension.getId()); 063 // } 064 extension = session.save(extension); 065 Query query = session.createQuery("DELETE FROM " + LocalExtensionFile.class.getSimpleName() + " WHERE extensionId=:id"); 066 query.setParameter("id", extension.getId()); 067 query.executeUpdate(); 068 069 for (File file : files) { 070 LocalExtensionFile localFile = new LocalExtensionFile(); 071 localFile.setExtensionId(extension.getId()); 072 localFile.setPluginKey(extension.getPluginKey()); 073 localFile.setFilename(file.getName()); 074 session.save(localFile); 075 } 076 077 session.commit(); 078 return extension; 079 } 080 081 public void keep(List<LocalExtension> extensions) { 082 List<Integer> ids = new ArrayList<Integer>(); 083 for (LocalExtension extension : extensions) { 084 ids.add(extension.getId()); 085 } 086 087 if (!ids.isEmpty()) { 088 Query query = session.createQuery("DELETE " + LocalExtensionFile.class.getSimpleName() + " WHERE extensionId NOT IN (:ids)"); 089 query.setParameter("ids", ids); 090 query.executeUpdate(); 091 092 query = session.createQuery("DELETE " + LocalExtension.class.getSimpleName() + " WHERE id NOT IN (:ids)"); 093 query.setParameter("ids", ids); 094 query.executeUpdate(); 095 session.commit(); 096 } 097 } 098 }