Fóruns

Início » Liferay Portal » English » 3. Development

Visualização combinada Visão plana Exibição em árvore
Tópicos [ Anterior | Próximo ]
toggle
Riccardo Rotondo
Generate static method with Liferay Service Builder
19 de Abril de 2013 08:04
Resposta

Riccardo Rotondo

Ranking: Junior Member

Mensagens: 29

Data de entrada: 11 de Junho de 2010

Mensagens recentes

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
19 de Abril de 2013 09:38
Resposta

David H Nebinger

Community Moderator

Ranking: Liferay Legend

Mensagens: 11087

Data de entrada: 1 de Setembro de 2006

Mensagens recentes

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
22 de Abril de 2013 01:34
Resposta

Riccardo Rotondo

Ranking: Junior Member

Mensagens: 29

Data de entrada: 11 de Junho de 2010

Mensagens recentes

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
22 de Abril de 2013 02:49
Resposta

Amit Doshi

Ranking: Liferay Master

Mensagens: 549

Data de entrada: 29 de Dezembro de 2010

Mensagens recentes

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
22 de Abril de 2013 07:04
Resposta

David H Nebinger

Community Moderator

Ranking: Liferay Legend

Mensagens: 11087

Data de entrada: 1 de Setembro de 2006

Mensagens recentes

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.