class EcmFileCreatedEventHandler implements ApplicationListener<EcmEvent> {

    public void onEcmFileCreated(EcmEvent ecmEvent)
    {

        getAuditPropertyEntityAdapter().setUserId(ecmEvent.getUserId());

        // parent folder must already be in ArkCase
        AcmFolder parentFolder = getFolderAndFilesUtils().lookupArkCaseFolder(ecmEvent.getParentNodeId());
        if (parentFolder == null)
        {
            log.debug("Parent folder is not in ArkCase, so not creating the new file {}", ecmEvent.getNodeId());
            return;
        }

        if (getFolderAndFilesUtils().lookupArkCaseFile(ecmEvent.getNodeId(), parentFolder.getId()) != null)
        {
            log.debug("ArkCase already has the file with CMIS id {}", ecmEvent.getNodeId());
            return;
        }

        AcmContainer container = getFolderAndFilesUtils().lookupArkCaseContainer(parentFolder.getId());
        if (container == null)
        {
            log.debug("Can't find container for the new file with id {}, exiting.", ecmEvent.getNodeId());
            return;
        }

        MDC.put(MDCConstants.EVENT_MDC_REQUEST_ALFRESCO_USER_ID_KEY, ecmEvent.getUserId());
        MDC.put(MDCConstants.EVENT_MDC_REQUEST_ID_KEY, UUID.randomUUID().toString());

        String cmisRepositoryId = getFolderService().getCmisRepositoryId(parentFolder);

        Document cmisDocument = getFolderAndFilesUtils().lookupCmisDocument(cmisRepositoryId, ecmEvent.getNodeId());
        if (cmisDocument == null)
        {
            log.error("No document to be loaded - exiting.");
            return;
        }

        try
        {

            EcmFile addedToArkCase = getFileService().upload(
                    ecmEvent.getNodeName(),
                    getFolderAndFilesUtils().findFileType(cmisDocument),
                    "Document",
                    cmisDocument.getContentStream().getStream(),
                    cmisDocument.getContentStreamMimeType(),
                    ecmEvent.getNodeName(),
                    new UsernamePasswordAuthenticationToken(ecmEvent.getUserId(), ecmEvent.getUserId()),
                    parentFolder.getCmisFolderId(),
                    container.getContainerObjectType(),
                    container.getContainerObjectId(),
                    parentFolder.getCmisRepositoryId(),
                    cmisDocument);
            log.info("Added file to ArkCase with CMIS ID [{}] and ArkCase ID [{}]", ecmEvent.getNodeId(), addedToArkCase.getId());

        }
        catch (AcmCreateObjectFailedException | AcmUserActionFailedException e)
        {
            log.error("Could not add file with CMIS ID [{}] to ArkCase: {}", ecmEvent.getNodeId(), e.getMessage(), e);
        }

    }

}