use of org.alfresco.utility.model.UserModel in project records-management by Alfresco.
the class CreateRMSiteUnitTest method testRMSiteCreationExceptionThrown.
@Test(expected = RuntimeException.class)
public void testRMSiteCreationExceptionThrown() throws Exception {
Event mockedEvent = mock(Event.class);
DBObject mockedData = mock(DBObject.class);
Exception mockedException = mock(RuntimeException.class);
String siteId = randomUUID().toString();
String siteManager = randomUUID().toString();
String password = "password";
when(mockedData.get(FIELD_SITE_ID)).thenReturn(siteId);
when(mockedData.get(FIELD_SITE_MANAGER_NAME)).thenReturn(siteManager);
when(mockedData.get(FIELD_SITE_MANAGER_PASSWORD)).thenReturn(password);
when(mockedEvent.getData()).thenReturn(mockedData);
when(mockedSiteDataService.getSite(siteId)).thenReturn(null);
when(mockedRestAPIFactory.getRMSiteAPI(new UserModel(siteManager, siteManager))).thenThrow(mockedException);
when(mockedException.getMessage()).thenReturn(randomUUID().toString());
createRMSite.processEvent(mockedEvent);
verify(mockedSiteDataService, never()).addSite(any(SiteData.class));
verify(mockedSiteDataService, never()).addSiteMember(any(SiteMemberData.class));
}
use of org.alfresco.utility.model.UserModel in project records-management by Alfresco.
the class RMAbstractLoadComponent method loadOperation.
/**
* Helper method to load component specified by operation parameter in specified folder.
*
* @param folder - the folder to load component in
* @param dataObj - the query object that has record id information when filing one record
* @param operation - load operation, that specifies which type of component to load
* @return EventResult - the loading result or error if there was an exception on loading
*/
private EventResult loadOperation(FolderData folder, DBObject dataObj, String operation) {
if (FILE_RECORD_OPERATION.equals(operation)) {
String recordId = (String) dataObj.get(FIELD_RECORD_ID);
if (isBlank(recordId)) {
return new EventResult("Request data not complete for filing unfiled record: " + dataObj, false);
}
}
UserData user = getRandomUser(logger);
String username = user.getUsername();
String password = user.getPassword();
UserModel userModel = new UserModel(username, password);
try {
List<Event> scheduleEvents = new ArrayList<Event>();
String message = executeOperation(folder, dataObj, operation, userModel);
DBObject eventData = BasicDBObjectBuilder.start().add(FIELD_CONTEXT, folder.getContext()).add(FIELD_PATH, folder.getPath()).get();
Event nextEvent = new Event(getEventNameComplete(), eventData);
scheduleEvents.add(nextEvent);
DBObject resultData = BasicDBObjectBuilder.start().add("msg", message).add("path", folder.getPath()).add("username", username).get();
return new EventResult(resultData, scheduleEvents);
} catch (Exception e) {
String error = e.getMessage();
String stack = ExceptionUtils.getStackTrace(e);
// Grab REST API information
DBObject data = BasicDBObjectBuilder.start().append("error", error).append("username", username).append("path", folder.getPath()).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 ScheduleInPlaceRecordLoaders method processEvent.
@Override
protected EventResult processEvent(Event event) throws Exception {
if (!enabled) {
return new EventResult(DECLARING_NOT_WANTED_MSG, new Event(getEventNameComplete(), null));
}
long sessionCount = sessionService.getActiveSessionsCount();
int loaderSessionsToCreate = maxActiveLoaders - (int) sessionCount;
StringBuilder eventOutputMsg = new StringBuilder();
List<Event> nextEvents = new ArrayList<>(loaderSessionsToCreate + 1);
/*
* Prepare files
*/
restCoreAPI.authenticateUser(new UserModel(username, password));
prepareFilesToBeDeclared(eventOutputMsg);
if (unscheduledFilesCache.isEmpty()) {
// Make sure there are no files in process of being declared.
int scheduledInPlaceRecords = (int) recordService.getRecordCountInSpecifiedPaths(ExecutionState.SCHEDULED.name(), null);
if (scheduledInPlaceRecords > 0) {
// Reschedule self. Allow events to finish before raising the done event
Event nextEvent = new Event(getEventNameRescheduleSelf(), System.currentTimeMillis() + loadCheckDelay, null);
nextEvents.add(nextEvent);
eventOutputMsg.append("Waiting for " + scheduledInPlaceRecords + " in progress declare in place records events. Rescheduled self.");
return new EventResult(eventOutputMsg.toString(), nextEvents);
}
// no more files to declare, raise done event
return new EventResult(DONE_EVENT_MSG, new Event(getEventNameComplete(), null));
}
/*
* Schedule worker events
*/
for (int i = 0; i < loaderSessionsToCreate; i++) {
RecordData record = unscheduledFilesCache.poll();
if (record == null) {
break;
}
try {
nextEvents.add(scheduleFile(record, eventOutputMsg));
} catch (EventAlreadyScheduledException ex) {
logger.info("File " + record.getId() + " has already been scheduled. Skip it.", ex);
}
}
numberOfRecordsDeclared += nextEvents.size();
/*
* Reschedule self
*/
Event nextEvent = new Event(getEventNameRescheduleSelf(), System.currentTimeMillis() + loadCheckDelay, null);
nextEvents.add(nextEvent);
eventOutputMsg.append("Raised further " + (nextEvents.size() - 1) + " events and rescheduled self.");
return new EventResult(eventOutputMsg.toString(), nextEvents);
}
use of org.alfresco.utility.model.UserModel in project records-management by Alfresco.
the class CreateRMSite method processEvent.
@Override
public EventResult processEvent(Event event) throws Exception {
mandatoryObject("event", event);
DBObject dataObj = (DBObject) event.getData();
if (dataObj == null) {
throw new IllegalStateException("This processor requires data object with fields '" + FIELD_SITE_ID + ", " + FIELD_SITE_MANAGER_NAME + "'.");
}
String siteId = (String) dataObj.get(FIELD_SITE_ID);
String siteManager = (String) dataObj.get(FIELD_SITE_MANAGER_NAME);
String siteManagersPassword = (String) dataObj.get(FIELD_SITE_MANAGER_PASSWORD);
Boolean onlyLoadInDb = (Boolean) dataObj.get(FIELD_ONLY_DB_LOAD);
if (isBlank(siteId) || isBlank(siteManager) || isBlank(siteManagersPassword)) {
return new EventResult("Requests data not complete for site creation: " + dataObj, false);
}
SiteData site = siteDataService.getSite(siteId);
if (site != null) {
if (site.getCreationState() == Created) {
return new EventResult("RM Site already exists in DB: " + site, false);
}
if (site.getCreationState() != Created) {
return new EventResult("Site state has changed: " + site, false);
}
}
String msg = null;
RMSiteAPI rmSiteAPI = restAPIFactory.getRMSiteAPI(new UserModel(siteManager, siteManagersPassword));
String guid = null;
if (onlyLoadInDb == null) {
RMSite siteModel = RMSite.builder().compliance(STANDARD).title(RM_SITE_TITLE).description(RM_SITE_DESC).build();
RMSite rmSite = rmSiteAPI.createRMSite(siteModel);
String statusCode = restAPIFactory.getRmRestWrapper().getStatusCode();
if (HttpStatus.valueOf(Integer.parseInt(statusCode)) != HttpStatus.CREATED) {
return new EventResult("RM site could not be created.", false);
}
guid = rmSite.getGuid();
msg = "Created site: " + siteId + " Site creator: " + siteManager;
} else {
RMSite alreadyCreatedRMSite = rmSiteAPI.getSite();
guid = alreadyCreatedRMSite.getGuid();
msg = "RM site already exists, just loading it in the DB.";
}
if (site == null) {
// Create data
site = new SiteData();
site.setSiteId(siteId);
site.setTitle(RM_SITE_TITLE);
site.setGuid(RM_SITE_GUID);
site.setDomain(RM_SITE_DOMAIN);
site.setDescription(RM_SITE_DESC);
site.setSitePreset(RM_SITE_PRESET);
site.setVisibility(RM_SITE_VISIBILITY);
site.setType(RM_SITE_TYPE);
site.setCreationState(Scheduled);
siteDataService.addSite(site);
}
// Start by marking them as failures in order to handle all eventualities
siteDataService.setSiteCreationState(siteId, null, Failed);
siteDataService.setSiteMemberCreationState(siteId, siteManager, Failed);
SiteMemberData siteMember = siteDataService.getSiteMember(RM_SITE_ID, siteManager);
if (siteMember == null) {
// Record the administrator
SiteMemberData rmAdminMember = new SiteMemberData();
rmAdminMember.setCreationState(Created);
rmAdminMember.setRole(Administrator.toString());
rmAdminMember.setSiteId(RM_SITE_ID);
rmAdminMember.setUsername(siteManager);
siteDataService.addSiteMember(rmAdminMember);
}
// Mark the site.
siteDataService.setSiteCreationState(siteId, guid, Created);
siteDataService.setSiteMemberCreationState(siteId, siteManager, Created);
loadSpecialContainersInDB(siteId, siteManager, siteManagersPassword);
event = new Event(eventNameSiteCreated, null);
if (logger.isDebugEnabled()) {
logger.debug(msg);
}
return new EventResult(msg, event);
}
use of org.alfresco.utility.model.UserModel in project records-management by Alfresco.
the class CreateRMSite method loadSpecialContainersInDB.
private void loadSpecialContainersInDB(String siteId, String siteManager, String password) throws Exception {
UserModel userModel = new UserModel(siteManager, password);
FilePlan filePlanEntity = restAPIFactory.getFilePlansAPI(userModel).getFilePlan(FILE_PLAN_ALIAS);
FolderData filePlan = new FolderData(// already unique
filePlanEntity.getId(), FILEPLAN_CONTEXT, "/" + PATH_SNIPPET_SITES + "/" + siteId + "/" + PATH_SNIPPET_FILE_PLAN, 0L, 0L);
fileFolderService.createNewFolder(filePlan);
// add Unfiled record container
UnfiledContainer unfiledContainer = restAPIFactory.getUnfiledContainersAPI(userModel).getUnfiledContainer(UNFILED_RECORDS_CONTAINER_ALIAS);
FolderData unfiledRecordContainer = new FolderData(// already unique
unfiledContainer.getId(), UNFILED_CONTEXT, "/" + PATH_SNIPPET_SITES + "/" + siteId + "/" + PATH_SNIPPET_FILE_PLAN + "/" + PATH_SNIPPET_UNFILED_RECORD_CONTAINER, 0L, 0L);
fileFolderService.createNewFolder(unfiledRecordContainer);
// add Transfer container
TransferContainer transferContainerEntity = restAPIFactory.getTransferContainerAPI(userModel).getTransferContainer(TRANSFERS_ALIAS);
FolderData transferContainer = new FolderData(// already unique
transferContainerEntity.getId(), TRANSFER_CONTEXT, "/" + PATH_SNIPPET_SITES + "/" + siteId + "/" + PATH_SNIPPET_FILE_PLAN + "/" + PATH_SNIPPET_TRANSFER_CONTAINER, 0L, 0L);
fileFolderService.createNewFolder(transferContainer);
}
Aggregations