Search in sources :

Example 1 with ConsumerProperty

use of org.candlepin.model.ConsumerProperty in project candlepin by candlepin.

the class EventBuilder method setEventData.

/**
 * This method is used with any type of event and any target entity.
 * <p>
 * Note: For {@link Type#MODIFIED} events, it can be called twice consecutively
 * to first pass in the original, and then the updated entity. Alternatively,
 * {@link #setEventData(Eventful, Eventful)} can be used in the same use case.
 * </p>
 * @param entity The target entity of the Event
 * @return The builder object
 */
public EventBuilder setEventData(Eventful entity) {
    if (entity != null) {
        // Be careful to check for null before setting so we don't overwrite anything useful
        if (entity instanceof Named && ((Named) entity).getName() != null) {
            event.setTargetName(((Named) entity).getName());
        }
        if (entity instanceof Owned) {
            String ownerId = ((Owned) entity).getOwnerId();
            if (ownerId != null) {
                event.setOwnerId(ownerId);
            }
        }
        if (entity instanceof Entitlement) {
            event.setReferenceType(Event.ReferenceType.POOL);
            Pool referencedPool = ((Entitlement) entity).getPool();
            if (referencedPool != null && referencedPool.getId() != null) {
                event.setReferenceId(referencedPool.getId());
            }
        }
        if (entity.getId() != null) {
            event.setEntityId((String) entity.getId());
            if (entity instanceof ConsumerProperty) {
                Consumer owningConsumer = ((ConsumerProperty) entity).getConsumer();
                if (owningConsumer != null && owningConsumer.getUuid() != null) {
                    event.setConsumerUuid(owningConsumer.getUuid());
                }
            }
        }
        if (event.getTarget().equals(Target.POOL) && event.getType().equals(Type.CREATED)) {
            Map<String, String> eventData = new HashMap<>();
            eventData.put("subscriptionId", ((Pool) entity).getSubscriptionId());
            try {
                event.setEventData(mapper.writeValueAsString(eventData));
            } catch (JsonProcessingException e) {
                log.error("Error while building JSON for pool.created event.", e);
                throw new IseException("Error while building JSON for pool.created event.");
            }
        }
    }
    return this;
}
Also used : Named(org.candlepin.model.Named) Owned(org.candlepin.model.Owned) Consumer(org.candlepin.model.Consumer) IseException(org.candlepin.common.exceptions.IseException) HashMap(java.util.HashMap) Pool(org.candlepin.model.Pool) Entitlement(org.candlepin.model.Entitlement) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ConsumerProperty(org.candlepin.model.ConsumerProperty)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 HashMap (java.util.HashMap)1 IseException (org.candlepin.common.exceptions.IseException)1 Consumer (org.candlepin.model.Consumer)1 ConsumerProperty (org.candlepin.model.ConsumerProperty)1 Entitlement (org.candlepin.model.Entitlement)1 Named (org.candlepin.model.Named)1 Owned (org.candlepin.model.Owned)1 Pool (org.candlepin.model.Pool)1