/**
 * 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.portal.verify;

import com.liferay.portal.dao.db.PostgreSQLDB;
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 java.sql.Connection;
import java.sql.PreparedStatement;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Michael Bowerman
 */
public class VerifyPostgreSQL extends VerifyProcess {

	@Override
	protected void doVerify() throws Exception {
		DB db = DBFactoryUtil.getDB();

		String dbType = db.getType();

		if (!dbType.equals(DB.TYPE_POSTGRESQL)) {
			return;
		}

		Map<String, String> oidColumnNames = new HashMap<String, String>();

		oidColumnNames.put("DLContent", "data_");

		updatePostgreSQLRules(oidColumnNames);
	}

	protected void updatePostgreSQLRules(Map<String, String> oidColumnNames)
		throws Exception {

		Connection con = null;
		PreparedStatement ps = null;

		for (Map.Entry<String, String> column : oidColumnNames.entrySet()) {
			String tableName = column.getKey();
			String columnName = column.getValue();

			try {
				con = DataAccess.getUpgradeOptimizedConnection();

				ps = con.prepareStatement(
					PostgreSQLDB.getCreateRulesSQL(tableName, columnName));

				ps.executeUpdate();
			}
			finally {
				DataAccess.cleanUp(con, ps);
			}
		}
	}

}