@Override
  public void addUserAttributes(
      User user, String[] customUserAttributeNames, AssetEntryQuery assetEntryQuery)
      throws Exception {

    if ((user == null) || (customUserAttributeNames.length == 0)) {
      return;
    }

    Group companyGroup = GroupLocalServiceUtil.getCompanyGroup(user.getCompanyId());

    long[] allCategoryIds = assetEntryQuery.getAllCategoryIds();

    PrimitiveLongList allCategoryIdsList =
        new PrimitiveLongList(allCategoryIds.length + customUserAttributeNames.length);

    allCategoryIdsList.addAll(allCategoryIds);

    for (String customUserAttributeName : customUserAttributeNames) {
      ExpandoBridge userCustomAttributes = user.getExpandoBridge();

      Serializable userCustomFieldValue =
          userCustomAttributes.getAttribute(customUserAttributeName);

      if (userCustomFieldValue == null) {
        continue;
      }

      String userCustomFieldValueString = userCustomFieldValue.toString();

      List<AssetCategory> assetCategories =
          AssetCategoryLocalServiceUtil.search(
              companyGroup.getGroupId(),
              userCustomFieldValueString,
              new String[0],
              QueryUtil.ALL_POS,
              QueryUtil.ALL_POS);

      for (AssetCategory assetCategory : assetCategories) {
        allCategoryIdsList.add(assetCategory.getCategoryId());
      }
    }

    assetEntryQuery.setAllCategoryIds(allCategoryIdsList.getArray());
  }
  public AssetEntryQuery getAssetEntryQuery() throws Exception {
    ThemeDisplay themeDisplay = (ThemeDisplay) _request.getAttribute(WebKeys.THEME_DISPLAY);

    long[] groupIds = getGroupIds();

    if (!ArrayUtil.contains(groupIds, themeDisplay.getScopeGroupId())) {
      groupIds = ArrayUtil.append(groupIds, themeDisplay.getScopeGroupId());
    }

    AssetEntryQuery assetEntryQuery =
        AssetPublisherUtil.getAssetEntryQuery(_portletPreferences, groupIds);

    long[] classNameIds = getClassNameIds();
    long[] classTypeIds = getClassTypeIds();

    if (isSubtypeFieldsFilterEnabled()
        && (classNameIds.length == 1)
        && (classTypeIds.length == 1)
        && Validator.isNotNull(getDDMStructureFieldName())
        && Validator.isNotNull(getDDMStructureFieldValue())) {

      AssetRendererFactory assetRendererFactory =
          AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassName(
              PortalUtil.getClassName(classNameIds[0]));

      Tuple classTypeFieldName =
          assetRendererFactory.getClassTypeFieldName(
              classTypeIds[0], getDDMStructureFieldName(), themeDisplay.getLocale());

      long ddmStructureId = GetterUtil.getLong(classTypeFieldName.getObject(3));

      assetEntryQuery.setAttribute(
          "ddmStructureFieldName",
          DDMIndexerUtil.encodeName(
              ddmStructureId, getDDMStructureFieldName(), themeDisplay.getLocale()));
      assetEntryQuery.setAttribute("ddmStructureFieldValue", getDDMStructureFieldValue());
    }

    AssetPublisherUtil.processAssetEntryQuery(
        themeDisplay.getUser(), _portletPreferences, assetEntryQuery);

    assetEntryQuery.setAllCategoryIds(getAllAssetCategoryIds());

    if (hasLayoutGroup(groupIds)) {
      assetEntryQuery.setAllTagIds(
          AssetTagLocalServiceUtil.getTagIds(groupIds, getAllAssetTagNames()));
    } else {
      assetEntryQuery.setAllTagIds(
          AssetTagLocalServiceUtil.getTagIds(getGroupIds(), getAllAssetTagNames()));
    }

    assetEntryQuery.setClassTypeIds(classTypeIds);
    assetEntryQuery.setEnablePermissions(isEnablePermissions());
    assetEntryQuery.setExcludeZeroViewCount(isExcludeZeroViewCount());

    String portletName = getPortletName();

    if (!portletName.equals(PortletKeys.RELATED_ASSETS)) {
      assetEntryQuery.setGroupIds(getGroupIds());
    }

    if (isShowOnlyLayoutAssets()) {
      assetEntryQuery.setLayout(themeDisplay.getLayout());
    }

    if (portletName.equals(PortletKeys.RELATED_ASSETS)) {
      AssetEntry layoutAssetEntry = (AssetEntry) _request.getAttribute(WebKeys.LAYOUT_ASSET_ENTRY);

      if (layoutAssetEntry != null) {
        assetEntryQuery.setLinkedAssetEntryId(layoutAssetEntry.getEntryId());
      }
    }

    assetEntryQuery.setPaginationType(getPaginationType());
    assetEntryQuery.setOrderByCol1(getOrderByColumn1());
    assetEntryQuery.setOrderByCol2(getOrderByColumn2());
    assetEntryQuery.setOrderByType1(getOrderByType1());
    assetEntryQuery.setOrderByType2(getOrderByType2());

    return assetEntryQuery;
  }
  @Override
  public AssetEntryQuery getAssetEntryQuery(
      PortletPreferences portletPreferences, long[] scopeGroupIds)
      throws PortalException, SystemException {

    AssetEntryQuery assetEntryQuery = new AssetEntryQuery();

    long[] allAssetCategoryIds = new long[0];
    long[] anyAssetCategoryIds = new long[0];
    long[] notAllAssetCategoryIds = new long[0];
    long[] notAnyAssetCategoryIds = new long[0];

    String[] allAssetTagNames = new String[0];
    String[] anyAssetTagNames = new String[0];
    String[] notAllAssetTagNames = new String[0];
    String[] notAnyAssetTagNames = new String[0];

    for (int i = 0; true; i++) {
      String[] queryValues = portletPreferences.getValues("queryValues" + i, null);

      if ((queryValues == null) || (queryValues.length == 0)) {
        break;
      }

      boolean queryContains =
          GetterUtil.getBoolean(portletPreferences.getValue("queryContains" + i, StringPool.BLANK));
      boolean queryAndOperator =
          GetterUtil.getBoolean(
              portletPreferences.getValue("queryAndOperator" + i, StringPool.BLANK));
      String queryName = portletPreferences.getValue("queryName" + i, StringPool.BLANK);

      if (Validator.equals(queryName, "assetCategories")) {
        long[] assetCategoryIds = GetterUtil.getLongValues(queryValues);

        if (queryContains && queryAndOperator) {
          allAssetCategoryIds = assetCategoryIds;
        } else if (queryContains && !queryAndOperator) {
          anyAssetCategoryIds = assetCategoryIds;
        } else if (!queryContains && queryAndOperator) {
          notAllAssetCategoryIds = assetCategoryIds;
        } else {
          notAnyAssetCategoryIds = assetCategoryIds;
        }
      } else {
        if (queryContains && queryAndOperator) {
          allAssetTagNames = queryValues;
        } else if (queryContains && !queryAndOperator) {
          anyAssetTagNames = queryValues;
        } else if (!queryContains && queryAndOperator) {
          notAllAssetTagNames = queryValues;
        } else {
          notAnyAssetTagNames = queryValues;
        }
      }
    }

    assetEntryQuery.setAllCategoryIds(allAssetCategoryIds);

    for (String assetTagName : allAssetTagNames) {
      long[] allAssetTagIds = AssetTagLocalServiceUtil.getTagIds(scopeGroupIds, assetTagName);

      assetEntryQuery.addAllTagIdsArray(allAssetTagIds);
    }

    assetEntryQuery.setAnyCategoryIds(anyAssetCategoryIds);

    long[] anyAssetTagIds = AssetTagLocalServiceUtil.getTagIds(scopeGroupIds, anyAssetTagNames);

    assetEntryQuery.setAnyTagIds(anyAssetTagIds);

    assetEntryQuery.setNotAllCategoryIds(notAllAssetCategoryIds);

    for (String assetTagName : notAllAssetTagNames) {
      long[] notAllAssetTagIds = AssetTagLocalServiceUtil.getTagIds(scopeGroupIds, assetTagName);

      assetEntryQuery.addNotAllTagIdsArray(notAllAssetTagIds);
    }

    assetEntryQuery.setNotAnyCategoryIds(notAnyAssetCategoryIds);

    long[] notAnyAssetTagIds =
        AssetTagLocalServiceUtil.getTagIds(scopeGroupIds, notAnyAssetTagNames);

    assetEntryQuery.setNotAnyTagIds(notAnyAssetTagIds);

    return assetEntryQuery;
  }