use of org.springframework.orm.hibernate3.HibernateCallback in project gocd by gocd.
the class MaterialRepository method getTotalModificationsFor.
public Long getTotalModificationsFor(final MaterialInstance materialInstance) {
String key = materialModificationCountKey(materialInstance);
Long totalCount = (Long) goCache.get(key);
if (totalCount == null || totalCount == 0) {
synchronized (key) {
totalCount = (Long) goCache.get(key);
if (totalCount == null || totalCount == 0) {
totalCount = (Long) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query q = session.createQuery("select count(*) FROM Modification WHERE materialId = ?");
q.setLong(0, materialInstance.getId());
return q.uniqueResult();
}
});
goCache.put(key, totalCount);
}
}
}
return totalCount;
}
use of org.springframework.orm.hibernate3.HibernateCallback in project gocd by gocd.
the class MaterialRepository method latestModificationRunByPipeline.
public Long latestModificationRunByPipeline(final CaseInsensitiveString pipelineName, final Material material) {
final long materialId = findMaterialInstance(material).getId();
String key = cacheKeyForLatestPmrForPipelineKey(materialId, pipelineName.toLower());
Long modificationId = (Long) goCache.get(key);
if (modificationId == null) {
synchronized (key) {
modificationId = (Long) goCache.get(key);
if (modificationId == null) {
modificationId = (Long) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
SQLQuery sqlQuery = session.createSQLQuery("SELECT MAX(pmr.toRevisionId) toRevisionId " + "FROM (SELECT torevisionid, pipelineid FROM pipelineMaterialRevisions WHERE materialid = :material_id) AS pmr\n" + "INNER JOIN pipelines p ON ( p.name = :pipeline_name AND p.id = pmr.pipelineId)");
sqlQuery.setParameter("material_id", materialId);
sqlQuery.setParameter("pipeline_name", pipelineName.toString());
sqlQuery.addScalar("toRevisionId", new LongType());
return sqlQuery.uniqueResult();
}
});
if (modificationId == null) {
modificationId = -1L;
}
goCache.put(key, modificationId);
}
}
}
return modificationId;
}
use of org.springframework.orm.hibernate3.HibernateCallback in project gocd by gocd.
the class MaterialRepository method findLastBuiltModificationId.
private Long findLastBuiltModificationId(final Pipeline pipeline, final MaterialInstance materialInstance) {
BigInteger result = (BigInteger) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
String sql = "SELECT fromRevisionId " + " FROM pipelineMaterialRevisions pmr " + " INNER JOIN pipelines p on p.id = pmr.pipelineId " + " WHERE materialId = ? " + " AND p.name = ? " + " AND pipelineId < ? " + " ORDER BY pmr.id DESC" + " LIMIT 1";
SQLQuery query = session.createSQLQuery(sql);
query.setLong(0, materialInstance.getId());
query.setString(1, pipeline.getName());
query.setLong(2, pipeline.getId());
return query.uniqueResult();
}
});
return result == null ? null : result.longValue();
}
use of org.springframework.orm.hibernate3.HibernateCallback in project gocd by gocd.
the class MaterialRepository method findLatestModification.
Modification findLatestModification(final MaterialInstance expandedInstance) {
Modifications modifications = cachedModifications(expandedInstance);
if (modifications != null && !modifications.isEmpty()) {
return modifications.get(0);
}
String cacheKey = latestMaterialModificationsKey(expandedInstance);
synchronized (cacheKey) {
Modification modification = (Modification) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query query = session.createQuery("FROM Modification WHERE materialId = ? ORDER BY id DESC");
query.setMaxResults(1);
query.setLong(0, expandedInstance.getId());
return query.uniqueResult();
}
});
goCache.put(cacheKey, new Modifications(modification));
return modification;
}
}
use of org.springframework.orm.hibernate3.HibernateCallback in project gocd by gocd.
the class ScheduleTestUtil method modForRev.
private List<Modification> modForRev(final RevisionsForMaterial revision) {
return (List<Modification>) materialRepository.getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query q = session.createQuery("from Modification where revision in (:in) order by id desc");
q.setParameterList("in", revision.revs);
List list = q.list();
if (list.isEmpty()) {
throw new RuntimeException("you are trying to load revision " + revision + " which doesn't exist");
}
return list;
}
});
}
Aggregations