QueryManager implements IQueryManager { @Override public boolean isAlreadyProcessed(final IUserMessage userMessage) throws PersistenceException { boolean result = false; final EntityManager em = EntityManagerUtil.getEntityManager(); final String query = "SELECT COUNT(um) " + "FROM UserMessage um JOIN um.states s1 " + "WHERE um.DIRECTION = org.holodeckb2b.interfaces.messagemodel.Direction.IN " + "AND um.MESSAGE_ID = :msgId " + "AND s1.PROC_STATE_NUM = (SELECT MAX(s2.PROC_STATE_NUM) FROM um.states s2) " + "AND s1.STATE IN ( org.holodeckb2b.interfaces.processingmodel.ProcessingState.DELIVERED, " + "org.holodeckb2b.interfaces.processingmodel.ProcessingState.FAILURE)"; try { em.getTransaction().begin(); result = em.createQuery(query, Long.class) .setParameter("msgId", userMessage.getMessageId()) .getSingleResult() > 0; } catch (final NoResultException nothingFound) { result = false; } catch (final Exception e) { throw new PersistenceException("Could not execute query \"isAlreadyDelivered\"", e); } finally { em.getTransaction().commit(); em.close(); } return result; }  @Override List<V> getMessageUnitsInState(
               Class<T> type, Direction direction, ProcessingState[] states); @Override Collection<IMessageUnitEntity> getMessageUnitsWithId(String messageId, 
    															Direction... direction); @Override Collection<IMessageUnitEntity> getMessageUnitsWithLastStateChangedBefore(Date maxLastChangeDate); @Override List<V> getMessageUnitsForPModesInState(Class<T> type,
                                    Collection<String> pmodeIds, ProcessingState state); @Override void ensureCompletelyLoaded(V messageUnit); static void loadCompletely(MessageUnit jpaMessageUnit); @Override int getNumberOfTransmissions(IUserMessageEntity userMessage); @Override boolean isAlreadyProcessed(final IUserMessage userMessage); List<IMessageUnitEntity> getMessageUnitHistory(final Date start, final int max);  }
@Test public void isAlreadyProcessed() throws PersistenceException { assertTrue(queryManager.isAlreadyProcessed(TestData.userMsg5)); assertFalse(queryManager.isAlreadyProcessed(TestData.userMsg3)); }