use of org.candlepin.model.Named 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;
}
Aggregations