Example usage for com.liferay.portal.kernel.xml Element elementTextTrim

List of usage examples for com.liferay.portal.kernel.xml Element elementTextTrim

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.xml Element elementTextTrim.

Prototype

public String elementTextTrim(String name);

Source Link

Usage

From source file:com.liferay.portlet.blogs.lar.WordPressImporter.java

License:Open Source License

protected static void importComment(PortletDataContext context, User defaultUser,
        MBMessageDisplay messageDisplay, Map<Long, Long> messageIdMap, BlogsEntry entry, Element commentEl)
        throws PortalException, SystemException {

    MBThread thread = messageDisplay.getThread();

    long commentId = GetterUtil
            .getLong(commentEl.elementTextTrim(SAXReaderUtil.createQName("comment_id", _NS_WP)));

    String commentContent = commentEl.elementTextTrim(SAXReaderUtil.createQName("comment_content", _NS_WP));

    if (Validator.isNull(commentContent)) {
        return;/*from w w w.  jav a 2s  .c  om*/
    }

    String commentAuthor = commentEl.elementTextTrim(SAXReaderUtil.createQName("comment_author", _NS_WP));

    commentAuthor = commentAuthor.substring(0, Math.min(75, commentAuthor.length()));

    long commentParentId = GetterUtil
            .getLong(commentEl.elementTextTrim(SAXReaderUtil.createQName("comment_parent", _NS_WP)));

    if (commentParentId == 0) {
        commentParentId = messageDisplay.getMessage().getMessageId();
    } else {
        commentParentId = messageIdMap.get(commentParentId);
    }

    ServiceContext serviceContext = new ServiceContext();

    serviceContext.setAddGroupPermissions(true);
    serviceContext.setAddGuestPermissions(true);

    MBMessage message = MBMessageLocalServiceUtil.addDiscussionMessage(defaultUser.getUserId(), commentAuthor,
            context.getGroupId(), BlogsEntry.class.getName(), entry.getEntryId(), thread.getThreadId(),
            commentParentId, null, commentContent, serviceContext);

    messageIdMap.put(commentId, message.getMessageId());
}

From source file:com.liferay.portlet.blogs.lar.WordPressImporter.java

License:Open Source License

protected static void importEntry(PortletDataContext context, User defaultUser, Map<String, Long> userMap,
        DateFormat dateFormat, Element entryEl) throws PortalException, SystemException {

    String creator = entryEl.elementText(SAXReaderUtil.createQName("creator", _NS_DC));

    Long userId = userMap.get(creator);

    if (userId == null) {
        userId = context.getUserId(null);
    }/*from w  w  w  . j a v a 2s . c  o m*/

    String title = entryEl.elementTextTrim("title");

    if (Validator.isNull(title)) {
        title = entryEl.elementTextTrim(SAXReaderUtil.createQName("post_name", _NS_WP));
    }

    String content = entryEl.elementText(SAXReaderUtil.createQName("encoded", _NS_CONTENT));

    content = content.replaceAll("\\n", "\n<br />");

    // LPS-1425

    if (Validator.isNull(content)) {
        content = "<br />";
    }

    String dateText = entryEl.elementTextTrim(SAXReaderUtil.createQName("post_date_gmt", _NS_WP));

    Date postDate = new Date();

    try {
        postDate = dateFormat.parse(dateText);
    } catch (ParseException pe) {
        _log.warn("Parse " + dateText, pe);
    }

    Calendar cal = Calendar.getInstance();

    cal.setTime(postDate);

    int displayDateMonth = cal.get(Calendar.MONTH);
    int displayDateDay = cal.get(Calendar.DAY_OF_MONTH);
    int displayDateYear = cal.get(Calendar.YEAR);
    int displayDateHour = cal.get(Calendar.HOUR_OF_DAY);
    int displayDateMinute = cal.get(Calendar.MINUTE);

    String pingStatusText = entryEl.elementTextTrim(SAXReaderUtil.createQName("ping_status", _NS_WP));

    boolean allowPingbacks = pingStatusText.equalsIgnoreCase("open");
    boolean allowTrackbacks = allowPingbacks;

    String statusText = entryEl.elementTextTrim(SAXReaderUtil.createQName("status", _NS_WP));

    int workflowAction = WorkflowConstants.ACTION_PUBLISH;

    if (statusText.equalsIgnoreCase("draft")) {
        workflowAction = WorkflowConstants.ACTION_SAVE_DRAFT;
    }

    String[] assetTagNames = null;

    String categoryText = entryEl.elementTextTrim("category");

    if (Validator.isNotNull(categoryText)) {
        assetTagNames = new String[] { categoryText };
    }

    ServiceContext serviceContext = new ServiceContext();

    serviceContext.setAddGroupPermissions(true);
    serviceContext.setAddGuestPermissions(true);
    serviceContext.setAssetTagNames(assetTagNames);
    serviceContext.setScopeGroupId(context.getGroupId());
    serviceContext.setWorkflowAction(workflowAction);

    BlogsEntry entry = null;

    try {
        entry = BlogsEntryLocalServiceUtil.addEntry(userId, title, StringPool.BLANK, content, displayDateMonth,
                displayDateDay, displayDateYear, displayDateHour, displayDateMinute, allowPingbacks,
                allowTrackbacks, null, false, null, null, null, serviceContext);
    } catch (Exception e) {
        _log.error("Add entry " + title, e);

        return;
    }

    MBMessageDisplay messageDisplay = MBMessageLocalServiceUtil.getDiscussionMessageDisplay(userId,
            context.getGroupId(), BlogsEntry.class.getName(), entry.getEntryId(),
            WorkflowConstants.STATUS_APPROVED);

    Map<Long, Long> messageIdMap = new HashMap<Long, Long>();

    List<Node> commentNodes = entryEl.selectNodes("wp:comment", "wp:comment_parent/text()");

    for (Node commentNode : commentNodes) {
        Element commentEl = (Element) commentNode;

        importComment(context, defaultUser, messageDisplay, messageIdMap, entry, commentEl);
    }
}