/*
 * Copyright 2014-2016 CyberVision, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.kaaproject.data_migration;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.kaaproject.data_migration.model.Configuration;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import static org.kaaproject.data_migration.utils.Utils.encodeUuids;

public class UpdateUuidsMigration {
    private Connection connection;

    public UpdateUuidsMigration(Connection connection) {
        this.connection = connection;
    }

    public void transform() throws IOException, SQLException {
            QueryRunner run = new QueryRunner();
            ResultSetHandler<List<Configuration>> rsHandler = new BeanListHandler<Configuration>(Configuration.class);
            List<Configuration> configs = run.query(connection, "SELECT * FROM configuration", rsHandler);
            for (Configuration config : configs) {
                JsonNode json = new ObjectMapper().readTree(config.getConfiguration_body());
                JsonNode jsonEncoded = encodeUuids(json);
                byte[] encodedConfigurationBody = jsonEncoded.toString().getBytes();

                int updates = run.update(connection, "UPDATE configuration SET configuration_body=? WHERE id=?", encodedConfigurationBody, config.getId());
                if (updates != 1) {
                    System.err.println("Error: failed to update configuration: " + config);
                }
            }
    }

}

@@ -637,24 +637,262 @@
 migration;%0A%0A
+import com.datastax.driver.core.Cluster;%0Aimport com.mongodb.MongoClient;%0Aimport com.mongodb.client.FindIterable;%0Aimport com.mongodb.client.MongoCollection;%0Aimport com.mongodb.client.MongoDatabase;%0Aimport com.mongodb.client.model.Filters;%0A
 import org.a
@@ -1030,32 +1030,58 @@
 eanListHandler;%0A
+import org.bson.Document;%0A
 import org.codeh
@@ -1201,24 +1201,76 @@
 nfiguration;
+%0Aimport org.kaaproject.data_migration.utils.Options;
 %0A%0Aimport jav
@@ -1369,16 +1369,67 @@
 .List;%0A%0A
+import static com.mongodb.client.model.Filters.eq;%0A
 import s
@@ -1560,16 +1560,130 @@
 nection;
+%0A    private MongoClient client;%0A    private Cluster cluster;%0A    private String dbName;%0A    private String nosql;
 %0A%0A    pu
@@ -1729,16 +1729,54 @@
 nnection
+, String host, String db, String nosql
 ) %7B%0A    
@@ -1805,24 +1805,214 @@
 connection;%0A
+        client = new MongoClient(host);%0A        cluster = Cluster.builder()%0A                .addContactPoint(host)%0A                .build();%0A        dbName = db;%0A        this.nosql = nosql;%0A
     %7D%0A%0A    p
@@ -2077,20 +2077,16 @@
         
-    
 QueryRun
@@ -2122,20 +2122,16 @@
         
-  
-  
 ResultSe
@@ -2237,20 +2237,16 @@
         
-    
 List%3CCon
@@ -2340,20 +2340,16 @@
         
-  
-  
 for (Con
@@ -2375,28 +2375,24 @@
  configs) %7B%0A
-    
             
@@ -2472,36 +2472,32 @@
 ));%0A            
-   
- 
 JsonNode jsonEnc
@@ -2522,20 +2522,16 @@
 (json);%0A
-    
         
@@ -2616,20 +2616,16 @@
         
-   
- 
 int upda
@@ -2768,20 +2768,16 @@
         
-    
 if (upda
@@ -2784,28 +2784,24 @@
 tes != 1) %7B%0A
-    
             
@@ -2892,27 +2892,729 @@
     
-    %7D%0A            %7D
+%7D%0A        %7D%0A%0A        if (nosql.equals(Options.DEFAULT_NO_SQL)) %7B%0A            MongoDatabase database = client.getDatabase(dbName);%0A            MongoCollection%3CDocument%3E userConfiguration = database.getCollection(%22user_configuration%22);%0A            FindIterable%3CDocument%3E documents = userConfiguration.find();%0A            for (Document d : documents) %7B%0A                String body = (String) d.get(%22body%22);%0A                JsonNode json = new ObjectMapper().readTree(body);%0A                JsonNode jsonEncoded = encodeUuids(json);%0A                userConfiguration.updateOne(Filters.eq(%22_id%22, d.get(%22_id%22)), Filters.eq(%22$set%22, Filters.eq(%22body%22, jsonEncoded)));%0A            %7D%0A%0A        %7D else %7B%0A            //TODO%0A        %7D%0A
 %0A   

Update uuids in user_configuration MongoDB