use of org.alfresco.bm.dataload.rm.services.RecordData in project records-management by Alfresco.
the class FileUnfiledRecordUnitTest method testFileRecordOperation.
@Test
public void testFileRecordOperation() throws Exception {
loadSingleComponent.setEventNameComplete(EVENT_UNFILED_RECORD_FILED);
loadSingleComponent.setDelay(1L);
Event mockedEvent = mock(Event.class);
DBObject mockedData = mock(DBObject.class);
when(mockedData.get(FIELD_CONTEXT)).thenReturn("someContext");
when(mockedData.get(FIELD_PATH)).thenReturn("/aPath");
when(mockedData.get(FIELD_LOAD_OPERATION)).thenReturn(FILE_RECORD_OPERATION);
when(mockedData.get(FIELD_RECORD_ID)).thenReturn("recordId1");
when(mockedEvent.getData()).thenReturn(mockedData);
FolderData mockedFolder = mock(FolderData.class);
when(mockedFolder.getId()).thenReturn("folderId");
when(mockedFolder.getPath()).thenReturn("/aPath");
when(mockedFolder.getContext()).thenReturn("someContext");
when(mockedFileFolderService.getFolder("someContext", "/aPath")).thenReturn(mockedFolder);
when(mockedEvent.getSessionId()).thenReturn("someId");
String recordId1 = "recordId1";
String recordParentPath1 = "/recordParentPath1";
RecordData mockedRecordData1 = mock(RecordData.class);
when(mockedRecordData1.getId()).thenReturn(recordId1);
when(mockedRecordData1.getParentPath()).thenReturn(recordParentPath1);
when(mockedRecordService.getRecord(recordId1)).thenReturn(mockedRecordData1);
mockSiteAndUserData();
EventResult result = loadSingleComponent.processEvent(mockedEvent, new StopWatch());
verify(mockedFileFolderService, times(1)).incrementFileCount(mockedFolder.getContext(), mockedFolder.getPath(), 1);
verify(mockedFileFolderService, times(1)).incrementFileCount(UNFILED_CONTEXT, mockedRecordData1.getParentPath(), -1);
verify(mockedRecordService, times(1)).updateRecord(any(RecordData.class));
assertEquals(true, result.isSuccess());
DBObject data = (DBObject) result.getData();
assertEquals("Filed record with id " + recordId1 + ".", data.get("msg"));
assertEquals("/aPath", data.get(FIELD_PATH));
assertEquals("aUser", data.get("username"));
assertEquals(1, result.getNextEvents().size());
Event event = result.getNextEvents().get(0);
assertEquals(EVENT_UNFILED_RECORD_FILED, event.getName());
DBObject eventData = (DBObject) event.getData();
assertEquals("someContext", eventData.get(FIELD_CONTEXT));
assertEquals("/aPath", eventData.get(FIELD_PATH));
}
use of org.alfresco.bm.dataload.rm.services.RecordData in project records-management by Alfresco.
the class FileUnfiledRecordUnitTest method testFileRecordOperationWithRestAPiException.
@Test
public void testFileRecordOperationWithRestAPiException() throws Exception {
loadSingleComponent.setEventNameComplete(EVENT_UNFILED_RECORD_FILED);
Event mockedEvent = mock(Event.class);
DBObject mockedData = mock(DBObject.class);
when(mockedData.get(FIELD_CONTEXT)).thenReturn("someContext");
when(mockedData.get(FIELD_PATH)).thenReturn("/aPath");
when(mockedData.get(FIELD_LOAD_OPERATION)).thenReturn(FILE_RECORD_OPERATION);
when(mockedData.get(FIELD_RECORD_ID)).thenReturn("recordId1");
when(mockedEvent.getData()).thenReturn(mockedData);
FolderData mockedFolder = mock(FolderData.class);
when(mockedFolder.getId()).thenReturn("folderId");
when(mockedFolder.getPath()).thenReturn("/aPath");
when(mockedFolder.getContext()).thenReturn("someContext");
when(mockedFileFolderService.getFolder("someContext", "/aPath")).thenReturn(mockedFolder);
when(mockedEvent.getSessionId()).thenReturn("someId");
RecordData mockedRecordData = mock(RecordData.class);
when(mockedRecordService.getRecord("recordId1")).thenReturn(mockedRecordData);
Mockito.doThrow(new Exception("someError")).when(mockedRecordsAPI).fileRecord(any(RecordBodyFile.class), any(String.class));
mockSiteAndUserData();
EventResult result = loadSingleComponent.processEvent(mockedEvent, new StopWatch());
verify(mockedFileFolderService, never()).deleteFolder(mockedFolder.getContext(), mockedFolder.getPath() + "/locked", false);
verify(mockedFileFolderService, never()).incrementFileCount(any(String.class), any(String.class), any(Long.class));
assertEquals(false, result.isSuccess());
DBObject data = (DBObject) result.getData();
assertNotNull(data.get("error"));
assertEquals("someError", data.get("error"));
assertEquals("aUser", data.get("username"));
assertEquals(mockedFolder.getPath(), data.get("path"));
assertNotNull(data.get("stack"));
assertEquals(0, result.getNextEvents().size());
}
use of org.alfresco.bm.dataload.rm.services.RecordData in project records-management by Alfresco.
the class RMBaseEventProcessor method uploadElectronicRecordInUnfiledContext.
/**
* Helper method for uploading one record on specified unfiled container or unfiled record folder.
*
* @param folder - unfiled container or unfiled record folder that will contain uploaded records
* @param userModel - UserModel instance with wich rest api will be called
* @param nameIdentifier - a string identifier that the uploaded records will start with
* @param loadFilePlanComponentDelay - delay between upload record operations
* @throws Exception
*/
public void uploadElectronicRecordInUnfiledContext(FolderData folder, UserModel userModel, String nameIdentifier, // to-check: type of exception
long loadFilePlanComponentDelay) throws // to-check: type of exception
Exception {
String unique;
boolean isUnfiledContainer = fileFolderService.getFolder(UNFILED_CONTEXT, UNFILED_RECORD_CONTAINER_PATH).equals(folder);
String folderPath = folder.getPath();
File file = testFileService.getFile();
if (file == null) {
throw new RuntimeException("No test files exist for upload: " + testFileService);
}
unique = UUID.randomUUID().toString();
String newfilePlanComponentName = nameIdentifier + unique + "-" + file.getName();
String newfilePlanComponentTitle = "title: " + newfilePlanComponentName;
// Build record properties
UnfiledContainerChild unfiledContainerChildModel = UnfiledContainerChild.builder().name(newfilePlanComponentName).nodeType(CONTENT_TYPE).properties(UnfiledContainerChildProperties.builder().title(newfilePlanComponentTitle).description(EMPTY).build()).build();
String newRecordId;
String newRecordName;
if (isUnfiledContainer) {
UnfiledContainerAPI unfiledContainersAPI = getRestAPIFactory().getUnfiledContainersAPI(userModel);
UnfiledContainerChild uploadedRecord = unfiledContainersAPI.uploadRecord(unfiledContainerChildModel, folder.getId(), file);
newRecordId = uploadedRecord.getId();
newRecordName = uploadedRecord.getName();
} else {
UnfiledRecordFolderAPI unfiledRecordFoldersAPI = getRestAPIFactory().getUnfiledRecordFoldersAPI(userModel);
UnfiledContainerChild uploadedRecord = unfiledRecordFoldersAPI.uploadRecord(unfiledContainerChildModel, folder.getId(), file);
newRecordId = uploadedRecord.getId();
newRecordName = uploadedRecord.getName();
}
RecordData record = new RecordData(newRecordId, RecordContext.RECORD, newRecordName, folderPath, null, ExecutionState.UNFILED_RECORD_DECLARED);
recordService.createRecord(record);
fileFolderService.incrementFileCount(folder.getContext(), folderPath, 1);
}
use of org.alfresco.bm.dataload.rm.services.RecordData 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.bm.dataload.rm.services.RecordData in project records-management by Alfresco.
the class ScheduleInPlaceRecordLoaders method prepareFilesToBeDeclared.
/**
* Helper method that makes sure the collaboration site contains enough files to declare.
* If the collaboration site doesn't have enough files it creates new empty files.
* The method caches the ids of the files ready to be declared in unscheduledFileBuffer queue.
*
* @param eventOutputMsg
* @throws Exception
*/
public void prepareFilesToBeDeclared(StringBuilder eventOutputMsg) throws Exception {
if (!unscheduledFilesCache.isEmpty()) {
return;
}
eventOutputMsg.append("Preparing files to declare: \n");
// Get the collaboration site document library
String documentLibraryNodeId = getCollaborationSiteDoclib(eventOutputMsg);
// Get the existing files in the provided paths
for (String relativePath : collabSitePaths) {
if (numberOfFilesLeftToPreload() <= 0) {
// we have enough files cached
return;
}
preloadExistingFiles(documentLibraryNodeId, relativePath, eventOutputMsg);
}
/*
* Not enough files to load, create new files
*/
if (recordDeclarationLimit == 0) {
// If number of records to declare is 0 declare all files we can find but don't create new files
return;
}
int filesToCreate = numberOfFilesLeftToPreload();
if (filesToCreate > 0) {
// Create a folder in document library
ContentModel currentNodeModel = new ContentModel();
currentNodeModel.setNodeRef(documentLibraryNodeId);
NodeDetail targetFolder = restCoreAPI.withCoreAPI().usingNode(currentNodeModel).defineNodes().folder("AutoGeneratedFiles");
for (int i = 0; i < filesToCreate; i++) {
// Create a new file
NodeDetail file = targetFolder.file("recordToBe");
eventOutputMsg.append("Created file " + file.getId() + ".");
RecordData record = new RecordData(file.getId(), RecordContext.IN_PLACE_RECORD, file.getName(), null, null, ExecutionState.SCHEDULED);
unscheduledFilesCache.add(record);
if (numberOfFilesLeftToPreload() <= 0) {
return;
}
}
}
}
Aggregations