Example usage for com.liferay.portal.kernel.workflow WorkflowHandler getWorkflowDefinitionLink

List of usage examples for com.liferay.portal.kernel.workflow WorkflowHandler getWorkflowDefinitionLink

Introduction

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

Prototype

public WorkflowDefinitionLink getWorkflowDefinitionLink(long companyId, long groupId, long classPK)
            throws PortalException;

Source Link

Usage

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//w ww.  ja va  2 s .c  om
 */
@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;
}