Foros de discusión

Hook for EditArticleAction

thumbnail
Gerhard Horatschek, modificado hace 9 años.

Hook for EditArticleAction

Junior Member Mensajes: 86 Fecha de incorporación: 20/03/09 Mensajes recientes
Hi all,

A question regarding making a hook via the PluginsSDK for the EditArticleAction (struts-action) for articles (journal).

Like mentioned in the Liferay Wiki it is possible to make hooks for EventHandles, Services and model listener by specifing this in the liferay-hook.xml.
In some other Wiki/Blog entry I found that it is possible to overwrite struts-actions:

<hook>
<struts-action>
    <struts-action-path>/journal</struts-action-path>
    <struts-action-impl>
        com.liferay.portlet.journal.action.EditArticleAction
    </struts-action-impl>
</struts-action>
</hook>

But after deploying this hook, the original EditArticleAction class from Liferay is still used.
What could be the reason? Any ideas?

Thank you!
Regards
thumbnail
Vipin Bardia, modificado hace 9 años.

RE: Hook for EditArticleAction

Regular Member Mensajes: 162 Fecha de incorporación: 28/02/11 Mensajes recientes
Hi Gerhard,

If you want to override EditArticleAction then your xml should looks like this -


<struts-action>
        <struts-action-path>/journal/edit_article</struts-action-path>
        <struts-action-impl>com.custom.NewJournal</struts-action-impl>
</struts-action>  


Java class NewJournal


public class NewJournal extends BaseStrutsPortletAction {
	@Override
	public String render(StrutsPortletAction originalStrutsPortletAction,
			PortletConfig portletConfig, RenderRequest renderRequest,
			RenderResponse renderResponse) throws Exception {
		// Your Custom Code
		return originalStrutsPortletAction.render(null, portletConfig, renderRequest,
				renderResponse);
	}
}



Regards.
thumbnail
Gerhard Horatschek, modificado hace 9 años.

RE: Hook for EditArticleAction

Junior Member Mensajes: 86 Fecha de incorporación: 20/03/09 Mensajes recientes
Thank you!

Ok, but I want to override the whole action class and add some methods in the code:


	<struts-action>
		<struts-action-path>/journal/edit_article</struts-action-path>
		<struts-action-impl>com.company.CustomEditArticleAction</struts-action-impl>
	</struts-action>


public class CustomEditArticleAction extends PortletAction {

	public static final String VERSION_SEPARATOR = "_version_";
	private static Log _log = LogFactoryUtil.getLog(EditArticleAction.class);
	
	@Override
	public void processAction(
			ActionMapping actionMapping, ActionForm actionForm,
			PortletConfig portletConfig, ActionRequest actionRequest,
			ActionResponse actionResponse)
		throws Exception {
               ........
	}

	@Override
	public ActionForward render(
			ActionMapping actionMapping, ActionForm actionForm,
			PortletConfig portletConfig, RenderRequest renderRequest,
			RenderResponse renderResponse)
		throws Exception {
               ........
	}

	protected String myOwnMethod(String content) {
               ........
	}

}


I can configure this, but there are compiler errors:

    [javac] 4. ERROR in ...\liferay-plugins-sdk-6.2.0\hooks\XYZ-hook\docroot\WEB-INF\src\com\company\CustomEditArticleAction.java (at line 70)
    [javac] 	import com.liferay.portal.struts.PortletAction;
    [javac] 	       ^^^^^^^^^^^^^^^^^^^^^^^^^
    [javac] The import com.liferay.portal.struts cannot be resolved


Classpath of the hook contains the path of the portal (<classpathentry kind="src" path="/portal-master"/>).
How can I fix this?
thumbnail
Vipin Bardia, modificado hace 9 años.

RE: Hook for EditArticleAction

Regular Member Mensajes: 162 Fecha de incorporación: 28/02/11 Mensajes recientes
Hi Gerhard,

You have to extend BaseStrutsPortletAction when you override any portlet struts action.

Use this link for better understanding - https://www.liferay.com/documentation/liferay-portal/6.1/development/-/ai/lp-6-1-dgen06-overriding-and-adding-struts-actions-0

Regards.
thumbnail
Gerhard Horatschek, modificado hace 9 años.

RE: Hook for EditArticleAction

Junior Member Mensajes: 86 Fecha de incorporación: 20/03/09 Mensajes recientes
Hi,

I have tried to overwrite another portlet action - the EditFileEntryAction of the document library - and again there are problems.

liferay-hook.xml

    <struts-action>
        <struts-action-path>/document_library/edit_file_entry</struts-action-path>
        <struts-action-impl>com.company.documentlibrary.action.CustomEditFileEntryAction</struts-action-impl>
    </struts-action>


Code:

package com.company.documentlibrary.action;
public class CustomEditFileEntryAction extends BaseStrutsPortletAction {

	public static final String TEMP_RANDOM_SUFFIX = "--tempRandomSuffix--";


	public void processAction(
			StrutsPortletAction originalStrutsPortletAction,
			ActionMapping actionMapping, ActionForm actionForm,
			PortletConfig portletConfig, ActionRequest actionRequest,
			ActionResponse actionResponse)
		throws Exception {

		originalStrutsPortletAction.processAction(null, portletConfig, actionRequest, actionResponse);
		
	}


	public String render(
			StrutsPortletAction originalStrutsPortletAction,
			ActionMapping actionMapping, ActionForm actionForm,
			PortletConfig portletConfig, RenderRequest renderRequest,
			RenderResponse renderResponse)
		throws Exception {

		return originalStrutsPortletAction.render(
	            null, portletConfig, renderRequest, renderResponse);
	}
}


causes following error when I try to add a document in the document and media portlet:

ERROR [RuntimePageImpl-10][PortletRequestProcessor:466] Forward does not exist

On the screen I see an empty portlet ...

What could be the problem?
I think I implement the hook like described in the documentation ...
Any response or hint would be appreciated!
Thank you!
Best regards
thumbnail
Mayur Patel, modificado hace 9 años.

RE: Hook for EditArticleAction

Expert Mensajes: 358 Fecha de incorporación: 17/11/10 Mensajes recientes
Hi,

Can you please try by passing originalStrutsPortletAction in both action and render method.

originalStrutsPortletAction.processAction(originalStrutsPortletAction, portletConfig, actionRequest, actionResponse);

originalStrutsPortletAction.render(originalStrutsPortletAction, portletConfig, renderRequest, renderResponse);

Also, have a look at below blog,

https://www.liferay.com/web/mika.koivisto/blog/-/blogs/7132115

Thanks.
thumbnail
Gerhard Horatschek, modificado hace 9 años.

RE: Hook for EditArticleAction

Junior Member Mensajes: 86 Fecha de incorporación: 20/03/09 Mensajes recientes
Hi,

I found the reason: the parameters actionMapping and actionForm causes the troubles, so when I remove them the hook was registered well.

Next I want to overwrite the updateFileEntry function by adding following code:

public FileEntry updateFileEntry(
		StrutsPortletAction originalStrutsPortletAction,
		PortletConfig portletConfig, ActionRequest actionRequest,
		ActionResponse actionResponse)
	throws Exception {
String title = ParamUtil.getString(uploadPortletRequest, "title");
..
..
title = "TitleFromHook";
..
}


But when adding a document via the document and media portlet the funtion is not called (I do not reach the function via debugging and the title is not from my code).

Any ideas?
Thank you!
Best regards
thumbnail
Mayur Patel, modificado hace 9 años.

RE: Hook for EditArticleAction

Expert Mensajes: 358 Fecha de incorporación: 17/11/10 Mensajes recientes
Hi,

May be this one can help.

Best Practice

When overriding an existing Struts action, it’s usually best to override the method that takes the original Struts action handle as a parameter and execute that original Struts action. Think of the original action as a servlet filter or aspect. If you override the method that takes the original action handle as a parameter and don’t explicitly execute it, the original action won’t be executed. If you override the execute method that does not take the original action as a parameter, you are ignoring the original action and it won’t be executed.

From the below link:

https://www.liferay.com/documentation/liferay-portal/6.1/development/-/ai/lp-6-1-dgen06-overriding-and-adding-struts-actions-0

Thanks.