@Override
  public String getPortletId() {
    AssetRendererFactory<MBCategory> assetRendererFactory =
        AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClass(MBCategory.class);

    return assetRendererFactory.getPortletId();
  }
  protected String getEntryURLViewInContext(
      PortletRequest portletRequest, PortletResponse portletResponse, AssetEntry assetEntry)
      throws Exception {

    AssetRendererFactory assetRendererFactory =
        AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassName(
            assetEntry.getClassName());

    AssetRenderer assetRenderer = assetRendererFactory.getAssetRenderer(assetEntry.getClassPK());

    String viewInContextURL =
        assetRenderer.getURLViewInContext(
            (LiferayPortletRequest) portletRequest, (LiferayPortletResponse) portletResponse, null);

    if (Validator.isNotNull(viewInContextURL)
        && !viewInContextURL.startsWith(Http.HTTP_WITH_SLASH)
        && !viewInContextURL.startsWith(Http.HTTPS_WITH_SLASH)) {

      ThemeDisplay themeDisplay = (ThemeDisplay) portletRequest.getAttribute(WebKeys.THEME_DISPLAY);

      viewInContextURL = themeDisplay.getPortalURL() + viewInContextURL;
    }

    return viewInContextURL;
  }
  @Override
  protected String getTitle(
      JSONObject jsonObject, AssetRenderer<?> assetRenderer, ServiceContext serviceContext) {

    MBMessage mbMessage = MBMessageLocalServiceUtil.fetchMBMessage(jsonObject.getLong("classPK"));

    AssetRendererFactory<?> assetRendererFactory =
        AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassName(
            assetRenderer.getClassName());

    String typeName = assetRendererFactory.getTypeName(serviceContext.getLocale());

    ResourceBundle resourceBundle =
        ResourceBundle.getBundle("content.Language", serviceContext.getLocale());

    if ((mbMessage != null) && mbMessage.isDiscussion()) {
      return LanguageUtil.format(
          resourceBundle,
          "x-mentioned-you-in-a-comment-in-a-x",
          new String[] {
            HtmlUtil.escape(assetRenderer.getUserName()),
            StringUtil.toLowerCase(HtmlUtil.escape(typeName))
          },
          false);
    } else {
      return LanguageUtil.format(
          resourceBundle,
          "x-mentioned-you-in-a-x",
          new String[] {
            HtmlUtil.escape(assetRenderer.getUserName()),
            StringUtil.toLowerCase(HtmlUtil.escape(typeName))
          },
          false);
    }
  }
  public long[] getAvailableClassNameIds() {
    if (_availableClassNameIds == null) {
      ThemeDisplay themeDisplay = (ThemeDisplay) _request.getAttribute(WebKeys.THEME_DISPLAY);

      _availableClassNameIds =
          AssetRendererFactoryRegistryUtil.getClassNameIds(themeDisplay.getCompanyId(), true);
    }

    return _availableClassNameIds;
  }
  protected void sendNotificationEvent(
      final MicroblogsEntry microblogsEntry, ServiceContext serviceContext) throws PortalException {

    final JSONObject notificationEventJSONObject = JSONFactoryUtil.createJSONObject();

    notificationEventJSONObject.put("className", MicroblogsEntry.class.getName());
    notificationEventJSONObject.put("classPK", microblogsEntry.getMicroblogsEntryId());
    notificationEventJSONObject.put(
        "entryTitle",
        MicroblogsUtil.getProcessedContent(
            StringUtil.shorten(microblogsEntry.getContent(), 50), serviceContext));

    AssetRendererFactory<MicroblogsEntry> assetRendererFactory =
        AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClass(MicroblogsEntry.class);

    AssetRenderer<MicroblogsEntry> assetRenderer =
        assetRendererFactory.getAssetRenderer(microblogsEntry.getMicroblogsEntryId());

    String entryURL = StringPool.BLANK;

    try {
      entryURL =
          assetRenderer.getURLViewInContext(
              serviceContext.getLiferayPortletRequest(),
              serviceContext.getLiferayPortletResponse(),
              null);
    } catch (Exception e) {
      if (_log.isDebugEnabled()) {
        _log.debug(e, e);
      }
    }

    notificationEventJSONObject.put("entryURL", entryURL);
    notificationEventJSONObject.put("userId", microblogsEntry.getUserId());

    final List<Long> receiverUserIds = MicroblogsUtil.getSubscriberUserIds(microblogsEntry);

    Callable<Void> callable =
        new Callable<Void>() {

          @Override
          public Void call() throws Exception {
            MessageBusUtil.sendMessage(
                DestinationNames.ASYNC_SERVICE,
                new NotificationProcessCallable(
                    receiverUserIds, microblogsEntry, notificationEventJSONObject));

            return null;
          }
        };

    TransactionCommitCallbackUtil.registerCallback(callable);
  }
  protected static AssetRenderer doGetAssetRenderer(String className, long classPK)
      throws Exception {

    if (className.equals(MBThread.class.getName())) {
      className = MBMessage.class.getName();

      MBThread mbThread = MBThreadLocalServiceUtil.getThread(classPK);

      classPK = mbThread.getRootMessageId();
    }

    AssetRendererFactory assetRendererFactory =
        AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassName(className);

    return assetRendererFactory.getAssetRenderer(classPK);
  }
  protected String getEntryURLAssetPublisher(
      PortletRequest portletRequest, PortletResponse portletResponse, AssetEntry assetEntry)
      throws Exception {

    AssetRendererFactory assetRendererFactory =
        AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassName(
            assetEntry.getClassName());

    StringBundler sb = new StringBundler(4);

    sb.append(getAssetPublisherURL(portletRequest));
    sb.append(assetRendererFactory.getType());
    sb.append("/id/");
    sb.append(assetEntry.getEntryId());

    return sb.toString();
  }
  protected String[] getClassNames(String className) {
    if (Validator.isNotNull(className)) {
      return new String[] {className};
    } else {
      List<AssetRendererFactory> rendererFactories =
          AssetRendererFactoryRegistryUtil.getAssetRendererFactories();

      String[] classNames = new String[rendererFactories.size()];

      for (int i = 0; i < rendererFactories.size(); i++) {
        AssetRendererFactory rendererFactory = rendererFactories.get(i);

        classNames[i] = rendererFactory.getClassName();
      }

      return classNames;
    }
  }
  protected void setDDMStructure() throws Exception {
    ThemeDisplay themeDisplay = (ThemeDisplay) _request.getAttribute(WebKeys.THEME_DISPLAY);

    _ddmStructureDisplayFieldValue = StringPool.BLANK;
    _ddmStructureFieldLabel = StringPool.BLANK;
    _ddmStructureFieldName = StringPool.BLANK;
    _ddmStructureFieldValue = null;

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

    if (isSubtypeFieldsFilterEnabled()
        && (classNameIds.length == 1)
        && (classTypeIds.length == 1)) {

      _ddmStructureDisplayFieldValue =
          GetterUtil.getString(
              _portletPreferences.getValue("ddmStructureDisplayFieldValue", StringPool.BLANK));
      _ddmStructureFieldName =
          GetterUtil.getString(
              _portletPreferences.getValue("ddmStructureFieldName", StringPool.BLANK));
      _ddmStructureFieldValue =
          _portletPreferences.getValue("ddmStructureFieldValue", StringPool.BLANK);

      if (Validator.isNotNull(_ddmStructureFieldName)
          && Validator.isNotNull(_ddmStructureFieldValue)) {

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

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

        _ddmStructureFieldLabel = (String) classTypeFieldName.getObject(0);
      }
    }
  }
  protected String getArticleId(PortletRequest portletRequest) throws PortalException {

    long assetEntryId = GetterUtil.getLong(getParameter(portletRequest, "assetEntryId"));

    AssetEntry assetEntry = _assetEntryLocalService.fetchEntry(assetEntryId);

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

    JournalArticleAssetRendererFactory articleAssetRendererFactory =
        (JournalArticleAssetRendererFactory)
            AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassName(
                JournalArticle.class.getName());

    JournalArticleAssetRenderer articleAssetRenderer =
        (JournalArticleAssetRenderer)
            articleAssetRendererFactory.getAssetRenderer(assetEntry.getClassPK());

    JournalArticle article = articleAssetRenderer.getArticle();

    return StringUtil.toUpperCase(article.getArticleId());
  }
Exemple #11
0
  protected List<AssetEntry> getAssetEntries(
      PortletRequest portletRequest, PortletPreferences preferences) throws Exception {

    ThemeDisplay themeDisplay = (ThemeDisplay) portletRequest.getAttribute(WebKeys.THEME_DISPLAY);

    AssetEntryQuery assetEntryQuery =
        AssetPublisherUtil.getAssetEntryQuery(
            preferences, new long[] {themeDisplay.getScopeGroupId()});

    boolean anyAssetType = GetterUtil.getBoolean(preferences.getValue("anyAssetType", null), true);

    if (!anyAssetType) {
      long[] availableClassNameIds = AssetRendererFactoryRegistryUtil.getClassNameIds();

      long[] classNameIds = AssetPublisherUtil.getClassNameIds(preferences, availableClassNameIds);

      assetEntryQuery.setClassNameIds(classNameIds);
    }

    long[] classTypeIds = GetterUtil.getLongValues(preferences.getValues("classTypeIds", null));

    assetEntryQuery.setClassTypeIds(classTypeIds);

    boolean enablePermissions =
        GetterUtil.getBoolean(preferences.getValue("enablePermissions", null));

    assetEntryQuery.setEnablePermissions(enablePermissions);

    int rssDelta = GetterUtil.getInteger(preferences.getValue("rssDelta", "20"));

    assetEntryQuery.setEnd(rssDelta);

    boolean excludeZeroViewCount =
        GetterUtil.getBoolean(preferences.getValue("excludeZeroViewCount", null));

    assetEntryQuery.setExcludeZeroViewCount(excludeZeroViewCount);

    long[] groupIds =
        AssetPublisherUtil.getGroupIds(
            preferences, themeDisplay.getScopeGroupId(), themeDisplay.getLayout());

    assetEntryQuery.setGroupIds(groupIds);

    boolean showOnlyLayoutAssets =
        GetterUtil.getBoolean(preferences.getValue("showOnlyLayoutAssets", null));

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

    String orderByColumn1 =
        GetterUtil.getString(preferences.getValue("orderByColumn1", "modifiedDate"));

    assetEntryQuery.setOrderByCol1(orderByColumn1);

    String orderByColumn2 = GetterUtil.getString(preferences.getValue("orderByColumn2", "title"));

    assetEntryQuery.setOrderByCol2(orderByColumn2);

    String orderByType1 = GetterUtil.getString(preferences.getValue("orderByType1", "DESC"));

    assetEntryQuery.setOrderByType1(orderByType1);

    String orderByType2 = GetterUtil.getString(preferences.getValue("orderByType2", "ASC"));

    assetEntryQuery.setOrderByType2(orderByType2);

    assetEntryQuery.setStart(0);

    return AssetEntryServiceUtil.getEntries(assetEntryQuery);
  }
 protected AssetRendererFactory getAssetRendererFactory() {
   return AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassName(getClassName());
 }
  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;
  }
  public static Map<String, PortletURL> getAddPortletURLs(
      LiferayPortletRequest liferayPortletRequest,
      LiferayPortletResponse liferayPortletResponse,
      long groupId,
      long[] classNameIds,
      long[] classTypeIds,
      long[] allAssetCategoryIds,
      String[] allAssetTagNames,
      String redirect)
      throws Exception {

    ThemeDisplay themeDisplay =
        (ThemeDisplay) liferayPortletRequest.getAttribute(WebKeys.THEME_DISPLAY);

    Map<String, PortletURL> addPortletURLs =
        new TreeMap<>(new ModelResourceComparator(themeDisplay.getLocale()));

    for (long classNameId : classNameIds) {
      String className = PortalUtil.getClassName(classNameId);

      AssetRendererFactory<?> assetRendererFactory =
          AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassName(className);

      if (Validator.isNull(assetRendererFactory.getPortletId())) {
        continue;
      }

      Portlet portlet =
          PortletLocalServiceUtil.getPortletById(
              themeDisplay.getCompanyId(), assetRendererFactory.getPortletId());

      if (!portlet.isActive()) {
        continue;
      }

      ClassTypeReader classTypeReader = assetRendererFactory.getClassTypeReader();

      List<ClassType> classTypes =
          classTypeReader.getAvailableClassTypes(
              PortalUtil.getCurrentAndAncestorSiteGroupIds(themeDisplay.getScopeGroupId()),
              themeDisplay.getLocale());

      if ((classTypeIds.length == 0) || classTypes.isEmpty()) {
        PortletURL addPortletURL =
            getAddPortletURL(
                liferayPortletRequest,
                liferayPortletResponse,
                groupId,
                className,
                0,
                allAssetCategoryIds,
                allAssetTagNames,
                redirect);

        if (addPortletURL != null) {
          addPortletURLs.put(className, addPortletURL);
        }
      }

      for (ClassType classType : classTypes) {
        long classTypeId = classType.getClassTypeId();

        if (ArrayUtil.contains(classTypeIds, classTypeId) || (classTypeIds.length == 0)) {

          PortletURL addPortletURL =
              getAddPortletURL(
                  liferayPortletRequest,
                  liferayPortletResponse,
                  groupId,
                  className,
                  classTypeId,
                  allAssetCategoryIds,
                  allAssetTagNames,
                  redirect);

          if (addPortletURL != null) {
            String mesage = className + CLASSNAME_SEPARATOR + classType.getName();

            addPortletURLs.put(mesage, addPortletURL);
          }
        }
      }
    }

    return addPortletURLs;
  }
  public static PortletURL getAddPortletURL(
      LiferayPortletRequest liferayPortletRequest,
      LiferayPortletResponse liferayPortletResponse,
      long groupId,
      String className,
      long classTypeId,
      long[] allAssetCategoryIds,
      String[] allAssetTagNames,
      String redirect)
      throws Exception {

    ThemeDisplay themeDisplay =
        (ThemeDisplay) liferayPortletRequest.getAttribute(WebKeys.THEME_DISPLAY);

    AssetRendererFactory<?> assetRendererFactory =
        AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassName(className);

    if ((assetRendererFactory == null)
        || !assetRendererFactory.hasAddPermission(
            themeDisplay.getPermissionChecker(), groupId, classTypeId)) {

      return null;
    }

    PortletURL addPortletURL =
        assetRendererFactory.getURLAdd(liferayPortletRequest, liferayPortletResponse, classTypeId);

    if (addPortletURL == null) {
      return null;
    }

    if (redirect != null) {
      addPortletURL.setParameter("redirect", redirect);
    }

    String referringPortletResource = ParamUtil.getString(liferayPortletRequest, "portletResource");

    if (Validator.isNotNull(referringPortletResource)) {
      addPortletURL.setParameter("referringPortletResource", referringPortletResource);
    } else {
      PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();

      addPortletURL.setParameter("referringPortletResource", portletDisplay.getId());

      if (allAssetCategoryIds != null) {
        Map<Long, String> assetVocabularyAssetCategoryIds = new HashMap<>();

        for (long assetCategoryId : allAssetCategoryIds) {
          AssetCategory assetCategory =
              AssetCategoryLocalServiceUtil.fetchAssetCategory(assetCategoryId);

          if (assetCategory == null) {
            continue;
          }

          long assetVocabularyId = assetCategory.getVocabularyId();

          if (assetVocabularyAssetCategoryIds.containsKey(assetVocabularyId)) {

            String assetCategoryIds = assetVocabularyAssetCategoryIds.get(assetVocabularyId);

            assetVocabularyAssetCategoryIds.put(
                assetVocabularyId, assetCategoryIds + StringPool.COMMA + assetCategoryId);
          } else {
            assetVocabularyAssetCategoryIds.put(assetVocabularyId, String.valueOf(assetCategoryId));
          }
        }

        for (Map.Entry<Long, String> entry : assetVocabularyAssetCategoryIds.entrySet()) {

          long assetVocabularyId = entry.getKey();
          String assetCategoryIds = entry.getValue();

          addPortletURL.setParameter("assetCategoryIds_" + assetVocabularyId, assetCategoryIds);
        }
      }

      if (allAssetTagNames != null) {
        addPortletURL.setParameter("assetTagNames", StringUtil.merge(allAssetTagNames));
      }
    }

    addPortletURL.setPortletMode(PortletMode.VIEW);
    addPortletURL.setWindowState(LiferayWindowState.POP_UP);

    return addPortletURL;
  }
 @Override
 public AssetRendererFactory getAssetRendererFactory() {
   return AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassName(getClassName());
 }
  @Override
  public List<AssetEntry> getAssetEntries(
      PortletPreferences preferences,
      Layout layout,
      long scopeGroupId,
      int max,
      boolean checkPermission)
      throws PortalException, SystemException {

    AssetEntryQuery assetEntryQuery = getAssetEntryQuery(preferences, new long[] {scopeGroupId});

    boolean anyAssetType = GetterUtil.getBoolean(preferences.getValue("anyAssetType", null), true);

    if (!anyAssetType) {
      long[] availableClassNameIds =
          AssetRendererFactoryRegistryUtil.getClassNameIds(layout.getCompanyId());

      long[] classNameIds = getClassNameIds(preferences, availableClassNameIds);

      assetEntryQuery.setClassNameIds(classNameIds);
    }

    long[] classTypeIds = GetterUtil.getLongValues(preferences.getValues("classTypeIds", null));

    assetEntryQuery.setClassTypeIds(classTypeIds);

    boolean enablePermissions =
        GetterUtil.getBoolean(preferences.getValue("enablePermissions", null));

    assetEntryQuery.setEnablePermissions(enablePermissions);

    assetEntryQuery.setEnd(max);

    boolean excludeZeroViewCount =
        GetterUtil.getBoolean(preferences.getValue("excludeZeroViewCount", null));

    assetEntryQuery.setExcludeZeroViewCount(excludeZeroViewCount);

    long[] groupIds = getGroupIds(preferences, scopeGroupId, layout);

    assetEntryQuery.setGroupIds(groupIds);

    boolean showOnlyLayoutAssets =
        GetterUtil.getBoolean(preferences.getValue("showOnlyLayoutAssets", null));

    if (showOnlyLayoutAssets) {
      assetEntryQuery.setLayout(layout);
    }

    String orderByColumn1 =
        GetterUtil.getString(preferences.getValue("orderByColumn1", "modifiedDate"));

    assetEntryQuery.setOrderByCol1(orderByColumn1);

    String orderByColumn2 = GetterUtil.getString(preferences.getValue("orderByColumn2", "title"));

    assetEntryQuery.setOrderByCol2(orderByColumn2);

    String orderByType1 = GetterUtil.getString(preferences.getValue("orderByType1", "DESC"));

    assetEntryQuery.setOrderByType1(orderByType1);

    String orderByType2 = GetterUtil.getString(preferences.getValue("orderByType2", "ASC"));

    assetEntryQuery.setOrderByType2(orderByType2);

    assetEntryQuery.setStart(0);

    if (checkPermission) {
      return AssetEntryServiceUtil.getEntries(assetEntryQuery);
    } else {
      return AssetEntryLocalServiceUtil.getEntries(assetEntryQuery);
    }
  }