public List<KBArticle> getSectionsKBArticles( long groupId, String[] sections, int status, int start, int end, OrderByComparator orderByComparator) throws SystemException { String[] array = AdminUtil.escapeSections(sections); for (int i = 0; i < array.length; i++) { array[i] = StringUtil.quote(array[i], StringPool.PERCENT); } if (status == WorkflowConstants.STATUS_ANY) { return kbArticlePersistence.findByG_P_S_L( groupId, KBArticleConstants.DEFAULT_PARENT_RESOURCE_PRIM_KEY, array, true, start, end, orderByComparator); } else if (status == WorkflowConstants.STATUS_APPROVED) { return kbArticlePersistence.findByG_P_S_M( groupId, KBArticleConstants.DEFAULT_PARENT_RESOURCE_PRIM_KEY, array, true, start, end, orderByComparator); } return kbArticlePersistence.findByG_P_S_S( groupId, KBArticleConstants.DEFAULT_PARENT_RESOURCE_PRIM_KEY, array, status, start, end, orderByComparator); }
public int getSectionsKBArticlesCount(long groupId, String[] sections, int status) throws SystemException { String[] array = AdminUtil.escapeSections(sections); for (int i = 0; i < array.length; i++) { array[i] = StringUtil.quote(array[i], StringPool.PERCENT); } if (status == WorkflowConstants.STATUS_ANY) { return kbArticlePersistence.countByG_P_S_L( groupId, KBArticleConstants.DEFAULT_PARENT_RESOURCE_PRIM_KEY, array, true); } else if (status == WorkflowConstants.STATUS_APPROVED) { return kbArticlePersistence.countByG_P_S_M( groupId, KBArticleConstants.DEFAULT_PARENT_RESOURCE_PRIM_KEY, array, true); } return kbArticlePersistence.countByG_P_S_S( groupId, KBArticleConstants.DEFAULT_PARENT_RESOURCE_PRIM_KEY, array, status); }
protected Map<String, String> getEmailKBArticleDiffs(KBArticle kbArticle) { Map<String, String> emailKBArticleDiffs = new HashMap<String, String>(); for (String param : new String[] {"content", "title"}) { String value = BeanPropertiesUtil.getString(kbArticle, param); try { value = AdminUtil.getKBArticleDiff( kbArticle.getResourcePrimKey(), kbArticle.getVersion() - 1, kbArticle.getVersion(), param); } catch (Exception e) { _log.error(e, e); } emailKBArticleDiffs.put(param, value); } return emailKBArticleDiffs; }
public KBArticle updateKBArticle( long userId, long resourcePrimKey, String title, String content, String description, String[] sections, String dirName, ServiceContext serviceContext) throws PortalException, SystemException { // KB article User user = userPersistence.findByPrimaryKey(userId); int version = KBArticleConstants.DEFAULT_VERSION; int status = WorkflowConstants.STATUS_DRAFT; validate(title, content); KBArticle oldKBArticle = getLatestKBArticle(resourcePrimKey, WorkflowConstants.STATUS_ANY); long oldResourcePrimKey = oldKBArticle.getResourcePrimKey(); long oldGroupId = oldKBArticle.getGroupId(); Date oldCreateDate = oldKBArticle.getCreateDate(); long oldRootResourcePrimKey = oldKBArticle.getRootResourcePrimKey(); long oldParentResourcePrimKey = oldKBArticle.getParentResourcePrimKey(); int oldVersion = oldKBArticle.getVersion(); double oldPriority = oldKBArticle.getPriority(); int oldViewCount = oldKBArticle.getViewCount(); int oldStatus = oldKBArticle.getStatus(); KBArticle kbArticle = null; if (oldStatus == WorkflowConstants.STATUS_APPROVED) { long kbArticleId = counterLocalService.increment(); kbArticle = kbArticlePersistence.create(kbArticleId); version = oldVersion + 1; } else { kbArticle = oldKBArticle; version = oldVersion; } if (oldStatus == WorkflowConstants.STATUS_PENDING) { status = WorkflowConstants.STATUS_PENDING; } kbArticle.setResourcePrimKey(oldResourcePrimKey); kbArticle.setGroupId(oldGroupId); kbArticle.setCompanyId(user.getCompanyId()); kbArticle.setUserId(user.getUserId()); kbArticle.setUserName(user.getFullName()); kbArticle.setCreateDate(oldCreateDate); kbArticle.setModifiedDate(serviceContext.getModifiedDate(null)); kbArticle.setRootResourcePrimKey(oldRootResourcePrimKey); kbArticle.setParentResourcePrimKey(oldParentResourcePrimKey); kbArticle.setVersion(version); kbArticle.setTitle(title); kbArticle.setContent(content); kbArticle.setDescription(description); kbArticle.setPriority(oldPriority); kbArticle.setSections(StringUtil.merge(AdminUtil.escapeSections(sections))); kbArticle.setViewCount(oldViewCount); kbArticle.setLatest(true); kbArticle.setMain(false); kbArticle.setStatus(status); kbArticlePersistence.update(kbArticle, false); if (oldVersion < version) { oldKBArticle.setLatest(false); kbArticlePersistence.update(oldKBArticle, false); } // Resources if ((serviceContext.getGroupPermissions() != null) || (serviceContext.getGuestPermissions() != null)) { updateKBArticleResources( kbArticle, serviceContext.getGroupPermissions(), serviceContext.getGuestPermissions()); } // Asset updateKBArticleAsset( userId, kbArticle, serviceContext.getAssetCategoryIds(), serviceContext.getAssetTagNames()); // Attachments updateKBArticleAttachments(kbArticle, oldVersion, dirName, serviceContext); // Workflow WorkflowHandlerRegistryUtil.startWorkflowInstance( user.getCompanyId(), kbArticle.getGroupId(), userId, KBArticle.class.getName(), resourcePrimKey, kbArticle, serviceContext); return kbArticle; }
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(); }
public KBArticle addKBArticle( long userId, long parentResourcePrimKey, String title, String content, String description, String[] sections, String dirName, ServiceContext serviceContext) throws PortalException, SystemException { // KB article User user = userPersistence.findByPrimaryKey(userId); long groupId = serviceContext.getScopeGroupId(); double priority = getPriority(groupId, parentResourcePrimKey); Date now = new Date(); validate(title, content); long kbArticleId = counterLocalService.increment(); long resourcePrimKey = counterLocalService.increment(); long rootResourcePrimKey = getRootResourcePrimKey(resourcePrimKey, parentResourcePrimKey); KBArticle kbArticle = kbArticlePersistence.create(kbArticleId); kbArticle.setUuid(serviceContext.getUuid()); kbArticle.setResourcePrimKey(resourcePrimKey); kbArticle.setGroupId(groupId); kbArticle.setCompanyId(user.getCompanyId()); kbArticle.setUserId(user.getUserId()); kbArticle.setUserName(user.getFullName()); kbArticle.setCreateDate(serviceContext.getCreateDate(now)); kbArticle.setModifiedDate(serviceContext.getModifiedDate(now)); kbArticle.setRootResourcePrimKey(rootResourcePrimKey); kbArticle.setParentResourcePrimKey(parentResourcePrimKey); kbArticle.setVersion(KBArticleConstants.DEFAULT_VERSION); kbArticle.setTitle(title); kbArticle.setContent(content); kbArticle.setDescription(description); kbArticle.setPriority(priority); kbArticle.setSections(StringUtil.merge(AdminUtil.escapeSections(sections))); kbArticle.setViewCount(0); kbArticle.setLatest(true); kbArticle.setMain(false); kbArticle.setStatus(WorkflowConstants.STATUS_DRAFT); kbArticlePersistence.update(kbArticle, false); // Resources resourceLocalService.addModelResources(kbArticle, serviceContext); // Asset updateKBArticleAsset( userId, kbArticle, serviceContext.getAssetCategoryIds(), serviceContext.getAssetTagNames()); // Attachments addKBArticleAttachments(kbArticle, dirName, serviceContext); // Workflow WorkflowHandlerRegistryUtil.startWorkflowInstance( user.getCompanyId(), groupId, userId, KBArticle.class.getName(), resourcePrimKey, kbArticle, serviceContext); return kbArticle; }
protected PortletURL getKBArticleURL( long plid, boolean privateLayout, KBArticle kbArticle, HttpServletRequest request) throws Exception { PortletURL firstMatchPortletURL = null; List<Layout> layouts = getCandidateLayouts(plid, privateLayout, kbArticle); for (Layout layout : layouts) { LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout.getLayoutType(); List<Portlet> portlets = layoutTypePortlet.getAllPortlets(); for (Portlet portlet : portlets) { String rootPortletId = PortletConstants.getRootPortletId(portlet.getPortletId()); if (rootPortletId.equals(PortletKeys.KNOWLEDGE_BASE_DISPLAY)) { PortletPreferences portletPreferences = PortletPreferencesFactoryUtil.getPortletSetup( layout, portlet.getPortletId(), StringPool.BLANK); long kbFolderClassNameId = PortalUtil.getClassNameId(KBFolderConstants.getClassName()); long resourceClassNameId = GetterUtil.getLong( portletPreferences.getValue("resourceClassNameId", null), kbFolderClassNameId); long resourcePrimKey = GetterUtil.getLong( portletPreferences.getValue("resourcePrimKey", null), KBFolderConstants.DEFAULT_PARENT_FOLDER_ID); if (resourceClassNameId == kbFolderClassNameId) { if (isParentFolder(resourcePrimKey, kbArticle.getKbFolderId())) { return getKBArticleURL(layout.getPlid(), portlet.getPortletId(), kbArticle, request); } } else if (resourcePrimKey == kbArticle.getResourcePrimKey()) { return getKBArticleURL(layout.getPlid(), portlet.getPortletId(), kbArticle, request); } if (firstMatchPortletURL == null) { firstMatchPortletURL = getKBArticleURL(layout.getPlid(), portlet.getPortletId(), kbArticle, request); } } if (rootPortletId.equals(PortletKeys.KNOWLEDGE_BASE_SECTION)) { PortletPreferences portletPreferences = PortletPreferencesFactoryUtil.getPortletSetup( layout, portlet.getPortletId(), StringPool.BLANK); String[] kbArticlesSections = portletPreferences.getValues("kbArticlesSections", new String[0]); KBArticle rootKBArticle = KBArticleLocalServiceUtil.fetchLatestKBArticle( kbArticle.getRootResourcePrimKey(), WorkflowConstants.STATUS_APPROVED); if (rootKBArticle == null) { continue; } String[] sections = AdminUtil.unescapeSections(rootKBArticle.getSections()); for (String section : sections) { if (!ArrayUtil.contains(kbArticlesSections, section)) { continue; } return getKBArticleURL(layout.getPlid(), portlet.getPortletId(), kbArticle, request); } } if (rootPortletId.equals(PortletKeys.KNOWLEDGE_BASE_ARTICLE)) { PortletPreferences portletPreferences = PortletPreferencesFactoryUtil.getPortletSetup( layout, portlet.getPortletId(), StringPool.BLANK); long resourcePrimKey = GetterUtil.getLong(portletPreferences.getValue("resourcePrimKey", null)); KBArticle selKBArticle = KBArticleLocalServiceUtil.fetchLatestKBArticle( resourcePrimKey, WorkflowConstants.STATUS_APPROVED); if (selKBArticle == null) { continue; } long rootResourcePrimKey = kbArticle.getRootResourcePrimKey(); long selRootResourcePrimKey = selKBArticle.getRootResourcePrimKey(); if (rootResourcePrimKey == selRootResourcePrimKey) { return getKBArticleURL(layout.getPlid(), portlet.getPortletId(), kbArticle, request); } if (firstMatchPortletURL == null) { firstMatchPortletURL = getKBArticleURL(layout.getPlid(), portlet.getPortletId(), kbArticle, request); } } } } return firstMatchPortletURL; }
protected void notifySubscribers(KBComment kbComment, ServiceContext serviceContext) throws PortalException { PortletPreferences preferences = portletPreferencesLocalService.getPreferences( kbComment.getCompanyId(), kbComment.getGroupId(), PortletKeys.PREFS_OWNER_TYPE_GROUP, PortletKeys.PREFS_PLID_SHARED, PortletKeys.KNOWLEDGE_BASE_ADMIN, null); if (!AdminUtil.isSuggestionStatusChangeNotificationEnabled( kbComment.getStatus(), preferences)) { return; } String fromName = AdminUtil.getEmailFromName(preferences, serviceContext.getCompanyId()); String fromAddress = AdminUtil.getEmailFromAddress(preferences, kbComment.getCompanyId()); String subject = AdminUtil.getEmailKBArticleSuggestionNotificationSubject( kbComment.getStatus(), preferences); String body = AdminUtil.getEmailKBArticleSuggestionNotificationBody(kbComment.getStatus(), preferences); KBArticle kbArticle = kbArticleLocalService.getLatestKBArticle( kbComment.getClassPK(), WorkflowConstants.STATUS_APPROVED); String kbArticleContent = StringUtil.replace( kbArticle.getContent(), new String[] {"href=\"/", "src=\"/"}, new String[] { "href=\"" + serviceContext.getPortalURL() + "/", "src=\"" + serviceContext.getPortalURL() + "/" }); SubscriptionSender subscriptionSender = new AdminSubscriptionSender(kbArticle, serviceContext); subscriptionSender.setBody(body); subscriptionSender.setCompanyId(kbArticle.getCompanyId()); subscriptionSender.setContextAttribute("[$ARTICLE_CONTENT$]", kbArticleContent, false); subscriptionSender.setContextAttribute("[$ARTICLE_TITLE$]", kbArticle.getTitle(), false); subscriptionSender.setContextAttribute("[$COMMENT_CONTENT$]", kbComment.getContent(), false); 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()); User user = userLocalService.getUser(kbComment.getUserId()); subscriptionSender.addRuntimeSubscribers(user.getEmailAddress(), user.getFullName()); subscriptionSender.flushNotificationsAsync(); }
@Override protected void doImportStagedModel(PortletDataContext portletDataContext, KBArticle kbArticle) throws Exception { long userId = portletDataContext.getUserId(kbArticle.getUserUuid()); if (kbArticle.getParentResourcePrimKey() != KBFolderConstants.DEFAULT_PARENT_FOLDER_ID) { if (kbArticle.getClassNameId() == kbArticle.getParentResourceClassNameId()) { StagedModelDataHandlerUtil.importReferenceStagedModels( portletDataContext, kbArticle, KBArticle.class); } else { StagedModelDataHandlerUtil.importReferenceStagedModels( portletDataContext, kbArticle, KBFolder.class); } } Map<Long, Long> kbArticleResourcePrimKeys = (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(KBArticle.class); long parentResourcePrimKey = MapUtil.getLong( kbArticleResourcePrimKeys, kbArticle.getParentResourcePrimKey(), KBFolderConstants.DEFAULT_PARENT_FOLDER_ID); long resourcePrimaryKey = MapUtil.getLong(kbArticleResourcePrimKeys, kbArticle.getResourcePrimKey(), 0); if (parentResourcePrimKey == KBFolderConstants.DEFAULT_PARENT_FOLDER_ID) { Map<Long, Long> kbFolderResourcePrimKeys = (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(KBFolder.class); parentResourcePrimKey = MapUtil.getLong( kbFolderResourcePrimKeys, kbArticle.getParentResourcePrimKey(), KBFolderConstants.DEFAULT_PARENT_FOLDER_ID); } String[] sections = AdminUtil.unescapeSections(kbArticle.getSections()); ServiceContext serviceContext = portletDataContext.createServiceContext(kbArticle); KBArticle importedKBArticle = null; if (portletDataContext.isDataStrategyMirror()) { KBArticle existingKBArticle = KBArticleUtil.fetchByR_V(resourcePrimaryKey, kbArticle.getVersion()); if (existingKBArticle == null) { existingKBArticle = fetchStagedModelByUuidAndGroupId( kbArticle.getUuid(), portletDataContext.getScopeGroupId()); } if (existingKBArticle == null) { serviceContext.setUuid(kbArticle.getUuid()); existingKBArticle = KBArticleLocalServiceUtil.fetchLatestKBArticle( resourcePrimaryKey, WorkflowConstants.STATUS_ANY); if (existingKBArticle == null) { importedKBArticle = KBArticleLocalServiceUtil.addKBArticle( userId, kbArticle.getParentResourceClassNameId(), parentResourcePrimKey, kbArticle.getTitle(), kbArticle.getUrlTitle(), kbArticle.getContent(), kbArticle.getDescription(), kbArticle.getSourceURL(), sections, null, serviceContext); KBArticleLocalServiceUtil.updatePriority( importedKBArticle.getResourcePrimKey(), kbArticle.getPriority()); } else { KBArticleLocalServiceUtil.updateKBArticle( userId, existingKBArticle.getResourcePrimKey(), kbArticle.getTitle(), kbArticle.getContent(), kbArticle.getDescription(), kbArticle.getSourceURL(), sections, null, null, serviceContext); KBArticleLocalServiceUtil.moveKBArticle( userId, existingKBArticle.getResourcePrimKey(), existingKBArticle.getParentResourceClassNameId(), parentResourcePrimKey, kbArticle.getPriority()); importedKBArticle = KBArticleLocalServiceUtil.getLatestKBArticle( existingKBArticle.getResourcePrimKey(), WorkflowConstants.STATUS_APPROVED); } } else { importedKBArticle = existingKBArticle; } } else { importedKBArticle = KBArticleLocalServiceUtil.addKBArticle( userId, kbArticle.getParentResourceClassNameId(), parentResourcePrimKey, kbArticle.getTitle(), kbArticle.getUrlTitle(), kbArticle.getContent(), kbArticle.getDescription(), kbArticle.getSourceURL(), sections, null, serviceContext); KBArticleLocalServiceUtil.updatePriority( importedKBArticle.getResourcePrimKey(), kbArticle.getPriority()); } importKBArticleAttachments(portletDataContext, kbArticle, importedKBArticle); portletDataContext.importClassedModel(kbArticle, importedKBArticle); if (!kbArticle.isMain()) { kbArticleResourcePrimKeys.put( kbArticle.getResourcePrimKey(), importedKBArticle.getResourcePrimKey()); } }