// Path: src/main/java/org/easycassandra/ClassInformations.java
// public enum ClassInformations {
// 
//     INSTACE;
//     /**
//      * field that contains all class on Cassandra.
//      * Key - qualifield
//      * value - the wrapper of class
//      */
//     private Map<String, ClassInformation> classMap;
// 
//     /**
//      * The integer class is used to enum how default.
//      */
//     public static final Class<?> DEFAULT_ENUM_CLASS = Integer.class;
// 
//     /**
//      * return the wrapper of class.
//      * @param clazz class to find
//      * @return {@link ClassInformation}
//      */
//     public ClassInformation getClass(Class<?> clazz) {
// 
//         if (classMap.get(clazz.getName()) == null) {
//             classMap.put(clazz.getName(), new ClassInformation(clazz));
//         }
//         return classMap.get(clazz.getName());
//     }
// 
//     {
//         classMap = Collections.synchronizedMap(new TreeMap<String, ClassInformation>());
//     }
// }
// 
// Path: src/main/java/org/easycassandra/FieldJavaNotEquivalentCQLException.java
// public class FieldJavaNotEquivalentCQLException extends EasyCassandraException {
// 
//     private static final long serialVersionUID = 1L;
// 
//     /**
//      * Constructor to FieldJavaNotEquivalentCQLException.
//      * @param message information to log
//      */
//     public FieldJavaNotEquivalentCQLException(String message) {
//         super(message);
// 
//     }
// 
// }
// 
// Path: src/main/java/org/easycassandra/KeyProblemsException.java
// public class KeyProblemsException extends EasyCassandraException {
// 
//     /**
//      * Constructor to KeyProblemsException.
//      * @param message
//      *            information to log
//      */
//     public KeyProblemsException(String message) {
//         super(message);
// 
//     }
// 
// 	private static final long serialVersionUID = 1L;
// }


import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import org.easycassandra.ClassInformation;
import org.easycassandra.ClassInformations;
import org.easycassandra.FieldInformation;
import org.easycassandra.FieldJavaNotEquivalentCQLException;
import org.easycassandra.KeyProblemsException;
import org.easycassandra.persistence.cassandra.AddColumnUtil.AddColumn;
import org.easycassandra.persistence.cassandra.VerifyRowUtil.VerifyRow;
import com.datastax.driver.core.ColumnDefinitions.Definition;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.exceptions.InvalidQueryException;
import com.datastax.driver.core.querybuilder.QueryBuilder;
import com.datastax.driver.core.querybuilder.Select;
import java.util.Collections;
import java.util.Comparator;
import org.easycassandra.Order;

/*
 * Copyright 2013 Otávio Gonçalves de Santana (otaviojava)
 * 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.easycassandra.persistence.cassandra;




/**
 * Class to fix a column family.
 * @author otaviojava
 */
class FixColumnFamily {

    private static final String QUERY_PRIMARY_KEY = " PRIMARY KEY (";
    private static final String CLUSTERING_ORDER = " WITH CLUSTERING ORDER BY (";

    /**
     * verify if exist column family and try to create.
     * @param session - bridge to cassandra data base
     * @param keySpace - the keyspace
     * @param familyColumn - name of family column
     * @param class1 - bean
     * @return - if get it or not
     */
    public boolean verifyColumnFamily(Session session, String keySpace, String familyColumn,
            Class<?> class1) {
        try {
            Select select = QueryBuilder.select().from(keySpace, familyColumn).limit(1);
            ResultSet resultSet = session.execute(select);
            verifyRowType(resultSet, class1, session);
            findIndex(class1, session);
            return true;
        } catch (InvalidQueryException exception) {

            if (exception.getCause().getMessage().contains("unconfigured columnfamily ")) {
                Logger.getLogger(FixColumnFamily.class.getName()).info(
                        "Column family doesn't exist, try to create");
                createColumnFamily(familyColumn, class1, session);
                findIndex(class1, session);
                return true;
            }
        }

        return false;
    }

    /**
     * Column family exists verify.
     * @param resultSet
     */
    private void verifyRowType(ResultSet resultSet, Class<?> class1, Session session) {
        Map<String, String> mapNameType = new HashMap<String, String>();
        for (Definition column : resultSet.getColumnDefinitions()) {
            mapNameType
                    .put(column.getName(), column.getType().getName().name());
        }

// Path: src/main/java/org/easycassandra/ClassInformations.java
// public enum ClassInformations {
// 
//     INSTACE;
//     /**
//      * field that contains all class on Cassandra.
//      * Key - qualifield
//      * value - the wrapper of class
//      */
//     private Map<String, ClassInformation> classMap;
// 
//     /**
//      * The integer class is used to enum how default.
//      */
//     public static final Class<?> DEFAULT_ENUM_CLASS = Integer.class;
// 
//     /**
//      * return the wrapper of class.
//      * @param clazz class to find
//      * @return {@link ClassInformation}
//      */
//     public ClassInformation getClass(Class<?> clazz) {
// 
//         if (classMap.get(clazz.getName()) == null) {
//             classMap.put(clazz.getName(), new ClassInformation(clazz));
//         }
//         return classMap.get(clazz.getName());
//     }
// 
//     {
//         classMap = Collections.synchronizedMap(new TreeMap<String, ClassInformation>());
//     }
// }
// 
// Path: src/main/java/org/easycassandra/FieldJavaNotEquivalentCQLException.java
// public class FieldJavaNotEquivalentCQLException extends EasyCassandraException {
// 
//     private static final long serialVersionUID = 1L;
// 
//     /**
//      * Constructor to FieldJavaNotEquivalentCQLException.
//      * @param message information to log
//      */
//     public FieldJavaNotEquivalentCQLException(String message) {
//         super(message);
// 
//     }
// 
// }
// 
// Path: src/main/java/org/easycassandra/KeyProblemsException.java
// public class KeyProblemsException extends EasyCassandraException {
// 
//     /**
//      * Constructor to KeyProblemsException.
//      * @param message
//      *            information to log
//      */
//     public KeyProblemsException(String message) {
//         super(message);
// 
//     }
// 
// 	private static final long serialVersionUID = 1L;
// }


// Path: src/main/java/org/easycassandra/persistence/cassandra/FixColumnFamily.java
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import org.easycassandra.ClassInformation;
import org.easycassandra.ClassInformations;
import org.easycassandra.FieldInformation;
import org.easycassandra.FieldJavaNotEquivalentCQLException;
import org.easycassandra.KeyProblemsException;
import org.easycassandra.persistence.cassandra.AddColumnUtil.AddColumn;
import org.easycassandra.persistence.cassandra.VerifyRowUtil.VerifyRow;
import com.datastax.driver.core.ColumnDefinitions.Definition;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.exceptions.InvalidQueryException;
import com.datastax.driver.core.querybuilder.QueryBuilder;
import com.datastax.driver.core.querybuilder.Select;
import java.util.Collections;
import java.util.Comparator;
import org.easycassandra.Order;

/*
 * Copyright 2013 Otávio Gonçalves de Santana (otaviojava)
 * 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.easycassandra.persistence.cassandra;




/**
 * Class to fix a column family.
 * @author otaviojava
 */
class FixColumnFamily {

    private static final String QUERY_PRIMARY_KEY = " PRIMARY KEY (";
    private static final String CLUSTERING_ORDER = " WITH CLUSTERING ORDER BY (";

    /**
     * verify if exist column family and try to create.
     * @param session - bridge to cassandra data base
     * @param keySpace - the keyspace
     * @param familyColumn - name of family column
     * @param class1 - bean
     * @return - if get it or not
     */
    public boolean verifyColumnFamily(Session session, String keySpace, String familyColumn,
            Class<?> class1) {
        try {
            Select select = QueryBuilder.select().from(keySpace, familyColumn).limit(1);
            ResultSet resultSet = session.execute(select);
            verifyRowType(resultSet, class1, session);
            findIndex(class1, session);
            return true;
        } catch (InvalidQueryException exception) {

            if (exception.getCause().getMessage().contains("unconfigured columnfamily ")) {
                Logger.getLogger(FixColumnFamily.class.getName()).info(
                        "Column family doesn't exist, try to create");
                createColumnFamily(familyColumn, class1, session);
                findIndex(class1, session);
                return true;
            }
        }

        return false;
    }

    /**
     * Column family exists verify.
     * @param resultSet
     */
    private void verifyRowType(ResultSet resultSet, Class<?> class1, Session session) {
        Map<String, String> mapNameType = new HashMap<String, String>();
        for (Definition column : resultSet.getColumnDefinitions()) {
            mapNameType
                    .put(column.getName(), column.getType().getName().name());
        }

        ClassInformation classInformation = ClassInformations.INSTACE.getClass(class1);
