Search in sources :

Example 6 with UserModel

use of org.alfresco.utility.model.UserModel 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);
    }
}
Also used : UserModel(org.alfresco.utility.model.UserModel) RecordData(org.alfresco.bm.dataload.rm.services.RecordData) EventResult(org.alfresco.bm.event.EventResult) RestAPIFactory(org.alfresco.rest.core.RestAPIFactory) Event(org.alfresco.bm.event.Event) Record(org.alfresco.rest.rm.community.model.record.Record) DBObject(com.mongodb.DBObject)

Example 7 with UserModel

use of org.alfresco.utility.model.UserModel in project records-management by Alfresco.

the class PrepareRMSite method processEvent.

/**
 * @see org.alfresco.bm.event.AbstractEventProcessor#processEvent(org.alfresco.bm.event.Event)
 */
@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);
    BasicDBObjectBuilder builder = BasicDBObjectBuilder.start();
    builder.add(FIELD_SITE_ID, RM_SITE_ID).add(FIELD_SITE_MANAGER_NAME, getUsername()).add(FIELD_SITE_MANAGER_PASSWORD, getPassword());
    boolean existsRMSite = restAPIFactory.getRMSiteAPI(userModel).existsRMSite();
    // RM site exists and it is loaded in MongoDB
    if (existsRMSite && rmSite != null && 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 == null) {
        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);
}
Also used : UserModel(org.alfresco.utility.model.UserModel) SiteData(org.alfresco.bm.site.SiteData) EventResult(org.alfresco.bm.event.EventResult) BasicDBObjectBuilder(com.mongodb.BasicDBObjectBuilder) UserData(org.alfresco.bm.user.UserData) ArrayList(java.util.ArrayList) Event(org.alfresco.bm.event.Event) DBObject(com.mongodb.DBObject)

Aggregations

UserModel (org.alfresco.utility.model.UserModel)7 Event (org.alfresco.bm.event.Event)6 DBObject (com.mongodb.DBObject)5 EventResult (org.alfresco.bm.event.EventResult)5 ArrayList (java.util.ArrayList)3 SiteData (org.alfresco.bm.site.SiteData)3 RecordData (org.alfresco.bm.dataload.rm.services.RecordData)2 SiteMemberData (org.alfresco.bm.site.SiteMemberData)2 UserData (org.alfresco.bm.user.UserData)2 BasicDBObjectBuilder (com.mongodb.BasicDBObjectBuilder)1 FolderData (org.alfresco.bm.cm.FolderData)1 EventAlreadyScheduledException (org.alfresco.bm.dataload.rm.exceptions.EventAlreadyScheduledException)1 RestAPIFactory (org.alfresco.rest.core.RestAPIFactory)1 FilePlan (org.alfresco.rest.rm.community.model.fileplan.FilePlan)1 Record (org.alfresco.rest.rm.community.model.record.Record)1 RMSite (org.alfresco.rest.rm.community.model.site.RMSite)1 TransferContainer (org.alfresco.rest.rm.community.model.transfercontainer.TransferContainer)1 UnfiledContainer (org.alfresco.rest.rm.community.model.unfiledcontainer.UnfiledContainer)1 RMSiteAPI (org.alfresco.rest.rm.community.requests.gscore.api.RMSiteAPI)1 Test (org.junit.Test)1