/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2018 by rumatoest at github.com
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE. 
 */
package com.github.jneat.mybatis;

import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;

import java.io.IOException;
import java.io.StringWriter;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;

/**
 * Allows you to easy denormalize your schema, using text fields with java properties.
 * This handler use java Properties to de/serialize you string key-value pairs.
 * Instead using java.util.Properties you use Map<String, String> implementations.
 * WARNING. You must use only HashMap or LinkedHashMap, but your field must be defined
 * as Map<String, String>!
 * 
 * <b>Things you should note about result</b>
 * <ul>
 * <li>It will never be null, you'll get empty map for null DB</li>
 * <li>It will be immutable, so in order to update you should create new Map instance</li>
 * <li>Result map is lazy, it will parse field value only on demand, thus exception may be thrown if DB value is not valid</li>
 * </ul>
 */
@MappedTypes({Map.class, HashMap.class, LinkedHashMap.class, MapLazyWrapper.class})
public class MapTypeHandler extends NotNullResultTypeHandler<Map<String, String>> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, Map<String, String> parameter, JdbcType jdbcType) throws SQLException {
        Properties properties = new Properties();
        properties.putAll(parameter);
        StringWriter sw = new StringWriter();
        try {
            properties.store(sw, "Generated by mybatis-types");
        } catch (IOException ex) {
            throw new RuntimeException(ex.getMessage(), ex);
        }
        ps.setString(i, sw.toString());
    }

    @Override
    public Map<String, String> getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String value = rs.getString(columnName);
        return new MapLazyWrapper(value);
    }

    @Override
    public Map<String, String> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String value = rs.getString(columnIndex);
        return new MapLazyWrapper(value);
    }

    @Override
    public Map<String, String> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        String value = cs.getString(columnIndex);
        return new MapLazyWrapper(value);
    }

}