Search in sources :

Example 1 with RestAPIFactory

use of org.alfresco.rest.core.RestAPIFactory 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 2 with RestAPIFactory

use of org.alfresco.rest.core.RestAPIFactory in project records-management by Alfresco.

the class RMBaseEventProcessor method isIdValid.

/**
 * Helper method to check if one id is a valid record folder, unfiled container, or unfiled record folder id.
 *
 * @param id - folder id that is checked
 * @param context - context of the checked folder id
 * @return <code>true</code> if the id is valid, or <code>false</code> otherwhise.
 */
private boolean isIdValid(String id, String context) {
    boolean result = false;
    if (RECORD_FOLDER_CONTEXT.equals(context)) {
        RestAPIFactory restAPIFactory = getRestAPIFactory();
        restAPIFactory.getRecordFolderAPI().getRecordFolder(id);
        String statusCode = restAPIFactory.getRmRestWrapper().getStatusCode();
        if (HttpStatus.valueOf(Integer.parseInt(statusCode)) == HttpStatus.OK) {
            return true;
        }
    } else if (UNFILED_CONTEXT.equals(context)) {
        RestAPIFactory restAPIFactory = getRestAPIFactory();
        restAPIFactory.getUnfiledRecordFoldersAPI().getUnfiledRecordFolder(id);
        String statusCode = restAPIFactory.getRmRestWrapper().getStatusCode();
        if (HttpStatus.valueOf(Integer.parseInt(statusCode)) == HttpStatus.OK) {
            return true;
        } else {
            restAPIFactory.getUnfiledContainersAPI().getUnfiledContainer(id);
            statusCode = restAPIFactory.getRmRestWrapper().getStatusCode();
            if (HttpStatus.valueOf(Integer.parseInt(statusCode)) == HttpStatus.OK) {
                return true;
            }
        }
    }
    return result;
}
Also used : RestAPIFactory(org.alfresco.rest.core.RestAPIFactory)

Aggregations

RestAPIFactory (org.alfresco.rest.core.RestAPIFactory)2 DBObject (com.mongodb.DBObject)1 RecordData (org.alfresco.bm.dataload.rm.services.RecordData)1 Event (org.alfresco.bm.event.Event)1 EventResult (org.alfresco.bm.event.EventResult)1 Record (org.alfresco.rest.rm.community.model.record.Record)1 UserModel (org.alfresco.utility.model.UserModel)1