掲示板

Generate static method with Liferay Service Builder

thumbnail
10年前 に Riccardo Rotondo によって更新されました。

Generate static method with Liferay Service Builder

Junior Member 投稿: 29 参加年月日: 10/06/11 最新の投稿
Dear All,

I'm working with liferay service builder and following the documentation I'm writing all the methods for a VirtualPath table in the class:

sb.model.impl.VirtualPathImpl

Everything works fine until I didn't try to implement a static method. Once I write:


public class VirtualPathImpl extends VirtualPathBaseImpl {
	/*
	 * NOTE FOR DEVELOPERS:
	 *
	 * Never reference this class directly. All methods that expect a virtual path model instance should use the {@link it.infn.ct.einfrsrv.sb.model.VirtualPath} interface instead.
	 */
	public VirtualPathImpl() {
	}
        
        public static VirtualPath exist (String path, long userId){
            VirtualPath vp = null;
            
            Log _log =LogFactoryUtil.getLog(VirtualPathImpl.class);
            try {
                DynamicQuery query = 
                        DynamicQueryFactoryUtil.forClass(VirtualPath.class)
                        .add(PropertyFactoryUtil.forName("virtualPath")
                        .eq(path))
                        .add(PropertyFactoryUtil.forName("userId")
                        .eq(new Long (userId)));
                List <virtualpath> queryVP = null;
                queryVP = VirtualPathLocalServiceUtil.dynamicQuery(query);
                
                if (queryVP.isEmpty()) {
                        _log.debug("VirtualPath '"+path+
                                "' not present for userId:"+
                                Long.toString(userId));
                }else{
                    vp = queryVP.get(0);
                    _log.debug("VirtualPath '"+vp.getVirtualPath()+"' found");
                }
                
            } catch (SystemException ex) {
                Logger.getLogger(VirtualPathImpl.class.getName()).
                        log(Level.SEVERE, null, ex);
            }
            return vp;
        }
        
}

</virtualpath>


The code for that static method exist is not being generated and I'm not able to reference it neither from sb.model.VirtualPath nor from sb.service.VirtualPathLocalServiceUtil

What am I doing wrong? Static method are supposed to be implemented somewhere else?

Thank you very much for you help.

Regards,

Riccardo
thumbnail
10年前 に David H Nebinger によって更新されました。

RE: Generate static method with Liferay Service Builder

Liferay Legend 投稿: 14914 参加年月日: 06/09/02 最新の投稿
Static methods are not supported.

Instead put the method into VirtualPathLocalServiceImpl and you can access it from VirtualPathLocalServiceUtil after you build services.

In general you should never put business logic into the entity implementations. Most of these types of methods would not be available in the generated VirtualPath interface and trying to call the method will result in an UnsupportedOperationException every time.
thumbnail
10年前 に Riccardo Rotondo によって更新されました。

RE: Generate static method with Liferay Service Builder

Junior Member 投稿: 29 参加年月日: 10/06/11 最新の投稿
Dear David,

thank you for your quick reply!

David H Nebinger:

Instead put the method into VirtualPathLocalServiceImpl and you can access it from VirtualPathLocalServiceUtil after you build services.


I already tried that and it didn't work. I tried again this morning, this time putting the method non-static, and in it generated the method correctly!

David H Nebinger:

In general you should never put business logic into the entity implementations. Most of these types of methods would not be available in the generated VirtualPath interface and trying to call the method will result in an UnsupportedOperationException every time.


With into entity implementations what do you mean? That I should not put method in VirtualPathImpl and it's more recommended to write them into VirtualPathLocalServiceImpl? But what if a method is needed both in local and remote interface? Should I implement the method twice?

Thank you very much for you help.

Regards,

Riccardo
thumbnail
10年前 に Amit Doshi によって更新されました。

RE: Generate static method with Liferay Service Builder

Liferay Master 投稿: 550 参加年月日: 10/12/29 最新の投稿
Hi Riccardo,


With into entity implementations what do you mean? That I should not put method in VirtualPathImpl and it's more recommended to write them into VirtualPathLocalServiceImpl? But what if a method is needed both in local and remote interface? Should I implement the method twice?


VirtualPathImpl, VirtualPathLocalServiceImpl and VirtualPathServiceImpl are all different.

VirtualPathImpl - As david said, there should not be any bussiness logic in that class. It was the extended model implementation for the Virtual path service. Represents a row in the virtualpath service database table, with each column mapped to a property of this class.

VirtualPAthLocalServiceImpl - For local Service, In this class where you have to write your business logic & methods and re-run the service and access it using VirtualPathLocalServiceUtil.

VirtualPathServiceImpl - For remote Service, In this class where you have to write your business logic & methods and re-run the service and access it using VirtualPathServiceUtil.


But what if a method is needed both in local and remote interface? Should I implement the method twice?


Yes . you need to implement that in both.
thumbnail
10年前 に David H Nebinger によって更新されました。

RE: Generate static method with Liferay Service Builder

Liferay Legend 投稿: 14914 参加年月日: 06/09/02 最新の投稿
Riccardo Rotondo:
With into entity implementations what do you mean? That I should not put method in VirtualPathImpl and it's more recommended to write them into VirtualPathLocalServiceImpl? But what if a method is needed both in local and remote interface? Should I implement the method twice?


Nope, take a queue from how Liferay structures their remote service implementations. Most of them, you'll see, invoke the permission checker stuff and then pass through to the local service implementation.

This way you're only implementing once but reusing for both local and remote.