/*******************************************************************************
 * Copyright 2019 2Fi Business Solutions Ltd.
 *
 * This code is part of the Core project.
 *
 * This code is copyrighted. Under no circumstances should any party, people,
 * or organization should redistribute any portions of this code in any form,
 * either verbatim or through electronic media, to any third parties, unless
 * under explicit written permission by 2Fi Business Solutions Ltd.
 ******************************************************************************/
package com.ffii.core.support;

import java.beans.PropertyEditorSupport;
import java.util.Date;

import org.springframework.util.StringUtils;

import com.ffii.core.utils.DateUtils;

/**
 * Property Editor for Date in the format defined by {@link DateUtils#PARSE_PATTERNS}<br>
 * <p>
 * Defaults to <code>null</code> if cannot be parsed, never throw an Exception
 * </p>
 *
 * @see DateUtils#parseDateStrictly(String, String[], Date)
 *
 * @author Patrick
 */
public class SimpleDateEditor extends PropertyEditorSupport {

	@Override
	public void setAsText(String text) throws IllegalArgumentException {
		if (StringUtils.hasText(text))
			setValue(DateUtils.parseDateStrictly(text, DateUtils.PARSE_PATTERNS, null));
		else
			setValue(null);
	}

	@Override
	public String getAsText() {
		return DateUtils.formatDate((Date) getValue());
	}

}
