public static Identifiable findFirst( Class<? extends Identifiable> clazz, String field, Object value) throws PersistenceException { final OqlQuery oqlQuery = new OqlQuery("from " + clazz.getName() + " as obj where obj." + field + " = :aparam"); oqlQuery.getQuery().setMaxResults(1); try { List list = oqlQuery.getQuery().setParameter("aparam", value).list(); if (JSP.ex(list)) return (Identifiable) list.get(0); else return null; } catch (HibernateException e) { throw new PersistenceException(oqlQuery.doDebug(new Object[] {value}), e); } }
public static Object findUniqueObject( Class clazz, String field, Object value, PersistenceContext pc) throws PersistenceException { final OqlQuery oqlQuery = new OqlQuery("from " + clazz.getName() + " as obj where obj." + field + " = :aparam", pc); try { return oqlQuery.getQuery().setParameter("aparam", value).uniqueResult(); } catch (HibernateException e) { throw new PersistenceException(oqlQuery.doDebug(new Object[] {value}), e); } }
public static Identifiable findUnique( Class<? extends Identifiable> clazz, String field, Object value) throws PersistenceException { final OqlQuery oqlQuery = new OqlQuery("from " + clazz.getName() + " as obj where obj." + field + " = :aparam"); try { return (Identifiable) oqlQuery.getQuery().setParameter("aparam", value).uniqueResult(); } catch (HibernateException e) { throw new PersistenceException(oqlQuery.doDebug(new Object[] {value}), e); } }
public JobLogData run(JobLogData jobLogData) throws Exception { PersistenceContext pc = null; try { pc = PersistenceContext.getDefaultPersistenceContext(); // remove expired messages String hql = "select mes from " + Message.class.getName() + " as mes where mes.expires<:today "; OqlQuery query = new OqlQuery(hql); query.getQuery().setTimestamp("today", new Date()); List<Message> expMes = query.list(); for (Message mes : expMes) { mes.remove(); } // remove expired listener hql = "select lis from " + Listener.class.getName() + " as lis where lis.validityEnd<:today "; query = new OqlQuery(hql); query.getQuery().setTimestamp("today", new Date()); List<Listener> expLis = query.list(); for (Listener lis : expLis) { lis.remove(); } // remove orphan listener hql = "select distinct listener.theClass from " + Listener.class.getName() + " as listener "; query = new OqlQuery(hql); List<String> kinds = query.list(); String listenerTableName = HibernateUtilities.getTableName(Listener.class); // loop for each listener related object for (String theClass : kinds) { try { Class claz = Class.forName(theClass); if (ReflectionUtilities.extendsOrImplements(claz, IdentifiableSupport.class)) { String tableName = HibernateUtilities.getTableName(claz); String oldButAlwaysValidSQL = "SELECT listener.id " + "FROM " + listenerTableName + " listener LEFT OUTER JOIN " + tableName + " ref ON listener.identifiableId = ref.id " + "WHERE (listener.theClass = '" + theClass + "' AND ref.id IS NULL)"; Statement s = pc.session.connection().createStatement(); ResultSet r = s.executeQuery(oldButAlwaysValidSQL); StringBuffer ids = new StringBuffer(); boolean isFirst = true; while (r.next()) { if (!isFirst) ids.append(","); isFirst = false; ids.append("'" + r.getString("id") + "'"); } r.close(); if (!isFirst) { // todo: scale where ids are so many that reach sql string length String delSQL = "DELETE FROM " + listenerTableName + " WHERE ID IN (" + ids + ");"; s.execute(delSQL); } s.close(); } } catch (ClassNotFoundException e) { } } pc.commitAndClose(); jobLogData.notes = jobLogData.notes + "OrphanKiller executed on " + DateUtilities.dateAndHourToString(new Date()); } catch (Throwable e) { Tracer.platformLogger.error("OrphanKiller error", e); if (pc != null) { pc.rollbackAndClose(); } jobLogData.successfull = false; } return jobLogData; }