Search in sources :

Example 21 with HibernateCallback

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;
}
Also used : SQLException(java.sql.SQLException) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) HibernateCallback(org.springframework.orm.hibernate3.HibernateCallback)

Example 22 with HibernateCallback

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;
}
Also used : LongType(org.hibernate.type.LongType) SQLException(java.sql.SQLException) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) HibernateCallback(org.springframework.orm.hibernate3.HibernateCallback)

Example 23 with HibernateCallback

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();
}
Also used : BigInteger(java.math.BigInteger) HibernateCallback(org.springframework.orm.hibernate3.HibernateCallback) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString)

Example 24 with HibernateCallback

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;
    }
}
Also used : CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) HibernateCallback(org.springframework.orm.hibernate3.HibernateCallback)

Example 25 with HibernateCallback

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;
        }
    });
}
Also used : Query(org.hibernate.Query) HibernateCallback(org.springframework.orm.hibernate3.HibernateCallback) Session(org.hibernate.Session)

Aggregations

HibernateCallback (org.springframework.orm.hibernate3.HibernateCallback)27 Session (org.hibernate.Session)15 CaseInsensitiveString (com.thoughtworks.go.config.CaseInsensitiveString)10 SQLException (java.sql.SQLException)7 Query (org.hibernate.Query)6 List (java.util.List)5 Criteria (org.hibernate.Criteria)5 Test (org.junit.Test)4 OnmsCriteria (org.opennms.netmgt.model.OnmsCriteria)4 AgentCookie (com.thoughtworks.go.server.domain.AgentCookie)3 BigInteger (java.math.BigInteger)3 HibernateException (org.hibernate.HibernateException)3 AgentIdentifier (com.thoughtworks.go.remote.AgentIdentifier)2 ArrayList (java.util.ArrayList)2 SQLQuery (org.hibernate.SQLQuery)2 LongType (org.hibernate.type.LongType)2 RunIf (com.googlecode.junit.ext.RunIf)1 AbstractMaterial (com.thoughtworks.go.config.materials.AbstractMaterial)1 DependencyMaterial (com.thoughtworks.go.config.materials.dependency.DependencyMaterial)1 HgMaterial (com.thoughtworks.go.config.materials.mercurial.HgMaterial)1