JBoss Community Archive (Read Only)

GateIn WCM

5. Using WCM API in web applications

GateIn WCM offers a simplified API as an alternative to templates mechanism.

WCM API allows to access posts, categories, comments and uploads from third party applications deployed on GateIn / JBoss Portal.

WCM API is composed by the following packages:

package

description

org.gatein.wcm.api.domain

It contains WCM classes representing Category, Comment, Post and Upload

org.gatein.wcm.api.services

It contains main interface API for WCM query operations

org.gatein.wcm.api.util

It contains helper classes for html manipulation

org.gatein.wcm.api

It contains WcmApi used to get an instance of WCM API

5.1 WCM API in a nutshell

Get all posts linked with a category
String user = "anonymous";

WcmApiService wcm = WcmApi.getInstance();

Category cat = wcm.findCategory("/Events/Top Events", user);

List<Post> topEvents = wcm.findPosts(cat.getId(), Post.PUBLISHED, user);

for (Post post : topEvents) {
  ...
}

We can see in the example that main entry point of API is

WcmApiService wcm = WcmApi.getInstance();

WcmApi is just a helper class to give access to the EJB in a simplistic way.

5.2 org.gatein.wcm.api.domain

This package represents main elements of WCM domain:

Category
/**
 * Categories group content (Posts, Uploads).
 * Categories have a tree structure with Category's parent reference.
 * Light representation from org.gatein.wcm.domain.Category class for remote api.
 *
 * @author <a href="mailto:lponce@redhat.com">Lucas Ponce</a>
 */
public class Category implements Serializable {

    /**
     * Normal Category.
     * It's defined for semantic porpuse, an object should have only 1 Category but it can have N Tags.
     */
    public static final Character CATEGORY = '2';
    /**
     * Tag Category.
     * It's defined for semantic porpuse, an object should have only 1 Category but it can have N Tags.
     */
    public static final Character TAG = '3';
    /**
     * Folder Category.
     * A Folder Category can group other categories.
     */
    public static final Character FOLDER = '1';

    private Long id;
    private String name;
    private Character type;
    private int numChildren;
    private String path;

    ... getters and setters ...
}
Comment
/**
 * Comments are attached to Posts.
 * Light representation from org.gatein.wcm.domain.Comment class for remote api.
 *
 * @author <a href="mailto:lponce@redhat.com">Lucas Ponce</a>
 */
public class Comment implements Serializable {

    /**
     * Post can accept comments from non-logged users.
     */
    public static final Character ANONYMOUS = 'A';
    /**
     * Post accepts only comments from GateIn users.
     */
    public static final Character LOGGED = 'L';
    /**
     * This Post doesn't accept comments.
     */
    public static final Character NO_COMMENTS = 'N';

    private Long id;
    private Long postId;
    private String author;
    private String authorEmail;
    private String authorUrl;
    private Calendar created;
    private String content;
    private Character status;

    ... getters and setters ...
}
Post
/**
 * Post represents a content publication in the GateIn WCM system.
 * Light representation from org.gatein.wcm.domain.Post class for remote api.
 *
 * @author <a href="mailto:lponce@redhat.com">Lucas Ponce</a>
 */
public class Post implements Serializable {

    /**
     * Post is public and it can be accessible from Content Portlets.
     */
    public static final Character PUBLISHED = 'P';
    /**
     * Post is in draft mode and it can not be accessible from Content Portlets.
     */
    public static final Character DRAFT = 'D';

    private Long id;
    private Long version;
    private String author;
    private Calendar created;
    private String content;
    private String title;
    private String excerpt;
    private Character postStatus;
    private Calendar modified;
    private String locale;
    private Character commentsStatus;
    private Set<Comment> comments = new HashSet<Comment>();
    private Set<Category> categories = new HashSet<Category>();

    ... getters and setters ...
}
Upload
/**
 * Upload represents files uploaded into wcm as images or resources.
 * These resources will be accessible through special wcm links.
 * Files binary are stored directly into file system, this class represents metadata of the file.
 * Light representation from org.gatein.wcm.domain.Upload class for remote api.
 *
 * @author <a href="mailto:lponce@redhat.com">Lucas Ponce</a>
 */
public class Upload implements Serializable {

    private Long id;
    private Long version;
    private String fileName;
    private String storedName;
    private String mimeType;
    private Calendar created;
    private Calendar modified;
    private String user;
    private String description;
    private Set<Category> categories = new HashSet<Category>();

    ... getters and setters ...
}

5.3 org.gatein.wcm.api.services

Main WCM API for Query operations
/**
 * Remote API for org.gatein.wcm.api.domain.* model.
 * Query operations.
 *
 * @author <a href="mailto:lponce@redhat.com">Lucas Ponce</a>
 */
@Remote
public interface WcmApiService {

    /**
     * @param user who performs operation
     * @return List of Categories with no parent that user can read
     * @throws Exception
     */
    List<Category> findRootCategories(String user) throws Exception;

    /**
     * @param cat Parent category
     * @param user who performs operation
     * @return List of children Categories for cat
     * @throws Exception
     */
    List<Category> findChildren(Category cat, String user) throws Exception;

    /**
     * @param path Category's path
     * @param user who performs operation
     * @return Category's defined by path or null if user has not rights to read or if Category doesn't exist
     * @throws Exception
     */
    Category findCategory(String path, String user) throws Exception;

    /**
     * @param user who performs operation
     * @return List of Posts that user can read
     * @throws Exception
     */
    List<Post> findPosts(String user) throws Exception;

    /**
     * @param categoryId Category's id
     * @param user who performs operation
     * @return List of Posts attached to a Category
     * @throws Exception
     */
    List<Post> findPosts(Long categoryId, String user) throws Exception;

    /**
     * @param filterName Post name for filter
     * @param user who performs operation
     * @return List of Posts attached filtered by name
     * @throws Exception
     */
    List<Post> findPosts(String filterName, String user) throws Exception;

    /**
     * @param categoryId Category's id
     * @param status Post's status
     * @param user who performs operation
     * @return List of Posts linked by Category defined by categoryId that user can read filtered by Post's status
     * @throws Exception
     */
    List<Post> findPosts(Long categoryId, Character status, String user) throws Exception;

    /**
     * @param categoryId Category's id
     * @param locale Locale used as a key for a Relationship
     * @param status Post's status
     * @param user who performs operation
     * @return List of Posts linked by Category defined by categoryId that user can read filtered by Post's status
     * @throws Exception
     */
    List<Post> findPosts(Long categoryId, String locale, Character status, String user) throws Exception;

    /**
     * @param id Post's id
     * @param user who performs operation
     * @return Post defined by id if user has rights to read or null otherwise
     * @throws Exception
     */
    Post findPost(Long id, String user) throws Exception;

    /**
     * @param id Post's id
     * @param locale Locale used as a key for a Relationship
     * @param user who performs operation
     * @return Post defined by id if user has rights to read or null otherwise
     * @throws Exception
     */
    Post findPost(Long id, String locale, String user) throws Exception;

    /**
     * @param user who performs operation
     * @return List of Uploads that user can read
     * @throws Exception
     */
    List<Upload> findUploads(String user) throws Exception;

    /**
     * @param filterName Filter for Upload's description
     * @param user who performs operation
     * @return List of Uploads that user can read filtered by Upload's description
     * @throws Exception
     */
    List<Upload> findUploads(String filterName, String user) throws Exception;

    /**
     * @param categoryId Category's id
     * @param user who performs operation
     * @return List of Uploads that user can read filtered by Category defined by id
     * @throws Exception
     */
    List<Upload> findUploads(Long categoryId, String user) throws Exception;

    /**
     * @param id Upload's id
     * @param user who performs operation
     * @return Upload defined by id if user has rights to read
     * @throws Exception
     */
    Upload findUpload(Long id, String user) throws Exception;

    /**
     * Adds a Comment in a Post.
     *
     * @param postId Post's id
     * @param comment Comment to add
     * @param user who performs operation
     * @throws Exception
     */
    void add(Long postId, Comment comment, String user) throws Exception;

}

5.4 org.gatein.wcm.api.util

Helper class for html manipulation
/**
 * Helper class for WCM content manipulation.
 *
 * @author <a href="mailto:lponce@redhat.com">Lucas Ponce</a>
 */
public class WcmUtils {

    /**
     * @param html code from Post's content
     * @param index of the image to extract
     * @return src of the <img> tag to extract
     */
    public static String extractSrcImg(String html, int index) { ... }

    /**
     * @param html code from Post's content
     * @param index of the image to extract
     * @return <img> tag to extract
     */
    public static String extractImg(String html, int index) { ... }

}

5.5 Examples

There are some examples available under the examples/api-example folder

images/author/download/attachments/75137367/wcm-5.5-1.png

wcmPage.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="org.gatein.wcm.api.WcmApi" %>
<%@ page import="org.gatein.wcm.api.services.WcmApiService" %>
<%@ page import="org.gatein.wcm.api.domain.Category" %>
<%@ page import="org.gatein.wcm.api.domain.Post" %>
<%@ page import="java.util.List" %>
<%@ page import="org.gatein.wcm.api.util.WcmUtils" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>GateIn WCM API example using Bootstrap</title>

    <!-- Bootstrap core CSS -->
    <link href="css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="css/jumbotron.css" rel="stylesheet">

</head>

<body>

<div class="navbar navbar-inverse navbar-fixed-top">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="/api-example/wcmPage.jsp">GateIn WCM API example</a>
        </div>
        <div class="navbar-collapse collapse">
            <ul class="nav navbar-nav">
                <li class="active"><a href="/api-example/wcmPage.jsp">Home</a></li>
                <li><a href="/portal/classic/magazine">Portal</a></li>
                <li class="dropdown">
                    <a href="/portal/classic/magazine" class="dropdown-toggle" data-toggle="dropdown">TicketMonster's Magazine <b class="caret"></b></a>
                    <ul class="dropdown-menu">
                        <li><a href="/portal/classic/magazine/concert">Concert</a></li>
                        <li><a href="/portal/classic/magazine/theatre">Theatre</a></li>
                        <li><a href="/portal/classic/magazine/sporting">Sporting</a></li>
                    </ul>
                </li>
            </ul>
        </div><!--/.navbar-collapse -->
    </div>
</div>

<%
    /*
        Access to GateIn WCM API
     */
    String user = "anonymous";
    WcmApiService wcm = WcmApi.getInstance();
    Category cat = wcm.findCategory("/Events/Top Events", user);
    List<Post> topEvents = null;
    if (cat != null) {
        topEvents = wcm.findPosts(cat.getId(), Post.PUBLISHED, user);
    }
    if (topEvents != null) {

        if (topEvents.size() > 0) {
            Post top = topEvents.get(0);
%>
<!-- Main jumbotron for a primary marketing message or call to action -->
<div class="jumbotron">
    <div class="container">
        <h1><%= top.getTitle() %></h1>
        <p><img src="<%= WcmUtils.extractSrcImg(top.getContent(), 0) %>" class="top-img"><%= top.getExcerpt() %></p>
        <p><a class="btn btn-primary btn-lg" href="wcmDetail.jsp?id=<%= top.getId() %>">Read more &raquo;</a></p>
    </div>
</div>
<%
        }
        if (topEvents.size() > 1) {
%>
<div class="container">
    <!-- Example row of columns -->
    <!--
    <div class="row">
    -->
        <%
            for (int i = 1; i<topEvents.size(); i++) {
                Post event = topEvents.get(i);
        %>
        <div class="col-lg-4">
            <h2><%= event.getTitle() %></h2>
            <p><img src="<%= WcmUtils.extractSrcImg(event.getContent(), 0) %>" class="event-img"></p>
            <p><%= event.getExcerpt() %> </p>
            <p><a class="btn btn-default" href="wcmDetail.jsp?id=<%= event.getId() %>">Read details &raquo;</a></p>
        </div>
        <%
            }
        %>
    <!--
    </div>
    -->
    <hr>

    <footer>
        <p>&copy; TicketMonster's Magazine 2013</p>
    </footer>
</div> <!-- /container -->
<%
        }

    }
%>

<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="js/jquery.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>

images/author/download/attachments/75137367/wcm-5.5-2.png

wcmDetail.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="org.gatein.wcm.api.WcmApi" %>
<%@ page import="org.gatein.wcm.api.services.WcmApiService" %>
<%@ page import="org.gatein.wcm.api.domain.Category" %>
<%@ page import="org.gatein.wcm.api.domain.Post" %>
<%@ page import="java.util.List" %>
<%@ page import="org.gatein.wcm.api.util.WcmUtils" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>GateIn WCM API example using Bootstrap</title>

    <!-- Bootstrap core CSS -->
    <link href="css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="css/jumbotron.css" rel="stylesheet">

</head>

<body>

<div class="navbar navbar-inverse navbar-fixed-top">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="/api-example/wcmPage.jsp">GateIn WCM API example</a>
        </div>
        <div class="navbar-collapse collapse">
            <ul class="nav navbar-nav">
                <li class="active"><a href="/api-example/wcmPage.jsp">Home</a></li>
                <li><a href="/portal/classic/magazine">Portal</a></li>
                <li class="dropdown">
                    <a href="/portal/classic/magazine" class="dropdown-toggle" data-toggle="dropdown">TicketMonster's Magazine <b class="caret"></b></a>
                    <ul class="dropdown-menu">
                        <li><a href="/portal/classic/magazine/concert">Concert</a></li>
                        <li><a href="/portal/classic/magazine/theatre">Theatre</a></li>
                        <li><a href="/portal/classic/magazine/sporting">Sporting</a></li>
                    </ul>
                </li>
            </ul>
        </div><!--/.navbar-collapse -->
    </div>
</div>

<%
    /*
        Access to GateIn WCM API
     */
    String id = request.getParameter("id");
    String user = "anonymous";
    WcmApiService wcm = WcmApi.getInstance();
    if (id != null) {
        Post detail = wcm.findPost(new Long(id), user);
%>
<!-- Main jumbotron for a primary marketing message or call to action -->
<div class="jumbotron">
    <div class="container">
        <h1><%= detail.getTitle() %></h1>
        <p><img src="<%= WcmUtils.extractSrcImg(detail.getContent(), 0) %>" class="top-img"><%= detail.getExcerpt() %></p>
    </div>
</div>

<div class="container">
    <%
        String skipImg = WcmUtils.extractImg(detail.getContent(), 0);
        String content = detail.getContent();
        if (!"".equals(skipImg)) {
            content = content.replace(skipImg, "");
        }
    %>
    <p><%= content %></p>

    <hr>

    <footer>
        <p>&copy; TicketMonster's Magazine 2013</p>
    </footer>
</div>
<%
    }
%>
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="js/jquery.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
JBoss.org Content Archive (Read Only), exported from JBoss Community Documentation Editor at 2020-03-10 13:43:26 UTC, last content change 2014-02-24 17:05:44 UTC.