Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.32 KB | None | 0 0
  1.  
  2. import java.io.FileNotFoundException;
  3. import java.sql.*;
  4. import java.util.HashMap;
  5. import java.util.Vector;
  6.  
  7. public class DatabaseInserter {
  8. private Connection conn;
  9. private Statement stm;
  10.  
  11. HashMap<String,String> paragens = new HashMap<String,String>();
  12. BusStopsGetter stopsGetter = new BusStopsGetter();
  13. ParserHorariosCSV horariosGetter = new ParserHorariosCSV();
  14.  
  15. public DatabaseInserter(String arquivo) throws SQLException, ClassNotFoundException {
  16. Class.forName("org.sqlite.JDBC");
  17. this.conn = DriverManager.getConnection("jdbc:sqlite:" + arquivo);
  18. this.stm = this.conn.createStatement();
  19. stopsGetter.init();
  20. }
  21.  
  22. public static void main(String []args) throws FileNotFoundException{
  23. DatabaseInserter dbI = null;
  24. try {
  25. dbI = new DatabaseInserter("smtuc.sqlite");
  26. } catch (SQLException e) {
  27. // TODO Auto-generated catch block
  28. e.printStackTrace();
  29. } catch (ClassNotFoundException e) {
  30. // TODO Auto-generated catch block
  31. e.printStackTrace();
  32. }
  33. dbI.insertStops();
  34. dbI.insertLines();
  35.  
  36. }
  37. public void initDB() {
  38. try {
  39. //Remove e cria a tabela a cada execução. Mero exemplo
  40. this.stm.executeUpdate("DROP TABLE IF EXISTS pessoas");
  41. this.stm.executeUpdate("CREATE TABLE pessoas ("
  42. + "nome varchar(70) PRIMARY KEY NOT NULL,"
  43. + "idade integer);");
  44. } catch (SQLException e) {
  45. e.printStackTrace();
  46. }
  47. }
  48.  
  49.  
  50. public void insertStops() {
  51. String[] stopSplit, stopName, stopLocation;
  52. String name, latit, longit;
  53. for(String stopInfo : stopsGetter.getStops()){
  54. stopSplit = stopInfo.split(">");
  55. stopName = stopSplit[0].split(",");
  56. stopLocation = stopSplit[1].split(":");
  57.  
  58.  
  59. name = stopName[0];
  60. latit = stopLocation[0];
  61. longit = stopLocation[0];
  62. try {
  63. this.stm = this.conn.createStatement();
  64. this.stm.executeUpdate("INSERT INTO paragens (nome,latitude,longitude) VALUES (\""
  65. + name + "\",\""
  66. + latit + "\",\""
  67. + longit + "\")");
  68. } catch (SQLException e) {
  69. e.printStackTrace();
  70. }
  71. }
  72. }
  73. void insertLines() throws FileNotFoundException{
  74. for(int i = 0;i<horariosGetter.BUSES.length;i++){
  75. Linha l= horariosGetter.obterHorarioCSV(horariosGetter.BUSES[i]);
  76. insertLineInfo(l);
  77. }
  78. }
  79. public void insertLine(String id,String paragem1, String paragem2){
  80. try {
  81. this.stm = this.conn.createStatement();
  82. this.stm.executeUpdate("INSERT INTO paragens (id,paragem1_id,paragem2_id) VALUES (\""
  83. + id + "\",\""
  84. + paragem1 + "\",\""
  85. + paragem2 + "\")");
  86. } catch (SQLException e) {
  87. e.printStackTrace();
  88. }
  89. }
  90.  
  91. public void insertLineStopSchedule(String line_id,String paragem_id, int order,String time,int idaVoltaFlag,int tipoDiaFlag){
  92. try {
  93. this.stm = this.conn.createStatement();
  94. this.stm.executeUpdate("INSERT INTO linhas_paragens VALUES (\""
  95. + line_id + "\","
  96. + paragem_id + ","
  97. + order + ",\""
  98. + time + "\","
  99. + idaVoltaFlag+","
  100. + tipoDiaFlag+")");
  101. } catch (SQLException e) {
  102. e.printStackTrace();
  103. }
  104. }
  105. public void insertLineInfo(Linha line){
  106. insertLine(line.id,line.paragem1,line.paragem2);
  107.  
  108. //inserir horas de ida em dias úteis
  109. for(int i = 0;i<line.scheduleWorkDaysGoing.length;i++){
  110. for(int j=0;j<line.scheduleWorkDaysGoing[0].length;j++){
  111. if(line.scheduleWorkDaysGoing[i][j]!=null){
  112.  
  113. String time = (line.startHour + j)+":"+line.scheduleWorkDaysGoing[i][j];;
  114.  
  115. insertLineStopSchedule(line.id,getParagemId(line.paragem1),1,time,0,0);
  116.  
  117. }
  118. }
  119. }
  120.  
  121. //inserir horas de returno em dias úteis
  122. for(int i = 0;i<line.scheduleWorkDaysComing.length;i++){
  123. for(int j=0;j<line.scheduleWorkDaysComing[0].length;j++){
  124. if(line.scheduleWorkDaysComing[i][j]!=null){
  125. String time = (line.startHour + j)+":"+line.scheduleWorkDaysComing[i][j];;
  126. // line_id,String paragem_id, int order,String time,int idaVoltaFlag,int tipoDiaFlag
  127. insertLineStopSchedule(line.id,getParagemId(line.paragem2),getOrdemParagem(line,line.paragem2),time,1,0);
  128. }
  129. }
  130. }
  131.  
  132.  
  133. //inserir horas de ida aos sábados
  134. for(int i = 0;i<line.saturdaysGoing.length;i++){
  135. for(int j=0;j<line.saturdaysGoing[0].length;j++){
  136. if(line.saturdaysGoing[i][j]!=null){
  137.  
  138. String time = (line.startHour + j)+":"+line.saturdaysGoing[i][j];;
  139.  
  140. insertLineStopSchedule(line.id,getParagemId(line.paragem1),1,time,0,1);
  141.  
  142. }
  143. }
  144. }
  145.  
  146. //inserir horas de retorno aos sábados
  147. for(int i = 0;i<line.saturdaysComing.length;i++){
  148. for(int j=0;j<line.saturdaysComing[0].length;j++){
  149. if(line.saturdaysComing[i][j]!=null){
  150.  
  151. String time = (line.startHour + j)+":"+line.saturdaysComing[i][j];;
  152. // line_id,String paragem_id, int order,String time,int idaVoltaFlag,int tipoDiaFlag
  153. insertLineStopSchedule(line.id,getParagemId(line.paragem2),getOrdemParagem(line,line.paragem2),time,1,1);
  154.  
  155. }
  156. }
  157. }
  158.  
  159.  
  160. //inserir horas de ida aos domingos e feriados
  161. for(int i = 0;i<line.holidaysGoing.length;i++){
  162. for(int j=0;j<line.holidaysGoing[0].length;j++){
  163. if(line.holidaysGoing[i][j]!=null){
  164.  
  165. String time = (line.startHour + j)+":"+line.holidaysGoing[i][j];;
  166.  
  167. insertLineStopSchedule(line.id,getParagemId(line.paragem1),1,time,0,2);
  168.  
  169. }
  170. }
  171. }
  172.  
  173. //inserir horas de retorno aos sábados
  174. for(int i = 0;i<line.holidaysComing.length;i++){
  175. for(int j=0;j<line.holidaysComing[0].length;j++){
  176. if(line.holidaysComing[i][j]!=null){
  177.  
  178. String time = (line.startHour + j)+":"+line.holidaysComing[i][j];;
  179. // line_id,String paragem_id, int order,String time,int idaVoltaFlag,int tipoDiaFlag
  180. insertLineStopSchedule(line.id,getParagemId(line.paragem2),getOrdemParagem(line,line.paragem2),time,1,2);
  181.  
  182. }
  183. }
  184. }
  185. }
  186. private int getOrdemParagem(Linha line, String paragem) {
  187. // TODO Auto-generated method stub
  188. for(int i = 0;i<line.paragensOrdem.size();i++){
  189. if(paragem.equals(line.paragensOrdem.get(i))){
  190. return i;
  191. }
  192. }
  193. return -1;
  194. }
  195.  
  196. private String getParagemId(String paragem1) {
  197. String id = null;
  198. ResultSet rs;
  199. try {
  200. rs = this.stm.executeQuery("SELECT id FROM paragens WHERE nome = \""+paragem1+"\"");
  201.  
  202.  
  203. if(rs.first()){
  204. id = rs.getString("id");
  205. }
  206. rs.close();
  207.  
  208. } catch (SQLException e) {
  209. e.printStackTrace();
  210. }
  211. return id;
  212. }
  213.  
  214.  
  215.  
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement