protected void notifySubscribers(KBArticle kbArticle, ServiceContext serviceContext)
      throws PortalException, SystemException {

    if (Validator.isNull(serviceContext.getLayoutFullURL())) {
      return;
    }

    PortletPreferences preferences = ServiceContextUtil.getPortletPreferences(serviceContext);

    if (preferences == null) {
      long ownerId = kbArticle.getGroupId();
      int ownerType = PortletKeys.PREFS_OWNER_TYPE_GROUP;
      long plid = PortletKeys.PREFS_PLID_SHARED;
      String portletId = PortletKeys.KNOWLEDGE_BASE_ADMIN;
      String defaultPreferences = null;

      preferences =
          portletPreferencesLocalService.getPreferences(
              kbArticle.getCompanyId(), ownerId, ownerType, plid, portletId, defaultPreferences);
    }

    if (serviceContext.isCommandAdd() && !AdminUtil.getEmailKBArticleAddedEnabled(preferences)) {

      return;
    }

    if (serviceContext.isCommandUpdate()
        && !AdminUtil.getEmailKBArticleUpdatedEnabled(preferences)) {

      return;
    }

    String fromName = AdminUtil.getEmailFromName(preferences);
    String fromAddress = AdminUtil.getEmailFromAddress(preferences);

    String kbArticleContent =
        StringUtil.replace(
            kbArticle.getContent(),
            new String[] {"href=\"/", "src=\"/"},
            new String[] {
              "href=\"" + serviceContext.getPortalURL() + "/",
              "src=\"" + serviceContext.getPortalURL() + "/"
            });

    Map<String, String> kbArticleDiffs = getEmailKBArticleDiffs(kbArticle);

    for (String key : kbArticleDiffs.keySet()) {
      String value =
          StringUtil.replace(
              kbArticleDiffs.get(key),
              new String[] {"href=\"/", "src=\"/"},
              new String[] {
                "href=\"" + serviceContext.getPortalURL() + "/",
                "src=\"" + serviceContext.getPortalURL() + "/"
              });

      kbArticleDiffs.put(key, value);
    }

    String subject = null;
    String body = null;

    if (serviceContext.isCommandAdd()) {
      subject = AdminUtil.getEmailKBArticleAddedSubject(preferences);
      body = AdminUtil.getEmailKBArticleUpdatedBody(preferences);
    } else {
      subject = AdminUtil.getEmailKBArticleUpdatedSubject(preferences);
      body = AdminUtil.getEmailKBArticleUpdatedBody(preferences);
    }

    SubscriptionSender subscriptionSender = new AdminSubscriptionSender(kbArticle, serviceContext);

    subscriptionSender.setBody(body);
    subscriptionSender.setCompanyId(kbArticle.getCompanyId());
    subscriptionSender.setContextAttributes(
        "[$ARTICLE_CONTENT$]",
        kbArticleContent,
        "[$ARTICLE_CONTENT_DIFF$]",
        kbArticleDiffs.get("content"),
        "[$ARTICLE_TITLE$]",
        kbArticle.getTitle(),
        "[$ARTICLE_TITLE_DIFF$]",
        kbArticleDiffs.get("title"));
    subscriptionSender.setContextUserPrefix("ARTICLE");
    subscriptionSender.setFrom(fromAddress, fromName);
    subscriptionSender.setHtmlFormat(true);
    subscriptionSender.setMailId("kb_article", kbArticle.getKbArticleId());
    subscriptionSender.setPortletId(serviceContext.getPortletId());
    subscriptionSender.setReplyToAddress(fromAddress);
    subscriptionSender.setScopeGroupId(kbArticle.getGroupId());
    subscriptionSender.setSubject(subject);
    subscriptionSender.setUserId(kbArticle.getUserId());

    subscriptionSender.addPersistedSubscribers(KBArticle.class.getName(), kbArticle.getGroupId());
    subscriptionSender.addPersistedSubscribers(
        KBArticle.class.getName(), kbArticle.getResourcePrimKey());

    while (!kbArticle.isRoot()) {
      kbArticle =
          getLatestKBArticle(
              kbArticle.getParentResourcePrimKey(), WorkflowConstants.STATUS_APPROVED);

      subscriptionSender.addPersistedSubscribers(
          KBArticle.class.getName(), kbArticle.getResourcePrimKey());
    }

    subscriptionSender.flushNotificationsAsync();
  }
Esempio n. 2
0
  public void merge(ServiceContext serviceContext) {
    setAddGroupPermissions(serviceContext.isAddGroupPermissions());
    setAddGuestPermissions(serviceContext.isAddGuestPermissions());

    if (serviceContext.getAssetCategoryIds() != null) {
      setAssetCategoryIds(serviceContext.getAssetCategoryIds());
    }

    if (serviceContext.getAssetLinkEntryIds() != null) {
      setAssetLinkEntryIds(serviceContext.getAssetLinkEntryIds());
    }

    if (serviceContext.getAssetTagNames() != null) {
      setAssetTagNames(serviceContext.getAssetTagNames());
    }

    if (serviceContext.getAttributes() != null) {
      setAttributes(serviceContext.getAttributes());
    }

    if (Validator.isNotNull(serviceContext.getCommand())) {
      setCommand(serviceContext.getCommand());
    }

    if (serviceContext.getCompanyId() > 0) {
      setCompanyId(serviceContext.getCompanyId());
    }

    if (serviceContext.getCreateDate() != null) {
      setCreateDate(serviceContext.getCreateDate());
    }

    if (Validator.isNotNull(serviceContext.getCurrentURL())) {
      setCurrentURL(serviceContext.getCurrentURL());
    }

    if (serviceContext.getExpandoBridgeAttributes() != null) {
      setExpandoBridgeAttributes(serviceContext.getExpandoBridgeAttributes());
    }

    if (serviceContext.getGroupPermissions() != null) {
      setGroupPermissions(serviceContext.getGroupPermissions());
    }

    if (serviceContext.getGuestPermissions() != null) {
      setGuestPermissions(serviceContext.getGuestPermissions());
    }

    if (serviceContext.getHeaders() != null) {
      setHeaders(serviceContext.getHeaders());
    }

    setFailOnPortalException(serviceContext.isFailOnPortalException());
    setLanguageId(serviceContext.getLanguageId());

    if (Validator.isNotNull(serviceContext.getLayoutFullURL())) {
      setLayoutFullURL(serviceContext.getLayoutFullURL());
    }

    if (Validator.isNotNull(serviceContext.getLayoutURL())) {
      setLayoutURL(serviceContext.getLayoutURL());
    }

    if (serviceContext.getModifiedDate() != null) {
      setModifiedDate(serviceContext.getModifiedDate());
    }

    if (Validator.isNotNull(serviceContext.getPathMain())) {
      setPathMain(serviceContext.getPathMain());
    }

    if (serviceContext.getPlid() > 0) {
      setPlid(serviceContext.getPlid());
    }

    if (Validator.isNotNull(serviceContext.getPortalURL())) {
      setPortalURL(serviceContext.getPortalURL());
    }

    if (serviceContext.getPortletPreferencesIds() != null) {
      setPortletPreferencesIds(serviceContext.getPortletPreferencesIds());
    }

    if (Validator.isNotNull(serviceContext.getRemoteAddr())) {
      setRemoteAddr(serviceContext.getRemoteAddr());
    }

    if (Validator.isNotNull(serviceContext.getRemoteHost())) {
      setRemoteHost(serviceContext.getRemoteHost());
    }

    if (serviceContext.getScopeGroupId() > 0) {
      setScopeGroupId(serviceContext.getScopeGroupId());
    }

    setSignedIn(serviceContext.isSignedIn());

    if (Validator.isNotNull(serviceContext.getUserDisplayURL())) {
      setUserDisplayURL(serviceContext.getUserDisplayURL());
    }

    if (serviceContext.getUserId() > 0) {
      setUserId(serviceContext.getUserId());
    }

    if (Validator.isNotNull(serviceContext.getUuid())) {
      setUuid(serviceContext.getUuid());
    }

    if (serviceContext.getWorkflowAction() > 0) {
      setWorkflowAction(serviceContext.getWorkflowAction());
    }
  }