Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Riccardo Rotondo
Generate static method with Liferay Service Builder
April 19, 2013 8:04 AM
Answer

Riccardo Rotondo

Rank: Junior Member

Posts: 29

Join Date: June 11, 2010

Recent Posts

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:

 1
 2public class VirtualPathImpl extends VirtualPathBaseImpl {
 3    /*
 4     * NOTE FOR DEVELOPERS:
 5     *
 6     * 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.
 7     */
 8    public VirtualPathImpl() {
 9    }
10       
11        public static VirtualPath exist (String path, long userId){
12            VirtualPath vp = null;
13            
14            Log _log =LogFactoryUtil.getLog(VirtualPathImpl.class);
15            try {
16                DynamicQuery query =
17                        DynamicQueryFactoryUtil.forClass(VirtualPath.class)
18                        .add(PropertyFactoryUtil.forName("virtualPath")
19                        .eq(path))
20                        .add(PropertyFactoryUtil.forName("userId")
21                        .eq(new Long (userId)));
22                List <VirtualPath> queryVP = null;
23                queryVP = VirtualPathLocalServiceUtil.dynamicQuery(query);
24               
25                if (queryVP.isEmpty()) {
26                        _log.debug("VirtualPath '"+path+
27                                "' not present for userId:"+
28                                Long.toString(userId));
29                }else{
30                    vp = queryVP.get(0);
31                    _log.debug("VirtualPath '"+vp.getVirtualPath()+"' found");
32                }
33               
34            } catch (SystemException ex) {
35                Logger.getLogger(VirtualPathImpl.class.getName()).
36                        log(Level.SEVERE, null, ex);
37            }
38            return vp;
39        }
40       
41}


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
David H Nebinger
RE: Generate static method with Liferay Service Builder
April 19, 2013 9:38 AM
Answer

David H Nebinger

Community Moderator

Rank: Liferay Legend

Posts: 11261

Join Date: September 1, 2006

Recent Posts

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.
Riccardo Rotondo
RE: Generate static method with Liferay Service Builder
April 22, 2013 1:34 AM
Answer

Riccardo Rotondo

Rank: Junior Member

Posts: 29

Join Date: June 11, 2010

Recent Posts

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
Amit Doshi
RE: Generate static method with Liferay Service Builder
April 22, 2013 2:49 AM
Answer

Amit Doshi

Rank: Liferay Master

Posts: 549

Join Date: December 29, 2010

Recent Posts

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.
David H Nebinger
RE: Generate static method with Liferay Service Builder
April 22, 2013 7:04 AM
Answer

David H Nebinger

Community Moderator

Rank: Liferay Legend

Posts: 11261

Join Date: September 1, 2006

Recent Posts

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.