Example usage for com.mongodb BasicDBObjectBuilder start

List of usage examples for com.mongodb BasicDBObjectBuilder start

Introduction

In this page you can find the example usage for com.mongodb BasicDBObjectBuilder start.

Prototype

public static BasicDBObjectBuilder start() 

Source Link

Document

Creates a builder intialized with an empty document.

Usage

From source file:org.alfresco.bm.dataload.rm.fileplan.ScheduleFilePlanLoaders.java

License:Open Source License

/**
 * Helper method for preparing the load events for record folders children from the last level.
 *
 * @param loaderSessionsToCreate - the number of still active loader sessions
 * @param nextEvents - list of prepared events
 *///w w  w.  jav a  2s .c  om
private void prepareRecordFoldersOnLowestLevel(int loaderSessionsToCreate, List<Event> nextEvents) {
    int skip = 0;
    int limit = 100;

    while (nextEvents.size() < loaderSessionsToCreate) {
        // Get categories needing loading
        List<FolderData> emptyFolders = fileFolderService.getFoldersByCounts(RECORD_CATEGORY_CONTEXT,
                Long.valueOf(maxLevel), Long.valueOf(maxLevel), //max and min level are FILE_PLAN_LEVEL+depth, of last category, where we load lowest level of record folders
                0L, Long.valueOf(folderNumber - 1), //limit the maximum number of child folders to number of record folder to create - 1
                null, null, // Ignore file limits
                skip, limit);

        if (emptyFolders.size() == 0) {
            // The folders were populated in the mean time
            break;
        }
        // Schedule a load for each folder
        for (FolderData emptyFolder : emptyFolders) {
            int folderCount = fileFolderService
                    .getChildFolders(RECORD_FOLDER_CONTEXT, emptyFolder.getPath(), skip, limit).size();
            int foldersToCreate = folderNumber - folderCount;

            try {
                // Create a lock folder that has too many files and folders so that it won't be picked up
                // by this process in subsequent trawls
                String lockPath = emptyFolder.getPath() + "/locked";
                FolderData lockFolder = new FolderData(UUID.randomUUID().toString(), emptyFolder.getContext(),
                        lockPath, Long.MAX_VALUE, Long.MAX_VALUE);
                fileFolderService.createNewFolder(lockFolder);
                // We locked this, so the load can be scheduled.
                // The loader will remove the lock when it completes
                DBObject loadData = BasicDBObjectBuilder.start().add(FIELD_CONTEXT, emptyFolder.getContext())
                        .add(FIELD_PATH, emptyFolder.getPath())
                        .add(FIELD_ROOT_CATEGORIES_TO_CREATE, Integer.valueOf(0))
                        .add(FIELD_CATEGORIES_TO_CREATE, Integer.valueOf(0))
                        .add(FIELD_FOLDERS_TO_CREATE, Integer.valueOf(foldersToCreate)).get();
                Event loadEvent = new Event(eventNameLoadRecordCategories, 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);
            } catch (Exception e) {
                // The lock was already applied; find another
                continue;
            }
            // Check if we have enough
            if (nextEvents.size() >= loaderSessionsToCreate) {
                break;
            }
        }
        skip += limit;
    }
}

From source file:org.alfresco.bm.dataload.rm.fileplan.ScheduleRecordLoaders.java

License:Open Source License

/**
 * Helper method for preparing events for loading 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
 *//*from   www.  jav  a 2 s . c o  m*/
private void prepareRecords(int loaderSessionsToCreate, List<Event> nextEvents) {
    calculateListOfEmptyFolders();
    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 recordsToCreate = mapOfRecordsPerRecordFolder.get(emptyFolder)
                    - (int) emptyFolder.getFileCount();
            if (recordsToCreate <= 0) {
                mapOfRecordsPerRecordFolder.remove(emptyFolder);
            } else {
                try {
                    // Create a lock folder that has too many files and folders so that it won't be picked up
                    // by this process in subsequent trawls
                    String lockPath = emptyFolder.getPath() + "/locked";
                    FolderData lockFolder = new FolderData(UUID.randomUUID().toString(),
                            emptyFolder.getContext(), lockPath, Long.MAX_VALUE, Long.MAX_VALUE);
                    fileFolderService.createNewFolder(lockFolder);
                    // We locked this, so the load can be scheduled.
                    // The loader will remove the lock when it completes
                    DBObject loadData = BasicDBObjectBuilder.start()
                            .add(FIELD_CONTEXT, emptyFolder.getContext()).add(FIELD_PATH, emptyFolder.getPath())
                            .add(FIELD_RECORDS_TO_CREATE, Integer.valueOf(recordsToCreate)).get();
                    Event loadEvent = new Event(eventNameLoadRecords, 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);
                    mapOfRecordsPerRecordFolder.remove(emptyFolder);
                } catch (Exception e) {
                    mapOfRecordsPerRecordFolder.remove(emptyFolder);
                    // The lock was already applied; find another
                    continue;
                }
            }
            // Check if we have enough
            if (nextEvents.size() >= loaderSessionsToCreate) {
                break;
            }
        }
    }
}

From source file:org.alfresco.bm.dataload.rm.records.DeclareInPlaceRecords.java

License:Open Source License

@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.");
    }/*from w w  w .  j a  va  2s. c  o  m*/

    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();
        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) {
            eventOutputMsg.append("success");
            dbRecord.setExecutionState(ExecutionState.SUCCESS);
            dbRecord.setName(record.getName());
        } 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);
    }
}

From source file:org.alfresco.bm.dataload.rm.records.ScheduleInPlaceRecordLoaders.java

License:Open Source License

/**
 * Helper method that creates a declare record event for the provided file
 *
 * @param fileToSchedule info of the file to declare as record
 * @param eventOutputMsg/*  w  w w . j a  v  a  2 s.co  m*/
 * @return the declare as record event for the provided file
 * @throws EventAlreadyScheduledException if the provided file has already been scheduled
 */
private Event scheduleFile(RecordData fileToSchedule, StringBuilder eventOutputMsg)
        throws EventAlreadyScheduledException {
    eventOutputMsg.append("Sheduled file to be declared as record: " + fileToSchedule.getId() + ". ");

    // Create record in database to lock it
    try {
        recordService.createRecord(fileToSchedule);
    } catch (DuplicateRecordException ex) {
        throw new EventAlreadyScheduledException(eventNameDeclareInPlaceRecord, fileToSchedule.getId());
    }

    // Create an event
    DBObject declareData = BasicDBObjectBuilder.start().add(FIELD_ID, fileToSchedule.getId())
            .add(FIELD_USERNAME, username).add(FIELD_PASSWORD, password).get();

    Event declareEvent = new Event(getEventNameDeclareInPlaceRecord(), declareData);
    // Each load event must be associated with a session
    String sessionId = sessionService.startSession(declareData);
    declareEvent.setSessionId(sessionId);

    return declareEvent;
}

From source file:org.alfresco.bm.dataload.rm.site.PrepareRMSite.java

License:Open Source License

/**
 * @see org.alfresco.bm.event.AbstractEventProcessor#processEvent(org.alfresco.bm.event.Event)
 *///ww w  .j av a  2  s.  c o m
@Override
protected EventResult processEvent(Event event) throws Exception {
    StringBuilder msg = new StringBuilder("Preparing Records Management: \n");
    List<Event> events = new ArrayList<Event>(10);

    UserModel userModel = new UserModel(getUsername(), getPassword());
    //authenticate with provided credentials and verify that they are valid
    restCoreAPI.authenticateUser(userModel);
    restCoreAPI.withCoreAPI().usingAuthUser().getPerson();
    String statusCode = restCoreAPI.getStatusCode();
    if (HttpStatus.valueOf(Integer.parseInt(statusCode)) != HttpStatus.OK) {
        return new EventResult(
                "Provided RM Site Creator does not exist, or provided credentials are not valid.", false);
    }

    UserData rmAdmin = userDataService.findUserByUsername(getUsername());
    if (rmAdmin == null) {
        rmAdmin = new UserData();
        rmAdmin.setCreationState(Created);
        rmAdmin.setDomain(RM_SITE_DOMAIN);
        rmAdmin.setUsername(getUsername());
        rmAdmin.setPassword(getPassword());
        userDataService.createNewUser(rmAdmin);
    } else {
        // Check for creation
        if (rmAdmin.getCreationState() != Created) {
            userDataService.setUserCreationState(getUsername(), Created);
            msg.append("   Updating user " + getUsername() + " state to created.\n");
        }
    }

    SiteData rmSite = siteDataService.getSite(RM_SITE_ID);
    if (rmSite == null) {
        // Create data
        rmSite = new SiteData();
        rmSite.setSiteId(RM_SITE_ID);
        rmSite.setTitle(RM_SITE_TITLE);
        rmSite.setGuid(RM_SITE_GUID);
        rmSite.setDomain(RM_SITE_DOMAIN);
        rmSite.setDescription(RM_SITE_DESC);
        rmSite.setSitePreset(RM_SITE_PRESET);
        rmSite.setVisibility(RM_SITE_VISIBILITY);
        rmSite.setType(RM_SITE_TYPE);
        rmSite.setCreationState(Scheduled);
        siteDataService.addSite(rmSite);
        msg.append("   Added RM site '" + RM_SITE_ID + "' as created.\n");

        // Record the administrator
        SiteMemberData rmAdminMember = new SiteMemberData();
        rmAdminMember.setCreationState(Created);
        rmAdminMember.setRole(Administrator.toString());
        rmAdminMember.setSiteId(RM_SITE_ID);
        rmAdminMember.setUsername(getUsername());
        siteDataService.addSiteMember(rmAdminMember);
        msg.append("   Added user '" + getUsername() + "' RM administrator.\n");
    }

    BasicDBObjectBuilder builder = BasicDBObjectBuilder.start();
    builder.add(FIELD_SITE_ID, rmSite.getSiteId()).add(FIELD_SITE_MANAGER, getUsername());

    boolean existsRMSite = restAPIFactory.getRMSiteAPI(userModel).existsRMSite();

    // RM site exists and it is loaded in MongoDB
    if (existsRMSite && rmSite.getCreationState() == Created) {
        return new EventResult("RM Site already created, continue loading data.",
                new Event(getEventNameContinueLoadingData(), null));
    }

    // RM site exists and it is not loaded in MongoDB
    if (existsRMSite && rmSite.getCreationState() != Created) {
        builder.add(FIELD_ONLY_DB_LOAD, true);
        DBObject data = builder.get();
        events.add(new Event(getEventNameLoadRMSiteIntoDB(), data));
    }

    // RM site does not exist and will be created
    if (!existsRMSite) {
        DBObject data = builder.get();
        events.add(new Event(getEventNameRMSitePrepared(), data));
    }

    // Done
    return new EventResult(msg.toString(), events);
}

From source file:org.alfresco.bm.dataload.rm.unfiled.LoadUnfiledRecordFolders.java

License:Open Source License

/**
 * Helper method that creates specified number of root unfiled record folders if the specified container is unfiled record container,
 * or specified number of unfiled record folder children if the container is a root unfiled record folder or an ordinary unfiled record folder.
 *
 * @param container - unfiled record container, or an unfiled record folder
 * @param rootFoldersToCreate - number of root unfiled record folders to create
 * @param foldersToCreate - number of unfiled record folder children
 * @return EventResult - the loading result or error if there was an exception on loading
 * @throws IOException/* w  w  w  . j a  v  a2 s.c  om*/
 */
private EventResult loadUnfiledRecordFolder(FolderData container, int rootFoldersToCreate, int foldersToCreate)
        throws IOException {
    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>();
        //Create root unfiled record folders
        if (rootFoldersToCreate > 0) {
            super.resumeTimer();
            createRootUnfiledRecordFolder(container, userModel, rootFoldersToCreate,
                    ROOT_UNFILED_RECORD_FOLDER_NAME_IDENTIFIER, container.getContext(),
                    loadUnfiledRecordFolderDelay);
            super.suspendTimer();
            String lockedPath = container.getPath() + "/locked";
            fileFolderService.deleteFolder(container.getContext(), lockedPath, false);
        }

        //Create unfiled record folder children
        if (foldersToCreate > 0) {
            super.resumeTimer();
            createUnfiledRecordFolder(container, userModel, foldersToCreate,
                    UNFILED_RECORD_FOLDER_NAME_IDENTIFIER, container.getContext(),
                    loadUnfiledRecordFolderDelay);
            super.suspendTimer();
            // Clean up the lock
            String lockedPath = container.getPath() + "/locked";
            fileFolderService.deleteFolder(container.getContext(), lockedPath, false);
        }

        DBObject eventData = BasicDBObjectBuilder.start().add(FIELD_CONTEXT, container.getContext())
                .add(FIELD_PATH, container.getPath()).get();
        Event nextEvent = new Event(eventNameUnfiledRecordFoldersLoaded, eventData);

        scheduleEvents.add(nextEvent);
        DBObject resultData = BasicDBObjectBuilder.start()
                .add("msg",
                        "Created " + rootFoldersToCreate + " root unfiled record folders and " + foldersToCreate
                                + " unfiled folders children.")
                .add("path", container.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", container.getPath()).append("stack", stack).get();
        // Build failure result
        return new EventResult(data, false);
    }
}

From source file:org.alfresco.bm.dataload.rm.unfiled.LoadUnfiledRecords.java

License:Open Source License

/**
 * Helper method that load specified numbers of unfiled records in specified unfiled record container of unfiled record folder.
 *
 * @param container - unfiled record container, or an unfiled record folder
 * @param recordsToCreate - number of records to create
 * @return EventResult - the loading result or error if there was an exception on loading
 * @throws IOException/*from www . ja v  a  2 s .  co m*/
 */
private EventResult loadRecords(FolderData container, int recordsToCreate) throws IOException {
    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>();
        //Create records
        if (recordsToCreate > 0) {
            super.resumeTimer();
            //TODO uncomment this and remove createRecord when RM-4564 issue is fixed
            //uploadElectronicRecordInUnfiledContext(container, userModel, recordsToCreate, RECORD_NAME_IDENTIFIER, loadUnfiledRecordDelay);
            createNonElectonicRecordInUnfiledContext(container, userModel, recordsToCreate,
                    RECORD_NAME_IDENTIFIER, loadUnfiledRecordDelay);
            super.suspendTimer();
            // Clean up the lock
            String lockedPath = container.getPath() + "/locked";
            fileFolderService.deleteFolder(container.getContext(), lockedPath, false);
        }

        DBObject eventData = BasicDBObjectBuilder.start().add(FIELD_CONTEXT, container.getContext())
                .add(FIELD_PATH, container.getPath()).get();
        Event nextEvent = new Event(eventNameUnfiledRecordsLoaded, eventData);

        scheduleEvents.add(nextEvent);
        DBObject resultData = BasicDBObjectBuilder.start()
                .add("msg", "Created " + recordsToCreate + " records.").add("path", container.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", container.getPath()).append("stack", stack).get();
        // Build failure result
        return new EventResult(data, false);
    }
}

From source file:org.alfresco.bm.dataload.rm.unfiled.ScheduleUnfiledRecordFolderLoaders.java

License:Open Source License

/**
 * Helper method for preparing the events that load the root unfiled record folders.
 *
 * @param loaderSessionsToCreate - the number of still active loader sessions
 * @param nextEvents - list of prepared events
 *///from w w w  .ja va  2 s .  c  o m
private void prepareRootUnfiledRecordFolders(int loaderSessionsToCreate, List<Event> nextEvents) {
    int skip = 0;
    int limit = 100;
    while (nextEvents.size() < loaderSessionsToCreate) {
        // Get categories needing loading
        List<FolderData> emptyFolders = fileFolderService.getFoldersByCounts(UNFILED_CONTEXT,
                Long.valueOf(UNFILED_RECORD_CONTAINER_LEVEL), Long.valueOf(UNFILED_RECORD_CONTAINER_LEVEL), //min and max level are 4, level of unfiled record container
                0L, Long.valueOf((rootUnfiledRecordFolderNumber - 1)), //limit the maximum number of child folders to rootUnfiledRecordFolderNumber - 1
                null, null, // Ignore file limits
                skip, limit);
        skip += limit;
        if (emptyFolders.size() == 0) {
            // The folders were populated in the mean time
            break;
        }
        // Schedule a load for each folder
        for (FolderData emptyFolder : emptyFolders) {
            int unfiledRecordFolderToCreate = rootUnfiledRecordFolderNumber
                    - (int) emptyFolder.getFolderCount();
            try {
                // Create a lock folder that has too many files and folders so that it won't be picked up
                // by this process in subsequent trawls
                String lockPath = emptyFolder.getPath() + "/locked";
                FolderData lockFolder = new FolderData(UUID.randomUUID().toString(), emptyFolder.getContext(),
                        lockPath, Long.MAX_VALUE, Long.MAX_VALUE);
                fileFolderService.createNewFolder(lockFolder);
                // We locked this, so the load can be scheduled.
                // The loader will remove the lock when it completes
                DBObject loadData = BasicDBObjectBuilder.start().add(FIELD_CONTEXT, emptyFolder.getContext())
                        .add(FIELD_PATH, emptyFolder.getPath())
                        .add(FIELD_UNFILED_ROOT_FOLDERS_TO_CREATE, Integer.valueOf(unfiledRecordFolderToCreate))
                        .add(FIELD_UNFILED_FOLDERS_TO_CREATE, Integer.valueOf(0)).get();
                Event loadEvent = new Event(eventNameLoadUnfiledRecordFolders, 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);
            } catch (Exception e) {
                // The lock was already applied; find another
                continue;
            }
            // Check if we have enough
            if (nextEvents.size() >= loaderSessionsToCreate) {
                break;
            }
        }
    }
}

From source file:org.alfresco.bm.dataload.rm.unfiled.ScheduleUnfiledRecordFolderLoaders.java

License:Open Source License

/**
 * Helper method for preparing the load of unfiled record folders children.
 *
 * @param loaderSessionsToCreate - the number of still active loader sessions
 * @param nextEvents - list of prepared events
 *//*  w  w  w .j  a v a  2s .  c o m*/
private void prepareUnfiledRecordFolders(int loaderSessionsToCreate, List<Event> nextEvents) {
    int skip = 0;
    int limit = 100;
    while (nextEvents.size() < loaderSessionsToCreate) {
        // Get folders needing loading
        List<FolderData> emptyFolders = fileFolderService.getFoldersByCounts(UNFILED_CONTEXT,
                Long.valueOf(UNFILED_RECORD_CONTAINER_LEVEL + 1), //min level is 5, level of root unfiled record folders
                Long.valueOf(maxLevel - 1), //max level is 4+unfiledRecordFolderDepth-1
                0L, Long.valueOf(unfiledRecordFolderNumber - 1), //limit the maximum number of child folders to rootUnfiledRecordFolderNumber - 1
                null, null, // Ignore file limits
                skip, limit);
        skip += limit;
        if (emptyFolders.size() == 0) {
            // The folders were populated in the mean time
            break;
        }
        // Schedule a load for each folder
        for (FolderData emptyFolder : emptyFolders) {
            int foldersToCreate = unfiledRecordFolderNumber - (int) emptyFolder.getFolderCount();
            try {
                // Create a lock folder that has too many files and folders so that it won't be picked up
                // by this process in subsequent trawls
                String lockPath = emptyFolder.getPath() + "/locked";
                FolderData lockFolder = new FolderData(UUID.randomUUID().toString(), emptyFolder.getContext(),
                        lockPath, Long.MAX_VALUE, Long.MAX_VALUE);
                fileFolderService.createNewFolder(lockFolder);
                // We locked this, so the load can be scheduled.
                // The loader will remove the lock when it completes
                DBObject loadData = BasicDBObjectBuilder.start().add(FIELD_CONTEXT, emptyFolder.getContext())
                        .add(FIELD_PATH, emptyFolder.getPath())
                        .add(FIELD_UNFILED_ROOT_FOLDERS_TO_CREATE, Integer.valueOf(0))
                        .add(FIELD_UNFILED_FOLDERS_TO_CREATE, Integer.valueOf(foldersToCreate)).get();
                Event loadEvent = new Event(eventNameLoadUnfiledRecordFolders, 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);
            } catch (Exception e) {
                // The lock was already applied; find another
                continue;
            }
            // Check if we have enough
            if (nextEvents.size() >= loaderSessionsToCreate) {
                break;
            }
        }
    }
}

From source file:org.alfresco.bm.dataload.rm.unfiled.ScheduleUnfiledRecordLoaders.java

License:Open Source License

/**
 * Helper method for preparing events for loading unfiled records randomly in the unfiled record folders structure or in specified unfiled record folder paths.
 *
 * @param loaderSessionsToCreate - the number of still active loader sessions
 * @param nextEvents - list of prepared events
 *//* w w  w . ja  v a 2s  .c o  m*/
private void prepareUnfiledRecords(int loaderSessionsToCreate, List<Event> nextEvents) {
    calculateListOfEmptyFolders();
    List<FolderData> emptyFolders = new ArrayList<FolderData>();
    emptyFolders.addAll(mapOfRecordsPerUnfiledRecordFolder.keySet());
    while (nextEvents.size() < loaderSessionsToCreate) {
        if (mapOfRecordsPerUnfiledRecordFolder == null || mapOfRecordsPerUnfiledRecordFolder.size() == 0) {
            break;
        }
        // Schedule a load for each folder
        for (FolderData emptyFolder : emptyFolders) {
            int recordsToCreate = mapOfRecordsPerUnfiledRecordFolder.get(emptyFolder)
                    - (int) emptyFolder.getFileCount();
            if (recordsToCreate <= 0) {
                mapOfRecordsPerUnfiledRecordFolder.remove(emptyFolder);
            } else {
                try {
                    // Create a lock folder that has too many files and folders so that it won't be picked up
                    // by this process in subsequent trawls
                    String lockPath = emptyFolder.getPath() + "/locked";
                    FolderData lockFolder = new FolderData(UUID.randomUUID().toString(),
                            emptyFolder.getContext(), lockPath, Long.MAX_VALUE, Long.MAX_VALUE);
                    fileFolderService.createNewFolder(lockFolder);
                    // We locked this, so the load can be scheduled.
                    // The loader will remove the lock when it completes
                    DBObject loadData = BasicDBObjectBuilder.start()
                            .add(FIELD_CONTEXT, emptyFolder.getContext()).add(FIELD_PATH, emptyFolder.getPath())
                            .add(FIELD_RECORDS_TO_CREATE, Integer.valueOf(recordsToCreate)).get();
                    Event loadEvent = new Event(eventNameLoadUnfiledRecords, 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);
                    mapOfRecordsPerUnfiledRecordFolder.remove(emptyFolder);
                } catch (Exception e) {
                    mapOfRecordsPerUnfiledRecordFolder.remove(emptyFolder);
                    // The lock was already applied; find another
                    continue;
                }
            }
            // Check if we have enough
            if (nextEvents.size() >= loaderSessionsToCreate) {
                break;
            }
        }
    }
}