package it.mice.voila.runtime.entity;

import it.mice.voila.runtime.util.DateTimeUtils;
import it.mice.voila.runtime.util.FormatterUtil;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.Date;

import org.apache.commons.lang.time.DateUtils;

/**
 * Classe DAOGeneric. La presente abstract class rappresenta il punto d'accesso unico per
 * tutti i dataaccess. Essa, oltre a fornire la connessione al DBMS, offre anche la
 * possibilità, in futuro, di aggiungere funzioni centralizzate utilizzabili da tutti i
 * dataaccess.
 */
public abstract class CoreEntityGeneric implements CoreEntity {
	protected static final String STRING_0 = "0";
	protected static final String STRING_1 = "1";
	protected static final Byte BYTE_0 = new Byte((byte) 0);
	protected static final Byte BYTE_1 = new Byte((byte) 1);
	protected static final Integer INTEGER_0 = new Integer(0);
	protected static final Integer INTEGER_1 = new Integer(1);
	/**
	 * Costruttore EntityGeneric.
	 */
	public CoreEntityGeneric() {
	}

	protected boolean isValueChanged(Object currentValue, Object newValue,
			boolean ignoreCase) {
		if (currentValue == null) {
			return (newValue != null);
		} else {
			if (currentValue instanceof BigDecimal) {
				if (newValue == null) {
					return true;
				}
				return ((BigDecimal) currentValue).compareTo(((BigDecimal) newValue)) != 0;
			} else if (currentValue instanceof Date) {
				if (newValue == null) {
					return true;
				}
				return !DateUtils.isSameInstant((Date)currentValue, (Date)newValue);
			} else {
				if (ignoreCase) {
					return (!((String) currentValue).equalsIgnoreCase((String) newValue));
				} else {
					return (!currentValue.equals(newValue));
				}
			}
		}
	}

	protected boolean isValueChanged(Object currentValue, Object newValue) {
		if (currentValue == null) {
			return (newValue != null);
		} else {
			return (currentValue != newValue);
		}
	}

	public CoreEntity getThis() {
		return this;
	}

	protected String getStringCheckedAgainstNullContent(String token) {
		return (token.equals("null")) ? null : token;
	}

	protected String getStringAsString(String value) {
		return value;
	}

	protected Timestamp getTimestampCheckedAgainstNullContent(String token) {
		return (token.equals("null")) ? null : new Timestamp(DateTimeUtils.parseStandardDate(token).getTime());
	}

	protected String getTimestampAsString(Timestamp value) {
		return (value == null) ? "null" : DateTimeUtils.formatStandardDate(value);
	}

	protected Date getDateCheckedAgainstNullContent(String token) {
		return (token.equals("null")) ? null : DateTimeUtils.parseStandardDate(token);
	}
	
	protected Date getDateNoMillisecondCheckedAgainstNullContent(String token) {
		return (token.equals("null")) ? null : DateTimeUtils.parseStandardDateWithoutMillisecond(token);
	}

	
	protected String getDateAsString(Date date) {
		return (date == null) ? "null" : DateTimeUtils.formatStandardDate(date);
	}

	protected Byte getByteCheckedAgainstNullContent(String token) {
		return (token.equals("null")) ? null : new Byte(token);
	}

	protected String getByteAsString(Byte value) {
		return (value == null) ? "null" : value.toString();
	}

	protected Short getShortCheckedAgainstNullContent(String token) {
		return (token.equals("null")) ? null : new Short(token);
	}

	protected String getShortAsString(Short value) {
		return (value == null) ? "null" : value.toString();
	}

	protected Integer getIntegerCheckedAgainstNullContent(String token) {
		return (token.equals("null")) ? null : new Integer(token);
	}

	protected String getIntegerAsString(Integer value) {
		return (value == null) ? "null" : value.toString();
	}

	protected Long getLongCheckedAgainstNullContent(String token) {
		return (token.equals("null")) ? null : new Long(token);
	}

	protected String getLongAsString(Long value) {
		return (value == null) ? "null" : value.toString();
	}

	protected BigInteger getBigIntegerCheckedAgainstNullContent(String token) {
		return (token.equals("null")) ? null : new BigInteger(token);
	}

	protected String getBigIntegerAsString(BigInteger value) {
		return (value == null) ? "null" : value.toString();
	}

	protected BigDecimal getBigDecimalCheckedAgainstNullContent(String token) {
		return (token.equals("null")) ? null : new BigDecimal(token);
	}

	protected String getBigDecimalAsString(BigDecimal value) {
		return (value == null) ? "null" : value.toString();
	}

	protected Boolean equalsPreliminaryCheck(Object o) {
		if (this == o) {
			return Boolean.TRUE;
		}
		if (o == null || getClass() != o.getClass()) {
			return Boolean.FALSE;
		}
		return null;
	}

	protected Boolean equalsAttributeCheck(Object thisAttribute, Object otherAttribute) {
		if (thisAttribute != null ? !thisAttribute.equals(otherAttribute)
				: otherAttribute != null) {
			return Boolean.FALSE;
		}
		return null;
	}

	protected int hashCodeFor(Object attribute) {
		return (attribute != null ? attribute.hashCode() : 0);
	}

	public java.sql.Timestamp getCurrentTimestamp() {
		return new java.sql.Timestamp(System.currentTimeMillis());
	}

	protected String formatDate(java.sql.Timestamp date) {
		return FormatterUtil.formatAttributeToString(date);
	}

	protected String formatDate(java.util.Date date) {
		return FormatterUtil.formatAttributeToString(date);
	}

	/**
	 * emptyIfNull.
	 * @param address2
	 * @return
	 */
	protected Object emptyIfNull(Object value) {
		if (value == null) {
			return "";
		}
		return value.toString();
	}

}
