use of org.alfresco.utility.model.UserModel in project records-management by Alfresco.
the class DeclareInPlaceRecords method processEvent.
@Override
protected EventResult processEvent(Event event) throws Exception {
StringBuilder eventOutputMsg = new StringBuilder("Declaring file as record: \n");
super.suspendTimer();
if (event == null) {
throw new IllegalStateException("This processor requires an event.");
}
DBObject dataObj = (DBObject) event.getData();
if (dataObj == null) {
throw new IllegalStateException(MessageFormat.format(INVALID_DATA_MSG_TEMPLATE, FIELD_ID, FIELD_USERNAME, FIELD_PASSWORD));
}
String id = (String) dataObj.get(FIELD_ID);
String username = (String) dataObj.get(FIELD_USERNAME);
String password = (String) dataObj.get(FIELD_PASSWORD);
if (isBlank(id) || isBlank(username) || isBlank(password)) {
throw new IllegalStateException(MessageFormat.format(INVALID_DATA_MSG_TEMPLATE, FIELD_ID, FIELD_USERNAME, FIELD_PASSWORD));
}
try {
// Get the record from database
RecordData dbRecord = recordService.getRecord(id);
if (dbRecord.getExecutionState() != ExecutionState.SCHEDULED) {
throw new IllegalStateException("The record + " + id + " was found but it was already processed");
}
// Call the REST API
super.resumeTimer();
RestAPIFactory restAPIFactory = getRestAPIFactory();
Record record = restAPIFactory.getFilesAPI(new UserModel(username, password)).declareAsRecord(id);
String statusCode = restAPIFactory.getRmRestWrapper().getStatusCode();
super.suspendTimer();
TimeUnit.MILLISECONDS.sleep(declareInPlaceRecordDelay);
if (HttpStatus.valueOf(Integer.parseInt(statusCode)) == HttpStatus.CREATED) {
String recordParentId = record.getParentId();
String unfiledContainerId = getRestAPIFactory().getUnfiledContainersAPI().getUnfiledContainer(FilePlanComponentAlias.UNFILED_RECORDS_CONTAINER_ALIAS).getId();
if (!unfiledContainerId.equals(recordParentId)) {
dbRecord.setExecutionState(ExecutionState.FAILED);
recordService.updateRecord(dbRecord);
return new EventResult("Declaring record with id=" + id + " didn't take place.", false);
}
eventOutputMsg.append("success");
dbRecord.setExecutionState(ExecutionState.UNFILED_RECORD_DECLARED);
dbRecord.setName(record.getName());
String parentPath = fileFolderService.getFolder(recordParentId).getPath();
fileFolderService.incrementFileCount(UNFILED_CONTEXT, parentPath, 1);
dbRecord.setParentPath(parentPath);
} else {
eventOutputMsg.append("Failed with code " + statusCode + ".\n " + restAPIFactory.getRmRestWrapper().assertLastError().getBriefSummary() + ". \n" + restAPIFactory.getRmRestWrapper().assertLastError().getStackTrace());
dbRecord.setExecutionState(ExecutionState.FAILED);
}
recordService.updateRecord(dbRecord);
return new EventResult(eventOutputMsg.toString(), new Event(getEventNameInPlaceRecordsDeclared(), dataObj));
} catch (Exception e) {
String error = e.getMessage();
String stack = ExceptionUtils.getStackTrace(e);
// Grab REST API information
DBObject data = BasicDBObjectBuilder.start().append("error", error).append(FIELD_ID, id).append(FIELD_USERNAME, username).append(FIELD_PASSWORD, password).append("stack", stack).get();
// Build failure result
return new EventResult(data, false);
}
}
use of org.alfresco.utility.model.UserModel in project records-management by Alfresco.
the class PrepareRMSite method processEvent.
/**
* @see org.alfresco.bm.event.AbstractEventProcessor#processEvent(org.alfresco.bm.event.Event)
*/
@Override
protected EventResult processEvent(Event event) throws Exception {
StringBuilder msg = new StringBuilder("Preparing Records Management: \n");
List<Event> events = new ArrayList<Event>(10);
UserModel userModel = new UserModel(getUsername(), getPassword());
// authenticate with provided credentials and verify that they are valid
restCoreAPI.authenticateUser(userModel);
restCoreAPI.withCoreAPI().usingAuthUser().getPerson();
String statusCode = restCoreAPI.getStatusCode();
if (HttpStatus.valueOf(Integer.parseInt(statusCode)) != HttpStatus.OK) {
return new EventResult("Provided RM Site Creator does not exist, or provided credentials are not valid.", false);
}
UserData rmAdmin = userDataService.findUserByUsername(getUsername());
if (rmAdmin == null) {
rmAdmin = new UserData();
rmAdmin.setCreationState(Created);
rmAdmin.setDomain(RM_SITE_DOMAIN);
rmAdmin.setUsername(getUsername());
rmAdmin.setPassword(getPassword());
userDataService.createNewUser(rmAdmin);
} else {
// Check for creation
if (rmAdmin.getCreationState() != Created) {
userDataService.setUserCreationState(getUsername(), Created);
msg.append(" Updating user " + getUsername() + " state to created.\n");
}
}
SiteData rmSite = siteDataService.getSite(RM_SITE_ID);
BasicDBObjectBuilder builder = BasicDBObjectBuilder.start();
builder.add(FIELD_SITE_ID, RM_SITE_ID).add(FIELD_SITE_MANAGER_NAME, getUsername()).add(FIELD_SITE_MANAGER_PASSWORD, getPassword());
boolean existsRMSite = restAPIFactory.getRMSiteAPI(userModel).existsRMSite();
// RM site exists and it is loaded in MongoDB
if (existsRMSite && rmSite != null && rmSite.getCreationState() == Created) {
return new EventResult("RM Site already created, continue loading data.", new Event(getEventNameContinueLoadingData(), null));
}
// RM site exists and it is not loaded in MongoDB
if (existsRMSite && rmSite == null) {
builder.add(FIELD_ONLY_DB_LOAD, true);
DBObject data = builder.get();
events.add(new Event(getEventNameLoadRMSiteIntoDB(), data));
}
// RM site does not exist and will be created
if (!existsRMSite) {
DBObject data = builder.get();
events.add(new Event(getEventNameRMSitePrepared(), data));
}
// Done
return new EventResult(msg.toString(), events);
}
Aggregations