use of org.alfresco.bm.dataload.rm.services.RecordData 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.bm.dataload.rm.services.RecordData in project records-management by Alfresco.
the class ScheduleFilingUnfiledRecords method getAllUnfiledParentPaths.
/**
* Helper method to obtain all parent paths for unfiled records present on db.
*
* @return all unfiled unfiled parent paths.
*/
private LinkedHashSet<String> getAllUnfiledParentPaths() {
List<RecordData> existingRecords = getAllUnfiledRecords();
LinkedHashSet<String> existingPaths = new LinkedHashSet<>();
for (RecordData record : existingRecords) {
existingPaths.add(record.getParentPath());
}
return existingPaths;
}
use of org.alfresco.bm.dataload.rm.services.RecordData in project records-management by Alfresco.
the class DeclareInPlaceRecordsUnitTest method testDeclareAsRecordWithFail.
@Test
public void testDeclareAsRecordWithFail() throws Exception {
String fileId = "testFileId";
String username = "testUserName";
String password = "testPassword";
String summary = "testSummary";
String stack = "testStack";
long delay = 10L;
declareInPlaceRecords.setDeclareInPlaceRecordDelay(delay);
Event mockedEvent = mock(Event.class);
DBObject mockedData = mock(DBObject.class);
when(mockedData.get(FIELD_ID)).thenReturn(fileId);
when(mockedData.get(FIELD_USERNAME)).thenReturn(username);
when(mockedData.get(FIELD_PASSWORD)).thenReturn(password);
when(mockedEvent.getData()).thenReturn(mockedData);
RecordData dbRecord = new RecordData(fileId, RecordContext.IN_PLACE_RECORD, "testFileName", "testFilePath", "testInPlacePath", ExecutionState.SCHEDULED);
when(mockedRecordService.getRecord(fileId)).thenReturn(dbRecord);
FilesAPI mockedFilesAPI = mock(FilesAPI.class);
when(mockedRestAPIFactory.getFilesAPI(any(UserModel.class))).thenReturn(mockedFilesAPI);
RMRestWrapper mockedRmRestWrapper = mock(RMRestWrapper.class);
when(mockedRmRestWrapper.getStatusCode()).thenReturn(Integer.toString(HttpStatus.UNAUTHORIZED.value()));
RestErrorModel mockedRestErrorModel = mock(RestErrorModel.class);
when(mockedRestErrorModel.getBriefSummary()).thenReturn(summary);
when(mockedRestErrorModel.getStackTrace()).thenReturn(stack);
when(mockedRmRestWrapper.assertLastError()).thenReturn(mockedRestErrorModel);
when(mockedRestAPIFactory.getRmRestWrapper()).thenReturn(mockedRmRestWrapper);
when(mockedApplicationContext.getBean("restAPIFactory", RestAPIFactory.class)).thenReturn(mockedRestAPIFactory);
EventResult result = declareInPlaceRecords.processEvent(mockedEvent, new StopWatch());
assertEquals(true, result.isSuccess());
assertEquals("Declaring file as record: \nFailed with code 401.\n " + summary + ". \n" + stack, result.getData());
assertEquals(1, result.getNextEvents().size());
assertEquals(declareInPlaceRecords.getEventNameInPlaceRecordsDeclared(), result.getNextEvents().get(0).getName());
}
use of org.alfresco.bm.dataload.rm.services.RecordData in project records-management by Alfresco.
the class DeclareInPlaceRecordsUnitTest method testDeclareAsRecordWithSuccess.
@Test
public void testDeclareAsRecordWithSuccess() throws Exception {
String fileId = "testFileId";
String username = "testUserName";
String password = "testPassword";
long delay = 10L;
declareInPlaceRecords.setDeclareInPlaceRecordDelay(delay);
Event mockedEvent = mock(Event.class);
DBObject mockedData = mock(DBObject.class);
when(mockedData.get(FIELD_ID)).thenReturn(fileId);
when(mockedData.get(FIELD_USERNAME)).thenReturn(username);
when(mockedData.get(FIELD_PASSWORD)).thenReturn(password);
when(mockedEvent.getData()).thenReturn(mockedData);
RecordData dbRecord = new RecordData(fileId, RecordContext.IN_PLACE_RECORD, "testFileName", "testFilePath", "testInPlacePath", ExecutionState.SCHEDULED);
when(mockedRecordService.getRecord(fileId)).thenReturn(dbRecord);
FilesAPI mockedFilesAPI = mock(FilesAPI.class);
when(mockedRestAPIFactory.getFilesAPI(any(UserModel.class))).thenReturn(mockedFilesAPI);
Record record = mock(Record.class);
when(record.getName()).thenReturn("newRecordName");
when(record.getParentId()).thenReturn("unfiledContainerID");
when(mockedFilesAPI.declareAsRecord(fileId)).thenReturn(record);
RMRestWrapper mockedRmRestWrapper = mock(RMRestWrapper.class);
when(mockedRmRestWrapper.getStatusCode()).thenReturn(Integer.toString(HttpStatus.CREATED.value()));
when(mockedRestAPIFactory.getRmRestWrapper()).thenReturn(mockedRmRestWrapper);
UnfiledContainer mockedUnfiledContainer = mock(UnfiledContainer.class);
when(mockedUnfiledContainer.getId()).thenReturn("unfiledContainerID");
UnfiledContainerAPI mockedUnfiledContainerAPI = mock(UnfiledContainerAPI.class);
when(mockedUnfiledContainerAPI.getUnfiledContainer(FilePlanComponentAlias.UNFILED_RECORDS_CONTAINER_ALIAS)).thenReturn(mockedUnfiledContainer);
when(mockedRestAPIFactory.getUnfiledContainersAPI()).thenReturn(mockedUnfiledContainerAPI);
FolderData mockedParentFolder = mock(FolderData.class);
when(mockedParentFolder.getPath()).thenReturn(UNFILED_RECORD_CONTAINER_PATH);
when(mockedFileFolderService.getFolder("unfiledContainerID")).thenReturn(mockedParentFolder);
when(mockedApplicationContext.getBean("restAPIFactory", RestAPIFactory.class)).thenReturn(mockedRestAPIFactory);
EventResult result = declareInPlaceRecords.processEvent(mockedEvent, new StopWatch());
assertEquals(true, result.isSuccess());
assertEquals("Declaring file as record: \nsuccess", result.getData());
assertEquals(1, result.getNextEvents().size());
assertEquals(declareInPlaceRecords.getEventNameInPlaceRecordsDeclared(), result.getNextEvents().get(0).getName());
}
use of org.alfresco.bm.dataload.rm.services.RecordData in project records-management by Alfresco.
the class ScheduleFilingUnfiledRecordsUnitTest method testFileRecordsFromUnfiledRecordContainer.
@Test
public void testFileRecordsFromUnfiledRecordContainer() throws Exception {
int maxActiveLoaders = 8;
String configuredPath1 = "/e1/e2/e3";
String entirePath1 = RECORD_CONTAINER_PATH + configuredPath1;
String paths = configuredPath1;
String recordId1 = "recordId1";
String recordParentPath1 = "/";
String recordParentFullPath1 = UNFILED_RECORD_CONTAINER_PATH;
String recordId2 = "recordId2";
String recordParentPath2 = "/recordParentPath2";
String recordParentFullPath2 = UNFILED_RECORD_CONTAINER_PATH + recordParentPath2;
String recordId3 = "recordId3";
String recordParentPath3 = "/recordParentPath3";
String recordParentFullPath3 = UNFILED_RECORD_CONTAINER_PATH + recordParentPath3;
String recordId4 = "recordId4";
String recordParentPath4 = "/recordParentPath4";
String recordParentFullPath4 = UNFILED_RECORD_CONTAINER_PATH + recordParentPath4;
String fileFromPathsStr = recordParentPath1;
scheduleFilingUnfiledRecords.setFileUnfiledRecords(true);
scheduleFilingUnfiledRecords.setMaxActiveLoaders(maxActiveLoaders);
scheduleFilingUnfiledRecords.setRecordFilingLimit("4");
scheduleFilingUnfiledRecords.setFileToRecordFolderPaths(paths);
scheduleFilingUnfiledRecords.setFileFromUnfiledPaths(fileFromPathsStr);
FolderData mockedRecordFolder1 = mock(FolderData.class);
when(mockedRecordFolder1.getId()).thenReturn("recordFolder1Id");
when(mockedRecordFolder1.getContext()).thenReturn(RECORD_FOLDER_CONTEXT);
when(mockedRecordFolder1.getPath()).thenReturn(entirePath1);
when(mockedFileFolderService.getFolder(RECORD_FOLDER_CONTEXT, entirePath1)).thenReturn(mockedRecordFolder1);
RecordData mockedRecordData1 = mock(RecordData.class);
when(mockedRecordData1.getId()).thenReturn(recordId1);
when(mockedRecordData1.getParentPath()).thenReturn(recordParentFullPath1);
RecordData mockedRecordData2 = mock(RecordData.class);
when(mockedRecordData2.getId()).thenReturn(recordId2);
when(mockedRecordData2.getParentPath()).thenReturn(recordParentFullPath2);
RecordData mockedRecordData3 = mock(RecordData.class);
when(mockedRecordData3.getId()).thenReturn(recordId3);
when(mockedRecordData3.getParentPath()).thenReturn(recordParentFullPath3);
RecordData mockedRecordData4 = mock(RecordData.class);
when(mockedRecordData4.getId()).thenReturn(recordId4);
when(mockedRecordData4.getParentPath()).thenReturn(recordParentFullPath4);
when(mockedRecordService.getRecordsInPaths(ExecutionState.UNFILED_RECORD_DECLARED.name(), null, 0, 100)).thenReturn(Arrays.asList(mockedRecordData1, mockedRecordData2, mockedRecordData3, mockedRecordData4));
when(mockedRecordService.getRecordsInPaths(ExecutionState.UNFILED_RECORD_DECLARED.name(), null, 100, 100)).thenReturn(new ArrayList<>());
when(mockedRecordService.getRandomRecord(ExecutionState.UNFILED_RECORD_DECLARED.name(), Arrays.asList(recordParentFullPath2, recordParentFullPath3, recordParentFullPath4, recordParentFullPath1))).thenReturn(mockedRecordData1).thenReturn(mockedRecordData2).thenReturn(mockedRecordData3).thenReturn(mockedRecordData4).thenReturn(null);
FolderData mockedUnfiledRecordFolder = mock(FolderData.class);
when(mockedUnfiledRecordFolder.getId()).thenReturn("folderId1");
when(mockedUnfiledRecordFolder.getContext()).thenReturn(UNFILED_CONTEXT);
when(mockedUnfiledRecordFolder.getPath()).thenReturn(recordParentFullPath1);
when(mockedFileFolderService.getFolder(UNFILED_CONTEXT, recordParentFullPath1)).thenReturn(mockedUnfiledRecordFolder);
FolderData mockedUnfiledRecordFolder1 = mock(FolderData.class);
when(mockedUnfiledRecordFolder1.getId()).thenReturn("newfolderId2");
when(mockedUnfiledRecordFolder1.getContext()).thenReturn(UNFILED_CONTEXT);
when(mockedUnfiledRecordFolder1.getPath()).thenReturn(recordParentFullPath2);
when(mockedFileFolderService.getFolder(UNFILED_CONTEXT, recordParentFullPath2)).thenReturn(mockedUnfiledRecordFolder1);
FolderData mockedUnfiledRecordFolder2 = mock(FolderData.class);
when(mockedUnfiledRecordFolder2.getId()).thenReturn("newfolderId3");
when(mockedUnfiledRecordFolder2.getContext()).thenReturn(UNFILED_CONTEXT);
when(mockedUnfiledRecordFolder2.getPath()).thenReturn(recordParentFullPath3);
when(mockedFileFolderService.getFolder(UNFILED_CONTEXT, recordParentFullPath3)).thenReturn(mockedUnfiledRecordFolder2);
FolderData mockedUnfiledRecordFolder3 = mock(FolderData.class);
when(mockedUnfiledRecordFolder3.getId()).thenReturn("newfolderId4");
when(mockedUnfiledRecordFolder3.getContext()).thenReturn(UNFILED_CONTEXT);
when(mockedUnfiledRecordFolder3.getPath()).thenReturn(recordParentFullPath4);
when(mockedFileFolderService.getFolder(UNFILED_CONTEXT, recordParentFullPath4)).thenReturn(mockedUnfiledRecordFolder3);
when(mockedFileFolderService.getChildFolders(UNFILED_CONTEXT, recordParentFullPath1, 0, 100)).thenReturn(Arrays.asList(mockedUnfiledRecordFolder1, mockedUnfiledRecordFolder2, mockedUnfiledRecordFolder3)).thenReturn(new ArrayList<>());
EventResult result = scheduleFilingUnfiledRecords.processEvent(null, new StopWatch());
verify(mockedFileFolderService, never()).getFoldersByCounts(any(String.class), any(Long.class), any(Long.class), any(Long.class), any(Long.class), any(Long.class), any(Long.class), any(Integer.class), any(Integer.class));
verify(mockedRecordService, times(4)).updateRecord(any(RecordData.class));
assertEquals(true, result.isSuccess());
verify(mockedSessionService, times(4)).startSession(any(DBObject.class));
assertEquals("Raised further " + (4) + " events and rescheduled self.", result.getData());
assertEquals(5, result.getNextEvents().size());
List<String> listOfIds = Arrays.asList(recordId1, recordId2, recordId3, recordId4);
for (int i = 0; i < 4; i++) {
Event event = result.getNextEvents().get(i);
assertEquals(TEST_EVENT_FILE_UNFILED_RECORDS, event.getName());
DBObject dataObj = (DBObject) event.getData();
assertNotNull(dataObj);
assertEquals(RECORD_FOLDER_CONTEXT, (String) dataObj.get(FIELD_CONTEXT));
assertEquals(FILE_RECORD_OPERATION, dataObj.get(FIELD_LOAD_OPERATION));
assertEquals(listOfIds.get(i), dataObj.get(FIELD_RECORD_ID));
}
assertEquals(TEST_EVENT_RESCHEDULE_SELF, result.getNextEvents().get(4).getName());
}
Aggregations