/**
* {@inheritDoc}
*/
@Override
public <T extends ObjectWithId> Event createEvent(EventId eventId, T target, Map<String, Object> extraMetadata) {
Account account = null;
User user = null;
try {
user = securityService.getCurrentUser();
account = user.getAccount();
} catch (AuthenticationException ae) {
// We will not persist any READ_* events when a user is not logged in
if (eventId == EventId.READ) {
logger.debug("Anonymous read events are not persisted (" + target + "): " + eventId);
return null;
}
} catch (UnavailableSecurityManagerException e) {
logger.warn("Unable to derive user from SecurityService. A User will be derived from the target (" +
target + ") if possible. If not, no event will be persisted", e);
} catch (Exception nfi) {
logger.warn("Unknown exception type in EventService", nfi);
}
if (extraMetadata == null) {
extraMetadata = new HashMap<>();
}
// TODO: Figure out a way to make these automatically-generated metadata keys constants somewhere
// Extend the context with T information
if (target != null) {
// Fill out ObjectWithId information
extraMetadata.put("targetId", target.getId());
extraMetadata.put("targetVersion", target.getVersion());
// Fill out type (camel casing of the object type)
extraMetadata.put("targetType", target.getClass().getSimpleName());
// Fill out the SobaObject information
if (target instanceof SobaObject) {
SobaObject tSobaObject = (SobaObject) target;
// Attempt to gather the user/account from the target of the event when there is no user logged in.
// We can only do this for subclasses of SobaObject because the other two objects that create events
// (Account/User) can only provide one piece of the puzzle.
if (user == null) {
user = tSobaObject.getUser();
account = tSobaObject.getAccount();
}
extraMetadata.put("targetVisibility", tSobaObject.getVisibility());
extraMetadata.put("targetAlias", tSobaObject.getAlias());
extraMetadata.put("targetHashtags", tSobaObject.getHashtags());
}
// Fill in specific object information
if (target instanceof Account) {
Account tAccount = (Account) target;
extraMetadata.put("targetFuid", tAccount.getFuid());
extraMetadata.put("targetName", tAccount.getName());
} else if (target instanceof InventoryItem) {
InventoryItem inventoryItem = (InventoryItem)target;
Connection connection = inventoryItem.getConnection();
ConnectionProvider tConnectionProvider = connectionProviderFactory.connectionProviderFromId(