/*
 * Huawei Technologies Co., Ltd. Copyright 2005-2016,  All rights reserved
 * 
 * Author: z00206646
 * Date  : 2016-8-10
 */
package com.huawei.soa.ignite.test;

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */


import java.sql.*;
import java.util.*;
import org.apache.ignite.cache.*;
import org.apache.ignite.cache.store.jdbc.*;
import org.apache.ignite.configuration.*;

import com.huawei.soa.ignite.util.IgniteMetaUtils.IgniteProdCacheType;

/**
 * CacheConfig definition.
 *
 * Code generated by Apache Ignite Schema Import utility: 07/11/2016.
 */
public class CacheConfig {
    /**
     * Create JDBC type for PM_ENTITY_RELEASE.
     *
     * @param cacheName Cache name.
     * @return Configured JDBC type.
     */
    public static JdbcType jdbcTypeTestTable(String cacheName) {
        JdbcType jdbcType = new JdbcType();

        jdbcType.setCacheName(cacheName);
        jdbcType.setDatabaseSchema("dequations");
        jdbcType.setDatabaseTable("TEST_TABLE");
        jdbcType.setKeyType("com.huawei.soa.ignite.test.TestTableKey");
        jdbcType.setValueType("com.huawei.soa.ignite.test.TestTable");

        // Key fields for PM_ENTITY_RELEASE.
        Collection<JdbcTypeField> keys = new ArrayList<>();
        keys.add(new JdbcTypeField(Types.VARCHAR, "COLUMN1", String.class, "column1"));
        jdbcType.setKeyFields(keys.toArray(new JdbcTypeField[keys.size()]));

        // Value fields for PM_ENTITY_RELEASE.
        Collection<JdbcTypeField> vals = new ArrayList<>();
       
        vals.add(new JdbcTypeField(Types.VARCHAR, "COLUMN1", String.class, "column1"));
        vals.add(new JdbcTypeField(Types.VARCHAR, "COLUMN2", String.class, "column2"));
        jdbcType.setValueFields(vals.toArray(new JdbcTypeField[vals.size()]));

        return jdbcType;
    }

    /**
     * Create SQL Query descriptor for PM_ENTITY_RELEASE.
     *
     * @return Configured query entity.
     */
    public static QueryEntity queryTestTable() {
        QueryEntity qryEntity = new QueryEntity();

        qryEntity.setKeyType("com.huawei.soa.ignite.test.TestTableKey");
        qryEntity.setValueType("com.huawei.soa.ignite.test.TestTable");

        // Query fields for PM_ENTITY_RELEASE.
        LinkedHashMap<String, String> fields = new LinkedHashMap<>();

        fields.put("column1", "java.lang.String");
        fields.put("column2", "java.lang.String");
        qryEntity.setFields(fields);

        // Indexes for PM_ENTITY_RELEASE.
        Collection<QueryIndex> idxs = new ArrayList<>();

        QueryIndex idx = new QueryIndex();

        idx.setName("COLUMN1");

        LinkedHashMap<String, Boolean> idxFlds = new LinkedHashMap<>();

        idxFlds.put("column1", true);
        idxFlds.put("column2", true);

        idx.setFields(idxFlds);

        idxs.add(idx);

        idxs.add(new QueryIndex("column1", true, "column1Index"));

        qryEntity.setIndexes(idxs);

        return qryEntity;
    }

    

    /**
     * Configure cache.
     *
     * @param cacheName Cache name.
     * @param storeFactory Cache store factory.
     * @return Cache configuration.
     */
    public static <K, V> CacheConfiguration<K, V> cache(
            String cacheName, 
            
            CacheJdbcPojoStoreFactory<K, V> storeFactory) {
        if (storeFactory == null)
             throw new IllegalArgumentException("Cache store factory cannot be null.");

        CacheConfiguration<K, V> ccfg = new CacheConfiguration<>(cacheName);
        ccfg.setCacheStoreFactory(storeFactory);
        ccfg.setReadThrough(false);
        ccfg.setWriteThrough(false);
       
        // Configure JDBC types. 
        Collection<JdbcType> jdbcTypes = new ArrayList<>();
 
        jdbcTypes.add(jdbcTypeTestTable(cacheName));

        storeFactory.setTypes(jdbcTypes.toArray(new JdbcType[jdbcTypes.size()]));



        return ccfg;
    }
}

