Example usage for com.liferay.portal.kernel.util PortalUtil getDate

List of usage examples for com.liferay.portal.kernel.util PortalUtil getDate

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.util PortalUtil getDate.

Prototype

public static Date getDate(int month, int day, int year, int hour, int min, TimeZone timeZone,
        Class<? extends PortalException> clazz) throws PortalException 

Source Link

Document

Returns the date object for the specified month, day, year, hour, minute, and time zone, optionally throwing an exception if the date is invalid.

Usage

From source file:com.liferay.blogs.service.impl.BlogsEntryLocalServiceImpl.java

License:Open Source License

@Override
public BlogsEntry addEntry(long userId, String title, String subtitle, String urlTitle, String description,
        String content, int displayDateMonth, int displayDateDay, int displayDateYear, int displayDateHour,
        int displayDateMinute, boolean allowPingbacks, boolean allowTrackbacks, String[] trackbacks,
        String coverImageCaption, ImageSelector coverImageImageSelector, ImageSelector smallImageImageSelector,
        ServiceContext serviceContext) throws PortalException {

    User user = userPersistence.findByPrimaryKey(userId);

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

    return addEntry(userId, title, subtitle, urlTitle, description, content, displayDate, allowPingbacks,
            allowTrackbacks, trackbacks, coverImageCaption, coverImageImageSelector, smallImageImageSelector,
            serviceContext);/*from   ww  w.j  a  v a  2 s .  c om*/
}

From source file:com.liferay.blogs.service.impl.BlogsEntryLocalServiceImpl.java

License:Open Source License

@Override
public BlogsEntry updateEntry(long userId, long entryId, String title, String subtitle, String urlTitle,
        String description, String content, int displayDateMonth, int displayDateDay, int displayDateYear,
        int displayDateHour, int displayDateMinute, boolean allowPingbacks, boolean allowTrackbacks,
        String[] trackbacks, String coverImageCaption, ImageSelector coverImageImageSelector,
        ImageSelector smallImageImageSelector, ServiceContext serviceContext) throws PortalException {

    User user = userPersistence.findByPrimaryKey(userId);

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

    return updateEntry(userId, entryId, title, subtitle, urlTitle, description, content, displayDate,
            allowPingbacks, allowTrackbacks, trackbacks, coverImageCaption, coverImageImageSelector,
            smallImageImageSelector, serviceContext);
}

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

License:Open Source License

/**
 * Adds a web content article with additional parameters.
 *
 * <p>//w  ww. j  ava 2s . 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

/**
 * Updates the web content article with additional parameters.
 *
 * @param  userId the primary key of the user updating the web content
 *         article/*from  ww  w  .j  a  v a 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());
}