/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.content.targeting.portlet.util;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.language.LanguageUtil;
import com.liferay.portal.kernel.util.ArrayUtil;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portlet.asset.model.AssetCategory;
import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
import java.util.Locale;
import javax.portlet.PortletConfig;
/**
* @author Eudaldo Alonso
*/
public class UserSegmentQueryRule extends AssetQueryRule implements QueryRule {
public UserSegmentQueryRule() throws PortalException, SystemException {
this(true, true, 0, null, 0, null);
}
public UserSegmentQueryRule(
boolean andOperator, boolean contains, long assetEntryId,
long[] userSegmentAssetCategoryIds, int index, Locale locale)
throws PortalException, SystemException {
super(assetEntryId, index, locale);
_andOperator = andOperator;
_contains = contains;
_userSegmentAssetCategoryIds = validateUserSegmentAssetCategoryIds(
userSegmentAssetCategoryIds);
}
public boolean evaluate(long[] userSegmentAssetCategoryIds) {
if (isDefaultRule()) {
return true;
}
if (_contains) {
if (_andOperator) {
return ArrayUtil.containsAll(
userSegmentAssetCategoryIds, _userSegmentAssetCategoryIds);
}
for (long userSegmentAssetCategoryId :
_userSegmentAssetCategoryIds) {
if (ArrayUtil.contains(
userSegmentAssetCategoryIds,
userSegmentAssetCategoryId)) {
return true;
}
}
return false;
}
if (_andOperator) {
for (long userSegmentAssetCategoryId :
_userSegmentAssetCategoryIds) {
if (ArrayUtil.contains(
userSegmentAssetCategoryIds,
userSegmentAssetCategoryId)) {
return false;
}
}
return true;
}
for (long userSegmentAssetCategoryId : _userSegmentAssetCategoryIds) {
if (!ArrayUtil.contains(
userSegmentAssetCategoryIds, userSegmentAssetCategoryId)) {
return true;
}
}
return false;
}
public String getSummary(PortletConfig portletConfig, Locale locale)
throws SystemException {
if (ArrayUtil.isEmpty(_userSegmentAssetCategoryIds)) {
return LanguageUtil.get(portletConfig, locale, "default");
}
String summary = StringPool.BLANK;
if (!_contains) {
summary = summary + htmlOperator(
null, _contains, portletConfig, locale);
}
return summary +
getUserSegmentNames(
locale,
htmlOperator(_andOperator, _contains, portletConfig, locale));
}
public long[] getUserSegmentAssetCategoryIds() {
return _userSegmentAssetCategoryIds;
}
public String getUserSegmentAssetCategoryIdsAsString() {
if (ArrayUtil.isEmpty(_userSegmentAssetCategoryIds)) {
return StringPool.BLANK;
}
return StringUtil.merge(_userSegmentAssetCategoryIds);
}
public String getUserSegmentAssetCategoryNames(Locale locale)
throws SystemException {
return getUserSegmentNames(locale, _CATEGORY_SEPARATOR);
}
public String getUserSegmentNames(Locale locale) throws SystemException {
return getUserSegmentNames(locale, StringPool.COMMA_AND_SPACE);
}
public boolean isAndOperator() {
return _andOperator;
}
public boolean isContains() {
return _contains;
}
public boolean isValid() {
if (!super.isValid() ||
ArrayUtil.isEmpty(_userSegmentAssetCategoryIds)) {
return false;
}
return true;
}
public void setAndOperator(boolean andOperator) {
_andOperator = andOperator;
}
public void setContains(boolean contains) {
_contains = contains;
}
public void setUserSegmentAssetCategoryIds(
long[] userSegmentAssetCategoryIds) {
_userSegmentAssetCategoryIds = userSegmentAssetCategoryIds;
}
protected String getUserSegmentNames(Locale locale, String separator)
throws SystemException {
if (ArrayUtil.isEmpty(_userSegmentAssetCategoryIds)) {
return StringPool.BLANK;
}
StringBundler sb = new StringBundler(
(_userSegmentAssetCategoryIds.length * 2) - 1);
for (int i = 0; i < _userSegmentAssetCategoryIds.length; i++) {
AssetCategory assetCategory =
AssetCategoryLocalServiceUtil.fetchAssetCategory(
_userSegmentAssetCategoryIds[i]);
if (assetCategory == null) {
continue;
}
sb.append(assetCategory.getTitle(locale));
if (i != (_userSegmentAssetCategoryIds.length - 1)) {
sb.append(separator);
}
}
return sb.toString();
}
protected long[] validateUserSegmentAssetCategoryIds(
long[] userSegmentAssetCategoryIds) {
if (Validator.isNull(userSegmentAssetCategoryIds)) {
return userSegmentAssetCategoryIds;
}
long[] validatedUserSegmentAssetCategoryIds =
userSegmentAssetCategoryIds;
for (long userSegmentAssetCategoryId : userSegmentAssetCategoryIds) {
try {
AssetCategory assetCategory =
AssetCategoryLocalServiceUtil.fetchAssetCategory(
userSegmentAssetCategoryId);
if (assetCategory == null) {
validatedUserSegmentAssetCategoryIds = ArrayUtil.remove(
validatedUserSegmentAssetCategoryIds,
userSegmentAssetCategoryId);
}
}
catch (Exception e) {
}
}
return validatedUserSegmentAssetCategoryIds;
}
private static final String _CATEGORY_SEPARATOR = "_CATEGORY_";
private boolean _andOperator;
private boolean _contains = true;
private long[] _userSegmentAssetCategoryIds;
}