/*
* 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.core;
import ch.hortis.sonar.core.service.Executor;
import ch.hortis.sonar.model.Snapshot;
import ch.hortis.sonar.model.SnapshotGroup;
import ch.hortis.sonar.service.MavenProjectService;
import ch.hortis.sonar.service.Service;
import ch.hortis.sonar.service.SnapshotGroupService;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.persistence.Query;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class MeasuresCalculatorJob extends BaseJob {
protected static final Logger LOG = LoggerFactory.getLogger(Batch.class);
protected MavenProjectService projectService;
protected SnapshotGroupService snapshotGroupService;
public MeasuresCalculatorJob() {
}
public MavenProjectService getProjectService() {
return projectService;
}
public void setProjectService(MavenProjectService projectService) {
this.projectService = projectService;
}
public SnapshotGroupService getSnapshotGroupService() {
return snapshotGroupService;
}
public void setSnapshotGroupService(SnapshotGroupService snapshotGroupService) {
this.snapshotGroupService = snapshotGroupService;
}
public void process(JobExecutionContext context) throws JobExecutionException {
LOG.debug("Processing job " + this.getClass().getName());
if (getProjectService() == null) {
setProjectService(new MavenProjectService(getEntityManager()));
}
if (getSnapshotGroupService() == null) {
setSnapshotGroupService(new SnapshotGroupService(getEntityManager()));
}
long startTime = System.currentTimeMillis();
LOG.debug("Calling getGroupsToProcess()");
Collection<SnapshotGroup> groups = getGroupsToProcess();
LOG.debug("Calling getGroupsToProcess() returned " + groups.size() + " groups");
int processed = 0;
for (SnapshotGroup group : groups) {
if (snapshotGroupService.isReadyToCalculateMeasures(group, getProjectService())) {
try {
getEntityManager().getTransaction().begin();
LOG.debug("Processing SnapshotGroup " + group.getId());
processGroup(group);
resetLastFlag(group);
getEntityManager().merge(group);
getEntityManager().getTransaction().commit();
LOG.debug("Done processing SnapshotGroup " + group.getId());
} catch (Exception e) {
LOG.error("Error while processing the SnapshotGroup with id=" + group.getId(), e);
getEntityManager().getTransaction().rollback();
removeInvalidGroup(group);
} finally {
processed++;
}
}
}
if (processed > 0) {
long timeTaken = System.currentTimeMillis() - startTime;
LOG.info(processed + " SnapshotGroups processed in " + (timeTaken) + " ms");
//super.getPersistence().dumpStats(LOG);
}
}
protected Collection<SnapshotGroup> getGroupsToProcess() {
Query query = getEntityManager().createQuery("SELECT g FROM SnapshotGroup g WHERE g.processed=false ORDER BY g.createdAt asc");
return query.getResultList();
}
public static List<Service> services = new ArrayList<Service>();
protected void processGroup(SnapshotGroup group) {
Executor executor = new Executor(Service.getAllServices(getEntityManager()));
executor.execute(group);
for (Snapshot snapshot : group.getSnapshots()) {
if (snapshot.getId() == null) {
LOG.debug("Persisting Snapshot");
getEntityManager().persist(snapshot);
getEntityManager().flush();
}
}
group.setProcessed(true);
}
protected void resetLastFlag(SnapshotGroup group) {
Query query = getEntityManager().createQuery("SELECT g FROM SnapshotGroup g WHERE g.mavenProject = :project AND g.last=true AND g.processed=true");
query.setParameter("project", group.getMavenProject());
Collection<SnapshotGroup> lastGroups = query.getResultList();
for (SnapshotGroup lastGroup : lastGroups) {
lastGroup.setLast(Boolean.FALSE);
getEntityManager().merge(lastGroup);
}
group.setLast(true);
}
private void removeInvalidGroup(SnapshotGroup group) {
//Remove invalid groups
getEntityManager().clear();
getEntityManager().getTransaction().begin();
Query query = getEntityManager().createQuery("DELETE FROM Snapshot WHERE snapshotGroup.id=:groupId");
query.setParameter("groupId", group.getId());
query.executeUpdate();
query = getEntityManager().createQuery("DELETE FROM SnapshotGroup WHERE id=:groupId");
query.setParameter("groupId", group.getId());
query.executeUpdate();
LOG.info("Remove SnapshotGroup with id=" + group.getId());
getEntityManager().getTransaction().commit();
}
}