Friday, March 5, 2021

Reading CSV File Data

Sample data

RoleName,displayName,RoleType,locCity,coNumber
AAA1,AAA1 DN,business,AA,1
AAA1,AAA1 DN,business,BB,2
AAA1,AAA1 DN,business,CC,3
AAA2,AAA2 DN,business,AA,1
AAA2,AAA2 DN,business,BB,2
AAA2,AAA2 DN,business,CC,3
AAA2,AAA2 DN,business,DD,4
AAA2,AAA2 DN,business,EE,5
AAA2,AAA2 DN,,business,FF,6
AAA2,AAA2 DN,business,GG,7
AAA2,AAA2 DN,business,HH,8
AAA2,AAA2 DN,business,II,9
AAA2,AAA2 DN,business,JJ,10
AAA2,AAA2 DN,business,KK,11
AAA2,AAA2 DN,business,LL,12
AAA2,AAA2 DN,business,MM,13
AAA1,AAA1 DN,business,DD,4
AAA1,AAA1 DN,business,EE,5
AAA2,AAA2 DN,business,NN,14

Sample Code
 
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE Rule PUBLIC "sailpoint.dtd" "sailpoint.dtd">
<Rule  language="beanshell"  name="VIS CreateBundle">
  <Source>
  <![CDATA[
import java.io.File;
import java.util.List;
import java.util.HashMap;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import sailpoint.api.SailPointContext;
import sailpoint.api.SailPointFactory;
import sailpoint.object.Bundle;
import sailpoint.object.Identity;
import sailpoint.object.IdentitySelector;
import sailpoint.spring.SpringStarter;
import sailpoint.tools.GeneralException;
import sailpoint.tools.RFC4180LineParser;
import sailpoint.object.AccountSelectorRules;
import sailpoint.object.IdentitySelector.MatchTerm;
import sailpoint.object.IdentitySelector.MatchExpression;

		
		String dlm = ",";
		String csvFileName = "/tmp/bCopy/SampleBundle.csv";

		System.out.println("Role Creation Started ...");

		File bundleFile = new File(csvFileName);
		if ((!bundleFile.exists()) || (bundleFile.isDirectory())) {
			System.out.println("Unable to find the bundle csv file: " + csvFileName);
			return;
		}

		System.out.println("Reading Bundle Data from: '" + csvFileName);

		
		try {
			int lineCounter = 0;
			ArrayList headerStrings = new ArrayList();
			String thisLine = "";
		
			BufferedReader fileIn = new BufferedReader(new FileReader(csvFileName));

			RFC4180LineParser parser = new RFC4180LineParser(dlm);

			while (null != (thisLine = fileIn.readLine())) {
				ArrayList tokens = parser.parseLine(thisLine);
				if (lineCounter == 0) {
					for (int i = 0; i<tokens.size(); i++) {
						headerStrings.add((String) (tokens.get(i)));
					}
				}else {
					HashMap lineHash = new HashMap();
					for (int i = 0; i<tokens.size(); i++) {
						String headerString = headerStrings.get(i);
						String valueString = (String) tokens.get(i);
						if (null != valueString) {
							valueString = valueString.trim();
						}else{
							//Added Else Condition
							valueString = "";
						}
						lineHash.put(headerString, valueString);
					}
					try{ 
						//buildRole(lineHash);
					}catch (Exception e){
						System.out.println("Error While Creating Bundle " + lineHash);				
					}	
				}
				lineCounter++;				
			}
			fileIn.close();
			System.out.println("Role Creation Completed ...");					
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (GeneralException e) {
			e.printStackTrace();
		}
]]>
  </Source>
</Rule>

No comments:

Post a Comment