class EcmFileFolderMovedEventHandler implements ApplicationListener<EcmEvent> {

    public void onEcmFileMoved(EcmEvent ecmEvent)
    {
        getAuditPropertyEntityAdapter().setUserId(ecmEvent.getUserId());

        AcmFolder sourceParentFolder = getFolderAndFilesUtils().lookupArkCaseFolder(ecmEvent.getSourceParentNodeId());
        AcmFolder targetParentFolder = getFolderAndFilesUtils().lookupArkCaseFolder(ecmEvent.getTargetParentNodeId());

        if (ecmEvent.getNodeType().equals(EcmFileConstants.ECM_SYNC_NODE_TYPE_DOCUMENT))
        {
            if (sourceParentFolder != null && targetParentFolder != null)
            {
                moveFile(ecmEvent, sourceParentFolder, targetParentFolder);
            }
            else if (sourceParentFolder != null && targetParentFolder == null)
            {
                deleteFile(ecmEvent, sourceParentFolder);
            }
            else if (sourceParentFolder == null && targetParentFolder != null)
            {
                try
                {
                    String message = String.format(MOVED_INTO_MESSAGE, ecmEvent.getNodeType());
                    getFolderService().removeLockAndSendMessage(targetParentFolder.getId(), message);

                    getFolderAndFilesUtils().uploadFile(ecmEvent, targetParentFolder);
                }
                catch (AcmCreateObjectFailedException | AcmUserActionFailedException e)
                {
                    log.error("Could not add file with CMIS ID [{}] to ArkCase: {}", ecmEvent.getNodeId(), e.getMessage(), e);
                }
            }
        }
        else if (ecmEvent.getNodeType().equals(EcmFileConstants.ECM_SYNC_NODE_TYPE_FOLDER))
        {
            if (sourceParentFolder != null && targetParentFolder != null)
            {
                moveFolder(ecmEvent, targetParentFolder);
            }
            else if (sourceParentFolder != null && targetParentFolder == null)
            {
                deleteFolder(ecmEvent);
            }
            else if (sourceParentFolder == null && targetParentFolder != null)
            {
                AcmFolder created = null;
                try
                {
                    String message = String.format(MOVED_INTO_MESSAGE, ecmEvent.getNodeType());
                    getFolderService().removeLockAndSendMessage(targetParentFolder.getId(), message);

                    created = getFolderService().createFolder(targetParentFolder, ecmEvent.getNodeId(), ecmEvent.getNodeName());
                }
                catch (AcmUserActionFailedException | AcmFolderException e)
                {
                    log.debug("Can't create new folder with node id [{}]", ecmEvent.getNodeId());
                }

                try
                {
                    getFolderService().recordMetadataOfExistingFolderChildren(created, ecmEvent.getUserId());
                }
                catch (AcmUserActionFailedException | AcmObjectNotFoundException e)
                {
                    log.debug("Can not create folder children for folder with id: {} and name: {} ", created.getId(), created.getName());
                }

                log.debug("Finished creating new folder with node id {}, ArkCase id {}", created.getCmisFolderId(), created.getId());
            }
        }
    }

}