I am facing the problem, while adding custom method in EXT. Here is my code,
public class MyBlogsEntryServiceImpl extends BlogsEntryServiceImpl{
protected static final String DISPLAY_STYLE_VIEWS = "views";
protected static final String DISPLAY_STYLE_AVATAR = "avatar";
public List<BlogsEntry> getCompanyEntriesByViews(
long companyId, Date displayDate, int status, int max)
throws PortalException, SystemException {
long blogsEntryClassNameId = ClassNameLocalServiceUtil.getClassNameId(BlogsEntry.class);
DynamicQuery blogsEntriesQuery = DynamicQueryFactoryUtil.forClass(BlogsEntry.class, "BlogsEntry")
.setProjection(ProjectionFactoryUtil.property("BlogsEntry.entryId"))
.add(PropertyFactoryUtil.forName("BlogsEntry.companyId").eq(companyId));
DynamicQuery mostViewedAssetsQuery = DynamicQueryFactoryUtil.forClass(AssetEntry.class, "AssetEntry")
.add(PropertyFactoryUtil.forName("AssetEntry.classPK").in(blogsEntriesQuery))
.add(PropertyFactoryUtil.forName("AssetEntry.classNameId").eq(blogsEntryClassNameId))
.addOrder(OrderFactoryUtil.desc("AssetEntry.viewCount"));
if (max > 0) {
mostViewedAssetsQuery.setLimit(0, max);
}
List<AssetEntry> assetEntries = AssetEntryLocalServiceUtil.dynamicQuery(mostViewedAssetsQuery);
List<BlogsEntry> blogsEntries = new ArrayList<BlogsEntry>(assetEntries.size());
for (AssetEntry asset : assetEntries) {
try {
BlogsEntry entry = blogsEntryLocalService.getBlogsEntry(asset.getClassPK());
if (BlogsEntryPermission.contains(getPermissionChecker(), entry, ActionKeys.VIEW)) {
blogsEntries.add(entry);
}
} catch (Exception e) {
_log.warn("Cannot find BlogsEntry with id " + asset.getClassPK());
}
}
return blogsEntries;
}
}
In the above code I got exception for the getCompanyEntriesByViews() method, Here is error log
05:22:29,156 INFO [localhost-startStop-1][DialectDetector:71] Determine dialect for MySQL 5
05:22:29,281 INFO [localhost-startStop-1][DialectDetector:136] Found dialect org.hibernate.dialect.MySQLDialect
Starting Liferay Portal Community Edition 6.2 CE GA2 (Newton / Build 6201 / March 20, 2014)
05:22:52,093 INFO [localhost-startStop-1][BaseDB:484] Database does not support case sensitive queries
05:22:52,546 INFO [localhost-startStop-1][ServerDetector:119] Server supports hot deploy
05:22:52,546 INFO [localhost-startStop-1][PluginPackageUtil:1013] Reading plugin package for the root context
05:25:23,250 INFO [localhost-startStop-1][AutoDeployDir:139] Auto deploy scanner started for E:\liferay62-migration\liferay-portal-6.2-ce-ga2\deploy
05:25:23,828 ERROR [localhost-startStop-1][JSONWebServiceRegistrator:96] java.lang.IllegalArgumentException: java.lang.NoSuchMethodException: com.sun.proxy.$Proxy153.getCompanyEntriesByViews(long, java.util.Date, int, int)
java.lang.IllegalArgumentException: java.lang.NoSuchMethodException: com.sun.proxy.$Proxy153.getCompanyEntriesByViews(long, java.util.Date, int, int)
at com.liferay.portal.jsonwebservice.JSONWebServiceActionConfig.<init>(JSONWebServiceActionConfig.java:79)
at com.liferay.portal.jsonwebservice.JSONWebServiceActionsManagerImpl.registerJSONWebServiceAction(JSONWebServiceActionsManagerImpl.java:269)
at com.liferay.portal.kernel.jsonwebservice.JSONWebServiceActionsManagerUtil.registerJSONWebServiceAction(JSONWebServiceActionsManagerUtil.java:92)
at com.liferay.portal.jsonwebservice.JSONWebServiceRegistrator.registerJSONWebServiceAction(JSONWebServiceRegistrator.java:261)
at com.liferay.portal.jsonwebservice.JSONWebServiceRegistrator.onJSONWebServiceBean(JSONWebServiceRegistrator.java:195)
at com.liferay.portal.jsonwebservice.JSONWebServiceRegistrator.processBean(JSONWebServiceRegistrator.java:93)
at com.liferay.portal.jsonwebservice.JSONWebServiceRegistrator.processAllBeans(JSONWebServiceRegistrator.java:68)
at com.liferay.portal.jsonwebservice.JSONWebServiceActionsManagerImpl.registerServletContext(JSONWebServiceActionsManagerImpl.java:324)
at com.liferay.portal.kernel.jsonwebservice.JSONWebServiceActionsManagerUtil.registerServletContext(JSONWebServiceActionsManagerUtil.java:102)
at com.liferay.portal.events.GlobalStartupAction.run(GlobalStartupAction.java:317)
at com.liferay.portal.events.EventsProcessorImpl.processEvent(EventsProcessorImpl.java:108)
at com.liferay.portal.events.EventsProcessorImpl.process(EventsProcessorImpl.java:59)
at com.liferay.portal.events.EventsProcessorUtil.process(EventsProcessorUtil.java:32)
at com.liferay.portal.servlet.MainServlet.processGlobalStartupEvents(MainServlet.java:1038)
at com.liferay.portal.servlet.MainServlet.init(MainServlet.java:314)
at javax.servlet.GenericServlet.init(GenericServlet.java:160)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5176)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5460)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:656)
at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1635)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NoSuchMethodException: com.sun.proxy.$Proxy153.getCompanyEntriesByViews(long, java.util.Date, int, int)
at java.lang.Class.getMethod(Unknown Source)
at com.liferay.portal.jsonwebservice.JSONWebServiceActionConfig.<init>(JSONWebServiceActionConfig.java:73)
... 31 more
I have some doubt. 1. Can we add our custom method in EXT ? 2. if yes, then what is the solution of above issue.
Please give me suggestion.
I think the problem here is that Liferay will return an Instance of BlogsEntryServiceImpl and not of MyBlogsEntryServiceImpl. Therefore the NoSuchMethodException is risen. You should implement the method in BlogsEntryServiceImpl and not inherit from the class. Another thing here is, that you are trying to invoke the Method via a Proxy Object. So it's not clear on what actual Implementation you are invoking the method. Also you should consider if a hook approach would't be sufficent. An advice we always give in the forum is: Only use EXT if there REALLY REALLY is no other way than that. Maybe you take a look here:
https://www.liferay.com/de/documentation/liferay-portal/6.2/development/-/ai/override-a-portal-service-with-hook-liferay-portal-6-2-dev-guide-en
If you can achive the desired change via a hook, you should definatly go this way, as this will not break your update capabilities of your portal.