Search in sources :

Example 1 with ChannelEntity

use of io.joynr.messaging.bounceproxy.controller.directory.jdbc.entities.ChannelEntity in project joynr by bmwcarit.

the class ChannelDatabase method addChannel.

@Override
public void addChannel(Channel channel) {
    logger.trace("add channel {}", channel);
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        BounceProxyInformationEntity bpInfoEntity = em.find(BounceProxyInformationEntity.class, channel.getBounceProxy().getId());
        if (bpInfoEntity == null) {
            tx.rollback();
            logger.error("No bounce proxy with ID {} registered for channel {}", channel.getBounceProxy().getId(), channel.getChannelId());
            throw new IllegalArgumentException("No bounce proxy with ID '" + channel.getBounceProxy().getId() + "' registered for channel '" + channel.getChannelId() + "'");
        }
        ChannelEntity entity = new ChannelEntity(channel.getChannelId(), bpInfoEntity, channel.getLocation().toString());
        em.persist(entity);
        tx.commit();
    } catch (RuntimeException ex) {
        logger.error("Error persisting channel with ID {}: error: {}", channel.getChannelId(), ex.getMessage());
        if (tx != null && tx.isActive())
            tx.rollback();
        throw ex;
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) ChannelEntity(io.joynr.messaging.bounceproxy.controller.directory.jdbc.entities.ChannelEntity) BounceProxyInformationEntity(io.joynr.messaging.bounceproxy.controller.directory.jdbc.entities.BounceProxyInformationEntity)

Example 2 with ChannelEntity

use of io.joynr.messaging.bounceproxy.controller.directory.jdbc.entities.ChannelEntity in project joynr by bmwcarit.

the class ChannelDatabase method getChannel.

@Override
@CheckForNull
public Channel getChannel(String ccid) {
    logger.trace("getChannel({})", ccid);
    EntityManager em = emf.createEntityManager();
    ChannelEntity channelEntity = em.find(ChannelEntity.class, ccid);
    if (channelEntity == null) {
        logger.debug("no channel found for ID {}", ccid);
        return null;
    }
    return channelEntity.convertToChannel();
}
Also used : EntityManager(javax.persistence.EntityManager) ChannelEntity(io.joynr.messaging.bounceproxy.controller.directory.jdbc.entities.ChannelEntity) CheckForNull(javax.annotation.CheckForNull)

Example 3 with ChannelEntity

use of io.joynr.messaging.bounceproxy.controller.directory.jdbc.entities.ChannelEntity in project joynr by bmwcarit.

the class ChannelDatabase method getChannels.

@Override
public List<Channel> getChannels() {
    logger.trace("getChannels()");
    List<Channel> channels = new LinkedList<Channel>();
    EntityManager em = emf.createEntityManager();
    String sqlQueryString = String.format("SELECT x FROM %s x", ChannelEntity.class.getSimpleName());
    Query query = em.createQuery(sqlQueryString);
    @SuppressWarnings("unchecked") List<ChannelEntity> resultList = query.getResultList();
    for (ChannelEntity result : resultList) {
        channels.add(result.convertToChannel());
    }
    logger.debug("retrieved {} channels", channels.size());
    return channels;
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) Channel(io.joynr.messaging.info.Channel) ChannelEntity(io.joynr.messaging.bounceproxy.controller.directory.jdbc.entities.ChannelEntity) LinkedList(java.util.LinkedList)

Example 4 with ChannelEntity

use of io.joynr.messaging.bounceproxy.controller.directory.jdbc.entities.ChannelEntity in project joynr by bmwcarit.

the class BounceProxyDatabase method updateChannelAssignment.

@Override
public void updateChannelAssignment(String ccid, BounceProxyInformation bpInfo) throws IllegalArgumentException {
    logger.trace("updateChannelAssignment(ccid={}, bpId={})", ccid, bpInfo.getId());
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        BounceProxyEntity bpEntity = em.find(BounceProxyEntity.class, bpInfo.getId());
        if (bpEntity == null) {
            logger.error("No bounce proxy with ID {} registered", bpInfo.getId());
            throw new IllegalArgumentException("No bounce proxy with ID '" + bpInfo.getId() + "' registered");
        }
        ChannelEntity channelEntity = em.find(ChannelEntity.class, ccid);
        if (channelEntity == null) {
            logger.error("No channel with ID {} was registered before.", ccid);
            throw new IllegalArgumentException("No channel with ID '" + ccid + "' was registered before.");
        }
        // TODO what to do if channel with the same ID is added, but
        // different other properties?
        bpEntity.addChannel(channelEntity);
        em.merge(bpEntity);
        tx.commit();
    } catch (RuntimeException ex) {
        logger.error("Channel assignment could not be persisted. error: {}", ex.getMessage());
        if (tx != null && tx.isActive())
            tx.rollback();
        throw ex;
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) BounceProxyEntity(io.joynr.messaging.bounceproxy.controller.directory.jdbc.entities.BounceProxyEntity) ChannelEntity(io.joynr.messaging.bounceproxy.controller.directory.jdbc.entities.ChannelEntity)

Aggregations

ChannelEntity (io.joynr.messaging.bounceproxy.controller.directory.jdbc.entities.ChannelEntity)4 EntityManager (javax.persistence.EntityManager)4 EntityTransaction (javax.persistence.EntityTransaction)2 BounceProxyEntity (io.joynr.messaging.bounceproxy.controller.directory.jdbc.entities.BounceProxyEntity)1 BounceProxyInformationEntity (io.joynr.messaging.bounceproxy.controller.directory.jdbc.entities.BounceProxyInformationEntity)1 Channel (io.joynr.messaging.info.Channel)1 LinkedList (java.util.LinkedList)1 CheckForNull (javax.annotation.CheckForNull)1 Query (javax.persistence.Query)1