Saturday, July 3, 2021

Sailpoint IdentityIQ - Rule - PostIterate

 Rule  - PostIterate

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE Rule PUBLIC "sailpoint.dtd" "sailpoint.dtd">
<Rule language="beanshell" name="EXP Post Iterate Rule" type="PostIterate">
  <Description>
	This rule is called after the connector processes the data in a file.
	This rule archives the data file to the archive folder with the file name appended with the latest timestamp.   
  </Description>
  <Signature returnType="void">
    <Inputs>
      <Argument name="log">
        <Description>
          The log object associated with the SailPointContext.
        </Description>
      </Argument>
      <Argument name="context">
        <Description>
          A sailpoint.api.SailPointContext object that can be used to query the database if necessary.
        </Description>
      </Argument>
      <Argument name="application">
        <Description>
          Application being iterated.
        </Description>
      </Argument>
      <Argument name="schema">
        <Description>
          Schema requested during iteration.
        </Description>
      </Argument>
      <Argument name="stats">
        <Description>
            A map of the stats for the current file that was just iterated Contains keys:
				fileName : (String) filename of the file about to be processed
				absolutePath : (String) absolute filename
				length : (Long) length in bytes
				lastModified : (Long) last time the file was updated Java GMT
				columnNames : (List) column names that were used during the iteration
				objectsIterated : (Long) total number of objects iterated during this run
        </Description>
      </Argument>
    </Inputs>
  </Signature>
  <Source>
	
	    import java.io.File;
		import java.text.SimpleDateFormat;
		import java.util.Date;

	   public boolean archiveFile(String path, String fileName, String extension){

		boolean success = true;
		Date date = new Date();
		String timeStamp = new SimpleDateFormat("MM-dd-yyyy_HH:mm:ss").format(date);

		String archivePath = path+"/archive";
		File archiveFolder = new File(archivePath);

		String oldFileName = path+"/"+fileName+extension;
		File oldFile = new File(oldFileName);
		if (!archiveFolder.exists()){
			success = archiveFolder.mkdir();
		}
		if(success){
			File newFile = new File(archiveFolder+"/"+fileName+"_"+timeStamp+extension);
			success = oldFile.renameTo(newFile);
		}
		return success;
	}
	
	System.out.println("Entering Post Iterate Rule");
	
	String absolutePath = stats.get("absolutePath");
	String fileName = stats.get("fileName");
	String fileNameWithoutExtension = fileName.substring(0,fileName.indexOf("."));
	String extension = fileName.substring(fileName.indexOf("."),fileName.length());
	String filePath = absolutePath.replace("/"+fileName, "");
		
		System.out.println("filePath : "+filePath);
		System.out.println("fileNameWithoutExtension : "+fileNameWithoutExtension);
		System.out.println("extension : "+extension);
	
	boolean success = archiveFile(filePath, fileNameWithoutExtension, extension);	
	System.out.println("Exiting Post Iterate Rule");	
  
  </Source>
</Rule>