Example usage for com.liferay.portal.kernel.workflow WorkflowConstants STATUS_DRAFT

List of usage examples for com.liferay.portal.kernel.workflow WorkflowConstants STATUS_DRAFT

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.workflow WorkflowConstants STATUS_DRAFT.

Prototype

int STATUS_DRAFT

To view the source code for com.liferay.portal.kernel.workflow WorkflowConstants STATUS_DRAFT.

Click Source Link

Usage

From source file:com.liferay.journal.service.impl.JournalArticleLocalServiceImpl.java

License:Open Source License

/**
 * Adds a web content article with additional parameters.
 *
 * <p>/*from ww w  . j  av a  2  s . com*/
 * The web content articles hold HTML content wrapped in XML. The XML lets
 * you specify the article's default locale and available locales. Here is a
 * content example:
 * </p>
 *
 * <p>
 * <pre>
 * <code>
 * &lt;?xml version='1.0' encoding='UTF-8'?&gt;
 * &lt;root default-locale="en_US" available-locales="en_US"&gt;
 *    &lt;static-content language-id="en_US"&gt;
 *       &lt;![CDATA[&lt;p&gt;&lt;b&gt;&lt;i&gt;test&lt;i&gt; content&lt;b&gt;&lt;/p&gt;]]&gt;
 *    &lt;/static-content&gt;
 * &lt;/root&gt;
 * </code>
 * </pre>
 * </p>
 *
 * @param  userId the primary key of the web content article's creator/owner
 * @param  groupId the primary key of the web content article's group
 * @param  folderId the primary key of the web content article folder
 * @param  classNameId the primary key of the DDMStructure class if the web
 *         content article is related to a DDM structure, the primary key of
 *         the class name associated with the article, or
 *         JournalArticleConstants.CLASSNAME_ID_DEFAULT in the journal-api
 *         module otherwise
 * @param  classPK the primary key of the DDM structure, if the primary key
 *         of the DDMStructure class is given as the
 *         <code>classNameId</code> parameter, the primary key of the class
 *         associated with the web content article, or <code>0</code>
 *         otherwise
 * @param  articleId the primary key of the web content article
 * @param  autoArticleId whether to auto generate the web content article ID
 * @param  version the web content article's version
 * @param  titleMap the web content article's locales and localized titles
 * @param  descriptionMap the web content article's locales and localized
 *         descriptions
 * @param  content the HTML content wrapped in XML
 * @param  ddmStructureKey the primary key of the web content article's DDM
 *         structure, if the article is related to a DDM structure, or
 *         <code>null</code> otherwise
 * @param  ddmTemplateKey the primary key of the web content article's DDM
 *         template
 * @param  layoutUuid the unique string identifying the web content
 *         article's display page
 * @param  displayDateMonth the month the web content article is set to
 *         display
 * @param  displayDateDay the calendar day the web content article is set to
 *         display
 * @param  displayDateYear the year the web content article is set to
 *         display
 * @param  displayDateHour the hour the web content article is set to
 *         display
 * @param  displayDateMinute the minute the web content article is set to
 *         display
 * @param  expirationDateMonth the month the web content article is set to
 *         expire
 * @param  expirationDateDay the calendar day the web content article is set
 *         to expire
 * @param  expirationDateYear the year the web content article is set to
 *         expire
 * @param  expirationDateHour the hour the web content article is set to
 *         expire
 * @param  expirationDateMinute the minute the web content article is set to
 *         expire
 * @param  neverExpire whether the web content article is not set to auto
 *         expire
 * @param  reviewDateMonth the month the web content article is set for
 *         review
 * @param  reviewDateDay the calendar day the web content article is set for
 *         review
 * @param  reviewDateYear the year the web content article is set for review
 * @param  reviewDateHour the hour the web content article is set for review
 * @param  reviewDateMinute the minute the web content article is set for
 *         review
 * @param  neverReview whether the web content article is not set for review
 * @param  indexable whether the web content article is searchable
 * @param  smallImage whether the web content article has a small image
 * @param  smallImageURL the web content article's small image URL
 * @param  smallImageFile the web content article's small image file
 * @param  images the web content's images
 * @param  articleURL the web content article's accessible URL
 * @param  serviceContext the service context to be applied. Can set the
 *         UUID, creation date, modification date, expando bridge
 *         attributes, guest permissions, group permissions, asset category
 *         IDs, asset tag names, asset link entry IDs, URL title, and
 *         workflow actions for the web content article. Can also set
 *         whether to add the default guest and group permissions.
 * @return the web content article
 */
@Indexable(type = IndexableType.REINDEX)
@Override
public JournalArticle addArticle(long userId, long groupId, long folderId, long classNameId, long classPK,
        String articleId, boolean autoArticleId, double version, Map<Locale, String> titleMap,
        Map<Locale, String> descriptionMap, String content, String ddmStructureKey, String ddmTemplateKey,
        String layoutUuid, int displayDateMonth, int displayDateDay, int displayDateYear, int displayDateHour,
        int displayDateMinute, int expirationDateMonth, int expirationDateDay, int expirationDateYear,
        int expirationDateHour, int expirationDateMinute, boolean neverExpire, int reviewDateMonth,
        int reviewDateDay, int reviewDateYear, int reviewDateHour, int reviewDateMinute, boolean neverReview,
        boolean indexable, boolean smallImage, String smallImageURL, File smallImageFile,
        Map<String, byte[]> images, String articleURL, ServiceContext serviceContext) throws PortalException {

    // Article

    User user = userLocalService.getUser(userId);
    articleId = StringUtil.toUpperCase(StringUtil.trim(articleId));

    Date displayDate = null;
    Date expirationDate = null;
    Date reviewDate = null;

    if (classNameId == JournalArticleConstants.CLASSNAME_ID_DEFAULT) {
        displayDate = PortalUtil.getDate(displayDateMonth, displayDateDay, displayDateYear, displayDateHour,
                displayDateMinute, user.getTimeZone(), null);

        if (!neverExpire) {
            expirationDate = PortalUtil.getDate(expirationDateMonth, expirationDateDay, expirationDateYear,
                    expirationDateHour, expirationDateMinute, user.getTimeZone(),
                    ArticleExpirationDateException.class);
        }

        if (!neverReview) {
            reviewDate = PortalUtil.getDate(reviewDateMonth, reviewDateDay, reviewDateYear, reviewDateHour,
                    reviewDateMinute, user.getTimeZone(), ArticleReviewDateException.class);
        }
    }

    byte[] smallImageBytes = null;

    try {
        smallImageBytes = FileUtil.getBytes(smallImageFile);
    } catch (IOException ioe) {
    }

    Date now = new Date();

    validateDDMStructureId(groupId, folderId, ddmStructureKey);

    if (autoArticleId) {
        articleId = String.valueOf(counterLocalService.increment());
    }

    validate(user.getCompanyId(), groupId, classNameId, articleId, autoArticleId, version, titleMap, content,
            ddmStructureKey, ddmTemplateKey, displayDate, expirationDate, smallImage, smallImageURL,
            smallImageFile, smallImageBytes, serviceContext);

    validateReferences(groupId, ddmStructureKey, ddmTemplateKey, layoutUuid, smallImage, smallImageURL,
            smallImageBytes, 0, content);

    serviceContext.setAttribute("articleId", articleId);

    long id = counterLocalService.increment();

    String articleResourceUuid = GetterUtil.getString(serviceContext.getAttribute("articleResourceUuid"));

    long resourcePrimKey = journalArticleResourceLocalService.getArticleResourcePrimKey(articleResourceUuid,
            groupId, articleId);

    JournalArticle article = journalArticlePersistence.create(id);

    Locale locale = getArticleDefaultLocale(content);

    String title = titleMap.get(locale);

    article.setUuid(serviceContext.getUuid());
    article.setResourcePrimKey(resourcePrimKey);
    article.setGroupId(groupId);
    article.setCompanyId(user.getCompanyId());
    article.setUserId(user.getUserId());
    article.setUserName(user.getFullName());
    article.setFolderId(folderId);
    article.setClassNameId(classNameId);
    article.setClassPK(classPK);
    article.setTreePath(article.buildTreePath());
    article.setArticleId(articleId);
    article.setVersion(version);
    article.setUrlTitle(getUniqueUrlTitle(id, groupId, articleId, title, null, serviceContext));

    content = format(user, groupId, article, content);

    article.setContent(content);

    article.setDDMStructureKey(ddmStructureKey);
    article.setDDMTemplateKey(ddmTemplateKey);
    article.setDefaultLanguageId(LocaleUtil.toLanguageId(locale));
    article.setLayoutUuid(layoutUuid);
    article.setDisplayDate(displayDate);
    article.setExpirationDate(expirationDate);
    article.setReviewDate(reviewDate);
    article.setIndexable(indexable);
    article.setSmallImage(smallImage);
    article.setSmallImageId(counterLocalService.increment());
    article.setSmallImageURL(smallImageURL);

    if ((expirationDate == null) || expirationDate.after(now)) {
        article.setStatus(WorkflowConstants.STATUS_DRAFT);
    } else {
        article.setStatus(WorkflowConstants.STATUS_EXPIRED);
    }

    article.setStatusByUserId(userId);
    article.setStatusDate(serviceContext.getModifiedDate(now));
    article.setExpandoBridgeAttributes(serviceContext);

    journalArticlePersistence.update(article);

    // Article localization

    _addArticleLocalizedFields(user.getCompanyId(), article.getId(), titleMap, descriptionMap);

    // Resources

    if (serviceContext.isAddGroupPermissions() || serviceContext.isAddGuestPermissions()) {

        addArticleResources(article, serviceContext.isAddGroupPermissions(),
                serviceContext.isAddGuestPermissions());
    } else {
        addArticleResources(article, serviceContext.getGroupPermissions(),
                serviceContext.getGuestPermissions());
    }

    // Small image

    saveImages(smallImage, article.getSmallImageId(), smallImageFile, smallImageBytes);

    // Asset

    updateAsset(userId, article, serviceContext.getAssetCategoryIds(), serviceContext.getAssetTagNames(),
            serviceContext.getAssetLinkEntryIds(), serviceContext.getAssetPriority());

    // Dynamic data mapping

    if (classNameLocalService.getClassNameId(DDMStructure.class) == classNameId) {

        updateDDMStructurePredefinedValues(classPK, content, serviceContext);
    } else {
        updateDDMLinks(id, groupId, ddmStructureKey, ddmTemplateKey, true);
    }

    // Email

    PortletPreferences preferences = ServiceContextUtil.getPortletPreferences(serviceContext);

    articleURL = buildArticleURL(articleURL, groupId, folderId, articleId);

    serviceContext.setAttribute("articleURL", articleURL);

    sendEmail(article, articleURL, preferences, "requested", serviceContext);

    // Workflow

    if (classNameId == JournalArticleConstants.CLASSNAME_ID_DEFAULT) {
        startWorkflowInstance(userId, article, serviceContext);
    } else {
        updateStatus(userId, article, WorkflowConstants.STATUS_APPROVED, null, serviceContext,
                new HashMap<String, Serializable>());
    }

    return journalArticlePersistence.findByPrimaryKey(article.getId());
}

From source file:com.liferay.journal.service.impl.JournalArticleLocalServiceImpl.java

License:Open Source License

/**
 * Copies the web content article matching the group, article ID, and
 * version. This method creates a new article, extracting all the values
 * from the old one and updating its article ID.
 *
 * @param  userId the primary key of the web content article's creator/owner
 * @param  groupId the primary key of the web content article's group
 * @param  oldArticleId the primary key of the old web content article
 * @param  newArticleId the primary key of the new web content article
 * @param  autoArticleId whether to auto-generate the web content article ID
 * @param  version the web content article's version
 * @return the new web content article/*from w w  w  .j  a  v  a 2s  .  com*/
 */
@Indexable(type = IndexableType.REINDEX)
@Override
public JournalArticle copyArticle(long userId, long groupId, String oldArticleId, String newArticleId,
        boolean autoArticleId, double version) throws PortalException {

    // Article

    User user = userLocalService.getUser(userId);
    oldArticleId = StringUtil.toUpperCase(StringUtil.trim(oldArticleId));
    newArticleId = StringUtil.toUpperCase(StringUtil.trim(newArticleId));

    JournalArticle oldArticle = journalArticlePersistence.findByG_A_V(groupId, oldArticleId, version);

    if (autoArticleId) {
        newArticleId = String.valueOf(counterLocalService.increment());
    } else {
        validate(newArticleId);

        if (journalArticlePersistence.countByG_A(groupId, newArticleId) > 0) {

            StringBundler sb = new StringBundler(5);

            sb.append("{groupId=");
            sb.append(groupId);
            sb.append(", articleId=");
            sb.append(newArticleId);
            sb.append("}");

            throw new DuplicateArticleIdException(sb.toString());
        }
    }

    long id = counterLocalService.increment();

    long resourcePrimKey = journalArticleResourceLocalService.getArticleResourcePrimKey(groupId, newArticleId);

    JournalArticle newArticle = journalArticlePersistence.create(id);

    newArticle.setResourcePrimKey(resourcePrimKey);
    newArticle.setGroupId(groupId);
    newArticle.setCompanyId(user.getCompanyId());
    newArticle.setUserId(user.getUserId());
    newArticle.setUserName(user.getFullName());
    newArticle.setFolderId(oldArticle.getFolderId());
    newArticle.setTreePath(oldArticle.getTreePath());
    newArticle.setArticleId(newArticleId);
    newArticle.setVersion(JournalArticleConstants.VERSION_DEFAULT);
    newArticle.setUrlTitle(getUniqueUrlTitle(id, groupId, newArticleId, oldArticle.getTitleCurrentValue()));

    try {
        copyArticleImages(oldArticle, newArticle);
    } catch (Exception e) {
        newArticle.setContent(oldArticle.getContent());
    }

    newArticle.setDDMStructureKey(oldArticle.getDDMStructureKey());
    newArticle.setDDMTemplateKey(oldArticle.getDDMTemplateKey());
    newArticle.setDefaultLanguageId(oldArticle.getDefaultLanguageId());
    newArticle.setLayoutUuid(oldArticle.getLayoutUuid());
    newArticle.setDisplayDate(oldArticle.getDisplayDate());
    newArticle.setExpirationDate(oldArticle.getExpirationDate());
    newArticle.setReviewDate(oldArticle.getReviewDate());
    newArticle.setIndexable(oldArticle.isIndexable());
    newArticle.setSmallImage(oldArticle.isSmallImage());
    newArticle.setSmallImageId(counterLocalService.increment());
    newArticle.setSmallImageURL(oldArticle.getSmallImageURL());

    WorkflowHandler workflowHandler = WorkflowHandlerRegistryUtil
            .getWorkflowHandler(JournalArticle.class.getName());

    WorkflowDefinitionLink workflowDefinitionLink = workflowHandler
            .getWorkflowDefinitionLink(oldArticle.getCompanyId(), oldArticle.getGroupId(), oldArticle.getId());

    if (oldArticle.isPending() || (workflowDefinitionLink != null)) {
        newArticle.setStatus(WorkflowConstants.STATUS_DRAFT);
    } else {
        newArticle.setStatus(oldArticle.getStatus());
    }

    ExpandoBridgeUtil.copyExpandoBridgeAttributes(oldArticle.getExpandoBridge(), newArticle.getExpandoBridge());

    journalArticlePersistence.update(newArticle);

    // Article localization

    String urlTitle = JournalUtil.getUrlTitle(id, oldArticle.getUrlTitle());

    int uniqueUrlTitleCount = _getUniqueUrlTitleCount(groupId, newArticleId, urlTitle);

    Map<Locale, String> newTitleMap = oldArticle.getTitleMap();

    for (Locale locale : newTitleMap.keySet()) {
        StringBundler sb = new StringBundler(5);

        sb.append(newTitleMap.get(locale));
        sb.append(StringPool.SPACE);
        sb.append(LanguageUtil.get(locale, "duplicate"));
        sb.append(StringPool.SPACE);
        sb.append(uniqueUrlTitleCount);

        newTitleMap.put(locale, sb.toString());
    }

    _addArticleLocalizedFields(newArticle.getCompanyId(), newArticle.getId(), newTitleMap,
            oldArticle.getDescriptionMap());

    // Resources

    addArticleResources(newArticle, true, true);

    // Small image

    if (oldArticle.isSmallImage()) {
        Image image = imageLocalService.fetchImage(oldArticle.getSmallImageId());

        if (image != null) {
            byte[] smallImageBytes = image.getTextObj();

            imageLocalService.updateImage(newArticle.getSmallImageId(), smallImageBytes);
        }
    }

    // Asset

    long[] assetCategoryIds = assetCategoryLocalService.getCategoryIds(JournalArticle.class.getName(),
            oldArticle.getResourcePrimKey());
    String[] assetTagNames = assetTagLocalService.getTagNames(JournalArticle.class.getName(),
            oldArticle.getResourcePrimKey());

    AssetEntry oldAssetEntry = assetEntryLocalService.getEntry(JournalArticle.class.getName(),
            oldArticle.getResourcePrimKey());

    List<AssetLink> assetLinks = assetLinkLocalService.getDirectLinks(oldAssetEntry.getEntryId(), false);

    long[] assetLinkEntryIds = ListUtil.toLongArray(assetLinks, AssetLink.ENTRY_ID2_ACCESSOR);

    updateAsset(userId, newArticle, assetCategoryIds, assetTagNames, assetLinkEntryIds,
            oldAssetEntry.getPriority());

    // Dynamic data mapping

    updateDDMLinks(id, groupId, oldArticle.getDDMStructureKey(), oldArticle.getDDMTemplateKey(), true);

    return newArticle;
}

From source file:com.liferay.journal.service.impl.JournalArticleLocalServiceImpl.java

License:Open Source License

/**
 * Moves the latest version of the web content article matching the group
 * and article ID to the recycle bin.//w  ww .j a v a2 s  .  co m
 *
 * @param  userId the primary key of the user updating the web content
 *         article
 * @param  article the web content article
 * @return the updated web content article, which was moved to the Recycle
 *         Bin
 */
@Indexable(type = IndexableType.REINDEX)
@Override
public JournalArticle moveArticleToTrash(long userId, JournalArticle article) throws PortalException {

    // Article

    if (article.isInTrash()) {
        throw new TrashEntryException();
    }

    int oldStatus = article.getStatus();

    if (oldStatus == WorkflowConstants.STATUS_PENDING) {
        article.setStatus(WorkflowConstants.STATUS_DRAFT);
    }

    journalArticlePersistence.update(article);

    List<JournalArticle> articleVersions = journalArticlePersistence.findByG_A(article.getGroupId(),
            article.getArticleId());

    articleVersions = ListUtil.sort(articleVersions, new ArticleVersionComparator());

    List<ObjectValuePair<Long, Integer>> articleVersionStatusOVPs = new ArrayList<>();

    if ((articleVersions != null) && !articleVersions.isEmpty()) {
        articleVersionStatusOVPs = getArticleVersionStatuses(articleVersions);
    }

    article = updateStatus(userId, article.getId(), WorkflowConstants.STATUS_IN_TRASH,
            new HashMap<String, Serializable>(), new ServiceContext());

    // Trash

    JournalArticleResource articleResource = journalArticleResourceLocalService
            .getArticleResource(article.getResourcePrimKey());

    UnicodeProperties typeSettingsProperties = new UnicodeProperties();

    typeSettingsProperties.put("title", article.getArticleId());

    TrashEntry trashEntry = trashEntryLocalService.addTrashEntry(userId, article.getGroupId(),
            JournalArticle.class.getName(), article.getResourcePrimKey(), articleResource.getUuid(), null,
            oldStatus, articleVersionStatusOVPs, typeSettingsProperties);

    String trashArticleId = TrashUtil.getTrashTitle(trashEntry.getEntryId());

    for (JournalArticle articleVersion : articleVersions) {
        articleVersion.setArticleId(trashArticleId);
        articleVersion.setStatus(WorkflowConstants.STATUS_IN_TRASH);

        journalArticlePersistence.update(articleVersion);
    }

    articleResource.setArticleId(trashArticleId);

    journalArticleResourcePersistence.update(articleResource);

    article.setArticleId(trashArticleId);

    article = journalArticlePersistence.update(article);

    // Asset

    assetEntryLocalService.updateVisible(JournalArticle.class.getName(), article.getResourcePrimKey(), false);

    // Attachments

    for (FileEntry fileEntry : article.getImagesFileEntries()) {
        PortletFileRepositoryUtil.movePortletFileEntryToTrash(userId, fileEntry.getFileEntryId());
    }

    // Comment

    if (isArticleCommentsEnabled(article.getCompanyId())) {
        CommentManagerUtil.moveDiscussionToTrash(JournalArticle.class.getName(), article.getResourcePrimKey());
    }

    // Social

    JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject();

    extraDataJSONObject.put("title", article.getTitleMapAsXML());

    SocialActivityManagerUtil.addActivity(userId, article, SocialActivityConstants.TYPE_MOVE_TO_TRASH,
            extraDataJSONObject.toString(), 0);

    if (oldStatus == WorkflowConstants.STATUS_PENDING) {
        workflowInstanceLinkLocalService.deleteWorkflowInstanceLink(article.getCompanyId(),
                article.getGroupId(), JournalArticle.class.getName(), article.getId());
    }

    return article;
}

From source file:com.liferay.journal.service.impl.JournalArticleLocalServiceImpl.java

License:Open Source License

/**
 * Updates the web content article with additional parameters.
 *
 * @param  userId the primary key of the user updating the web content
 *         article/*  w  w w  . java  2 s  .com*/
 * @param  groupId the primary key of the web content article's group
 * @param  folderId the primary key of the web content article folder
 * @param  articleId the primary key of the web content article
 * @param  version the web content article's version
 * @param  titleMap the web content article's locales and localized titles
 * @param  descriptionMap the web content article's locales and localized
 *         descriptions
 * @param  content the HTML content wrapped in XML. For more information,
 *         see the content example in the {@link #addArticle(long, long,
 *         long, long, long, String, boolean, double, Map, Map, String,
 *         String, String, String, int, int, int, int, int, int, int, int,
 *         int, int, boolean, int, int, int, int, int, boolean, boolean,
 *         boolean, String, File, Map, String, ServiceContext)} description.
 * @param  ddmStructureKey the primary key of the web content article's DDM
 *         structure, if the article is related to a DDM structure, or
 *         <code>null</code> otherwise
 * @param  ddmTemplateKey the primary key of the web content article's DDM
 *         template
 * @param  layoutUuid the unique string identifying the web content
 *         article's display page
 * @param  displayDateMonth the month the web content article is set to
 *         display
 * @param  displayDateDay the calendar day the web content article is set to
 *         display
 * @param  displayDateYear the year the web content article is set to
 *         display
 * @param  displayDateHour the hour the web content article is set to
 *         display
 * @param  displayDateMinute the minute the web content article is set to
 *         display
 * @param  expirationDateMonth the month the web content article is set to
 *         expire
 * @param  expirationDateDay the calendar day the web content article is set
 *         to expire
 * @param  expirationDateYear the year the web content article is set to
 *         expire
 * @param  expirationDateHour the hour the web content article is set to
 *         expire
 * @param  expirationDateMinute the minute the web content article is set to
 *         expire
 * @param  neverExpire whether the web content article is not set to auto
 *         expire
 * @param  reviewDateMonth the month the web content article is set for
 *         review
 * @param  reviewDateDay the calendar day the web content article is set for
 *         review
 * @param  reviewDateYear the year the web content article is set for review
 * @param  reviewDateHour the hour the web content article is set for review
 * @param  reviewDateMinute the minute the web content article is set for
 *         review
 * @param  neverReview whether the web content article is not set for review
 * @param  indexable whether the web content is searchable
 * @param  smallImage whether to update web content article's a small image.
 *         A file must be passed in as <code>smallImageFile</code> value,
 *         otherwise the current small image is deleted.
 * @param  smallImageURL the web content article's small image URL
 *         (optionally <code>null</code>)
 * @param  smallImageFile the web content article's new small image file
 *         (optionally <code>null</code>). Must pass in
 *         <code>smallImage</code> value of <code>true</code> to replace the
 *         article's small image file.
 * @param  images the web content's images (optionally <code>null</code>)
 * @param  articleURL the web content article's accessible URL (optionally
 *         <code>null</code>)
 * @param  serviceContext the service context to be applied. Can set the
 *         modification date, expando bridge attributes, asset category IDs,
 *         asset tag names, asset link entry IDs, asset priority, workflow
 *         actions, URL title , and can set whether to add the default
 *         command update for the web content article. With respect to
 *         social activities, by setting the service context's command to
 *         {@link Constants#UPDATE}, the invocation is considered a web
 *         content update activity; otherwise it is considered a web content
 *         add activity.
 * @return the updated web content article
 */
@Indexable(type = IndexableType.REINDEX)
@Override
public JournalArticle updateArticle(long userId, long groupId, long folderId, String articleId, double version,
        Map<Locale, String> titleMap, Map<Locale, String> descriptionMap, String content,
        String ddmStructureKey, String ddmTemplateKey, String layoutUuid, int displayDateMonth,
        int displayDateDay, int displayDateYear, int displayDateHour, int displayDateMinute,
        int expirationDateMonth, int expirationDateDay, int expirationDateYear, int expirationDateHour,
        int expirationDateMinute, boolean neverExpire, int reviewDateMonth, int reviewDateDay,
        int reviewDateYear, int reviewDateHour, int reviewDateMinute, boolean neverReview, boolean indexable,
        boolean smallImage, String smallImageURL, File smallImageFile, Map<String, byte[]> images,
        String articleURL, ServiceContext serviceContext) throws PortalException {

    // Article

    User user = userLocalService.getUser(userId);
    articleId = StringUtil.toUpperCase(StringUtil.trim(articleId));

    byte[] smallImageBytes = null;

    try {
        smallImageBytes = FileUtil.getBytes(smallImageFile);
    } catch (IOException ioe) {
    }

    JournalArticle latestArticle = getLatestArticle(groupId, articleId, WorkflowConstants.STATUS_ANY);

    JournalArticle article = latestArticle;

    boolean imported = ExportImportThreadLocal.isImportInProcess();

    double latestVersion = latestArticle.getVersion();

    boolean addNewVersion = false;

    if (imported) {
        article = getArticle(groupId, articleId, version);
    } else {
        if ((version > 0) && (version != latestVersion)) {
            StringBundler sb = new StringBundler(4);

            sb.append("Version ");
            sb.append(version);
            sb.append(" is not the same as ");
            sb.append(latestVersion);

            throw new ArticleVersionException(sb.toString());
        }

        serviceContext.validateModifiedDate(latestArticle, ArticleVersionException.class);

        if (latestArticle.isApproved() || latestArticle.isExpired() || latestArticle.isScheduled()) {

            addNewVersion = true;

            version = getNextVersion(article);
        }
    }

    Date displayDate = null;
    Date expirationDate = null;
    Date reviewDate = null;

    if (article.getClassNameId() == JournalArticleConstants.CLASSNAME_ID_DEFAULT) {

        displayDate = PortalUtil.getDate(displayDateMonth, displayDateDay, displayDateYear, displayDateHour,
                displayDateMinute, user.getTimeZone(), null);

        if (!neverExpire) {
            expirationDate = PortalUtil.getDate(expirationDateMonth, expirationDateDay, expirationDateYear,
                    expirationDateHour, expirationDateMinute, user.getTimeZone(),
                    ArticleExpirationDateException.class);
        }

        if (!neverReview) {
            reviewDate = PortalUtil.getDate(reviewDateMonth, reviewDateDay, reviewDateYear, reviewDateHour,
                    reviewDateMinute, user.getTimeZone(), ArticleReviewDateException.class);
        }
    }

    Date now = new Date();

    boolean expired = false;

    if ((expirationDate != null) && expirationDate.before(now)) {
        expired = true;
    }

    validate(user.getCompanyId(), groupId, latestArticle.getClassNameId(), titleMap, content, ddmStructureKey,
            ddmTemplateKey, displayDate, expirationDate, smallImage, smallImageURL, smallImageFile,
            smallImageBytes, serviceContext);

    validateReferences(groupId, ddmStructureKey, ddmTemplateKey, layoutUuid, smallImage, smallImageURL,
            smallImageBytes, latestArticle.getSmallImageId(), content);

    if (addNewVersion) {
        long id = counterLocalService.increment();

        article = journalArticlePersistence.create(id);

        article.setResourcePrimKey(latestArticle.getResourcePrimKey());
        article.setGroupId(latestArticle.getGroupId());
        article.setCompanyId(latestArticle.getCompanyId());
        article.setUserId(user.getUserId());
        article.setUserName(user.getFullName());
        article.setCreateDate(latestArticle.getCreateDate());
        article.setClassNameId(latestArticle.getClassNameId());
        article.setClassPK(latestArticle.getClassPK());
        article.setArticleId(articleId);
        article.setVersion(version);
        article.setSmallImageId(latestArticle.getSmallImageId());

        _addArticleLocalizedFields(article.getCompanyId(), article.getId(), titleMap, descriptionMap);
    } else {
        _updateArticleLocalizedFields(article.getCompanyId(), article.getId(), titleMap, descriptionMap);
    }

    Locale locale = getArticleDefaultLocale(content);

    String title = titleMap.get(locale);

    content = format(user, groupId, article, content);

    article.setFolderId(folderId);
    article.setTreePath(article.buildTreePath());
    article.setUrlTitle(getUniqueUrlTitle(article.getId(), groupId, article.getArticleId(), title,
            latestArticle.getUrlTitle(), serviceContext));
    article.setContent(content);
    article.setDDMStructureKey(ddmStructureKey);
    article.setDDMTemplateKey(ddmTemplateKey);
    article.setDefaultLanguageId(LocaleUtil.toLanguageId(locale));
    article.setLayoutUuid(layoutUuid);
    article.setDisplayDate(displayDate);
    article.setExpirationDate(expirationDate);
    article.setReviewDate(reviewDate);
    article.setIndexable(indexable);
    article.setSmallImage(smallImage);

    if (smallImage) {
        if ((smallImageFile != null) && (smallImageBytes != null)) {
            article.setSmallImageId(counterLocalService.increment());
        }
    } else {
        article.setSmallImageId(0);
    }

    article.setSmallImageURL(smallImageURL);

    if (latestArticle.isPending()) {
        article.setStatus(latestArticle.getStatus());
    } else if (!expired) {
        article.setStatus(WorkflowConstants.STATUS_DRAFT);
    } else {
        article.setStatus(WorkflowConstants.STATUS_EXPIRED);
    }

    ExpandoBridgeUtil.setExpandoBridgeAttributes(latestArticle.getExpandoBridge(), article.getExpandoBridge(),
            serviceContext);

    journalArticlePersistence.update(article);

    // Asset

    if (hasModifiedLatestApprovedVersion(groupId, articleId, version)) {
        updateAsset(userId, article, serviceContext.getAssetCategoryIds(), serviceContext.getAssetTagNames(),
                serviceContext.getAssetLinkEntryIds(), serviceContext.getAssetPriority());
    }

    // Dynamic data mapping

    if (classNameLocalService.getClassNameId(DDMStructure.class) == article.getClassNameId()) {

        updateDDMStructurePredefinedValues(article.getClassPK(), content, serviceContext);
    } else {
        updateDDMLinks(article.getId(), groupId, ddmStructureKey, ddmTemplateKey, addNewVersion);
    }

    // Small image

    saveImages(smallImage, article.getSmallImageId(), smallImageFile, smallImageBytes);

    // Email

    PortletPreferences preferences = ServiceContextUtil.getPortletPreferences(serviceContext);

    // Workflow

    if (expired && imported) {
        updateStatus(userId, article, article.getStatus(), articleURL, serviceContext,
                new HashMap<String, Serializable>());
    }

    if (serviceContext.getWorkflowAction() == WorkflowConstants.ACTION_PUBLISH) {

        articleURL = buildArticleURL(articleURL, groupId, folderId, articleId);

        serviceContext.setAttribute("articleURL", articleURL);

        sendEmail(article, articleURL, preferences, "requested", serviceContext);

        startWorkflowInstance(userId, article, serviceContext);
    }

    return journalArticlePersistence.findByPrimaryKey(article.getId());
}

From source file:com.liferay.journal.service.impl.JournalArticleLocalServiceImpl.java

License:Open Source License

/**
 * Updates the translation of the web content article.
 *
 * @param  groupId the primary key of the web content article's group
 * @param  articleId the primary key of the web content article
 * @param  version the web content article's version
 * @param  locale the locale of the web content article's display template
 * @param  title the translated web content article title
 * @param  description the translated web content article description
 * @param  content the HTML content wrapped in XML. For more information,
 *         see the content example in the {@link #addArticle(long, long,
 *         long, long, long, String, boolean, double, Map, Map, String,
 *         String, String, String, int, int, int, int, int, int, int, int,
 *         int, int, boolean, int, int, int, int, int, boolean, boolean,
 *         boolean, String, File, Map, String, ServiceContext)} description.
 * @param  images the web content's images
 * @param  serviceContext the service context to be applied. Can set the
 *         modification date and URL title for the web content article.
 * @return the updated web content article
 *///from  w  w  w  . jav  a  2 s. c  o  m
@Indexable(type = IndexableType.REINDEX)
@Override
public JournalArticle updateArticleTranslation(long groupId, String articleId, double version, Locale locale,
        String title, String description, String content, Map<String, byte[]> images,
        ServiceContext serviceContext) throws PortalException {

    validateContent(content);

    JournalArticle oldArticle = getLatestArticle(groupId, articleId, WorkflowConstants.STATUS_ANY);

    double oldVersion = oldArticle.getVersion();

    if ((version > 0) && (version != oldVersion)) {
        StringBundler sb = new StringBundler(4);

        sb.append("Version ");
        sb.append(version);
        sb.append(" is not the same as ");
        sb.append(oldVersion);

        throw new ArticleVersionException(sb.toString());
    }

    boolean incrementVersion = false;

    if (oldArticle.isApproved() || oldArticle.isExpired()) {
        incrementVersion = true;
    }

    if (serviceContext != null) {
        serviceContext.validateModifiedDate(oldArticle, ArticleVersionException.class);
    }

    JournalArticle article = null;

    User user = userLocalService.fetchUser(oldArticle.getUserId());

    if (user == null) {
        user = userLocalService.getDefaultUser(oldArticle.getCompanyId());
    }

    Locale defaultLocale = getArticleDefaultLocale(content);

    if (incrementVersion) {
        double newVersion = getNextVersion(oldArticle);

        long id = counterLocalService.increment();

        article = journalArticlePersistence.create(id);

        article.setResourcePrimKey(oldArticle.getResourcePrimKey());
        article.setGroupId(oldArticle.getGroupId());
        article.setCompanyId(oldArticle.getCompanyId());
        article.setUserId(oldArticle.getUserId());
        article.setUserName(user.getFullName());
        article.setCreateDate(oldArticle.getCreateDate());
        article.setFolderId(oldArticle.getFolderId());
        article.setClassNameId(oldArticle.getClassNameId());
        article.setClassPK(oldArticle.getClassPK());
        article.setArticleId(articleId);
        article.setVersion(newVersion);
        article.setUrlTitle(
                getUniqueUrlTitle(id, groupId, articleId, title, oldArticle.getUrlTitle(), serviceContext));
        article.setDDMStructureKey(oldArticle.getDDMStructureKey());
        article.setDDMTemplateKey(oldArticle.getDDMTemplateKey());
        article.setDefaultLanguageId(LocaleUtil.toLanguageId(defaultLocale));
        article.setLayoutUuid(oldArticle.getLayoutUuid());
        article.setDisplayDate(oldArticle.getDisplayDate());
        article.setExpirationDate(oldArticle.getExpirationDate());
        article.setReviewDate(oldArticle.getReviewDate());
        article.setIndexable(oldArticle.getIndexable());
        article.setSmallImage(oldArticle.getSmallImage());
        article.setSmallImageId(oldArticle.getSmallImageId());

        if (article.getSmallImageId() == 0) {
            article.setSmallImageId(counterLocalService.increment());
        }

        article.setSmallImageURL(oldArticle.getSmallImageURL());

        article.setStatus(WorkflowConstants.STATUS_DRAFT);
        article.setStatusDate(new Date());

        ExpandoBridgeUtil.copyExpandoBridgeAttributes(oldArticle.getExpandoBridge(),
                article.getExpandoBridge());

        // Article localization

        _addArticleLocalizedFields(article.getCompanyId(), article.getId(), oldArticle.getTitleMap(),
                oldArticle.getDescriptionMap());

        // Dynamic data mapping

        updateDDMLinks(id, groupId, oldArticle.getDDMStructureKey(), oldArticle.getDDMTemplateKey(), true);
    } else {
        article = oldArticle;
    }

    _updateArticleLocalizedFields(article.getCompanyId(), article.getId(), title, description,
            LocaleUtil.toLanguageId(locale));

    content = format(user, groupId, article, content);

    article.setContent(content);

    journalArticlePersistence.update(article);

    return article;
}

From source file:com.liferay.journal.service.impl.JournalArticleLocalServiceImpl.java

License:Open Source License

protected List<ObjectValuePair<Long, Integer>> getArticleVersionStatuses(List<JournalArticle> articles) {

    List<ObjectValuePair<Long, Integer>> articleVersionStatusOVPs = new ArrayList<>(articles.size());

    for (JournalArticle article : articles) {
        int status = article.getStatus();

        if (status == WorkflowConstants.STATUS_PENDING) {
            status = WorkflowConstants.STATUS_DRAFT;
        }//from w  ww  .j  ava 2 s. c o  m

        ObjectValuePair<Long, Integer> articleVersionStatusOVP = new ObjectValuePair<>(article.getId(), status);

        articleVersionStatusOVPs.add(articleVersionStatusOVP);
    }

    return articleVersionStatusOVPs;
}

From source file:com.liferay.journal.service.impl.JournalFolderLocalServiceImpl.java

License:Open Source License

protected void moveDependentsToTrash(List<Object> foldersAndArticles, long trashEntryId)
        throws PortalException {

    for (Object object : foldersAndArticles) {
        if (object instanceof JournalArticle) {

            // Article

            JournalArticle article = (JournalArticle) object;

            if (article.getStatus() == WorkflowConstants.STATUS_IN_TRASH) {
                continue;
            }//from w  ww  . jav  a  2 s.  c o  m

            // Articles

            List<JournalArticle> articles = journalArticlePersistence.findByG_A(article.getGroupId(),
                    article.getArticleId());

            for (JournalArticle curArticle : articles) {

                // Article

                int curArticleOldStatus = curArticle.getStatus();

                curArticle.setStatus(WorkflowConstants.STATUS_IN_TRASH);

                journalArticlePersistence.update(curArticle);

                // Trash

                int status = curArticleOldStatus;

                if (curArticleOldStatus == WorkflowConstants.STATUS_PENDING) {

                    status = WorkflowConstants.STATUS_DRAFT;
                }

                if (curArticleOldStatus != WorkflowConstants.STATUS_APPROVED) {

                    trashVersionLocalService.addTrashVersion(trashEntryId, JournalArticle.class.getName(),
                            curArticle.getId(), status, null);
                }

                // Workflow

                if (curArticleOldStatus == WorkflowConstants.STATUS_PENDING) {

                    workflowInstanceLinkLocalService.deleteWorkflowInstanceLink(curArticle.getCompanyId(),
                            curArticle.getGroupId(), JournalArticle.class.getName(), curArticle.getId());
                }
            }

            // Asset

            assetEntryLocalService.updateVisible(JournalArticle.class.getName(), article.getResourcePrimKey(),
                    false);

            // Indexer

            Indexer<JournalArticle> indexer = IndexerRegistryUtil.nullSafeGetIndexer(JournalArticle.class);

            indexer.reindex(article);
        } else if (object instanceof JournalFolder) {

            // Folder

            JournalFolder folder = (JournalFolder) object;

            if (folder.isInTrashExplicitly()) {
                continue;
            }

            int oldStatus = folder.getStatus();

            folder.setStatus(WorkflowConstants.STATUS_IN_TRASH);

            journalFolderPersistence.update(folder);

            // Trash

            if (oldStatus != WorkflowConstants.STATUS_APPROVED) {
                trashVersionLocalService.addTrashVersion(trashEntryId, JournalFolder.class.getName(),
                        folder.getFolderId(), oldStatus, null);
            }

            // Folders and articles

            List<Object> curFoldersAndArticles = getFoldersAndArticles(folder.getGroupId(),
                    folder.getFolderId());

            moveDependentsToTrash(curFoldersAndArticles, trashEntryId);

            // Asset

            assetEntryLocalService.updateVisible(JournalFolder.class.getName(), folder.getFolderId(), false);

            // Indexer

            Indexer<JournalFolder> indexer = IndexerRegistryUtil.nullSafeGetIndexer(JournalFolder.class);

            indexer.reindex(folder);
        }
    }
}

From source file:com.liferay.journal.service.persistence.test.JournalArticleFinderTest.java

License:Open Source License

@Test
public void testDraftArticles() throws Exception {
    QueryDefinition<JournalArticle> queryDefinition = new QueryDefinition<>(WorkflowConstants.STATUS_ANY);

    testQueryByG_C(_group.getGroupId(), Collections.<Long>emptyList(),
            JournalArticleConstants.CLASSNAME_ID_DEFAULT, queryDefinition, 2);

    queryDefinition.setOwnerUserId(TestPropsValues.getUserId());

    JournalArticle article = JournalTestUtil.addArticleWithXMLContent(_group.getGroupId(),
            _folder.getFolderId(), JournalArticleConstants.CLASSNAME_ID_DEFAULT, "<title>Article 1</title>",
            _basicWebContentDDMStructure.getStructureKey(), _basicWebContentDDMTemplate.getTemplateKey());

    article.setUserId(_USER_ID);//from   w  w w.ja v a 2 s .c  o  m
    article.setStatus(WorkflowConstants.STATUS_DRAFT);

    JournalArticleLocalServiceUtil.updateJournalArticle(article);

    _articles.add(article);

    queryDefinition.setIncludeOwner(true);
    queryDefinition.setOwnerUserId(_USER_ID);
    queryDefinition.setStatus(WorkflowConstants.STATUS_APPROVED);

    testQueryByG_C(_group.getGroupId(), Collections.<Long>emptyList(),
            JournalArticleConstants.CLASSNAME_ID_DEFAULT, queryDefinition, 3);

    queryDefinition.setIncludeOwner(false);

    testQueryByG_C(_group.getGroupId(), Collections.<Long>emptyList(),
            JournalArticleConstants.CLASSNAME_ID_DEFAULT, queryDefinition, 0);
}

From source file:com.liferay.journal.service.test.JournalArticleServiceTest.java

License:Open Source License

@Test
public void testFetchLatestArticleByDraftStatusWhenNoDraftArticle() throws Exception {

    _article = JournalTestUtil.updateArticle(_article, "Version 2");

    _latestArticle = fetchLatestArticle(WorkflowConstants.STATUS_DRAFT);

    Assert.assertNull(_latestArticle);//from w  w  w  .  ja  va  2  s. c  o m
}

From source file:com.liferay.journal.service.test.JournalArticleServiceTest.java

License:Open Source License

@Test
public void testGetGroupArticlesWhenUserNotNullAndStatusAny() throws Exception {

    List<JournalArticle> expectedArticles = addArticles(2, RandomTestUtil.randomString());

    _article = updateArticleStatus(_article, WorkflowConstants.STATUS_DRAFT);

    expectedArticles.add(_article);/*w w w  .jav  a  2  s . c om*/

    int count = JournalArticleServiceUtil.getGroupArticlesCount(_group.getGroupId(), _article.getUserId(),
            JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID);

    Assert.assertEquals(3, count);

    List<JournalArticle> articles = JournalArticleServiceUtil.getGroupArticles(_group.getGroupId(),
            _article.getUserId(), JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID, QueryUtil.ALL_POS,
            QueryUtil.ALL_POS, null);

    Assert.assertEquals(expectedArticles, articles);
}