use of org.alfresco.bm.dataload.rm.services.RecordData in project records-management by Alfresco.
the class ScheduleFilingUnfiledRecords method prepareUnfiledRecords.
/**
* Helper method for preparing events for filing unfiled records randomly in the record folders structure or in specified record folder paths.
*
* @param loaderSessionsToCreate - the number of still active loader sessions
* @param nextEvents - list of prepared events
*/
private void prepareUnfiledRecords(int loaderSessionsToCreate, List<Event> nextEvents) {
int numberOfRecords = recordFilingLimit;
// initialiaze the number of records to file to all only once if the limit is set to 0
if (mapOfRecordsPerRecordFolder == null && recordFilingLimit == 0) {
List<String> listOfUnfiledRecordFoldersPaths = null;
if (fileFromUnfiledPaths != null && fileFromUnfiledPaths.size() > 0) {
listOfUnfiledRecordFoldersPaths = getListOfUnfiledRecordFoldersPaths();
}
numberOfRecords = (int) recordService.getRecordCountInSpecifiedPaths(ExecutionState.UNFILED_RECORD_DECLARED.name(), listOfUnfiledRecordFoldersPaths);
}
// if the number of records to load is not greater than 0, nothing to file
if (mapOfRecordsPerRecordFolder == null && numberOfRecords == 0) {
return;
}
mapOfRecordsPerRecordFolder = calculateListOfEmptyFolders(mapOfRecordsPerRecordFolder, fileToRecordFolderPaths, numberOfRecords);
List<FolderData> emptyFolders = new ArrayList<FolderData>();
emptyFolders.addAll(mapOfRecordsPerRecordFolder.keySet());
while (nextEvents.size() < loaderSessionsToCreate) {
if (mapOfRecordsPerRecordFolder == null || mapOfRecordsPerRecordFolder.size() == 0) {
break;
}
// Schedule a load for each folder
for (FolderData emptyFolder : emptyFolders) {
int recordsToFile = mapOfRecordsPerRecordFolder.get(emptyFolder) - (int) emptyFolder.getFileCount();
if (recordsToFile <= 0) {
mapOfRecordsPerRecordFolder.remove(emptyFolder);
} else {
try {
List<String> listOfUnfiledRecordFoldersPaths = null;
if (fileFromUnfiledPaths != null && fileFromUnfiledPaths.size() > 0) {
listOfUnfiledRecordFoldersPaths = getListOfUnfiledRecordFoldersPaths();
}
boolean notEnoughRecordsInDb = false;
int i;
for (i = 0; i < recordsToFile; i++) {
RecordData randomRecord = recordService.getRandomRecord(ExecutionState.UNFILED_RECORD_DECLARED.name(), listOfUnfiledRecordFoldersPaths);
if (randomRecord == null) {
notEnoughRecordsInDb = true;
break;
}
DBObject loadData = BasicDBObjectBuilder.start().add(FIELD_CONTEXT, emptyFolder.getContext()).add(FIELD_PATH, emptyFolder.getPath()).add(FIELD_LOAD_OPERATION, FILE_RECORD_OPERATION).add(FIELD_RECORD_ID, randomRecord.getId()).get();
Event loadEvent = new Event(getEventNameFileUnfiledRecords(), loadData);
// Each load event must be associated with a session
String sessionId = sessionService.startSession(loadData);
loadEvent.setSessionId(sessionId);
// Add the event to the list
nextEvents.add(loadEvent);
randomRecord.setExecutionState(ExecutionState.UNFILED_RECORD_SCHEDULED_FOR_FILING);
recordService.updateRecord(randomRecord);
// Check if we have enough
if (nextEvents.size() >= loaderSessionsToCreate) {
break;
}
}
if (// all records prepared for this folder
i == recordsToFile) {
mapOfRecordsPerRecordFolder.remove(emptyFolder);
} else // did not reached the end of the for
{
if (// exited because there are no more records to file in mongoDB
notEnoughRecordsInDb) {
mapOfRecordsPerRecordFolder = null;
break;
} else // exited because reached maximum active loaders
{
mapOfRecordsPerRecordFolder.put(emptyFolder, mapOfRecordsPerRecordFolder.get(emptyFolder) - i - 1);
}
}
} catch (Exception e) {
mapOfRecordsPerRecordFolder.remove(emptyFolder);
continue;
}
}
// Check if we have enough
if (nextEvents.size() >= loaderSessionsToCreate) {
break;
}
}
}
}
Aggregations