@Test public void updateVersion() throws VersionException, ChannelNotFoundException { final Channel channel = newChannel(UUIDUtil.newUAID(), UUID.randomUUID().toString(), 0); jpaDataStore.saveChannel(channel); final String channelId = jpaDataStore.updateVersion(channel.getEndpointToken(), 1); assertThat(channelId, is(equalTo(channel.getChannelId()))); final Channel updated = jpaDataStore.getChannel(channelId); assertThat(updated.getVersion(), is(1L)); }
@Override public String updateVersion(final String endpointToken, final long version) throws VersionException, ChannelNotFoundException { final JpaOperation<ChannelDTO> updateVersion = new JpaOperation<ChannelDTO>() { @Override public ChannelDTO perform(final EntityManager em) { final TypedQuery<ChannelDTO> select = em.createQuery("SELECT c FROM ChannelDTO c where c.endpointToken = :endpointToken", ChannelDTO.class); select.setParameter("endpointToken", endpointToken); final List<ChannelDTO> resultList = select.getResultList(); if (resultList.isEmpty()) { return null; } final ChannelDTO channelDTO = resultList.get(0); if (channelDTO != null) { if (version > channelDTO.getVersion()) { channelDTO.setVersion(version); em.merge(channelDTO); } else { throw new VersionException("New version [" + version + "] must be greater than current version [" + channelDTO.getVersion() + "]"); } } return channelDTO; } }; try { final ChannelDTO channelDto = jpaExecutor.execute(updateVersion); if (channelDto == null) { throw new ChannelNotFoundException("No Channel for endpointToken [" + endpointToken + "] was found", endpointToken); } return channelDto.getChannelId(); } catch (final JpaException e) { final Throwable cause = e.getCause(); if (cause instanceof VersionException) { throw (VersionException) cause; } throw e; } }
JpaDataStore implements DataStore { @Override public String updateVersion(final String endpointToken, final long version) throws VersionException, ChannelNotFoundException { final JpaOperation<ChannelDTO> updateVersion = new JpaOperation<ChannelDTO>() { @Override public ChannelDTO perform(final EntityManager em) { final TypedQuery<ChannelDTO> select = em.createQuery("SELECT c FROM ChannelDTO c where c.endpointToken = :endpointToken", ChannelDTO.class); select.setParameter("endpointToken", endpointToken); final List<ChannelDTO> resultList = select.getResultList(); if (resultList.isEmpty()) { return null; } final ChannelDTO channelDTO = resultList.get(0); if (channelDTO != null) { if (version > channelDTO.getVersion()) { channelDTO.setVersion(version); em.merge(channelDTO); } else { throw new VersionException("New version [" + version + "] must be greater than current version [" + channelDTO.getVersion() + "]"); } } return channelDTO; } }; try { final ChannelDTO channelDto = jpaExecutor.execute(updateVersion); if (channelDto == null) { throw new ChannelNotFoundException("No Channel for endpointToken [" + endpointToken + "] was found", endpointToken); } return channelDto.getChannelId(); } catch (final JpaException e) { final Throwable cause = e.getCause(); if (cause instanceof VersionException) { throw (VersionException) cause; } throw e; } } }
JpaDataStore implements DataStore { @Override public String updateVersion(final String endpointToken, final long version) throws VersionException, ChannelNotFoundException { final JpaOperation<ChannelDTO> updateVersion = new JpaOperation<ChannelDTO>() { @Override public ChannelDTO perform(final EntityManager em) { final TypedQuery<ChannelDTO> select = em.createQuery("SELECT c FROM ChannelDTO c where c.endpointToken = :endpointToken", ChannelDTO.class); select.setParameter("endpointToken", endpointToken); final List<ChannelDTO> resultList = select.getResultList(); if (resultList.isEmpty()) { return null; } final ChannelDTO channelDTO = resultList.get(0); if (channelDTO != null) { if (version > channelDTO.getVersion()) { channelDTO.setVersion(version); em.merge(channelDTO); } else { throw new VersionException("New version [" + version + "] must be greater than current version [" + channelDTO.getVersion() + "]"); } } return channelDTO; } }; try { final ChannelDTO channelDto = jpaExecutor.execute(updateVersion); if (channelDto == null) { throw new ChannelNotFoundException("No Channel for endpointToken [" + endpointToken + "] was found", endpointToken); } return channelDto.getChannelId(); } catch (final JpaException e) { final Throwable cause = e.getCause(); if (cause instanceof VersionException) { throw (VersionException) cause; } throw e; } } JpaDataStore(final String persistenceUnit); }
JpaDataStore implements DataStore { @Override public String updateVersion(final String endpointToken, final long version) throws VersionException, ChannelNotFoundException { final JpaOperation<ChannelDTO> updateVersion = new JpaOperation<ChannelDTO>() { @Override public ChannelDTO perform(final EntityManager em) { final TypedQuery<ChannelDTO> select = em.createQuery("SELECT c FROM ChannelDTO c where c.endpointToken = :endpointToken", ChannelDTO.class); select.setParameter("endpointToken", endpointToken); final List<ChannelDTO> resultList = select.getResultList(); if (resultList.isEmpty()) { return null; } final ChannelDTO channelDTO = resultList.get(0); if (channelDTO != null) { if (version > channelDTO.getVersion()) { channelDTO.setVersion(version); em.merge(channelDTO); } else { throw new VersionException("New version [" + version + "] must be greater than current version [" + channelDTO.getVersion() + "]"); } } return channelDTO; } }; try { final ChannelDTO channelDto = jpaExecutor.execute(updateVersion); if (channelDto == null) { throw new ChannelNotFoundException("No Channel for endpointToken [" + endpointToken + "] was found", endpointToken); } return channelDto.getChannelId(); } catch (final JpaException e) { final Throwable cause = e.getCause(); if (cause instanceof VersionException) { throw (VersionException) cause; } throw e; } } JpaDataStore(final String persistenceUnit); @Override void savePrivateKeySalt(final byte[] salt); @Override byte[] getPrivateKeySalt(); @Override boolean saveChannel(final Channel channel); @Override Channel getChannel(final String channelId); @Override void removeChannels(final Set<String> channelIds); @Override Set<String> getChannelIds(final String uaid); @Override void removeChannels(final String uaid); @Override String updateVersion(final String endpointToken, final long version); @Override String saveUnacknowledged(final String channelId, final long version); @Override Set<Ack> getUnacknowledged(final String uaid); @Override Set<Ack> removeAcknowledged(final String uaid, final Set<Ack> acked); }
JpaDataStore implements DataStore { @Override public String updateVersion(final String endpointToken, final long version) throws VersionException, ChannelNotFoundException { final JpaOperation<ChannelDTO> updateVersion = new JpaOperation<ChannelDTO>() { @Override public ChannelDTO perform(final EntityManager em) { final TypedQuery<ChannelDTO> select = em.createQuery("SELECT c FROM ChannelDTO c where c.endpointToken = :endpointToken", ChannelDTO.class); select.setParameter("endpointToken", endpointToken); final List<ChannelDTO> resultList = select.getResultList(); if (resultList.isEmpty()) { return null; } final ChannelDTO channelDTO = resultList.get(0); if (channelDTO != null) { if (version > channelDTO.getVersion()) { channelDTO.setVersion(version); em.merge(channelDTO); } else { throw new VersionException("New version [" + version + "] must be greater than current version [" + channelDTO.getVersion() + "]"); } } return channelDTO; } }; try { final ChannelDTO channelDto = jpaExecutor.execute(updateVersion); if (channelDto == null) { throw new ChannelNotFoundException("No Channel for endpointToken [" + endpointToken + "] was found", endpointToken); } return channelDto.getChannelId(); } catch (final JpaException e) { final Throwable cause = e.getCause(); if (cause instanceof VersionException) { throw (VersionException) cause; } throw e; } } JpaDataStore(final String persistenceUnit); @Override void savePrivateKeySalt(final byte[] salt); @Override byte[] getPrivateKeySalt(); @Override boolean saveChannel(final Channel channel); @Override Channel getChannel(final String channelId); @Override void removeChannels(final Set<String> channelIds); @Override Set<String> getChannelIds(final String uaid); @Override void removeChannels(final String uaid); @Override String updateVersion(final String endpointToken, final long version); @Override String saveUnacknowledged(final String channelId, final long version); @Override Set<Ack> getUnacknowledged(final String uaid); @Override Set<Ack> removeAcknowledged(final String uaid, final Set<Ack> acked);  }