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.
-
In the Guestbook portlet’s
docroot/WEB-INF/src
directory, add a new package namedcom.liferay.docs.guestbook.wrappers
. -
Right-click the
com.liferay.docs.guestbook.wrappers
package and select New → Class. Give it the nameGuestbook
and in the Superclass field, browse for theGuestbookWrapper
class. Then click Finish.The
GuestbookWrapper
class was generated by Service Builder, and implements the guestbook model you were referring to previously. -
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) { super(guestbook); }
-
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.
-
In the
GuestbookBacking
bean, replace theimport com.liferay.docs.guestbook.model.Guestbook;
statement withimport 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. -
Wrap each error marked
-Util
call withnew Guestbook(...)
. For example,GuestbookUtil.create(0L);
should look likenew 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. -
In the
getGuestbooks()
method, replaceguestbooks.add(guestbook);
withguestbooks.add(new Guestbook(guestbook));
. -
In the
getSelectedGuestbook()
method, replace the following:selectedGuestbook = firstGuestbookByName;
with:
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.
-
Right-click the
com.liferay.docs.guestbook.wrappers
package and select New → Class. Give it the nameEntry
and in the Superclass field, browse for theEntryWrapper
class. Then click Finish. -
Define the explicit constructor, similarly to what you did in the
Guestbook
class:public Entry(com.liferay.docs.guestbook.model.Entry entry) { super(entry); }
-
Generate the
serialVersionUID
by hovering your mouse over the class name and selecting Add generated serial version ID. -
Open the
EntryBacking
bean and replace theimport com.liferay.docs.guestbook.model.Entry;
statement withimport com.liferay.docs.guestbook.wrappers.Entry;
. Repeat this step for theGuestbookBacking
bean, as well. -
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 theEntryBacking
bean’sadd()
method, replaceEntry entry = EntryUtil.create(0L);
with the following:Entry entry = new Entry(EntryUtil.create(0L));
-
In the
getEntries()
method of theGuestbookBacking
bean, replaceentries.add(entry);
withentries.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.