Search in sources :

Example 31 with RecordData

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;
            }
        }
    }
}
Also used : RecordData(org.alfresco.bm.dataload.rm.services.RecordData) FolderData(org.alfresco.bm.cm.FolderData) ArrayList(java.util.ArrayList) Event(org.alfresco.bm.event.Event) DBObject(com.mongodb.DBObject)

Aggregations

RecordData (org.alfresco.bm.dataload.rm.services.RecordData)31 Test (org.junit.Test)22 Event (org.alfresco.bm.event.Event)15 DBObject (com.mongodb.DBObject)14 EventResult (org.alfresco.bm.event.EventResult)14 StopWatch (org.apache.commons.lang3.time.StopWatch)12 FolderData (org.alfresco.bm.cm.FolderData)10 UserModel (org.alfresco.utility.model.UserModel)6 FilesAPI (org.alfresco.rest.rm.community.requests.gscore.api.FilesAPI)4 UnfiledContainerAPI (org.alfresco.rest.rm.community.requests.gscore.api.UnfiledContainerAPI)4 RMRestWrapper (org.alfresco.rest.core.RMRestWrapper)3 Record (org.alfresco.rest.rm.community.model.record.Record)3 ArrayList (java.util.ArrayList)2 Random (java.util.Random)2 LoadSingleComponentUnitTest (org.alfresco.bm.dataload.LoadSingleComponentUnitTest)2 RecordBodyFile (org.alfresco.rest.rm.community.model.record.RecordBodyFile)2 UnfiledContainer (org.alfresco.rest.rm.community.model.unfiledcontainer.UnfiledContainer)2 UnfiledContainerChild (org.alfresco.rest.rm.community.model.unfiledcontainer.UnfiledContainerChild)2 UnfiledRecordFolderAPI (org.alfresco.rest.rm.community.requests.gscore.api.UnfiledRecordFolderAPI)2 ContentModel (org.alfresco.utility.model.ContentModel)2