Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.10 KB | None | 0 0
  1. package fr.ekium.cartographie.services.imports;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10. import java.text.ParseException;
  11. import java.text.SimpleDateFormat;
  12. import java.util.ArrayList;
  13. import java.util.Date;
  14. import java.util.HashMap;
  15. import java.util.HashSet;
  16.  
  17. import jxl.Cell;
  18. import jxl.Sheet;
  19. import jxl.Workbook;
  20. import jxl.read.biff.BiffException;
  21. import fr.ekium.cartographie.domain.Branch;
  22. import fr.ekium.cartographie.domain.Function;
  23. import fr.ekium.cartographie.domain.InterviewTeam;
  24. import fr.ekium.cartographie.domain.Job;
  25. import fr.ekium.cartographie.domain.Person;
  26. import fr.ekium.cartographie.domain.PersonHasRoleType;
  27. import fr.ekium.cartographie.domain.Pole;
  28. import fr.ekium.cartographie.domain.Profil;
  29. import fr.ekium.cartographie.domain.RoleType;
  30.  
  31. public class ExcelDataLoader {
  32.  
  33. String pathFile;
  34. int startLine;
  35.  
  36. Workbook workbook;
  37. Sheet syntheseSheet;
  38. Connection con;
  39.  
  40.  
  41.  
  42.  
  43.  
  44. /**
  45. * @param pathFile of file contain data for Ekium employees
  46. * @throws BiffException
  47. * @throws IOException
  48. * @throws SQLException
  49. */
  50. public ExcelDataLoader(String pathFile, int startLine) throws BiffException, IOException, SQLException {
  51. this.pathFile = pathFile;
  52. this.startLine = startLine;
  53.  
  54. this.workbook = Workbook.getWorkbook(new File(pathFile));
  55. this.syntheseSheet = workbook.getSheet("SYNTHESE");
  56.  
  57. String url = "jdbc:mysql://localhost:3306/cartographie_ekium";
  58. String user = "root";
  59. String password = null;
  60. con = DriverManager.getConnection(url, user, password);
  61. }
  62.  
  63.  
  64. /**
  65. * Load Ekium roles data in database
  66. * @throws SQLException
  67. */
  68. public void loadRoleData() throws SQLException{
  69. ArrayList<String> rolessName = new ArrayList<String>();
  70.  
  71. for (Cell cell : syntheseSheet.getColumn(11)) {
  72. if((cell.getRow()>startLine) && (!cell.getContents().equals(""))){
  73. String content = cell.getContents();
  74. if(!rolessName.contains(content)){
  75. rolessName.add(content);
  76. }
  77. }
  78. }
  79.  
  80. int j = 1;
  81. for(String role : rolessName){
  82. // if(RoleType.findRoleTypesByNameEquals(role).getResultList().size() == 0){
  83. // RoleType rt = new RoleType();
  84. // rt.setName(role);
  85. // rt.setPeople(new HashSet<Person>());
  86. // rt.persist();
  87. // }
  88.  
  89. String queryString = "INSERT INTO `cartographie_ekium`.`role_type` (`id`, `name`) VALUES ('"+ j++ +"', '"+role+"');";
  90. System.out.println(queryString);
  91. Statement stmt = con.createStatement();
  92. stmt.execute(queryString);
  93. }
  94. }
  95.  
  96.  
  97. /**
  98. * Load Ekium profil data in database
  99. * @throws SQLException
  100. */
  101. public void loadProfilData() throws SQLException{
  102. ArrayList<String> profilsName = new ArrayList<String>();
  103.  
  104. for (Cell cell : syntheseSheet.getColumn(10)) {
  105. if((cell.getRow()>startLine) && (!cell.getContents().equals(""))){
  106. String content = cell.getContents();
  107. if(!profilsName.contains(content)){
  108. profilsName.add(content);
  109. }
  110. }
  111. }
  112.  
  113. int j = 2;
  114. int i = 1;
  115. for(String profil : profilsName){
  116. // if(Profil.findProfilsByNameEquals(profil).getResultList().size() == 0){
  117. // Profil p = new Profil();
  118. // p.setName(profil);
  119. // p.setCompModelId(null);
  120. // p.setPeople(new HashSet<Person>());
  121. // p.persist();
  122. // }
  123.  
  124. String queryString = "INSERT INTO `cartographie_ekium`.`profil` (`id`, `name`, `comp_model_id`) VALUES ('"+ i++ +"', '"+profil+"', NULL);";
  125. System.out.println(queryString);
  126. Statement stmt = con.createStatement();
  127. stmt.execute(queryString);
  128. }
  129. }
  130.  
  131. /**
  132. * Load Ekium pole data in database
  133. * @throws SQLException
  134. */
  135. public void loadPoleData() throws SQLException{
  136.  
  137. ArrayList<String> polesName = new ArrayList<String>();
  138.  
  139. for (Cell cell : syntheseSheet.getColumn(9)) {
  140. if((cell.getRow()>startLine) && (!cell.getContents().equals(""))){
  141. String content = cell.getContents();
  142. if(!polesName.contains(content)){
  143. polesName.add(content);
  144. }
  145. }
  146. }
  147. int j = 1;
  148. for(String pole : polesName){
  149. // if(Pole.findPolesByNameEquals(pole).getResultList().size() == 0){
  150. // Pole p = new Pole();
  151. // p.setName(pole);
  152. // p.setPeople(new HashSet<Person>());
  153. // p.persist();
  154. // }
  155.  
  156. String queryString = "INSERT INTO `cartographie_ekium`.`pole` (`id`, `name`) VALUES ('"+ j++ +"', '"+pole+"');";
  157. System.out.println(queryString);
  158. Statement stmt = con.createStatement();
  159. stmt.execute(queryString);
  160. }
  161. }
  162.  
  163.  
  164. /**
  165. * Load Ekium branch data in database
  166. * @throws SQLException
  167. */
  168. public void loadBranchData() throws SQLException{
  169. ArrayList<String> branchsName = new ArrayList<String>();
  170.  
  171. for (Cell cell : syntheseSheet.getColumn(8)) {
  172. if((cell.getRow()>startLine) && (!cell.getContents().equals(""))){
  173. String content = cell.getContents();
  174. if(!branchsName.contains(content)){
  175. branchsName.add(content);
  176. }
  177. }
  178. }
  179. int j = 1;
  180. for(String branch : branchsName){
  181. // if(Branch.findBranchesByNameEquals(branch).getResultList().size() == 0){
  182. // Branch b = new Branch();
  183. // b.setName(branch);
  184. // b.setPeople(new HashSet<Person>());
  185. // b.persist();
  186. // }
  187.  
  188. String queryString = "INSERT INTO `cartographie_ekium`.`branch` (`id`, `name`) VALUES ('"+ j++ +"', '"+branch+"');";
  189. System.out.println(queryString);
  190. Statement stmt = con.createStatement();
  191. stmt.execute(queryString);
  192. }
  193. }
  194.  
  195.  
  196. /**
  197. * Load Ekium job data in database
  198. * @throws SQLException
  199. */
  200. public void loadJobData() throws SQLException{
  201. ArrayList<String> jobsName = new ArrayList<String>();
  202.  
  203. for (Cell cell : syntheseSheet.getColumn(7)) {
  204. if((cell.getRow()>startLine) && (!cell.getContents().equals(""))){
  205. String content = cell.getContents();
  206. if(!jobsName.contains(content)){
  207. jobsName.add(content);
  208. }
  209. }
  210. }
  211.  
  212. int j = 1;
  213. for(String job : jobsName){
  214. // if(Job.findJobsByNameEquals(job).getResultList().size() == 0){
  215. // Job j = new Job();
  216. // j.setName(job);
  217. // j.setPeople(new HashSet<Person>());
  218. // j.persist();
  219. // }
  220.  
  221. String queryString = "INSERT INTO `cartographie_ekium`.`job` (`id`, `name`) VALUES ('"+ j++ +"', '"+job+"');";
  222. System.out.println(queryString);
  223. Statement stmt = con.createStatement();
  224. stmt.execute(queryString);
  225. }
  226. }
  227.  
  228.  
  229. /**
  230. * Load Ekium function data in database
  231. * @throws SQLException
  232. */
  233. public void loadFunctionData() throws SQLException{
  234. ArrayList<String> functionsName = new ArrayList<String>();
  235.  
  236. for (Cell cell : syntheseSheet.getColumn(6)) {
  237. if((cell.getRow()>startLine) && (!cell.getContents().equals(""))){
  238. String content = cell.getContents();
  239. if(!functionsName.contains(content)){
  240. functionsName.add(content);
  241. }
  242. }
  243. }
  244.  
  245. int j = 1;
  246. for(String function : functionsName){
  247. // if(Function.findFunctionsByNameEquals(function).getResultList().size() == 0){
  248. // Function f = new Function();
  249. // f.setName(function);
  250. // f.setPeople(new HashSet<Person>());
  251. // f.persist();
  252. // }
  253.  
  254. String queryString = "INSERT INTO `cartographie_ekium`.`function` (`id`, `name`) VALUES ('"+ j++ +"', '"+function+"');";
  255. System.out.println(queryString);
  256. Statement stmt = con.createStatement();
  257. stmt.execute(queryString);
  258. }
  259. }
  260.  
  261. public Connection getCon() {
  262. return con;
  263. }
  264.  
  265.  
  266. /**
  267. * @param startLine index of begin treatement
  268. * @param endLine index of end treatement
  269. * @throws SQLException
  270. *
  271. * Method to load person data in database line by line
  272. * TODO Regler le probeme des dates
  273. */
  274. public void loadPersonDataLineByLine() throws SQLException{
  275.  
  276. SimpleDateFormat dfIn = new SimpleDateFormat("dd/MM/yyyy" );
  277. SimpleDateFormat dfOut = new SimpleDateFormat("yy-MM-dd" );
  278.  
  279. int cpt = 2;
  280. int i = startLine;
  281. //Loop to load data person line by line
  282. while(!syntheseSheet.getCell(2, i).getContents().equals("")){
  283.  
  284. System.out.println("#################################################");
  285. HashMap<String, Object> personData = new HashMap<String, Object>();
  286.  
  287. //Loop to load data person column by column
  288. for (int j = 1; j < 12; j++) {
  289. Cell cell = syntheseSheet.getCell(j, i);
  290. String content = cell.getContents();
  291. Cell titleCell = syntheseSheet.getCell(j, 13);
  292. String titleContent = titleCell.getContents();
  293.  
  294. System.out.println(titleContent + " (" + j + " - " + i + ") : " + content);
  295.  
  296. //if(titleContent.equals("Nom")){
  297. // personData.put(titleContent, content);
  298. //}
  299. //else if(titleContent.equals("Prénom")){
  300. //personData.put(titleContent, content);
  301. // }
  302. //Nom and Prenom are handled by the else statement
  303. if(titleContent.equals("Sexe")){
  304. if(content.equals("H")){
  305. personData.put(titleContent, 1);
  306. }else{
  307. personData.put(titleContent, 0);
  308. }
  309. }
  310. else if(titleContent.equals("Date de naissance")){
  311. try {
  312. Date date = dfIn.parse(content);
  313. System.out.println("Date naissance : " + dfOut.format(date));
  314. //personData.put(titleContent, dfOut.format(date));
  315. personData.put(titleContent, dfOut.format(date));
  316. } catch (Exception e) {
  317. System.out.println("Error while parsing date" );
  318. e.printStackTrace();
  319. }
  320. }
  321. else if(titleContent.equals("Date d'entrée")){
  322. try {
  323. Date date = dfIn.parse(content);
  324. System.out.println("Date d'entrée : " + dfOut.format(date));
  325. //personData.put(titleContent, dfOut.format(date));
  326. personData.put(titleContent, dfOut.format(date));
  327. } catch (Exception e) {
  328. System.out.println("Error while parsing date" );
  329. e.printStackTrace();
  330. }
  331. }else{
  332. personData.put(titleContent, content);
  333. }
  334.  
  335. }
  336.  
  337. String login = personData.get("Prénom")+"."+personData.get("Nom");
  338. login = login.toLowerCase();
  339.  
  340. String mail = login+"@ekium.eu";
  341.  
  342. System.out.println("Login : " + login);
  343. System.out.println("Mail : " + mail);
  344.  
  345. /////////////////////////////////////////////////////////////////////////
  346.  
  347. String profilName = (String)personData.get("Profil");
  348. String queryProfilString = "SELECT id FROM profil WHERE name LIKE '"+profilName+"';";
  349. System.out.println(queryProfilString);
  350. Statement stmt = con.createStatement();
  351. ResultSet rs = stmt.executeQuery(queryProfilString);
  352. long profilId = 0;
  353. while (rs.next()) {
  354. profilId = rs.getLong("id");
  355. System.out.println("Intger ID : " + profilId);
  356. }
  357.  
  358. String poleName = (String)personData.get("Pôle");
  359. String queryPoleString = "SELECT id FROM pole WHERE name LIKE '"+poleName+"';";
  360. System.out.println(queryPoleString);
  361. stmt = con.createStatement();
  362. rs = stmt.executeQuery(queryPoleString);
  363. long poleId = 0;
  364. while (rs.next()) {
  365. poleId = rs.getLong("id");
  366. System.out.println("Intger ID : " + poleId);
  367. }
  368.  
  369. String jobName = (String)personData.get("Métier");
  370. String queryJobString = "SELECT id FROM job WHERE name LIKE '"+jobName+"';";
  371. System.out.println(queryJobString);
  372. stmt = con.createStatement();
  373. rs = stmt.executeQuery(queryJobString);
  374. long jobId = 0;
  375. while (rs.next()) {
  376. jobId = rs.getLong("id");
  377. System.out.println("Intger ID : " + jobId);
  378. }
  379.  
  380. String functionName = (String)personData.get("Poste");
  381. String queryFunctionString = "SELECT id FROM function WHERE name LIKE '"+functionName+"';";
  382. System.out.println(queryFunctionString);
  383. stmt = con.createStatement();
  384. rs = stmt.executeQuery(queryFunctionString);
  385. long functionId = 0;
  386. while (rs.next()) {
  387. functionId = rs.getLong("id");
  388. System.out.println("Intger ID : " + functionId);
  389. }
  390.  
  391. String branchName = (String)personData.get("Branche");
  392. String queryBranchString = "SELECT id FROM branch WHERE name LIKE '"+branchName+"';";
  393. System.out.println(queryBranchString);
  394. stmt = con.createStatement();
  395. rs = stmt.executeQuery(queryBranchString);
  396. long branchId = 0;
  397. while (rs.next()) {
  398. branchId = rs.getLong("id");
  399. System.out.println("Intger ID : " + branchId);
  400. }
  401.  
  402. String queryRoleIdString = "SELECT id FROM role_type WHERE name LIKE '"+personData.get("Droit")+"';";
  403. System.out.println(queryRoleIdString);
  404. stmt = con.createStatement();
  405. rs = stmt.executeQuery(queryRoleIdString);
  406. long roleId = 0;
  407. while (rs.next()) {
  408. roleId = rs.getLong("id");
  409. System.out.println("Intger ID : " + roleId);
  410. }
  411.  
  412.  
  413. // Person p = new Person();
  414. // p.setAddress("address");
  415. // p.setBeginWorkDate((Date) personData.get("Date d'entrée"));
  416. // p.setBirth((Date) personData.get("Date de naissance"));
  417. // p.setCellPhone(0);
  418. // p.setFirstname((String) personData.get("Prénom"));
  419. // p.setIsMale((Boolean) (personData.get("Sexe").equals("male")));
  420. // p.setLastEvalDate((Date) personData.get("Date d'entrée"));
  421. // p.setLastname((String) personData.get("Nom"));
  422. // p.setLogin(login);
  423. // p.setMail(mail);
  424. // p.setOfficePhone(0);
  425. // p.setPassword("user");
  426. // p.setPostalCode(0);
  427. // p.setTown("town");
  428. // System.out.println("looking for branch named : "+personData.get("Branche"));
  429. // p.setBranchId(Branch.findBranchesByNameEquals((String)personData.get("Branche")).getSingleResult());
  430. // p.setFunctionId(Function.findFunctionsByNameEquals((String)personData.get("Poste")).getSingleResult());
  431. // p.setJobId(Job.findJobsByNameEquals((String)personData.get("Métier")).getSingleResult());
  432. // p.setPoleId(Pole.findPolesByNameEquals((String)personData.get("Pôle")).getSingleResult());
  433. // p.setProfilId(Profil.findProfilsByNameEquals((String)personData.get("Profil")).getSingleResult());
  434. // p.persist();
  435.  
  436. String queryString = "INSERT INTO `cartographie_ekium`.`person` (`id`, `address`, `begin_work_date`, `birth`, `cell_phone`, `firstname`, `is_male`, `last_eval_date`, `lastname`, `login`, `mail`, `office_phone`, `password`, `postal_code`, `town`, `branch_id`, `function_id`, `job_id`, `pole_id`, `profil_id`, `role_type_id`) VALUES ('"+ cpt++ +"', 'address', '"+personData.get("Date d'entrée")+"', '"+personData.get("Date de naissance")+"', '0', '"+personData.get("Prénom")+"', b'"+personData.get("Sexe")+"', '"+personData.get("Date d'entrée")+"', '"+personData.get("Nom")+"', '"+login+"', '"+mail+"', '0', 'user', '0', 'town', '"+branchId+"', '"+functionId+"', '"+jobId+"', '"+poleId+"', '"+profilId+"','"+roleId+"');";
  437. System.out.println(queryString);
  438. Statement insertStmt = con.createStatement();
  439. insertStmt.execute(queryString);
  440.  
  441. String queryPersonIdString = "SELECT id FROM person WHERE login LIKE '"+login+"';";
  442. System.out.println(queryPersonIdString);
  443. stmt = con.createStatement();
  444. rs = stmt.executeQuery(queryPersonIdString);
  445. long personId = 0;
  446. while (rs.next()) {
  447. personId = rs.getLong("id");
  448. System.out.println("Intger ID : " + personId);
  449. }
  450.  
  451. // PersonHasRoleType type = new PersonHasRoleType();
  452. // System.out.println(Person.findPeopleByLoginEquals(login));
  453. // type.setPersonId(Person.findPeopleByLoginEquals(login).getSingleResult());
  454. // System.out.println(personData.get("Droit"));
  455. // type.setRoleTypeId(RoleType.findRoleTypesByNameEquals((String)personData.get("Droit")).getSingleResult());
  456. // type.persist();
  457.  
  458. String roleQueryString = "INSERT INTO `cartographie_ekium`.`person_has_role_type` (`id`, `person_id`, `role_type_id`) VALUES ('"+cpt+"', '"+personId+"', '"+roleId+"');";
  459. System.out.println(roleQueryString);
  460. Statement roleInsertStmt = con.createStatement();
  461. roleInsertStmt.execute(roleQueryString);
  462.  
  463. // InterviewTeam interviewTeam = new InterviewTeam();
  464. // interviewTeam.setEmployee(Person.findPeopleByLoginEquals(login).getSingleResult());
  465. // interviewTeam.persist();
  466.  
  467. String interviewTeamQueryString = "INSERT INTO `cartographie_ekium`.`interview_team` (`id` ,`version` ,`employee` ,`interlocutor` ,`manager` ) VALUES ('"+cpt+"', '0', '"+personId+"', NULL, NULL);";
  468. System.out.println(interviewTeamQueryString);
  469. Statement interviewTeamInsertStmt = con.createStatement();
  470. interviewTeamInsertStmt.execute(interviewTeamQueryString);
  471.  
  472. }
  473.  
  474.  
  475. }
  476.  
  477.  
  478. /**
  479. * @param args
  480. * @throws IOException
  481. * @throws BiffException
  482. * @throws SQLException
  483. */
  484. public static void main(String[] args) throws ParseException {
  485. ExcelDataLoader dataLoader = null;
  486.  
  487. try {
  488. dataLoader = new ExcelDataLoader("Cartographie Paris Le Havre.xls", 41);
  489.  
  490. // dataLoader.loadPoleData();
  491. // dataLoader.loadProfilData();
  492. // dataLoader.loadBranchData();
  493. // dataLoader.loadJobData();
  494. // dataLoader.loadFunctionData();
  495. // dataLoader.loadRoleData();
  496.  
  497. dataLoader.loadPersonDataLineByLine();
  498.  
  499. } catch (BiffException e) {
  500. // TODO Auto-generated catch block
  501. e.printStackTrace();
  502. } catch (IOException e) {
  503. // TODO Auto-generated catch block
  504. e.printStackTrace();
  505. } catch (SQLException e) {
  506. // TODO Auto-generated catch block
  507. e.printStackTrace();
  508. }
  509. // }finally{
  510. // try {
  511. // //dataLoader.getCon().close();
  512. // } catch (SQLException e) {
  513. // // TODO Auto-generated catch block
  514. // e.printStackTrace();
  515. // }
  516. // }
  517.  
  518. }
  519.  
  520. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement