* {@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(
connection.getProviderId());
extraMetadata.put("targetExternalId", inventoryItem.getExternalId());
extraMetadata.put("targetExternalType", inventoryItem.getType());
extraMetadata.put("targetConnectionId", connection.getId());
extraMetadata.put("targetConnectionAlias", connection.getAlias());
extraMetadata.put("targetConnectionHashtags", connection.getHashtags());
extraMetadata.put("targetConnectionVersion", connection.getVersion());
extraMetadata.put("targetProviderId", tConnectionProvider.getId());
extraMetadata.put("targetProviderDisplayName", tConnectionProvider.getDisplayName());
extraMetadata.put("targetProviderType", tConnectionProvider.getType());
// Fill in the extra metadata stored in the nodeMetadata
extraMetadata.putAll(getMetadataFromInventoryItem(inventoryItem));
} else if (target instanceof Connection) {
Connection tConnection = (Connection) target;
ConnectionProvider tConnectionProvider = connectionProviderFactory.connectionProviderFromId(
tConnection.getProviderId());
extraMetadata.put("targetProviderId", tConnectionProvider.getId());
extraMetadata.put("targetProviderDisplayName", tConnectionProvider.getDisplayName());
extraMetadata.put("targetProviderType", tConnectionProvider.getType());
} else if (target instanceof User) {
User tUser = (User) target;
extraMetadata.put("targetFuid", tUser.getFuid());
extraMetadata.put("targetFullname", tUser.getFullname());
extraMetadata.put("targetUsername", tUser.getUsername());
} else if (target instanceof SobaMessage) {
// This is actually already handled in MessageServiceImpl. This was just put here to help keep track
// of the different types of objects we're supporting in case we want to do more later.
}