Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.datawords.consoleApp;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import java.util.stream.Collectors;
- import org.apache.commons.validator.UrlValidator;
- import com.datawords.kinsoku.engine.IEngine;
- import com.datawords.kinsoku.engine.KinsokuEngine;
- import com.datawords.kinsoku.engine.Languages.Supported;
- import com.datawords.kinsoku.engine.TagPair;
- import com.datawords.spring.model.Content;
- import com.mysql.jdbc.PreparedStatement;
- import com.mysql.jdbc.Statement;
- import lombok.extern.slf4j.Slf4j;
- @Slf4j
- public class Main {
- private static IEngine engine;
- public static void main(String[] args) throws SQLException{
- System.out.println("Coucou");
- try {
- Class.forName("com.mysql.jdbc.Driver");
- } catch (ClassNotFoundException e) {
- System.out.println("Where is your MySQL JDBC Driver?");
- e.printStackTrace();
- return;
- }
- System.out.println("MySQL JDBC Driver Registered!");
- Connection connection = null;
- Statement statement = null;
- try {
- connection = DriverManager
- .getConnection("jdbc:mysql://localhost:3306/omegawatches","root", "root");
- if (connection != null) {
- System.out.println("You made it, take control your database now!");
- } else {
- System.out.println("Failed to make connection!");
- }
- //STEP 4: Execute a query
- System.out.println("Creating statement...");
- statement = (Statement) connection.createStatement();
- String sql;
- sql = "SELECT value_id, value FROM gene_bluefoot_entity_varchar_test ORDER BY value_id";
- ResultSet rs = statement.executeQuery(sql);
- //STEP 5: Extract data from result set
- while(rs.next()){
- //Retrieve by column name
- int value_id = rs.getInt("value_id");
- String value = rs.getString("value");
- String input = value;
- System.out.println("ID : "+value_id);
- String[] schemes = {"http","https"};
- UrlValidator urlValidator = new UrlValidator(schemes);
- if (urlValidator.isValid(input)) {
- log.debug("url!->next");
- continue;
- }
- Map<Supported, String> output = new HashMap<>();
- for (Supported lang : Supported.values())
- {
- Content rawContent = new Content(lang.toString(), rs.getString("value"));
- engine = new KinsokuEngine(rawContent, new TagPair("<pre>", "</pre>"));
- output.put(lang, stripSingleCharProtection(engine.getFinalContent().getContent()));
- }
- final String finalInput = rs.getString("value");
- output = output.entrySet().stream().filter(x -> !x.getValue().equals(finalInput)).collect(Collectors.toMap(x -> x.getKey(), x -> x.getValue()));
- if(output.size() > 1 && output.values().stream().collect(Collectors.toSet()).size() > 1){
- System.out.println("Multiple Languages treatment : "+output.keySet().toString()+ " for content : "+finalInput);
- continue;
- }
- else if(output.isEmpty()){
- log.debug("NO UPDATED CONTENT");
- continue;
- }
- input = output.values().iterator().next();
- //Display values
- System.out.print("Value id : " + value_id);
- System.out.println(" "+value+" : "+input);
- //Update database : storing modified content in column value2
- PreparedStatement preparedStatement = null;
- String updateTableSQL = "UPDATE gene_bluefoot_entity_varchar_test SET value2 = ? "
- + " WHERE value_id = ?";
- preparedStatement = (PreparedStatement) connection.prepareStatement(updateTableSQL);
- preparedStatement.setString(1, input);
- preparedStatement.setInt(2, value_id);
- //execute update SQL statement
- preparedStatement.executeUpdate();
- System.out.println("Record is updated to table!");
- preparedStatement.close();
- }
- //STEP 6: Clean-up environment
- rs.close();
- statement.close();
- } catch (SQLException e) {
- System.out.println("Connection Failed! Check output console");
- e.printStackTrace();
- return;
- }
- finally{
- try{
- if(connection!=null){
- connection.close();
- System.out.println("Connection closed");
- }
- }catch(SQLException se){
- se.printStackTrace();
- }
- }
- // Get charsets for all languages
- // For each line of tables : do pattern treatment
- // Save result
- }
- public static String stripSingleCharProtection(String input){
- String before = input;
- Pattern pattern = Pattern.compile("<pre>.</pre>");
- Matcher matcher = pattern.matcher(before);
- // Check all occurrences
- while (matcher.find()) {
- System.out.print("Start index: " + matcher.start());
- System.out.print(" End index: " + matcher.end());
- char ch = input.charAt(matcher.start()+5);
- System.out.println("Char to save: "+ch);
- before=before.replaceFirst("<pre>.</pre>", String.valueOf(ch));
- matcher=pattern.matcher(before);
- }
- if (!before.equals(input)){
- System.out.println("New String" +before);
- }
- return before;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement