-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSVParser.java
More file actions
175 lines (155 loc) · 5.46 KB
/
Copy pathCSVParser.java
File metadata and controls
175 lines (155 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
public class CSVParser {
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String csvPath = "";
System.out.println("Enter your CSV file path:");
try {
csvPath = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
CSVParser obj = new CSVParser();
obj.execute(csvPath);
}
public void execute(String csvPath) {
//Lists for values:
List<String[]> dArrayList = new ArrayList<String[]>();
List<String[]> eArrayList = new ArrayList<String[]>();
List<String[]> sArrayList = new ArrayList<String[]>();
try {
loadCSV(dArrayList, eArrayList, sArrayList, csvPath);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//Time to insert into SQL file!
File file = new File("insert_commands.sql");
FileWriter fw = null;
BufferedWriter bw = null;
try {
file.createNewFile();
fw = new FileWriter(file.getAbsoluteFile());
bw = new BufferedWriter(fw);
//Write insert lines for dArrayList:
String dValues = getInsertDepartments(dArrayList);
bw.write("INSERT INTO department (dep_id,dep_name) VALUES\n\t" + dValues + ";\n\n");
//Write insert lines for eArrayList:
String eValues = getInsertEmployees(eArrayList);
bw.write("INSERT INTO employee (type,emp_id,first_name,last_name,gender,hire_date,termination_date,department1,department2,bonus,manager_start_date) VALUES\n\t" + eValues + ";\n\n");
//Write insert lines for sArrayList:
String sValues = getInsertSalaries(sArrayList);
bw.write("INSERT INTO salary (emp_id,start_date,end_date,salary_amt) VALUES \n\t" + sValues + ";");
bw.close();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
//Loads given Lists with data provided from csvPath.
private static void loadCSV(List<String[]> dArrayList, List<String[]> eArrayList, List<String[]> sArrayList, String csvPath) throws IOException {
BufferedReader br = null;
String line;
br = new BufferedReader(new FileReader(csvPath));
while ((line = br.readLine()) != null) {
String[] values = line.split(",");
String header = values[0];
//Check if department type:
if (header.length() == 1) {
dArrayList.add(values);
//Check if employee type:
} else if (header.equals("MANAGER") || header.equals("EMPLOYEE")) {
eArrayList.add(values);
//Check if salary type:
} else {
sArrayList.add(values);
}
}
br.close();
}
//Converts List of departments to String values.
private static String getInsertDepartments(List<String[]> dArrayList) {
String dValues = "";
for (String[] dArray : dArrayList) {
dValues = dValues + "(" + dArray[0] + ",\"" + dArray[1] + "\"),\n\t";
}
return dValues.substring(0, dValues.length() - 3);
}
//Converts List of employees to String values.
private static String getInsertEmployees(List<String[]> eArrayList) throws ParseException {
String eValues = "";
for (String[] eArray : eArrayList) {
eValues = eValues + "(";
for (int i = 0; i < eArray.length; i++) {
//For department:
if (i == 7) {
String[] depArray = eArray[i].split(";");
if (depArray.length > 1) {
eValues = eValues + depArray[0] + "," + depArray[1] + ",";
} else {
eValues = eValues + depArray[0] + ",NULL,";
}
//For dates:
} else if (!eArray[i].equals("null") && (i == 5 || i == 6 || (eArray[0].equals("MANAGER") && i == 9))) {
SimpleDateFormat inFormat = new SimpleDateFormat("M/d/yyyy");
SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd");
String date = outFormat.format(inFormat.parse(eArray[i]));
eValues = eValues + "\"" + date + "\",";
//All other cases:
} else {
if (eArray[i].equals("null")) {
eValues = eValues + "NULL,";
} else if (i == 8) {
eValues = eValues + eArray[i] + ",";
} else {
eValues = eValues + "\"" + eArray[i] + "\",";
}
}
}
//Employees have two fewer fields than managers:
if (eArray[0].equals("EMPLOYEE")) {
eValues = eValues + "NULL,NULL),\n\t";
} else {
eValues = eValues.substring(0, eValues.length() - 1) + "),\n\t";
}
}
return eValues.substring(0, eValues.length() - 3);
}
//Converts List of salaries to String values.
private static String getInsertSalaries(List<String[]> sArrayList) throws ParseException {
String sValues = "";
for (String[] sArray : sArrayList) {
sValues = sValues + "(";
for (int i = 0; i < sArray.length; i++) {
if (i == 0) {
sValues = sValues + "\"" + sArray[i] + "\",";
} else if (i == 3) {
sValues = sValues + sArray[i] + "),\n\t";
} else {
if (sArray[i].equals("null")) {
sValues = sValues + "NULL,";
} else {
SimpleDateFormat inFormat = new SimpleDateFormat("M/d/yyyy");
SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd");
String date = outFormat.format(inFormat.parse(sArray[i]));
sValues = sValues + "\"" + date + "\",";
}
}
}
}
return sValues.substring(0, sValues.length() - 3);
}
}