Tuesday, May 25, 2010

file copy

/*

Developer:    Andrew Casey
Date:         20/05/2010
Reference:    REC20.15
Description:  Created a new class to copy a file to a new location.

*/

/**
* This class has been created to allow move and copy functions, rather than using a java class
* or an Exec() function. 
*
* The class uses the file attachment functions and relies on the FILEDB url to put/get/delete 
* the attachment.
*
* @version 1.0
* @author  Andrew Casey
*/
class copyFile;
   
   method copyFile();
   method copyTheFile(&strCurrFile As string, &strNewFile As string, &blnDeleteFile As boolean, &bnDeleteAttach As boolean);
   
end-class;

/**
  * This is an empty constructor method.
  *
  */
method copyFile
   
end-method;

/**
  * This method will perform the "file copy".
  *
  * @param &strCurrFile    This is the full path and name for the file to be copied.
  * @param &strNewFile     This is the full path and name of the destination, ie. where 
  *                        the file should be copied to.
  * @param &blnDeleteFile  A boolean switch on whether or not the original file should be
  *                        deleted. A True would work like a "move" and a False would work
  *                        like a "copy".
  * @param &bnDeleteAttach A boolean switch on whether or not to remove the file attachment 
  *                        from the database record.
  */
method copyTheFile
   /+ &strCurrFile as String, +/
   /+ &strNewFile as String, +/
   /+ &blnDeleteFile as Boolean, +/
   /+ &bnDeleteAttach as Boolean +/
   
   Local File &flOrigFile;
   
   Local array of string &aryFilePath;
   
   Local integer &intRtrn, &i;
   Local string &strPSHome, &strSeparator, &strCurrFileName;
   Local boolean &blnExists;
   
   rem Derive the separator - used later in the split function;
   &strPSHome = GetEnv("PS_HOME");
   
   rem if the file is on unix then the first character will be a "/";
   If Substring(&strPSHome, 1, 1) = "/" Then;
      &strSeparator = "/";
   Else
      &strSeparator = "\";
   End-If;
   
   rem Now check that the "current" file actually exists;
   If FileExists(&strCurrFile, %FilePath_Absolute) Then;
      
      rem Derive the name of the actual file - for the "current" file;
      &aryFilePath = Split(&strCurrFile, &strSeparator);
      &strCurrFileName = &aryFilePath [&aryFilePath.Len];
      
      rem Add the file to the database;
      &intRtrn = PutAttachment(URL.FILEDB, &strCurrFileName, &strCurrFile);
      If &intRtrn <> %Attachment_Success Then;
         WriteToLog(%ApplicationLogFence_Error, "Add Attachment failed. PutAttachment error: " | String(&intRtrn) | ". File: " | &strCurrFile);
      Else
         rem Adding the attachment did work - now place the attachment where required;
         &intRtrn = GetAttachment(URL.FILEDB, &strCurrFileName, &strNewFile);
         If &intRtrn <> %Attachment_Success Then;
            WriteToLog(%ApplicationLogFence_Error, "Get Attachment failed. GetAttachment error: " | String(&intRtrn) | ". File: " | &strCurrFileName);
         Else
            rem Remove the attachment from the database - if necessary;
            If &bnDeleteAttach Then;
               &intRtrn = DeleteAttachment(URL.FILEDB, &strCurrFileName);
               If &intRtrn <> %Attachment_Success Then;
                  WriteToLog(%ApplicationLogFence_Error, "Delete Attachment failed. DeleteAttachment error: " | String(&intRtrn) | ". File: " | &strCurrFileName);
               End-If;
            End-If;
            rem Remove the original file - if necessary;
            &flOrigFile = GetFile(&strCurrFile, "R", %FilePath_Absolute);
            If &flOrigFile.IsOpen Then;
               &flOrigFile.Delete();
            End-If;
            &flOrigFile.Close();
         End-If;
      End-If;
   Else
      rem File doesn't exist;
      WriteToLog(%ApplicationLogFence, "File does not exist: " | &strCurrFile);
   End-If;
   
end-method;