AnsweredAssumed Answered

FTP to Alfresco FTP transfer

Question asked by patil on Dec 16, 2010
Latest reply on May 9, 2011 by patil
Hi ,

I have to transfer the files from FTP to alfresco. For this i am using FTP to FTP(Alfresco) transfer. I am successful in doing this.

But while moving the content, I should be able to create the contents of custom Content types and assign the metadata.
How to achieve this?

I am using the below code for the  FTP to FTP trasfer(IT makes cm:content type transfer)

   FTPUtilityJobs.checkFTPConnection(alfrescoClient, alfrescoFtpIP, Integer.valueOf(alfrescoFtpPort));
      FTPUtilityJobs.checkFTPConnection(client, ftpIP, Integer.valueOf(ftpPort));

      ftpLogin = client.login(ftpUserName, ftpPassword);
      LOGGER.debug("logged in to the ftp:::" + ftpLogin);
      LOGGER.debug("Parameters of the userna");
      alfrescoLogin = alfrescoClient.login(alfrescoFtpUserName,
      LOGGER.debug("logged in to the Alfresco:::" + alfrescoLogin);

      if (ftpLogin && alfrescoLogin) {
         LOGGER.debug("Connected to both the ftps");

         //create the initially required folder structure
         FTPUtilityJobs.createInitialFolderStructure(alfrescoClient ,alfrescoFtpDestinationPath);
         FTPUtilityJobs.setInitialChangeWorkingDirectory(alfrescoFtpDestinationPath, alfrescoClient);
          LOGGER.debug("Working directory is::"+alfrescoClient.printWorkingDirectory());
         List<String> destFilesAndDirectory = FTPUtilityJobs.getRecursiveFileListingWithChangeDirectory(alfrescoClient,alfrescoFtpDestinationPath);
         //read all the files from the ftp from where the files to be migrated
         Map<String,FTPFile> foldersAndDirectory = FTPUtilityJobs.getRecursiveFileListingMap(client,ftpSourcePath);
         Set<String> set = foldersAndDirectory.keySet();            
         Iterator<String> iterator =set.iterator();
         List<String> sortePaths = new ArrayList<String>();
         //sorted so that folder creation paths will be proper
         //get the alfresco structure to decide whether to copy or not
         //Map<String,FTPFile> alfrescoFoldersAndDirectory = FTPUtilityJobs.getRecursiveFileListingMap(alfrescoClient,alfrescoFtpDestinationPath);
            // now create alll the folders         
         createRequiredFolderStructure(sortePaths, alfrescoClient, foldersAndDirectory, alfrescoFtpDestinationPath, ftpSourcePath);
            //Now start moving the files
            for(String locationKey :sortePaths){         
               int attempt = 0;
               //String locationKey =;
               FTPFile file = foldersAndDirectory.get(locationKey);         
               if (!file.isDirectory()) {
                  // Let's just assume success for now.
                  LOGGER.debug("file transfer started");

                  // Although you would think the store command should be
                  // sent to server2
                  // first, in reality, ftp servers like wu-ftpd start
                  // accepting data
                  // connections right after entering passive mode.
                  // Additionally, they
                  // don't even send the positive preliminary reply until
                  // after the
                  // transfer is completed (in the case of passive mode
                  // transfers).
                  // Therefore, calling store first would hang waiting for
                  // a preliminary
                  // reply.

                  if (client.remoteRetrieve(locationKey)) {
                     LOGGER.debug("retrieving from FTP servers is successful");                                             
                     boolean uploaded = alfrescoClient.remoteStore(alfrescoFtpDestinationPath+(locationKey.substring(ftpSourcePath.length())));
                     if(uploaded || attempt>=2){
                        LOGGER.debug("retrieving and creating from FTP servers is successful");   
                         break __reattempt;

               } /*else if (file.isDirectory()) {
                  LOGGER.debug("Creatring the directory::"
                        + file.getName());
                  // Creates a new subdirectory on the FTP server in the
                  // current directory (if a relative pathname is given)
                  // or where specified (if an absolute pathname is
                  // given).
                  if (alfrescoClient.makeDirectory(alfrescoFtpDestinationPath+(locationKey.substring(ftpSourcePath.length())))) {
                     LOGGER.debug("Folder structure created in the path:::"+alfrescoFtpDestinationPath+(locationKey.substring(ftpSourcePath.length())));
                  } else {
                           LOGGER.debug("Could not create the folderin alfresco"+alfrescoFtpDestinationPath+(locationKey.substring(ftpSourcePath.length())));

               }catch (Exception e) {
                  LOGGER.error("Error happened in connecting to the alfresco FTP serer");      
                  throw new WebScriptException("Exception happened while transferring the files");
                  LOGGER.debug("Already the file exists now dont copy the content");