All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.threewks.thundr.csv.CsvMappingFactory Maven / Gradle / Ivy

The newest version!
/*
 * This file is a component of thundr, a software library from 3wks.
 * Read more: http://3wks.github.io/thundr/
 * Copyright (C) 2014 3wks, 
 *
 * 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 com.threewks.thundr.csv;

import java.util.HashMap;
import java.util.Map;

import com.opencsv.bean.HeaderColumnNameMappingStrategy;
import com.opencsv.bean.HeaderColumnNameTranslateMappingStrategy;
import com.opencsv.bean.MappingStrategy;

// TODO - v3 - NAO allow better bean mapping by allowing classes to be registered
// to use the HeaderColumnNameTranslateMappingFactory is so required.

public class CsvMappingFactory {
	private Map, MappingStrategy> strategies = new HashMap<>();

	@SuppressWarnings("unchecked")
	public  MappingStrategy getMappingStrategy(Class type) {
		MappingStrategy registered = (MappingStrategy) strategies.get(type);
		return registered != null ? registered : createDefaultMappingStrategy(type);
	}

	@SuppressWarnings("deprecation")
	protected  MappingStrategy createDefaultMappingStrategy(Class type) {
		HeaderColumnNameMappingStrategy mapper = new HeaderColumnNameMappingStrategy();
		mapper.setType(type);
		return mapper;
	}

	public  void register(Class type, MappingStrategy strategy) {
		strategies.put(type, strategy);
	}

	public  void deregister(Class type) {
		this.strategies.remove(type);
	}

	/**
	 * Rather than register a {@link MappingStrategy} directly, you can just register a mapping of column names to bean property names,
	 * which will automatically be translated when beans of this type are imported or bound.
	 * 
	 * 
	 * @param type
	 * @param columnNames
	 * @see HeaderColumnNameTranslateMappingStrategy#setColumnMapping(Map)
	 */
	public  void register(Class type, Map columnNames) {
		HeaderColumnNameTranslateMappingStrategy mappingStrategy = new HeaderColumnNameTranslateMappingStrategy() {
			// This field is only set when parsing a csv and causes view rendering to fail
			@Override
			public void setColumnMapping(Map columnMapping) {
				super.setColumnMapping(columnMapping);
				this.header = columnMapping.keySet().toArray(new String[0]);
			}
		};
		mappingStrategy.setColumnMapping(columnNames);
		mappingStrategy.setType((Class) type);
		strategies.put(type, mappingStrategy);
	}

	public  boolean contains(Class type) {
		return strategies.containsKey(type);
	}

	public  boolean contains(Class type, Class> strategyClass) {
		MappingStrategy mappingStrategy = strategies.get(type);
		return mappingStrategy != null && mappingStrategy.getClass().equals(strategyClass);
	}
}