/*
* 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 ch.hortis.sonar.model.SnapshotGroup;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import java.util.Collection;
import java.util.List;
/**
* Class to obtain {@link ch.hortis.sonar.model.SnapshotGroup} from the persistence layer.
*
* @see ch.hortis.sonar.model.Snapshot
*/
public class SnapshotGroupService extends Service {
public SnapshotGroupService(EntityManager manager) {
super(manager);
}
public SnapshotGroup getLastProcessedSnapshotGroup(Integer projectId) throws NoResultException {
Query query = manager.createNamedQuery( SnapshotGroup.SQL_SELECT_LAST_PROCESSED );
query.setParameter("idProject", projectId);
return (SnapshotGroup) query.getSingleResult();
}
public SnapshotGroup getLastUnprocessedGroup(Integer projectId, long delayInMs, long roofTime ) throws NoResultException {
Query query = manager.createNamedQuery( SnapshotGroup.SQL_SELECT_LAST_UNPROCESSED );
query.setParameter("idProject", projectId);
List snapshot = query.getResultList();
if (snapshot.isEmpty()) {
throw new NoResultException();
}
roofTime += delayInMs;
for (Object aSnapshot : snapshot) {
SnapshotGroup grp = (SnapshotGroup) aSnapshot;
if ( grp.getCreatedAt().getTime() < roofTime ) {
return grp;
}
}
throw new NoResultException();
}
public Collection<SnapshotGroup> getUnprocessedGroups() {
Query query = manager.createNamedQuery( SnapshotGroup.SQL_SELECT_UNPROCESSED );
Collection<SnapshotGroup> list = query.getResultList();
log.debug("Selected unprocessed snapshot groups : " + list.size() );
return list;
}
public boolean isReadyToCalculateMeasures( Integer groupId, MavenProjectService projectService ) {
return isReadyToCalculateMeasures( manager.find( SnapshotGroup.class, groupId ), projectService );
}
public boolean isReadyToCalculateMeasures( SnapshotGroup group, MavenProjectService projectService ) {
boolean ready = false;
if ( !group.getProcessed() ) {
if ( projectService == null ) {
projectService = new MavenProjectService( manager );
}
MavenProject rootProject = group.getMavenProject();
Collection<MavenProject> modules = projectService.getModules( rootProject, true );
if ( group.getSnapshots().size() == ( modules.size() + 1 ) ) {
ready = true;
}
}
return ready;
}
}