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

com.threewks.thundr.action.method.bind.csv.CsvBinder 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.action.method.bind.csv;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import com.atomicleopard.expressive.Expressive;
import com.opencsv.CSVReader;
import com.opencsv.bean.CsvToBean;
import com.opencsv.bean.MappingStrategy;
import com.threewks.thundr.bind.BindException;
import com.threewks.thundr.bind.Binder;
import com.threewks.thundr.csv.CsvCommon;
import com.threewks.thundr.csv.CsvMappingFactory;
import com.threewks.thundr.http.ContentType;
import com.threewks.thundr.introspection.ParameterDescription;
import com.threewks.thundr.request.Request;
import com.threewks.thundr.request.Response;

/**
 * Can bind text/csv content to {@link CSVReader} or List controller parameters.
 */
public class CsvBinder implements Binder {

	protected CsvMappingFactory mappingFactory;

	public CsvBinder(CsvMappingFactory mappingFactory) {
		super();
		this.mappingFactory = mappingFactory;
	}

	@Override
	public void bindAll(Map bindings, Request req, Response resp) {
		if (Expressive.isNotEmpty(bindings) && canBind(req.getContentTypeString())) {
			for (Map.Entry binding : bindings.entrySet()) {
				if (binding.getValue() == null) {
					ParameterDescription key = binding.getKey();
					Object value = attemptToBind(key, req);
					bindings.put(key, value);
				}
			}
		}
	}

	public boolean canBind(String contentType) {
		return ContentType.TextCsv.matches(contentType);
	}

	private Object attemptToBind(ParameterDescription key, Request req) {
		try {
			if (key.isA(CSVReader.class)) {
				return CsvCommon.createCsvReader(req);
			}
			if (CsvCommon.isAListOfStringArray(key)) {
				return createStringArrayList(req);
			}
			if (CsvCommon.isAListOfBeans(key)) {
				return createBeanList(key, req);
			}

			return null;
		} catch (IOException e) {
			throw new BindException(e, "Failed to bind csv content: %s", e.getMessage());
		}
	}

	@SuppressWarnings("unchecked")
	protected Object createBeanList(ParameterDescription key, Request req) throws IOException {
		Class type = (Class) key.getGenericType(0);
		MappingStrategy mapper = mappingFactory.getMappingStrategy(type);
		CSVReader csvReader = CsvCommon.createCsvReader(req);
		CsvToBean beanConverter = new CsvToBean();
		List result = beanConverter.parse(mapper, csvReader);
		csvReader.close();
		return result;
	}

	protected Object createStringArrayList(Request req) throws IOException {
		CSVReader csvReader = CsvCommon.createCsvReader(req);
		List listOfStringArray = csvReader.readAll();
		csvReader.close();
		return listOfStringArray;
	}
}