Extending the Permissions Scheme with Wrapper Classes

When extending a portlet’s permissions scheme, it’s best practice to create wrapper classes that extend your model entity wrappers. This gives your permissions methods a unique place to reside. By doing this, each entity of your guestbook is wrapped with permissions you add to wrapper classes. This enables you to easily create actions and permissions for each entity.

You may have noticed when you were creating your guestbook and entry managed beans that the com.liferay.docs.guestbook.model.Guestbook and com.liferay.docs.guestbook.model.Entry objects were used to create and manage your guestbook and entry entities. The wrapper classes you create in this section will replace those classes, making permissions resources available in your managed beans. Before updating your managed beans, you’ll need to create the wrapper classes first. You’ll start with creating the Guestbook wrapper class.

  1. In the Guestbook portlet’s docroot/WEB-INF/src directory, add a new package named com.liferay.docs.guestbook.wrappers.

  2. Right-click the com.liferay.docs.guestbook.wrappers package and select NewClass. Give it the name Guestbook and in the Superclass field, browse for the GuestbookWrapper class. Then click Finish.

    The GuestbookWrapper class was generated by Service Builder, and implements the guestbook model you were referring to previously.

  3. You’ll need to define an explicit constructor since the implicit super constructor GuestbookWrapper() is undefined for the default constructor. Add the following constructor:

     public Guestbook(com.liferay.docs.guestbook.model.Guestbook guestbook) {
  4. Hover your mouse over the Guestbook class name and you’ll notice a few options appear. Select the Add generated serial version ID. The following variable with a similar ID is added to your class:

     private static final long serialVersionUID = -420986486105631030L;

    Since this class is Serializable, it’s best practice to create a serial version ID. Serialization translates the class’ state into a format that can be stored and rebuilt on your local machine or across a network to other computer environments.

Your Guestbook wrapper class is created. Although it is very bare bones at the moment, this class gives a good starting place for additional permissions-related methods to reside. Since you now want your guestbook bean to use the Guestbook wrapper class instead of the Guestbook model class, you’ll need to make a few adjustments in your managed bean.

  1. In the GuestbookBacking bean, replace the import com.liferay.docs.guestbook.model.Guestbook; statement with import com.liferay.docs.guestbook.wrappers.Guestbook;.

    You’ll notice error markings appear throughout the class. Some error markings in your class are related to using a -Util class. These code statements need to be adjusted to work with your new wrapper class, which you’ll do next.

  2. Wrap each error marked -Util call with new Guestbook(...). For example, GuestbookUtil.create(0L); should look like new Guestbook(GuestbookUtil.create(0L));.

    Once you finish doing this to all the -Util calls, you’ll have two remaining errors markings, which will require similar changes.

  3. In the getGuestbooks() method, replace guestbooks.add(guestbook); with guestbooks.add(new Guestbook(guestbook));.

  4. In the getSelectedGuestbook() method, replace the following:

     selectedGuestbook = firstGuestbookByName;


     selectedGuestbook = new Guestbook(firstGuestbookByName);

Awesome! Your guestbook bean is now using your new Guestbook wrapper class!

Next, you’ll create the Entry wrapper class, which will accomplish the same things as the Guestbook wrapper class, but for entries.

  1. Right-click the com.liferay.docs.guestbook.wrappers package and select NewClass. Give it the name Entry and in the Superclass field, browse for the EntryWrapper class. Then click Finish.

  2. Define the explicit constructor, similarly to what you did in the Guestbook class:

     public Entry(com.liferay.docs.guestbook.model.Entry entry) {
  3. Generate the serialVersionUID by hovering your mouse over the class name and selecting Add generated serial version ID.

  4. Open the EntryBacking bean and replace the import com.liferay.docs.guestbook.model.Entry; statement with import com.liferay.docs.guestbook.wrappers.Entry;. Repeat this step for the GuestbookBacking bean, as well.

  5. Just as you did previously, you’ll need to modify a few code statements to resolve errors dealing with switching to the Entry wrapper class from the model class. First, in the EntryBacking bean’s add() method, replace Entry entry = EntryUtil.create(0L); with the following:

     Entry entry = new Entry(EntryUtil.create(0L));
  6. In the getEntries() method of the GuestbookBacking bean, replace entries.add(entry); with entries.add(new Entry(entry));.

You’ve successfully migrated your backing beans from using the model entity to a wrapper entity! Now each instance of your guestbook and entry entities will be wrapped with any permissions configured in your new wrapper classes. To take advantage of your new wrapper classes, you’ll add permissions methods to them relating to each entity. In the next section, you’ll test out the extended permissions scheme by adding a permission to your wrapper classes.

« Adding Permissions Resources to the Service LayerUpdating the UI with Extended Permissions Scheme »
Was this article helpful?
1 out of 1 found this helpful