class EcmFileFolderCopiedEventHandler implements ApplicationListener<EcmEvent> {

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

        // if the copied file's folder ID is not an ArkCase folder, then the file was copied to a
        // non-ArkCase destination, and no further work is needed
        AcmFolder targetParentFolder = getFolderAndFilesUtils().lookupArkCaseFolder(ecmEvent.getParentNodeId());
        if (targetParentFolder == null)
        {
            log.debug("Can't find folder for the copied file with id {}, no further action.", ecmEvent.getNodeId());
            return;
        }

        if (ecmEvent.getNodeType().equals(EcmFileConstants.ECM_SYNC_NODE_TYPE_DOCUMENT))
        {
            // If it already exists, the copy has already been done in ArkCase, and no further work is needed
            EcmFile arkCaseFile = getFolderAndFilesUtils().lookupArkCaseFile(ecmEvent.getNodeId(), targetParentFolder.getId());
            if (targetParentFolder != null && arkCaseFile == null)
            {
                List<EcmFile> listFiles = getFileDao().findByCmisFileId(ecmEvent.getSourceOfCopyNodeId());
                if (listFiles.isEmpty())
                {
                    // not in arkcase, so the folder that the file was copied from is not an Arkcase folder
                    try
                    {
                        getFolderAndFilesUtils().uploadFile(ecmEvent, targetParentFolder);
                    }
                    catch (AcmCreateObjectFailedException | AcmUserActionFailedException e)
                    {
                        log.debug("Could not add file with CMIS ID [{}] to ArkCase: [{}]", ecmEvent.getNodeId(), e.getMessage(), e);
                    }
                }
                else
                {
                    // in arkcase, so the folder that the file was copied from is Arkcase folder
                    copyFile(ecmEvent, targetParentFolder, listFiles);
                }
            }
        }
        else if (ecmEvent.getNodeType().equals(EcmFileConstants.ECM_SYNC_NODE_TYPE_FOLDER))
        {
            AcmFolder arkCaseFolder = getFolderAndFilesUtils().lookupArkCaseFolder(ecmEvent.getNodeId());
            if (targetParentFolder != null && arkCaseFolder == null)
            {
                AcmFolder created = null;
                try
                {
                    String message = String.format(COPY_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());
            }
            else if (targetParentFolder != null && arkCaseFolder != null)
            {
                try
                {
                    List<AcmObject> acmObjects = getFolderService().getFolderChildren(arkCaseFolder.getId());
                    if (acmObjects.isEmpty())
                    {
                        getFolderService().recordMetadataOfExistingFolderChildren(arkCaseFolder, ecmEvent.getUserId());
                    }
                }
                catch (AcmUserActionFailedException | AcmObjectNotFoundException e)
                {
                    log.debug("Could not create children in folder with CMIS ID [{}] to ArkCase: [{}]", ecmEvent.getNodeId(),
                            e.getMessage(),
                            e);
                }

            }
        }
    }

}