At my internship at an IT company, I was assigned a task to write either a script or a java program to convert a CSV file into an LDIF file format which is I believe many organization use to populate/modify/delete their directory of many users. I am attempting to write a java program that would help me convert CSV file into an LDIF file. This LDIF file is then imported into an e-directory to add new users. Currently, I have a simple program that works but require major enhancement which I am unable to deliver.
Screenshot of Sample CSV file (First line is the header):
csv file sample
Sample CSV file in Notepad++:
csv file in Notepad++
(The first row is the header. Each element in a row is separated by commas (,))
Sample LDIF file generated using sample code given below (OUTPUT):
dn: cn=demotest1, ou=Data, o=Data
changetype: add
ou: Data
objectClass: dt1
objectClass: test_demo1
objectClass: demotest1
objectClass: Employee
cn: demotest1
uid: test_demo1
SAMAccountName: demt1
givenName: demotest1
sn: dt1
dn: cn=demotest2, ou=Data, o=Data
changetype: add
ou: Data
objectClass: dt2
objectClass: test_demo2
objectClass: demotest2
objectClass: Employee
cn: demotest2
uid: test_demo2
SAMAccountName: demt2
givenName: demotest2
sn: dt2
Note: First row as the header is excluded from the output. Each row in the CSV file is converted into a set of data (lines= dn: to sn:) and each set of data is separated by a blank line.
Given below is the code sample I used to generate the above LDIF file:
package readcsv;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
/**
*
* @author Dorjee
*/
public class ReadCSV {
public static void main(String[] args) {
ReadCSV obj = new ReadCSV();
obj.run();
}
public void run() {
String csvFile = "/Users/Dorjee/Desktop/sampleCSV.csv"; //Path of file to be read.
BufferedReader br = null;
String line = "";
String csvSplitBy = ",";
String[] column;
int count = 0;
try {
PrintStream out = new PrintStream(new FileOutputStream("OutputLDIFFile.ldif"));
br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {
// using comma as separator
column = line.split(csvSplitBy);
//End format of the ouput file.
//Change according to .CSV file.
//Count used to exclude the reading of the first line.
if (count > 0) {
out.println("dn: cn="+column[5]+", ou="+column[7]+", o=Data"+
"\nchangetype: " + column[2]
+ "\nou: " + column[7]
+ "\nobjectClass: " + column[3]
+ "\nobjectClass: " + column[4]
+ "\nobjectClass: " + column[5]
+ "\nobjectClass: " + column[6]
+ "\ncn: " + column[5]
+ "\nuid: "+column[4]
+ "\nSAMAccountName: "+column[1]
+ "\ngivenName: "+column[0]
+ "\nsn: "+column[3]
+ "\n"
);
}
count++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("Done");
}
}
This is a Simple code that helps generate the LDIF file from CSV file. It is helpful since the set of data to be generated here can span to thousands of lines. But this code is obviously not adequate. As you can see, I will have to alter the codes at the highlighted area above every time a different CSV file with varied number of columns needs to be converted to LDIF file. It takes up a good amount of time as I have come across files with more than fifty columns and it increases chances of error while I am altering the code manually. On top of that some columns have empty values (which needs to be excluded from the output).
• How can I exclude the empty values from being in the output?
• Is there a way to automate the generation of output even if the CSV file has different number of columns? I tried ArrayList but I could not figure a way to solve the issue.
Something like this should skip empty columns:
out.println("dn: cn="+column[5]+", ou="+column[7]+", o=Data"+
"\nchangetype: " + column[2]
+ ((column[7].length()>0)?"\nou: " + column[7]:"")
+ ((column[3].length()>0)?"\nobjectClass: " + column[3]:"")
+ ((column[4].length()>0)?"\nobjectClass: " + column[4]:"")
+ ((column[5].length()>0)?"\nobjectClass: " + column[5]:"")
+ ((column[6].length()>0)?"\nobjectClass: " + column[6]:"")
+ ((column[5].length()>0)?"\ncn: " + column[5]:"")
+ ((column[4].length()>0)?"\nuid: "+column[4]:"")
+ ((column[1].length()>0)?"\nSAMAccountName: "+column[1]:"")
+ ((column[0].length()>0)?"\ngivenName: "+column[0]:"")
+ ((column[3].length()>0)?"\nsn: "+column[3]:"")
+ "\n"
);
If you also need to check the number of columns:
out.println("dn: cn="+column[5]+", ou="+column[7]+", o=Data"+
"\nchangetype: " + column[2]
+ ((column.length > 7 && column[7].length()>0)?"\nou: " + column[7]:"")
+ ((column.length > 3 && column[3].length()>0)?"\nobjectClass: " + column[3]:"")
+ ((column.length > 4 && column[4].length()>0)?"\nobjectClass: " + column[4]:"")
+ ((column.length > 5 && column[5].length()>0)?"\nobjectClass: " + column[5]:"")
+ ((column.length > 6 && column[6].length()>0)?"\nobjectClass: " + column[6]:"")
+ ((column.length > 5 && column[5].length()>0)?"\ncn: " + column[5]:"")
+ ((column.length > 4 && column[4].length()>0)?"\nuid: "+column[4]:"")
+ ((column.length > 1 && column[1].length()>0)?"\nSAMAccountName: "+column[1]:"")
+ ((column.length > 0 && column[0].length()>0)?"\ngivenName: "+column[0]:"")
+ ((column.length > 3 && column[3].length()>0)?"\nsn: "+column[3]:"")
+ "\n"
);
It is looking a little ugly it might be easier with a helper function.
static public String check(String column[],String line,int index) {
return ((column.length > index && column[index].length()>0)?line + column[index]:"");
}
...
out.println("dn: cn="+column[5]+", ou="+column[7]+", o=Data"+
"\nchangetype: " + column[2]
+ check(column,"\nou: ",7)
+ check(column,"\nobjectClass: " ,3)
+ check(column,"\nobjectClass: ",4)
+ check(column,"\nobjectClass: ",5)
+ check(column,"\nobjectClass: ",6)
+ check(column,"\ncn: ",5)
+ check(column,"\nuid: ",4)
+ check(column,"\nSAMAccountName: ",1)
+ check(column,"\ngivenName: ",0)
+ check(column,"\nsn: ",3)
+ "\n"
);