Advertisement
Guest User

Untitled

a guest
Oct 17th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.56 KB | None | 0 0
  1. package com.datawords.consoleApp;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. import java.util.regex.Matcher;
  9. import java.util.regex.Pattern;
  10. import java.util.stream.Collectors;
  11.  
  12. import org.apache.commons.validator.UrlValidator;
  13.  
  14. import com.datawords.kinsoku.engine.IEngine;
  15. import com.datawords.kinsoku.engine.KinsokuEngine;
  16. import com.datawords.kinsoku.engine.Languages.Supported;
  17. import com.datawords.kinsoku.engine.TagPair;
  18. import com.datawords.spring.model.Content;
  19. import com.mysql.jdbc.PreparedStatement;
  20. import com.mysql.jdbc.Statement;
  21.  
  22. import lombok.extern.slf4j.Slf4j;
  23.  
  24. @Slf4j
  25. public class Main {
  26.    
  27.     private static IEngine engine;
  28.    
  29.     public static void main(String[] args) throws SQLException{
  30.        
  31.  
  32.        
  33.         System.out.println("Coucou");
  34.         try {
  35.             Class.forName("com.mysql.jdbc.Driver");
  36.         } catch (ClassNotFoundException e) {
  37.             System.out.println("Where is your MySQL JDBC Driver?");
  38.             e.printStackTrace();
  39.             return;
  40.         }
  41.  
  42.         System.out.println("MySQL JDBC Driver Registered!");
  43.         Connection connection = null;
  44.         Statement statement = null;
  45.  
  46.         try {
  47.             connection = DriverManager
  48.             .getConnection("jdbc:mysql://localhost:3306/omegawatches","root", "root");
  49.             if (connection != null) {
  50.                 System.out.println("You made it, take control your database now!");
  51.             } else {
  52.                 System.out.println("Failed to make connection!");
  53.             }
  54.               //STEP 4: Execute a query
  55.             System.out.println("Creating statement...");
  56.             statement = (Statement) connection.createStatement();
  57.             String sql;
  58.             sql = "SELECT value_id, value FROM gene_bluefoot_entity_varchar_test ORDER BY value_id";
  59.             ResultSet rs = statement.executeQuery(sql);
  60.  
  61.               //STEP 5: Extract data from result set
  62.               while(rs.next()){
  63.                  //Retrieve by column name
  64.                  int value_id  = rs.getInt("value_id");
  65.                  String value = rs.getString("value");
  66.                  String input = value;
  67.                  
  68.                  System.out.println("ID : "+value_id);
  69.                  
  70.                  String[] schemes = {"http","https"};
  71.                  UrlValidator urlValidator = new UrlValidator(schemes);
  72.                  if (urlValidator.isValid(input)) {
  73.                      log.debug("url!->next");
  74.                      continue;
  75.                  }
  76.                  
  77.  
  78.                  Map<Supported, String> output = new HashMap<>();
  79.                  for (Supported lang : Supported.values())
  80.                  {
  81.                      Content rawContent = new Content(lang.toString(), rs.getString("value"));
  82.                      engine = new KinsokuEngine(rawContent, new TagPair("<pre>", "</pre>"));
  83.                      output.put(lang, stripSingleCharProtection(engine.getFinalContent().getContent()));
  84.                  }
  85.                  
  86.                  final String finalInput = rs.getString("value");
  87.                  output = output.entrySet().stream().filter(x -> !x.getValue().equals(finalInput)).collect(Collectors.toMap(x -> x.getKey(), x -> x.getValue()));
  88.  
  89.                  
  90.  
  91.  
  92.                  
  93.                  if(output.size() > 1 && output.values().stream().collect(Collectors.toSet()).size() > 1){
  94.                      System.out.println("Multiple Languages treatment : "+output.keySet().toString()+ " for content : "+finalInput);
  95.                      
  96.                      continue;
  97.                  }
  98.                  
  99.                  else if(output.isEmpty()){
  100.                      log.debug("NO UPDATED CONTENT");
  101.                      continue;
  102.                  }
  103.                  
  104.                  input = output.values().iterator().next();
  105.                  //Display values
  106.                  System.out.print("Value id : " + value_id);
  107.                  System.out.println(" "+value+" : "+input);
  108.                  //Update database : storing modified content in column value2
  109.                  PreparedStatement preparedStatement = null;
  110.                  String updateTableSQL = "UPDATE gene_bluefoot_entity_varchar_test SET value2 = ? "
  111.                                           + " WHERE value_id = ?";
  112.                  
  113.    
  114.                  preparedStatement = (PreparedStatement) connection.prepareStatement(updateTableSQL);
  115.                  preparedStatement.setString(1, input);
  116.                  preparedStatement.setInt(2, value_id);
  117.                  //execute update SQL statement
  118.                  preparedStatement.executeUpdate();
  119.  
  120.                  System.out.println("Record is updated to table!");
  121.                  preparedStatement.close();
  122.  
  123.               }
  124.               //STEP 6: Clean-up environment
  125.               rs.close();
  126.               statement.close();
  127.            
  128.  
  129.         } catch (SQLException e) {
  130.             System.out.println("Connection Failed! Check output console");
  131.             e.printStackTrace();
  132.             return;
  133.         }
  134.         finally{
  135.           try{
  136.               if(connection!=null){
  137.                  connection.close();
  138.                 System.out.println("Connection closed");
  139.               }
  140.            }catch(SQLException se){
  141.               se.printStackTrace();
  142.            }
  143.         }
  144.  
  145.  
  146.  
  147.        
  148.         // Get charsets for all languages
  149.        
  150.        
  151.         // For each line of tables : do pattern treatment
  152.        
  153.        
  154.         // Save result
  155.        
  156.        
  157.  
  158.     }
  159.     public static String stripSingleCharProtection(String input){
  160.         String before = input;
  161.         Pattern pattern = Pattern.compile("<pre>.</pre>");
  162.         Matcher matcher = pattern.matcher(before);
  163.         // Check all occurrences
  164.         while (matcher.find()) {
  165.             System.out.print("Start index: " + matcher.start());
  166.             System.out.print(" End index: " + matcher.end());
  167.             char ch = input.charAt(matcher.start()+5);
  168.             System.out.println("Char to save: "+ch);
  169.             before=before.replaceFirst("<pre>.</pre>", String.valueOf(ch));
  170.             matcher=pattern.matcher(before);
  171.         }
  172.         if (!before.equals(input)){
  173.             System.out.println("New String" +before);
  174.         }
  175.         return before;
  176.     }
  177.  
  178.  
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement