/**
* This program is copyright (c) 2007 Hortis-GRC SA.
*
* This file is part of Sonar.
* Sonar is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
package ch.hortis.sonar.service;
import ch.hortis.sonar.model.MavenProject;
import javax.persistence.EntityManager;
import javax.persistence.FlushModeType;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import java.util.Collection;
import java.util.ArrayList;
/**
* Class to obtain {@link ch.hortis.sonar.model.MavenProject} from the persistence layer.
* @see ch.hortis.sonar.model.MavenProject
*/
public class MavenProjectService extends Service {
public MavenProjectService( EntityManager manager ) {
super( manager );
}
public MavenProject getMavenProject( String groupId, String artifactId, String branch ) throws NoResultException {
Query query = manager.createNamedQuery( MavenProject.SQL_SELECT_BY_MVN_ID );
query.setFlushMode( FlushModeType.COMMIT );
query.setParameter( "groupId", groupId );
query.setParameter( "artifactId", artifactId );
query.setParameter( "branch", branch );
MavenProject mavenProject = (MavenProject) query.getSingleResult();
log.debug( "Found maven project db row " + mavenProject.getId() + " " + mavenProject.getArtifactId() );
return mavenProject;
}
public Collection<MavenProject> getModules( MavenProject project, boolean includeSubmodules ) {
Collection<MavenProject> result = new ArrayList<MavenProject>();
Query query = manager.createNamedQuery( MavenProject.SQL_SELECT_BY_PARENT_PROJECT_ID );
query.setFlushMode( FlushModeType.COMMIT );
query.setParameter( "id", project.getId() );
Collection<MavenProject> modules = query.getResultList();
result.addAll( modules );
if (includeSubmodules) {
for (MavenProject module : modules) {
Collection<MavenProject> submodules = getModules(module, true);
result.addAll( submodules );
}
}
return result;
}
}