Search in sources :

Example 6 with HibernateCallback

use of org.springframework.orm.hibernate3.HibernateCallback in project gocd by gocd.

the class OauthRepository method deleteUsersOauthGrants.

public void deleteUsersOauthGrants(final List<String> userIds) {
    txnTemplate.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            getHibernateTemplate().execute(new HibernateCallback() {

                public Object doInHibernate(Session session) throws HibernateException, SQLException {
                    deleteEntitiesByUserIds(OauthAuthorization.class, session, userIds);
                    deleteEntitiesByUserIds(OauthToken.class, session, userIds);
                    return true;
                }
            });
        }
    });
}
Also used : TransactionStatus(org.springframework.transaction.TransactionStatus) HibernateCallback(org.springframework.orm.hibernate3.HibernateCallback) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Session(org.hibernate.Session)

Example 7 with HibernateCallback

use of org.springframework.orm.hibernate3.HibernateCallback in project gocd by gocd.

the class MaterialRepository method findRevisionsMatching.

@SuppressWarnings("unchecked")
public List<MatchedRevision> findRevisionsMatching(final MaterialConfig materialConfig, final String searchString) {
    return (List<MatchedRevision>) getHibernateTemplate().execute(new HibernateCallback() {

        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            String sql = "SELECT m.*" + " FROM modifications AS m" + " INNER JOIN materials mat ON mat.id = m.materialId" + " WHERE mat.fingerprint = :finger_print" + " AND (m.revision || ' ' || COALESCE(m.username, '') || ' ' || COALESCE(m.comment, '') LIKE :search_string OR m.pipelineLabel LIKE :search_string)" + " ORDER BY m.id DESC" + " LIMIT 5";
            SQLQuery query = session.createSQLQuery(sql);
            query.addEntity("m", Modification.class);
            Material material = materialConfigConverter.toMaterial(materialConfig);
            query.setString("finger_print", material.getFingerprint());
            query.setString("search_string", "%" + searchString + "%");
            final List<MatchedRevision> list = new ArrayList<>();
            for (Modification mod : (List<Modification>) query.list()) {
                list.add(material.createMatchedRevision(mod, searchString));
            }
            return list;
        }
    });
}
Also used : DependencyMaterial(com.thoughtworks.go.config.materials.dependency.DependencyMaterial) AbstractMaterial(com.thoughtworks.go.config.materials.AbstractMaterial) HibernateCallback(org.springframework.orm.hibernate3.HibernateCallback) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString)

Example 8 with HibernateCallback

use of org.springframework.orm.hibernate3.HibernateCallback in project gocd by gocd.

the class MaterialRepository method hasPipelineEverRunWith.

public boolean hasPipelineEverRunWith(final String pipelineName, final MaterialRevisions revisions) {
    return (Boolean) getHibernateTemplate().execute(new HibernateCallback() {

        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            int numberOfMaterials = revisions.getRevisions().size();
            int match = 0;
            for (MaterialRevision revision : revisions) {
                long materialId = findOrCreateFrom(revision.getMaterial()).getId();
                long modificationId = revision.getLatestModification().getId();
                String key = cacheKeyForHasPipelineEverRunWithModification(pipelineName, materialId, modificationId);
                if (goCache.get(key) != null) {
                    match++;
                    continue;
                }
                String sql = "SELECT materials.id" + " FROM pipelineMaterialRevisions" + " INNER JOIN pipelines ON pipelineMaterialRevisions.pipelineId = pipelines.id" + " INNER JOIN modifications on modifications.id  = pipelineMaterialRevisions.torevisionId" + " INNER JOIN materials on modifications.materialId = materials.id" + " WHERE materials.id = ? AND pipelineMaterialRevisions.toRevisionId >= ? AND pipelineMaterialRevisions.fromRevisionId <= ? AND pipelines.name = ?" + " GROUP BY materials.id;";
                SQLQuery query = session.createSQLQuery(sql);
                query.setLong(0, materialId);
                query.setLong(1, modificationId);
                query.setLong(2, modificationId);
                query.setString(3, pipelineName);
                if (!query.list().isEmpty()) {
                    match++;
                    goCache.put(key, Boolean.TRUE);
                }
            }
            return match == numberOfMaterials;
        }
    });
}
Also used : HibernateCallback(org.springframework.orm.hibernate3.HibernateCallback) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString)

Example 9 with HibernateCallback

use of org.springframework.orm.hibernate3.HibernateCallback in project gocd by gocd.

the class MaterialRepository method modificationAfter.

private long modificationAfter(final long id, final MaterialInstance materialInstance) {
    BigInteger result = (BigInteger) getHibernateTemplate().execute(new HibernateCallback() {

        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            String sql = "SELECT id " + " FROM modifications " + " WHERE materialId = ? " + "        AND id > ?" + " ORDER BY id" + " LIMIT 1";
            SQLQuery query = session.createSQLQuery(sql);
            query.setLong(0, materialInstance.getId());
            query.setLong(1, id);
            return query.uniqueResult();
        }
    });
    return result == null ? id : result.longValue();
}
Also used : BigInteger(java.math.BigInteger) HibernateCallback(org.springframework.orm.hibernate3.HibernateCallback) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString)

Example 10 with HibernateCallback

use of org.springframework.orm.hibernate3.HibernateCallback in project opennms by OpenNMS.

the class EventCountDaoHibernate method getUeiCounts.

@Override
public Set<CountedObject<String>> getUeiCounts(final Integer limit) {
    Set<CountedObject<String>> ueis = new TreeSet<CountedObject<String>>();
    HibernateCallback<List<CountedObject<String>>> hc = new HibernateCallback<List<CountedObject<String>>>() {

        @Override
        public List<CountedObject<String>> doInHibernate(Session session) throws HibernateException {
            Query queryObject = session.createQuery("SELECT event.eventUei, COUNT(event.eventUei) FROM OnmsEvent event GROUP BY event.eventUei ORDER BY COUNT(event.eventUei) desc");
            queryObject.setMaxResults(limit);
            SessionFactoryUtils.applyTransactionTimeout(queryObject, getSessionFactory());
            List<CountedObject<String>> ueis = new ArrayList<CountedObject<String>>();
            @SuppressWarnings("unchecked") final List<Object[]> l = queryObject.list();
            for (final Object[] o : l) {
                ueis.add(new CountedObject<String>((String) o[0], (Long) o[1]));
            }
            return ueis;
        }
    };
    ueis.addAll((List<CountedObject<String>>) getHibernateTemplate().executeWithNativeSession(hc));
    return ueis;
}
Also used : Query(org.hibernate.Query) ArrayList(java.util.ArrayList) TreeSet(java.util.TreeSet) CountedObject(org.opennms.netmgt.dao.api.CountedObject) ArrayList(java.util.ArrayList) List(java.util.List) HibernateCallback(org.springframework.orm.hibernate3.HibernateCallback) CountedObject(org.opennms.netmgt.dao.api.CountedObject) 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