/**
 * Copyright (c) 2000-2017 Liferay, Inc. All rights reserved.
 *
 * The contents of this file are subject to the terms of the Liferay Enterprise
 * pSubscription License ("License"). You may not use this file except in
 * compliance with the License. You can obtain a copy of the License by
 * contacting Liferay, Inc. See the License for the specific language governing
 * permissions and limitations under the License, including but not limited to
 * distribution rights of the Software.
 *
 *
 *
 */

package com.liferay.portlet.messageboards.util;

import com.liferay.portal.kernel.dao.db.DB;
import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
import com.liferay.portal.kernel.dao.jdbc.DataAccess;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;

import java.util.Date;

/**
 * @author Eric Yan
 */
public class MBMessageDateDigesterUtil {

	public static Date digest(Date date) {
		DB db = DBFactoryUtil.getDB();

		if (db.isSupportsDateMilliseconds()) {
			return date;
		}

		String dbType = db.getType();

		if (dbType.equals(DB.TYPE_MYSQL)) {
			return _digestMySQLDate(date);
		}

		return date;
	}

	private static Date _digestMySQLDate(Date date) {
		long time = date.getTime();

		long milliseconds = time % 1000;

		if (milliseconds > 0) {
			Connection connection = null;

			try {
				connection = DataAccess.getConnection();

				DatabaseMetaData databaseMetaData = connection.getMetaData();

				int dbMajorVersion = databaseMetaData.getDatabaseMajorVersion();
				int dbMinorVersion = databaseMetaData.getDatabaseMinorVersion();

				if (((dbMajorVersion == 5) && (dbMinorVersion == 5)) ||
					(Math.round((double)milliseconds / 1000) == 0)) {

					time -= milliseconds;
				}
				else {
					time += (1000 - milliseconds);
				}

				return new Date(time);
			}
			catch (SQLException se) {
				_log.error("Unable to to get database version", se);
			}
			finally {
				DataAccess.cleanUp(connection);
			}
		}

		return date;
	}

	private static Log _log = LogFactoryUtil.getLog(
		MBMessageDateDigesterUtil.class);

}