/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * The contents of this file are subject to the terms of the Liferay Enterprise
 * Subscription 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.asset.browser.web.internal.search;

import com.liferay.asset.kernel.model.AssetEntry;
import com.liferay.portal.kernel.dao.search.EmptyOnClickRowChecker;
import com.liferay.portal.kernel.util.ArrayUtil;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.StringUtil;

import javax.portlet.PortletRequest;
import javax.portlet.RenderResponse;

/**
 * @author Jürgen Kappler
 */
public class AddAssetEntryChecker extends EmptyOnClickRowChecker {

	public AddAssetEntryChecker(
		PortletRequest portletRequest, RenderResponse renderResponse,
		long assetEntryId) {

		super(renderResponse);

		_portletRequest = portletRequest;
		_assetEntryId = assetEntryId;
	}

	@Override
	public boolean isChecked(Object obj) {
		if ((_portletRequest == null) ||
			(ParamUtil.getString(_portletRequest, "selectedAssetEntryIds") ==
				null)) {

			return super.isChecked(obj);
		}

		String[] selectedAssetEntryIds = StringUtil.split(
			ParamUtil.getString(_portletRequest, "selectedAssetEntryIds"));

		if (ArrayUtil.isEmpty(selectedAssetEntryIds)) {
			return false;
		}

		AssetEntry entry = (AssetEntry)obj;

		if (!ArrayUtil.contains(
				selectedAssetEntryIds, String.valueOf(entry.getEntryId()))) {

			return false;
		}

		return true;
	}

	@Override
	public boolean isDisabled(Object obj) {
		AssetEntry assetEntry = (AssetEntry)obj;

		if (assetEntry.getEntryId() == _assetEntryId) {
			return true;
		}

		return super.isDisabled(obj);
	}

	private final long _assetEntryId;
	private PortletRequest _portletRequest;

}