I have a csv file with multiple columns that I need to use to create a custom object for mapping values. Column 1 being key for column 2, Column 2 key for column3 etc.
Has anyone implemented this and can provide any guidance?
Hi Rebekah,
Welcome to SailPoint Developer community.
Yes, you can implement this. I have done this couple of years back.
Will provide you some sample code for the same.
Thanks
Krish
Here is a sample code
?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE Rule PUBLIC "sailpoint.dtd" "sailpoint.dtd">
<Rule language="beanshell" name="Write to Custom Object">
<Source>
import sailpoint.object.Identity;
import sailpoint.object.Custom;
import java.util.*;
import java.io.*;
Map hashMap = new HashMap();
List valueList = new ArrayList();
String line = "";
BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\Downloads\\CSVDemo.csv"));
try
{
//parsing a CSV file into BufferedReader class constructor
while ((line = br.readLine()) != null) //returns a Boolean value
{
String[] data = line.split(",");
if(hashMap.containsKey(data[0])){
valueList = new ArrayList();
hashMap.put(data[0],valueList);
}else{
valueList.add(data[1]);
valueList.add(data[2]);
hashMap.put(data[0],valueList);
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
Custom cust = new Custom();
cust.setName("Test Custom Object");
cust.put("data",hashMap);
context.saveObject(cust);
context.commitTransaction();
</Source>
</Rule>
This works but it’s getting tricky with the large number of value>list mappings. Would the best way to address be to write additional maps for each data element with if data[0] matches to create a new key/value pair for data[1] putting data [2] as value list and so on for each subkey or is there a better way?
i.e.
Value1, secondValue1, thirdValue1, fourthValue1
Value1, secondValue1 third,Value1, fourthValue2
Value1, secondValue2, thirdValue1 fourthValue1
With the help of your code @sunnyajmera I was able to accomplish the nested value mapping I needed. Ended up with this:
import sailpoint.object.Identity;
import sailpoint.object.Custom;
import java.util.*;
import java.io.*;
List<String> header = null;
Map customData = new HashMap();
String line = "";
BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\CustomMap`Preformatted text`.csv"));
try {
while ((line = br.readLine()) != null) {
String[] data = line.split(",");
if (header == null) {
header = Arrays.asList(data);
} else {
String key = data[0];
if ("data".equals(key)) {
continue;
}
if (!customData.containsKey(key)) {
customData.put(key, new HashMap());
}
Map nestedMap = (Map) customData.get(key);
for (int i = 1; i < data.length; i += 2) {
String nestedKey = data[i];
String nestedValue = data[i + 1];
if (!nestedMap.containsKey(nestedKey)) {
nestedMap.put(nestedKey, new ArrayList());
}
List nestedList = (List) nestedMap.get(nestedKey);
nestedList.add(nestedValue);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
Custom cust = new Custom();
cust.setName("Test Custom Object");
cust.put("data", customData);
context.saveObject(cust);
context.commitTransaction();
This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.