use of org.alfresco.bm.event.selector.EventProcessorResponse in project records-management by Alfresco.
the class CreateRMSiteMember method processEvent.
@Override
protected EventResult processEvent(Event event) throws Exception {
if (event == null) {
throw new IllegalStateException("This processor requires an event.");
}
DBObject dataObj = (DBObject) event.getData();
if (dataObj == null) {
throw new IllegalStateException("This processor requires data with field " + FIELD_USERNAME);
}
String username = (String) dataObj.get(FIELD_USERNAME);
EventProcessorResponse response = null;
Event nextEvent = null;
String msg = null;
// Check the input
if (StringUtils.isBlank(username)) {
dataObj.put(MSG_KEY, INVALID_SITE_MEMBER_REQUEST_MSG);
return new EventResult(dataObj, false);
}
// Get the membership data
SiteMemberData siteMember = siteDataService.getSiteMember(PATH_SNIPPET_RM_SITE_ID, username);
if (siteMember == null) {
dataObj.put(MSG_KEY, MessageFormat.format(SITE_MEMBER_MISSING_MSG_TEMPLATE, username));
return new EventResult(dataObj, false);
}
if (siteMember.getCreationState() != DataCreationState.Scheduled) {
dataObj.put(MSG_KEY, MessageFormat.format(SITE_MEMBER_ALREADY_PROCESSED_MSG_TEMPLATE, siteMember));
return new EventResult(dataObj, false);
}
// Start by marking it as a failure in order to handle all failure paths
siteDataService.setSiteMemberCreationState(PATH_SNIPPET_RM_SITE_ID, username, DataCreationState.Failed);
String roleStr = siteMember.getRole();
try {
// assign RM roles to new members as admin user
RMUserAPI rmUserAPI = getRestAPIFactory().getRMUserAPI();
rmUserAPI.assignRoleToUser(username, roleStr);
siteDataService.setSiteMemberCreationState(PATH_SNIPPET_RM_SITE_ID, username, DataCreationState.Created);
siteMember = siteDataService.getSiteMember(PATH_SNIPPET_RM_SITE_ID, username);
EventDataObject responseData = new EventDataObject(STATUS.SUCCESS, siteMember);
response = new EventProcessorResponse("Added RM site member", true, responseData);
msg = MessageFormat.format(CREATED_RM_SITE_MEMBER_MSG_TEMPLATE, response);
nextEvent = new Event(eventNameRMSiteMemberCreated, null);
} catch (Exception e) {
// Failure
throw new RuntimeException("Create RM site member as user: admin failed (" + e.getMessage() + "): " + siteMember, e);
}
logger.debug(msg);
EventResult result = new EventResult(msg, nextEvent);
return result;
}
Aggregations