package it.mice.voila.runtime.hibernatesearch.annotation;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.hibernate.annotations.common.util.StringHelper;
import org.hibernate.search.SearchException;
import org.hibernate.search.bridge.TwoWayStringBridge;

public class ItalianDateBridge implements TwoWayStringBridge {
	private SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

	public ItalianDateBridge() {
	}

	public Object stringToObject(String stringValue) {
		if (StringHelper.isEmpty(stringValue)) {
			return null;
		}
		try {
			return new SimpleDateFormat(sdf.toPattern()).parse(stringValue);
		} catch (ParseException e) {
			throw new SearchException("Unable to parse into date: " + stringValue, e);
		}
	}

	public String objectToString(Object object) {
		return object != null ? new SimpleDateFormat(sdf.toPattern()).format((Date) object) : null;
	}
}
