/**
 * 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.service.persistence.impl;

import com.liferay.portal.kernel.cache.CacheListener;
import com.liferay.portal.kernel.cache.CacheListenerScope;
import com.liferay.portal.kernel.cache.PortalCache;
import com.liferay.portal.kernel.dao.jdbc.MappingSqlQuery;
import com.liferay.portal.kernel.dao.jdbc.MappingSqlQueryFactoryUtil;
import com.liferay.portal.kernel.dao.jdbc.RowMapper;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.model.BaseModel;
import com.liferay.portal.service.persistence.BasePersistence;

import java.sql.Types;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @author Shuyang Zhou
 */
public class CachelessTableMapperImpl
		<L extends BaseModel<L>, R extends BaseModel<R>>
	extends TableMapperImpl<L, R> {

	public CachelessTableMapperImpl(
		String tableName, String leftColumnName, String rightColumnName,
		BasePersistence<L> leftBasePersistence,
		BasePersistence<R> rightBasePersistence) {

		super(
			tableName, leftColumnName, rightColumnName, leftBasePersistence,
			rightBasePersistence);

		getTableMappingSqlQuery =
			MappingSqlQueryFactoryUtil.getMappingSqlQuery(
				leftBasePersistence.getDataSource(),
				"SELECT * FROM " + tableName + " WHERE " + leftColumnName +
					" = ? AND " + rightColumnName + " = ?",
				new int[] {Types.BIGINT, Types.BIGINT}, RowMapper.COUNT);

		leftToRightPortalCache = new DummyPortalCache(
			leftToRightPortalCache.getName());
		rightToLeftPortalCache = new DummyPortalCache(
			rightToLeftPortalCache.getName());

		destroy();
	}

	@Override
	protected boolean containsTableMapping(
		long leftPrimaryKey, long rightPrimaryKey, boolean updateCache)
	throws SystemException {

		List<Integer> counts = null;

		try {
			counts = getTableMappingSqlQuery.execute(
				leftPrimaryKey, rightPrimaryKey);
		}
		catch (Exception e) {
			throw new SystemException(e);
		}

		if (counts.isEmpty()) {
			return false;
		}

		int count = counts.get(0);

		if (count == 0) {
			return false;
		}

		return true;
	}

	protected final MappingSqlQuery<Integer> getTableMappingSqlQuery;

	protected static class DummyPortalCache
		implements PortalCache<Long, long[]> {

		@Override
		public void destroy() {
		}

		@Override
		public Collection<long[]> get(Collection<Long> keys) {
			return Collections.emptyList();
		}

		@Override
		public long[] get(Long key) {
			return null;
		}

		@Override
		public List<Long> getKeys() {
			return Collections.emptyList();
		}

		@Override
		public String getName() {
			return name;
		}

		@Override
		public void put(Long key, long[] value) {
		}

		@Override
		public void put(Long key, long[] value, int timeToLive) {
		}

		@Override
		public void registerCacheListener(
			CacheListener<Long, long[]> cacheListener) {
		}

		@Override
		public void registerCacheListener(
			CacheListener<Long, long[]> cacheListener,
			CacheListenerScope cacheListenerScope) {
		}

		@Override
		public void remove(Long key) {
		}

		@Override
		public void removeAll() {
		}

		@Override
		public void unregisterCacheListener(
			CacheListener<Long, long[]> cacheListener) {
		}

		@Override
		public void unregisterCacheListeners() {
		}

		protected DummyPortalCache(String name) {
			this.name = name;
		}

		protected final String name;

	}

}