protected void addFileEntryTypeAttributes(Document document, DLFileVersion dlFileVersion)
      throws PortalException, SystemException {

    DLFileEntryType dlFileEntryType =
        DLFileEntryTypeLocalServiceUtil.getDLFileEntryType(dlFileVersion.getFileEntryTypeId());

    List<DDMStructure> ddmStructures = dlFileEntryType.getDDMStructures();

    for (DDMStructure ddmStructure : ddmStructures) {
      Fields fields = null;

      try {
        DLFileEntryMetadata fileEntryMetadata =
            DLFileEntryMetadataLocalServiceUtil.getFileEntryMetadata(
                ddmStructure.getStructureId(), dlFileVersion.getFileVersionId());

        fields = StorageEngineUtil.getFields(fileEntryMetadata.getDDMStorageId());
      } catch (Exception e) {
      }

      if (fields != null) {
        DDMIndexerUtil.addAttributes(document, ddmStructure, fields);
      }
    }
  }
  protected String extractDDMContent(DLFileVersion dlFileVersion, Locale locale) throws Exception {

    List<DLFileEntryMetadata> dlFileEntryMetadatas =
        DLFileEntryMetadataLocalServiceUtil.getFileVersionFileEntryMetadatas(
            dlFileVersion.getFileVersionId());

    StringBundler sb = new StringBundler(dlFileEntryMetadatas.size());

    for (DLFileEntryMetadata dlFileEntryMetadata : dlFileEntryMetadatas) {
      Fields fields = null;

      try {
        fields = StorageEngineUtil.getFields(dlFileEntryMetadata.getDDMStorageId());
      } catch (Exception e) {
      }

      if (fields != null) {
        DDMStructure ddmStructure =
            DDMStructureLocalServiceUtil.getStructure(dlFileEntryMetadata.getDDMStructureId());

        sb.append(DDMIndexerUtil.extractAttributes(ddmStructure, fields, locale));
      }
    }

    return sb.toString();
  }
  protected String extractDDMContent(JournalArticle article, String languageId) throws Exception {

    DDMStructure ddmStructure =
        _ddmStructureLocalService.fetchStructure(
            article.getGroupId(),
            PortalUtil.getClassNameId(JournalArticle.class),
            article.getDDMStructureKey(),
            true);

    if (ddmStructure == null) {
      return StringPool.BLANK;
    }

    DDMFormValues ddmFormValues = null;

    try {
      Fields fields = JournalConverterUtil.getDDMFields(ddmStructure, article.getDocument());

      ddmFormValues = FieldsToDDMFormValuesConverterUtil.convert(ddmStructure, fields);
    } catch (Exception e) {
      return StringPool.BLANK;
    }

    if (ddmFormValues == null) {
      return StringPool.BLANK;
    }

    return DDMIndexerUtil.extractAttributes(
        ddmStructure, ddmFormValues, LocaleUtil.fromLanguageId(languageId));
  }
  protected void addDDMStructureAttributes(Document document, JournalArticle article)
      throws Exception {

    DDMStructure ddmStructure =
        _ddmStructureLocalService.fetchStructure(
            article.getGroupId(),
            PortalUtil.getClassNameId(JournalArticle.class),
            article.getDDMStructureKey(),
            true);

    if (ddmStructure == null) {
      return;
    }

    document.addKeyword(Field.CLASS_TYPE_ID, ddmStructure.getStructureId());

    DDMFormValues ddmFormValues = null;

    try {
      Fields fields = JournalConverterUtil.getDDMFields(ddmStructure, article.getDocument());

      ddmFormValues = FieldsToDDMFormValuesConverterUtil.convert(ddmStructure, fields);
    } catch (Exception e) {
      return;
    }

    if (ddmFormValues != null) {
      DDMIndexerUtil.addAttributes(document, ddmStructure, ddmFormValues);
    }
  }
  protected void addFileEntryTypeAttributes(Document document, DLFileVersion dlFileVersion)
      throws PortalException {

    List<DLFileEntryMetadata> dlFileEntryMetadatas =
        DLFileEntryMetadataLocalServiceUtil.getFileVersionFileEntryMetadatas(
            dlFileVersion.getFileVersionId());

    for (DLFileEntryMetadata dlFileEntryMetadata : dlFileEntryMetadatas) {
      Fields fields = null;

      try {
        fields = StorageEngineUtil.getFields(dlFileEntryMetadata.getDDMStorageId());
      } catch (Exception e) {
      }

      if (fields != null) {
        DDMStructure ddmStructure =
            DDMStructureLocalServiceUtil.getStructure(dlFileEntryMetadata.getDDMStructureId());

        DDMIndexerUtil.addAttributes(document, ddmStructure, fields);
      }
    }
  }
  public void postProcessContextQuery(BooleanQuery contextQuery, SearchContext searchContext)
      throws Exception {

    if (_log.isDebugEnabled()) {
      _log.debug(" postProcessContextQuery()");
    }

    Map<String, Object> params = (Map<String, Object>) searchContext.getAttribute("params");

    if (params == null) {
      return;
    }

    Map<String, Serializable> searchByFields =
        (Map<String, Serializable>) params.get(SampleConstants.STRUCTURE_FIELDS);

    String structureKey = (String) params.get(SampleConstants.STRUCTURE_KEY);

    Long groupId = (Long) params.get(SampleConstants.STRUCTURE_GROUP_ID);

    Locale locale = searchContext.getLocale();

    DDMStructure structure =
        DDMStructureLocalServiceUtil.fetchStructure(
            groupId, PortalUtil.getClassNameId(JournalArticle.class), structureKey, true);

    if (!isCustomSearch(structure, groupId, searchByFields)) {
      return;
    }

    for (String fieldName : searchByFields.keySet()) {
      try {
        Serializable value = searchByFields.get(fieldName);

        String indexType = structure.getFieldProperty(fieldName, "indexType");

        if (Validator.isNull(indexType)) {
          if (_log.isDebugEnabled()) {
            _log.debug("fieldName " + fieldName + " is not indexable");
          }

          continue;
        }

        // some types are stored in a special way

        try {
          value = DDMUtil.getIndexedFieldValue(value, structure.getFieldType(fieldName));
        } catch (StructureFieldException sfe) {
          _log.error(sfe);

          continue;
        }

        contextQuery.addRequiredTerm(
            DDMIndexerUtil.encodeName(structure.getStructureId(), fieldName, locale),
            StringPool.QUOTE + value + StringPool.QUOTE);
      } catch (Exception e) {
        _log.error("Error processing custom search", e);
      }
    }

    _log.debug(contextQuery);
  }
  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;
  }