Minimizing the ext environment

There has been a lot of work trying to minimize the use of the ext environment to keep your site more maintainable and easier to customize. Here are some tips that has helped me keep my ext small and more maintainable/manageable which will also make upgrades more smooth.

 

Move anything possible in ext to hooks.

1. Do you need to override a *ServiceImpl.java class? (v6.0 or later)

For example, to override the UserLocalServiceImp class put this in your liferay-hook.xml

<hook>
    <service>
        <service-type>com.liferay.portal.service.UserLocalService</service-type>
        <service-impl>com.liferay.testhook.hook.service.impl.TestUserLocalServiceImpl</service-impl>
    </service>
</hook>

Then in your class just extend the UserLocalServiceWrapper and override any methods you want.

public class TestUserLocalServiceImpl extends UserLocalServiceWrapper {

Check out the test-hook portlet located in svn://svn.liferay.com/repos/public/plugins/trunk/portlets/test-hook-portlet/ for more info.

 

2. Do you need to override any classes listed in the liferay-hook.dtd or do you need to add event actions?

For example, do you need to override the ScreennameValidator, ScreennameGenerator, Document library hook, or add postLoginActions, servicePreActions, etc.? These can be overriden through portal.properties in a hook and even allow hot deploy overriding! That means you don't need to restart tomcat to test a new change, you can just redeploy your hook. See the full list in lifeary-hook_6_0_0.dtd. New properties will continually be added so keep an eye for new ways to minimize your ext.

 

Use custom attributes.

1. Do you need to customize user attributes?

Instead of modifying service.xml to add a new column and regenerating all the services, use a custom attribute. There is practically no performance difference, String attributes (only string!) are indexed, and you can do it through the admin GUI. One thing you need to keep in mind is to give view or update permissions apprpriately.

They are even picked up automatically in the create account form! You don't even need to modify any java classes to add a new attribute for registering. Just create a JSP hook and add this easy taglib to the create_account.jsp.

        <liferay-ui:custom-attribute
            className="com.liferay.portal.model.User"
            classPK="<%= 0 %>"
            editable="<%= true %>"
            label="<%= true %>"
            name="favoriteColor"
        />

 In the action class, it will pick any custom attributes and update them accordingly. The same is true for most all other models too (ie Documents, Images, Web content).

 

Extend rather than override.

1. Do you need to override any *Action.java classes?

Any classes in struts-config.xml or liferay-portlet.xml should be extended rather than overrided. This will hopefully make upgrading less painfulby keeping the current code and just adding your own or tweaking variables so you get the behaviour you want.

For example, say you created a custom attribute "Favorite Color" and you want to make it required to be filled out.


<struts-config>
    <action-mappings>
        <action path="/login/create_account" type="com.liferay.test.ext.portlet.login.action.TestCreateAccountAction">
            <forward name="portlet.login.create_account" path="portlet.login.create_account" />
        </action>
</struts-config>

Then in your class:

public class TestCreateAccountAction extends CreateAccountAction {
    protected void addUser(ActionRequest actionRequest, ActionResponse actionResponse)
        throws Exception {

        Map<String, Serializable> expandoBridgeAttributes =
            PortalUtil.getExpandoBridgeAttributes(
                ExpandoBridgeFactoryUtil.getExpandoBridge(User.class.getName()), actionRequest);

         String favoriteColor = (String)expandoBridgeAttributes.get("favoriteColor");

         if (Validator.isNull(favoriteColor)) {
                throw new RequiredFieldException("favoriteColor", "favoriteColorLabel"); // v6.0/EE specific code
         
         }

        super.addUser(actionRequest, actionResponse);  // Don't touch current code to make upgrades painless =)
    }
}

 

2. Do you need to override JSPs?

Use a JSP hook. (See www.liferay.com/community/wiki/-/wiki/Main/Portal+Hook+Plugins)

And use the buffer util taglib to achieve a similar affect to what I did with CreateAccountAction.java class above. Let's say we want to remove the javascript on the bottom, change the word "save" to "create" and add the custom attribute favorite color to the registration form after the last name.

 

<%@ include file="/html/portlet/login/init.jsp" %>

<liferay-util:buffer var="html">
    <liferay-util:include page="/html/portlet/login/create_account.portal.jsp" />
</liferay-util:buffer>

<liferay-util:buffer var="customHtml">
   <liferay-ui:custom-attribute
        className="com.liferay.portal.model.User"
        classPK="<%= 0 %>"
        editable="<%= true %>"
        label="<%= true %>"
        name="favoriteColor"
    />
</liferay-util:buffer>

<%
int x = html.lastIndexOf("<script type=\"text/javascript\"");

if (x != -1) {
    y = html.indexOf("</script>", x);

    html = html.substring(0, x) + html.substring(y + 9);
}

html = html.replace(LanguageUtil.get(pageContext, "save"), LanguageUtil.get(pageContext, "create"));

x = html.indexOf(LanguageUtil.get(pageContext, "last-name"));

if (x != -1) {
    y = html.indexOf("</div>", x);

    html = html.substring(0, y) + customHtml + html.substring(y);
}
%>

<%= html %>

 

Conclusion

Hopefully that was useful to you. Of course, this isn't always possible and you'll have to resort to direct overwriting. But I hope this can minimize the pain of upgrading for some people.

If there are commonly overrided classes, let's see if we can figure out ways to make it easier, make it possible to use a technique mentioned above, or be moved to a hook =). Or...contribute to trunk if it's useful, that way we maintain it for you and others also benefit from it.

If you guys have any other methods you like to use or if I missed anything, please leave a comment.

Blogs
Very nice article emoticon

I was trying to use your example but the class "UserLocalServiceWrapper" or "UserWrapper" are not on the liferay distribution (5.2.3), only on SVN, is it possible to use this with the official distribution?

Best Regards,
Joao Sequeira
Hi Amos,
Thanks for the article I have couple of questions. I am using 5.2.2 in the first half it says about using plugin for extending the user attributes. When I added that in hooks I am getting error about service tags.
Secondly in later part it talks about changing struts xml. Does it need to be done in plugin/hooks?
Please let me know.
Thanks
Hi Anand,

Making changes to struts-config.xml has to be done in ext environment.
Hi Team,

You need to edit the "struts-config-ext.xml" file to point to your custom Action class.
Assuming Liferay is deployed in "D:\liferay-portal-6.0.5" folder. A blank "struts-config-ext.xml" can be found at
D:\liferay-portal-6.0.5\tomcat-6.0.26\webapps\ROOT\WEB-INF\ folder
<action path="/login/create_account" type="ae.rak.hook.portlet.login.action.MyCreateAccountAction">
<forward name="portlet.login.create_account" path="portlet.login.create_account" />
</action>

Next add the custom action class into the classes folder of the portal.
D:\liferay-portal-6.0.5\tomcat-6.0.26\webapps\ROOT\WEB-INF\classes

It is easy to compile your custom Action java file.
1. Install Liferay IDE
2. Create a new 'Liferay Hook Plug-in Project'
3. Create your custom Action java file & compile
The IDE provides all help in compiling your .java file to a .class file
Hi Deepak,

Is there any way of doing this using the deploy process of a Hook Plug-in?
Hi Henrique,

I have tried overriding the action class with a hook plugin, but it seems to be problems with the classloader: http://www.liferay.com/community/forums/-/message_boards/message/7817764#_19_message_7817764

I am afraid that the EXT approach is going to be the best solution...

Hope it helps,

Luis
Nice article, thank you.

I would like to ask you, how is possible to use the extended User object (TestUserImpl) from the test-hook example in a JSP code - f.e. in the portlet/blogs/view.jsp.
It seems the JSP compiler does not see the class, so I cannot import it:

<%@ page import="com.liferay.testhook.hook.model.impl.TestUserImpl" %>

Only a type can be imported. com.liferay.testhook.hook.model.impl.TestUserImpl resolves to a package

Is there an easy way, how to do it?

Thanks.
Hello
if I want to override the AssetEntryLocalServiceImpl.updateEntry method in a hook, what I have to do?

Extending AssetEntryLocalServiceWrapper seems to be very tricky, because I have to substitute all the *persistence variables with corresponding *LocalServiceUtil class. And it still doesn't work, because how can I replace assetEntryPersistence.setAssetCategories? Does anyone help me?
Hi!

I'm triying to override a method in com.liferay.portal.service.LayoutLocalService.

I use this code in liferay-hook.xml:

<service>
<service-type>com.liferay.portal.service.LayoutLocalService</service-type>
<service-impl>com.prueba.portal.service.MyLayoutLocalService</service-impl>
</service>

and override the method in this class (MyLayoutLocalService) (it extends LayoutLocalServiceWrapper).

But when I deploy the hook and use the method, it doesn't use the new method and stills using the old one.

How can I made this work? Where could I find lifeary-hook_6_0_0.dtd?

Kind regards! and thank you for the blog entry!
Finally I found the problem, there were some code that was executed before tue function I wanted to override, once I remove this wrong code everithing works as expected.
Hi All,

I need some solution on user management module.

When we create new user in liferay it will go to user_ table in lportal database. But our requirement is we want to store the userdetails to be stored in our database as well.

I mean.. When we create new user, details should be stored in both the database (user_ table in lportal database and user table in our database).

Can anybody suggest where to write the code to do above task?
we are working in ext environment...

Thanks in advance....
Hi Prasana,

There a couple ways to do this, the best way I can think of is to create a model listener for add user and update user that will update your own database.
Hi Amos,
regarding the hint about overriding classes listed in liferay-hook.dtd, I suppose we can't change for example " login.events.post=com.liferay.portal.events.LoginPostAction " to " login.events.post=com.liferay.portal.events.MyOwnLoginPostAction "

in other words, we can override the properties, but the new classes that we need to use instead, like MyOwnLoginPostAction, must be added via ext, right? Overriding portal-impl or portal-service classes, can be done only via ext, except of the custom services that can be overridden by hook.
I got it, we can do it. It's like a list of classes that can be overridden no matter if they are portal-impl. It's good to know, thank you Amos
Amos Fong, Thanks for your reply...

I am very new to liferay Can you explain in detail?
There is a short tutorial in this wiki:

http://www.liferay.com/community/wiki/-/wiki/Main/Portal+Hook+Plugins

And there is a sample test hook here that has an example of a model listener:

http://svn.liferay.com/repos/public/plugins/trunk/portlets/test-hook-portlet/

user: guest, no password
Thanks Amos...

We are using openSSO ... we need to sync openSSO data with liferay data. Can you suggest some solution?
Hi Amos,
Can you give me some instructions to implement the custom services(which connects to a different database) in liferay action classes. Plz let me know the way to handle creating services through service builder and Spring and Hibernate configurations in the plugin hook.

thanks
prakash
In sample wrapper user of type SampleUserImpl is returned. How can it be accessed from other class??
Hello,

i use a hook to extend a jsp page and i will use in my jsp a code like this <%@page import="my.packahe.my.classes"%>
where can'i put my jar file?
Hi.

Does anyone know if it is possible to override a workflow handler via the hook mechanism, or does that need to be done via ext?

Thanks,
Pete
Hi Peter,
If i listen correctly , you want to override the default workflow thats available in liferay. You can override that by selecting the workflow from controlpanel and paste the XML document thats available in Kaleo-web\web-inf\src\meta-inf\definitions\single-approver.xml. Liferay by default provided single approver workflow , you can override how ever you want by adding multiple tasks and transitions.

And you can add a new workflow using controlpanel also , just go to workflow and click on add and upload the xml document to that ....

thanks
Prakash.
Thanks for your reply. Yeah, I've created my own workflow which is essentially the single approver workflow with an additional step added when the workflow is approved. I want to send an email notification out to all interested users that a new piece of content is available in the portal. In this case it happens to be a new document library document.

I want to include in the email a link to the new document. I'm struggling trying to build that URL given the data that is available in the velocity context that is available at the point that the email is generated.

I'd like to override the code that creates the starts the workflow so that the URL to the document (as well as some other data like the list of roles that have access to the folder in which the document lives) is pre-populated in the workflow context. The code I'd like to override is the DLFileEntryWorkflowHandler. This seemed like a good place to build the URL to the newly uploaded document given that the DLFileEntry is passed into the startWorkflowInstance method. The default behaviour of this method (inherited from BaseWorkflowHandler) is to ignore the the model object passed in. I'd like to override that method to do something useful like build the URL to the document and stick it in the workflowContext. I'm attempting to do this via the ext method of overriding but if it is at all possible to do this via a hook I would prefer that approach.

Pete
Thanks Amos, great post, really useful!!!

1. Do you need to override any *Action.java classes?

For doing this couldn't you create a hook pluging?

Thanks in advance,

Luis
Hi Amos
If I define new method in TestUserLocalServiceImpl, can I use it in other portlet made by Liferay Plugin SDK?
Can I use hook plugin to define new method in userService?
Thanks.
No you cannot define a new method using a hook because it doesn't override the Util class, only the Impl class. You should use ext to define a new method in a Liferay Service.
But it actually might be better to create your own service/class so there's better separation between your code and Liferay's.
[...] Shahin Ali: Can i change the sorting order of message thread posts in the message boards to 'last posted first' present order is first posted first using liferay 6 EE Thanks. Try a hook on the... [...] Read More
I'm wondering what is the best way to implement custom Store. I'd need an alternative to JcrStore. I'm not using JackRabbit but ModeShape. And I'm wondering if I should use Hook or Ext.
I implemented it with Ext, no other means possible I think...
@Jakub, do you mean another Document Library store using this property, (dl.store.impl)? I know it used to be possible and I think it should still be. Did you try specifying that property in your hook's portal.properties and making your store extend BaseStore?
I want to extend the EditPagesAction class of Lifreay I want to add a tab in look and feel section of edit_pages_look_and_feel.jsp.I want to know how I Can use hook here .

public class NPPEditPagesAction extends EditPagesAction {

public void processAction(ActionMapping mapping, ActionForm form,
PortletConfig portletConfig, ActionRequest actionRequest,
ActionResponse actionResponse) throws Exception {

System.out.println("Inside processAction...createTheme()");
String cmd = ParamUtil.getString(actionRequest, Constants.CMD);

try {
if (cmd.equals("createtheme")) {
createTheme(actionRequest);
}

super.processAction(mapping, form, portletConfig, actionRequest,actionResponse);

} catch (Exception e) {

}
}

public ActionForward render(ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
RenderRequest renderRequest, RenderResponse renderResponse)
throws Exception {

return super.render(mapping, form, portletConfig, renderRequest, renderResponse);
//return mapping.findForward(getForward(renderRequest, "portlet.communities.edit_pages"));

}

@SuppressWarnings("static-access")
public static void createTheme(ActionRequest actionRequest) {

System.out.println("Inside createTheme...");
ThemeUtil themeUtil = new ThemeUtil();
final String themeZipFilePath = ThemeUtil.SAMPLE_THEMES_PATH;
final String themeConfigFilePath = ThemeUtil.THEMES_CONFIG_PATH;
try{
//To Modify liferay-look-and-feel.xml
themeUtil.getXMLDetails(themeConfigFilePath);

//To Extract the sample themes zip folder to tomcat location
themeUtil.extractFolder(themeZipFilePath);

} catch (Exception ex) {
ex.printStackTrace();
}

}

<custom-jsp-dir>/WEB-INF</custom-jsp-dir>
<struts-action>
<struts-action-path>/layout_management/edit_pages</struts-action-path>
<struts-action-impl>com.liferay.portlet.communities.action.NPPEditPagesAction</struts-action-impl>
</struts-action>

I am unable to execute my java code.please let me know where I am going wrong
[...] The below blog may help on how to extend a services via hook. http://www.liferay.com/web/amos.fong/blog/-/blogs/minimizing-the-ext-environment Mark as an Answer [...] Read More
Hi Amos, how to make user can post comment ? In my liferay, error "Blogs in temporarily unavailable" when submit comment.
In your code you write:

throw new RequiredFieldException("favoriteColor", "favoriteColorLabel"); // v6.0/EE specific code

Is there a suitable exception in the Community Edition (6.1) ?
[...] This article will share two cool features - Hooks and Plugins Ext - in Liferay portal 6. Speciall thanks to Amos Fong, so that we could minimize the ext environment by using hooks. A lot of thanks to... [...] Read More
Hello Liferay Community,

I am very new to Liferay, and am trying to program a Hook using 6.1 that will override CreateAccountAction as shown in this post. Is it possible??? If not, in what type of Liferay project would one include the code shown in this post (i.e. ext, new portlet, etc.)
Thanks in advance for your response. I'm really stuck!

E.
@Elijah
Yes, you can override CreateAccountAction in hook now, not by methods in this post, but see this post:
http://www.liferay.com/web/mika.koivisto/blog/-/blogs/overriding-and-adding-struts-actions-from-hook-plugins
Hello Amos,

Thank you for your response.

I've read link on overriding struts actions several times. It is mentioned in the link that only two interfaces can be overridden: BaseStrutsPortletAction and BaseStrutsAction. Neither of these allows for directly extending CreateAccountAction, which is in portal-impl.jar.

I need to validate existing users on login by simply comparing their input with what is already in the database. I would like to extend and override CreateAccountAction.java to do this, as you have shown above in THIS post. To implement as above, where would I place the code, in a hook, ext? Your help would be greatly appreciated.

E.
Following this post, you would use ext plugin and edit struts-action-ext.xml to point to your own implementation.
Thanks, Amos, for your response. I may have to do my implementation as an ext plugin rather than a hook. I'm wondering, however, how what you have shown in this post minimizes the ext environment if it needs to be implemented as an ext anyway. Could you please clarify?
It may not be obvious that it is possible to extend struts action classes through struts-config-ext.xml rather than having override the entire class which is more work and less maintainable. So this was just a tip for those who weren't already aware of it.
It wasn't obvious to me, but I am new to Liferay, so that may be the reason why.
Is there a tutorial or working example I could reference on how to extend or access methods within a struts action class such as CreateAccountAction without overriding the entire class? I've reference the post from Mika Koivisto (http://www.liferay.com/web/mika.koivisto/blog/-/blogs/7132115) several times.... what am I missing?